forked from resiprocate/resiprocate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoteParticipant.cxx
2623 lines (2397 loc) · 97.1 KB
/
RemoteParticipant.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
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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "ConversationManager.hxx"
#include "sdp/SdpHelperResip.hxx"
#include "sdp/Sdp.hxx"
#include <sdp/SdpCodec.h> // sipX SdpCodec
#include "RemoteParticipant.hxx"
#include "Conversation.hxx"
#include "UserAgent.hxx"
#include "DtmfEvent.hxx"
#include "ReconSubsystem.hxx"
#include <rutil/Log.hxx>
#include <rutil/Logger.hxx>
#include <rutil/DnsUtil.hxx>
#include <rutil/Random.hxx>
#include <resip/stack/DtmfPayloadContents.hxx>
#include <resip/stack/SipFrag.hxx>
#include <resip/stack/ExtensionHeader.hxx>
#include <resip/dum/DialogUsageManager.hxx>
#include <resip/dum/ClientInviteSession.hxx>
#include <resip/dum/ServerInviteSession.hxx>
#include <resip/dum/ClientSubscription.hxx>
#include <resip/dum/ServerOutOfDialogReq.hxx>
#include <resip/dum/ServerSubscription.hxx>
#include <rutil/WinLeakCheck.hxx>
using namespace recon;
using namespace sdpcontainer;
using namespace resip;
using namespace std;
#define RESIPROCATE_SUBSYSTEM ReconSubsystem::RECON
/* Technically, there are a range of features that need to be implemented
to be fully (S)AVPF compliant.
However, it is speculated that (S)AVPF peers will communicate with legacy
systems that just fudge the RTP/SAVPF protocol in their SDP. Enabling
this define allows such behavior to be tested.
http://www.ietf.org/mail-archive/web/rtcweb/current/msg01145.html
"1) RTCWEB end-point will always signal AVPF or SAVPF. I signalling
gateway to legacy will change that by removing the F to AVP or SAVP."
http://www.ietf.org/mail-archive/web/rtcweb/current/msg04380.html
*/
//#define RTP_SAVPF_FUDGE
// UAC
RemoteParticipant::RemoteParticipant(ParticipantHandle partHandle,
ConversationManager& conversationManager,
DialogUsageManager& dum,
RemoteParticipantDialogSet& remoteParticipantDialogSet)
: Participant(partHandle, conversationManager),
AppDialog(dum),
mDum(dum),
mDialogSet(remoteParticipantDialogSet),
mDialogId(Data::Empty, Data::Empty, Data::Empty),
mState(Connecting),
mOfferRequired(false),
mLocalHold(true),
mRemoteHold(false),
mLocalSdp(0),
mRemoteSdp(0)
{
InfoLog(<< "RemoteParticipant created (UAC), handle=" << mHandle);
}
// UAS - or forked leg
RemoteParticipant::RemoteParticipant(ConversationManager& conversationManager,
DialogUsageManager& dum,
RemoteParticipantDialogSet& remoteParticipantDialogSet)
: Participant(conversationManager),
AppDialog(dum),
mDum(dum),
mDialogSet(remoteParticipantDialogSet),
mDialogId(Data::Empty, Data::Empty, Data::Empty),
mState(Connecting),
mOfferRequired(false),
mLocalHold(true),
mLocalSdp(0),
mRemoteSdp(0)
{
InfoLog(<< "RemoteParticipant created (UAS or forked leg), handle=" << mHandle);
}
RemoteParticipant::~RemoteParticipant()
{
if(!mDialogId.getCallId().empty())
{
mDialogSet.removeDialog(mDialogId);
}
// unregister from Conversations
// Note: ideally this functionality would exist in Participant Base class - but dynamic_cast required in unregisterParticipant will not work
ConversationMap::iterator it;
for(it = mConversations.begin(); it != mConversations.end(); it++)
{
it->second->unregisterParticipant(this);
}
mConversations.clear();
// Delete Sdp memory
if(mLocalSdp) delete mLocalSdp;
if(mRemoteSdp) delete mRemoteSdp;
InfoLog(<< "RemoteParticipant destroyed, handle=" << mHandle);
}
unsigned int
RemoteParticipant::getLocalRTPPort()
{
return mDialogSet.getLocalRTPPort();
}
//static const resip::ExtensionHeader h_AlertInfo("Alert-Info");
void
RemoteParticipant::initiateRemoteCall(const NameAddr& destination)
{
SharedPtr<UserProfile> profile;
initiateRemoteCall(destination, profile, std::multimap<resip::Data,resip::Data>());
}
void
RemoteParticipant::initiateRemoteCall(const NameAddr& destination, SharedPtr<UserProfile>& callingProfile, const std::multimap<resip::Data,resip::Data>& extraHeaders)
{
SdpContents offer;
SharedPtr<UserProfile> profile = callingProfile;
if(!profile)
{
DebugLog(<<"initiateRemoteCall: no callingProfile supplied, calling getDefaultOutgoingConversationProfile");
profile = mConversationManager.getUserAgent()->getDefaultOutgoingConversationProfile();
}
buildSdpOffer(mLocalHold, offer);
SharedPtr<SipMessage> invitemsg = mDum.makeInviteSession(
destination,
profile,
&offer,
&mDialogSet);
std::multimap<resip::Data,resip::Data>::const_iterator it = extraHeaders.begin();
for( ; it != extraHeaders.end(); it++)
{
resip::Data headerName(it->first);
resip::Data value(it->second);
StackLog(<<"processing an extension header: " << headerName << ": " << value);
resip::Headers::Type hType = resip::Headers::getType(headerName.data(), (int)headerName.size());
if(hType == resip::Headers::UNKNOWN)
{
resip::ExtensionHeader h_Tmp(headerName.c_str());
resip::ParserContainer<resip::StringCategory>& pc = invitemsg->header(h_Tmp);
resip::StringCategory sc(value);
pc.push_back(sc);
}
else
{
WarningLog(<<"Discarding header '"<<headerName<<"', only extension headers permitted");
}
}
mDialogSet.sendInvite(invitemsg);
// Clear any pending hold/unhold requests since our offer/answer here will handle it
if(mPendingRequest.mType == Hold ||
mPendingRequest.mType == Unhold)
{
mPendingRequest.mType = None;
}
// Adjust RTP streams
adjustRTPStreams(true);
// Special case of this call - since call in addToConversation will not work, since we didn't know our bridge port at that time
applyBridgeMixWeights();
}
int
RemoteParticipant::getConnectionPortOnBridge()
{
if(mDialogSet.getActiveRemoteParticipantHandle() == mHandle)
{
return mDialogSet.getConnectionPortOnBridge();
}
else
{
// If this is not active fork leg, then we don't want to effect the bridge mixer.
// Note: All forked endpoints/participants have the same connection port on the bridge
return -1;
}
}
int
RemoteParticipant::getMediaConnectionId()
{
return mDialogSet.getMediaConnectionId();
}
void
RemoteParticipant::destroyParticipant()
{
try
{
if(mState != Terminating)
{
stateTransition(Terminating);
if(mInviteSessionHandle.isValid())
{
mInviteSessionHandle->end();
}
else
{
mDialogSet.end();
}
}
}
catch(BaseException &e)
{
WarningLog(<< "RemoteParticipant::destroyParticipant exception: " << e);
}
catch(...)
{
WarningLog(<< "RemoteParticipant::destroyParticipant unknown exception");
}
}
void
RemoteParticipant::addToConversation(Conversation* conversation, unsigned int inputGain, unsigned int outputGain)
{
Participant::addToConversation(conversation, inputGain, outputGain);
if(mLocalHold && !conversation->shouldHold()) // If we are on hold and we now shouldn't be, then unhold
{
unhold();
}
}
void
RemoteParticipant::removeFromConversation(Conversation *conversation)
{
Participant::removeFromConversation(conversation);
checkHoldCondition();
}
void
RemoteParticipant::checkHoldCondition()
{
// Return to Offer a hold sdp if we are not in any conversations, or all the conversations we are in have conditions such that a hold is required
bool shouldHold = true;
ConversationMap::iterator it;
for(it = mConversations.begin(); it != mConversations.end(); it++)
{
if(!it->second->shouldHold())
{
shouldHold = false;
break;
}
}
setLocalHold(shouldHold);
}
void
RemoteParticipant::setLocalHold(bool _hold)
{
if(mLocalHold != _hold)
{
if(_hold)
{
hold();
}
else
{
unhold();
}
}
}
void
RemoteParticipant::stateTransition(State state)
{
Data stateName;
switch(state)
{
case Connecting:
stateName = "Connecting"; break;
case Accepted:
stateName = "Accepted"; break;
case Connected:
stateName = "Connected"; break;
case Redirecting:
stateName = "Redirecting"; break;
case Holding:
stateName = "Holding"; break;
case Unholding:
stateName = "Unholding"; break;
case Replacing:
stateName = "Replacing"; break;
case PendingOODRefer:
stateName = "PendingOODRefer"; break;
case Terminating:
stateName = "Terminating"; break;
default:
stateName = "Unknown: " + Data(state); break;
}
InfoLog( << "RemoteParticipant::stateTransition of handle=" << mHandle << " to state=" << stateName );
mState = state;
if(mState == Connected && mPendingRequest.mType != None)
{
PendingRequestType type = mPendingRequest.mType;
mPendingRequest.mType = None;
switch(type)
{
case Hold:
hold();
break;
case Unhold:
unhold();
break;
case Redirect:
redirect(mPendingRequest.mDestination);
break;
case RedirectTo:
redirectToParticipant(mPendingRequest.mDestInviteSessionHandle);
break;
case None:
break;
}
}
}
void
RemoteParticipant::accept()
{
try
{
// Accept SIP call if required
if(mState == Connecting && mInviteSessionHandle.isValid())
{
ServerInviteSession* sis = dynamic_cast<ServerInviteSession*>(mInviteSessionHandle.get());
if(sis && !sis->isAccepted())
{
if(getLocalRTPPort() == 0)
{
WarningLog(<< "RemoteParticipant::accept cannot accept call, since no free RTP ports, rejecting instead.");
sis->reject(480); // Temporarily Unavailable - no free RTP ports
return;
}
// Clear any pending hold/unhold requests since our offer/answer here will handle it
if(mPendingRequest.mType == Hold ||
mPendingRequest.mType == Unhold)
{
mPendingRequest.mType = None;
}
if(mOfferRequired)
{
provideOffer(true /* postOfferAccept */);
}
else if(mPendingOffer.get() != 0)
{
provideAnswer(*mPendingOffer.get(), true /* postAnswerAccept */, false /* postAnswerAlert */);
}
else
{
// It is possible to get here if the app calls alert with early true. There is special logic in
// RemoteParticipantDialogSet::accept to handle the case then an alert call followed immediately by
// accept. In this case the answer from the alert will be queued waiting on the flow to be ready, and
// we need to ensure the accept call is also delayed until the answer completes.
mDialogSet.accept(mInviteSessionHandle);
}
stateTransition(Accepted);
}
}
// Accept Pending OOD Refer if required
else if(mState == PendingOODRefer)
{
acceptPendingOODRefer();
}
else
{
WarningLog(<< "RemoteParticipant::accept called in invalid state: " << mState);
}
}
catch(BaseException &e)
{
WarningLog(<< "RemoteParticipant::accept exception: " << e);
}
catch(...)
{
WarningLog(<< "RemoteParticipant::accept unknown exception");
}
}
void
RemoteParticipant::alert(bool earlyFlag)
{
try
{
if(mState == Connecting && mInviteSessionHandle.isValid())
{
ServerInviteSession* sis = dynamic_cast<ServerInviteSession*>(mInviteSessionHandle.get());
if(sis && !sis->isAccepted())
{
if(earlyFlag && mPendingOffer.get() != 0)
{
if(getLocalRTPPort() == 0)
{
WarningLog(<< "RemoteParticipant::alert cannot alert call with early media, since no free RTP ports, rejecting instead.");
sis->reject(480); // Temporarily Unavailable - no free RTP ports
return;
}
provideAnswer(*mPendingOffer.get(), false /* postAnswerAccept */, true /* postAnswerAlert */);
mPendingOffer.release();
}
else
{
sis->provisional(180, earlyFlag);
}
}
}
else
{
WarningLog(<< "RemoteParticipant::alert called in invalid state: " << mState);
}
}
catch(BaseException &e)
{
WarningLog(<< "RemoteParticipant::alert exception: " << e);
}
catch(...)
{
WarningLog(<< "RemoteParticipant::alert unknown exception");
}
}
void
RemoteParticipant::reject(unsigned int rejectCode)
{
try
{
// Reject UAS Invite Session if required
if(mState == Connecting && mInviteSessionHandle.isValid())
{
ServerInviteSession* sis = dynamic_cast<ServerInviteSession*>(mInviteSessionHandle.get());
if(sis && !sis->isAccepted())
{
sis->reject(rejectCode);
}
}
// Reject Pending OOD Refer request if required
else if(mState == PendingOODRefer)
{
rejectPendingOODRefer(rejectCode);
}
else
{
WarningLog(<< "RemoteParticipant::reject called in invalid state: " << mState);
}
}
catch(BaseException &e)
{
WarningLog(<< "RemoteParticipant::reject exception: " << e);
}
catch(...)
{
WarningLog(<< "RemoteParticipant::reject unknown exception");
}
}
void
RemoteParticipant::redirect(NameAddr& destination)
{
try
{
if(mPendingRequest.mType == None)
{
if((mState == Connecting || mState == Accepted || mState == Connected) && mInviteSessionHandle.isValid())
{
ServerInviteSession* sis = dynamic_cast<ServerInviteSession*>(mInviteSessionHandle.get());
// If this is a UAS session and we haven't sent a final response yet - then redirect via 302 response
if(sis && !sis->isAccepted() && mState == Connecting)
{
NameAddrs destinations;
destinations.push_back(destination);
mConversationManager.onParticipantRedirectSuccess(mHandle);
sis->redirect(destinations);
}
else if(mInviteSessionHandle->isConnected()) // redirect via blind transfer
{
mInviteSessionHandle->refer(NameAddr(destination.uri()) /* remove tags */, true /* refersub */);
stateTransition(Redirecting);
}
else
{
mPendingRequest.mType = Redirect;
mPendingRequest.mDestination = destination;
}
}
else if(mState == PendingOODRefer)
{
redirectPendingOODRefer(destination);
}
else
{
mPendingRequest.mType = Redirect;
mPendingRequest.mDestination = destination;
}
}
else
{
WarningLog(<< "RemoteParticipant::redirect error: request pending");
mConversationManager.onParticipantRedirectFailure(mHandle, 406 /* Not Acceptable */);
}
}
catch(BaseException &e)
{
WarningLog(<< "RemoteParticipant::redirect exception: " << e);
}
catch(...)
{
WarningLog(<< "RemoteParticipant::redirect unknown exception");
}
}
void
RemoteParticipant::redirectToParticipant(InviteSessionHandle& destParticipantInviteSessionHandle)
{
try
{
if(destParticipantInviteSessionHandle.isValid())
{
if(mPendingRequest.mType == None)
{
if((mState == Connecting || mState == Accepted || mState == Connected) && mInviteSessionHandle.isValid())
{
ServerInviteSession* sis = dynamic_cast<ServerInviteSession*>(mInviteSessionHandle.get());
// If this is a UAS session and we haven't sent a final response yet - then redirect via 302 response
if(sis && !sis->isAccepted() && mState == Connecting)
{
NameAddrs destinations;
destinations.push_back(NameAddr(destParticipantInviteSessionHandle->peerAddr().uri())); // ensure we don't get to or from tag by only using the inner uri()
mConversationManager.onParticipantRedirectSuccess(mHandle);
sis->redirect(destinations);
}
else if(mInviteSessionHandle->isConnected()) // redirect via attended transfer (with replaces)
{
mInviteSessionHandle->refer(NameAddr(destParticipantInviteSessionHandle->peerAddr().uri()) /* remove tags */, destParticipantInviteSessionHandle /* session to replace) */, true /* refersub */);
stateTransition(Redirecting);
}
else
{
mPendingRequest.mType = RedirectTo;
mPendingRequest.mDestInviteSessionHandle = destParticipantInviteSessionHandle;
}
}
else
{
mPendingRequest.mType = RedirectTo;
mPendingRequest.mDestInviteSessionHandle = destParticipantInviteSessionHandle;
}
}
else
{
WarningLog(<< "RemoteParticipant::redirectToParticipant error: request pending");
mConversationManager.onParticipantRedirectFailure(mHandle, 406 /* Not Acceptable */);
}
}
else
{
WarningLog(<< "RemoteParticipant::redirectToParticipant error: destParticipant has no valid InviteSession");
mConversationManager.onParticipantRedirectFailure(mHandle, 406 /* Not Acceptable */);
}
}
catch(BaseException &e)
{
WarningLog(<< "RemoteParticipant::redirectToParticipant exception: " << e);
}
catch(...)
{
WarningLog(<< "RemoteParticipant::redirectToParticipant unknown exception");
}
}
void
RemoteParticipant::hold()
{
mLocalHold=true;
InfoLog(<< "RemoteParticipant::hold request: handle=" << mHandle);
try
{
if(mPendingRequest.mType == None)
{
if(mState == Connected && mInviteSessionHandle.isValid())
{
provideOffer(false /* postOfferAccept */);
stateTransition(Holding);
}
else
{
mPendingRequest.mType = Hold;
}
}
else if(mPendingRequest.mType == Unhold)
{
mPendingRequest.mType = None; // Unhold pending, so move to do nothing
return;
}
else if(mPendingRequest.mType == Hold)
{
return; // Hold already pending
}
else
{
WarningLog(<< "RemoteParticipant::hold error: request already pending");
}
}
catch(BaseException &e)
{
WarningLog(<< "RemoteParticipant::hold exception: " << e);
}
catch(...)
{
WarningLog(<< "RemoteParticipant::hold unknown exception");
}
}
void
RemoteParticipant::unhold()
{
mLocalHold=false;
InfoLog(<< "RemoteParticipant::unhold request: handle=" << mHandle);
try
{
if(mPendingRequest.mType == None)
{
if(mState == Connected && mInviteSessionHandle.isValid())
{
provideOffer(false /* postOfferAccept */);
stateTransition(Unholding);
}
else
{
mPendingRequest.mType = Unhold;
}
}
else if(mPendingRequest.mType == Hold)
{
mPendingRequest.mType = None; // Hold pending, so move do nothing
return;
}
else if(mPendingRequest.mType == Unhold)
{
return; // Unhold already pending
}
else
{
WarningLog(<< "RemoteParticipant::unhold error: request already pending");
}
}
catch(BaseException &e)
{
WarningLog(<< "RemoteParticipant::unhold exception: " << e);
}
catch(...)
{
WarningLog(<< "RemoteParticipant::unhold unknown exception");
}
}
void
RemoteParticipant::setRemoteHold(bool remoteHold)
{
bool stateChanged = (remoteHold != mRemoteHold);
mRemoteHold = remoteHold;
if(stateChanged)
{
mConversationManager.onParticipantRequestedHold(mHandle, mRemoteHold);
}
}
void
RemoteParticipant::setPendingOODReferInfo(ServerOutOfDialogReqHandle ood, const SipMessage& referMsg)
{
stateTransition(PendingOODRefer);
mPendingOODReferMsg = referMsg;
mPendingOODReferNoSubHandle = ood;
}
void
RemoteParticipant::setPendingOODReferInfo(ServerSubscriptionHandle ss, const SipMessage& referMsg)
{
stateTransition(PendingOODRefer);
mPendingOODReferMsg = referMsg;
mPendingOODReferSubHandle = ss;
}
void
RemoteParticipant::acceptPendingOODRefer()
{
if(mState == PendingOODRefer)
{
SharedPtr<UserProfile> profile;
bool accepted = false;
if(mPendingOODReferNoSubHandle.isValid())
{
mPendingOODReferNoSubHandle->send(mPendingOODReferNoSubHandle->accept(202)); // Accept OOD Refer
profile = mPendingOODReferNoSubHandle->getUserProfile();
accepted = true;
}
else if(mPendingOODReferSubHandle.isValid())
{
mPendingOODReferSubHandle->send(mPendingOODReferSubHandle->accept(202)); // Accept OOD Refer
profile = mPendingOODReferSubHandle->getUserProfile();
accepted = true;
}
if(accepted)
{
// Create offer
SdpContents offer;
buildSdpOffer(mLocalHold, offer);
// Build the Invite
SharedPtr<SipMessage> invitemsg = mDum.makeInviteSessionFromRefer(mPendingOODReferMsg,
profile,
mPendingOODReferSubHandle, // Note will be invalid if refer no-sub, which is fine
&offer,
DialogUsageManager::None, //EncryptionLevel
0, //Aleternative Contents
&mDialogSet);
mDialogSet.sendInvite(invitemsg);
adjustRTPStreams(true);
stateTransition(Connecting);
}
else
{
WarningLog(<< "acceptPendingOODRefer - no valid handles");
mConversationManager.onParticipantTerminated(mHandle, 500);
delete this;
}
}
}
void
RemoteParticipant::rejectPendingOODRefer(unsigned int statusCode)
{
if(mState == PendingOODRefer)
{
if(mPendingOODReferNoSubHandle.isValid())
{
mPendingOODReferNoSubHandle->send(mPendingOODReferNoSubHandle->reject(statusCode));
mConversationManager.onParticipantTerminated(mHandle, statusCode);
}
else if(mPendingOODReferSubHandle.isValid())
{
mPendingOODReferSubHandle->send(mPendingOODReferSubHandle->reject(statusCode));
mConversationManager.onParticipantTerminated(mHandle, statusCode);
}
else
{
WarningLog(<< "rejectPendingOODRefer - no valid handles");
mConversationManager.onParticipantTerminated(mHandle, 500);
}
mDialogSet.destroy(); // Will also cause "this" to be deleted
}
}
void
RemoteParticipant::redirectPendingOODRefer(resip::NameAddr& destination)
{
if(mState == PendingOODRefer)
{
if(mPendingOODReferNoSubHandle.isValid())
{
SharedPtr<SipMessage> redirect = mPendingOODReferNoSubHandle->reject(302 /* Moved Temporarily */);
redirect->header(h_Contacts).clear();
redirect->header(h_Contacts).push_back(destination);
mPendingOODReferNoSubHandle->send(redirect);
mConversationManager.onParticipantTerminated(mHandle, 302 /* Moved Temporarily */);
}
else if(mPendingOODReferSubHandle.isValid())
{
SharedPtr<SipMessage> redirect = mPendingOODReferSubHandle->reject(302 /* Moved Temporarily */);
redirect->header(h_Contacts).clear();
redirect->header(h_Contacts).push_back(destination);
mPendingOODReferSubHandle->send(redirect);
mConversationManager.onParticipantTerminated(mHandle, 302 /* Moved Temporarily */);
}
else
{
WarningLog(<< "rejectPendingOODRefer - no valid handles");
mConversationManager.onParticipantTerminated(mHandle, 500);
}
mDialogSet.destroy(); // Will also cause "this" to be deleted
}
}
void
RemoteParticipant::processReferNotify(const SipMessage& notify)
{
unsigned int code = 400; // Bad Request - default if for some reason a valid sipfrag is not present
SipFrag* frag = dynamic_cast<SipFrag*>(notify.getContents());
if (frag)
{
// Get StatusCode from SipFrag
if (frag->message().isResponse())
{
code = frag->message().header(h_StatusLine).statusCode();
}
}
// Check if success or failure response code was in SipFrag
if(code >= 200 && code < 300)
{
if(mState == Redirecting)
{
if (mHandle) mConversationManager.onParticipantRedirectSuccess(mHandle);
stateTransition(Connected);
}
}
else if(code >= 300)
{
if(mState == Redirecting)
{
if (mHandle) mConversationManager.onParticipantRedirectFailure(mHandle, code);
stateTransition(Connected);
}
}
}
void
RemoteParticipant::provideOffer(bool postOfferAccept)
{
std::auto_ptr<SdpContents> offer(new SdpContents);
resip_assert(mInviteSessionHandle.isValid());
buildSdpOffer(mLocalHold, *offer);
mDialogSet.provideOffer(offer, mInviteSessionHandle, postOfferAccept);
mOfferRequired = false;
}
bool
RemoteParticipant::provideAnswer(const SdpContents& offer, bool postAnswerAccept, bool postAnswerAlert)
{
auto_ptr<SdpContents> answer(new SdpContents);
resip_assert(mInviteSessionHandle.isValid());
bool answerOk = buildSdpAnswer(offer, *answer);
if(answerOk)
{
mDialogSet.provideAnswer(answer, mInviteSessionHandle, postAnswerAccept, postAnswerAlert);
}
else
{
mInviteSessionHandle->reject(488);
}
return answerOk;
}
void
RemoteParticipant::buildSdpOffer(bool holdSdp, SdpContents& offer)
{
SdpContents::Session::Medium *audioMedium = 0;
ConversationProfile *profile = dynamic_cast<ConversationProfile*>(mDialogSet.getUserProfile().get());
std::auto_ptr<SdpContents> _sessionCaps;
if(!profile) // This can happen for UAC calls
{
DebugLog(<<"buildSdpOffer: no ConversationProfile available, calling getDefaultOutgoingConversationProfile");
profile = mConversationManager.getUserAgent()->getDefaultOutgoingConversationProfile().get();
// if using the default profile, we need a copy of the session caps that we can modify
_sessionCaps.reset(new SdpContents(profile->sessionCaps()));
}
// If we already have a local sdp for this sesion, then use this to form the next offer - doing so will ensure
// that we do not switch codecs or payload id's mid session.
if(mInviteSessionHandle.isValid() && mInviteSessionHandle->getLocalSdp().session().media().size() != 0)
{
offer = mInviteSessionHandle->getLocalSdp();
// Set sessionid and version for this sdp
UInt64 currentTime = Timer::getTimeMicroSec();
offer.session().origin().getSessionId() = currentTime;
offer.session().origin().getVersion() = currentTime;
// Find the audio medium
for (std::list<SdpContents::Session::Medium>::iterator mediaIt = offer.session().media().begin();
mediaIt != offer.session().media().end(); mediaIt++)
{
if(mediaIt->name() == "audio" &&
(mediaIt->protocol() == Symbols::RTP_AVP ||
mediaIt->protocol() == Symbols::RTP_SAVP ||
#ifdef RTP_SAVPF_FUDGE
mediaIt->protocol() == Symbols::RTP_SAVPF ||
#endif
mediaIt->protocol() == Symbols::UDP_TLS_RTP_SAVP))
{
audioMedium = &(*mediaIt);
break;
}
}
resip_assert(audioMedium);
// Add any codecs from our capabilities that may not be in current local sdp - since endpoint may have changed and may now be capable
// of handling codecs that it previously could not (common when endpoint is a B2BUA).
SdpContents* sessionCaps = _sessionCaps.get();
if(!sessionCaps)
{
sessionCaps = &(profile->sessionCaps());
}
int highPayloadId = 96; // Note: static payload id's are in range of 0-96
// Iterate through codecs in session caps and check if already in offer
for (std::list<SdpContents::Session::Codec>::iterator codecsIt = sessionCaps->session().media().front().codecs().begin();
codecsIt != sessionCaps->session().media().front().codecs().end(); codecsIt++)
{
bool found=false;
bool payloadIdCollision=false;
for (std::list<SdpContents::Session::Codec>::iterator codecsIt2 = audioMedium->codecs().begin();
codecsIt2 != audioMedium->codecs().end(); codecsIt2++)
{
if(isEqualNoCase(codecsIt->getName(), codecsIt2->getName()) &&
codecsIt->getRate() == codecsIt2->getRate())
{
found = true;
}
else if(codecsIt->payloadType() == codecsIt2->payloadType())
{
payloadIdCollision = true;
}
// Keep track of highest payload id in offer - used if we need to resolve a payload id conflict
if(codecsIt2->payloadType() > highPayloadId)
{
highPayloadId = codecsIt2->payloadType();
}
}
if(!found)
{
if(payloadIdCollision)
{
highPayloadId++;
codecsIt->payloadType() = highPayloadId;
}
else if(codecsIt->payloadType() > highPayloadId)
{
highPayloadId = codecsIt->payloadType();
}
audioMedium->addCodec(*codecsIt);
}
}
}
else
{
// Build base offer
mConversationManager.buildSdpOffer(profile, offer);
// Assumes there is only 1 media stream in session caps and it the audio one
audioMedium = &offer.session().media().front();
resip_assert(audioMedium);
// Set the local RTP Port
audioMedium->port() = mDialogSet.getLocalRTPPort();
}
// Add Crypto attributes (if required) - assumes there is only 1 media stream
audioMedium->clearAttribute("crypto");
audioMedium->clearAttribute("encryption");
audioMedium->clearAttribute("tcap");
audioMedium->clearAttribute("pcfg");
offer.session().clearAttribute("fingerprint");
offer.session().clearAttribute("setup");
if(mDialogSet.getSecureMediaMode() == ConversationProfile::Srtp)
{
// Note: We could add the crypto attribute to the "SDP Capabilties Negotiation"
// potential configuration if secure media is not required - but other implementations
// should ignore them any way if just plain RTP is used. It is thought the
// current implementation will increase interopability. (ie. SNOM Phones)
Data crypto;
switch(mDialogSet.getSrtpCryptoSuite())
{
case flowmanager::MediaStream::SRTP_AES_CM_128_HMAC_SHA1_32:
crypto = "1 AES_CM_128_HMAC_SHA1_32 inline:" + mDialogSet.getLocalSrtpSessionKey().base64encode();
audioMedium->addAttribute("crypto", crypto);
crypto = "2 AES_CM_128_HMAC_SHA1_80 inline:" + mDialogSet.getLocalSrtpSessionKey().base64encode();
audioMedium->addAttribute("crypto", crypto);