Skip to content
Snippets Groups Projects
Commit 859e5e3a authored by Tristan Matthews's avatar Tristan Matthews
Browse files

* #9979: use std::tr1::array instead of plain array for audio buffers

This will allow us to do more compile-time bounds checking.
parent 42b52b0c
No related branches found
No related tags found
No related merge requests found
...@@ -37,28 +37,27 @@ ...@@ -37,28 +37,27 @@
namespace sfl { namespace sfl {
static const SFLDataFormat initFadeinFactor = 32000; static const SFLDataFormat INIT_FADE_IN_FACTOR = 32000;
AudioRtpRecord::AudioRtpRecord() : AudioRtpRecord::AudioRtpRecord() :
audioCodec_(0) audioCodec_(0)
, audioCodecMutex_() , audioCodecMutex_()
, codecPayloadType_(0) , codecPayloadType_(0)
, hasDynamicPayloadType_(false) , hasDynamicPayloadType_(false)
, decData_() // std::tr1::arrays will be 0-initialized
, resampledData_()
, encodedData_()
, converter_(0) , converter_(0)
, codecSampleRate_(0) , codecSampleRate_(0)
, codecFrameSize_(0) , codecFrameSize_(0)
, converterSamplingRate_(0) , converterSamplingRate_(0)
, dtmfQueue_() , dtmfQueue_()
, micAmplFactor_(initFadeinFactor) , micAmplFactor_(INIT_FADE_IN_FACTOR)
, noiseSuppress_(0) , noiseSuppress_(0)
, audioProcessMutex_() , audioProcessMutex_()
, callId_("") , callId_("")
, dtmfPayloadType_(101) // same as Asterisk , dtmfPayloadType_(101) // same as Asterisk
{ {}
memset(decData_, 0x0, sizeof decData_);
memset(resampledData_, 0x0, sizeof resampledData_);
memset(encodedData_, 0x0, sizeof encodedData_);
}
AudioRtpRecord::~AudioRtpRecord() AudioRtpRecord::~AudioRtpRecord()
{ {
...@@ -116,14 +115,10 @@ void AudioRtpRecordHandler::putDtmfEvent(int digit) ...@@ -116,14 +115,10 @@ void AudioRtpRecordHandler::putDtmfEvent(int digit)
int AudioRtpRecordHandler::processDataEncode() int AudioRtpRecordHandler::processDataEncode()
{ {
SFLDataFormat *micData = audioRtpRecord_.decData_;
unsigned char *micDataEncoded = audioRtpRecord_.encodedData_;
SFLDataFormat *micDataConverted = audioRtpRecord_.resampledData_;
int codecSampleRate = getCodecSampleRate(); int codecSampleRate = getCodecSampleRate();
int mainBufferSampleRate = Manager::instance().getMainBuffer()->getInternalSamplingRate(); int mainBufferSampleRate = Manager::instance().getMainBuffer()->getInternalSamplingRate();
double resampleFactor = (double)mainBufferSampleRate / codecSampleRate; double resampleFactor = (double) mainBufferSampleRate / codecSampleRate;
// compute nb of byte to get coresponding to 1 audio frame // compute nb of byte to get coresponding to 1 audio frame
int samplesToGet = resampleFactor * getCodecFrameSize(); int samplesToGet = resampleFactor * getCodecFrameSize();
...@@ -132,10 +127,12 @@ int AudioRtpRecordHandler::processDataEncode() ...@@ -132,10 +127,12 @@ int AudioRtpRecordHandler::processDataEncode()
if (Manager::instance().getMainBuffer()->availForGet(id_) < bytesToGet) if (Manager::instance().getMainBuffer()->availForGet(id_) < bytesToGet)
return 0; return 0;
SFLDataFormat *micData = audioRtpRecord_.decData_.data();
int bytes = Manager::instance().getMainBuffer()->getData(micData, bytesToGet, id_); int bytes = Manager::instance().getMainBuffer()->getData(micData, bytesToGet, id_);
if (bytes != bytesToGet) { if (bytes != bytesToGet) {
ERROR("%s : asked %d bytes from mainbuffer, got %d", __PRETTY_FUNCTION__, bytesToGet, bytes); ERROR("%s : asked %d bytes from mainbuffer, got %d",
__PRETTY_FUNCTION__, bytesToGet, bytes);
return 0; return 0;
} }
...@@ -147,6 +144,7 @@ int AudioRtpRecordHandler::processDataEncode() ...@@ -147,6 +144,7 @@ int AudioRtpRecordHandler::processDataEncode()
echoCanceller.getData(micData); echoCanceller.getData(micData);
SFLDataFormat *out = micData; SFLDataFormat *out = micData;
SFLDataFormat *micDataConverted = audioRtpRecord_.resampledData_.data();
if (codecSampleRate != mainBufferSampleRate) { if (codecSampleRate != mainBufferSampleRate) {
out = micDataConverted; out = micDataConverted;
...@@ -161,6 +159,7 @@ int AudioRtpRecordHandler::processDataEncode() ...@@ -161,6 +159,7 @@ int AudioRtpRecordHandler::processDataEncode()
{ {
ost::MutexLock lock(audioRtpRecord_.audioCodecMutex_); ost::MutexLock lock(audioRtpRecord_.audioCodecMutex_);
unsigned char *micDataEncoded = audioRtpRecord_.encodedData_.data();
return audioRtpRecord_.audioCodec_->encode(micDataEncoded, out, getCodecFrameSize()); return audioRtpRecord_.audioCodec_->encode(micDataEncoded, out, getCodecFrameSize());
} }
} }
...@@ -172,16 +171,14 @@ void AudioRtpRecordHandler::processDataDecode(unsigned char *spkrData, size_t si ...@@ -172,16 +171,14 @@ void AudioRtpRecordHandler::processDataDecode(unsigned char *spkrData, size_t si
int codecSampleRate = getCodecSampleRate(); int codecSampleRate = getCodecSampleRate();
SFLDataFormat *spkrDataDecoded = audioRtpRecord_.decData_;
SFLDataFormat *spkrDataConverted = audioRtpRecord_.resampledData_;
int mainBufferSampleRate = Manager::instance().getMainBuffer()->getInternalSamplingRate(); int mainBufferSampleRate = Manager::instance().getMainBuffer()->getInternalSamplingRate();
int inSamples; int inSamples = 0;
SFLDataFormat *spkrDataDecoded = audioRtpRecord_.decData_.data();
{ {
ost::MutexLock lock(audioRtpRecord_.audioCodecMutex_); ost::MutexLock lock(audioRtpRecord_.audioCodecMutex_);
// Return the size of data in samples // Return the size of data in samples
inSamples = audioRtpRecord_.audioCodec_->decode(spkrDataDecoded , spkrData , size); inSamples = audioRtpRecord_.audioCodec_->decode(spkrDataDecoded, spkrData, size);
} }
fadeIn(spkrDataDecoded, inSamples, &audioRtpRecord_.micAmplFactor_); fadeIn(spkrDataDecoded, inSamples, &audioRtpRecord_.micAmplFactor_);
...@@ -194,10 +191,10 @@ void AudioRtpRecordHandler::processDataDecode(unsigned char *spkrData, size_t si ...@@ -194,10 +191,10 @@ void AudioRtpRecordHandler::processDataDecode(unsigned char *spkrData, size_t si
// test if resampling is required // test if resampling is required
if (codecSampleRate != mainBufferSampleRate) { if (codecSampleRate != mainBufferSampleRate) {
out = audioRtpRecord_.resampledData_.data();
// Do sample rate conversion // Do sample rate conversion
outSamples = ((float) inSamples * ((float) mainBufferSampleRate / (float) codecSampleRate)); outSamples = ((float) inSamples * ((float) mainBufferSampleRate / (float) codecSampleRate));
audioRtpRecord_.converter_->resample(spkrDataDecoded, spkrDataConverted, codecSampleRate, mainBufferSampleRate, inSamples); audioRtpRecord_.converter_->resample(spkrDataDecoded, out, codecSampleRate, mainBufferSampleRate, inSamples);
out = spkrDataConverted;
} }
if (Manager::instance().getEchoCancelState()) if (Manager::instance().getEchoCancelState())
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
using std::ptrdiff_t; using std::ptrdiff_t;
#include <ccrtp/rtp.h> #include <ccrtp/rtp.h>
#include <tr1/array>
#include <list> #include <list>
class SIPCall; class SIPCall;
...@@ -72,7 +73,7 @@ timeval2microtimeout(const timeval& t) ...@@ -72,7 +73,7 @@ timeval2microtimeout(const timeval& t)
/** /**
* Class meant to store internal data in order to encode/decode, * Class meant to store internal data in order to encode/decode,
* resample, process, and packetize audio streams. This class should not be * resample, process, and packetize audio streams. This class should not be
* handled directly. Use AudioRtpRecorrdHandeler * handled directly. Use AudioRtpRecordHandler
*/ */
class AudioRtpRecord { class AudioRtpRecord {
public: public:
...@@ -83,9 +84,9 @@ class AudioRtpRecord { ...@@ -83,9 +84,9 @@ class AudioRtpRecord {
ost::Mutex audioCodecMutex_; ost::Mutex audioCodecMutex_;
int codecPayloadType_; int codecPayloadType_;
bool hasDynamicPayloadType_; bool hasDynamicPayloadType_;
SFLDataFormat decData_[DEC_BUFFER_SIZE]; std::tr1::array<SFLDataFormat, DEC_BUFFER_SIZE> decData_;
SFLDataFormat resampledData_[DEC_BUFFER_SIZE]; std::tr1::array<SFLDataFormat, DEC_BUFFER_SIZE> resampledData_;
unsigned char encodedData_[DEC_BUFFER_SIZE]; std::tr1::array<unsigned char, DEC_BUFFER_SIZE> encodedData_;
SamplerateConverter *converter_; SamplerateConverter *converter_;
int codecSampleRate_; int codecSampleRate_;
int codecFrameSize_; int codecFrameSize_;
...@@ -136,7 +137,7 @@ class AudioRtpRecordHandler { ...@@ -136,7 +137,7 @@ class AudioRtpRecordHandler {
} }
const unsigned char *getMicDataEncoded() const { const unsigned char *getMicDataEncoded() const {
return audioRtpRecord_.encodedData_; return audioRtpRecord_.encodedData_.data();
} }
void initBuffers(); void initBuffers();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment