forked from sipsorcery-org/sipsorcery
/
MediaStream.cs
870 lines (744 loc) · 35.2 KB
/
MediaStream.cs
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
//-----------------------------------------------------------------------------
// Filename: MediaStream.cs
//
// Description: Define a Media Stream to centralize all related objects: local/remote tracks, rtcp session, ip end point
// The goal is to simplify RTPSession class
//
// Author(s):
// Christophe Irles
//
// History:
// 05 Apr 2022 Christophe Irles Created (based on existing code from previous RTPSession class)
//
// License:
// BSD 3-Clause "New" or "Revised" License, see included LICENSE.md file.
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Microsoft.Extensions.Logging;
using SIPSorcery.Net;
using SIPSorcery.Sys;
namespace SIPSorcery.net.RTP
{
public class MediaStream
{
protected internal class PendingPackages
{
public RTPHeader hdr;
public int localPort;
public IPEndPoint remoteEndPoint;
public byte[] buffer;
public VideoStream videoStream;
public PendingPackages() { }
public PendingPackages(RTPHeader hdr, int localPort, IPEndPoint remoteEndPoint, byte[] buffer, VideoStream videoStream)
{
this.hdr = hdr;
this.localPort = localPort;
this.remoteEndPoint = remoteEndPoint;
this.buffer = buffer;
this.videoStream = videoStream;
}
}
protected object _pendingPackagesLock = new object();
protected List<PendingPackages> _pendingPackagesBuffer = new List<PendingPackages>();
private static ILogger logger = Log.Logger;
private uint m_lastRtpTimestamp;
private RtpSessionConfig RtpSessionConfig;
protected SecureContext SecureContext;
protected SrtpHandler SrtpHandler;
private RTPReorderBuffer RTPReorderBuffer = null;
MediaStreamTrack m_localTrack;
protected RTPChannel rtpChannel = null;
protected bool _isClosed = false;
public int Index = -1;
#region EVENTS
/// <summary>
/// Fires when the connection for a media type is classified as timed out due to not
/// receiving any RTP or RTCP packets within the given period.
/// </summary>
public event Action<int, SDPMediaTypesEnum> OnTimeoutByIndex;
/// <summary>
/// Gets fired when an RTCP report is sent. This event is for diagnostics only.
/// </summary>
public event Action<int, SDPMediaTypesEnum, RTCPCompoundPacket> OnSendReportByIndex;
/// <summary>
/// Gets fired when an RTP packet is received from a remote party.
/// Parameters are:
/// - Remote endpoint packet was received from,
/// - The media type the packet contains, will be audio or video,
/// - The full RTP packet.
/// </summary>
public event Action<int, IPEndPoint, SDPMediaTypesEnum, RTPPacket> OnRtpPacketReceivedByIndex;
/// <summary>
/// Gets fired when an RTP event is detected on the remote call party's RTP stream.
/// </summary>
public event Action<int, IPEndPoint, RTPEvent, RTPHeader> OnRtpEventByIndex;
/// <summary>
/// Gets fired when an RTCP report is received. This event is for diagnostics only.
/// </summary>
public event Action<int, IPEndPoint, SDPMediaTypesEnum, RTCPCompoundPacket> OnReceiveReportByIndex;
public event Action<bool> OnIsClosedStateChanged;
#endregion EVENTS
#region PROPERTIES
public Boolean AcceptRtpFromAny { get; set; } = false;
/// <summary>
/// Indicates whether the session has been closed. Once a session is closed it cannot
/// be restarted.
/// </summary>
public bool IsClosed
{
get
{
return _isClosed;
}
set
{
if (_isClosed == value)
{
return;
}
_isClosed = value;
//Clear previous buffer
ClearPendingPackages();
OnIsClosedStateChanged?.Invoke(_isClosed);
}
}
/// <summary>
/// In order to detect RTP events from the remote party this property needs to
/// be set to the payload ID they are using.
/// </summary>
public int RemoteRtpEventPayloadID { get; set; } = RTPSession.DEFAULT_DTMF_EVENT_PAYLOAD_ID;
/// <summary>
/// To type of this media
/// </summary>
public SDPMediaTypesEnum MediaType { get; set; }
/// <summary>
/// The local track. Will be null if we are not sending this media.
/// </summary>
public MediaStreamTrack LocalTrack
{
get
{
return m_localTrack;
}
set
{
m_localTrack = value;
if (m_localTrack != null)
{
// Need to create a sending SSRC and set it on the RTCP session.
if (RtcpSession != null)
{
RtcpSession.Ssrc = m_localTrack.Ssrc;
}
if (MediaType == SDPMediaTypesEnum.audio)
{
if (m_localTrack.Capabilities != null && !m_localTrack.NoDtmfSupport &&
!m_localTrack.Capabilities.Any(x => x.ID == RTPSession.DTMF_EVENT_PAYLOAD_ID))
{
SDPAudioVideoMediaFormat rtpEventFormat = new SDPAudioVideoMediaFormat(
SDPMediaTypesEnum.audio,
RTPSession.DTMF_EVENT_PAYLOAD_ID,
SDP.TELEPHONE_EVENT_ATTRIBUTE,
RTPSession.DEFAULT_AUDIO_CLOCK_RATE,
SDPAudioVideoMediaFormat.DEFAULT_AUDIO_CHANNEL_COUNT,
"0-16");
m_localTrack.Capabilities.Add(rtpEventFormat);
}
}
}
}
}
/// <summary>
/// The remote video track. Will be null if the remote party is not sending this media
/// </summary>
public MediaStreamTrack RemoteTrack { get; set; }
/// <summary>
/// The reporting session for this media stream.
/// </summary>
public RTCPSession RtcpSession { get; set; }
/// <summary>
/// The remote RTP end point this stream is sending media to.
/// </summary>
public IPEndPoint DestinationEndPoint { get; set; }
/// <summary>
/// The remote RTP control end point this stream is sending to RTCP reports for the media stream to.
/// </summary>
public IPEndPoint ControlDestinationEndPoint { get; set; }
#endregion PROPERTIES
#region REORDER BUFFER
public void AddBuffer(TimeSpan dropPacketTimeout)
{
RTPReorderBuffer = new RTPReorderBuffer(dropPacketTimeout);
}
public void RemoveBuffer(TimeSpan dropPacketTimeout)
{
RTPReorderBuffer = null;
}
public Boolean UseBuffer()
{
return RTPReorderBuffer != null;
}
public RTPReorderBuffer GetBuffer()
{
return RTPReorderBuffer;
}
#endregion REORDER BUFFER
#region SECURITY CONTEXT
public void SetSecurityContext(ProtectRtpPacket protectRtp, ProtectRtpPacket unprotectRtp, ProtectRtpPacket protectRtcp, ProtectRtpPacket unprotectRtcp)
{
if (SecureContext != null)
{
logger.LogTrace($"Tried adding new SecureContext for media type {MediaType}, but one already existed");
}
SecureContext = new SecureContext(protectRtp, unprotectRtp, protectRtcp, unprotectRtcp);
DispatchPendingPackages();
}
public SecureContext GetSecurityContext()
{
return SecureContext;
}
public Boolean IsSecurityContextReady()
{
return (SecureContext != null);
}
private (bool, byte[]) UnprotectBuffer(byte[] buffer)
{
if (SecureContext != null)
{
int res = SecureContext.UnprotectRtpPacket(buffer, buffer.Length, out int outBufLen);
if (res == 0)
{
return (true, buffer.Take(outBufLen).ToArray());
}
else
{
logger.LogWarning($"SRTP unprotect failed for {MediaType}, result {res}.");
}
}
return (false, buffer);
}
public bool EnsureBufferUnprotected(byte[] buf, RTPHeader header, out RTPPacket packet)
{
if (RtpSessionConfig.IsSecure || RtpSessionConfig.UseSdpCryptoNegotiation)
{
var (succeeded, newBuffer) = UnprotectBuffer(buf);
if (!succeeded)
{
packet = null;
return false;
}
packet = new RTPPacket(newBuffer);
}
else
{
packet = new RTPPacket(buf);
}
packet.Header.ReceivedTime = header.ReceivedTime;
return true;
}
public SrtpHandler GetOrCreateSrtpHandler()
{
if (SrtpHandler == null)
{
SrtpHandler = new SrtpHandler();
}
return SrtpHandler;
}
#endregion SECURITY CONTEXT
#region RTP CHANNEL
public void AddRtpChannel(RTPChannel rtpChannel)
{
this.rtpChannel = rtpChannel;
}
public Boolean HasRtpChannel()
{
return rtpChannel != null;
}
public RTPChannel GetRTPChannel()
{
return rtpChannel;
}
#endregion RTP CHANNEL
#region SEND PACKET
protected Boolean CheckIfCanSendRtpRaw()
{
if (IsClosed)
{
logger.LogWarning($"SendRtpRaw was called for an {MediaType} packet on an closed RTP session.");
return false;
}
if (LocalTrack == null)
{
logger.LogWarning($"SendRtpRaw was called for an {MediaType} packet on an RTP session without a local track.");
return false;
}
if ((LocalTrack.StreamStatus == MediaStreamStatusEnum.RecvOnly) || (LocalTrack.StreamStatus == MediaStreamStatusEnum.Inactive))
{
logger.LogWarning($"SendRtpRaw was called for an {MediaType} packet on an RTP session with a Stream Status set to {LocalTrack.StreamStatus}");
return false;
}
if ((RtpSessionConfig.IsSecure || RtpSessionConfig.UseSdpCryptoNegotiation) && SecureContext?.ProtectRtpPacket == null)
{
logger.LogWarning("SendRtpPacket cannot be called on a secure session before calling SetSecurityContext.");
return false;
}
return true;
}
protected void SendRtpRaw(byte[] data, uint timestamp, int markerBit, int payloadType, Boolean checkDone, ushort? seqNum = null)
{
if (checkDone || CheckIfCanSendRtpRaw())
{
ProtectRtpPacket protectRtpPacket = SecureContext?.ProtectRtpPacket;
int srtpProtectionLength = (protectRtpPacket != null) ? RTPSession.SRTP_MAX_PREFIX_LENGTH : 0;
RTPPacket rtpPacket = new RTPPacket(data.Length + srtpProtectionLength);
rtpPacket.Header.SyncSource = LocalTrack.Ssrc;
rtpPacket.Header.SequenceNumber = seqNum ?? LocalTrack.GetNextSeqNum();
rtpPacket.Header.Timestamp = timestamp;
rtpPacket.Header.MarkerBit = markerBit;
rtpPacket.Header.PayloadType = payloadType;
if (RemoteTrack.HeaderExtensions.TryGetValue(SDPMediaAnnouncement.RTP_HEADER_EXTENSION_ID_ABS_SEND_TIME, out var ext) &&
ext.Uri == SDPMediaAnnouncement.RTP_HEADER_EXTENSION_URI_ABS_SEND_TIME)
{
rtpPacket.Header.AddAbsSendTimeExtension();
}
Buffer.BlockCopy(data, 0, rtpPacket.Payload, 0, data.Length);
var rtpBuffer = rtpPacket.GetBytes();
if (protectRtpPacket == null)
{
rtpChannel.Send(RTPChannelSocketsEnum.RTP, DestinationEndPoint, rtpBuffer);
}
else
{
int outBufLen = 0;
int rtperr = protectRtpPacket(rtpBuffer, rtpBuffer.Length - srtpProtectionLength, out outBufLen);
if (rtperr != 0)
{
logger.LogError("SendRTPPacket protection failed, result " + rtperr + ".");
}
else
{
rtpChannel.Send(RTPChannelSocketsEnum.RTP, DestinationEndPoint, rtpBuffer.Take(outBufLen).ToArray());
}
}
m_lastRtpTimestamp = timestamp;
RtcpSession?.RecordRtpPacketSend(rtpPacket);
}
}
/// <summary>
/// Allows additional control for sending raw RTP payloads. No framing or other processing is carried out.
/// </summary>
/// <param name="mediaType">The media type of the RTP packet being sent. Must be audio or video.</param>
/// <param name="payload">The RTP packet payload.</param>
/// <param name="timestamp">The timestamp to set on the RTP header.</param>
/// <param name="markerBit">The value to set on the RTP header marker bit, should be 0 or 1.</param>
/// <param name="payloadTypeID">The payload ID to set in the RTP header.</param>
/// <param name="seqNum"> The RTP sequence number </param>
public void SendRtpRaw(byte[] data, uint timestamp, int markerBit, int payloadType, ushort seqNum)
{
SendRtpRaw(data, timestamp, markerBit, payloadType, false, seqNum);
}
/// <summary>
/// Allows additional control for sending raw RTP payloads. No framing or other processing is carried out.
/// </summary>
/// <param name="mediaType">The media type of the RTP packet being sent. Must be audio or video.</param>
/// <param name="payload">The RTP packet payload.</param>
/// <param name="timestamp">The timestamp to set on the RTP header.</param>
/// <param name="markerBit">The value to set on the RTP header marker bit, should be 0 or 1.</param>
/// <param name="payloadTypeID">The payload ID to set in the RTP header.</param>
public void SendRtpRaw(byte[] data, uint timestamp, int markerBit, int payloadType)
{
SendRtpRaw(data, timestamp, markerBit, payloadType, false);
}
/// <summary>
/// Allows additional control for sending raw RTCP payloads
/// </summary>
/// <param name="rtcpBytes">Raw RTCP report data to send.</param>
public void SendRtcpRaw(byte[] rtcpBytes)
{
if (SendRtcpReport(rtcpBytes))
{
RTCPCompoundPacket rtcpCompoundPacket = null;
try
{
rtcpCompoundPacket = new RTCPCompoundPacket(rtcpBytes);
}
catch (Exception excp)
{
logger.LogWarning($"Can't create RTCPCompoundPacket from the provided RTCP bytes. {excp.Message}");
}
if (rtcpCompoundPacket != null)
{
OnSendReportByIndex?.Invoke(Index, MediaType, rtcpCompoundPacket);
}
}
}
/// <summary>
/// Sends the RTCP report to the remote call party.
/// </summary>
/// <param name="reportBuffer">The serialised RTCP report to send.</param>
/// <returns>True if report was sent</returns>
private bool SendRtcpReport(byte[] reportBuffer)
{
if ((RtpSessionConfig.IsSecure || RtpSessionConfig.UseSdpCryptoNegotiation) && !IsSecurityContextReady())
{
logger.LogWarning("SendRtcpReport cannot be called on a secure session before calling SetSecurityContext.");
return false;
}
else if (ControlDestinationEndPoint != null)
{
//logger.LogDebug($"SendRtcpReport: {reportBytes.HexStr()}");
var sendOnSocket = RtpSessionConfig.IsRtcpMultiplexed ? RTPChannelSocketsEnum.RTP : RTPChannelSocketsEnum.Control;
var protectRtcpPacket = SecureContext?.ProtectRtcpPacket;
if (protectRtcpPacket == null)
{
rtpChannel.Send(sendOnSocket, ControlDestinationEndPoint, reportBuffer);
}
else
{
byte[] sendBuffer = new byte[reportBuffer.Length + RTPSession.SRTP_MAX_PREFIX_LENGTH];
Buffer.BlockCopy(reportBuffer, 0, sendBuffer, 0, reportBuffer.Length);
int outBufLen = 0;
int rtperr = protectRtcpPacket(sendBuffer, sendBuffer.Length - RTPSession.SRTP_MAX_PREFIX_LENGTH, out outBufLen);
if (rtperr != 0)
{
logger.LogWarning("SRTP RTCP packet protection failed, result " + rtperr + ".");
}
else
{
rtpChannel.Send(sendOnSocket, ControlDestinationEndPoint, sendBuffer.Take(outBufLen).ToArray());
}
}
}
return true;
}
/// <summary>
/// Sends the RTCP report to the remote call party.
/// </summary>
/// <param name="report">RTCP report to send.</param>
public void SendRtcpReport(RTCPCompoundPacket report)
{
if ((RtpSessionConfig.IsSecure || RtpSessionConfig.UseSdpCryptoNegotiation) && !IsSecurityContextReady() && report.Bye != null)
{
// Do nothing. The RTCP BYE gets generated when an RTP session is closed.
// If that occurs before the connection was able to set up the secure context
// there's no point trying to send it.
}
else
{
var reportBytes = report.GetBytes();
SendRtcpReport(reportBytes);
OnSendReportByIndex?.Invoke(Index, MediaType, report);
}
}
/// <summary>
/// Allows sending of RTCP feedback reports.
/// </summary>
/// <param name="mediaType">The media type of the RTCP report being sent. Must be audio or video.</param>
/// <param name="feedback">The feedback report to send.</param>
public void SendRtcpFeedback(RTCPFeedback feedback)
{
var reportBytes = feedback.GetBytes();
SendRtcpReport(reportBytes);
}
#endregion SEND PACKET
#region RECEIVE PACKET
public void OnReceiveRTPPacket(RTPHeader hdr, int localPort, IPEndPoint remoteEndPoint, byte[] buffer, VideoStream videoStream = null)
{
RTPPacket rtpPacket = null;
if (RemoteRtpEventPayloadID != 0 && hdr.PayloadType == RemoteRtpEventPayloadID)
{
if (!EnsureBufferUnprotected(buffer, hdr, out rtpPacket))
{
// Cache pending packages to use it later to prevent missing frames
// when DTLS was not completed yet as a Server bt already completed as a client
AddPendingPackage(hdr, localPort, remoteEndPoint, buffer, videoStream);
return;
}
RaiseOnRtpEventByIndex(remoteEndPoint, new RTPEvent(rtpPacket.Payload), rtpPacket.Header);
return;
}
// Set the remote track SSRC so that RTCP reports can match the media type.
if (RemoteTrack != null && RemoteTrack.Ssrc == 0 && DestinationEndPoint != null)
{
bool isValidSource = AdjustRemoteEndPoint(hdr.SyncSource, remoteEndPoint);
if (isValidSource)
{
logger.LogDebug($"Set remote track ({MediaType} - index={Index}) SSRC to {hdr.SyncSource}.");
RemoteTrack.Ssrc = hdr.SyncSource;
}
}
// Note AC 24 Dec 2020: The problem with waiting until the remote description is set is that the remote peer often starts sending
// RTP packets at the same time it signals its SDP offer or answer. Generally this is not a problem for audio but for video streams
// the first RTP packet(s) are the key frame and if they are ignored the video stream will take additional time or manual
// intervention to synchronise.
//if (RemoteDescription != null)
//{
// Don't hand RTP packets to the application until the remote description has been set. Without it
// things like the common codec, DTMF support etc. are not known.
//SDPMediaTypesEnum mediaType = (rtpMediaType.HasValue) ? rtpMediaType.Value : DEFAULT_MEDIA_TYPE;
// For video RTP packets an attempt will be made to collate into frames. It's up to the application
// whether it wants to subscribe to frames of RTP packets.
rtpPacket = null;
if (RemoteTrack != null)
{
LogIfWrongSeqNumber($"{MediaType}", hdr, RemoteTrack);
ProcessHeaderExtensions(hdr);
}
if (!EnsureBufferUnprotected(buffer, hdr, out rtpPacket))
{
return;
}
// When receiving an Payload from other peer, it will be related to our LocalDescription,
// not to RemoteDescription (as proved by Azure WebRTC Implementation)
var format = LocalTrack?.GetFormatForPayloadID(hdr.PayloadType);
if ((rtpPacket != null) && (format != null))
{
if (UseBuffer())
{
var reorderBuffer = GetBuffer();
reorderBuffer.Add(rtpPacket);
while (reorderBuffer.Get(out var bufferedPacket))
{
if (RemoteTrack != null)
{
LogIfWrongSeqNumber($"{MediaType}", bufferedPacket.Header, RemoteTrack);
RemoteTrack.LastRemoteSeqNum = bufferedPacket.Header.SequenceNumber;
}
videoStream?.ProcessVideoRtpFrame(remoteEndPoint, bufferedPacket, format.Value);
RaiseOnRtpPacketReceivedByIndex(remoteEndPoint, bufferedPacket);
}
}
else
{
videoStream?.ProcessVideoRtpFrame(remoteEndPoint, rtpPacket, format.Value);
RaiseOnRtpPacketReceivedByIndex(remoteEndPoint, rtpPacket);
}
RtcpSession?.RecordRtpPacketReceived(rtpPacket);
}
}
#endregion RECEIVE PACKET
#region TO RAISE EVENTS FROM INHERITED CLASS
public void RaiseOnReceiveReportByIndex(IPEndPoint ipEndPoint, RTCPCompoundPacket rtcpPCompoundPacket)
{
OnReceiveReportByIndex?.Invoke(Index, ipEndPoint, MediaType, rtcpPCompoundPacket);
}
protected void RaiseOnRtpEventByIndex(IPEndPoint ipEndPoint, RTPEvent rtpEvent, RTPHeader rtpHeader)
{
OnRtpEventByIndex?.Invoke(Index, ipEndPoint, rtpEvent, rtpHeader);
}
protected void RaiseOnRtpPacketReceivedByIndex(IPEndPoint ipEndPoint, RTPPacket rtpPacket)
{
OnRtpPacketReceivedByIndex?.Invoke(Index, ipEndPoint, MediaType, rtpPacket);
}
private void RaiseOnTimeoutByIndex(SDPMediaTypesEnum mediaType)
{
OnTimeoutByIndex?.Invoke(Index, mediaType);
}
#endregion TO RAISE EVENTS FROM INHERITED CLASS
#region PENDING PACKAGES LOGIC
// Submit all previous cached packages to self
protected virtual void DispatchPendingPackages()
{
PendingPackages[] pendingPackagesArray = null;
var isContextValid = SecureContext != null && !IsClosed;
lock (_pendingPackagesLock)
{
if (isContextValid)
{
pendingPackagesArray = _pendingPackagesBuffer.ToArray();
}
_pendingPackagesBuffer.Clear();
}
if (isContextValid)
{
foreach (var pendingPackage in pendingPackagesArray)
{
if (pendingPackage != null)
{
OnReceiveRTPPacket(pendingPackage.hdr, pendingPackage.localPort, pendingPackage.remoteEndPoint, pendingPackage.buffer, pendingPackage.videoStream);
}
}
}
}
// Clear previous buffer
protected virtual void ClearPendingPackages()
{
lock (_pendingPackagesLock)
{
_pendingPackagesBuffer.Clear();
}
}
// Cache pending packages to use it later to prevent missing frames
// when DTLS was not completed yet as a Server but already completed as a client
protected virtual bool AddPendingPackage(RTPHeader hdr, int localPort, IPEndPoint remoteEndPoint, byte[] buffer, VideoStream videoStream = null)
{
const int MAX_PENDING_PACKAGES_BUFFER_SIZE = 32;
if (SecureContext == null && !IsClosed)
{
lock (_pendingPackagesLock)
{
//ensure buffer max size
while (_pendingPackagesBuffer.Count > 0 && _pendingPackagesBuffer.Count >= MAX_PENDING_PACKAGES_BUFFER_SIZE)
{
_pendingPackagesBuffer.RemoveAt(0);
}
_pendingPackagesBuffer.Add(new PendingPackages(hdr, localPort, remoteEndPoint, buffer, videoStream));
}
return true;
}
return false;
}
#endregion
protected void LogIfWrongSeqNumber(string trackType, RTPHeader header, MediaStreamTrack track)
{
if (track.LastRemoteSeqNum != 0 &&
header.SequenceNumber != (track.LastRemoteSeqNum + 1) &&
!(header.SequenceNumber == 0 && track.LastRemoteSeqNum == ushort.MaxValue))
{
logger.LogWarning($"{trackType} stream sequence number jumped from {track.LastRemoteSeqNum} to {header.SequenceNumber}.");
}
}
/// <summary>
/// Adjusts the expected remote end point for a particular media type.
/// </summary>
/// <param name="mediaType">The media type of the RTP packet received.</param>
/// <param name="ssrc">The SSRC from the RTP packet header.</param>
/// <param name="receivedOnEndPoint">The actual remote end point that the RTP packet came from.</param>
/// <returns>True if remote end point for this media type was the expected one or it was adjusted. False if
/// the remote end point was deemed to be invalid for this media type.</returns>
protected bool AdjustRemoteEndPoint(uint ssrc, IPEndPoint receivedOnEndPoint)
{
bool isValidSource = false;
IPEndPoint expectedEndPoint = DestinationEndPoint;
if (expectedEndPoint.Address.Equals(receivedOnEndPoint.Address) && expectedEndPoint.Port == receivedOnEndPoint.Port)
{
// Exact match on actual and expected destination.
isValidSource = true;
}
else if (AcceptRtpFromAny || (expectedEndPoint.Address.IsPrivate() && !receivedOnEndPoint.Address.IsPrivate())
//|| (IPAddress.Loopback.Equals(receivedOnEndPoint.Address) || IPAddress.IPv6Loopback.Equals(receivedOnEndPoint.Address
)
{
// The end point doesn't match BUT we were supplied a private address in the SDP and the remote source is a public address
// so high probability there's a NAT on the network path. Switch to the remote end point (note this can only happen once
// and only if the SSRV is 0, i.e. this is the first RTP packet.
// If the remote end point is a loopback address then it's likely that this is a test/development
// scenario and the source can be trusted.
// AC 12 Jul 2020: Commented out the expression that allows the end point to be change just because it's a loopback address.
// A breaking case is doing an attended transfer test where two different agents are using loopback addresses.
// The expression allows an older session to override the destination set by a newer remote SDP.
// AC 18 Aug 2020: Despite the carefully crafted rules below and https://github.com/sipsorcery/sipsorcery/issues/197
// there are still cases that were a problem in one scenario but acceptable in another. To accommodate a new property
// was added to allow the application to decide whether the RTP end point switches should be liberal or not.
logger.LogDebug($"{MediaType} end point switched for RTP ssrc {ssrc} from {expectedEndPoint} to {receivedOnEndPoint}.");
DestinationEndPoint = receivedOnEndPoint;
if (RtpSessionConfig.IsRtcpMultiplexed)
{
ControlDestinationEndPoint = DestinationEndPoint;
}
else
{
ControlDestinationEndPoint = new IPEndPoint(DestinationEndPoint.Address, DestinationEndPoint.Port + 1);
}
isValidSource = true;
}
else
{
logger.LogWarning($"RTP packet with SSRC {ssrc} received from unrecognised end point {receivedOnEndPoint}.");
}
return isValidSource;
}
/// <summary>
/// Creates a new RTCP session for a media track belonging to this RTP session.
/// </summary>
/// <param name="mediaType">The media type to create the RTP session for. Must be
/// audio or video.</param>
/// <returns>A new RTCPSession object. The RTCPSession must have its Start method called
/// in order to commence sending RTCP reports.</returns>
public Boolean CreateRtcpSession()
{
if (RtcpSession == null)
{
RtcpSession = new RTCPSession(MediaType, 0);
RtcpSession.OnTimeout += RaiseOnTimeoutByIndex;
return true;
}
return false;
}
/// <summary>
/// Sets the remote end points for a media type supported by this RTP session.
/// </summary>
/// <param name="mediaType">The media type, must be audio or video, to set the remote end point for.</param>
/// <param name="rtpEndPoint">The remote end point for RTP packets corresponding to the media type.</param>
/// <param name="rtcpEndPoint">The remote end point for RTCP packets corresponding to the media type.</param>
public void SetDestination(IPEndPoint rtpEndPoint, IPEndPoint rtcpEndPoint)
{
DestinationEndPoint = rtpEndPoint;
ControlDestinationEndPoint = rtcpEndPoint;
}
/// <summary>
/// Attempts to get the highest priority sending format for the remote call party.
/// </summary>
/// <returns>The first compatible media format found for the specified media type.</returns>
public SDPAudioVideoMediaFormat GetSendingFormat()
{
if (LocalTrack != null || RemoteTrack != null)
{
if (LocalTrack == null)
{
return RemoteTrack.Capabilities.First();
}
else if (RemoteTrack == null)
{
return LocalTrack.Capabilities.First();
}
SDPAudioVideoMediaFormat format;
if (MediaType == SDPMediaTypesEnum.audio)
{
format = SDPAudioVideoMediaFormat.GetCompatibleFormats(RemoteTrack.Capabilities, LocalTrack.Capabilities)
.Where(x => x.ID != RemoteRtpEventPayloadID).FirstOrDefault();
}
else
{
format = SDPAudioVideoMediaFormat.GetCompatibleFormats(RemoteTrack.Capabilities, LocalTrack.Capabilities).First();
}
if (format.IsEmpty())
{
// It's not expected that this occurs as a compatibility check is done when the remote session description
// is set. By this point a compatible codec should be available.
throw new ApplicationException($"No compatible sending format could be found for media {MediaType}.");
}
else
{
return format;
}
}
else
{
throw new ApplicationException($"Cannot get the {MediaType} sending format, missing either local or remote {MediaType} track.");
}
}
public void ProcessHeaderExtensions(RTPHeader header)
{
header.GetHeaderExtensions().ToList().ForEach(x =>
{
if (RemoteTrack != null)
{
var ntpTimestamp = x.GetNtpTimestamp(RemoteTrack.HeaderExtensions);
if (ntpTimestamp.HasValue)
{
RemoteTrack.LastAbsoluteCaptureTimestamp = new TimestampPair() { NtpTimestamp = ntpTimestamp.Value, RtpTimestamp = header.Timestamp };
}
}
});
}
public MediaStream(RtpSessionConfig config, int index)
{
RtpSessionConfig = config;
this.Index = index;
}
}
}