From 62b703a80cf9c15727edb2d7c8ad61f939e00b51 Mon Sep 17 00:00:00 2001
From: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
Date: Fri, 23 Jan 2015 12:03:26 -0500
Subject: [PATCH] Fix audio only with libav

Refs #64597

Change-Id: Idc7eb4450d732258afb34dee75ec2f72b00f9997
---
 daemon/contrib/src/libav/rules.mak                  |  6 ++++++
 daemon/src/account.cpp                              |  4 ++--
 daemon/src/client/callmanager.cpp                   |  6 ++++++
 daemon/src/client/callmanager.h                     |  1 +
 daemon/src/client/videomanager.cpp                  |  5 -----
 daemon/src/client/videomanager.h                    |  1 -
 .../media/audio/audiortp/avformat_rtp_session.cpp   |  8 --------
 daemon/src/media/audio/coreaudio/corelayer.cpp      |  2 --
 daemon/src/media/libav_utils.cpp                    | 13 +++++++++++--
 daemon/src/media/libav_utils.h                      |  4 +++-
 daemon/src/sip/sdp.cpp                              |  4 ++--
 daemon/src/sip/sipaccountbase.cpp                   |  2 +-
 12 files changed, 32 insertions(+), 24 deletions(-)

diff --git a/daemon/contrib/src/libav/rules.mak b/daemon/contrib/src/libav/rules.mak
index 42962e38d1..c7ee60a325 100644
--- a/daemon/contrib/src/libav/rules.mak
+++ b/daemon/contrib/src/libav/rules.mak
@@ -18,6 +18,12 @@ LIBAVCONF = \
 #encoders
 LIBAVCONF += \
         --enable-libx264             \
+        --enable-encoder=g722        \
+        --enable-encoder=libx264     \
+        --enable-encoder=pcm_alaw    \
+        --enable-encoder=pcm_mulaw   \
+        --enable-encoder=libopus     \
+        --enable-encoder=libspeex    \
         --enable-encoder=libvpx      \
         --disable-decoder=libvpx     \
         --disable-decoder=libvpx_vp8 \
diff --git a/daemon/src/account.cpp b/daemon/src/account.cpp
index 78c014b0c9..a23a6e14cb 100644
--- a/daemon/src/account.cpp
+++ b/daemon/src/account.cpp
@@ -166,7 +166,7 @@ void Account::loadDefaultCodecs()
     setActiveAudioCodecs(result);
 #ifdef RING_VIDEO
     // we don't need to validate via setVideoCodecs, since these are defaults
-    videoCodecList_ = libav_utils::getDefaultCodecs();
+    videoCodecList_ = libav_utils::getDefaultVideoCodecs();
 #endif
 }
 
@@ -314,7 +314,7 @@ isCodecValid(const map<string, string> &codec, const vector<map<string, string>
 static bool
 isCodecListValid(const vector<map<string, string> > &list)
 {
-    const auto defaults(libav_utils::getDefaultCodecs());
+    const auto defaults(libav_utils::getDefaultVideoCodecs());
     if (list.size() != defaults.size()) {
         RING_ERR("New codec list has a different length than the list of supported codecs");
         return false;
diff --git a/daemon/src/client/callmanager.cpp b/daemon/src/client/callmanager.cpp
index 718415398a..42450ed2bf 100644
--- a/daemon/src/client/callmanager.cpp
+++ b/daemon/src/client/callmanager.cpp
@@ -32,6 +32,7 @@
 #include <cstring>
 
 #include "callmanager.h"
+#include "libav_utils.h"
 #include "call_factory.h"
 
 #include "sip/sipcall.h"
@@ -41,6 +42,11 @@
 #include "logger.h"
 #include "manager.h"
 
+CallManager::CallManager()
+{
+    libav_utils::sfl_avcodec_init();
+}
+
 void CallManager::registerEvHandlers(struct ring_call_ev_handlers* evHandlers)
 {
     evHandlers_ = *evHandlers;
diff --git a/daemon/src/client/callmanager.h b/daemon/src/client/callmanager.h
index 5990aabb86..004321a035 100644
--- a/daemon/src/client/callmanager.h
+++ b/daemon/src/client/callmanager.h
@@ -55,6 +55,7 @@ class AudioZrtpSession;
 class CallManager
 {
     public:
+        CallManager();
         void registerEvHandlers(struct ring_call_ev_handlers* evHandlers);
 
     // Methods
diff --git a/daemon/src/client/videomanager.cpp b/daemon/src/client/videomanager.cpp
index 7b9827103c..f1b30dc5aa 100644
--- a/daemon/src/client/videomanager.cpp
+++ b/daemon/src/client/videomanager.cpp
@@ -38,11 +38,6 @@
 #include "logger.h"
 #include "manager.h"
 
-VideoManager::VideoManager()
-{
-    libav_utils::sfl_avcodec_init();
-}
-
 void VideoManager::registerEvHandlers(struct ring_video_ev_handlers* evHandlers)
 {
     evHandlers_ = *evHandlers;
diff --git a/daemon/src/client/videomanager.h b/daemon/src/client/videomanager.h
index a455ae960e..c6c21f48cc 100644
--- a/daemon/src/client/videomanager.h
+++ b/daemon/src/client/videomanager.h
@@ -61,7 +61,6 @@ class VideoManager
         ring::video::VideoDeviceMonitor videoDeviceMonitor_ = {};
 
     public:
-        VideoManager();
         void registerEvHandlers(struct ring_video_ev_handlers* evHandlers);
         ring::video::VideoDeviceMonitor& getVideoDeviceMonitor();
 
diff --git a/daemon/src/media/audio/audiortp/avformat_rtp_session.cpp b/daemon/src/media/audio/audiortp/avformat_rtp_session.cpp
index 17396842c2..3f26ef5f40 100644
--- a/daemon/src/media/audio/audiortp/avformat_rtp_session.cpp
+++ b/daemon/src/media/audio/audiortp/avformat_rtp_session.cpp
@@ -72,9 +72,7 @@ class AudioSender {
         std::map<std::string, std::string> args_;
         const AudioFormat format_;
         std::unique_ptr<ring::MediaEncoder> audioEncoder_;
-#ifdef RING_VIDEO
         std::unique_ptr<ring::MediaIOHandle> muxContext_;
-#endif
         std::unique_ptr<ring::Resampler> resampler_;
         const double secondsPerPacket_ {0.02}; // 20 ms
 
@@ -110,17 +108,13 @@ AudioSender::setup(SocketPair& socketPair)
     auto dest = args_["destination"].c_str();
 
     audioEncoder_.reset(new MediaEncoder);
-#ifdef RING_VIDEO
     muxContext_.reset(socketPair.createIOContext());
-#endif // RING_VIDEO
 
     try {
         /* Encoder setup */
         audioEncoder_->setOptions(args_);
         audioEncoder_->openOutput(enc_name, "rtp", dest, NULL, false);
-#ifdef RING_VIDEO
         audioEncoder_->setIOContext(muxContext_);
-#endif // RING_VIDEO
         audioEncoder_->startIO();
     } catch (const MediaEncoderException &e) {
         RING_ERR("%s", e.what());
@@ -138,9 +132,7 @@ void
 AudioSender::cleanup()
 {
     audioEncoder_.reset();
-#ifdef RING_VIDEO
     muxContext_.reset();
-#endif // RING_VIDEO
 }
 
 void
diff --git a/daemon/src/media/audio/coreaudio/corelayer.cpp b/daemon/src/media/audio/coreaudio/corelayer.cpp
index 3deb9c8823..5cbcf25a84 100644
--- a/daemon/src/media/audio/coreaudio/corelayer.cpp
+++ b/daemon/src/media/audio/coreaudio/corelayer.cpp
@@ -429,12 +429,10 @@ void CoreLayer::write(AudioUnitRenderActionFlags* ioActionFlags,
         playbackBuff_.resize(inNumberFrames);
 
         if (tone) {
-            RING_WARN("TONE");
             tone->getNext(playbackBuff_, playbackGain_);
 
         }
         else if (file_tone) {
-            RING_WARN("FILE TONE");
             file_tone->getNext(playbackBuff_, playbackGain_);
         }
         else {
diff --git a/daemon/src/media/libav_utils.cpp b/daemon/src/media/libav_utils.cpp
index 261e58c4bd..4a22348cfa 100644
--- a/daemon/src/media/libav_utils.cpp
+++ b/daemon/src/media/libav_utils.cpp
@@ -30,6 +30,7 @@
  *  as that of the covered work.
  */
 
+#include "config.h"
 #include "libav_deps.h"
 #include "video/video_base.h"
 #include "logger.h"
@@ -43,6 +44,8 @@
 #include <exception>
 
 std::map<std::string, std::string> encoders_;
+
+#ifdef RING_VIDEO
 std::vector<std::string> installed_video_codecs_;
 
 static void
@@ -61,15 +64,18 @@ findInstalledVideoCodecs()
             RING_ERR("Didn't find \"%s\" encoder", it.second.c_str());
     }
 }
+#endif // RING_VIDEO
 
 namespace libav_utils {
 
+#ifdef RING_VIDEO
 std::vector<std::string> getVideoCodecList()
 {
     if (installed_video_codecs_.empty())
         findInstalledVideoCodecs();
     return installed_video_codecs_;
 }
+#endif // RING_VIDEO
 
 // protect libav/ffmpeg access
 static int
@@ -145,8 +151,9 @@ static void init_once()
 
     // ffmpeg doesn't know RTP format for H263 (payload type = 34)
     //encoders["H263"]          = "h263";
-
+#ifdef RING_VIDEO
     findInstalledVideoCodecs();
+#endif // RING_VIDEO
 }
 
 static std::once_flag already_called;
@@ -156,8 +163,9 @@ void sfl_avcodec_init()
     std::call_once(already_called, init_once);
 }
 
+#ifdef RING_VIDEO
 std::vector<std::map<std::string, std::string> >
-getDefaultCodecs()
+getDefaultVideoCodecs()
 {
     const char * const DEFAULT_BITRATE = "400";
     sfl_avcodec_init();
@@ -175,6 +183,7 @@ getDefaultCodecs()
     }
     return result;
 }
+#endif // RING_VIDEO
 
 int libav_pixel_format(int fmt)
 {
diff --git a/daemon/src/media/libav_utils.h b/daemon/src/media/libav_utils.h
index 2327c08615..91f17844af 100644
--- a/daemon/src/media/libav_utils.h
+++ b/daemon/src/media/libav_utils.h
@@ -46,7 +46,9 @@ namespace libav_utils {
 
     std::vector<std::string> getVideoCodecList();
 
-    std::vector<std::map <std::string, std::string> > getDefaultCodecs();
+#ifdef RING_VIDEO
+    std::vector<std::map <std::string, std::string> > getDefaultVideoCodecs();
+#endif // RING_VIDEO
 
     const char *const DEFAULT_H264_PROFILE_LEVEL_ID = "profile-level-id=428014";
     const char *const MAX_H264_PROFILE_LEVEL_ID = "profile-level-id=640034";
diff --git a/daemon/src/sip/sdp.cpp b/daemon/src/sip/sdp.cpp
index 1dbd30537b..136bef2c0e 100644
--- a/daemon/src/sip/sdp.cpp
+++ b/daemon/src/sip/sdp.cpp
@@ -1115,7 +1115,7 @@ bool Sdp::getOutgoingVideoSettings(map<string, string> &args) const
     if (not codec.empty()) {
         const string encoder(libav_utils::encodersMap()[codec]);
         if (encoder.empty()) {
-            RING_DBG("Couldn't find encoder for \"%s\"\n", codec.c_str());
+            RING_DBG("Couldn't find video encoder for \"%s\"\n", codec.c_str());
             return false;
         } else {
             args["codec"] = encoder;
@@ -1141,7 +1141,7 @@ bool Sdp::getOutgoingAudioSettings(map<string, string> &args) const
     if (not codec.empty()) {
         const string encoder(libav_utils::encodersMap()[codec]);
         if (encoder.empty()) {
-            RING_DBG("Couldn't find encoder for \"%s\"\n", codec.c_str());
+            RING_DBG("Couldn't find audio encoder for \"%s\"\n", codec.c_str());
             return false;
         } else {
             args["codec"] = encoder;
diff --git a/daemon/src/sip/sipaccountbase.cpp b/daemon/src/sip/sipaccountbase.cpp
index 91a991c4de..36b07ee313 100644
--- a/daemon/src/sip/sipaccountbase.cpp
+++ b/daemon/src/sip/sipaccountbase.cpp
@@ -133,7 +133,7 @@ void SIPAccountBase::unserialize(const YAML::Node &node)
     if (tmp.empty()) {
         // Video codecs are an empty list
         RING_WARN("Loading default video codecs");
-        tmp = libav_utils::getDefaultCodecs();
+        tmp = libav_utils::getDefaultVideoCodecs();
     }
 #endif
     // validate it
-- 
GitLab