From 79354e7c2b2a1df7889986ea2e73eb1d0aba32f6 Mon Sep 17 00:00:00 2001
From: Pierre Lespagnol <pierre.lespagnol@savoirfairelinux.com>
Date: Thu, 11 Jul 2019 15:20:35 -0400
Subject: [PATCH] account: enable codecs by default

Enables Opus, H264 and VP8 for Jami accounts, and all codecs for SIP
accounts.

Change-Id: Ie7b7773d50afa52ddb2adf917eaee8c4dd6af07f
---
 src/account.cpp                     | 18 +++++++++---------
 src/account.h                       |  3 ++-
 src/client/configurationmanager.cpp |  3 +--
 src/jamidht/jamiaccount.cpp         | 15 +++++++++++++++
 src/jamidht/jamiaccount.h           |  2 ++
 src/manager.cpp                     |  5 ++---
 src/media/media_codec.h             |  2 +-
 src/sip/sipaccount.cpp              | 15 +++++++++++++++
 src/sip/sipaccount.h                |  2 ++
 9 files changed, 49 insertions(+), 16 deletions(-)

diff --git a/src/account.cpp b/src/account.cpp
index 4111ad595b..bcbcdbff8a 100644
--- a/src/account.cpp
+++ b/src/account.cpp
@@ -360,15 +360,6 @@ Account::setActiveCodecs(const std::vector<unsigned>& list)
                  const std::shared_ptr<AccountCodecInfo>& b) {
                   return a->order < b->order;
               });
-
-    if (!hasActiveCodec(MEDIA_AUDIO)) {
-        JAMI_WARN("All audio codecs disabled, enabling all");
-        setAllCodecsActive(MEDIA_AUDIO, true);
-    }
-    if (!hasActiveCodec(MEDIA_VIDEO)) {
-        JAMI_WARN("All video codecs disabled, enabling all");
-        setAllCodecsActive(MEDIA_VIDEO, true);
-    }
 }
 
 std::string
@@ -563,6 +554,15 @@ Account::setAllCodecsActive(MediaType mediaType, bool active)
     }
 }
 
+void
+Account::setCodecActive(unsigned codecId)
+{
+    for (auto& codecIt: accountCodecInfoList_) {
+        if (codecIt->systemCodecInfo.avcodecId == codecId)
+            codecIt->isActive = true;
+    }
+}
+
 std::vector<std::shared_ptr<AccountCodecInfo>>
 Account::getActiveAccountCodecInfoList(MediaType mediaType) const
 {
diff --git a/src/account.h b/src/account.h
index 575da296b2..97a6d9b0c7 100644
--- a/src/account.h
+++ b/src/account.h
@@ -248,7 +248,7 @@ class Account : public Serializable, public std::enable_shared_from_this<Account
          * Update both the codec order structure and the codec string used for
          * SDP offer and configuration respectively
          */
-        void setActiveCodecs(const std::vector<unsigned>& list);
+        virtual void setActiveCodecs(const std::vector<unsigned>& list);
         std::shared_ptr<AccountCodecInfo> searchCodecById(unsigned codecId, MediaType mediaType);
         std::vector<std::shared_ptr<AccountCodecInfo>> getActiveAccountCodecInfoList(MediaType mediaType) const;
         std::shared_ptr<AccountCodecInfo> searchCodecByPayload(unsigned payload, MediaType mediaType);
@@ -493,6 +493,7 @@ class Account : public Serializable, public std::enable_shared_from_this<Account
         std::shared_ptr<AccountCodecInfo> searchCodecByName(const std::string& name, MediaType mediaType);
         std::vector<unsigned> getAccountCodecInfoIdList(MediaType mediaType) const;
         void setAllCodecsActive(MediaType mediaType, bool active);
+        void setCodecActive(unsigned codecId);
 };
 
 static inline std::ostream&
diff --git a/src/client/configurationmanager.cpp b/src/client/configurationmanager.cpp
index 0ecbddaf78..de861aac79 100644
--- a/src/client/configurationmanager.cpp
+++ b/src/client/configurationmanager.cpp
@@ -570,8 +570,7 @@ getActiveCodecList(const std::string& accountID)
 }
 
 void
-setActiveCodecList(const std::string& accountID
-        , const std::vector<unsigned>& list)
+setActiveCodecList(const std::string& accountID, const std::vector<unsigned>& list)
 {
     if (auto acc = jami::Manager::instance().getAccount(accountID))
     {
diff --git a/src/jamidht/jamiaccount.cpp b/src/jamidht/jamiaccount.cpp
index 7c798d75d4..72c2f509d6 100644
--- a/src/jamidht/jamiaccount.cpp
+++ b/src/jamidht/jamiaccount.cpp
@@ -313,6 +313,8 @@ JamiAccount::JamiAccount(const std::string& accountID, bool /* presenceEnabled *
     std::ifstream proxyCache(cachePath_ + DIR_SEPARATOR_STR "dhtproxy");
     if (proxyCache)
       std::getline(proxyCache, proxyServerCached_);
+
+    setActiveCodecs({});
 }
 
 JamiAccount::~JamiAccount()
@@ -3744,4 +3746,17 @@ JamiAccount::getNearbyPeers() const
     return discoveredPeerMap_;
 }
 
+
+void
+JamiAccount::setActiveCodecs(const std::vector<unsigned>& list)
+{
+    Account::setActiveCodecs(list);
+    if (!hasActiveCodec(MEDIA_AUDIO))
+        setCodecActive(AV_CODEC_ID_OPUS);
+    if (!hasActiveCodec(MEDIA_VIDEO)) {
+        setCodecActive(AV_CODEC_ID_H264);
+        setCodecActive(AV_CODEC_ID_VP8);
+    }
+}
+
 } // namespace jami
diff --git a/src/jamidht/jamiaccount.h b/src/jamidht/jamiaccount.h
index 2edb7ed07a..e21208840b 100644
--- a/src/jamidht/jamiaccount.h
+++ b/src/jamidht/jamiaccount.h
@@ -162,6 +162,8 @@ class JamiAccount : public SIPAccountBase {
          */
         std::map<std::string, bool> getTrackedBuddyPresence() const;
 
+        void setActiveCodecs(const std::vector<unsigned>& list) override;
+
         /**
          * Connect to the DHT.
          */
diff --git a/src/manager.cpp b/src/manager.cpp
index 879473294d..8d0c00fa62 100644
--- a/src/manager.cpp
+++ b/src/manager.cpp
@@ -2754,11 +2754,10 @@ Manager::addAccount(const std::map<std::string, std::string>& details, const std
     }
 
     newAccount->setAccountDetails(details);
-
-    preferences.addAccount(newAccountID);
-
+    saveConfig(newAccount);
     newAccount->doRegister();
 
+    preferences.addAccount(newAccountID);
     saveConfig();
 
     emitSignal<DRing::ConfigurationSignal::AccountsChanged>();
diff --git a/src/media/media_codec.h b/src/media/media_codec.h
index b5158acdd9..4ce0998374 100644
--- a/src/media/media_codec.h
+++ b/src/media/media_codec.h
@@ -159,7 +159,7 @@ struct AccountCodecInfo
 
     const SystemCodecInfo& systemCodecInfo;
     unsigned order {0}; /*used to define preferred codec list order in UI*/
-    bool isActive {true};
+    bool isActive {false};
     /* account custom values */
     unsigned payloadType;
     unsigned bitrate;
diff --git a/src/sip/sipaccount.cpp b/src/sip/sipaccount.cpp
index aed61a2b47..597753b863 100644
--- a/src/sip/sipaccount.cpp
+++ b/src/sip/sipaccount.cpp
@@ -157,6 +157,7 @@ SIPAccount::SIPAccount(const std::string& accountID, bool presenceEnabled)
     via_addr_.host.ptr = 0;
     via_addr_.host.slen = 0;
     via_addr_.port = 0;
+    setActiveCodecs({});
 }
 
 SIPAccount::~SIPAccount()
@@ -2226,4 +2227,18 @@ SIPAccount::createBindingAddress()
     return ret;
 }
 
+void
+SIPAccount::setActiveCodecs(const std::vector<unsigned>& list)
+{
+    Account::setActiveCodecs(list);
+    if (!hasActiveCodec(MEDIA_AUDIO)) {
+        JAMI_WARN("All audio codecs disabled, enabling all");
+        setAllCodecsActive(MEDIA_AUDIO, true);
+    }
+    if (!hasActiveCodec(MEDIA_VIDEO)) {
+        JAMI_WARN("All video codecs disabled, enabling all");
+        setAllCodecsActive(MEDIA_VIDEO, true);
+    }
+}
+
 } // namespace jami
diff --git a/src/sip/sipaccount.h b/src/sip/sipaccount.h
index fae3b6b459..f577c86396 100644
--- a/src/sip/sipaccount.h
+++ b/src/sip/sipaccount.h
@@ -533,6 +533,8 @@ class SIPAccount : public SIPAccountBase {
          */
         IpAddr createBindingAddress();
 
+        void setActiveCodecs(const std::vector<unsigned>& list) override;
+
     private:
         void doRegister1_();
         void doRegister2_();
-- 
GitLab