Skip to content

Commit 766317c

Browse files
committed
F Added ProxyTrack disposal, refactoring, cleanup
1 parent 42ad615 commit 766317c

5 files changed

Lines changed: 80 additions & 41 deletions

File tree

src/RTSPServerFFmpeg/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@
5959
using (var server = new RTSPServer(port, userName, password))
6060
{
6161
if (!string.IsNullOrEmpty(videoUri))
62-
server.AddVideoTrack(new ProxyTrack(ProxyTrackType.Video, videoUri));
62+
server.AddVideoTrack(new ProxyTrack(TrackType.Video, videoUri));
6363

6464
if (!string.IsNullOrEmpty(audioUri))
65-
server.AddAudioTrack(new ProxyTrack(ProxyTrackType.Audio, audioUri));
65+
server.AddAudioTrack(new ProxyTrack(TrackType.Audio, audioUri));
6666

6767
server.OverrideSDP(sdp, true);
6868

src/SharpRTSPServer/ProxyTrack.cs

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,16 @@
99

1010
namespace SharpRTSPServer
1111
{
12-
public enum ProxyTrackType : int
13-
{
14-
Video = 0,
15-
Audio = 1
16-
}
17-
18-
public class ProxyTrack : TrackBase
12+
public class ProxyTrack : TrackBase, IDisposable
1913
{
2014
public override string Codec => "PROXY";
2115

2216
public override int ID { get; set; }
2317
public override int PayloadType { get; set; }
2418

2519
private bool _isReady = false;
20+
private bool _disposedValue;
21+
2622
public override bool IsReady
2723
{
2824
get
@@ -33,7 +29,7 @@ public override bool IsReady
3329

3430
public Uri Uri { get; }
3531

36-
public ProxyTrack(ProxyTrackType type, string uri)
32+
public ProxyTrack(TrackType type, string uri)
3733
{
3834
this.ID = (int)type;
3935
this.Uri = new Uri(uri, UriKind.Absolute);
@@ -59,25 +55,53 @@ private async void Connect(Uri uri)
5955
{
6056
await Task.Run(() =>
6157
{
62-
UdpClient udpClient = new UdpClient(uri.Port);
63-
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(uri.Host), 0);
64-
_isReady = true;
65-
66-
while (true)
58+
try
6759
{
68-
try
60+
using (UdpClient udpClient = new UdpClient(uri.Port))
6961
{
70-
byte[] rtp = udpClient.Receive(ref remoteEndPoint);
71-
uint rtpTimestamp = RTPPacketUtil.ReadRtpTimestamp(rtp);
72-
Sink.FeedInRawRTP(ID, rtpTimestamp, new List<Memory<byte>>() { rtp });
73-
//Debug.WriteLine($"This message was sent from {remoteEndPoint.Address} on their port number {remoteEndPoint.Port}");
74-
}
75-
catch (Exception e)
76-
{
77-
Debug.WriteLine(e.ToString());
62+
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(uri.Host), 0);
63+
_isReady = true;
64+
65+
while (_isReady)
66+
{
67+
try
68+
{
69+
byte[] rtp = udpClient.Receive(ref remoteEndPoint);
70+
uint rtpTimestamp = RTPPacketUtil.ReadTS(rtp);
71+
Sink.FeedInRawRTP(ID, rtpTimestamp, new List<Memory<byte>>() { rtp });
72+
}
73+
catch (Exception e)
74+
{
75+
Debug.WriteLine(e.ToString());
76+
}
77+
}
7878
}
7979
}
80+
catch(Exception ee)
81+
{
82+
Debug.WriteLine(ee.ToString());
83+
_isReady = false;
84+
}
8085
});
8186
}
87+
88+
protected virtual void Dispose(bool disposing)
89+
{
90+
if (!_disposedValue)
91+
{
92+
if(disposing)
93+
{
94+
_isReady = false;
95+
}
96+
97+
_disposedValue = true;
98+
}
99+
}
100+
101+
public void Dispose()
102+
{
103+
Dispose(disposing: true);
104+
GC.SuppressFinalize(this);
105+
}
82106
}
83107
}

src/SharpRTSPServer/RTPPacketUtil.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@ public static class RTPPacketUtil
77
{
88
public const int RTP_VERSION = 2;
99

10-
public static uint ReadRtpTimestamp(byte[] data)
11-
{
12-
uint rtpTimestamp = ((uint)data[4] << 24) + (uint)(data[5] << 16) + (uint)(data[6] << 8) + data[7];
13-
return rtpTimestamp;
14-
}
15-
1610
public static void WriteHeader(
1711
Span<byte> rtpPacket,
1812
int rtpVersion,
@@ -31,14 +25,20 @@ public static void WriteSequenceNumber(Span<byte> rtpPacket, ushort sequenceId)
3125
BinaryPrimitives.WriteUInt16BigEndian(rtpPacket.Slice(2), sequenceId);
3226
}
3327

34-
public static void WriteTS(Span<byte> rtp_packet, uint ts)
28+
public static void WriteSSRC(Span<byte> rtp_packet, uint ssrc)
3529
{
36-
BinaryPrimitives.WriteUInt32BigEndian(rtp_packet.Slice(4), ts);
30+
BinaryPrimitives.WriteUInt32BigEndian(rtp_packet.Slice(8), ssrc);
3731
}
3832

39-
public static void WriteSSRC(Span<byte> rtp_packet, uint ssrc)
33+
public static uint ReadTS(byte[] data)
4034
{
41-
BinaryPrimitives.WriteUInt32BigEndian(rtp_packet.Slice(8), ssrc);
35+
uint rtpTimestamp = ((uint)data[4] << 24) + (uint)(data[5] << 16) + (uint)(data[6] << 8) + data[7];
36+
return rtpTimestamp;
37+
}
38+
39+
public static void WriteTS(Span<byte> rtp_packet, uint ts)
40+
{
41+
BinaryPrimitives.WriteUInt32BigEndian(rtp_packet.Slice(4), ts);
4242
}
4343
}
4444
}

src/SharpRTSPServer/RTSPServer.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ private string GenerateSDP()
513513
sdp.Append("c=IN IP4 0.0.0.0\n");
514514

515515
// VIDEO
516-
VideoTrack.BuildSDP(sdp);
516+
VideoTrack?.BuildSDP(sdp);
517517

518518
// AUDIO
519519
AudioTrack?.BuildSDP(sdp);
@@ -745,6 +745,18 @@ protected virtual void Dispose(bool disposing)
745745
{
746746
StopListen();
747747
_stopping?.Dispose();
748+
749+
if(VideoTrack is IDisposable disposableVideoTrack)
750+
{
751+
disposableVideoTrack.Dispose();
752+
VideoTrack = null;
753+
}
754+
755+
if (AudioTrack is IDisposable disposableAudioTrack)
756+
{
757+
disposableAudioTrack.Dispose();
758+
AudioTrack = null;
759+
}
748760
}
749761
}
750762

@@ -826,9 +838,6 @@ public class RTPStream
826838
/// </summary>
827839
public class RTSPConnection
828840
{
829-
public const int VIDEO = 0;
830-
public const int AUDIO = 1;
831-
832841
/// <summary>
833842
/// RTSP conneciton listener.
834843
/// </summary>
@@ -855,12 +864,12 @@ public class RTSPConnection
855864
/// <summary>
856865
/// Video stream.
857866
/// </summary>
858-
public RTPStream Video { get { return Streams[0]; } }
867+
public RTPStream Video { get { return Streams[(int)TrackType.Video]; } }
859868

860869
/// <summary>
861870
/// Audio stream.
862871
/// </summary>
863-
public RTPStream Audio { get { return Streams[1]; } }
872+
public RTPStream Audio { get { return Streams[(int)TrackType.Audio]; } }
864873

865874
public RTPStream[] Streams { get; } = new RTPStream[]
866875
{
@@ -1046,4 +1055,10 @@ public static string ToHexString(byte[] data)
10461055
#endif
10471056
}
10481057
}
1058+
1059+
public enum TrackType : int
1060+
{
1061+
Video = 0,
1062+
Audio = 1
1063+
}
10491064
}

src/SharpRTSPServer/TrackBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public virtual void FeedInRawSamples(uint rtpTimestamp, List<byte[]> samples)
3232
if (!Sink.CanAcceptNewSamples())
3333
return;
3434

35-
if (ID != 0 && ID != 1)
35+
if (ID != (int)TrackType.Video && ID != (int)TrackType.Audio)
3636
throw new ArgumentOutOfRangeException("ID must be 0 for video or 1 for audio");
3737

3838
(List<Memory<byte>> rtpPackets, List<IMemoryOwner<byte>> memoryOwners) = CreateRtpPackets(samples, rtpTimestamp);

0 commit comments

Comments
 (0)