From d8de5efea1e0ca0792f49c75931bf004fe6a4117 Mon Sep 17 00:00:00 2001
From: Pierre Lespagnol <pierre.lespagnol@savoirfairelinux.com>
Date: Mon, 14 Sep 2020 16:47:58 -0400
Subject: [PATCH] recorder: host notify each peer when recording + cleanup

- Allows conference host to notify each participant when recording
- Remove unnecessary callback
- Notify host if remaining participant is still recording after conference
- Update host notification if participant leave the conference

Change-Id: Ib17675442f2e175fe8711c695bc737ffa6d89f05
---
 src/conference.cpp                    | 19 +++++++++++++++++--
 src/media/video/video_rtp_session.cpp |  5 -----
 src/media/video/video_rtp_session.h   |  3 ---
 src/sip/sipcall.cpp                   | 15 ++++-----------
 src/sip/sipcall.h                     |  4 ++++
 5 files changed, 25 insertions(+), 21 deletions(-)

diff --git a/src/conference.cpp b/src/conference.cpp
index 6d6fe50f9e..03e7f2cc14 100644
--- a/src/conference.cpp
+++ b/src/conference.cpp
@@ -123,6 +123,9 @@ Conference::~Conference()
                     call->toggleRecording();
                 }
             }
+            // Notify that the remaining peer is still recording after conference
+            if (call->isPeerRecording())
+                call->setRemoteRecording(true);
         }
     }
 #endif // ENABLE_VIDEO
@@ -245,8 +248,11 @@ Conference::remove(const std::string& participant_id)
 {
     if (participants_.erase(participant_id)) {
 #ifdef ENABLE_VIDEO
-        if (auto call = Manager::instance().callFactory.getCall<SIPCall>(participant_id))
+        if (auto call = Manager::instance().callFactory.getCall<SIPCall>(participant_id)) {
             call->getVideoRtp().exitConference();
+            if (call->isPeerRecording())
+                call->setRemoteRecording(false);
+        }
 #endif // ENABLE_VIDEO
     }
 }
@@ -344,10 +350,19 @@ Conference::getDisplayNames() const
 bool
 Conference::toggleRecording()
 {
-    if (not isRecording())
+    bool newState = not isRecording();
+    if (newState)
         initRecorder(recorder_);
     else
         deinitRecorder(recorder_);
+
+    // Notify each participant
+    for (const auto& participant_id : participants_) {
+        if (auto call = Manager::instance().callFactory.getCall<SIPCall>(participant_id)) {
+            call->updateRecState(newState);
+        }
+    }
+
     return Recordable::toggleRecording();
 }
 
diff --git a/src/media/video/video_rtp_session.cpp b/src/media/video/video_rtp_session.cpp
index 0d3fc06a55..34ab180936 100644
--- a/src/media/video/video_rtp_session.cpp
+++ b/src/media/video/video_rtp_session.cpp
@@ -649,8 +649,6 @@ VideoRtpSession::initRecorder(std::shared_ptr<MediaRecorder>& rec)
             }
         }
     }
-    if (recordingStateCallback_)
-        recordingStateCallback_(true);
 }
 
 void
@@ -666,9 +664,6 @@ VideoRtpSession::deinitRecorder(std::shared_ptr<MediaRecorder>& rec)
             input->detach(ob);
         }
     }
-    if (recordingStateCallback_)
-        recordingStateCallback_(false);
-
 }
 
 void
diff --git a/src/media/video/video_rtp_session.h b/src/media/video/video_rtp_session.h
index d7f404c7f3..12865ba565 100644
--- a/src/media/video/video_rtp_session.h
+++ b/src/media/video/video_rtp_session.h
@@ -97,9 +97,6 @@ public:
     const std::string& getInput() const { return input_; }
 
     void setChangeOrientationCallback(std::function<void(int)> cb);
-    void setRecStateCallback(std::function<void(bool)> cb) {
-        recordingStateCallback_ = std::move(cb);
-    }
     void initRecorder(std::shared_ptr<MediaRecorder>& rec) override;
     void deinitRecorder(std::shared_ptr<MediaRecorder>& rec) override;
 
diff --git a/src/sip/sipcall.cpp b/src/sip/sipcall.cpp
index 79c277b316..49526896be 100644
--- a/src/sip/sipcall.cpp
+++ b/src/sip/sipcall.cpp
@@ -1067,12 +1067,6 @@ SIPCall::startAllMedia()
                 this_->setVideoOrientation(angle);
         });
     });
-    videortp_->setRecStateCallback([wthis = weak()] (bool state) {
-        runOnMainThread([wthis, state] {
-            if (auto this_ = wthis.lock())
-                this_->updateRecState(state);
-        });
-    });
 #endif
 
     for (const auto& slot : slots) {
@@ -1490,6 +1484,7 @@ SIPCall::toggleRecording()
 
     // add streams to recorder before starting the record
     if (not Call::isRecording()) {
+        updateRecState(true);
         std::stringstream ss;
         ss << "Conversation at %TIMESTAMP between " << getSIPAccount().getUserUri() << " and "
            << peerUri_;
@@ -1501,6 +1496,7 @@ SIPCall::toggleRecording()
             videortp_->initRecorder(recorder_);
 #endif
     } else {
+        updateRecState(false);
         deinitRecorder();
     }
     pendingRecord_ = false;
@@ -1635,11 +1631,7 @@ SIPCall::rtpSetupSuccess(MediaType type)
 void
 SIPCall::setRemoteRecording(bool state)
 {
-    std::string id {};
-    if (getConfId().empty())
-        id = getCallId();
-    else
-        id = getConfId();
+    const std::string& id = getConfId().empty() ? getCallId() : getConfId();
     if (state) {
         JAMI_WARN("SIP remote recording enabled");
         emitSignal<DRing::CallSignal::RemoteRecordingChanged>(id, getPeerNumber(), true);
@@ -1647,6 +1639,7 @@ SIPCall::setRemoteRecording(bool state)
         JAMI_WARN("SIP remote recording disabled");
         emitSignal<DRing::CallSignal::RemoteRecordingChanged>(id, getPeerNumber(), false);
     }
+    peerRecording_ = state;
 }
 
 } // namespace jami
diff --git a/src/sip/sipcall.h b/src/sip/sipcall.h
index 72478f8108..17db1742c8 100644
--- a/src/sip/sipcall.h
+++ b/src/sip/sipcall.h
@@ -237,6 +237,8 @@ public: // NOT SIP RELATED (good candidates to be moved elsewhere)
 
     void setRemoteRecording(bool state);
 
+    bool isPeerRecording() { return peerRecording_; }
+
 private:
     using clock = std::chrono::steady_clock;
     using time_point = clock::time_point;
@@ -370,6 +372,8 @@ private:
 
     OnReadyCb holdCb_ {};
     OnReadyCb offHoldCb_ {};
+
+    bool peerRecording_ {false};
 };
 
 // Helpers
-- 
GitLab