2
* Copyright (C) 2004, 2005, 2006, 2009, 2008, 2009, 2010 Savoir-Faire Linux Inc.
3
* Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 3 of the License, or
8
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
* Additional permission under GNU GPL version 3 section 7:
20
* If you modify this program, or any covered work, by linking or
21
* combining it with the OpenSSL project's OpenSSL library (or a
22
* modified version of that library), containing parts covered by the
23
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
24
* grants you additional permission to convey the resulting work.
25
* Corresponding Source for a non-source form of such a combination
26
* shall include the source code for the parts of OpenSSL used as well
27
* as that of the covered work.
30
#include "AudioRtpRecordHandler.h"
32
#include "audio/audiolayer.h"
38
static const SFLDataFormat initFadeinFactor = 32000;
40
AudioRtpRecord::AudioRtpRecord () : _audioCodec (NULL)
41
, _hasDynamicPayloadType (false)
43
, _micDataConverted (NULL)
44
, _micDataEncoded (NULL)
45
, _spkrDataDecoded (NULL)
46
, _spkrDataConverted (NULL)
48
, _audioLayerSampleRate (0)
49
, _codecSampleRate (0)
50
, _audioLayerFrameSize (0)
52
, _micFadeInComplete (false)
53
, _spkrFadeInComplete (false)
54
, _micAmplFactor (initFadeinFactor)
55
, _spkrAmplFactor (initFadeinFactor)
56
, _audioProcess (NULL)
57
, _noiseSuppress (NULL)
59
// _audioLayerFrameSize = manager->getAudioDriver()->getFrameSize(); // in ms
60
// _audioLayerSampleRate = manager->getAudioDriver()->getSampleRate();
62
_audioLayerFrameSize = Manager::instance().getAudioDriver()->getFrameSize(); // in ms
63
_audioLayerSampleRate = Manager::instance().getAudioDriver()->getSampleRate();
68
AudioRtpRecord::~AudioRtpRecord()
75
if (_micDataConverted)
76
delete [] _micDataConverted;
78
_micDataConverted = NULL;
81
delete [] _micDataEncoded;
83
_micDataEncoded = NULL;
86
delete [] _spkrDataDecoded;
88
_spkrDataDecoded = NULL;
90
if (_spkrDataConverted)
91
delete [] _spkrDataConverted;
93
_spkrDataConverted = NULL;
107
delete _audioProcess;
108
_audioProcess = NULL;
111
if (_noiseSuppress) {
112
delete _noiseSuppress;
113
_noiseSuppress = NULL;
118
AudioRtpRecordHandler::AudioRtpRecordHandler (ManagerImpl *manager, SIPCall *ca) : _audioRtpRecord (), _ca (ca) {}
121
AudioRtpRecordHandler::~AudioRtpRecordHandler() {}
123
void AudioRtpRecordHandler::setRtpMedia (AudioCodec* audioCodec)
126
// Set varios codec info to reduce indirection
127
_audioRtpRecord._audioCodec = audioCodec;
128
_audioRtpRecord._codecPayloadType = audioCodec->getPayload();
129
_audioRtpRecord._codecSampleRate = audioCodec->getClockRate();
130
_audioRtpRecord._codecFrameSize = audioCodec->getFrameSize();
131
_audioRtpRecord._hasDynamicPayloadType = audioCodec->hasDynamicPayload();
135
void AudioRtpRecordHandler::init()
137
// init noise reduction process
138
// _noiseSuppress = new NoiseSuppress (getCodecFrameSize(), getCodecSampleRate());
139
// _audioProcess = new AudioProcessing (_noiseSuppress);
141
// _audioRtpRecord._noiseSuppress
145
void AudioRtpRecordHandler::initBuffers()
147
int codecSampleRate = _audioRtpRecord._codecSampleRate;
149
// Set sampling rate, main buffer choose the highest one
150
Manager::instance().getAudioDriver()->getMainBuffer()->setInternalSamplingRate (codecSampleRate);
152
// initialize SampleRate converter using AudioLayer's sampling rate
153
// (internal buffers initialized with maximal sampling rate and frame size)
154
_audioRtpRecord._converter = new SamplerateConverter (getAudioLayerSampleRate(), getAudioLayerFrameSize());
156
// int nbSamplesMax = (int) ( (getCodecSampleRate() * getAudioLayerFrameSize() / 1000)) * 2;
157
int nbSamplesMax = (int) ( (getCodecSampleRate() * 44100 / 1000)) * 2;
158
_audioRtpRecord._micData = new SFLDataFormat[nbSamplesMax];
159
_audioRtpRecord._micDataConverted = new SFLDataFormat[nbSamplesMax];
160
_audioRtpRecord._micDataEncoded = new unsigned char[nbSamplesMax * 2];
161
_audioRtpRecord._spkrDataConverted = new SFLDataFormat[nbSamplesMax];
162
_audioRtpRecord._spkrDataDecoded = new SFLDataFormat[nbSamplesMax];
164
Manager::instance().addStream (_ca->getCallId());
167
void AudioRtpRecordHandler::initNoiseSuppress()
169
NoiseSuppress *noiseSuppress = new NoiseSuppress (getCodecFrameSize(), getCodecSampleRate());
170
AudioProcessing *processing = new AudioProcessing (noiseSuppress);
172
_audioRtpRecord._noiseSuppress = noiseSuppress;
173
_audioRtpRecord._audioProcess = processing;
176
void AudioRtpRecordHandler::putDtmfEvent (int digit)
178
sfl::DtmfEvent *dtmf = new sfl::DtmfEvent();
179
dtmf->payload.event = digit;
180
dtmf->payload.ebit = false; // end of event bit
181
dtmf->payload.rbit = false; // reserved bit
182
dtmf->payload.duration = 1; // duration for this event
183
dtmf->newevent = true;
185
getEventQueue()->push_back (dtmf);
186
_debug ("AudioRtpSession: Put Dtmf Event %d", getEventQueue()->size());
189
int AudioRtpRecordHandler::processDataEncode (void)
192
AudioCodec *audioCodec = _audioRtpRecord._audioCodec;
193
AudioLayer *audioLayer = Manager::instance().getAudioDriver();
195
SFLDataFormat *micData = _audioRtpRecord._micData;
196
unsigned char *micDataEncoded = _audioRtpRecord._micDataEncoded;
197
SFLDataFormat *micDataConverted = _audioRtpRecord._micDataConverted;
202
int codecFrameSize = audioCodec->getFrameSize();
203
int codecSampleRate = audioCodec->getClockRate();
205
int mainBufferSampleRate = audioLayer->getMainBuffer()->getInternalSamplingRate();
207
// compute codec framesize in ms
208
float fixedCodecFramesize = computeCodecFrameSize (codecFrameSize, codecSampleRate);
210
// compute nb of byte to get coresponding to 20 ms at audio layer frame size (44.1 khz)
211
int bytesToGet = computeNbByteAudioLayer (fixedCodecFramesize);
213
// available bytes inside ringbuffer
214
int availBytesFromMic = audioLayer->getMainBuffer()->availForGet (_ca->getCallId());
216
if (availBytesFromMic < bytesToGet)
219
// Get bytes from micRingBuffer to data_from_mic
220
int nbSample = audioLayer->getMainBuffer()->getData (micData, bytesToGet, 100, _ca->getCallId()) / sizeof (SFLDataFormat);
222
// process mic fade in
223
if (!_audioRtpRecord._micFadeInComplete)
224
_audioRtpRecord._micFadeInComplete = fadeIn (micData, nbSample, &_audioRtpRecord._micAmplFactor);
229
// nb bytes to be sent over RTP
232
// test if resampling is required
233
if (codecSampleRate != mainBufferSampleRate) {
235
int nbSampleUp = nbSample;
237
nbSample = _audioRtpRecord._converter->downsampleData (micData, micDataConverted, codecSampleRate, mainBufferSampleRate, nbSampleUp);
239
if (Manager::instance().audioPreference.getNoiseReduce())
240
_audioRtpRecord._audioProcess->processAudio (micDataConverted, nbSample * sizeof (SFLDataFormat));
242
compSize = audioCodec->codecEncode (micDataEncoded, micDataConverted, nbSample * sizeof (SFLDataFormat));
244
if (Manager::instance().audioPreference.getNoiseReduce())
245
_audioRtpRecord._audioProcess->processAudio (micData, nbSample * sizeof (SFLDataFormat));
247
// no resampling required
248
compSize = audioCodec->codecEncode (micDataEncoded, micData, nbSample * sizeof (SFLDataFormat));
254
void AudioRtpRecordHandler::processDataDecode (unsigned char *spkrData, unsigned int size)
257
AudioCodec *audioCodec = _audioRtpRecord._audioCodec;
258
AudioLayer *audioLayer = Manager::instance().getAudioDriver();
263
int codeFrameSize = audioCodec->getFrameSize();
264
int codecSampleRate = audioCodec->getClockRate();
266
SFLDataFormat *spkrDataDecoded = _audioRtpRecord._spkrDataConverted;
267
SFLDataFormat *spkrDataConverted = _audioRtpRecord._spkrDataDecoded;
269
int mainBufferSampleRate = audioLayer->getMainBuffer()->getInternalSamplingRate();
271
// Return the size of data in bytes
272
int expandedSize = audioCodec->codecDecode (spkrDataDecoded , spkrData , size);
274
// buffer _receiveDataDecoded ----> short int or int16, coded on 2 bytes
275
int nbSample = expandedSize / sizeof (SFLDataFormat);
277
if (!_audioRtpRecord._spkrFadeInComplete)
278
_audioRtpRecord._spkrFadeInComplete = fadeIn (spkrDataDecoded, nbSample, &_audioRtpRecord._micAmplFactor);
280
// test if resampling is required
281
if (codecSampleRate != mainBufferSampleRate) {
283
// Do sample rate conversion
284
int nbSampleDown = nbSample;
286
nbSample = _audioRtpRecord._converter->upsampleData (spkrDataDecoded, spkrDataConverted, codecSampleRate, mainBufferSampleRate, nbSampleDown);
288
// put data in audio layer, size in byte
289
audioLayer->getMainBuffer()->putData (spkrDataConverted, nbSample * sizeof (SFLDataFormat), 100, _ca->getCallId());
293
// put data in audio layer, size in byte
294
audioLayer->getMainBuffer()->putData (spkrDataDecoded, expandedSize, 100, _ca->getCallId());
298
bool AudioRtpRecordHandler::fadeIn (SFLDataFormat *audio, int size, SFLDataFormat *factor)
301
// if factor reach 0, this function should no be called anymore
305
// apply amplitude factor;
308
audio[size] /= *factor;
312
*factor /= FADEIN_STEP_SIZE;