|
1 | 1 | using Microsoft.Extensions.Configuration; |
2 | 2 | using SharpRTSPServer; |
3 | 3 | using System; |
| 4 | +using System.Collections.Generic; |
4 | 5 | using System.Diagnostics; |
5 | 6 | using System.IO; |
| 7 | +using System.Net; |
| 8 | +using System.Net.Sockets; |
6 | 9 | using System.Text; |
7 | 10 | using System.Threading; |
| 11 | +using System.Threading.Tasks; |
8 | 12 |
|
9 | 13 | IConfiguration config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build(); |
10 | 14 | string hostName = config["HostName"]; |
|
58 | 62 |
|
59 | 63 | using (var server = new RTSPServer(port, userName, password)) |
60 | 64 | { |
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()) |
82 | 66 | { |
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 | + } |
84 | 114 | } |
| 115 | +} |
85 | 116 |
|
86 | | - if (process != null) |
| 117 | +Task RunUdpClient(ProxyTrack track, Uri uri, CancellationToken cancellationToken) |
| 118 | +{ |
| 119 | + return Task.Run(() => |
87 | 120 | { |
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); |
90 | 147 | } |
91 | 148 |
|
92 | 149 | void Process_OutputDataReceived(object sender, DataReceivedEventArgs e) |
|
0 commit comments