Skip to content

Commit e1a6b69

Browse files
committed
F Added Pcap sample, removed UDP client from the ProxyTrack to make it more versatile
1 parent 7f01053 commit e1a6b69

8 files changed

Lines changed: 577 additions & 70 deletions

File tree

src/RTSPServerApp/frag_bunny.mp4

-550 KB
Binary file not shown.

src/RTSPServerFFmpeg/Program.cs

Lines changed: 82 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
using Microsoft.Extensions.Configuration;
22
using SharpRTSPServer;
33
using System;
4+
using System.Collections.Generic;
45
using System.Diagnostics;
56
using System.IO;
7+
using System.Net;
8+
using System.Net.Sockets;
69
using System.Text;
710
using System.Threading;
11+
using System.Threading.Tasks;
812

913
IConfiguration config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
1014
string hostName = config["HostName"];
@@ -58,35 +62,88 @@
5862

5963
using (var server = new RTSPServer(port, userName, password))
6064
{
61-
if (!string.IsNullOrEmpty(videoUri))
62-
server.AddVideoTrack(new ProxyTrack(TrackType.Video, videoUri));
63-
64-
if (!string.IsNullOrEmpty(audioUri))
65-
server.AddAudioTrack(new ProxyTrack(TrackType.Audio, audioUri));
66-
67-
server.OverrideSDP(sdp, true);
68-
69-
try
70-
{
71-
server.StartListen();
72-
}
73-
catch (Exception ex)
74-
{
75-
Console.WriteLine(ex.ToString());
76-
}
77-
78-
Console.WriteLine($"RTSP URL is rtsp://{userName}:{password}@{hostName}:{port}");
79-
80-
Console.WriteLine("Press any key to exit");
81-
while (!Console.KeyAvailable)
65+
using (CancellationTokenSource cts = new CancellationTokenSource())
8266
{
83-
Thread.Sleep(250);
67+
ProxyTrack videoTrack = null;
68+
ProxyTrack audioTrack = null;
69+
Task videoTask = null;
70+
Task audioTask = null;
71+
72+
if (!string.IsNullOrEmpty(videoUri))
73+
{
74+
videoTrack = new ProxyTrack(TrackType.Video);
75+
videoTask = RunUdpClient(videoTrack, new Uri(videoUri, UriKind.Absolute), cts.Token);
76+
server.AddVideoTrack(videoTrack);
77+
}
78+
79+
if (!string.IsNullOrEmpty(audioUri))
80+
{
81+
audioTrack = new ProxyTrack(TrackType.Audio);
82+
audioTask = RunUdpClient(audioTrack, new Uri(audioUri, UriKind.Absolute), cts.Token);
83+
server.AddAudioTrack(audioTrack);
84+
}
85+
86+
server.OverrideSDP(sdp, true);
87+
88+
videoTrack?.Start();
89+
audioTrack?.Start();
90+
91+
try
92+
{
93+
server.StartListen();
94+
}
95+
catch (Exception ex)
96+
{
97+
Console.WriteLine(ex.ToString());
98+
}
99+
100+
Console.WriteLine($"RTSP URL is rtsp://{userName}:{password}@{hostName}:{port}");
101+
102+
Console.WriteLine("Press any key to exit");
103+
while (!Console.KeyAvailable)
104+
{
105+
Thread.Sleep(250);
106+
}
107+
108+
await cts.CancelAsync();
109+
110+
if (process != null)
111+
{
112+
process.Kill();
113+
}
84114
}
115+
}
85116

86-
if (process != null)
117+
Task RunUdpClient(ProxyTrack track, Uri uri, CancellationToken cancellationToken)
118+
{
119+
return Task.Run(() =>
87120
{
88-
process.Kill();
89-
}
121+
try
122+
{
123+
using (UdpClient udpClient = new UdpClient(uri.Port))
124+
{
125+
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(uri.Host), 0);
126+
127+
while (!cancellationToken.IsCancellationRequested)
128+
{
129+
try
130+
{
131+
byte[] rtp = udpClient.Receive(ref remoteEndPoint);
132+
uint rtpTimestamp = RTPPacketUtil.ReadTS(rtp);
133+
track.FeedInRawSamples(rtpTimestamp, new List<byte[]>() { rtp });
134+
}
135+
catch (Exception e)
136+
{
137+
Debug.WriteLine(e.ToString());
138+
}
139+
}
140+
}
141+
}
142+
catch (Exception ee)
143+
{
144+
Debug.WriteLine(ee.ToString());
145+
}
146+
}, cancellationToken);
90147
}
91148

92149
void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)

0 commit comments

Comments
 (0)