Skip to content
Snippets Groups Projects
Commit dbc4ace9 authored by Guillaume Roguez's avatar Guillaume Roguez
Browse files

audiorecorder: use ThreadLoop

Use our integrated threadloop class than re-invent the weel.
Also cleanup code using modern C++.

Issue: #79703
Change-Id: Ibc3b0da9d0d24b4fc17950b946c1c3b9f134cdc5
parent 5236ab05
Branches
No related tags found
No related merge requests found
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
* Copyright (C) 2004-2015 Savoir-faire Linux Inc. * Copyright (C) 2004-2015 Savoir-faire Linux Inc.
* *
* Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com> * 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 * 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 * it under the terms of the GNU General Public License as published by
...@@ -21,74 +22,70 @@ ...@@ -21,74 +22,70 @@
#include "audiorecorder.h" #include "audiorecorder.h"
#include "audiorecord.h" #include "audiorecord.h"
#include "ringbufferpool.h" #include "ringbufferpool.h"
#include "logger.h" #include "audiobuffer.h"
#include <chrono> #include <chrono>
#include <thread>
#include <sstream> #include <sstream>
#include <unistd.h> #include <algorithm> // std::min
namespace ring { namespace ring {
int AudioRecorder::count_ = 0; static constexpr std::size_t BUFFER_LENGTH {10000};
static constexpr std::chrono::milliseconds SLEEP_TIME {20};
AudioRecorder::AudioRecorder(AudioRecord* arec, RingBufferPool& rbp) AudioRecorder::AudioRecorder(AudioRecord* arec, RingBufferPool& rbp)
: recorderId_(), ringBufferPool_(rbp), arecord_(arec), running_(false) : ringBufferPool_(rbp)
, thread_() , buffer_(new AudioBuffer(BUFFER_LENGTH, ringBufferPool_.getInternalAudioFormat()))
, arecord_(arec)
, thread_(
[this] { return true; },
[this] { process(); },
[] {})
{ {
++count_; std::string id("processd_");
std::string id("processid_");
// convert count into string // convert count into string
std::string s; std::string s;
std::ostringstream out; std::ostringstream out;
out << count_; out << nextProcessID();
s = out.str(); s = out.str();
recorderId_ = id.append(s); recorderId_ = id.append(s);
} }
AudioRecorder::~AudioRecorder() unsigned
AudioRecorder::nextProcessID() noexcept
{ {
running_ = false; static unsigned id = 0;
return ++id;
if (thread_.joinable())
thread_.join();
} }
void AudioRecorder::init() { void
if (!arecord_->isRecording()) { AudioRecorder::init() {
if (!arecord_->isRecording())
arecord_->setSndFormat(ringBufferPool_.getInternalAudioFormat()); arecord_->setSndFormat(ringBufferPool_.getInternalAudioFormat());
} }
}
void AudioRecorder::start() void
AudioRecorder::start()
{ {
if (running_) return; if (thread_.isRunning())
running_ = true; return;
thread_ = std::thread(&AudioRecorder::run, this); thread_.start();
} }
/** void
* Reimplementation of run() AudioRecorder::process()
*/
void AudioRecorder::run()
{ {
static const size_t BUFFER_LENGTH = 10000; auto availableSamples = ringBufferPool_.availableForGet(recorderId_);
static const std::chrono::milliseconds SLEEP_TIME(20); // 20 ms buffer_->resize(std::min(availableSamples, BUFFER_LENGTH));
ringBufferPool_.getData(*buffer_, recorderId_);
AudioBuffer buffer(BUFFER_LENGTH, ringBufferPool_.getInternalAudioFormat());
while (running_) {
const size_t availableSamples = ringBufferPool_.availableForGet(recorderId_);
buffer.resize(std::min(availableSamples, BUFFER_LENGTH));
ringBufferPool_.getData(buffer, recorderId_);
if (availableSamples > 0) if (availableSamples > 0)
arecord_->recData(buffer); arecord_->recData(*buffer_);
std::this_thread::sleep_for(SLEEP_TIME); std::this_thread::sleep_for(SLEEP_TIME);
} }
}
} // namespace ring } // namespace ring
/* /*
* Copyright (C) 2004-2015 Savoir-faire Linux Inc. * Copyright (C) 2004-2015 Savoir-faire Linux Inc.
* *
* Alexandre Savard <alexandre.savard@savoirfairelinux.com> * 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 * 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 * it under the terms of the GNU General Public License as published by
...@@ -18,26 +19,25 @@ ...@@ -18,26 +19,25 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/ */
#ifndef AUDIORECORDER_H_ #pragma once
#define AUDIORECORDER_H_
#include "threadloop.h"
#include "noncopyable.h" #include "noncopyable.h"
#include <thread>
#include <atomic>
#include <string> #include <string>
#include <memory>
namespace ring { namespace ring {
class RingBufferPool; class RingBufferPool;
class AudioRecord; class AudioRecord;
class AudioBuffer;
class AudioRecorder { class AudioRecorder {
public: public:
AudioRecorder(AudioRecord* arec, RingBufferPool& rbp); AudioRecorder(AudioRecord* arec, RingBufferPool& rbp);
~AudioRecorder();
std::string getRecorderID() const { std::string getRecorderID() const noexcept {
return recorderId_; return recorderId_;
} }
...@@ -54,16 +54,14 @@ class AudioRecorder { ...@@ -54,16 +54,14 @@ class AudioRecorder {
private: private:
NON_COPYABLE(AudioRecorder); NON_COPYABLE(AudioRecorder);
void run(); static unsigned nextProcessID() noexcept;
void process();
static int count_;
std::string recorderId_; std::string recorderId_;
RingBufferPool& ringBufferPool_; RingBufferPool& ringBufferPool_;
std::unique_ptr<AudioBuffer> buffer_;
AudioRecord* arecord_; AudioRecord* arecord_;
std::atomic<bool> running_; ThreadLoop thread_;
std::thread thread_;
}; };
} // namespace ring } // namespace ring
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment