forked from resiprocate/resiprocate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMediaStream.cxx
349 lines (309 loc) · 10.2 KB
/
MediaStream.cxx
1
2
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
46
47
48
49
50
51
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
197
198
199
200
201
202
203
204
205
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <boost/function.hpp>
#include <rutil/Log.hxx>
#include <rutil/Logger.hxx>
#include <rutil/Timer.hxx>
#include "MediaStream.hxx"
#include "FlowManagerSubsystem.hxx"
#include "FlowManager.hxx"
using namespace flowmanager;
#ifdef USE_SSL
using namespace dtls;
#endif
using namespace resip;
using namespace std;
#define RESIPROCATE_SUBSYSTEM FlowManagerSubsystem::FLOWMANAGER
MediaStream::MediaStream(asio::io_service& ioService,
#ifdef USE_SSL
asio::ssl::context& sslContext,
#endif
MediaStreamHandler& mediaStreamHandler,
const StunTuple& localRtpBinding,
const StunTuple& localRtcpBinding,
#ifdef USE_SSL
DtlsFactory* dtlsFactory,
#endif
NatTraversalMode natTraversalMode,
const char* natTraversalServerHostname,
unsigned short natTraversalServerPort,
const char* stunUsername,
const char* stunPassword) :
#ifdef USE_SSL
mDtlsFactory(dtlsFactory),
#endif
mSRTPSessionInCreated(false),
mSRTPSessionOutCreated(false),
mNatTraversalMode(natTraversalMode),
mNatTraversalServerHostname(natTraversalServerHostname),
mNatTraversalServerPort(natTraversalServerPort),
mStunUsername(stunUsername),
mStunPassword(stunPassword),
mMediaStreamHandler(mediaStreamHandler)
{
// Rtcp is enabled if localRtcpBinding transport type != None
mRtcpEnabled = localRtcpBinding.getTransportType() != StunTuple::None;
if(mRtcpEnabled)
{
mRtpFlow = new Flow(ioService,
#ifdef USE_SSL
sslContext,
#endif
RTP_COMPONENT_ID,
localRtpBinding,
*this);
mRtcpFlow = new Flow(ioService,
#ifdef USE_SSL
sslContext,
#endif
RTCP_COMPONENT_ID,
localRtcpBinding,
*this);
mRtpFlow->activateFlow(StunMessage::PropsPortPair);
// If doing an allocation then wait until RTP flow is allocated, then activate RTCP flow
if(natTraversalMode != TurnAllocation)
{
mRtcpFlow->activateFlow();
}
}
else
{
mRtpFlow = new Flow(ioService,
#ifdef USE_SSL
sslContext,
#endif
RTP_COMPONENT_ID,
localRtpBinding,
*this);
mRtpFlow->activateFlow(StunMessage::PropsPortEven);
mRtcpFlow = 0;
}
}
MediaStream::~MediaStream()
{
{
Lock lock(mMutex);
if(mSRTPSessionOutCreated)
{
mSRTPSessionOutCreated = false;
srtp_dealloc(mSRTPSessionOut);
}
if(mSRTPSessionInCreated)
{
mSRTPSessionInCreated = false;
srtp_dealloc(mSRTPSessionIn);
}
}
delete mRtpFlow;
if(mRtcpEnabled)
{
delete mRtcpFlow;
}
}
bool
MediaStream::createOutboundSRTPSession(SrtpCryptoSuite cryptoSuite, const char* key, unsigned int keyLen)
{
if(keyLen != SRTP_MASTER_KEY_LEN)
{
ErrLog(<< "Unable to create outbound SRTP session, invalid keyLen=" << keyLen);
return false;
}
err_status_t status;
Lock lock(mMutex);
if(mSRTPSessionOutCreated)
{
// Check if settings are the same - if so just return true
if(cryptoSuite == mCryptoSuiteOut && memcmp(mSRTPMasterKeyOut, key, keyLen) == 0)
{
InfoLog(<< "Outbound SRTP session settings unchanged.");
return true;
}
else
{
InfoLog(<< "Re-creating outbound SRTP session with new settings.");
mSRTPSessionOutCreated = false;
srtp_dealloc(mSRTPSessionOut);
}
}
memset(&mSRTPPolicyOut, 0, sizeof(mSRTPPolicyOut));
// Copy key locally
memcpy(mSRTPMasterKeyOut, key, SRTP_MASTER_KEY_LEN);
// load default srtp/srtcp policy settings
mCryptoSuiteOut = cryptoSuite;
switch(cryptoSuite)
{
case SRTP_AES_CM_128_HMAC_SHA1_80:
crypto_policy_set_aes_cm_128_hmac_sha1_80(&mSRTPPolicyOut.rtp);
crypto_policy_set_aes_cm_128_hmac_sha1_80(&mSRTPPolicyOut.rtcp);
break;
case SRTP_AES_CM_128_HMAC_SHA1_32:
crypto_policy_set_aes_cm_128_hmac_sha1_32(&mSRTPPolicyOut.rtp);
crypto_policy_set_aes_cm_128_hmac_sha1_32(&mSRTPPolicyOut.rtcp);
break;
default:
ErrLog(<< "Unable to create outbound SRTP session, invalid crypto suite=" << cryptoSuite);
return false;
}
// set remaining policy settings
mSRTPPolicyOut.ssrc.type = ssrc_any_outbound;
mSRTPPolicyOut.key = mSRTPMasterKeyOut;
mSRTPPolicyOut.window_size = 64;
// Allocate and initailize the SRTP sessions
status = srtp_create(&mSRTPSessionOut, &mSRTPPolicyOut);
if(status)
{
ErrLog(<< "Unable to create srtp out session, error code=" << status);
return false;
}
mSRTPSessionOutCreated = true;
return true;
}
bool
MediaStream::createInboundSRTPSession(SrtpCryptoSuite cryptoSuite, const char* key, unsigned int keyLen)
{
if(keyLen != SRTP_MASTER_KEY_LEN)
{
ErrLog(<< "Unable to create inbound SRTP session, invalid keyLen=" << keyLen);
return false;
}
err_status_t status;
Lock lock(mMutex);
if(mSRTPSessionInCreated)
{
// Check if settings are the same - if so just return true
if(cryptoSuite == mCryptoSuiteIn && memcmp(mSRTPMasterKeyIn, key, keyLen) == 0)
{
InfoLog(<< "Inbound SRTP session settings unchanged.");
return true;
}
else
{
InfoLog(<< "Re-creating inbound SRTP session with new settings.");
mSRTPSessionInCreated = false;
srtp_dealloc(mSRTPSessionIn);
}
}
memset(&mSRTPPolicyIn, 0, sizeof(mSRTPPolicyIn));
// Copy key locally
memcpy(mSRTPMasterKeyIn, key, SRTP_MASTER_KEY_LEN);
// load default srtp/srtcp policy settings
mCryptoSuiteIn = cryptoSuite;
switch(cryptoSuite)
{
case SRTP_AES_CM_128_HMAC_SHA1_80:
crypto_policy_set_aes_cm_128_hmac_sha1_80(&mSRTPPolicyIn.rtp);
crypto_policy_set_aes_cm_128_hmac_sha1_80(&mSRTPPolicyIn.rtcp);
break;
case SRTP_AES_CM_128_HMAC_SHA1_32:
crypto_policy_set_aes_cm_128_hmac_sha1_32(&mSRTPPolicyIn.rtp);
crypto_policy_set_aes_cm_128_hmac_sha1_32(&mSRTPPolicyIn.rtcp);
break;
default:
ErrLog(<< "Unable to create inbound SRTP session, invalid crypto suite=" << cryptoSuite);
return false;
}
// set remaining policy settings
mSRTPPolicyIn.ssrc.type = ssrc_any_inbound;
mSRTPPolicyIn.key = mSRTPMasterKeyIn;
mSRTPPolicyIn.window_size = 64;
// Allocate and initailize the SRTP sessions
status = srtp_create(&mSRTPSessionIn, &mSRTPPolicyIn);
if(status)
{
ErrLog(<< "Unable to create srtp in session, error code=" << status);
return false;
}
mSRTPSessionInCreated = true;
return true;
}
err_status_t
MediaStream::srtpProtect(void* data, int* size, bool rtcp)
{
Lock lock(mMutex);
err_status_t status = err_status_no_ctx;
if(mSRTPSessionOutCreated)
{
if(rtcp)
{
status = srtp_protect_rtcp(mSRTPSessionOut, data, size);
}
else
{
status = srtp_protect(mSRTPSessionOut, data, size);
}
}
return status;
}
err_status_t
MediaStream::srtpUnprotect(void* data, int* size, bool rtcp)
{
Lock lock(mMutex);
err_status_t status = err_status_no_ctx;
if(mSRTPSessionInCreated)
{
if(rtcp)
{
status = srtp_unprotect_rtcp(mSRTPSessionIn, data, size);
}
else
{
status = srtp_unprotect(mSRTPSessionIn, data, size);
}
}
return status;
}
void
MediaStream::onFlowReady(unsigned int componentId)
{
if(componentId == RTP_COMPONENT_ID && mNatTraversalMode == TurnAllocation && mRtcpFlow)
{
// RTP Flow is ready - we can now activate RTCP flow using the reservation token
mRtcpFlow->activateFlow(mRtpFlow->getReservationToken());
}
else
{
if(mRtpFlow && mRtcpFlow)
{
if(mRtpFlow->isReady() && mRtcpFlow->isReady())
{
mMediaStreamHandler.onMediaStreamReady(mRtpFlow->getSessionTuple(), mRtcpFlow->getSessionTuple());
}
}
else if(mRtpFlow && mRtpFlow->isReady())
{
mMediaStreamHandler.onMediaStreamReady(mRtpFlow->getSessionTuple(), StunTuple());
}
}
}
void
MediaStream::onFlowError(unsigned int componentId, unsigned int errorCode)
{
mMediaStreamHandler.onMediaStreamError(errorCode); // TODO assign real error code
}
/* ====================================================================
Copyright (c) 2007-2008, Plantronics, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of Plantronics nor the names of its contributors
may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
==================================================================== */