AudioRtpSession.cpp 10.7 KB
Newer Older
1
/*
2
 *  Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010, 2011 Savoir-Faire Linux Inc.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 *  Author: Pierre-Luc Bacon <pierre-luc.bacon@savoirfairelinux.com>
 *  Author: Alexandre Bourget <alexandre.bourget@savoirfairelinux.com>
 *  Author: Laurielle Lea <laurielle.lea@savoirfairelinux.com>
 *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
 *  Author: Yan Morin <yan.morin@savoirfairelinux.com>
 *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
 */

#include "AudioRtpSession.h"
#include "AudioSymmetricRtpSession.h"

#include "sip/sdp.h"
#include "audio/audiolayer.h"
#include <ccrtp/rtp.h>
#include <ccrtp/oqueue.h>

namespace sfl
{
AudioRtpSession::AudioRtpSession (SIPCall * sipcall, RtpMethod type, ost::RTPDataQueue *queue, ost::Thread *thread) :
46
47
					AudioRtpRecordHandler (sipcall)
					, _ca (sipcall)
48
49
50
					, _timestamp (0)
					, _timestampIncrement (0)
					, _timestampCount (0)
51
                    , _isStarted (false)
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
					, _type(type)
					, _queue(queue)
					, _thread(thread)
{
    assert (_ca);
    _queue->setTypeOfService (ost::RTPDataQueue::tosEnhanced);
}

AudioRtpSession::~AudioRtpSession()
{
    _info ("AudioRtpSession: Delete AudioRtpSession instance");
}

void AudioRtpSession::updateSessionMedia (AudioCodec *audioCodec)
{
    _debug ("AudioSymmetricRtpSession: Update session media");

    // Update internal codec for this session
    updateRtpMedia (audioCodec);

    int payloadType = getCodecPayloadType();
    int frameSize = getCodecFrameSize();
    int smplRate = getCodecSampleRate();
    int dynamic = getHasDynamicPayload();

    // G722 requires timetamp to be incremented at 8khz
    if (payloadType == g722PayloadType)
        _timestampIncrement = g722RtpTimeincrement;
    else
        _timestampIncrement = frameSize;

    _debug ("AudioRptSession: Codec payload: %d", payloadType);
    _debug ("AudioSymmetricRtpSession: Codec sampling rate: %d", smplRate);
    _debug ("AudioSymmetricRtpSession: Codec frame size: %d", frameSize);
    _debug ("AudioSymmetricRtpSession: RTP timestamp increment: %d", _timestampIncrement);


    if (payloadType == g722PayloadType) {
        _debug ("AudioSymmetricRtpSession: Setting G722 payload format");
        _queue->setPayloadFormat (ost::DynamicPayloadFormat ( (ost::PayloadType) payloadType, g722RtpClockRate));
    } else {
        if (dynamic) {
            _debug ("AudioSymmetricRtpSession: Setting dynamic payload format");
            _queue->setPayloadFormat (ost::DynamicPayloadFormat ( (ost::PayloadType) payloadType, smplRate));
        } else {
            _debug ("AudioSymmetricRtpSession: Setting static payload format");
            _queue->setPayloadFormat (ost::StaticPayloadFormat ( (ost::StaticPayloadType) payloadType));
        }
    }

    if (_type != Zrtp) {
		_ca->setRecordingSmplRate (getCodecSampleRate());
		_timestamp = _queue->getCurrentTimestamp();
    }
}

void AudioRtpSession::setSessionMedia (AudioCodec *audioCodec)
{
    _debug ("AudioSymmetricRtpSession: Set session media");

    // set internal codec info for this session
    setRtpMedia (audioCodec);

    // store codec info locally
    int payloadType = getCodecPayloadType();
    int frameSize = getCodecFrameSize();
    int smplRate = getCodecSampleRate();
    bool dynamic = getHasDynamicPayload();

    // G722 requires timestamp to be incremented at 8 kHz
    if (payloadType == g722PayloadType)
        _timestampIncrement = g722RtpTimeincrement;
    else
        _timestampIncrement = frameSize;

    _debug ("AudioRptSession: Codec payload: %d", payloadType);
    _debug ("AudioSymmetricRtpSession: Codec sampling rate: %d", smplRate);
    _debug ("AudioSymmetricRtpSession: Codec frame size: %d", frameSize);
    _debug ("AudioSymmetricRtpSession: RTP timestamp increment: %d", _timestampIncrement);

    if (payloadType == g722PayloadType) {
        _debug ("AudioSymmetricRtpSession: Setting G722 payload format");
        _queue->setPayloadFormat (ost::DynamicPayloadFormat ( (ost::PayloadType) payloadType, g722RtpClockRate));
    } else {
        if (dynamic) {
            _debug ("AudioSymmetricRtpSession: Setting dynamic payload format");
            _queue->setPayloadFormat (ost::DynamicPayloadFormat ( (ost::PayloadType) payloadType, smplRate));
        } else {
            _debug ("AudioSymmetricRtpSession: Setting static payload format");
            _queue->setPayloadFormat (ost::StaticPayloadFormat ( (ost::StaticPayloadType) payloadType));
        }
    }

    if (_type != Zrtp) {
		_ca->setRecordingSmplRate (getCodecSampleRate());
	}
}

void AudioRtpSession::sendDtmfEvent (sfl::DtmfEvent *dtmf)
{
	const int increment = (_type == Zrtp) ? 160 : _timestampIncrement;
    _debug ("AudioRtpSession: Send Dtmf");

    _timestamp += increment;

    // discard equivalent size of audio
    processDataEncode();

    // change Payload type for DTMF payload
    _queue->setPayloadFormat (ost::DynamicPayloadFormat ( (ost::PayloadType) getDtmfPayloadType(), 8000));

    // Set marker in case this is a new Event
    if (dtmf->newevent)
        _queue->setMark (true);

    // putData (_timestamp, (const unsigned char*) (& (dtmf->payload)), sizeof (ost::RTPPacket::RFC2833Payload));
    _queue->sendImmediate (_timestamp, (const unsigned char *) (& (dtmf->payload)), sizeof (ost::RTPPacket::RFC2833Payload));

    // This is no more a new event
    if (dtmf->newevent) {
        dtmf->newevent = false;
        _queue->setMark (false);
    }

    // get back the payload to audio
    _queue->setPayloadFormat (ost::StaticPayloadFormat ( (ost::StaticPayloadType) getCodecPayloadType()));

    // decrease length remaining to process for this event
    dtmf->length -= increment;

    dtmf->payload.duration++;

    // next packet is going to be the last one
    if ( (dtmf->length - increment) < increment)
        dtmf->payload.ebit = true;

    if (dtmf->length < increment) {
        delete dtmf;
        getEventQueue()->pop_front();
    }
}


void AudioRtpSession::receiveSpeakerData ()
{
197
198
    const ost::AppDataUnit* adu = _queue->getData (_queue->getFirstTimestamp());
    if (!adu)
199
200
        return;

201
202
    unsigned char* spkrDataIn = (unsigned char*) adu->getData(); // data in char
    unsigned int size = adu->getSize(); // size in char
203
204

    // DTMF over RTP, size must be over 4 in order to process it as voice data
205
    if (size > 4)
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
        processDataDecode (spkrDataIn, size);

    delete adu;
}



void AudioRtpSession::sendMicData()
{
    int compSize = processDataEncode();

    // if no data return
    if (!compSize)
        return;

    // Increment timestamp for outgoing packet
    _timestamp += _timestampIncrement;

    if (_type == Zrtp)
    	_queue->putData (_timestamp, getMicDataEncoded(), compSize);
    // putData put the data on RTP queue, sendImmediate bypass this queue
    _queue->sendImmediate (_timestamp, getMicDataEncoded(), compSize);
}


void AudioRtpSession::setSessionTimeouts (void)
{
    _debug ("AudioZrtpSession: Set session scheduling timeout (%d) and expireTimeout (%d)", sfl::schedulingTimeout, sfl::expireTimeout);

    _queue->setSchedulingTimeout (sfl::schedulingTimeout);
    _queue->setExpireTimeout (sfl::expireTimeout);
}

void AudioRtpSession::setDestinationIpAddress (void)
{
    _info ("AudioZrtpSession: Setting IP address for the RTP session");

    // Store remote ip in case we would need to forget current destination
    _remote_ip = ost::InetHostAddress (_ca->getLocalSDP()->getRemoteIP().c_str());

    if (!_remote_ip) {
        _warn ("AudioZrtpSession: Target IP address (%s) is not correct!",
               _ca->getLocalSDP()->getRemoteIP().data());
        return;
    }

    // Store remote port in case we would need to forget current destination
    _remote_port = (unsigned short) _ca->getLocalSDP()->getRemoteAudioPort();

    _info ("AudioZrtpSession: New remote address for session: %s:%d",
           _ca->getLocalSDP()->getRemoteIP().data(), _remote_port);

    if (!_queue->addDestination (_remote_ip, _remote_port)) {
        _warn ("AudioZrtpSession: Can't add new destination to session!");
        return;
    }
}

void AudioRtpSession::updateDestinationIpAddress (void)
{
    _debug ("AudioZrtpSession: Update destination ip address");

    // Destination address are stored in a list in ccrtp
    // This method remove the current destination entry

    if (!_queue->forgetDestination (_remote_ip, _remote_port, _remote_port+1))
        _warn ("AudioZrtpSession: Could not remove previous destination");

    // new destination is stored in call
    // we just need to recall this method
    setDestinationIpAddress();
}


int AudioRtpSession::startRtpThread (AudioCodec* audiocodec)
{
    if (_isStarted)
        return 0;

    _debug ("AudioSymmetricRtpSession: Starting main thread");

    _isStarted = true;
    setSessionTimeouts();
    setSessionMedia (audiocodec);
    initBuffers();
    initNoiseSuppress();

    _queue->enableStack();
    int ret = _thread->start();
	if (_type == Zrtp)
		return ret;

298
299
300
    AudioSymmetricRtpSession *self = dynamic_cast<AudioSymmetricRtpSession*>(this);
    assert(self);
    return self->startSymmetricRtpThread();
301
302
303
304
}

void AudioRtpSession::stopRtpThread ()
{
305
    _queue->disableStack();
306
    if (_type != Zrtp) {
Tristan Matthews's avatar
Tristan Matthews committed
307
308
309
310
        AudioSymmetricRtpSession *self = dynamic_cast<AudioSymmetricRtpSession*>(this);
        assert(self);
        _debug ("AudioSymmetricRtpSession: Stopping main thread");
        self->stopSymmetricRtpThread();
311
312
313
314
315
316
317
318
319
320
321
    }
}


bool AudioRtpSession::onRTPPacketRecv (ost::IncomingRTPPkt&)
{
    receiveSpeakerData();
    return true;
}

}