Commit bb88a719 authored by Adrien Béraud's avatar Adrien Béraud

switch to c++11 mutex and lock for MainBuffer

parent 86bdd9bc
......@@ -122,6 +122,9 @@ void AudioBuffer::applyGain(double gain)
for (auto &channel : samples_)
for (auto &sample : channel)
sample *= g;
/* for (unsigned i=0, f=frames(), c=channels(); i < c; ++i)
for (unsigned j = 0; j < f; ++j)
samples_[i][j] *= g;*/
}
size_t AudioBuffer::interleave(SFLAudioSample* out) const
......@@ -135,8 +138,8 @@ size_t AudioBuffer::interleave(SFLAudioSample* out) const
size_t AudioBuffer::interleaveFloat(float* out) const
{
for (unsigned i = 0; i < frames(); i++)
for (unsigned j = 0; j < samples_.size(); j++)
for (unsigned i=0, f=frames(), c=channels(); i < f; i++)
for (unsigned j = 0; j < c; j++)
*out++ = (float) samples_[j][i] * .000030517578125f;
return frames() * samples_.size();
......
......@@ -31,17 +31,17 @@
#include "mainbuffer.h"
#include "ringbuffer.h"
#include "sfl_types.h" // for SIZEBUF
#include <climits>
#include <limits>
#include <cstring>
#include <utility> // for std::pair
#include "scoped_lock.h"
//#include "scoped_lock.h"
#include "logger.h"
const char * const MainBuffer::DEFAULT_ID = "audiolayer_id";
MainBuffer::MainBuffer() : ringBufferMap_(), callIDMap_(), mutex_(), internalSamplingRate_(8000)
{
pthread_mutex_init(&mutex_, NULL);
//pthread_mutex_init(&mutex_, NULL);
}
MainBuffer::~MainBuffer()
......@@ -50,7 +50,7 @@ MainBuffer::~MainBuffer()
for (auto &item : ringBufferMap_)
delete item.second;
pthread_mutex_destroy(&mutex_);
//pthread_mutex_destroy(&mutex_);
}
void MainBuffer::setInternalSamplingRate(int sr)
......@@ -139,7 +139,7 @@ void MainBuffer::removeRingBuffer(const std::string &call_id)
void MainBuffer::bindCallID(const std::string & call_id1, const std::string & call_id2)
{
sfl::ScopedLock guard(mutex_);
std::lock_guard<std::mutex> guard(mutex_);
createRingBuffer(call_id1);
createCallIDSet(call_id1);
......@@ -154,7 +154,7 @@ void MainBuffer::bindCallID(const std::string & call_id1, const std::string & ca
void MainBuffer::bindHalfDuplexOut(const std::string & process_id, const std::string & call_id)
{
sfl::ScopedLock guard(mutex_);
std::lock_guard<std::mutex> guard(mutex_);
// This method is used only for active calls, if this call does not exist, do nothing
if (!getRingBuffer(call_id))
......@@ -167,7 +167,7 @@ void MainBuffer::bindHalfDuplexOut(const std::string & process_id, const std::st
void MainBuffer::unBindCallID(const std::string & call_id1, const std::string & call_id2)
{
sfl::ScopedLock guard(mutex_);
std::lock_guard<std::mutex> guard(mutex_);
removeCallIDfromSet(call_id1, call_id2);
removeCallIDfromSet(call_id2, call_id1);
......@@ -197,7 +197,7 @@ void MainBuffer::unBindCallID(const std::string & call_id1, const std::string &
void MainBuffer::unBindHalfDuplexOut(const std::string & process_id, const std::string & call_id)
{
sfl::ScopedLock guard(mutex_);
std::lock_guard<std::mutex> guard(mutex_);
removeCallIDfromSet(process_id, call_id);
......@@ -237,7 +237,7 @@ void MainBuffer::unBindAll(const std::string & call_id)
//void MainBuffer::putData(void *buffer, size_t toCopy, const std::string &call_id)
void MainBuffer::putData(AudioBuffer& buffer, const std::string &call_id)
{
sfl::ScopedLock guard(mutex_);
std::lock_guard<std::mutex> guard(mutex_);
RingBuffer* ring_buffer = getRingBuffer(call_id);
......@@ -248,7 +248,7 @@ void MainBuffer::putData(AudioBuffer& buffer, const std::string &call_id)
//size_t MainBuffer::getData(void *buffer, size_t toCopy, const std::string &call_id)
size_t MainBuffer::getData(AudioBuffer& buffer, const std::string &call_id)
{
sfl::ScopedLock guard(mutex_);
std::lock_guard<std::mutex> guard(mutex_);
CallIDSet* callid_set = getCallIDSet(call_id);
......@@ -296,7 +296,7 @@ size_t MainBuffer::getDataByID(AudioBuffer& buffer, const std::string & call_id,
size_t MainBuffer::availableForGet(const std::string &call_id)
{
sfl::ScopedLock guard(mutex_);
std::lock_guard<std::mutex> guard(mutex_);
CallIDSet* callid_set = getCallIDSet(call_id);
......@@ -313,7 +313,7 @@ size_t MainBuffer::availableForGet(const std::string &call_id)
} else {
size_t availableSamples = INT_MAX;
size_t availableSamples = std::numeric_limits<int>::max();
for (auto &i : *callid_set) {
const size_t nbSamples = availableForGetByID(i, call_id);
......@@ -344,7 +344,7 @@ size_t MainBuffer::availableForGetByID(const std::string &call_id,
size_t MainBuffer::discard(size_t toDiscard, const std::string &call_id)
{
sfl::ScopedLock guard(mutex_);
std::lock_guard<std::mutex> guard(mutex_);
CallIDSet* callid_set = getCallIDSet(call_id);
......@@ -367,7 +367,7 @@ void MainBuffer::discardByID(size_t toDiscard, const std::string & call_id, cons
void MainBuffer::flush(const std::string & call_id)
{
sfl::ScopedLock guard(mutex_);
std::lock_guard<std::mutex> guard(mutex_);
CallIDSet* callid_set = getCallIDSet(call_id);
......@@ -396,7 +396,7 @@ void MainBuffer::flushAllBuffers()
void MainBuffer::dumpInfo()
{
#if 0
sfl::ScopedLock guard(mutex_);
std::lock_guard<std::mutex> guard(mutex_);
// print each call and bound call ids
for (const auto &item_call : callIDMap_) {
......
......@@ -34,7 +34,7 @@
#include <map>
#include <set>
#include <string>
#include <pthread.h>
#include <mutex>
#include "audiobuffer.h"
#include "mainbuffer.h"
......@@ -84,10 +84,8 @@ class MainBuffer {
void unBindAll(const std::string &call_id);
//void putData(void *buffer, size_t toCopy, const std::string &call_id);
void putData(AudioBuffer& buffer, const std::string &call_id);
//size_t getData(void *buffer, size_t toCopy, const std::string &call_id);
size_t getData(AudioBuffer& buffer, const std::string &call_id);
size_t availableForGet(const std::string &call_id);
......@@ -126,7 +124,6 @@ class MainBuffer {
RingBuffer* getRingBuffer(const std::string &call_id);
const RingBuffer* getRingBuffer(const std::string & call_id) const;
//size_t getDataByID(void *buffer, size_t toCopy, const std::string &call_id, const std::string &reader_id);
size_t getDataByID(AudioBuffer& buffer, const std::string &call_id, const std::string &reader_id);
size_t availableForGetByID(const std::string &call_id, const std::string &reader_id) const;
......@@ -141,7 +138,7 @@ class MainBuffer {
typedef std::map<std::string, CallIDSet*> CallIDMap;
CallIDMap callIDMap_;
pthread_mutex_t mutex_;
std::mutex mutex_;
int internalSamplingRate_;
......
......@@ -747,7 +747,7 @@ bool OpenSLLayer::audioPlaybackFillWithToneOrRingtone(AudioBuffer &buffer)
bool OpenSLLayer::audioPlaybackFillWithUrgent(AudioBuffer &buffer, size_t samplesToGet)
{
// Urgent data (dtmf, incoming call signal) come first.
samplesToGet = std::min(samplesToGet, buffer.frames());
samplesToGet = std::min(samplesToGet, BUFFER_SIZE);
buffer.resize(samplesToGet);
urgentRingBuffer_.get(buffer, MainBuffer::DEFAULT_ID);
buffer.applyGain(playbackGain_);
......@@ -776,7 +776,7 @@ bool OpenSLLayer::audioPlaybackFillWithVoice(AudioBuffer &buffer, size_t samples
AudioBuffer out(buffer, false);
out.setSampleRate(sampleRate_);
converter_.resample(buffer, out);
buffer = std::move(out);
buffer = out;
}
return true;
......
......@@ -34,7 +34,6 @@
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
#include <vector>
#include <atomic>
#include "../audiolayer.h"
#include "logger.h"
......@@ -53,8 +52,8 @@ class AudioPreference;
class OpenSLThread;
#define ANDROID_BUFFER_QUEUE_LENGTH 2
#define BUFFER_SIZE 80
#define ANDROID_BUFFER_QUEUE_LENGTH 2U
#define BUFFER_SIZE 80U
/**
......
......@@ -109,7 +109,7 @@ PulseLayer::PulseLayer(AudioPreference &pref)
pa_context_set_state_callback(context_, context_state_callback, this);
if (pa_context_connect(context_, NULL , PA_CONTEXT_NOAUTOSPAWN , NULL) < 0)
if (pa_context_connect(context_, nullptr , PA_CONTEXT_NOAUTOSPAWN , nullptr) < 0)
throw std::runtime_error("Could not connect pulseaudio context to the server");
pa_threaded_mainloop_lock(mainloop_);
......@@ -165,7 +165,7 @@ void PulseLayer::context_state_callback(pa_context* c, void *user_data)
case PA_CONTEXT_READY:
DEBUG("Connection to PulseAudio server established");
pa_threaded_mainloop_signal(pulse->mainloop_, 0);
pa_context_subscribe(c, mask, NULL, pulse);
pa_context_subscribe(c, mask, nullptr, pulse);
pa_context_set_subscribe_callback(c, context_changed_callback, pulse);
pulse->updateSinkList();
pulse->updateSourceList();
......@@ -190,7 +190,7 @@ void PulseLayer::updateSinkList()
enumeratingSinks_ = true;
pa_operation *op = pa_context_get_sink_info_list(context_, sink_input_info_callback, this);
if (op != NULL)
if (op != nullptr)
pa_operation_unref(op);
}
......@@ -200,7 +200,7 @@ void PulseLayer::updateSourceList()
enumeratingSources_ = true;
pa_operation *op = pa_context_get_source_info_list(context_, source_input_info_callback, this);
if (op != NULL)
if (op != nullptr)
pa_operation_unref(op);
}
......@@ -257,7 +257,7 @@ const PaDeviceInfos* PulseLayer::getDeviceInfos(const std::vector<PaDeviceInfos>
{
std::vector<PaDeviceInfos>::const_iterator dev_info = std::find_if(list.begin(), list.end(), PaDeviceInfos::nameComparator(name));
if (dev_info == list.end()) return NULL;
if (dev_info == list.end()) return nullptr;
return &(*dev_info);
}
......@@ -304,7 +304,7 @@ void PulseLayer::createStreams(pa_context* c)
// Create playback stream
const PaDeviceInfos* dev_infos = getDeviceInfos(sinkList_, playbackDevice);
if (dev_infos == NULL) {
if (dev_infos == nullptr) {
dev_infos = &sinkList_[0];
DEBUG("Prefered playback device not found in device list, selecting %s instead.", dev_infos->name.c_str());
}
......@@ -317,7 +317,7 @@ void PulseLayer::createStreams(pa_context* c)
// Create capture stream
dev_infos = getDeviceInfos(sourceList_, captureDevice);
if (dev_infos == NULL) {
if (dev_infos == nullptr) {
dev_infos = &sourceList_[0];
DEBUG("Prefered capture device not found in device list, selecting %s instead.", dev_infos->name.c_str());
}
......@@ -330,7 +330,7 @@ void PulseLayer::createStreams(pa_context* c)
// Create ringtone stream
dev_infos = getDeviceInfos(sinkList_, ringtoneDevice);
if (dev_infos == NULL) {
if (dev_infos == nullptr) {
dev_infos = &sinkList_[0];
DEBUG("Prefered ringtone device not found in device list, selecting %s instead.", dev_infos->name.c_str());
}
......@@ -383,10 +383,10 @@ PulseLayer::stopStream()
pa_threaded_mainloop_lock(mainloop_);
if (playback_)
pa_stream_flush(playback_->pulseStream(), NULL, NULL);
pa_stream_flush(playback_->pulseStream(), nullptr, nullptr);
if (record_)
pa_stream_flush(record_->pulseStream(), NULL, NULL);
pa_stream_flush(record_->pulseStream(), nullptr, nullptr);
pa_threaded_mainloop_unlock(mainloop_);
......@@ -433,7 +433,7 @@ void PulseLayer::writeToSpeaker()
urgentRingBuffer_.get(linearbuff, MainBuffer::DEFAULT_ID); // retrive only the first sample_spec->channels channels
linearbuff.applyGain(playbackGain_);
linearbuff.interleave(data);
pa_stream_write(s, data, urgentBytes, NULL, 0, PA_SEEK_RELATIVE);
pa_stream_write(s, data, urgentBytes, nullptr, 0, PA_SEEK_RELATIVE);
// Consume the regular one as well (same amount of samples)
Manager::instance().getMainBuffer().discard(urgentSamples, MainBuffer::DEFAULT_ID);
return;
......@@ -449,7 +449,7 @@ void PulseLayer::writeToSpeaker()
AudioBuffer linearbuff(writableSamples, n_channels);
toneToPlay->getNext(linearbuff, playbackGain_); // retrive only n_channels
linearbuff.interleave(data);
pa_stream_write(s, data, writableBytes, NULL, 0, PA_SEEK_RELATIVE);
pa_stream_write(s, data, writableBytes, nullptr, 0, PA_SEEK_RELATIVE);
}
return;
......@@ -462,7 +462,7 @@ void PulseLayer::writeToSpeaker()
if (availSamples == 0) {
pa_stream_begin_write(s, (void**)&data, &writableBytes);
memset(data, 0, writableBytes);
pa_stream_write(s, data, writableBytes, NULL, 0, PA_SEEK_RELATIVE);
pa_stream_write(s, data, writableBytes, nullptr, 0, PA_SEEK_RELATIVE);
return;
}
......@@ -493,11 +493,11 @@ void PulseLayer::writeToSpeaker()
converter_.resample(linearbuff, rsmpl_out);
rsmpl_out.applyGain(playbackGain_);
rsmpl_out.interleave(data);
pa_stream_write(s, data, resampledBytes, NULL, 0, PA_SEEK_RELATIVE);
pa_stream_write(s, data, resampledBytes, nullptr, 0, PA_SEEK_RELATIVE);
} else {
linearbuff.applyGain(playbackGain_);
linearbuff.interleave(data);
pa_stream_write(s, data, resampledBytes, NULL, 0, PA_SEEK_RELATIVE);
pa_stream_write(s, data, resampledBytes, nullptr, 0, PA_SEEK_RELATIVE);
}
}
......@@ -506,7 +506,7 @@ void PulseLayer::readFromMic()
if (!record_ or !record_->isReady())
return;
const char *data = NULL;
const char *data = nullptr;
size_t bytes;
const size_t sample_size = record_->sampleSize();
......@@ -583,7 +583,7 @@ void PulseLayer::ringtoneToSpeaker()
memset(data, 0, bytes);
}
pa_stream_write(s, data, bytes, NULL, 0, PA_SEEK_RELATIVE);
pa_stream_write(s, data, bytes, nullptr, 0, PA_SEEK_RELATIVE);
}
void
......@@ -604,7 +604,7 @@ PulseLayer::context_changed_callback(pa_context* c,
context->sinkList_.clear();
op = pa_context_get_sink_info_list(c, sink_input_info_callback, userdata);
if (op != NULL)
if (op != nullptr)
pa_operation_unref(op);
default:
......@@ -621,7 +621,7 @@ PulseLayer::context_changed_callback(pa_context* c,
context->sourceList_.clear();
op = pa_context_get_source_info_list(c, source_input_info_callback, userdata);
if (op != NULL)
if (op != nullptr)
pa_operation_unref(op);
default:
......
......@@ -48,7 +48,7 @@ class AudioStream;
/**
* Convenience structure to hold PulseAudio device propreties such as supported channel number etc.
*/
typedef struct PaDeviceInfos {
typedef struct {
uint32_t index;
std::string name;
pa_sample_spec sample_spec;
......@@ -150,17 +150,17 @@ class PulseLayer : public AudioLayer {
AudioStream* ringtone_;
/**
* Contain the list of playback devices
* Contains the list of playback devices
*/
std::vector<PaDeviceInfos> sinkList_;
/**
* Contain the list of capture devices
* Contains the list of capture devices
*/
std::vector<PaDeviceInfos> sourceList_;
/**
* Returns a pointer to the PaEndpointInfos with the given name in sourceList_, or NULL if not found.
* Returns a pointer to the PaEndpointInfos with the given name in sourceList_, or nullptr if not found.
*/
const PaDeviceInfos* getDeviceInfos(const std::vector<PaDeviceInfos>&, const std::string& name) const;
......
kde @ 39ec90ae
Subproject commit 9531d976a881db54215ccad42752b09e51995e8e
Subproject commit 39ec90aec140f3184b298c6de670183421e66bff
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