audio_rtp_session.cpp 7.89 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
 *  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.
 */

35
#include "audio_rtp_session.h"
36
#include "logger.h"
37
#include "sip/sdp.h"
38
#include "sip/sipcall.h"
39
#include <ccrtp/oqueue.h>
40
#include "manager.h"
41

42
namespace sfl {
43
44
45
AudioRtpSession::AudioRtpSession(SIPCall &call, ost::RTPDataQueue &queue, ost::Thread &thread) :
    AudioRtpRecordHandler(call)
    , call_(call)
46
47
48
    , timestamp_(0)
    , timestampIncrement_(0)
    , queue_(queue)
49
50
51
52
    , isStarted_(false)
    , remote_ip_()
    , remote_port_(0)
    , timestampCount_(0)
53
    , thread_(thread)
54
{
55
    queue_.setTypeOfService(ost::RTPDataQueue::tosEnhanced);
56
57
58
59
}

AudioRtpSession::~AudioRtpSession()
{
60
    queue_.disableStack();
61
62
}

63
void AudioRtpSession::updateSessionMedia(AudioCodec &audioCodec)
64
{
65
    int lastSamplingRate = audioRtpRecord_.codecSampleRate_;
66

67
    setSessionMedia(audioCodec);
68

69
    Manager::instance().audioSamplingRateChanged(audioRtpRecord_.codecSampleRate_);
70

71
#if HAVE_SPEEXDSP
72
    if (lastSamplingRate != audioRtpRecord_.codecSampleRate_) {
73
74
        DEBUG("Update noise suppressor with sampling rate %d and frame size %d",
              getCodecSampleRate(), getCodecFrameSize());
75
        initNoiseSuppress();
76
    }
77
#endif
78
79
}

80
void AudioRtpSession::setSessionMedia(AudioCodec &audioCodec)
81
{
82
    setRtpMedia(&audioCodec);
83

84
    // G722 requires timestamp to be incremented at 8kHz
85
86
87
88
89
90
91
92
93
94
    const ost::PayloadType payloadType = getCodecPayloadType();
    if (payloadType == ost::sptG722) {
        const int G722_RTP_TIME_INCREMENT = 160;
        timestampIncrement_ = G722_RTP_TIME_INCREMENT;
    } else
        timestampIncrement_ = getCodecFrameSize();

    if (payloadType == ost::sptG722) {
        const int G722_RTP_CLOCK_RATE = 8000;
        queue_.setPayloadFormat(ost::DynamicPayloadFormat( payloadType, G722_RTP_CLOCK_RATE));
95
    } else {
96
97
98
99
        if (getHasDynamicPayload())
            queue_.setPayloadFormat(ost::DynamicPayloadFormat(payloadType, getCodecSampleRate()));
        else
            queue_.setPayloadFormat(ost::StaticPayloadFormat(static_cast<ost::StaticPayloadType>(payloadType)));
100
    }
101
}
102

103
void AudioRtpSession::sendDtmfEvent()
104
{
105
106
    DTMFEvent &dtmf(audioRtpRecord_.dtmfQueue_.front());
    DEBUG("Send RTP Dtmf (%d)", dtmf.payload.event);
107

108
109
    const int increment = getIncrementForDTMF();
    timestamp_ += increment;
110
111
112
113
114

    // discard equivalent size of audio
    processDataEncode();

    // change Payload type for DTMF payload
115
    queue_.setPayloadFormat(ost::DynamicPayloadFormat((ost::PayloadType) getDtmfPayloadType(), 8000));
116

117
118
119
120
    // Set marker in case this is a new Event
    if (dtmf.newevent)
        queue_.setMark(true);
    queue_.sendImmediate(timestamp_, (const unsigned char *) (& (dtmf.payload)), sizeof (ost::RTPPacket::RFC2833Payload));
121

122
123
124
125
126
127
128
    // This is no longer a new event
    if (dtmf.newevent) {
        dtmf.newevent = false;
        queue_.setMark(false);
    }

    // restore the payload to audio
129
130
    const ost::StaticPayloadFormat pf(static_cast<ost::StaticPayloadType>(getCodecPayloadType()));
    queue_.setPayloadFormat(pf);
131
132
133
134
135
136
137
138
139

    // 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)
        audioRtpRecord_.dtmfQueue_.pop_front();
140
141
142
}


143
void AudioRtpSession::receiveSpeakerData()
144
{
145
    const ost::AppDataUnit* adu = queue_.getData(queue_.getFirstTimestamp());
146

147
    if (!adu)
148
149
        return;

150
    unsigned char* spkrDataIn = (unsigned char*) adu->getData(); // data in char
151
    size_t size = adu->getSize(); // size in char
152
153

    // DTMF over RTP, size must be over 4 in order to process it as voice data
154
    if (size > 4)
155
        processDataDecode(spkrDataIn, size, adu->getType());
156
157
158
159
160
161
162
163
164
165

    delete adu;
}


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

    // if no data return
166
    if (compSize == 0)
167
168
169
        return;

    // Increment timestamp for outgoing packet
170
    timestamp_ += timestampIncrement_;
171

172
    // putData puts the data on RTP queue, sendImmediate bypass this queue
173
    queue_.sendImmediate(timestamp_, getMicDataEncoded(), compSize);
174
175
176
}


177
void AudioRtpSession::setSessionTimeouts()
178
{
179
180
    const int schedulingTimeout = 4000;
    const int expireTimeout = 1000000;
181
    DEBUG("Set session scheduling timeout (%d) and expireTimeout (%d)",
182
          schedulingTimeout, expireTimeout);
183

184
185
    queue_.setSchedulingTimeout(schedulingTimeout);
    queue_.setExpireTimeout(expireTimeout);
186
187
}

188
void AudioRtpSession::setDestinationIpAddress()
189
190
{
    // Store remote ip in case we would need to forget current destination
191
    remote_ip_ = ost::InetHostAddress(call_.getLocalSDP()->getRemoteIP().c_str());
192

193
    if (!remote_ip_) {
194
        WARN("Target IP address (%s) is not correct!",
195
              call_.getLocalSDP()->getRemoteIP().data());
196
197
198
199
        return;
    }

    // Store remote port in case we would need to forget current destination
200
    remote_port_ = (unsigned short) call_.getLocalSDP()->getRemoteAudioPort();
201

202
    DEBUG("New remote address for session: %s:%d",
203
          call_.getLocalSDP()->getRemoteIP().data(), remote_port_);
204

205
    if (!queue_.addDestination(remote_ip_, remote_port_)) {
206
        WARN("Can't add new destination to session!");
207
208
209
210
        return;
    }
}

211
void AudioRtpSession::updateDestinationIpAddress()
212
{
213
    DEBUG("Update destination ip address");
214
215
216
217

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

218
    if (!queue_.forgetDestination(remote_ip_, remote_port_, remote_port_ + 1))
219
        DEBUG("Did not remove previous destination");
220
221
222
223
224
225
226

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


227
int AudioRtpSession::startRtpThread(AudioCodec &audiocodec)
228
{
229
    if (isStarted_)
230
231
        return 0;

232
    DEBUG("Starting main thread");
233

234
    isStarted_ = true;
235
    setSessionTimeouts();
236
    setSessionMedia(audiocodec);
237
    initBuffers();
238
#if HAVE_SPEEXDSP
239
    initNoiseSuppress();
240
#endif
241

242
    queue_.enableStack();
243
244
    thread_.start();
    return 0;
245
246
247
}


248
bool AudioRtpSession::onRTPPacketRecv(ost::IncomingRTPPkt&)
249
250
251
252
253
{
    receiveSpeakerData();
    return true;
}

254
255
256
257
258
int AudioRtpSession::getIncrementForDTMF() const
{
    return timestampIncrement_;
}

259
}