audio_rtp_session.cpp 8.14 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
36
#include "audio_rtp_session.h"
#include "audio_symmetric_rtp_session.h"
37
#include "logger.h"
38
#include "sip/sdp.h"
39
#include "sip/sipcall.h"
40
41
42
#include "audio/audiolayer.h"
#include <ccrtp/rtp.h>
#include <ccrtp/oqueue.h>
43
#include "manager.h"
44

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

AudioRtpSession::~AudioRtpSession()
{
63
    queue_.disableStack();
64
65
}

66
void AudioRtpSession::updateSessionMedia(AudioCodec &audioCodec)
67
{
68
    int lastSamplingRate = audioRtpRecord_.codecSampleRate_;
69

70
    setSessionMedia(audioCodec);
71

72
    Manager::instance().audioSamplingRateChanged(audioRtpRecord_.codecSampleRate_);
73

74
    if (lastSamplingRate != audioRtpRecord_.codecSampleRate_) {
75
        DEBUG("AudioRtpSession: Update noise suppressor with sampling rate %d and frame size %d", getCodecSampleRate(), getCodecFrameSize());
76
        initNoiseSuppress();
77
78
79
80
    }

}

81
void AudioRtpSession::setSessionMedia(AudioCodec &audioCodec)
82
{
83
    setRtpMedia(&audioCodec);
84
85
86
87
88
89
90

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

91
    // G722 requires timestamp to be incremented at 8kHz
92
    if (payloadType == g722PayloadType)
93
        timestampIncrement_ = g722RtpTimeincrement;
94
    else
95
        timestampIncrement_ = frameSize;
96

97
98
99
100
    DEBUG("AudioRtpSession: Codec payload: %d", payloadType);
    DEBUG("AudioRtpSession: Codec sampling rate: %d", smplRate);
    DEBUG("AudioRtpSession: Codec frame size: %d", frameSize);
    DEBUG("AudioRtpSession: RTP timestamp increment: %d", timestampIncrement_);
101
102

    if (payloadType == g722PayloadType) {
103
        DEBUG("AudioRtpSession: Setting G722 payload format");
104
        queue_.setPayloadFormat(ost::DynamicPayloadFormat((ost::PayloadType) payloadType, g722RtpClockRate));
105
106
    } else {
        if (dynamic) {
107
            DEBUG("AudioRtpSession: Setting dynamic payload format");
108
            queue_.setPayloadFormat(ost::DynamicPayloadFormat((ost::PayloadType) payloadType, smplRate));
109
        } else {
110
            DEBUG("AudioRtpSession: Setting static payload format");
111
            queue_.setPayloadFormat(ost::StaticPayloadFormat((ost::StaticPayloadType) payloadType));
112
113
        }
    }
114
}
115

116
117
118
void AudioRtpSession::incrementTimestampForDTMF()
{
    timestamp_ += timestampIncrement_;
119
120
}

121
void AudioRtpSession::sendDtmfEvent()
122
{
123
    ost::RTPPacket::RFC2833Payload payload;
124

125
    payload.event = audioRtpRecord_.dtmfQueue_.front();
126
127
128
129
    payload.ebit = false; // end of event bit
    payload.rbit = false; // reserved bit
    payload.duration = 1; // duration for this event

130
    audioRtpRecord_.dtmfQueue_.pop_front();
131

132
    DEBUG("AudioRtpSession: Send RTP Dtmf (%d)", payload.event);
133

134
    incrementTimestampForDTMF();
135
136
137
138
139

    // discard equivalent size of audio
    processDataEncode();

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

142
143
144
    queue_.setMark(true);
    queue_.sendImmediate(timestamp_, (const unsigned char *)(&payload), sizeof(payload));
    queue_.setMark(false);
145
146

    // get back the payload to audio
147
    queue_.setPayloadFormat(ost::StaticPayloadFormat((ost::StaticPayloadType) getCodecPayloadType()));
148
149
150
}


151
void AudioRtpSession::receiveSpeakerData()
152
{
153
    const ost::AppDataUnit* adu = queue_.getData(queue_.getFirstTimestamp());
154

155
    if (!adu)
156
157
        return;

158
159
    unsigned char* spkrDataIn = (unsigned char*) adu->getData(); // data in char
    unsigned int size = adu->getSize(); // size in char
160
161

    // DTMF over RTP, size must be over 4 in order to process it as voice data
162
    if (size > 4)
163
        processDataDecode(spkrDataIn, size, adu->getType());
164
165
166
167
168
169
170
171
172
173

    delete adu;
}


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

    // if no data return
174
    if (compSize == 0)
175
176
177
        return;

    // Increment timestamp for outgoing packet
178
    timestamp_ += timestampIncrement_;
179

180
    // putData puts the data on RTP queue, sendImmediate bypass this queue
181
    queue_.sendImmediate(timestamp_, getMicDataEncoded(), compSize);
182
183
184
}


185
void AudioRtpSession::setSessionTimeouts()
186
{
187
    DEBUG("AudioRtpSession: Set session scheduling timeout (%d) and expireTimeout (%d)", sfl::schedulingTimeout, sfl::expireTimeout);
188

189
190
    queue_.setSchedulingTimeout(sfl::schedulingTimeout);
    queue_.setExpireTimeout(sfl::expireTimeout);
191
192
}

193
void AudioRtpSession::setDestinationIpAddress()
194
195
{
    // Store remote ip in case we would need to forget current destination
196
    remote_ip_ = ost::InetHostAddress(call_.getLocalSDP()->getRemoteIP().c_str());
197

198
    if (!remote_ip_) {
199
        WARN("AudioRtpSession: Target IP address (%s) is not correct!",
200
              call_.getLocalSDP()->getRemoteIP().data());
201
202
203
204
        return;
    }

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

Tristan Matthews's avatar
Tristan Matthews committed
207
    DEBUG("AudioRtpSession: New remote address for session: %s:%d",
208
          call_.getLocalSDP()->getRemoteIP().data(), remote_port_);
209

210
    if (!queue_.addDestination(remote_ip_, remote_port_)) {
211
        WARN("AudioRtpSession: Can't add new destination to session!");
212
213
214
215
        return;
    }
}

216
void AudioRtpSession::updateDestinationIpAddress()
217
{
218
    DEBUG("AudioRtpSession: Update destination ip address");
219
220
221
222

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

223
    if (!queue_.forgetDestination(remote_ip_, remote_port_, remote_port_ + 1))
224
        DEBUG("AudioRtpSession: Did not remove previous destination");
225
226
227
228
229
230
231

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


232
int AudioRtpSession::startRtpThread(AudioCodec &audiocodec)
233
{
234
    if (isStarted_)
235
236
        return 0;

237
    DEBUG("AudioRtpSession: Starting main thread");
238

239
    isStarted_ = true;
240
    setSessionTimeouts();
241
    setSessionMedia(audiocodec);
242
243
244
    initBuffers();
    initNoiseSuppress();

245
    queue_.enableStack();
246
247
    thread_.start();
    return 0;
248
249
250
}


251
bool AudioRtpSession::onRTPPacketRecv(ost::IncomingRTPPkt&)
252
253
254
255
256
257
{
    receiveSpeakerData();
    return true;
}

}