~ubuntu-branches/ubuntu/saucy/sflphone/saucy

« back to all changes in this revision

Viewing changes to sflphone-common/src/audio/audiortp/AudioRtpRecordHandler.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Francois Marier
  • Date: 2010-12-24 16:33:55 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20101224163355-tkvvikqxbrbav6up
Tags: 0.9.11-1
* New upstream release
* Add new build dependencies on libwebkit-dev and libyaml-dev

* Bump Standards-Version up to 3.9.1
* Bump debhelper compatibility to 8
* Patch another typo in the upstream code (lintian notice)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2004, 2005, 2006, 2009, 2008, 2009, 2010 Savoir-Faire Linux Inc.
 
3
 *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
 
4
 *
 
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.
 
13
 *
 
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.
 
17
 *
 
18
 *  Additional permission under GNU GPL version 3 section 7:
 
19
 *
 
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.
 
28
 */
 
29
 
 
30
#include "AudioRtpRecordHandler.h"
 
31
 
 
32
#include "audio/audiolayer.h"
 
33
#include "manager.h"
 
34
 
 
35
namespace sfl
 
36
{
 
37
 
 
38
static const SFLDataFormat initFadeinFactor = 32000;
 
39
 
 
40
AudioRtpRecord::AudioRtpRecord () : _audioCodec (NULL)
 
41
    , _hasDynamicPayloadType (false)
 
42
    , _micData (NULL)
 
43
    , _micDataConverted (NULL)
 
44
    , _micDataEncoded (NULL)
 
45
    , _spkrDataDecoded (NULL)
 
46
    , _spkrDataConverted (NULL)
 
47
    , _converter (NULL)
 
48
    , _audioLayerSampleRate (0)
 
49
    , _codecSampleRate (0)
 
50
    , _audioLayerFrameSize (0)
 
51
    , _codecFrameSize (0)
 
52
    , _micFadeInComplete (false)
 
53
    , _spkrFadeInComplete (false)
 
54
    , _micAmplFactor (initFadeinFactor)
 
55
    , _spkrAmplFactor (initFadeinFactor)
 
56
    , _audioProcess (NULL)
 
57
    , _noiseSuppress (NULL)
 
58
{
 
59
    // _audioLayerFrameSize = manager->getAudioDriver()->getFrameSize(); // in ms
 
60
    // _audioLayerSampleRate = manager->getAudioDriver()->getSampleRate();
 
61
 
 
62
    _audioLayerFrameSize = Manager::instance().getAudioDriver()->getFrameSize(); // in ms
 
63
    _audioLayerSampleRate = Manager::instance().getAudioDriver()->getSampleRate();
 
64
 
 
65
}
 
66
 
 
67
 
 
68
AudioRtpRecord::~AudioRtpRecord()
 
69
{
 
70
    if (_micData)
 
71
        delete [] _micData;
 
72
 
 
73
    _micData = NULL;
 
74
 
 
75
    if (_micDataConverted)
 
76
        delete [] _micDataConverted;
 
77
 
 
78
    _micDataConverted = NULL;
 
79
 
 
80
    if (_micDataEncoded)
 
81
        delete [] _micDataEncoded;
 
82
 
 
83
    _micDataEncoded = NULL;
 
84
 
 
85
    if (_spkrDataDecoded)
 
86
        delete [] _spkrDataDecoded;
 
87
 
 
88
    _spkrDataDecoded = NULL;
 
89
 
 
90
    if (_spkrDataConverted)
 
91
        delete [] _spkrDataConverted;
 
92
 
 
93
    _spkrDataConverted = NULL;
 
94
 
 
95
 
 
96
    if (_converter)
 
97
        delete _converter;
 
98
 
 
99
    _converter = NULL;
 
100
 
 
101
    if (_audioCodec) {
 
102
        delete _audioCodec;
 
103
        _audioCodec = NULL;
 
104
    }
 
105
 
 
106
    if (_audioProcess) {
 
107
        delete _audioProcess;
 
108
        _audioProcess = NULL;
 
109
    }
 
110
 
 
111
    if (_noiseSuppress) {
 
112
        delete _noiseSuppress;
 
113
        _noiseSuppress = NULL;
 
114
    }
 
115
}
 
116
 
 
117
 
 
118
AudioRtpRecordHandler::AudioRtpRecordHandler (ManagerImpl *manager, SIPCall *ca) : _audioRtpRecord (), _ca (ca) {}
 
119
 
 
120
 
 
121
AudioRtpRecordHandler::~AudioRtpRecordHandler() {}
 
122
 
 
123
void AudioRtpRecordHandler::setRtpMedia (AudioCodec* audioCodec)
 
124
{
 
125
 
 
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();
 
132
}
 
133
 
 
134
 
 
135
void AudioRtpRecordHandler::init()
 
136
{
 
137
    // init noise reduction process
 
138
    // _noiseSuppress = new NoiseSuppress (getCodecFrameSize(), getCodecSampleRate());
 
139
    // _audioProcess = new AudioProcessing (_noiseSuppress);
 
140
 
 
141
    // _audioRtpRecord._noiseSuppress
 
142
    // _audioRtpRecord.
 
143
}
 
144
 
 
145
void AudioRtpRecordHandler::initBuffers()
 
146
{
 
147
    int codecSampleRate = _audioRtpRecord._codecSampleRate;
 
148
 
 
149
    // Set sampling rate, main buffer choose the highest one
 
150
    Manager::instance().getAudioDriver()->getMainBuffer()->setInternalSamplingRate (codecSampleRate);
 
151
 
 
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());
 
155
 
 
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];
 
163
 
 
164
    Manager::instance().addStream (_ca->getCallId());
 
165
}
 
166
 
 
167
void AudioRtpRecordHandler::initNoiseSuppress()
 
168
{
 
169
    NoiseSuppress *noiseSuppress = new NoiseSuppress (getCodecFrameSize(), getCodecSampleRate());
 
170
    AudioProcessing *processing = new AudioProcessing (noiseSuppress);
 
171
 
 
172
    _audioRtpRecord._noiseSuppress = noiseSuppress;
 
173
    _audioRtpRecord._audioProcess = processing;
 
174
}
 
175
 
 
176
void AudioRtpRecordHandler::putDtmfEvent (int digit)
 
177
{
 
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;
 
184
    dtmf->length = 1000;
 
185
    getEventQueue()->push_back (dtmf);
 
186
    _debug ("AudioRtpSession: Put Dtmf Event %d", getEventQueue()->size());
 
187
}
 
188
 
 
189
int AudioRtpRecordHandler::processDataEncode (void)
 
190
{
 
191
 
 
192
    AudioCodec *audioCodec = _audioRtpRecord._audioCodec;
 
193
    AudioLayer *audioLayer = Manager::instance().getAudioDriver();
 
194
 
 
195
    SFLDataFormat *micData = _audioRtpRecord._micData;
 
196
    unsigned char *micDataEncoded = _audioRtpRecord._micDataEncoded;
 
197
    SFLDataFormat *micDataConverted = _audioRtpRecord._micDataConverted;
 
198
 
 
199
    assert (audioCodec);
 
200
    assert (audioLayer);
 
201
 
 
202
    int codecFrameSize = audioCodec->getFrameSize();
 
203
    int codecSampleRate = audioCodec->getClockRate();
 
204
 
 
205
    int mainBufferSampleRate = audioLayer->getMainBuffer()->getInternalSamplingRate();
 
206
 
 
207
    // compute codec framesize in ms
 
208
    float fixedCodecFramesize = computeCodecFrameSize (codecFrameSize, codecSampleRate);
 
209
 
 
210
    // compute nb of byte to get coresponding to 20 ms at audio layer frame size (44.1 khz)
 
211
    int bytesToGet = computeNbByteAudioLayer (fixedCodecFramesize);
 
212
 
 
213
    // available bytes inside ringbuffer
 
214
    int availBytesFromMic = audioLayer->getMainBuffer()->availForGet (_ca->getCallId());
 
215
 
 
216
    if (availBytesFromMic < bytesToGet)
 
217
        return 0;
 
218
 
 
219
    // Get bytes from micRingBuffer to data_from_mic
 
220
    int nbSample = audioLayer->getMainBuffer()->getData (micData, bytesToGet, 100, _ca->getCallId()) / sizeof (SFLDataFormat);
 
221
 
 
222
    // process mic fade in
 
223
    if (!_audioRtpRecord._micFadeInComplete)
 
224
        _audioRtpRecord._micFadeInComplete = fadeIn (micData, nbSample, &_audioRtpRecord._micAmplFactor);
 
225
 
 
226
    if (nbSample == 0)
 
227
        return nbSample;
 
228
 
 
229
    // nb bytes to be sent over RTP
 
230
    int compSize = 0;
 
231
 
 
232
    // test if resampling is required
 
233
    if (codecSampleRate != mainBufferSampleRate) {
 
234
 
 
235
        int nbSampleUp = nbSample;
 
236
 
 
237
        nbSample = _audioRtpRecord._converter->downsampleData (micData, micDataConverted, codecSampleRate, mainBufferSampleRate, nbSampleUp);
 
238
 
 
239
        if (Manager::instance().audioPreference.getNoiseReduce())
 
240
            _audioRtpRecord._audioProcess->processAudio (micDataConverted, nbSample * sizeof (SFLDataFormat));
 
241
 
 
242
        compSize = audioCodec->codecEncode (micDataEncoded, micDataConverted, nbSample * sizeof (SFLDataFormat));
 
243
    } else {
 
244
        if (Manager::instance().audioPreference.getNoiseReduce())
 
245
            _audioRtpRecord._audioProcess->processAudio (micData, nbSample * sizeof (SFLDataFormat));
 
246
 
 
247
        // no resampling required
 
248
        compSize = audioCodec->codecEncode (micDataEncoded, micData, nbSample * sizeof (SFLDataFormat));
 
249
    }
 
250
 
 
251
    return compSize;
 
252
}
 
253
 
 
254
void AudioRtpRecordHandler::processDataDecode (unsigned char *spkrData, unsigned int size)
 
255
{
 
256
 
 
257
    AudioCodec *audioCodec = _audioRtpRecord._audioCodec;
 
258
    AudioLayer *audioLayer = Manager::instance().getAudioDriver();
 
259
 
 
260
    if (!audioCodec)
 
261
        return;
 
262
 
 
263
    int codeFrameSize = audioCodec->getFrameSize();
 
264
    int codecSampleRate = audioCodec->getClockRate();
 
265
 
 
266
    SFLDataFormat *spkrDataDecoded = _audioRtpRecord._spkrDataConverted;
 
267
    SFLDataFormat *spkrDataConverted = _audioRtpRecord._spkrDataDecoded;
 
268
 
 
269
    int mainBufferSampleRate = audioLayer->getMainBuffer()->getInternalSamplingRate();
 
270
 
 
271
    // Return the size of data in bytes
 
272
    int expandedSize = audioCodec->codecDecode (spkrDataDecoded , spkrData , size);
 
273
 
 
274
    // buffer _receiveDataDecoded ----> short int or int16, coded on 2 bytes
 
275
    int nbSample = expandedSize / sizeof (SFLDataFormat);
 
276
 
 
277
    if (!_audioRtpRecord._spkrFadeInComplete)
 
278
        _audioRtpRecord._spkrFadeInComplete = fadeIn (spkrDataDecoded, nbSample, &_audioRtpRecord._micAmplFactor);
 
279
 
 
280
    // test if resampling is required
 
281
    if (codecSampleRate != mainBufferSampleRate) {
 
282
 
 
283
        // Do sample rate conversion
 
284
        int nbSampleDown = nbSample;
 
285
 
 
286
        nbSample = _audioRtpRecord._converter->upsampleData (spkrDataDecoded, spkrDataConverted, codecSampleRate, mainBufferSampleRate, nbSampleDown);
 
287
 
 
288
        // put data in audio layer, size in byte
 
289
        audioLayer->getMainBuffer()->putData (spkrDataConverted, nbSample * sizeof (SFLDataFormat), 100, _ca->getCallId());
 
290
 
 
291
 
 
292
    } else {
 
293
        // put data in audio layer, size in byte
 
294
        audioLayer->getMainBuffer()->putData (spkrDataDecoded, expandedSize, 100, _ca->getCallId());
 
295
    }
 
296
}
 
297
 
 
298
bool AudioRtpRecordHandler::fadeIn (SFLDataFormat *audio, int size, SFLDataFormat *factor)
 
299
{
 
300
 
 
301
    // if factor reach 0, this function should no be called anymore
 
302
    if (*factor <= 0)
 
303
        return true;
 
304
 
 
305
    // apply amplitude factor;
 
306
    while (size) {
 
307
        size--;
 
308
        audio[size] /= *factor;
 
309
    }
 
310
 
 
311
    // decrease factor
 
312
    *factor /= FADEIN_STEP_SIZE;
 
313
 
 
314
    return false;
 
315
 
 
316
}
 
317
 
 
318
}
 
319