AudioRtpSession.cpp 8.84 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
 *  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>
42
#include "manager.h"
43
44
45
46

namespace sfl
{
AudioRtpSession::AudioRtpSession (SIPCall * sipcall, RtpMethod type, ost::RTPDataQueue *queue, ost::Thread *thread) :
47
48
					AudioRtpRecordHandler (sipcall)
					, _ca (sipcall)
49
					, _type(type)
50
51
52
					, _timestamp (0)
					, _timestampIncrement (0)
					, _timestampCount (0)
53
					, _isStarted (false)
54
55
56
57
58
59
60
61
62
					, _queue(queue)
					, _thread(thread)
{
    assert (_ca);
    _queue->setTypeOfService (ost::RTPDataQueue::tosEnhanced);
}

AudioRtpSession::~AudioRtpSession()
{
Rafaël Carré's avatar
Rafaël Carré committed
63
    _queue->disableStack();
64
65
66
67
68
69
70
}

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

    // Update internal codec for this session
71
    int lastSamplingRate = _audioRtpRecord._codecSampleRate;
72

73
    setSessionMedia(audioCodec);
74

75
    Manager::instance().audioSamplingRateChanged(_audioRtpRecord._codecSampleRate);
76

77
78
79
    if (lastSamplingRate != _audioRtpRecord._codecSampleRate) {
        _debug ("AudioRtpSession: Update noise suppressor with sampling rate %d and frame size %d", getCodecSampleRate(), getCodecFrameSize());
        initNoiseSuppress();
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    }

}

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();

97
    // G722 requires timestamp to be incremented at 8kHz
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    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));
        }
    }

121
    if (_type != Zrtp)
122
123
124
		_ca->setRecordingSmplRate (getCodecSampleRate());
}

125
void AudioRtpSession::sendDtmfEvent ()
126
{
127
    ost::RTPPacket::RFC2833Payload payload;
128

129
130
131
132
133
134
135
136
137
138
    payload.event = _audioRtpRecord._dtmfQueue.front();
    payload.ebit = false; // end of event bit
    payload.rbit = false; // reserved bit
    payload.duration = 1; // duration for this event

    _audioRtpRecord._dtmfQueue.pop_front();

    _debug ("AudioRtpSession: Send RTP Dtmf (%d)", payload.event);

    _timestamp += (_type == Zrtp) ? 160 : _timestampIncrement;
139
140
141
142
143
144
145

    // discard equivalent size of audio
    processDataEncode();

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

146
147
148
    _queue->setMark (true);
    _queue->sendImmediate (_timestamp, (const unsigned char *) (&payload), sizeof (payload));
    _queue->setMark (false);
149
150
151
152
153
154
155
156

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


void AudioRtpSession::receiveSpeakerData ()
{
157
158
    const ost::AppDataUnit* adu = _queue->getData (_queue->getFirstTimestamp());
    if (!adu)
159
160
        return;

161
162
    unsigned char* spkrDataIn = (unsigned char*) adu->getData(); // data in char
    unsigned int size = adu->getSize(); // size in char
163
164

    // DTMF over RTP, size must be over 4 in order to process it as voice data
165
    if (size > 4)
166
        processDataDecode (spkrDataIn, size, adu->getType());
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

    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)
{
193
    _debug ("AudioRtpSession: Set session scheduling timeout (%d) and expireTimeout (%d)", sfl::schedulingTimeout, sfl::expireTimeout);
194
195
196
197
198
199
200

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

void AudioRtpSession::setDestinationIpAddress (void)
{
201
    _info ("AudioRtpSession: Setting IP address for the RTP session");
202
203
204
205
206

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

    if (!_remote_ip) {
207
        _warn ("AudioRtpSession: Target IP address (%s) is not correct!",
208
209
210
211
212
213
214
               _ca->getLocalSDP()->getRemoteIP().data());
        return;
    }

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

215
    _info ("AudioRtpSession: New remote address for session: %s:%d",
216
217
218
           _ca->getLocalSDP()->getRemoteIP().data(), _remote_port);

    if (!_queue->addDestination (_remote_ip, _remote_port)) {
219
        _warn ("AudioRtpSession: Can't add new destination to session!");
220
221
222
223
224
225
        return;
    }
}

void AudioRtpSession::updateDestinationIpAddress (void)
{
226
    _debug ("AudioRtpSession: Update destination ip address");
227
228
229
230
231

    // 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))
232
        _warn ("AudioRtpSession: Could not remove previous destination");
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

    // 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;

258
259
260
    AudioSymmetricRtpSession *self = dynamic_cast<AudioSymmetricRtpSession*>(this);
    assert(self);
    return self->startSymmetricRtpThread();
261
262
263
264
265
266
267
268
269
270
}


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

}