From b1d4a5c604857664043df1411666bafe5af8489c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lo=C3=AFc=20Siret?= <loic.siret@savoirfairelinux.com>
Date: Wed, 12 Oct 2016 09:48:18 -0400
Subject: [PATCH] audio: fix recording

Audio recording files are not closed properly, making them corrupt
on mac osx (wave header for file size or duration is 0) . Also
recording filenames contain duplicates, and sometimes missing
the entire filename.

This patch :
- adds call to closeFile when a call is remove from manager
- prevents filename duplication
- ensures filename is initialized on audio recorder

Change-Id: Iec3a606f9e65bbbfb5bcaf50ba924223c396507b
Tuleap: #999
---
 src/call.cpp                    |  1 +
 src/media/audio/audiorecord.cpp | 26 +++++++++++++-------------
 src/media/audio/audiorecord.h   |  4 ++--
 src/ringdht/ringaccount.cpp     |  1 +
 4 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/src/call.cpp b/src/call.cpp
index ee09daee09..2daba8fd3a 100644
--- a/src/call.cpp
+++ b/src/call.cpp
@@ -60,6 +60,7 @@ Call::removeCall()
     Manager::instance().callFactory.removeCall(*this);
     iceTransport_.reset();
     setState(CallState::OVER);
+    recAudio_->closeFile();
 }
 
 const std::string&
diff --git a/src/media/audio/audiorecord.cpp b/src/media/audio/audiorecord.cpp
index dde6a51ace..71cc60ba15 100644
--- a/src/media/audio/audiorecord.cpp
+++ b/src/media/audio/audiorecord.cpp
@@ -128,20 +128,20 @@ sanitize(std::string s)
 
 void AudioRecord::initFilename(const std::string &peerNumber)
 {
-    std::string fName(filename_);
-    fName.append("-" + sanitize(peerNumber) + "-" PACKAGE);
-
-    if (filename_.find(".wav") == std::string::npos) {
-        RING_DBG("Concatenate .wav file extension: name : %s", filename_.c_str());
-        fName.append(".wav");
+    RING_DBG("Initialize audio record for peer  : %s", peerNumber.c_str());
+    // if savePath_ don't contains filename
+    if (savePath_.find(".wav") == std::string::npos) {
+        filename_ = createFilename();
+        filename_.append("-" + sanitize(peerNumber) + "-" PACKAGE);
+        filename_.append(".wav");
+    } else {
+        filename_ = "";
     }
-
-    savePath_.append(fName);
 }
 
 std::string AudioRecord::getFilename() const
 {
-    return savePath_;
+    return savePath_ + filename_;
 }
 
 bool
@@ -152,8 +152,8 @@ AudioRecord::openFile()
     const bool doAppend = fileExists();
     const int access = doAppend ? SFM_RDWR : SFM_WRITE;
 
-    RING_DBG("Opening file %s with format %s", savePath_.c_str(), sndFormat_.toString().c_str());
-    fileHandle_.reset(new SndfileHandle (savePath_.c_str(),
+    RING_DBG("Opening file %s with format %s", getFilename().c_str(), sndFormat_.toString().c_str());
+    fileHandle_.reset(new SndfileHandle (getFilename().c_str(),
                                          access,
                                          SF_FORMAT_WAV | SF_FORMAT_PCM_16,
                                          sndFormat_.nb_channels,
@@ -187,7 +187,7 @@ AudioRecord::isOpenFile() const noexcept
 
 bool AudioRecord::fileExists() const
 {
-    return access(savePath_.c_str(), F_OK) != -1;
+    return access(getFilename().c_str(), F_OK) != -1;
 }
 
 bool AudioRecord::isRecording() const
@@ -211,7 +211,7 @@ AudioRecord::toggleRecording()
 void
 AudioRecord::stopRecording() const noexcept
 {
-    RING_DBG("Stop recording %s", savePath_.c_str());
+    RING_DBG("Stop recording %s", getFilename().c_str());
     recordingEnabled_ = false;
 }
 
diff --git a/src/media/audio/audiorecord.h b/src/media/audio/audiorecord.h
index 20035fcdfb..a11ffbb7ce 100644
--- a/src/media/audio/audiorecord.h
+++ b/src/media/audio/audiorecord.h
@@ -53,7 +53,7 @@ class AudioRecord {
         std::string getFilename() const;
 
         /**
-         * Check if no otehr file is opened, then create a new one
+         * Check if no other file is opened, then create a new one
          * @param filename A string containing teh file (with/without extension)
          * @param type     The sound file format (FILE_RAW, FILE_WAVE)
          * @param format   Internal sound format (INT16 / INT32)
@@ -62,7 +62,7 @@ class AudioRecord {
         bool openFile();
 
         /**
-         * Close the opend recording file. If wave: cout the number of byte
+         * Close the opened recording file. If wave: cout the number of byte
          */
         void closeFile();
 
diff --git a/src/ringdht/ringaccount.cpp b/src/ringdht/ringaccount.cpp
index 3a48f2c023..c2f62d8e1a 100644
--- a/src/ringdht/ringaccount.cpp
+++ b/src/ringdht/ringaccount.cpp
@@ -212,6 +212,7 @@ RingAccount::newOutgoingCall(const std::string& toUrl)
 
     call->setIPToIP(true);
     call->setSecure(isTlsEnabled());
+    call->initRecFilename(toUri);
 
     auto sthis = std::static_pointer_cast<RingAccount>(shared_from_this());
 
-- 
GitLab