From c74375b3f8e2466e79b359f814f5f158d2eb7527 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Thu, 16 Dec 2021 09:43:24 -0500
Subject: [PATCH] sipvoiplink: avoid to drop rotation's informations

if videoRtp is not initialized, the information is dropped. So,
it should be stored to avoid any drop of informations.
Note: in the future the container should change, because rotation
is currently applied to all video streams.

Change-Id: I036ffe7b6a248a3d8d7defeff1be78e008447fbd
GitLab: #678
---
 src/media/video/video_rtp_session.cpp |  5 ++--
 src/media/video/video_rtp_session.h   |  2 ++
 src/sip/sipcall.cpp                   | 41 +++++++++++++++------------
 src/sip/sipcall.h                     |  4 +++
 src/sip/sipvoiplink.cpp               |  4 +--
 5 files changed, 33 insertions(+), 23 deletions(-)

diff --git a/src/media/video/video_rtp_session.cpp b/src/media/video/video_rtp_session.cpp
index 6e7564e55b..f0aa2ddcd5 100644
--- a/src/media/video/video_rtp_session.cpp
+++ b/src/media/video/video_rtp_session.cpp
@@ -214,8 +214,8 @@ VideoRtpSession::startReceiver()
         receiveThread_->addIOContext(*socketPair_);
         receiveThread_->setSuccessfulSetupCb(onSuccessfulSetup_);
         receiveThread_->startLoop();
-        if (receiveThread_)
-            receiveThread_->setRequestKeyFrameCallback([this]() { cbKeyFrameRequest_(); });
+        receiveThread_->setRequestKeyFrameCallback([this]() { cbKeyFrameRequest_(); });
+        receiveThread_->setRotation(rotation_.load());
     } else {
         JAMI_DBG("Video receiving disabled");
         if (receiveThread_)
@@ -319,6 +319,7 @@ VideoRtpSession::forceKeyFrame()
 void
 VideoRtpSession::setRotation(int rotation)
 {
+    rotation_.store(rotation);
     if (receiveThread_)
         receiveThread_->setRotation(rotation);
 }
diff --git a/src/media/video/video_rtp_session.h b/src/media/video/video_rtp_session.h
index f8da52fc77..0c83d7e8d8 100644
--- a/src/media/video/video_rtp_session.h
+++ b/src/media/video/video_rtp_session.h
@@ -177,6 +177,8 @@ private:
     std::unique_ptr<CongestionControl> cc;
 
     std::function<void(void)> cbKeyFrameRequest_;
+
+    std::atomic<int> rotation_ {0};
 };
 
 } // namespace video
diff --git a/src/sip/sipcall.cpp b/src/sip/sipcall.cpp
index 4621da5bbb..fff0fc510f 100644
--- a/src/sip/sipcall.cpp
+++ b/src/sip/sipcall.cpp
@@ -176,6 +176,7 @@ SIPCall::createRtpSession(RtpStream& stream)
 #ifdef ENABLE_VIDEO
     else if (stream.mediaAttribute_->type_ == MediaType::MEDIA_VIDEO) {
         stream.rtpSession_ = std::make_shared<video::VideoRtpSession>(id_, getVideoSettings());
+        std::static_pointer_cast<video::VideoRtpSession>(stream.rtpSession_)->setRotation(rotation_);
     }
 #endif
     else {
@@ -2805,10 +2806,22 @@ SIPCall::getReceiveVideoFrameActiveWriter()
     return {};
 }
 
+#ifdef ENABLE_VIDEO
+std::shared_ptr<video::VideoRtpSession>
+SIPCall::getVideoRtp() const
+{
+    for (auto const& stream : rtpStreams_) {
+        auto rtp = stream.rtpSession_;
+        if (rtp->getMediaType() == MediaType::MEDIA_VIDEO) {
+            return std::dynamic_pointer_cast<video::VideoRtpSession>(rtp);
+        }
+    }
+    return nullptr;
+}
+
 bool
 SIPCall::addDummyVideoRtpSession()
 {
-#ifdef ENABLE_VIDEO
     JAMI_DBG("[call:%s] Add dummy video stream", getCallId().c_str());
 
     MediaAttribute mediaAttr(MediaType::MEDIA_VIDEO,
@@ -2822,9 +2835,6 @@ SIPCall::addDummyVideoRtpSession()
     auto& stream = rtpStreams_.back();
     createRtpSession(stream);
     return stream.rtpSession_ != nullptr;
-#endif
-
-    return false;
 }
 
 void
@@ -2850,6 +2860,15 @@ SIPCall::removeDummyVideoRtpSessions()
     }
 }
 
+void
+SIPCall::setRotation(int rotation)
+{
+    rotation_ = rotation;
+    if (auto videoRtp = getVideoRtp())
+        videoRtp->setRotation(rotation);
+}
+#endif
+
 void
 SIPCall::createSinks(const ConfInfo& infos)
 {
@@ -2887,20 +2906,6 @@ SIPCall::getAudioRtp() const
     return nullptr;
 }
 
-#ifdef ENABLE_VIDEO
-std::shared_ptr<video::VideoRtpSession>
-SIPCall::getVideoRtp() const
-{
-    for (auto const& stream : rtpStreams_) {
-        auto rtp = stream.rtpSession_;
-        if (rtp->getMediaType() == MediaType::MEDIA_VIDEO) {
-            return std::dynamic_pointer_cast<video::VideoRtpSession>(rtp);
-        }
-    }
-    return nullptr;
-}
-#endif
-
 std::vector<std::shared_ptr<RtpSession>>
 SIPCall::getRtpSessionList() const
 {
diff --git a/src/sip/sipcall.h b/src/sip/sipcall.h
index 11243c0f77..70ada6c030 100644
--- a/src/sip/sipcall.h
+++ b/src/sip/sipcall.h
@@ -267,6 +267,7 @@ public:
     std::shared_ptr<video::VideoRtpSession> getVideoRtp() const;
     bool addDummyVideoRtpSession() override;
     void removeDummyVideoRtpSessions() override;
+    void setRotation(int rotation);
 #endif
     // Get the list of current RTP sessions
     std::vector<std::shared_ptr<RtpSession>> getRtpSessionList() const;
@@ -469,6 +470,9 @@ private:
     void resetMediaReady();
 
     std::mutex setupSuccessMutex_;
+#ifdef ENABLE_VIDEO
+    int rotation_ {0};
+#endif
 };
 
 // Helpers
diff --git a/src/sip/sipvoiplink.cpp b/src/sip/sipvoiplink.cpp
index 021be9e068..3602b3d5da 100644
--- a/src/sip/sipvoiplink.cpp
+++ b/src/sip/sipvoiplink.cpp
@@ -1190,9 +1190,7 @@ handleMediaControl(SIPCall& call, pjsip_msg_body* body)
                         rotation -= 360;
                     JAMI_WARN("Rotate video %d deg.", rotation);
 #ifdef ENABLE_VIDEO
-                    auto const& videoRtp = call.getVideoRtp();
-                    if (videoRtp)
-                        videoRtp->setRotation(rotation);
+                    call.setRotation(rotation);
 #endif
                 } catch (const std::exception& e) {
                     JAMI_WARN("Error parsing angle: %s", e.what());
-- 
GitLab