From b99f1a0bd62e00791d43a01f08181eca2c73cdf7 Mon Sep 17 00:00:00 2001
From: Vladimir Stoiakin <VStoiakin@lavabit.com>
Date: Wed, 24 Jun 2020 18:34:28 +0300
Subject: [PATCH] build: fix some issues for building without video

Change-Id: I9d1d1a53070228b93728655021cdf1764b5eb6b2
Gitlab: #238
---
 meson.build                 |  7 +++++++
 src/client/ring_signal.cpp  |  2 +-
 src/media/media_decoder.cpp | 23 +++++++++++++++--------
 src/media/media_decoder.h   |  1 +
 src/media/media_encoder.cpp | 15 ++++++++++-----
 src/media/media_filter.cpp  |  2 +-
 src/meson.build             |  4 ++--
 7 files changed, 37 insertions(+), 17 deletions(-)

diff --git a/meson.build b/meson.build
index 7f2c150af0..c6da345ddb 100644
--- a/meson.build
+++ b/meson.build
@@ -113,7 +113,14 @@ if get_option('video')
     if get_option('hw_acceleration')
         conf.set('RING_ACCEL', true)
         conf.set('ENABLE_VIDEOTOOLBOX', host_machine.system() == 'darwin')
+    else
+        conf.set('RING_ACCEL', false)
+        conf.set('ENABLE_VIDEOTOOLBOX', false)
     endif
+else
+    conf.set('ENABLE_VIDEO', false)
+    conf.set('RING_ACCEL', false)
+    conf.set('ENABLE_VIDEOTOOLBOX', false)
 endif
 
 if get_option('plugins')
diff --git a/src/client/ring_signal.cpp b/src/client/ring_signal.cpp
index 94a85cbeaf..f3ed3752a7 100644
--- a/src/client/ring_signal.cpp
+++ b/src/client/ring_signal.cpp
@@ -102,10 +102,10 @@ getSignalHandlers()
         /* DataTransfer */
         exported_callback<DRing::DataTransferSignal::DataTransferEvent>(),
 
+#ifdef ENABLE_VIDEO
         /* MediaPlayer */
         exported_callback<DRing::MediaPlayerSignal::FileOpened>(),
 
-#ifdef ENABLE_VIDEO
         /* Video */
         exported_callback<DRing::VideoSignal::DeviceEvent>(),
         exported_callback<DRing::VideoSignal::DecodingStarted>(),
diff --git a/src/media/media_decoder.cpp b/src/media/media_decoder.cpp
index 0328470079..726aca8ff4 100644
--- a/src/media/media_decoder.cpp
+++ b/src/media/media_decoder.cpp
@@ -467,10 +467,14 @@ MediaDecoder::setupStream()
         JAMI_DBG() << "Using framerate emulation";
     startTime_ = av_gettime(); // used to set pts after decoding, and for rate emulation
 
+#ifdef RING_ACCEL
     if(!accel_) {
         JAMI_WARN("Not using hardware decoding for %s",  avcodec_get_name(decoderCtx_->codec_id));
         ret = avcodec_open2(decoderCtx_, inputDecoder_, nullptr);
     }
+#else
+    ret = avcodec_open2(decoderCtx_, inputDecoder_, nullptr);
+#endif
     if (ret < 0) {
         JAMI_ERR() << "Could not open codec: " << libav_utils::getError(ret);
         return -1;
@@ -516,14 +520,17 @@ MediaDecoder::decode(AVPacket& packet)
 {
     int frameFinished = 0;
     auto ret = avcodec_send_packet(decoderCtx_, &packet);
-    if ( accel_ && (ret < 0 && ret != AVERROR(EAGAIN)) ) {
-        JAMI_WARN("Decoding error falling back to software");
-        fallback_ = true;
-        accel_.reset();
-        avcodec_flush_buffers(decoderCtx_);
-        setupStream();
-        return DecodeStatus::FallBack;
-    } else if (ret < 0 && ret != AVERROR(EAGAIN)) {
+    if (ret < 0 && ret != AVERROR(EAGAIN)) {
+#ifdef RING_ACCEL
+        if (accel_) {
+            JAMI_WARN("Decoding error falling back to software");
+            fallback_ = true;
+            accel_.reset();
+            avcodec_flush_buffers(decoderCtx_);
+            setupStream();
+            return DecodeStatus::FallBack;
+        } else
+#endif
         return ret == AVERROR_EOF ? DecodeStatus::Success : DecodeStatus::DecodeError;
     }
 
diff --git a/src/media/media_decoder.h b/src/media/media_decoder.h
index 48edd213f0..dd111b2f28 100644
--- a/src/media/media_decoder.h
+++ b/src/media/media_decoder.h
@@ -62,6 +62,7 @@ class AudioFrame;
 namespace jami {
 
 using AudioFrame = DRing::AudioFrame;
+using VideoFrame = DRing::VideoFrame;
 struct AudioFormat;
 class RingBuffer;
 class Resampler;
diff --git a/src/media/media_encoder.cpp b/src/media/media_encoder.cpp
index c725e49f82..fe1ea7e4b4 100644
--- a/src/media/media_encoder.cpp
+++ b/src/media/media_encoder.cpp
@@ -602,6 +602,7 @@ MediaEncoder::prepareEncoderContext(AVCodec* outputCodec, bool is_video)
 void
 MediaEncoder::forcePresetX2645(AVCodecContext* encoderCtx)
 {
+#ifdef RING_ACCEL
     if(accel_ && accel_->getName() == "nvenc") {
         if (av_opt_set(encoderCtx, "preset", "fast", AV_OPT_SEARCH_CHILDREN))
             JAMI_WARN("Failed to set preset to 'fast'");
@@ -609,8 +610,9 @@ MediaEncoder::forcePresetX2645(AVCodecContext* encoderCtx)
             JAMI_WARN("Failed to set level to 'auto'");
         if (av_opt_set_int(encoderCtx, "zerolatency", 1, AV_OPT_SEARCH_CHILDREN))
             JAMI_WARN("Failed to set zerolatency to '1'");
-    }
-    else {
+    } else
+#endif
+    {
 #if (defined(TARGET_OS_IOS) && TARGET_OS_IOS)
         const char *speedPreset = "ultrafast";
 #else
@@ -965,12 +967,15 @@ MediaEncoder::stopEncoder()
 bool
 MediaEncoder::isDynBitrateSupported(AVCodecID codecid)
 {
+#ifdef RING_ACCEL
     if (accel_) {
         return accel_->dynBitrate();
-    } else {
-        if (codecid != AV_CODEC_ID_VP8)
-            return true;
     }
+#endif
+    if (codecid != AV_CODEC_ID_VP8)
+        return true;
+
+    return false;
 }
 
 void
diff --git a/src/media/media_filter.cpp b/src/media/media_filter.cpp
index 947bd8489a..406cc50c0f 100644
--- a/src/media/media_filter.cpp
+++ b/src/media/media_filter.cpp
@@ -200,7 +200,7 @@ MediaFilter::readOutput()
     std::unique_ptr<MediaFrame> frame;
     switch (av_buffersink_get_type(output_)) {
     case AVMEDIA_TYPE_VIDEO:
-        frame = std::make_unique<VideoFrame>();
+        frame = std::make_unique<DRing::VideoFrame>();
         break;
     case AVMEDIA_TYPE_AUDIO:
         frame = std::make_unique<AudioFrame>();
diff --git a/src/meson.build b/src/meson.build
index 8bc9d743ed..4225de7e25 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -11,12 +11,10 @@ libjami_sources = files(
     'client/datatransfer.cpp',
     'client/presencemanager.cpp',
     'client/ring_signal.cpp',
-    'client/videomanager.cpp',
     'config/yamlparser.cpp',
     'hooks/urlhook.cpp',
     'im/instant_messaging.cpp',
     'im/message_engine.cpp',
-    'jamidht/channeled_transfers.cpp',
     'jamidht/eth/libdevcore/Common.cpp',
     'jamidht/eth/libdevcore/CommonData.cpp',
     'jamidht/eth/libdevcore/FixedHash.cpp',
@@ -25,6 +23,7 @@ libjami_sources = files(
     'jamidht/accountarchive.cpp',
     'jamidht/account_manager.cpp',
     'jamidht/archive_account_manager.cpp',
+    'jamidht/channeled_transfers.cpp',
     'jamidht/channeled_transport.cpp',
     'jamidht/connectionmanager.cpp',
     'jamidht/contact_list.cpp',
@@ -205,6 +204,7 @@ endif
 
 if conf.get('ENABLE_VIDEO')
     libjami_sources += files(
+        'client/videomanager.cpp',
         'media/video/filter_transpose.cpp',
         'media/video/sinkclient.cpp',
         'media/video/video_base.cpp',
-- 
GitLab