commit:     1c91884873968997be4b0c954169d04dc839f1db
Author:     Stefan Strogin <steils <AT> gentoo <DOT> org>
AuthorDate: Thu Jun  3 02:07:03 2021 +0000
Commit:     Stefan Strogin <steils <AT> gentoo <DOT> org>
CommitDate: Thu Jun  3 02:11:10 2021 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=1c918848

net-im/telegram-desktop: add patch to fix crash on voice recording

- Don't crash on voice recording error.
- Fix voice recoding with FFmpeg 4.4.

See also: https://github.com/telegramdesktop/tdesktop/issues/16217

Package-Manager: Portage-3.0.19, Repoman-3.0.3
Signed-off-by: Stefan Strogin <steils <AT> gentoo.org>

 .../files/tdesktop-2.7.4-voice-crash.patch         | 262 +++++++++++++++++++++
 .../files/tdesktop-2.7.4-voice-ffmpeg44.patch      |  25 ++
 .../telegram-desktop-2.7.4-r1.ebuild               | 144 +++++++++++
 3 files changed, 431 insertions(+)

diff --git a/net-im/telegram-desktop/files/tdesktop-2.7.4-voice-crash.patch 
b/net-im/telegram-desktop/files/tdesktop-2.7.4-voice-crash.patch
new file mode 100644
index 00000000000..41e447989cb
--- /dev/null
+++ b/net-im/telegram-desktop/files/tdesktop-2.7.4-voice-crash.patch
@@ -0,0 +1,262 @@
+From 71deaa48afab2bcf9df67b9b347b1f44aad3a9ce Mon Sep 17 00:00:00 2001
+From: John Preston <[email protected]>
+Date: Thu, 13 May 2021 15:17:54 +0400
+Subject: [PATCH] Don't crash on voice recording error.
+
+Fixes #16217.
+---
+ .../media/audio/media_audio_capture.cpp       | 89 +++++++++++--------
+ 1 file changed, 54 insertions(+), 35 deletions(-)
+
+diff --git a/Telegram/SourceFiles/media/audio/media_audio_capture.cpp 
b/Telegram/SourceFiles/media/audio/media_audio_capture.cpp
+index a5965e0d1988..deb2474d7891 100644
+--- a/Telegram/SourceFiles/media/audio/media_audio_capture.cpp
++++ b/Telegram/SourceFiles/media/audio/media_audio_capture.cpp
+@@ -47,17 +47,17 @@ class Instance::Inner final : public QObject {
+       void start(Fn<void(Update)> updated, Fn<void()> error);
+       void stop(Fn<void(Result&&)> callback = nullptr);
+ 
+-      void timeout();
+-
+ private:
+-      void processFrame(int32 offset, int32 framesize);
++      void process();
++
++      [[nodiscard]] bool processFrame(int32 offset, int32 framesize);
+       void fail();
+ 
+-      void writeFrame(AVFrame *frame);
++      [[nodiscard]] bool writeFrame(AVFrame *frame);
+ 
+       // Writes the packets till EAGAIN is got from av_receive_packet()
+       // Returns number of packets written or -1 on error
+-      int writePackets();
++      [[nodiscard]] int writePackets();
+ 
+       Fn<void(Update)> _updated;
+       Fn<void()> _error;
+@@ -150,6 +150,7 @@ struct Instance::Inner::Private {
+       AVCodec *codec = nullptr;
+       AVCodecContext *codecContext = nullptr;
+       bool opened = false;
++      bool processing = false;
+ 
+       int srcSamples = 0;
+       int dstSamples = 0;
+@@ -217,7 +218,7 @@ struct Instance::Inner::Private {
+ 
+ Instance::Inner::Inner(QThread *thread)
+ : d(std::make_unique<Private>())
+-, _timer(thread, [=] { timeout(); }) {
++, _timer(thread, [=] { process(); }) {
+       moveToThread(thread);
+ }
+ 
+@@ -226,10 +227,10 @@ Instance::Inner::~Inner() {
+ }
+ 
+ void Instance::Inner::fail() {
+-      Expects(_error != nullptr);
+-
+       stop();
+-      _error();
++      if (const auto error = base::take(_error)) {
++              InvokeQueued(this, error);
++      }
+ }
+ 
+ void Instance::Inner::start(Fn<void(Update)> updated, Fn<void()> error) {
+@@ -384,13 +385,21 @@ void Instance::Inner::stop(Fn<void(Result&&)> callback) {
+       }
+       _timer.cancel();
+ 
+-      if (d->device) {
++      const auto needResult = (callback != nullptr);
++      const auto hadDevice = (d->device != nullptr);
++      if (hadDevice) {
+               alcCaptureStop(d->device);
+-              timeout(); // get last data
++              if (d->processing) {
++                      Assert(!needResult); // stop in the middle of 
processing - error.
++              } else {
++                      process(); // get last data
++              }
++              alcCaptureCloseDevice(d->device);
++              d->device = nullptr;
+       }
+ 
+       // Write what is left
+-      if (!_captured.isEmpty()) {
++      if (needResult && !_captured.isEmpty()) {
+               auto fadeSamples = kCaptureFadeInDuration * kCaptureFrequency / 
1000;
+               auto capturedSamples = static_cast<int>(_captured.size() / 
sizeof(short));
+               if ((_captured.size() % sizeof(short)) || (d->fullSamples + 
capturedSamples < kCaptureFrequency) || (capturedSamples < fadeSamples)) {
+@@ -414,11 +423,13 @@ void Instance::Inner::stop(Fn<void(Result&&)> callback) {
+ 
+                       int32 framesize = d->srcSamples * 
d->codecContext->channels * sizeof(short), encoded = 0;
+                       while (_captured.size() >= encoded + framesize) {
+-                              processFrame(encoded, framesize);
++                              if (!processFrame(encoded, framesize)) {
++                                      break;
++                              }
+                               encoded += framesize;
+                       }
+-                      writeFrame(nullptr); // drain the codec
+-                      if (encoded != _captured.size()) {
++                      // Drain the codec.
++                      if (!writeFrame(nullptr) || encoded != 
_captured.size()) {
+                               d->fullSamples = 0;
+                               d->dataPos = 0;
+                               d->data.clear();
+@@ -436,14 +447,14 @@ void Instance::Inner::stop(Fn<void(Result&&)> callback) {
+       _captured = QByteArray();
+ 
+       // Finish stream
+-      if (d->device) {
++      if (needResult && hadDevice) {
+               av_write_trailer(d->fmtContext);
+       }
+ 
+       QByteArray result = d->fullSamples ? d->data : QByteArray();
+       VoiceWaveform waveform;
+       qint32 samples = d->fullSamples;
+-      if (samples && !d->waveform.isEmpty()) {
++      if (needResult && samples && !d->waveform.isEmpty()) {
+               int64 count = d->waveform.size(), sum = 0;
+               if (count >= Player::kWaveformSamplesCount) {
+                       QVector<uint16> peaks;
+@@ -472,11 +483,7 @@ void Instance::Inner::stop(Fn<void(Result&&)> callback) {
+                       }
+               }
+       }
+-      if (d->device) {
+-              alcCaptureStop(d->device);
+-              alcCaptureCloseDevice(d->device);
+-              d->device = nullptr;
+-
++      if (hadDevice) {
+               if (d->codecContext) {
+                       avcodec_free_context(&d->codecContext);
+                       d->codecContext = nullptr;
+@@ -528,12 +535,17 @@ void Instance::Inner::stop(Fn<void(Result&&)> callback) {
+               d->waveform.clear();
+       }
+ 
+-      if (callback) {
++      if (needResult) {
+               callback({ result, waveform, samples });
+       }
+ }
+ 
+-void Instance::Inner::timeout() {
++void Instance::Inner::process() {
++      Expects(!d->processing);
++
++      d->processing = true;
++      const auto guard = gsl::finally([&] { d->processing = false; });
++
+       if (!d->device) {
+               _timer.cancel();
+               return;
+@@ -582,7 +594,9 @@ void Instance::Inner::timeout() {
+               // Write frames
+               int32 framesize = d->srcSamples * d->codecContext->channels * 
sizeof(short), encoded = 0;
+               while (uint32(_captured.size()) >= encoded + framesize + 
fadeSamples * sizeof(short)) {
+-                      processFrame(encoded, framesize);
++                      if (!processFrame(encoded, framesize)) {
++                              return;
++                      }
+                       encoded += framesize;
+               }
+ 
+@@ -597,13 +611,13 @@ void Instance::Inner::timeout() {
+       }
+ }
+ 
+-void Instance::Inner::processFrame(int32 offset, int32 framesize) {
++bool Instance::Inner::processFrame(int32 offset, int32 framesize) {
+       // Prepare audio frame
+ 
+       if (framesize % sizeof(short)) { // in the middle of a sample
+               LOG(("Audio Error: Bad framesize in writeFrame() for capture, 
framesize %1, %2").arg(framesize));
+               fail();
+-              return;
++              return false;
+       }
+       auto samplesCnt = static_cast<int>(framesize / sizeof(short));
+ 
+@@ -650,7 +664,7 @@ void Instance::Inner::processFrame(int32 offset, int32 
framesize) {
+               if ((res = av_samples_alloc(d->dstSamplesData, 0, 
d->codecContext->channels, d->dstSamples, d->codecContext->sample_fmt, 1)) < 0) 
{
+                       LOG(("Audio Error: Unable to av_samples_alloc for 
capture, error %1, %2").arg(res).arg(av_make_error_string(err, sizeof(err), 
res)));
+                       fail();
+-                      return;
++                      return false;
+               }
+               d->dstSamplesSize = av_samples_get_buffer_size(0, 
d->codecContext->channels, d->maxDstSamples, d->codecContext->sample_fmt, 0);
+       }
+@@ -658,7 +672,7 @@ void Instance::Inner::processFrame(int32 offset, int32 
framesize) {
+       if ((res = swr_convert(d->swrContext, d->dstSamplesData, d->dstSamples, 
(const uint8_t **)srcSamplesData, d->srcSamples)) < 0) {
+               LOG(("Audio Error: Unable to swr_convert for capture, error %1, 
%2").arg(res).arg(av_make_error_string(err, sizeof(err), res)));
+               fail();
+-              return;
++              return false;
+       }
+ 
+       // Write audio frame
+@@ -670,45 +684,50 @@ void Instance::Inner::processFrame(int32 offset, int32 
framesize) {
+ 
+       avcodec_fill_audio_frame(frame, d->codecContext->channels, 
d->codecContext->sample_fmt, d->dstSamplesData[0], d->dstSamplesSize, 0);
+ 
+-      writeFrame(frame);
++      if (!writeFrame(frame)) {
++              return false;
++      }
+ 
+       d->fullSamples += samplesCnt;
+ 
+       av_frame_free(&frame);
++      return true;
+ }
+ 
+-void Instance::Inner::writeFrame(AVFrame *frame) {
++bool Instance::Inner::writeFrame(AVFrame *frame) {
+       int res = 0;
+       char err[AV_ERROR_MAX_STRING_SIZE] = { 0 };
+ 
+       res = avcodec_send_frame(d->codecContext, frame);
+       if (res == AVERROR(EAGAIN)) {
+-              int packetsWritten = writePackets();
++              const auto packetsWritten = writePackets();
+               if (packetsWritten < 0) {
+                       if (frame && packetsWritten == AVERROR_EOF) {
+                               LOG(("Audio Error: EOF in packets received when 
EAGAIN was got in avcodec_send_frame()"));
+                               fail();
+                       }
+-                      return;
++                      return false;
+               } else if (!packetsWritten) {
+                       LOG(("Audio Error: No packets received when EAGAIN was 
got in avcodec_send_frame()"));
+                       fail();
+-                      return;
++                      return false;
+               }
+               res = avcodec_send_frame(d->codecContext, frame);
+       }
+       if (res < 0) {
+               LOG(("Audio Error: Unable to avcodec_send_frame for capture, 
error %1, %2").arg(res).arg(av_make_error_string(err, sizeof(err), res)));
+               fail();
+-              return;
++              return false;
+       }
+ 
+       if (!frame) { // drain
+               if ((res = writePackets()) != AVERROR_EOF) {
+                       LOG(("Audio Error: not EOF in packets received when 
draining the codec, result %1").arg(res));
+                       fail();
++                      return false;
+               }
+       }
++      return true;
+ }
+ 
+ int Instance::Inner::writePackets() {

diff --git a/net-im/telegram-desktop/files/tdesktop-2.7.4-voice-ffmpeg44.patch 
b/net-im/telegram-desktop/files/tdesktop-2.7.4-voice-ffmpeg44.patch
new file mode 100644
index 00000000000..4156956032d
--- /dev/null
+++ b/net-im/telegram-desktop/files/tdesktop-2.7.4-voice-ffmpeg44.patch
@@ -0,0 +1,25 @@
+From a8807bc915f2439acc7c84f06d931d96d6ca602a Mon Sep 17 00:00:00 2001
+From: John Preston <[email protected]>
+Date: Thu, 13 May 2021 15:33:42 +0400
+Subject: [PATCH] Fix voice recoding with FFmpeg 4.4.
+
+Fixes #16217.
+---
+ Telegram/SourceFiles/media/audio/media_audio_capture.cpp | 4 ++++
+ 1 file changed, 4 insertions(+)
+
+diff --git a/Telegram/SourceFiles/media/audio/media_audio_capture.cpp 
b/Telegram/SourceFiles/media/audio/media_audio_capture.cpp
+index deb2474d789..d129168783d 100644
+--- a/Telegram/SourceFiles/media/audio/media_audio_capture.cpp
++++ b/Telegram/SourceFiles/media/audio/media_audio_capture.cpp
+@@ -679,6 +679,10 @@ bool Instance::Inner::processFrame(int32 offset, int32 
framesize) {
+ 
+       AVFrame *frame = av_frame_alloc();
+ 
++      frame->format = d->codecContext->sample_fmt;
++      frame->channels = d->codecContext->channels;
++      frame->channel_layout = d->codecContext->channel_layout;
++      frame->sample_rate = d->codecContext->sample_rate;
+       frame->nb_samples = d->dstSamples;
+       frame->pts = av_rescale_q(d->fullSamples, AVRational { 1, 
d->codecContext->sample_rate }, d->codecContext->time_base);
+ 

diff --git a/net-im/telegram-desktop/telegram-desktop-2.7.4-r1.ebuild 
b/net-im/telegram-desktop/telegram-desktop-2.7.4-r1.ebuild
new file mode 100644
index 00000000000..095e7419b7c
--- /dev/null
+++ b/net-im/telegram-desktop/telegram-desktop-2.7.4-r1.ebuild
@@ -0,0 +1,144 @@
+# Copyright 2020-2021 Gentoo Authors
+# Distributed under the terms of the GNU General Public License v2
+
+EAPI=7
+
+PYTHON_COMPAT=( python3_{7,8,9} )
+
+inherit xdg cmake python-any-r1 flag-o-matic
+
+MY_P="tdesktop-${PV}-full"
+
+DESCRIPTION="Official desktop client for Telegram"
+HOMEPAGE="https://desktop.telegram.org";
+SRC_URI="https://github.com/telegramdesktop/tdesktop/releases/download/v${PV}/${MY_P}.tar.gz";
+
+LICENSE="BSD GPL-3-with-openssl-exception LGPL-2+"
+SLOT="0"
+KEYWORDS="~amd64 ~ppc64"
+IUSE="+dbus enchant +gtk +hunspell +spell wayland webkit +X"
+
+RDEPEND="
+       !net-im/telegram-desktop-bin
+       app-arch/lz4:=
+       dev-cpp/glibmm:2
+       dev-libs/xxhash
+       dev-qt/qtcore:5
+       dev-qt/qtgui:5[dbus?,jpeg,png,wayland?,X(-)?]
+       dev-qt/qtimageformats:5
+       dev-qt/qtnetwork:5[ssl]
+       dev-qt/qtsvg:5
+       dev-qt/qtwidgets:5[png,X(-)?]
+       media-fonts/open-sans
+       media-libs/fontconfig:=
+       media-libs/opus:=
+       ~media-libs/libtgvoip-2.4.4_p20210302
+       media-libs/openal
+       ~media-libs/tg_owt-0_pre20210422
+       media-video/ffmpeg:=[opus]
+       sys-libs/zlib:=[minizip]
+       dbus? (
+               dev-qt/qtdbus:5
+               dev-libs/libdbusmenu-qt[qt5(+)]
+       )
+       enchant? ( app-text/enchant:= )
+       gtk? ( x11-libs/gtk+:3[X?] )
+       hunspell? ( >=app-text/hunspell-1.7:= )
+       wayland? ( kde-frameworks/kwayland:= )
+       webkit? ( net-libs/webkit-gtk:= )
+       X? ( x11-libs/libxcb:= )
+"
+DEPEND="${RDEPEND}
+       dev-cpp/range-v3
+       =dev-cpp/ms-gsl-3*
+"
+BDEPEND="
+       ${PYTHON_DEPS}
+       >=dev-util/cmake-3.16
+       virtual/pkgconfig
+"
+REQUIRED_USE="
+       spell? (
+               ^^ ( enchant hunspell )
+       )
+"
+
+S="${WORKDIR}/${MY_P}"
+
+PATCHES=(
+       # https://github.com/desktop-app/cmake_helpers/pull/91
+       # https://github.com/desktop-app/lib_webview/pull/2
+       "${FILESDIR}/tdesktop-2.7.3-disable-webkit-separately.patch"
+       # 
https://github.com/desktop-app/lib_webview/commit/0b4100d7cecc4e748c51f3f51ebfd1392ec3978a
+       "${FILESDIR}/tdesktop-2.7.3-webview-include-gdkx.patch"
+       # https://github.com/desktop-app/lib_webview/pull/3
+       "${FILESDIR}/tdesktop-2.7.4-webview-fix-gcc11.patch"
+
+       # https://github.com/telegramdesktop/tdesktop/issues/16217
+       "${FILESDIR}/tdesktop-2.7.4-voice-crash.patch"
+       "${FILESDIR}/tdesktop-2.7.4-voice-ffmpeg44.patch"
+)
+
+pkg_pretend() {
+       if has ccache ${FEATURES}; then
+               ewarn
+               ewarn "ccache does not work with ${PN} out of the box"
+               ewarn "due to usage of precompiled headers"
+               ewarn "check bug https://bugs.gentoo.org/715114 for more info"
+               ewarn
+       fi
+}
+
+src_prepare() {
+       # no explicit toggle, doesn't build with the system one #752417
+       sed -i 's/DESKTOP_APP_USE_PACKAGED/NO_ONE_WILL_EVER_SET_THIS/' \
+               cmake/external/rlottie/CMakeLists.txt || die
+
+       cmake_src_prepare
+}
+
+src_configure() {
+       # gtk is really needed for image copy-paste due to 
https://bugreports.qt.io/browse/QTBUG-56595
+       local mycmakeargs=(
+               -DTDESKTOP_LAUNCHER_BASENAME="${PN}"
+               -DCMAKE_DISABLE_FIND_PACKAGE_tl-expected=ON  # header only lib, 
some git version. prevents warnings.
+
+               -DDESKTOP_APP_DISABLE_X11_INTEGRATION=$(usex X OFF ON)
+               -DDESKTOP_APP_DISABLE_WAYLAND_INTEGRATION=$(usex wayland OFF ON)
+               -DDESKTOP_APP_DISABLE_DBUS_INTEGRATION=$(usex dbus OFF ON)
+               -DDESKTOP_APP_DISABLE_GTK_INTEGRATION=$(usex gtk OFF ON)
+               -DDESKTOP_APP_DISABLE_WEBKIT_INTEGRATION=$(usex webkit OFF ON)
+               -DDESKTOP_APP_DISABLE_SPELLCHECK=$(usex spell OFF ON)  # 
enables hunspell (recommended)
+               -DDESKTOP_APP_USE_ENCHANT=$(usex enchant ON OFF)  # enables 
enchant and disables hunspell
+       )
+
+       if [[ -n ${MY_TDESKTOP_API_ID} && -n ${MY_TDESKTOP_API_HASH} ]]; then
+               einfo "Found custom API credentials"
+               mycmakeargs+=(
+                       -DTDESKTOP_API_ID="${MY_TDESKTOP_API_ID}"
+                       -DTDESKTOP_API_HASH="${MY_TDESKTOP_API_HASH}"
+               )
+       else
+               # 
https://github.com/telegramdesktop/tdesktop/blob/dev/snap/snapcraft.yaml
+               # Building with snapcraft API credentials by default
+               # Custom API credentials can be obtained here:
+               # 
https://github.com/telegramdesktop/tdesktop/blob/dev/docs/api_credentials.md
+               # After getting credentials you can export variables:
+               #  export MY_TDESKTOP_API_ID="17349""
+               #  export 
MY_TDESKTOP_API_HASH="344583e45741c457fe1862106095a5eb"
+               # and restart the build"
+               # you can set above variables (without export) in 
/etc/portage/env/net-im/telegram-desktop
+               # portage will use custom variable every build automatically
+               mycmakeargs+=(
+                       -DTDESKTOP_API_ID="611335"
+                       -DTDESKTOP_API_HASH="d524b414d21f4d37f08684c1df41ac9c"
+               )
+       fi
+
+       cmake_src_configure
+}
+
+pkg_postinst() {
+       xdg_pkg_postinst
+       use gtk || elog "enable 'gtk' useflag if you have image copy-paste 
problems"
+}

Reply via email to