Commit 61bf3908 authored by Tristan Matthews's avatar Tristan Matthews

* #11964: mainbuffer/ringbuffer: cleanup API

parent 9ae35e08
......@@ -588,14 +588,14 @@ void AlsaLayer::playback(int maxSamples)
unsigned int mainBufferSampleRate = Manager::instance().getMainBuffer()->getInternalSamplingRate();
bool resample = sampleRate_ != mainBufferSampleRate;
int toGet = Manager::instance().getMainBuffer()->availForGet(MainBuffer::DEFAULT_ID);
int toPut = maxSamples * sizeof(SFLDataFormat);
size_t toGet = Manager::instance().getMainBuffer()->availableForGet(MainBuffer::DEFAULT_ID);
const size_t toPut = maxSamples * sizeof(SFLDataFormat);
if (toGet <= 0) { // no audio available, play tone or silence
AudioLoop *tone = Manager::instance().getTelephoneTone();
AudioLoop *file_tone = Manager::instance().getTelephoneFile();
SFLDataFormat *out = (SFLDataFormat *) malloc(toPut);
SFLDataFormat *out = static_cast<SFLDataFormat *>(malloc(toPut));
if (tone) {
tone->getNext(out, maxSamples, getPlaybackGain());
......@@ -613,7 +613,7 @@ void AlsaLayer::playback(int maxSamples)
// play the regular sound samples
int maxNbBytesToGet = toPut;
size_t maxNbBytesToGet = toPut;
// Compute maximal value to get from the ring buffer
double resampleFactor = 1.0;
......@@ -655,7 +655,7 @@ void AlsaLayer::audioCallback()
int playbackAvailSmpl = snd_pcm_avail_update(playbackHandle_);
int playbackAvailBytes = playbackAvailSmpl * sizeof(SFLDataFormat);
int toGet = urgentRingBuffer_.AvailForGet(MainBuffer::DEFAULT_ID);
int toGet = urgentRingBuffer_.availableForGet(MainBuffer::DEFAULT_ID);
if (toGet > 0) {
// Urgent data (dtmf, incoming call signal) come first.
......@@ -663,7 +663,7 @@ void AlsaLayer::audioCallback()
toGet = playbackAvailBytes;
SFLDataFormat *out = (SFLDataFormat*) malloc(toGet);
urgentRingBuffer_.Get(out, toGet, MainBuffer::DEFAULT_ID);
urgentRingBuffer_.get(out, toGet, MainBuffer::DEFAULT_ID);
AudioLayer::applyGain(out, toGet / sizeof(SFLDataFormat), getPlaybackGain());
write(out, toGet, playbackHandle_);
......
......@@ -66,7 +66,7 @@ void AudioLayer::flushUrgent()
void AudioLayer::putUrgent(void* buffer, int toCopy)
{
ost::MutexLock guard(mutex_);
urgentRingBuffer_.Put(buffer, toCopy);
urgentRingBuffer_.put(buffer, toCopy);
}
void AudioLayer::applyGain(SFLDataFormat *src , int samples, int gain)
......
......@@ -65,11 +65,11 @@ void AudioRecorder::run()
buffer.assign(0);
while (running_) {
size_t availBytes = mbuffer_->availForGet(recorderId_);
mbuffer_->getData(buffer.data(), std::min(availBytes, buffer.size()), recorderId_);
const size_t availableBytes = mbuffer_->availableForGet(recorderId_);
mbuffer_->getData(buffer.data(), std::min(availableBytes, buffer.size()), recorderId_);
if (availBytes > 0)
arecord_->recData(buffer.data(), availBytes / sizeof(SFLDataFormat));
if (availableBytes > 0)
arecord_->recData(buffer.data(), availableBytes / sizeof(SFLDataFormat));
Thread::sleep(20);
}
......
......@@ -152,13 +152,13 @@ int AudioRtpRecordHandler::processDataEncode()
// compute nb of byte to get coresponding to 1 audio frame
int samplesToGet = resampleFactor * getCodecFrameSize();
int bytesToGet = samplesToGet * sizeof(SFLDataFormat);
const size_t bytesToGet = samplesToGet * sizeof(SFLDataFormat);
if (Manager::instance().getMainBuffer()->availForGet(id_) < bytesToGet)
if (Manager::instance().getMainBuffer()->availableForGet(id_) < bytesToGet)
return 0;
SFLDataFormat *micData = audioRtpRecord_.decData_.data();
int bytes = Manager::instance().getMainBuffer()->getData(micData, bytesToGet, id_);
const size_t bytes = Manager::instance().getMainBuffer()->getData(micData, bytesToGet, id_);
#ifdef RECTODISK
rtpNotResampled.write((const char *)micData, bytes);
......
......@@ -103,6 +103,12 @@ RingBuffer* MainBuffer::getRingBuffer(const std::string & call_id)
return (iter != ringBufferMap_.end()) ? iter->second : NULL;
}
const RingBuffer* MainBuffer::getRingBuffer(const std::string & call_id) const
{
RingBufferMap::const_iterator iter = ringBufferMap_.find(call_id);
return (iter != ringBufferMap_.end()) ? iter->second : NULL;
}
void MainBuffer::createRingBuffer(const std::string &call_id)
{
if (!getRingBuffer(call_id))
......@@ -162,7 +168,7 @@ void MainBuffer::unBindCallID(const std::string & call_id1, const std::string &
if (ringbuffer) {
ringbuffer->removeReadPointer(call_id1);
if (ringbuffer->getNbReadPointer() == 0) {
if (ringbuffer->hasNoReadPointers()) {
removeCallIDSet(call_id2);
removeRingBuffer(call_id2);
}
......@@ -173,7 +179,7 @@ void MainBuffer::unBindCallID(const std::string & call_id1, const std::string &
if (ringbuffer) {
ringbuffer->removeReadPointer(call_id2);
if (ringbuffer->getNbReadPointer() == 0) {
if (ringbuffer->hasNoReadPointers()) {
removeCallIDSet(call_id1);
removeRingBuffer(call_id1);
}
......@@ -191,7 +197,7 @@ void MainBuffer::unBindHalfDuplexOut(const std::string & process_id, const std::
if (ringbuffer) {
ringbuffer->removeReadPointer(process_id);
if (ringbuffer->getNbReadPointer() == 0) {
if (ringbuffer->hasNoReadPointers()) {
removeCallIDSet(call_id);
removeRingBuffer(call_id);
}
......@@ -222,17 +228,17 @@ void MainBuffer::unBindAll(const std::string & call_id)
}
}
void MainBuffer::putData(void *buffer, int toCopy, const std::string &call_id)
void MainBuffer::putData(void *buffer, size_t toCopy, const std::string &call_id)
{
ost::MutexLock guard(mutex_);
RingBuffer* ring_buffer = getRingBuffer(call_id);
if (ring_buffer)
ring_buffer->Put(buffer, toCopy);
ring_buffer->put(buffer, toCopy);
}
int MainBuffer::getData(void *buffer, int toCopy, const std::string &call_id)
size_t MainBuffer::getData(void *buffer, size_t toCopy, const std::string &call_id)
{
ost::MutexLock guard(mutex_);
......@@ -252,18 +258,18 @@ int MainBuffer::getData(void *buffer, int toCopy, const std::string &call_id)
} else {
memset(buffer, 0, toCopy);
int size = 0;
size_t size = 0;
for (CallIDSet::iterator iter_id = callid_set->begin();
iter_id != callid_set->end(); ++iter_id) {
int nbSmplToCopy = toCopy / sizeof(SFLDataFormat);
size_t nbSmplToCopy = toCopy / sizeof(SFLDataFormat);
SFLDataFormat mixBuffer[nbSmplToCopy];
memset(mixBuffer, 0, toCopy);
size = getDataByID(mixBuffer, toCopy, *iter_id, call_id);
if (size > 0) {
SFLDataFormat *dest = static_cast<SFLDataFormat*>(buffer);
for (int k = 0; k < nbSmplToCopy; ++k)
for (size_t k = 0; k < nbSmplToCopy; ++k)
dest[k] += mixBuffer[k];
}
}
......@@ -272,13 +278,13 @@ int MainBuffer::getData(void *buffer, int toCopy, const std::string &call_id)
}
}
int MainBuffer::getDataByID(void *buffer, int toCopy, const std::string & call_id, const std::string & reader_id)
size_t MainBuffer::getDataByID(void *buffer, size_t toCopy, const std::string & call_id, const std::string & reader_id)
{
RingBuffer* ring_buffer = getRingBuffer(call_id);
return ring_buffer ? ring_buffer->Get(buffer, toCopy, reader_id) : 0;
return ring_buffer ? ring_buffer->get(buffer, toCopy, reader_id) : 0;
}
int MainBuffer::availForGet(const std::string & call_id)
size_t MainBuffer::availableForGet(const std::string &call_id)
{
ost::MutexLock guard(mutex_);
......@@ -293,39 +299,39 @@ int MainBuffer::availForGet(const std::string & call_id)
if ((call_id != DEFAULT_ID) && (*iter_id == call_id))
DEBUG("This problem should not occur since we have %ld elements", callid_set->size());
return availForGetByID(*iter_id, call_id);
return availableForGetByID(*iter_id, call_id);
} else {
int avail_bytes = INT_MAX;
size_t availableBytes = INT_MAX;
for (CallIDSet::iterator i = callid_set->begin(); i != callid_set->end(); ++i) {
const int nb_bytes = availForGetByID(*i, call_id);
const size_t nbBytes = availableForGetByID(*i, call_id);
if (nb_bytes != 0)
avail_bytes = std::min(avail_bytes, nb_bytes);
if (nbBytes != 0)
availableBytes = std::min(availableBytes, nbBytes);
}
return avail_bytes != INT_MAX ? avail_bytes : 0;
return availableBytes != INT_MAX ? availableBytes : 0;
}
}
int MainBuffer::availForGetByID(const std::string &call_id,
const std::string &reader_id)
size_t MainBuffer::availableForGetByID(const std::string &call_id,
const std::string &reader_id) const
{
if (call_id != DEFAULT_ID and reader_id == call_id)
ERROR("RingBuffer has a readpointer on itself");
RingBuffer* ringbuffer = getRingBuffer(call_id);
const RingBuffer* ringbuffer = getRingBuffer(call_id);
if (ringbuffer == NULL) {
ERROR("RingBuffer does not exist");
return 0;
} else
return ringbuffer->AvailForGet(reader_id);
return ringbuffer->availableForGet(reader_id);
}
int MainBuffer::discard(int toDiscard, const std::string &call_id)
size_t MainBuffer::discard(size_t toDiscard, const std::string &call_id)
{
ost::MutexLock guard(mutex_);
......@@ -340,12 +346,12 @@ int MainBuffer::discard(int toDiscard, const std::string &call_id)
return toDiscard;
}
void MainBuffer::discardByID(int toDiscard, const std::string & call_id, const std::string & reader_id)
void MainBuffer::discardByID(size_t toDiscard, const std::string & call_id, const std::string & reader_id)
{
RingBuffer* ringbuffer = getRingBuffer(call_id);
if (ringbuffer)
ringbuffer->Discard(toDiscard, reader_id);
ringbuffer->discard(toDiscard, reader_id);
}
void MainBuffer::flush(const std::string & call_id)
......
......@@ -82,13 +82,13 @@ class MainBuffer {
void unBindAll(const std::string &call_id);
void putData(void *buffer, int toCopy, const std::string &call_id);
void putData(void *buffer, size_t toCopy, const std::string &call_id);
int getData(void *buffer, int toCopy, const std::string &call_id);
size_t getData(void *buffer, size_t toCopy, const std::string &call_id);
int availForGet(const std::string &call_id);
size_t availableForGet(const std::string &call_id);
int discard(int toDiscard, const std::string &call_id);
size_t discard(size_t toDiscard, const std::string &call_id);
void flush(const std::string &call_id);
......@@ -119,12 +119,13 @@ class MainBuffer {
void removeRingBuffer(const std::string &call_id);
RingBuffer* getRingBuffer(const std::string &call_id);
const RingBuffer* getRingBuffer(const std::string & call_id) const;
int getDataByID(void *buffer, int toCopy, const std::string &call_id, const std::string &reader_id);
size_t getDataByID(void *buffer, size_t toCopy, const std::string &call_id, const std::string &reader_id);
int availForGetByID(const std::string &call_id, const std::string &reader_id);
size_t availableForGetByID(const std::string &call_id, const std::string &reader_id) const;
void discardByID(int toDiscard, const std::string &call_id, const std::string &reader_id);
void discardByID(size_t toDiscard, const std::string &call_id, const std::string &reader_id);
void flushByID(const std::string &call_id, const std::string &reader_id);
......
......@@ -326,7 +326,7 @@ void PulseLayer::writeToSpeaker()
notifyIncomingCall();
size_t urgentBytes = urgentRingBuffer_.AvailForGet(MainBuffer::DEFAULT_ID);
size_t urgentBytes = urgentRingBuffer_.availableForGet(MainBuffer::DEFAULT_ID);
if (urgentBytes > writableBytes)
urgentBytes = writableBytes;
......@@ -334,7 +334,7 @@ void PulseLayer::writeToSpeaker()
void *data = 0;
if (urgentBytes) {
pa_stream_begin_write(s, &data, &urgentBytes);
urgentRingBuffer_.Get(data, urgentBytes, MainBuffer::DEFAULT_ID);
urgentRingBuffer_.get(data, urgentBytes, MainBuffer::DEFAULT_ID);
applyGain(static_cast<SFLDataFormat *>(data), urgentBytes / sizeof(SFLDataFormat), getPlaybackGain());
pa_stream_write(s, data, urgentBytes, NULL, 0, PA_SEEK_RELATIVE);
// Consume the regular one as well (same amount of bytes)
......@@ -357,7 +357,7 @@ void PulseLayer::writeToSpeaker()
flushUrgent(); // flush remaining samples in _urgentRingBuffer
size_t availSamples = Manager::instance().getMainBuffer()->availForGet(MainBuffer::DEFAULT_ID) / sizeof(SFLDataFormat);
size_t availSamples = Manager::instance().getMainBuffer()->availableForGet(MainBuffer::DEFAULT_ID) / sizeof(SFLDataFormat);
if (availSamples == 0) {
pa_stream_begin_write(s, &data, &writableBytes);
......
......@@ -39,28 +39,19 @@
#include "logger.h"
#include "ringbuffer.h"
// corespond to 106 ms (about 5 rtp packets)
#define MIN_BUFFER_SIZE 1280
int RingBuffer::count_rb = 0;
namespace {
// corresponds to 106 ms (about 5 rtp packets)
const size_t MIN_BUFFER_SIZE = 1280;
}
// Create a ring buffer with 'size' bytes
RingBuffer::RingBuffer(int size, const std::string &call_id) : endPos_(0)
RingBuffer::RingBuffer(size_t size, const std::string &call_id) :
endPos_(0)
, bufferSize_(size > MIN_BUFFER_SIZE ? size : MIN_BUFFER_SIZE)
, buffer_(NULL)
, readpointer_()
, buffer_(bufferSize_)
, readpointers_()
, buffer_id_(call_id)
, buffer_input_rec(0)
, buffer_output_rec(0)
{
buffer_ = new unsigned char[bufferSize_];
count_rb++;
}
RingBuffer::~RingBuffer()
{
delete[] buffer_;
}
{}
void
RingBuffer::flush(const std::string &call_id)
......@@ -74,19 +65,18 @@ RingBuffer::flushAll()
{
ReadPointer::iterator iter;
for (iter = readpointer_.begin(); iter != readpointer_.end(); ++iter)
for (iter = readpointers_.begin(); iter != readpointers_.end(); ++iter)
iter->second = endPos_;
}
int
RingBuffer::putLen()
size_t
RingBuffer::putLength() const
{
int startPos = (readpointer_.size() >= 1) ? getSmallestReadPointer() : 0;
const size_t startPos = (not readpointers_.empty()) ? getSmallestReadPointer() : 0;
return (endPos_ + bufferSize_ - startPos) % bufferSize_;
}
int
RingBuffer::getLen(const std::string &call_id)
size_t RingBuffer::getLength(const std::string &call_id) const
{
return (endPos_ + bufferSize_ - getReadPointer(call_id)) % bufferSize_;
}
......@@ -97,27 +87,26 @@ RingBuffer::debug()
DEBUG("Start=%d; End=%d; BufferSize=%d", getSmallestReadPointer(), endPos_, bufferSize_);
}
int
RingBuffer::getReadPointer(const std::string &call_id)
size_t RingBuffer::getReadPointer(const std::string &call_id) const
{
if (getNbReadPointer() == 0)
if (hasNoReadPointers())
return 0;
ReadPointer::iterator iter = readpointer_.find(call_id);
return (iter != readpointer_.end()) ? iter->second : 0;
ReadPointer::const_iterator iter = readpointers_.find(call_id);
return (iter != readpointers_.end()) ? iter->second : 0;
}
int
RingBuffer::getSmallestReadPointer()
size_t
RingBuffer::getSmallestReadPointer() const
{
if (getNbReadPointer() == 0)
if (hasNoReadPointers())
return 0;
int smallest = bufferSize_;
size_t smallest = bufferSize_;
ReadPointer::iterator iter;
ReadPointer::const_iterator iter;
for (iter = readpointer_.begin(); iter != readpointer_.end(); ++iter)
for (iter = readpointers_.begin(); iter != readpointers_.end(); ++iter)
if (iter->second < smallest)
smallest = iter->second;
......@@ -125,11 +114,11 @@ RingBuffer::getSmallestReadPointer()
}
void
RingBuffer::storeReadPointer(int pointer_value, const std::string &call_id)
RingBuffer::storeReadPointer(size_t pointer_value, const std::string &call_id)
{
ReadPointer::iterator iter = readpointer_.find(call_id);
ReadPointer::iterator iter = readpointers_.find(call_id);
if (iter != readpointer_.end())
if (iter != readpointers_.end())
iter->second = pointer_value;
else
DEBUG("Cannot find \"%s\" readPointer in \"%s\" ringbuffer", call_id.c_str(), buffer_id_.c_str());
......@@ -140,31 +129,30 @@ void
RingBuffer::createReadPointer(const std::string &call_id)
{
if (!hasThisReadPointer(call_id))
readpointer_.insert(std::pair<std::string, int> (call_id, endPos_));
readpointers_.insert(std::pair<std::string, int> (call_id, endPos_));
}
void
RingBuffer::removeReadPointer(const std::string &call_id)
{
ReadPointer::iterator iter = readpointer_.find(call_id);
ReadPointer::iterator iter = readpointers_.find(call_id);
if (iter != readpointer_.end())
readpointer_.erase(iter);
if (iter != readpointers_.end())
readpointers_.erase(iter);
}
bool
RingBuffer::hasThisReadPointer(const std::string &call_id)
RingBuffer::hasThisReadPointer(const std::string &call_id) const
{
return readpointer_.find(call_id) != readpointer_.end();
return readpointers_.find(call_id) != readpointers_.end();
}
int
RingBuffer::getNbReadPointer()
bool RingBuffer::hasNoReadPointers() const
{
return readpointer_.size();
return readpointers_.empty();
}
//
......@@ -173,24 +161,24 @@ RingBuffer::getNbReadPointer()
// This one puts some data inside the ring buffer.
void
RingBuffer::Put(void* buffer, int toCopy)
RingBuffer::put(void* buffer, size_t toCopy)
{
int len = putLen();
const size_t len = putLength();
if (toCopy > bufferSize_ - len)
toCopy = bufferSize_ - len;
unsigned char *src = (unsigned char *) buffer;
unsigned char *src = static_cast<unsigned char *>(buffer);
int pos = endPos_;
size_t pos = endPos_;
while (toCopy) {
int block = toCopy;
size_t block = toCopy;
if (block > bufferSize_ - pos) // Wrap block around ring ?
block = bufferSize_ - pos; // Fill in to the end of the buffer
memcpy(buffer_ + pos, src, block);
memcpy(&(*buffer_.begin()) + pos, src, block);
src += block;
pos = (pos + block) % bufferSize_;
toCopy -= block;
......@@ -202,41 +190,40 @@ RingBuffer::Put(void* buffer, int toCopy)
// For the reader only:
//
int
RingBuffer::AvailForGet(const std::string &call_id)
size_t
RingBuffer::availableForGet(const std::string &call_id) const
{
// Used space
return getLen(call_id);
return getLength(call_id);
}
// Get will move 'toCopy' bytes from the internal FIFO to 'buffer'
int
RingBuffer::Get(void *buffer, int toCopy, const std::string &call_id)
size_t
RingBuffer::get(void *buffer, size_t toCopy, const std::string &call_id)
{
if (getNbReadPointer() == 0)
if (hasNoReadPointers())
return 0;
if (!hasThisReadPointer(call_id))
if (not hasThisReadPointer(call_id))
return 0;
int len = getLen(call_id);
const size_t len = getLength(call_id);
if (toCopy > len)
toCopy = len;
int copied = toCopy;
const size_t copied = toCopy;
unsigned char *dest = (unsigned char *) buffer;
int startPos = getReadPointer(call_id);
size_t startPos = getReadPointer(call_id);
while (toCopy) {
int block = toCopy;
while (toCopy > 0) {
size_t block = toCopy;
if (block > bufferSize_ - startPos)
block = bufferSize_ - startPos;
memcpy(dest, buffer_ + startPos, block);
memcpy(dest, &(*buffer_.begin()) + startPos, block);
dest += block;
startPos = (startPos + block) % bufferSize_;
toCopy -= block;
......@@ -246,15 +233,15 @@ RingBuffer::Get(void *buffer, int toCopy, const std::string &call_id)
return copied;
}
int
RingBuffer::Discard(int toDiscard, const std::string &call_id)
size_t
RingBuffer::discard(size_t toDiscard, const std::string &call_id)
{
int len = getLen(call_id);
size_t len = getLength(call_id);
if (toDiscard > len)
toDiscard = len;
int startPos = (getReadPointer(call_id) + toDiscard) % bufferSize_;
size_t startPos = (getReadPointer(call_id) + toDiscard) % bufferSize_;
storeReadPointer(startPos, call_id);
......
......@@ -23,10 +23,11 @@
#define __RING_BUFFER__
#include <fstream>
#include <vector>
#include <map>
#include "noncopyable.h"
typedef std::map<std::string, int> ReadPointer;
typedef std::map<std::string, size_t> ReadPointer;
class RingBuffer {
public:
......@@ -34,12 +35,7 @@ class RingBuffer {
* Constructor
* @param size Size of the buffer to create
*/
RingBuffer(int size, const std::string &call_id);
/**
* Destructor
*/
~RingBuffer();
RingBuffer(size_t size, const std::string &call_id);