Commit 8c58f649 authored by Adrien Béraud's avatar Adrien Béraud

audiorecord: init with main buffer format

Audio recording used to declare using the codec audio format but was
using main buffer data.

When a recordable object toggles recording, set the recording format to
the main buffer's, reorganise some includes to remove unecessary dependencies
and use std::thread in audiorecorder.

Refs #41357
parent 0b7aa073
......@@ -33,13 +33,15 @@
#endif
#include "audiorecord.h"
#include "logger.h"
#include "fileutils.h"
#include <sndfile.hh>
#include <unistd.h>
#include <sstream> // for stringstream
#include <algorithm>
#include <sstream> // for stringstream
#include <cstdio>
#include "logger.h"
#include "fileutils.h"
#include <unistd.h>
namespace {
std::string
......@@ -164,6 +166,7 @@ bool AudioRecord::openFile()
const bool doAppend = fileExists();
const int access = doAppend ? SFM_RDWR : SFM_WRITE;
DEBUG("Opening file %s with format %s", savePath_.c_str(), sndFormat_.toString().c_str());
fileHandle_ = new SndfileHandle(savePath_.c_str(), access, SF_FORMAT_WAV | SF_FORMAT_PCM_16, sndFormat_.nb_channels, sndFormat_.sample_rate);
// check overloaded boolean operator
......
......@@ -31,13 +31,13 @@
#ifndef _AUDIO_RECORD_H
#define _AUDIO_RECORD_H
#include <string>
#include <cstdlib>
#include <memory>
#include "audiobuffer.h"
#include "sfl_types.h"
#include "noncopyable.h"
#include "audiobuffer.h"
#include <memory>
#include <string>
#include <cstdlib>
class SndfileHandle;
......
......@@ -29,15 +29,18 @@
*/
#include "audiorecorder.h"
#include "audiorecord.h"
#include "mainbuffer.h"
#include "logger.h"
#include <chrono>
#include <sstream>
#include <unistd.h>
int AudioRecorder::count_ = 0;
AudioRecorder::AudioRecorder(AudioRecord *arec, MainBuffer &mb) :
recorderId_(), mbuffer_(mb), arecord_(arec), running_(false), thread_(0)
recorderId_(), mbuffer_(mb), arecord_(arec), running_(false), thread_()
{
++count_;
......@@ -56,22 +59,20 @@ AudioRecorder::~AudioRecorder()
{
running_ = false;
if (thread_)
pthread_join(thread_, NULL);
if (thread_.joinable())
thread_.join();
}
void AudioRecorder::start()
{
running_ = true;
pthread_create(&thread_, NULL, &runCallback, this);
void AudioRecorder::init() {
if(!arecord_->isRecording()) {
arecord_->setSndFormat(mbuffer_.getInternalAudioFormat());
}
}
void *
AudioRecorder::runCallback(void *data)
void AudioRecorder::start()
{
AudioRecorder *context = static_cast<AudioRecorder*>(data);
context->run();
return NULL;
running_ = true;
thread_ = std::thread(&AudioRecorder::run, this);
}
/**
......@@ -80,7 +81,9 @@ AudioRecorder::runCallback(void *data)
void AudioRecorder::run()
{
static const size_t BUFFER_LENGTH = 10000;
AudioBuffer buffer(BUFFER_LENGTH, AudioFormat::MONO);
static const std::chrono::milliseconds SLEEP_TIME(20); // 20 ms
AudioBuffer buffer(BUFFER_LENGTH, mbuffer_.getInternalAudioFormat());
while (running_) {
const size_t availableSamples = mbuffer_.availableForGet(recorderId_);
......@@ -90,6 +93,6 @@ void AudioRecorder::run()
if (availableSamples > 0)
arecord_->recData(buffer);
usleep(20000); // 20 ms
std::this_thread::sleep_for(SLEEP_TIME);
}
}
......@@ -31,12 +31,14 @@
#ifndef AUDIORECORDER_H_
#define AUDIORECORDER_H_
#include <string>
#include <pthread.h>
#include "audiorecord.h"
#include "noncopyable.h"
#include <thread>
#include <atomic>
#include <string>
class MainBuffer;
class AudioRecord;
class AudioRecorder {
......@@ -47,19 +49,27 @@ class AudioRecorder {
return recorderId_;
}
/**
* Set the record to the current audio format.
* Should be called before start() at least once.
*/
void init();
/**
* Call to start recording.
*/
void start();
private:
NON_COPYABLE(AudioRecorder);
void run();
static void * runCallback(void *data);
static int count_;
std::string recorderId_;
MainBuffer &mbuffer_;
AudioRecord *arecord_;
bool running_;
pthread_t thread_;
std::atomic<bool> running_;
std::thread thread_;
};
#endif
......@@ -94,8 +94,6 @@ void AudioRtpSession::setSessionMedia(const std::vector<AudioCodec*> &audioCodec
queue_.setPayloadFormat(ost::StaticPayloadFormat(static_cast<ost::StaticPayloadType>(payloadType)));
}
call_.setRecordingFormat(encoderFormat);
transportRate_ = rtpStream_.getTransportRate();
DEBUG("Switching to a transport rate of %d ms", transportRate_);
}
......
......@@ -50,8 +50,13 @@ class Recordable {
/**
* This method must be implemented for this interface as calls and conferences
* have different behavior.
* Implementations must call the super method.
*/
virtual bool toggleRecording() = 0;
virtual bool toggleRecording() {
if(!isRecording())
recorder_.init();
return recAudio_.toggleRecording();
}
/**
* Stop recording
......
......@@ -146,7 +146,7 @@ Call::getLocalVideoPort()
bool
Call::toggleRecording()
{
const bool startRecording = Recordable::recAudio_.toggleRecording();
const bool startRecording = Recordable::toggleRecording();
MainBuffer &mbuffer = Manager::instance().getMainBuffer();
std::string process_id = Recordable::recorder_.getRecorderID();
......
......@@ -141,7 +141,7 @@ ParticipantSet Conference::getParticipantList() const
bool Conference::toggleRecording()
{
const bool startRecording = Recordable::recAudio_.toggleRecording();
const bool startRecording = Recordable::toggleRecording();
MainBuffer &mbuffer = Manager::instance().getMainBuffer();
std::string process_id(Recordable::recorder_.getRecorderID());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment