audio_rtp_session.cpp 8.16 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
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
AudioRtpSession::AudioRtpSession(SIPCall * sipcall, ost::RTPDataQueue *queue, ost::Thread *thread) :
47
    AudioRtpRecordHandler(sipcall)
48
49
50
51
    , ca_(sipcall)
    , timestamp_(0)
    , timestampIncrement_(0)
    , queue_(queue)
52
53
54
55
    , isStarted_(false)
    , remote_ip_()
    , remote_port_(0)
    , timestampCount_(0)
56
    , thread_(thread)
57
{
58
59
    assert(ca_);
    queue_->setTypeOfService(ost::RTPDataQueue::tosEnhanced);
60
61
62
63
}

AudioRtpSession::~AudioRtpSession()
{
64
    queue_->disableStack();
65
66
}

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

71
    setSessionMedia(audioCodec);
72

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

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

}

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

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

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

98
99
100
101
    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_);
102
103

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

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

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

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

131
    audioRtpRecord_.dtmfQueue_.pop_front();
132

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

135
    incrementTimestampForDTMF();
136
137
138
139
140

    // discard equivalent size of audio
    processDataEncode();

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

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

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


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

156
    if (!adu)
157
158
        return;

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

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

    delete adu;
}


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

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

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

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


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

190
191
    queue_->setSchedulingTimeout(sfl::schedulingTimeout);
    queue_->setExpireTimeout(sfl::expireTimeout);
192
193
}

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

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

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

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

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

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

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

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

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


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

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

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

246
    queue_->enableStack();
247
    return thread_->start();
248
249
250
}


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

}