Commit 28a7791b authored by Alexandre Savard's avatar Alexandre Savard

[#1881] MainBuffer implemented in AudioLayer/AudioRTP

However, Microphone Capture using alsa cannot be copied inot ring buffer.
The application was by passing the micRingBuffer in this case...
parent ba8fd56e
......@@ -144,6 +144,7 @@ AlsaLayer::stopStream (void)
int
AlsaLayer::canGetMic()
{
/*
int avail;
if (!_CaptureHandle)
......@@ -156,6 +157,8 @@ AlsaLayer::canGetMic()
return 0;
} else
return ( (avail<0) ?0:avail);
*/
return 0;
/*
if(_CaptureHandle)
......@@ -174,14 +177,16 @@ AlsaLayer::getMic (void *buffer, int toCopy)
}
else
return 0;*/
/*
int res = 0;
if (_CaptureHandle) {
res = read (buffer, toCopy);
adjustVolume (buffer, toCopy, SFL_PCM_CAPTURE);
}
return res;
*/
// return res;
return 0;
}
bool AlsaLayer::isCaptureActive (void)
......@@ -725,7 +730,7 @@ void AlsaLayer::audioCallback (void)
free (out);
out=0;
// Consume the regular one as well (same amount of bytes)
_voiceRingBuffer.Discard (toGet);
_mainBuffer.discard (toGet);
} else {
tone = _manager->getTelephoneTone();
toGet = 940 ;
......@@ -741,12 +746,12 @@ void AlsaLayer::audioCallback (void)
write (out , maxBytes);
} else {
// If nothing urgent, play the regular sound samples
normalAvail = _voiceRingBuffer.AvailForGet();
normalAvail = _mainBuffer.availForGet();
toGet = (normalAvail < (int) (framesPerBufferAlsa * sizeof (SFLDataFormat))) ? normalAvail : framesPerBufferAlsa * sizeof (SFLDataFormat);
out = (SFLDataFormat*) malloc (framesPerBufferAlsa * sizeof (SFLDataFormat));
if (toGet) {
_voiceRingBuffer.Get (out, toGet, spkrVolume);
_mainBuffer.getData (out, toGet, spkrVolume);
write (out, toGet);
} else {
bzero (out, framesPerBufferAlsa * sizeof (SFLDataFormat));
......@@ -759,19 +764,27 @@ void AlsaLayer::audioCallback (void)
}
// Additionally handle the mic's audio stream
//if(is_capture_running()){
/*micAvailAlsa = snd_pcm_avail_update(_CaptureHandle);
if(micAvailAlsa > 0) {
micAvailPut = _micRingBuffer.AvailForPut();
toPut = (micAvailAlsa <= micAvailPut) ? micAvailAlsa : micAvailPut;
in = (SFLDataFormat*)malloc(toPut * sizeof(SFLDataFormat));
toPut = read (in, toPut);
if (in != 0)
{
_micRingBuffer.Put(in, toPut, 100);
int micAvailAlsa;
int micAvailPut;
int toPut;
SFLDataFormat* in;
if(is_capture_running())
{
micAvailAlsa = snd_pcm_avail_update(_CaptureHandle);
if(micAvailAlsa > 0)
{
micAvailPut = _mainBuffer.availForPut();
toPut = (micAvailAlsa <= micAvailPut) ? micAvailAlsa : micAvailPut;
in = (SFLDataFormat*)malloc(toPut * sizeof(SFLDataFormat));
toPut = read (in, toPut);
if (in != 0)
{
_mainBuffer.putData(in, toPut, 100);
}
free(in); in=0;
}
free(in); in=0;
}*/
}
}
void* AlsaLayer::adjustVolume (void* buffer , int len, int stream)
......
......@@ -22,7 +22,9 @@
void AudioLayer::flushMain (void)
{
ost::MutexLock guard (_mutex);
_voiceRingBuffer.flush();
// should pass call id
_mainBuffer.flush();
}
void AudioLayer::flushUrgent (void)
......@@ -34,7 +36,7 @@ void AudioLayer::flushUrgent (void)
void AudioLayer::flushMic (void)
{
ost::MutexLock guard (_mutex);
_micRingBuffer.flush();
_mainBuffer.flushDefault();
}
int AudioLayer::putUrgent (void* buffer, int toCopy)
......@@ -53,18 +55,18 @@ int AudioLayer::putUrgent (void* buffer, int toCopy)
return 0;
}
int AudioLayer::putMain (void *buffer, int toCopy)
int AudioLayer::putMain (void *buffer, int toCopy, CallID call_id)
{
int a;
ost::MutexLock guard (_mutex);
a = _voiceRingBuffer.AvailForPut();
a = _mainBuffer.availForPut(call_id);
if (a >= toCopy) {
return _voiceRingBuffer.Put (buffer, toCopy, _defaultVolume);
return _mainBuffer.putData (buffer, toCopy, _defaultVolume, call_id);
} else {
_debug ("Chopping sound, Ouch! RingBuffer full ?\n");
return _voiceRingBuffer.Put (buffer, a, _defaultVolume);
return _mainBuffer.putData (buffer, a, _defaultVolume, call_id);
}
return 0;
......
......@@ -26,6 +26,7 @@
#include "audiodevice.h"
#include "ringbuffer.h"
#include "manager.h"
#include "mainbuffer.h"
#include <cc++/thread.h> // for ost::Mutex
......@@ -56,9 +57,7 @@ class AudioLayer {
: _defaultVolume(100)
, _layerType( type )
, _manager(manager)
, _voiceRingBuffer( SIZEBUF )
, _urgentRingBuffer( SIZEBUF)
, _micRingBuffer( SIZEBUF )
, _urgentRingBuffer( SIZEBUF )
, _indexIn ( 0 )
, _indexOut ( 0 )
, _sampleRate ( 0 )
......@@ -136,7 +135,7 @@ class AudioLayer {
* @param toCopy The size of the buffer
* @return int The number of bytes copied
*/
int putMain(void* buffer, int toCopy);
int putMain(void* buffer, int toCopy, CallID call_id = default_id);
void flushMain (void);
......@@ -193,6 +192,8 @@ class AudioLayer {
int getLayerType( void ) { return _layerType; }
MainBuffer* getMainBuffer( void ) { return &_mainBuffer; }
/**
* Default volume for incoming RTP and Urgent sounds.
*/
......@@ -218,9 +219,11 @@ class AudioLayer {
/**
* Urgent ring buffer used for ringtones
*/
RingBuffer _voiceRingBuffer;
// RingBuffer _voiceRingBuffer;
RingBuffer _urgentRingBuffer;
RingBuffer _micRingBuffer;
// RingBuffer _micRingBuffer;
MainBuffer _mainBuffer;
/**
* Number of audio cards on which capture stream has been opened
......
......@@ -179,6 +179,8 @@ AudioRtpRTX::~AudioRtpRTX ()
throw;
}
_audiolayer->getMainBuffer()->createRingBuffer(_ca->getCallId());
_ca = 0;
delete [] micData;
......@@ -222,6 +224,8 @@ AudioRtpRTX::initBuffers()
spkrDataConverted = new SFLDataFormat[nbSamplesMax];
spkrDataDecoded = new SFLDataFormat[nbSamplesMax];
_audiolayer->getMainBuffer()->createRingBuffer(_ca->getCallId());
}
......@@ -347,7 +351,7 @@ AudioRtpRTX::processDataEncode()
int maxBytesToGet = computeNbByteAudioLayer (fixed_codec_framesize);
// available bytes inside ringbuffer
int availBytesFromMic = _audiolayer->canGetMic();
int availBytesFromMic = _audiolayer->getMainBuffer()->availForGet(_ca->getCallId());
// set available byte to maxByteToGet
int bytesAvail = (availBytesFromMic < maxBytesToGet) ? availBytesFromMic : maxBytesToGet;
......@@ -356,7 +360,7 @@ AudioRtpRTX::processDataEncode()
return 0;
// Get bytes from micRingBuffer to data_from_mic
int nbSample = _audiolayer->getMic (micData , bytesAvail) / sizeof (SFLDataFormat);
int nbSample = _audiolayer->getMainBuffer()->getData (micData , bytesAvail, 100, _ca->getCallId()) / sizeof (SFLDataFormat);
// nb bytes to be sent over RTP
int compSize = 0;
......@@ -406,7 +410,7 @@ AudioRtpRTX::processDataDecode (unsigned char* spkrData, unsigned int size, int&
_nSamplesSpkr = nbSample;
// put data in audio layer, size in byte
_audiolayer->putMain (spkrDataConverted, nbSample * sizeof (SFLDataFormat));
_audiolayer->getMainBuffer()->putData (spkrDataConverted, nbSample * sizeof (SFLDataFormat), 100, _ca->getCallId());
} else {
......@@ -414,7 +418,7 @@ AudioRtpRTX::processDataDecode (unsigned char* spkrData, unsigned int size, int&
_nSamplesSpkr = nbSample;
// put data in audio layer, size in byte
_audiolayer->putMain (spkrDataDecoded, nbSample * sizeof (SFLDataFormat));
_audiolayer->getMainBuffer()->putData (spkrDataDecoded, nbSample * sizeof (SFLDataFormat), 100, _ca->getCallId());
}
// Notify (with a beep) an incoming call when there is already a call
......
......@@ -210,6 +210,13 @@ void MainBuffer::flush(CallID call_id)
}
void MainBuffer::flushDefault()
{
flushByID(default_id);
}
void MainBuffer::flushByID(CallID call_id)
{
......
......@@ -43,6 +43,10 @@ class MainBuffer {
~MainBuffer();
RingBuffer* createRingBuffer(CallID call_id);
bool removeRingBuffer(CallID call_id);
int putData(void *buffer, int toCopy, unsigned short volume = 100, CallID call_id = default_id);
int getData(void *buffer, int toCopy, unsigned short volume = 100, CallID call_id = default_id);
......@@ -55,14 +59,12 @@ class MainBuffer {
void flush(CallID call_id = default_id);
private:
void flushDefault();
RingBuffer* createRingBuffer(CallID call_id);
private:
RingBuffer* getRingBuffer(CallID call_id);
bool removeRingBuffer(CallID call_id);
int getDataByID(void *buffer, int toCopy, unsigned short volume = 100, CallID call_id = default_id);
int availForGetByID(CallID call_id = default_id);
......
......@@ -244,7 +244,7 @@ void PulseLayer::closePlaybackStream (void)
int PulseLayer::canGetMic()
{
if (record)
return _micRingBuffer.AvailForGet();
return 0;// _mainBuffer.availForGet(call_id);
else
return 0;
}
......@@ -252,7 +252,7 @@ int PulseLayer::canGetMic()
int PulseLayer::getMic (void *buffer, int toCopy)
{
if (record) {
return _micRingBuffer.Get (buffer, toCopy, 100);
return 0; // _mainBuffer.getData (buffer, toCopy, 100, call_id);
} else
return 0;
}
......@@ -261,8 +261,11 @@ void PulseLayer::startStream (void)
{
_debug ("PulseLayer::Start stream\n");
_urgentRingBuffer.flush();
_micRingBuffer.flush();
_voiceRingBuffer.flush();
// _micRingBuffer.flush();
// _voiceRingBuffer.flush();
_mainBuffer.flush();
_mainBuffer.flushDefault();
pa_threaded_mainloop_lock (m);
......@@ -355,7 +358,7 @@ void PulseLayer::writeToSpeaker (void)
_urgentRingBuffer.Get (out, toGet, 100);
pa_stream_write (playback->pulseStream() , out , toGet , pa_xfree, 0 , PA_SEEK_RELATIVE);
// Consume the regular one as well (same amount of bytes)
_voiceRingBuffer.Discard (toGet);
_mainBuffer.discard (toGet);
} else {
AudioLoop* tone = _manager->getTelephoneTone();
......@@ -374,12 +377,12 @@ void PulseLayer::writeToSpeaker (void)
pa_stream_write (playback->pulseStream() , out , toPlay , pa_xfree, 0 , PA_SEEK_RELATIVE) ;
} else {
out = (SFLDataFormat*) pa_xmalloc (framesPerBuffer * sizeof (SFLDataFormat));
normalAvail = _voiceRingBuffer.AvailForGet();
normalAvail = _mainBuffer.availForGet();
toGet = (normalAvail < (int) (framesPerBuffer * sizeof (SFLDataFormat))) ? normalAvail : framesPerBuffer * sizeof (SFLDataFormat);
if (toGet) {
_voiceRingBuffer.Get (out, toGet, 100);
_voiceRingBuffer.Discard (toGet);
_mainBuffer.getData (out, toGet, 100);
_mainBuffer.discard (toGet);
} else {
bzero (out, framesPerBuffer * sizeof (SFLDataFormat));
}
......@@ -402,7 +405,7 @@ void PulseLayer::readFromMic (void)
}
if (data != 0) {
_micRingBuffer.Put ( (void*) data ,r, 100);
_mainBuffer.putData ( (void*) data ,r, 100);
}
if (pa_stream_drop (record->pulseStream()) < 0) {
......
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