forked from DoubangoTelecom/webrtc2sip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp_wrap.cc
executable file
·743 lines (654 loc) · 27.7 KB
/
mp_wrap.cc
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
/* Copyright (C) 2012-2015 Doubango Telecom <http://www.doubango.org>
*
* This file is part of Open Source 'webrtc2sip' project
* <http://code.google.com/p/webrtc2sip/>
*
* 'webrtc2sip' 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.
*
* 'webrtc2sip' 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 'webrtc2sip'.
*/
#include "mp_wrap.h"
#include "mp_engine.h"
#include "mp_proxyplugin.h"
#include "mp_proxyplugin_mgr.h"
#include "mp_proxyplugin_consumer_audio.h"
#include "mp_proxyplugin_consumer_video.h"
#include "mp_proxyplugin_producer_audio.h"
#include "mp_proxyplugin_producer_video.h"
#include "db/mp_db_model.h"
#include "SipSession.h"
#include "SipEvent.h"
#include "ProxyPluginMgr.h"
#include "MediaSessionMgr.h"
#include "SipMessage.h"
#include "SipUri.h"
#include <assert.h>
#if !defined(MP_DTMF_CONTENT_TYPE)
# define MP_DTMF_CONTENT_TYPE "application/dtmf-relay"
#endif /* MP_DTMF_CONTENT_TYPE */
namespace webrtc2sip {
//
// MPSipCallback
//
MPSipCallback::MPSipCallback(MPObjectWrapper<MPEngine*> oEngine)
:SipCallback()
{
m_oEngine = oEngine;
}
MPSipCallback::~MPSipCallback()
{
}
void MPSipCallback::attachMediaProxyPlugins(MPObjectWrapper<MPPeer*> oPeer)
{
const ProxyPlugin* pcProxyPlugin;
MPObjectWrapper<MPProxyPlugin*> oMPProxyPlugin;
MPObjectWrapper<MPProxyPluginProducerAudio*> oMPProxyPluginProducerAudioRight, oMPProxyPluginProducerAudioLeft;
MPObjectWrapper<MPProxyPluginConsumerAudio*> oMPProxyPluginConsumerAudioRight, oMPProxyPluginConsumerAudioLeft;
MPObjectWrapper<MPProxyPluginProducerVideo*> oMPProxyPluginProducerVideoRight, oMPProxyPluginProducerVideoLeft;
MPObjectWrapper<MPProxyPluginConsumerVideo*> oMPProxyPluginConsumerVideoRight, oMPProxyPluginConsumerVideoLeft;
const MediaSessionMgr *pcMediaSessionMgrRight, *pcMediaSessionMgrLeft;
// make call to the right leg
pcMediaSessionMgrRight = oPeer->getCallSessionRight() ? const_cast<CallSession*>(oPeer->getCallSessionRight()->getWrappedCallSession())->getMediaMgr() : NULL;
pcMediaSessionMgrLeft = oPeer->getCallSessionLeft() ? const_cast<CallSession*>(oPeer->getCallSessionLeft()->getWrappedCallSession())->getMediaMgr() : NULL;
// media session will be null if the invite is immediately rejected. do nothing as dialog terminated will be called
if(pcMediaSessionMgrRight && pcMediaSessionMgrLeft){
// find consumers and producers
if((oPeer->getMediaType() & MPMediaType_Audio)){
if((pcProxyPlugin = pcMediaSessionMgrLeft->findProxyPluginConsumer(twrap_media_audio)) && (oMPProxyPlugin = MPProxyPluginMgr::findPlugin(pcProxyPlugin->getId()))){
oMPProxyPluginConsumerAudioLeft = *((MPObjectWrapper<MPProxyPluginConsumerAudio*>*)&oMPProxyPlugin);
}
else{
TSK_DEBUG_ERROR("cannot find audio consumer for left leg");
}
if((pcProxyPlugin = pcMediaSessionMgrLeft->findProxyPluginProducer(twrap_media_audio)) && (oMPProxyPlugin = MPProxyPluginMgr::findPlugin(pcProxyPlugin->getId()))){
oMPProxyPluginProducerAudioLeft = *((MPObjectWrapper<MPProxyPluginProducerAudio*>*)&oMPProxyPlugin);
}
else{
TSK_DEBUG_ERROR("cannot find audio producer for left leg");
}
if((pcProxyPlugin = pcMediaSessionMgrRight->findProxyPluginConsumer(twrap_media_audio)) && (oMPProxyPlugin = MPProxyPluginMgr::findPlugin(pcProxyPlugin->getId()))){
oMPProxyPluginConsumerAudioRight = *((MPObjectWrapper<MPProxyPluginConsumerAudio*>*)&oMPProxyPlugin);
}
else{
TSK_DEBUG_ERROR("cannot find audio consumer for right leg");
}
if((pcProxyPlugin = pcMediaSessionMgrRight->findProxyPluginProducer(twrap_media_audio)) && (oMPProxyPlugin = MPProxyPluginMgr::findPlugin(pcProxyPlugin->getId()))){
oMPProxyPluginProducerAudioRight = *((MPObjectWrapper<MPProxyPluginProducerAudio*>*)&oMPProxyPlugin);
}
else{
TSK_DEBUG_ERROR("cannot find audio producer for right leg");
}
// attach consumers and producers
if(oMPProxyPluginConsumerAudioLeft && oMPProxyPluginProducerAudioRight){ // Left -> Right
oMPProxyPluginConsumerAudioLeft->setProducerOpposite(oMPProxyPluginProducerAudioRight);
}
if(oMPProxyPluginConsumerAudioRight && oMPProxyPluginProducerAudioLeft){ // Right -> Left
oMPProxyPluginConsumerAudioRight->setProducerOpposite(oMPProxyPluginProducerAudioLeft);
}
}
if((oPeer->getMediaType() & MPMediaType_Video)){
if((pcProxyPlugin = pcMediaSessionMgrLeft->findProxyPluginConsumer(twrap_media_video)) && (oMPProxyPlugin = MPProxyPluginMgr::findPlugin(pcProxyPlugin->getId()))){
oMPProxyPluginConsumerVideoLeft = *((MPObjectWrapper<MPProxyPluginConsumerVideo*>*)&oMPProxyPlugin);
}
else{
TSK_DEBUG_WARN("cannot find video consumer for left leg");
}
if((pcProxyPlugin = pcMediaSessionMgrLeft->findProxyPluginProducer(twrap_media_video)) && (oMPProxyPlugin = MPProxyPluginMgr::findPlugin(pcProxyPlugin->getId()))){
oMPProxyPluginProducerVideoLeft = *((MPObjectWrapper<MPProxyPluginProducerVideo*>*)&oMPProxyPlugin);
}
else{
TSK_DEBUG_WARN("cannot find video producer for left leg");
}
if((pcProxyPlugin = pcMediaSessionMgrRight->findProxyPluginConsumer(twrap_media_video)) && (oMPProxyPlugin = MPProxyPluginMgr::findPlugin(pcProxyPlugin->getId()))){
oMPProxyPluginConsumerVideoRight = *((MPObjectWrapper<MPProxyPluginConsumerVideo*>*)&oMPProxyPlugin);
}
else{
TSK_DEBUG_WARN("cannot find video consumer for right leg");
}
if((pcProxyPlugin = pcMediaSessionMgrRight->findProxyPluginProducer(twrap_media_video)) && (oMPProxyPlugin = MPProxyPluginMgr::findPlugin(pcProxyPlugin->getId()))){
oMPProxyPluginProducerVideoRight = *((MPObjectWrapper<MPProxyPluginProducerVideo*>*)&oMPProxyPlugin);
}
else{
TSK_DEBUG_WARN("cannot find video producer for right leg");
}
// attach consumers and producers
if(oMPProxyPluginConsumerVideoLeft && oMPProxyPluginProducerVideoRight){ // Left -> Right
oMPProxyPluginConsumerVideoLeft->setProducerOpposite(oMPProxyPluginProducerVideoRight);
}
if(oMPProxyPluginConsumerVideoRight && oMPProxyPluginProducerVideoLeft){ // Right -> Left
oMPProxyPluginConsumerVideoRight->setProducerOpposite(oMPProxyPluginProducerVideoLeft);
}
}
}
else{
TSK_DEBUG_ERROR("Media sessions not ready yet");
}
}
/* @override SipCallback::OnInviteEvent */
int MPSipCallback::OnInviteEvent(const InviteEvent* e)
{
static const bool sIsLeftTrue = true;
static const bool sIsLeftFalse = false;
switch(e->getType())
{
default: break;
/* INCOMING REQUEST */
case tsip_i_request:
{
const SipMessage* pcSipMessage;
const tsip_message_t* pcWrappedSipMessage;
assert((pcSipMessage = e->getSipMessage()) && (pcWrappedSipMessage = pcSipMessage->getWrappedSipMessage()));
// Incoming INFO(dtmf-relay) ?
if(TSIP_REQUEST_IS_INFO(pcWrappedSipMessage) && TSIP_MESSAGE_HAS_CONTENT(pcWrappedSipMessage) && tsk_striequals(TSIP_MESSAGE_CONTENT_TYPE(pcWrappedSipMessage), MP_DTMF_CONTENT_TYPE))
{
MPObjectWrapper<MPPeer*> oPeer;
bool bFromLeftToRight = false;
const InviteSession* pcInviteSession;
// get the INVITE session associated to this event
if(!(pcInviteSession = e->getSession()))
{
TSK_DEBUG_WARN("No INVITE session associated to this event");
break;
}
uint64_t nSessionId = (uint64_t)pcInviteSession->getId();
if((oPeer = m_oEngine->getPeerBySessionId(nSessionId, sIsLeftTrue)))
{
bFromLeftToRight = true;
}
else if(!(oPeer = m_oEngine->getPeerBySessionId(nSessionId, sIsLeftFalse)))
{
TSK_DEBUG_WARN("Failed to find peer hosting session with id = %llu", nSessionId);
break;
}
const char* pcContent = (const char*)TSIP_MESSAGE_CONTENT_DATA(pcWrappedSipMessage);
size_t nContentLength = (size_t)TSIP_MESSAGE_CONTENT_DATA_LENGTH(pcWrappedSipMessage);
TSK_DEBUG_INFO("Processing INFO(dtmf-relay, %s->%s): %.*s", (bFromLeftToRight ? "Left" : "Right"), (bFromLeftToRight ? "Right" : "Left"), nContentLength, pcContent);
if(pcContent && nContentLength)
{
// find call session (receiver side)
const CallSession* pcCallSession = bFromLeftToRight
? oPeer->getCallSessionRight()->getWrappedCallSession()
: oPeer->getCallSessionLeft()->getWrappedCallSession();
assert(pcCallSession);
if(tsk_striequals(m_oEngine->m_pDtmfType, MP_DTMF_TYPE_RFC2833)) /* out-band (rfc2833) */
{
TSK_DEBUG_INFO("Gtw configured to relay DTMF using out-band(rfc2833) mode");
ActionConfig* pConfig = new ActionConfig();
if(pConfig)
{
pConfig->addHeader("Content-Type", MP_DTMF_CONTENT_TYPE);
}
if(!const_cast<CallSession*>(pcCallSession)->sendInfo(pcContent, nContentLength, pConfig))
{
TSK_DEBUG_ERROR("Failed to relay the DTMF digit - 'rfc2833'");
}
if(pConfig)
{
delete pConfig; pConfig = NULL;
}
}
else /* in-band (rfc4733) */
{
TSK_DEBUG_INFO("Gtw configured to relay DTMF using in-band(rfc4733) mode");
int nSignalIndex = tsk_strindexOf(pcContent, nContentLength, "Signal=");
if(nSignalIndex == -1)
{
TSK_DEBUG_ERROR("Failed to find 'Signal=' in the INFO content");
break;
}
const char cSignal = pcContent[nSignalIndex + 7];
// RFC 4733 code mapping
typedef struct dtmf_code_s { int code; char v; } dtmf_code_t;
static const dtmf_code_t __dtmf_codes[16] = { {0, '0'}, {1, '1'}, {2, '2'}, {3, '3'}, {4, '4'}, {5, '5'}, {6, '6'}, {7, '7'}, {8, '8'}, {9, '9'}, {10, '*'}, {11, '#'}, {12, 'A'}, {13, 'B'}, {14, 'C'}, {15, 'D'} };
static const size_t __dtmf_codes_count = (sizeof(__dtmf_codes)/sizeof(__dtmf_codes[0]));
// find the code
size_t i;
int dtmfCode = -1;
for(i = 0; i<__dtmf_codes_count; ++i)
{
if(__dtmf_codes[i].v == cSignal)
{
dtmfCode = __dtmf_codes[i].code;
}
}
if(dtmfCode == -1)
{
TSK_DEBUG_ERROR("Failed to map dtmf code with signal = '%c'", cSignal);
break;
}
if(!const_cast<CallSession*>(pcCallSession)->sendDTMF(dtmfCode))
{
TSK_DEBUG_INFO("Failed to relay the DTMF digit - 'rfc4733'");
}
}
}
}
break;
}//case tsip_i_request:
/* INCOMING NEW CALL (INVITE) */
case tsip_i_newcall:
{
assert(e->getSession() == NULL);
assert(e->getSipMessage() != NULL);
#define MP_REJECT_CALL(pcSession, nCode, pcPhrase) \
{ \
ActionConfig* config = new ActionConfig(); \
config->setResponseLine(nCode, pcPhrase); \
const_cast<CallSession*>(pcSession)->reject(config); \
if(config) delete config, config = tsk_null; \
} \
CallSession *pCallSessionLeft = NULL;
if((pCallSessionLeft = e->takeCallSessionOwnership()))
{
// Before accepting the call "link it with the bridge"
const MediaSessionMgr* pcMediaSessionMgrLeft = pCallSessionLeft->getMediaMgr();
const SipMessage* pcMsgLeft = e->getSipMessage();
assert(pcMsgLeft);
const tsip_message_t* pcWrappedMsgLeft = const_cast<SipMessage*>(pcMsgLeft)->getWrappedSipMessage();
static const bool g_bSessionLeft = true;
bool bClick2Call = false;
char *pDstUri = NULL, *pSrcUri = NULL, *pEmail = NULL, *pHa1 = NULL, *pImpi = NULL;
MPObjectWrapper<MPSipSessionAV*> oCallSessionLeft;
bool bHaveAudio = false, bHaveVideo = false;
MPMediaType_t eMediaType = MPMediaType_None;
// check whether it's a click2call request or not
if((bClick2Call = tsk_striequals(tsk_params_get_param_value(pcWrappedMsgLeft->Contact->uri->params, "click2call"), "yes")))
{
const char* pcEmail64 = pcWrappedMsgLeft->From->uri->user_name; // base64 encoded email address
const char* pcSipAddress64 = pcWrappedMsgLeft->To->uri->user_name; // base64 encoded SIP address
TSK_DEBUG_INFO("click2call('%s' -> '%s')", pcEmail64, pcSipAddress64);
// check database validity
if(!m_oEngine->getDB())
{
TSK_DEBUG_ERROR("No database to look into for the click2call operation");
MP_REJECT_CALL(pCallSessionLeft, 500, "No database to look into for the click2call operation");
goto end_of_new_i_call;
}
// decode email
tsk_base64_decode((const uint8_t*)pcEmail64, tsk_strlen(pcEmail64), &pEmail);
// find user account (email is store without being encoded to speedup JSON processing)
MPObjectWrapper<MPDbAccount*> oAccount= m_oEngine->getDB()->selectAccountByEmail(pEmail);
if(!oAccount)
{
TSK_DEBUG_ERROR("Failed to find user account with eamil = %s", pEmail);
MP_REJECT_CALL(pCallSessionLeft, 404, "Failed to find user account (click2call operation)");
goto end_of_new_i_call;
}
// make sure the account is activated
if(!oAccount->isActivated())
{
TSK_DEBUG_ERROR("Account with eamil = '%s' is not activated", pEmail);
MP_REJECT_CALL(pCallSessionLeft, 403, "Account not activated (click2call operation)");
goto end_of_new_i_call;
}
// find SIP account (SIP address is stored base64 encoded and is the email) - address could be the email (from==to)
MPObjectWrapper<MPDbAccountSip*> oAccountSip = m_oEngine->getDB()->selectAccountSipByAccountIdAndAddress(oAccount->getId(), pcSipAddress64);
if(!oAccountSip)
{
// probably (from==to) -> find first SIP address
oAccountSip = m_oEngine->getDB()->selectAccountSipByAccountId(oAccount->getId());
if(!oAccountSip)
{
TSK_DEBUG_ERROR("Failed to find SIP account with address = %s", pcSipAddress64);
MP_REJECT_CALL(pCallSessionLeft, 404, "Failed to find SIP address (click2call operation)");
goto end_of_new_i_call;
}
}
// decode SIP address
tsk_base64_decode((const uint8_t*)oAccountSip->getAddress64(), tsk_strlen(oAccountSip->getAddress64()), &pDstUri);
TSK_DEBUG_INFO("click2call('%s' -> '%s')", pEmail, pDstUri);
SipUri oDstUri(pDstUri);
if(!oDstUri.isValid())
{
TSK_DEBUG_ERROR("Failed to parse SIP address = %s", pDstUri);
MP_REJECT_CALL(pCallSessionLeft, 483, "Failed to parse SIP address (click2call operation)");
goto end_of_new_i_call;
}
// find SIP caller from the database
MPObjectWrapper<MPDbAccountSipCaller*> oAccountSipCaller = m_oEngine->getDB()->selectAccountSipCallerByAccountSipId(oAccountSip->getId());
// find SIP caller from 'config.xml'
if(!oAccountSipCaller)
{
oAccountSipCaller = m_oEngine->findAccountSipCallerByRealm(oDstUri.getHost());
if(!oAccountSipCaller)
{
TSK_DEBUG_ERROR("Failed to find SIP caller for domain = %s", oDstUri.getHost());
MP_REJECT_CALL(pCallSessionLeft, 404, "Failed to find SIP caller account (click2call operation)");
goto end_of_new_i_call;
}
}
pSrcUri = tsk_strdup(oAccountSipCaller->getImpu());
pHa1 = tsk_strdup(oAccountSipCaller->getHa1());
pImpi = tsk_strdup(oAccountSipCaller->getImpi());
}
else
{
pSrcUri = const_cast<SipMessage*>(pcMsgLeft)->getSipHeaderValue("f");
pDstUri = const_cast<SipMessage*>(pcMsgLeft)->getSipHeaderValue("t");
pHa1 = tsk_strdup(TSIP_HEADER_GET_PARAM_VALUE(pcWrappedMsgLeft->Contact, "ha1"));
const char* pcImpi = TSIP_HEADER_GET_PARAM_VALUE(pcWrappedMsgLeft->Contact, "impi");
if(pcImpi)
{
pImpi = tsk_url_decode(pcImpi);
}
}
if(e->getMediaType() & twrap_media_audio){ eMediaType = (MPMediaType_t)(eMediaType | MPMediaType_Audio); bHaveAudio = true; }
if(e->getMediaType() & twrap_media_video){ eMediaType = (MPMediaType_t)(eMediaType | MPMediaType_Video); bHaveVideo = true; }
oCallSessionLeft = new MPSipSessionAV(&pCallSessionLeft, eMediaType);
if(pDstUri && pcMediaSessionMgrLeft && (eMediaType != MPMediaType_None))
{
// find peer associated to this session (must be null)
MPObjectWrapper<MPPeer*> oPeer = m_oEngine->getPeerBySessionId(oCallSessionLeft->getWrappedSession()->getId(), g_bSessionLeft);
assert(!oPeer);
oPeer = new MPPeer(oCallSessionLeft);
oPeer->setSessionLeftState(MPPeerState_Connecting);
oPeer->setMediaType(eMediaType);
m_oEngine->insertPeer(oPeer);
// make call to the right leg
CallSession* pCallSessionRight = new CallSession(const_cast<SipStack*>(m_oEngine->m_oSipStack->getWrappedStack()));
MPObjectWrapper<MPSipSessionAV*> oCallSessionRight = new MPSipSessionAV(&pCallSessionRight, eMediaType);
oCallSessionRight->setSessionOpposite(oCallSessionLeft);
oCallSessionLeft->setSessionOpposite(oCallSessionRight);
const_cast<SipSession*>(oCallSessionRight->getWrappedSession())->setFromUri(pSrcUri);
const_cast<SipSession*>(oCallSessionRight->getWrappedSession())->setAuth(pHa1, pImpi);
#if 0 // now using "ssrc.local" and "ssrc.remote"
// use same SSRCs to make possible RTCP forwarding
if(tsk_striequals("application/sdp", TSIP_MESSAGE_CONTENT_TYPE(pcWrappedMsgLeft))){
tsdp_message_t* sdp_ro;
if((sdp_ro = tsdp_message_parse(TSIP_MESSAGE_CONTENT_DATA(pcWrappedMsgLeft), TSIP_MESSAGE_CONTENT_DATA_LENGTH(pcWrappedMsgLeft)))){
static const char* __ssrc_media_names[] = { "audio", "video" };
static const twrap_media_type_t __ssrc_media_types[] = { twrap_media_audio, twrap_media_video };
int ssrc_i;
uint32_t ssrc;
for(ssrc_i = 0; ssrc_i < sizeof(__ssrc_media_names)/sizeof(__ssrc_media_names[0]); ++ssrc_i){
const tsdp_header_M_t* pcSdpM = tsdp_message_find_media(sdp_ro, __ssrc_media_names[ssrc_i]);
if(pcSdpM){
const tsdp_header_A_t* pcSdpAudioA = tsdp_header_M_findA(pcSdpM, "ssrc");
if(pcSdpAudioA){
if(sscanf(pcSdpAudioA->value, "%u %*s", &ssrc) != EOF){
TSK_DEBUG_INFO("Using %s SSRC = %u", __ssrc_media_names[ssrc_i], ssrc);
const_cast<CallSession*>(oCallSessionRight->getWrappedCallSession())->setMediaSSRC(__ssrc_media_types[ssrc_i], ssrc);
}
}
}
}
TSK_OBJECT_SAFE_FREE(sdp_ro);
}
}
#endif
// filter codecs if media coder is OFF
if(MediaSessionMgr::defaultsGetByPassEncoding()){
int32_t nNegCodecs = const_cast<CallSession*>(oCallSessionLeft->getWrappedCallSession())->getNegotiatedCodecs();
TSK_DEBUG_INFO("Negotiated codecs with the left leg = %d", nNegCodecs);
const_cast<CallSession*>(oCallSessionRight->getWrappedCallSession())->setSupportedCodecs(nNegCodecs);
}
char* pRoute = tsk_null;
// add destination based on the Request-Line
const char* ws_src_ip = tsk_params_get_param_value (pcWrappedMsgLeft->line.request.uri->params, "ws-src-ip");
const int ws_src_port = tsk_params_get_param_value_as_int(pcWrappedMsgLeft->line.request.uri->params, "ws-src-port");
if(!tsk_strnullORempty(ws_src_ip) && ws_src_port > 0){
const char* ws_src_proto = tsk_params_get_param_value(pcWrappedMsgLeft->line.request.uri->params, "ws-src-proto");
tsk_sprintf(&pRoute, "<sip:%s:%d;transport=%s;lr>", ws_src_ip, ws_src_port, ws_src_proto ? ws_src_proto : "ws");
const_cast<SipSession*>(oCallSessionRight->getWrappedSession())->addHeader("Route", pRoute);
TSK_FREE(pRoute);
}
// add WebSocket src connection info in the AoR
if(TNET_SOCKET_TYPE_IS_WS(pcWrappedMsgLeft->src_net_type) || TNET_SOCKET_TYPE_IS_WSS(pcWrappedMsgLeft->src_net_type)){
tnet_ip_t src_ip;
tnet_port_t src_port;
if(tnet_get_ip_n_port(pcWrappedMsgLeft->local_fd, tsk_false/*remote*/, &src_ip, &src_port) == 0){
const char* src_proto = TNET_SOCKET_TYPE_IS_WS(pcWrappedMsgLeft->src_net_type) ? "ws" : "wss";
const_cast<SipSession*>(oCallSessionRight->getWrappedSession())->setWebSocketSrc(src_ip, src_port, src_proto);
tsk_sprintf(&pRoute, "<sip:%s:%d;transport=%s;lr>", src_ip, src_port, src_proto);
const_cast<SipSession*>(oCallSessionLeft->getWrappedSession())->addHeader("Route", pRoute);
TSK_FREE(pRoute);
}
}
// add Routes
int nRouteIndex = 0;
while((pRoute = const_cast<SipMessage*>(pcMsgLeft)->getSipHeaderValue("route", nRouteIndex++))){
const_cast<SipSession*>(oCallSessionRight->getWrappedSession())->addHeader("Route", pRoute);
TSK_FREE(pRoute);
}
oPeer->setCallSessionRight(oCallSessionRight);
oPeer->setSessionRightState(MPPeerState_Connecting);
const_cast<CallSession*>(oPeer->getCallSessionRight()->getWrappedCallSession())->call(pDstUri,
(bHaveAudio && bHaveVideo) ? twrap_media_audio_video
: (bHaveAudio ? twrap_media_audio : twrap_media_video)
);
if(pCallSessionRight){
delete pCallSessionRight, pCallSessionRight = NULL;
}
}
else{
MP_REJECT_CALL(pCallSessionLeft, 500, "Server error 501");
}
end_of_new_i_call:
if(pCallSessionLeft){
delete pCallSessionLeft, pCallSessionLeft = NULL;
}
TSK_FREE(pDstUri);
TSK_FREE(pSrcUri);
TSK_FREE(pEmail);
TSK_FREE(pHa1);
TSK_FREE(pImpi);
}
break;
}//case tsip_i_newcall:
/* ANSWER TO OUTGOING REQUEST */
case tsip_ao_request:
{
/*const SipMessage* pcSipMessage;
const InviteSession* pcInviteSession;
const tsip_message_t* pcWrappedSipMessage;
// get the INVITE session associated to this event
if(!(pcInviteSession = e->getSession()))
{
TSK_DEBUG_WARN("No INVITE session associated to this event");
break;
}
assert((pcSipMessage = e->getSipMessage()) && (pcWrappedSipMessage = pcSipMessage->getWrappedSipMessage()));
// make sure the response is for an INVITE from the left to the right leg
if(!(TSIP_RESPONSE_IS_TO_INVITE(pcWrappedSipMessage)))
{
// could be response to PRACK, UPDATE... sent from the gw
break;
}
// only provisional responses will be forwarded from the right to the left leg
// final responses will be forwarded using the onDialogEvent: 2xx when connected, 3xx-6xx when terminated, 401/407 will be handled by the gateway
const short nSipresponseCode = TSIP_RESPONSE_CODE(pcWrappedSipMessage);
const char* pcSipResponsePhrase = TSIP_RESPONSE_PHRASE(pcWrappedSipMessage);
if(nSipresponseCode > 99 && nSipresponseCode < 200)
{
uint64_t nSessionId = (uint64_t)pcInviteSession->getId();
bool bFromLeftToRight = false;
if((oPeer = m_oEngine->getPeerBySessionId(nSessionId, sIsLeftTrue)))
{
bFromLeftToRight = true;
}
else if(!(oPeer = m_oEngine->getPeerBySessionId(nSessionId, sIsLeftFalse)))
{
TSK_DEBUG_WARN("Failed to find peer hosting session with id = %llu", nSessionId);
break;
}
// find call session (receiver side)
const CallSession* pcCallSession = bFromLeftToRight
? oPeer->getCallSessionRight()->getWrappedCallSession()
: oPeer->getCallSessionLeft()->getWrappedCallSession();
if(!pcCallSession)
{
TSK_DEBUG_WARN("failed to find call session");
break;
}
}*/
break;
}
}
return 0;
}
/* @override SipCallback::OnDialogEvent */
int MPSipCallback::OnDialogEvent(const DialogEvent* e)
{
const SipSession *pcSession = e->getBaseSession();
if(!pcSession){
return 0;
}
const short code = e->getCode();
const unsigned nId = pcSession->getId();
switch(code)
{
case tsip_event_code_dialog_terminated:
case tsip_event_code_dialog_terminating:
{
MPObjectWrapper<MPPeer*> oPeer = m_oEngine->getPeerBySessionId(nId, true);
bool bIsleft = !!oPeer;
if(!oPeer){
oPeer = m_oEngine->getPeerBySessionId(nId, false);
}
if(oPeer){
// Get SIP response code
short nSipCode = 0;
const char* pcPhrase = NULL;
const SipMessage* pcMsg = e->getSipMessage();
if(pcMsg && const_cast<SipMessage*>(pcMsg)->isResponse()){
nSipCode = const_cast<SipMessage*>(pcMsg)->getResponseCode();
pcPhrase = const_cast<SipMessage*>(pcMsg)->getResponsePhrase();
}
if(bIsleft)
{
oPeer->setSessionLeftState(MPPeerState_Terminated);
oPeer->setLastSipResponseLeft(nSipCode);
}
else
{
oPeer->setSessionRightState(MPPeerState_Terminated);
oPeer->setLastSipResponseRight(nSipCode);
}
ActionConfig* config = new ActionConfig();
if(config && nSipCode > 0)
{
config->setResponseLine(nSipCode, pcPhrase);
}
if(oPeer->isSessionLeftActive() && oPeer->getCallSessionLeft())
{
const_cast<InviteSession*>(oPeer->getCallSessionLeft()->getWrappedInviteSession())->hangup(config);
}
if(oPeer->isSessionRightActive() && oPeer->getCallSessionRight())
{
const_cast<InviteSession*>(oPeer->getCallSessionRight()->getWrappedInviteSession())->hangup(config);
}
if(!oPeer->isSessionLeftActive() && !oPeer->isSessionRightActive())
{
m_oEngine->removePeer(oPeer->getId());
}
if(config){
delete config, config = NULL;
}
}
break;
}
case tsip_event_code_dialog_connected:
{
MPObjectWrapper<MPPeer*> oPeer = m_oEngine->getPeerBySessionId(nId, true);
bool bIsleft = !!oPeer;
if(!oPeer)
{
oPeer = m_oEngine->getPeerBySessionId(nId, false);
}
if(oPeer)
{
if(bIsleft)
{
oPeer->setSessionLeftState(MPPeerState_Connected);
}
else
{
oPeer->setSessionRightState(MPPeerState_Connected);
MPObjectWrapper<MPSipSessionAV*> oCallSessionLeft = oPeer->getCallSessionLeft();
MPObjectWrapper<MPSipSessionAV*> oCallSessionRight = oPeer->getCallSessionRight();
// right accepted the call -> accept left
if(!oPeer->isSessionLeftConnected() && oCallSessionLeft && oCallSessionRight)
{
// filter codecs if media coder is OFF
if(MediaSessionMgr::defaultsGetByPassEncoding()){
int32_t nNegCodecs = const_cast<CallSession*>(oCallSessionRight->getWrappedCallSession())->getNegotiatedCodecs();
TSK_DEBUG_INFO("Negotiated codecs with the right leg = %d", nNegCodecs);
const_cast<CallSession*>(oCallSessionLeft->getWrappedCallSession())->setSupportedCodecs(nNegCodecs);
}
const_cast<InviteSession*>(oCallSessionLeft->getWrappedInviteSession())->accept();
}
}
// attach media proxy plugins
if(oPeer->isSessionLeftConnected() && oPeer->isSessionRightConnected())
{
attachMediaProxyPlugins(oPeer);
}
}
break;
}
}
return 0;
}
/* @override SipCallback::OnRegistrationEvent */
int MPSipCallback::OnRegistrationEvent(const RegistrationEvent* e)
{
switch(e->getType())
{
default: break;
case tsip_i_newreg:
{
assert(e->getSession() == NULL);
RegistrationSession *pRegSession;
if((pRegSession = e->takeSessionOwnership())){
pRegSession->accept();
delete pRegSession, pRegSession = NULL;
}
break;
}
}
return 0;
}
MPObjectWrapper<MPSipCallback*> MPSipCallback::New(MPObjectWrapper<MPEngine*> oEngine)
{
MPObjectWrapper<MPSipCallback*> oCallback = new MPSipCallback(oEngine);
return oCallback;
}
//
// MPSipStack
//
MPSipStack::MPSipStack(MPObjectWrapper<MPSipCallback*> oCallback, const char* pRealmUri, const char* pPrivateId, const char* pPublicId)
{
m_pStack = new SipStack(*oCallback, pRealmUri, pPrivateId, pPublicId);
}
MPSipStack::~MPSipStack()
{
if(m_pStack){
delete m_pStack, m_pStack = NULL;
}
}
bool MPSipStack::isValid()
{
return (m_pStack && m_pStack->isValid());
}
const SipStack* MPSipStack::getWrappedStack()
{
return m_pStack;
}
MPObjectWrapper<MPSipStack*> MPSipStack::New(MPObjectWrapper<MPSipCallback*> oCallback, const char* pRealmUri, const char* pPrivateId, const char* pPublicId)
{
MPObjectWrapper<MPSipStack*> oStack = new MPSipStack(oCallback, pRealmUri, pPrivateId, pPublicId);
if(!oStack->isValid()){
MPObjectSafeRelease(oStack);
}
return oStack;
}
}// namespace