From 15f8fb84c4e575688f566e57451375b7db433a3a Mon Sep 17 00:00:00 2001
From: Nicolas Jager <nicolas.jager@savoirfairelinux.com>
Date: Fri, 8 Dec 2017 10:46:02 -0500
Subject: [PATCH] incoming call, getCallDetails inform about AUDIO_ONLY

make public Call::setEarlyDetails and change its name
for Call::updateDetails().
This permits to change details at creation AND after
as in the case of RingAccount where we don't have
details when the call is created.

Change-Id: I145624f93358990ed36d027bdb47bd4e498b1770
Signed-off-by: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
---
 src/call.cpp                |  6 +++---
 src/call.h                  | 11 +++++++++--
 src/ringdht/ringaccount.cpp |  3 ++-
 src/ringdht/ringaccount.h   |  3 ++-
 src/sip/sipaccount.cpp      |  7 +++++--
 src/sip/sipaccount.h        |  3 ++-
 src/sip/sipaccountbase.h    |  3 ++-
 src/sip/sipvoiplink.cpp     | 15 ++++++++++++---
 8 files changed, 37 insertions(+), 14 deletions(-)

diff --git a/src/call.cpp b/src/call.cpp
index 818caf9e26..5dad17049d 100644
--- a/src/call.cpp
+++ b/src/call.cpp
@@ -84,7 +84,7 @@ Call::Call(Account& account, const std::string& id, Call::CallType type,
     , type_(type)
     , account_(account)
 {
-    setEarlyDetails(details);
+    updateDetails(details);
 
     addStateListener([this](UNUSED Call::CallState call_state,
                             UNUSED Call::ConnectionState cnx_state,
@@ -312,9 +312,9 @@ Call::toggleRecording()
 }
 
 void
-Call::setEarlyDetails(const std::map<std::string, std::string>& details)
+Call::updateDetails(const std::map<std::string, std::string>& details)
 {
-    auto iter = details.find(DRing::Call::Details::AUDIO_ONLY);
+    const auto& iter = details.find(DRing::Call::Details::AUDIO_ONLY);
     if (iter != std::end(details))
         isAudioOnly_ = iter->second == TRUE_STR;
 }
diff --git a/src/call.h b/src/call.h
index 3194bf0f10..398ef83c2b 100644
--- a/src/call.h
+++ b/src/call.h
@@ -306,6 +306,15 @@ class Call : public Recordable, public std::enable_shared_from_this<Call> {
 
         virtual void restartMediaReceiver() = 0;
 
+        /**
+         * Update call details after creation.
+         * @param details to update
+         *
+         * /note No warranty to update any details, only some details can be modified.
+         *       See the implementation for more ... details :-).
+         */
+        void updateDetails(const std::map<std::string, std::string>& details);
+
     protected:
         virtual void merge(Call& scall);
 
@@ -339,8 +348,6 @@ class Call : public Recordable, public std::enable_shared_from_this<Call> {
     private:
         friend void hangupCallsIf(Call::SubcallSet, int, const std::function<bool(Call*)>&);
 
-        void setEarlyDetails(const std::map<std::string, std::string>& details);
-
         bool validStateTransition(CallState newState);
 
         void checkPendingIM();
diff --git a/src/ringdht/ringaccount.cpp b/src/ringdht/ringaccount.cpp
index 1f328cc331..88a2141818 100644
--- a/src/ringdht/ringaccount.cpp
+++ b/src/ringdht/ringaccount.cpp
@@ -304,12 +304,13 @@ RingAccount::flush()
 }
 
 std::shared_ptr<SIPCall>
-RingAccount::newIncomingCall(const std::string& from)
+RingAccount::newIncomingCall(const std::string& from, const std::map<std::string, std::string>& details)
 {
     std::lock_guard<std::mutex> lock(callsMutex_);
     auto call_it = pendingSipCalls_.begin();
     while (call_it != pendingSipCalls_.end()) {
         auto call = call_it->call.lock();
+        call->updateDetails(details);
         if (not call) {
             RING_WARN("newIncomingCall: discarding deleted call");
             call_it = pendingSipCalls_.erase(call_it);
diff --git a/src/ringdht/ringaccount.h b/src/ringdht/ringaccount.h
index 7782ef9b2c..0bf67d84a8 100644
--- a/src/ringdht/ringaccount.h
+++ b/src/ringdht/ringaccount.h
@@ -239,12 +239,13 @@ class RingAccount : public SIPAccountBase {
         /**
          * Create incoming SIPCall.
          * @param[in] from The origin of the call
+         * @param details use to set some specific details
          * @return std::shared_ptr<T> A shared pointer on the created call.
          *      The type of this instance is given in template argument.
          *      This type can be any base class of SIPCall class (included).
          */
         virtual std::shared_ptr<SIPCall>
-        newIncomingCall(const std::string& from = {}) override;
+        newIncomingCall(const std::string& from, const std::map<std::string, std::string>& details = {}) override;
 
         virtual bool isTlsEnabled() const override {
             return true;
diff --git a/src/sip/sipaccount.cpp b/src/sip/sipaccount.cpp
index 6d11d8fcb0..b388337fa2 100644
--- a/src/sip/sipaccount.cpp
+++ b/src/sip/sipaccount.cpp
@@ -159,10 +159,13 @@ SIPAccount::~SIPAccount()
 }
 
 std::shared_ptr<SIPCall>
-SIPAccount::newIncomingCall(const std::string& from UNUSED)
+SIPAccount::newIncomingCall(const std::string& from UNUSED, const std::map<std::string, std::string>& details)
 {
     auto& manager = Manager::instance();
-    return manager.callFactory.newCall<SIPCall, SIPAccount>(*this, manager.getNewCallID(), Call::CallType::INCOMING);
+    return manager.callFactory.newCall<SIPCall, SIPAccount>(*this,
+                                                            manager.getNewCallID(),
+                                                            Call::CallType::INCOMING,
+                                                            details);
 }
 
 template <>
diff --git a/src/sip/sipaccount.h b/src/sip/sipaccount.h
index 8a6cecdb82..10f5f1d856 100644
--- a/src/sip/sipaccount.h
+++ b/src/sip/sipaccount.h
@@ -478,12 +478,13 @@ class SIPAccount : public SIPAccountBase {
         /**
          * Create incoming SIPCall.
          * @param[in] from The origin uri of the call
+         * @param details use to set some specific details
          * @return std::shared_ptr<T> A shared pointer on the created call.
          *      The type of this instance is given in template argument.
          *      This type can be any base class of SIPCall class (included).
          */
         std::shared_ptr<SIPCall>
-        newIncomingCall(const std::string& from) override;
+        newIncomingCall(const std::string& from, const std::map<std::string, std::string>& details = {}) override;
 
         void onRegister(pjsip_regc_cbparam *param);
 
diff --git a/src/sip/sipaccountbase.h b/src/sip/sipaccountbase.h
index 67d5c4daf2..ed0302ea27 100644
--- a/src/sip/sipaccountbase.h
+++ b/src/sip/sipaccountbase.h
@@ -126,12 +126,13 @@ public:
     /**
      * Create incoming SIPCall.
      * @param[in] id The ID of the call
+     * @param details use to set some specific details
      * @return std::shared_ptr<T> A shared pointer on the created call.
      *      The type of this instance is given in template argument.
      *      This type can be any base class of SIPCall class (included).
      */
     virtual std::shared_ptr<SIPCall>
-    newIncomingCall(const std::string& from) = 0;
+    newIncomingCall(const std::string& from, const std::map<std::string, std::string>& details = {}) = 0;
 
     virtual bool isStunEnabled() const {
         return false;
diff --git a/src/sip/sipvoiplink.cpp b/src/sip/sipvoiplink.cpp
index 19a1ff03f1..d25b3a7627 100644
--- a/src/sip/sipvoiplink.cpp
+++ b/src/sip/sipvoiplink.cpp
@@ -273,7 +273,17 @@ transaction_request_cb(pjsip_rx_data *rdata)
 
     Manager::instance().hookPreference.runHook(rdata->msg_info.msg);
 
-    auto call = account->newIncomingCall(remote_user);
+    bool hasVideo = false;
+    bool hasAudio = false;
+    auto pj_str_video = pj_str((char*) "video");
+    auto pj_str_audio = pj_str((char*) "audio");
+    for (decltype(r_sdp->media_count) i=0 ; i < r_sdp->media_count; i++)
+        if (  pj_strcmp(&r_sdp->media[i]->desc.media, &pj_str_video) == 0 )
+            hasVideo = true;
+        else if (  pj_strcmp(&r_sdp->media[i]->desc.media, &pj_str_audio) == 0 )
+            hasAudio = true;
+
+    auto call = account->newIncomingCall(remote_user, {{"AUDIO_ONLY", ((not hasVideo and hasAudio) ? "true" : "false") }});
     if (!call) {
         return PJ_FALSE;
     }
@@ -333,7 +343,7 @@ transaction_request_cb(pjsip_rx_data *rdata)
 
     call->getSDP().receiveOffer(r_sdp,
         account->getActiveAccountCodecInfoList(MEDIA_AUDIO),
-        account->getActiveAccountCodecInfoList(account->isVideoEnabled() ? MEDIA_VIDEO : MEDIA_NONE),
+        account->getActiveAccountCodecInfoList(account->isVideoEnabled() and hasVideo ? MEDIA_VIDEO : MEDIA_NONE),
         account->getSrtpKeyExchange());
     call->setRemoteSdp(r_sdp);
 
@@ -434,7 +444,6 @@ transaction_request_cb(pjsip_rx_data *rdata)
             replacedCall->hangup(PJSIP_SC_OK);
     }
 
-
     return PJ_FALSE;
 }
 
-- 
GitLab