From d87f2d32c13641e770df79e0a90a7a632c865986 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Mon, 11 Nov 2019 16:23:35 -0500
Subject: [PATCH] smarttools: use main scheduler instead of dedicated thread

Change-Id: I9fdfd1a0e1815d37dbacc871c8ef27586a7a605f
---
 src/smartools.cpp | 61 ++++++++++++++++++-----------------------------
 src/smartools.h   | 54 +++++++++++++++++++++--------------------
 2 files changed, 51 insertions(+), 64 deletions(-)

diff --git a/src/smartools.cpp b/src/smartools.cpp
index bad62e018f..58ab782621 100644
--- a/src/smartools.cpp
+++ b/src/smartools.cpp
@@ -18,12 +18,9 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
  */
 #include "smartools.h"
-#include "media/media_decoder.h"
-#include "media/video/video_input.h"
-#include "media/video/video_device.h"
+#include "manager.h"
 #include "dring/callmanager_interface.h"
 #include "client/ring_signal.h"
-#include "string_utils.h"
 
 namespace jami {
 
@@ -34,14 +31,8 @@ Smartools& Smartools::getInstance()
     return instance_;
 }
 
-// Launch process() in new thread
-Smartools::Smartools()
-: loop_([] { return true; }, [this] { process(); }, [] {})
-{}
-
-Smartools::~Smartools()
-{
-    loop_.join();
+Smartools::~Smartools() {
+    stop();
 }
 
 void
@@ -52,21 +43,16 @@ Smartools::sendInfo()
     information_.clear();
 }
 
-void
-Smartools::process()
-{
-    // Send the signal SmartInfo
-    Smartools::sendInfo();
-    std::this_thread::sleep_for(refreshTimeMs_);
-}
-
 void
 Smartools::start(std::chrono::milliseconds refreshTimeMs)
 {
     JAMI_DBG("Start SmartInfo");
-    refreshTimeMs_ = refreshTimeMs;
-    loop_.stop();
-    loop_.start();
+    if (auto t = std::move(task_))
+        t->cancel();
+    task_ = Manager::instance().scheduler().scheduleAtFixedRate([this]{
+        sendInfo();
+        return true;
+    }, refreshTimeMs);
 }
 
 void
@@ -74,7 +60,8 @@ Smartools::stop()
 {
     std::lock_guard<std::mutex> lk(mutexInfo_);
     JAMI_DBG("Stop SmartInfo");
-    loop_.stop();
+    if (auto t = std::move(task_))
+        t->cancel();
     information_.clear();
 }
 
@@ -109,21 +96,21 @@ void
 Smartools::setRemoteAudioCodec(const std::string& remoteAudioCodec)
 {
     std::lock_guard<std::mutex> lk(mutexInfo_);
-    information_["remote audio codec"]= remoteAudioCodec;
+    information_["remote audio codec"] = remoteAudioCodec;
 }
 
 void
 Smartools::setLocalAudioCodec(const std::string& localAudioCodec)
 {
     std::lock_guard<std::mutex> lk(mutexInfo_);
-    information_["local audio codec"]= localAudioCodec;
+    information_["local audio codec"] = localAudioCodec;
 }
 
 void
 Smartools::setLocalVideoCodec(const std::string& localVideoCodec)
 {
     std::lock_guard<std::mutex> lk(mutexInfo_);
-    information_["local video codec"]= localVideoCodec;
+    information_["local video codec"] = localVideoCodec;
 }
 
 void
@@ -131,17 +118,15 @@ Smartools::setRemoteVideoCodec(const std::string& remoteVideoCodec, const std::s
 {
     std::lock_guard<std::mutex> lk(mutexInfo_);
     information_["remote video codec"]= remoteVideoCodec;
-    auto call = Manager::instance().getCallFromCallID(callID);
-    if (!call) {
-        return;
-    }
-    auto confID = call->getConfId();
-    if (confID != ""){
-        information_["type"]= "conference";
-        information_["callID"]= confID;
-    } else {
-        information_["type"]= "no conference";
-        information_["callID"]= callID;
+    if (auto call = Manager::instance().getCallFromCallID(callID)) {
+        auto confID = call->getConfId();
+        if (not confID.empty()) {
+            information_["type"]= "conference";
+            information_["callID"]= confID;
+        } else {
+            information_["type"]= "no conference";
+            information_["callID"]= callID;
+        }
     }
  }
 
diff --git a/src/smartools.h b/src/smartools.h
index 90758ef00f..b66b22d58d 100644
--- a/src/smartools.h
+++ b/src/smartools.h
@@ -19,37 +19,39 @@
  */
 #pragma once
 
-#include "threadloop.h"
-#include "manager.h"
 #include <string>
+#include <chrono>
+#include <mutex>
+#include <map>
+#include <memory>
 
 namespace jami {
+class RepeatedTask;
+
 class Smartools
 {
-    // Use for the unit tests
-    #ifdef TESTING
-        friend class SmartoolsTest;
-    #endif
+// Use for the unit tests
+#ifdef TESTING
+    friend class SmartoolsTest;
+#endif
 
-    public:
-        static Smartools& getInstance();
-        void start(std::chrono::milliseconds refreshTimeMs);
-        void stop();
-        void setFrameRate(const std::string& id, const std::string& fps);
-        void setResolution(const std::string& id, int width, int height);
-        void setLocalVideoCodec(const std::string& localVideoCodec);
-        void setRemoteVideoCodec(const std::string& remoteVideoCodec, const std::string& callID);
-        void setRemoteAudioCodec(const std::string& remoteAudioCodec);
-        void setLocalAudioCodec(const std::string& remoteAudioCodec);
-        void sendInfo();
+public:
+    static Smartools& getInstance();
+    void start(std::chrono::milliseconds refreshTimeMs);
+    void stop();
+    void setFrameRate(const std::string& id, const std::string& fps);
+    void setResolution(const std::string& id, int width, int height);
+    void setLocalVideoCodec(const std::string& localVideoCodec);
+    void setRemoteVideoCodec(const std::string& remoteVideoCodec, const std::string& callID);
+    void setRemoteAudioCodec(const std::string& remoteAudioCodec);
+    void setLocalAudioCodec(const std::string& remoteAudioCodec);
+    void sendInfo();
 
-    private:
-        Smartools();
-        ~Smartools();
-        void process();
-        std::map<std::string, std::string> information_;
-        std::mutex mutexInfo_; // Protect information_ from multithreading
-        std::chrono::milliseconds refreshTimeMs_ {500};
-        ThreadLoop loop_; // Has to be last member
+private:
+    Smartools() {};
+    ~Smartools();
+    std::mutex mutexInfo_; // Protect information_ from multithreading
+    std::map<std::string, std::string> information_;
+    std::shared_ptr<RepeatedTask> task_;
 };
-} //ring namespace
+}
-- 
GitLab