From 4c71686ea7282d43f9dee6b571a5bd6da580e6ea Mon Sep 17 00:00:00 2001
From: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
Date: Fri, 5 Feb 2016 11:06:07 -0500
Subject: [PATCH] media: move-up recordable class

Recordable must not be linked to audio only. We need it for
any media types. So this patch moves it up from media/audio directory
to media.
Also unlink from audio headers by using smart pointers.

Tuleap: #337
Change-Id: Ia697280a9e482897bdf8c3dc7c3152a7133d1397
---
 src/call.cpp                   |  6 +--
 src/call.h                     |  2 +-
 src/conference.cpp             |  5 +-
 src/conference.h               |  2 +-
 src/manager.h                  |  1 +
 src/media/Makefile.am          |  6 ++-
 src/media/audio/Makefile.am    |  2 -
 src/media/audio/recordable.cpp | 54 ----------------------
 src/media/audio/recordable.h   | 83 ----------------------------------
 src/media/recordable.cpp       | 82 +++++++++++++++++++++++++++++++++
 src/media/recordable.h         | 76 +++++++++++++++++++++++++++++++
 11 files changed, 171 insertions(+), 148 deletions(-)
 delete mode 100644 src/media/audio/recordable.cpp
 delete mode 100644 src/media/audio/recordable.h
 create mode 100644 src/media/recordable.cpp
 create mode 100644 src/media/recordable.h

diff --git a/src/call.cpp b/src/call.cpp
index 364e84229b..52ca55e84f 100644
--- a/src/call.cpp
+++ b/src/call.cpp
@@ -26,7 +26,7 @@
 #include "audio/ringbufferpool.h"
 #include "dring/call_const.h"
 #include "client/ring_signal.h"
-
+#include "audio/audiorecorder.h"
 #include "sip/sip_utils.h"
 #include "ip_utils.h"
 #include "array_size.h"
@@ -261,13 +261,13 @@ Call::toggleRecording()
 {
     const bool startRecording = Recordable::toggleRecording();
     RingBufferPool &rbPool = Manager::instance().getRingBufferPool();
-    std::string process_id = Recordable::recorder_.getRecorderID();
+    std::string process_id = Recordable::recorder_->getRecorderID();
 
     if (startRecording) {
         rbPool.bindHalfDuplexOut(process_id, id_);
         rbPool.bindHalfDuplexOut(process_id, RingBufferPool::DEFAULT_ID);
 
-        Recordable::recorder_.start();
+        Recordable::recorder_->start();
     } else {
         rbPool.unBindHalfDuplexOut(process_id, id_);
         rbPool.unBindHalfDuplexOut(process_id, RingBufferPool::DEFAULT_ID);
diff --git a/src/call.h b/src/call.h
index 245451174b..d4b3ac7d62 100644
--- a/src/call.h
+++ b/src/call.h
@@ -30,7 +30,7 @@
 
 #include "logger.h"
 
-#include "audio/recordable.h"
+#include "recordable.h"
 #include "ip_utils.h"
 #include "ice_transport.h"
 
diff --git a/src/conference.cpp b/src/conference.cpp
index 46b15c4f8d..d38dd91901 100644
--- a/src/conference.cpp
+++ b/src/conference.cpp
@@ -25,6 +25,7 @@
 #include "manager.h"
 #include "audio/audiolayer.h"
 #include "audio/ringbufferpool.h"
+#include "audio/audiorecorder.h"
 
 #ifdef RING_VIDEO
 #include "sip/sipcall.h"
@@ -149,7 +150,7 @@ bool Conference::toggleRecording()
     const bool startRecording = Recordable::toggleRecording();
     auto& rbPool = Manager::instance().getRingBufferPool();
 
-    std::string process_id(Recordable::recorder_.getRecorderID());
+    std::string process_id(Recordable::recorder_->getRecorderID());
 
     // start recording
     if (startRecording) {
@@ -158,7 +159,7 @@ bool Conference::toggleRecording()
 
         rbPool.bindHalfDuplexOut(process_id, RingBufferPool::DEFAULT_ID);
 
-        Recordable::recorder_.start();
+        Recordable::recorder_->start();
     } else {
         for (const auto &item : participants_)
             rbPool.unBindHalfDuplexOut(process_id, item);
diff --git a/src/conference.h b/src/conference.h
index d55dd5396b..91d1e27cad 100644
--- a/src/conference.h
+++ b/src/conference.h
@@ -28,7 +28,7 @@
 #include <string>
 #include <memory>
 
-#include "audio/recordable.h"
+#include "recordable.h"
 
 namespace ring {
 
diff --git a/src/manager.h b/src/manager.h
index 9700ba5173..688c9a087c 100644
--- a/src/manager.h
+++ b/src/manager.h
@@ -65,6 +65,7 @@ class SinkClient;
 class PluginManager;
 class AudioFile;
 class DTMF;
+class RingBufferPool;
 
 /** To send multiple string */
 typedef std::list<std::string> TokenList;
diff --git a/src/media/Makefile.am b/src/media/Makefile.am
index 730b78c04f..c30f17df78 100644
--- a/src/media/Makefile.am
+++ b/src/media/Makefile.am
@@ -17,7 +17,8 @@ libmedia_la_SOURCES = \
 	media_io_handle.cpp \
 	media_codec.cpp \
 	system_codec_container.cpp \
-	srtp.c
+	srtp.c \
+	recordable.cpp
 
 noinst_HEADERS = \
 	rtp_session.h \
@@ -31,7 +32,8 @@ noinst_HEADERS = \
 	media_device.h \
 	media_codec.h \
 	system_codec_container.h \
-	srtp.h
+	srtp.h \
+	recordable.h
 
 libmedia_la_LIBADD = \
 	./audio/libaudio.la
diff --git a/src/media/audio/Makefile.am b/src/media/audio/Makefile.am
index b6c52fa7fc..0b4a8de7e9 100644
--- a/src/media/audio/Makefile.am
+++ b/src/media/audio/Makefile.am
@@ -40,7 +40,6 @@ libaudio_la_SOURCES = \
 		ringbufferpool.cpp \
 		audiorecord.cpp \
 		audiorecorder.cpp \
-		recordable.cpp \
 		audiolayer.cpp \
 		resampler.cpp \
 		$(RING_SPEEXDSP_SRC) \
@@ -64,7 +63,6 @@ noinst_HEADERS = \
 		audiorecord.h \
 		audiorecorder.h \
 		audiolayer.h \
-		recordable.h \
 		$(RING_SPEEXDSP_HEAD) \
 		dcblocker.h \
 		resampler.h \
diff --git a/src/media/audio/recordable.cpp b/src/media/audio/recordable.cpp
deleted file mode 100644
index 163059c1bc..0000000000
--- a/src/media/audio/recordable.cpp
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- *  Copyright (C) 2004-2015 Savoir-faire Linux Inc.
- *
- *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 3 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
- */
-
-#include "recordable.h"
-#include "manager.h"
-#include "logger.h"
-
-namespace ring {
-
-Recordable::Recordable() : recAudio_(), recorder_(&recAudio_, Manager::instance().getRingBufferPool())
-{
-    RING_DBG("Set recording options: %s", Manager::instance().audioPreference.getRecordPath().c_str());
-    recAudio_.setRecordingOptions(AudioFormat::MONO(), Manager::instance().audioPreference.getRecordPath());
-}
-
-Recordable::~Recordable()
-{
-    if (recAudio_.isOpenFile())
-        recAudio_.closeFile();
-}
-
-void Recordable::initRecFilename(const std::string &filename)
-{
-    recAudio_.initFilename(filename);
-}
-
-std::string Recordable::getFilename() const
-{
-    return recAudio_.getFilename();
-}
-
-void Recordable::setRecordingFormat(AudioFormat format)
-{
-    recAudio_.setSndFormat(format);
-}
-
-} // namespace ring
diff --git a/src/media/audio/recordable.h b/src/media/audio/recordable.h
deleted file mode 100644
index 2bc781d220..0000000000
--- a/src/media/audio/recordable.h
+++ /dev/null
@@ -1,83 +0,0 @@
-/*
- *  Copyright (C) 2004-2015 Savoir-faire Linux Inc.
- *
- *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 3 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
- */
-
-#ifndef RECORDABLE_H
-#define RECORDABLE_H
-
-#include "audiorecord.h"
-#include "audiorecorder.h"
-
-namespace ring {
-
-class Recordable {
-
-    public:
-
-        Recordable();
-        virtual ~Recordable();
-
-        /**
-         * Return recording state (true/false)
-         */
-        bool isRecording() const {
-            return recAudio_.isRecording();
-        }
-
-        /**
-         * This method must be implemented for this interface as calls and conferences
-         * have different behavior.
-         * Implementations must call the super method.
-         */
-        virtual bool toggleRecording() {
-            if (not isRecording())
-                recorder_.init();
-            return recAudio_.toggleRecording();
-        }
-
-        /**
-         * Stop recording
-         */
-        void stopRecording() {
-            recAudio_.stopRecording();
-        }
-
-        /**
-         * Init the recording file name according to path specified in configuration
-         */
-        void initRecFilename(const std::string &filename);
-
-        /**
-         * Return the file path for this recording
-         */
-        virtual std::string getFilename() const;
-
-        /**
-         * Set recording sampling rate.
-         */
-        void setRecordingFormat(AudioFormat format);
-
-    protected:
-        AudioRecord recAudio_;
-        AudioRecorder recorder_;
-};
-
-} // namespace ring
-
-#endif
diff --git a/src/media/recordable.cpp b/src/media/recordable.cpp
new file mode 100644
index 0000000000..5e98b12b21
--- /dev/null
+++ b/src/media/recordable.cpp
@@ -0,0 +1,82 @@
+/*
+ *  Copyright (C) 2004-2015 Savoir-faire Linux Inc.
+ *
+ *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
+ */
+
+#include "recordable.h"
+#include "audio/audiorecord.h"
+#include "audio/audiorecorder.h"
+#include "manager.h"
+#include "logger.h"
+
+namespace ring {
+
+Recordable::Recordable()
+    : recAudio_(new AudioRecord)
+    , recorder_(new AudioRecorder {recAudio_.get(), Manager::instance().getRingBufferPool()})
+{
+    auto record_path = Manager::instance().audioPreference.getRecordPath();
+    RING_DBG("Set recording options: %s", record_path.c_str());
+    recAudio_->setRecordingOptions(AudioFormat::MONO(), record_path);
+}
+
+Recordable::~Recordable()
+{
+    if (recAudio_->isOpenFile())
+        recAudio_->closeFile();
+}
+
+void
+Recordable::initRecFilename(const std::string &filename)
+{
+    recAudio_->initFilename(filename);
+}
+
+std::string
+Recordable::getFilename() const
+{
+    return recAudio_->getFilename();
+}
+
+void
+Recordable::setRecordingFormat(AudioFormat format)
+{
+    recAudio_->setSndFormat(format);
+}
+
+bool
+Recordable::isRecording() const
+{
+    return recAudio_->isRecording();
+}
+
+bool
+Recordable::toggleRecording()
+{
+    if (not isRecording())
+        recorder_->init();
+    return recAudio_->toggleRecording();
+}
+
+void
+Recordable::stopRecording()
+{
+    recAudio_->stopRecording();
+}
+
+} // namespace ring
diff --git a/src/media/recordable.h b/src/media/recordable.h
new file mode 100644
index 0000000000..870feb6501
--- /dev/null
+++ b/src/media/recordable.h
@@ -0,0 +1,76 @@
+/*
+ *  Copyright (C) 2004-2016 Savoir-faire Linux Inc.
+ *
+ *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
+ *  Author: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
+ */
+
+#pragma once
+
+#include "audio/audiobuffer.h"
+
+#include <string>
+#include <memory>
+
+namespace ring {
+
+class AudioRecord;
+class AudioRecorder;
+
+class Recordable {
+public:
+    Recordable();
+    virtual ~Recordable();
+
+    /**
+     * Return recording state (true/false)
+     */
+    bool isRecording() const;
+
+    /**
+     * This method must be implemented for this interface as calls and conferences
+     * have different behavior.
+     * Implementations must call the super method.
+     */
+    virtual bool toggleRecording();
+
+    /**
+     * Stop recording
+     */
+    void stopRecording();
+
+    /**
+     * Init the recording file name according to path specified in configuration
+     */
+    void initRecFilename(const std::string& filename);
+
+    /**
+     * Return the file path for this recording
+     */
+    virtual std::string getFilename() const;
+
+    /**
+     * Set recording sampling rate.
+     */
+    void setRecordingFormat(AudioFormat format);
+
+protected:
+    std::unique_ptr<AudioRecord> recAudio_;
+    std::unique_ptr<AudioRecorder> recorder_;
+};
+
+} // namespace ring
-- 
GitLab