|
1 | 1 | using Microsoft.Extensions.Configuration; |
2 | 2 | using SharpRTSPServer; |
3 | 3 | using System; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Text; |
| 6 | +using System.Threading; |
4 | 7 |
|
5 | 8 | IConfiguration config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build(); |
6 | 9 | string hostName = config["HostName"]; |
7 | 10 | ushort port = ushort.Parse(config["Port"]); |
8 | 11 | string userName = config["UserName"]; |
9 | 12 | string password = config["Password"]; |
10 | 13 |
|
11 | | -// ffmpeg.exe -re -stream_loop -1 -i C:\Git\SharpMediaCoder\src\SharpMediaPlayer\frag_bunny.mp4 -vcodec copy -an -f rtp rtp://127.0.0.1:11111 -vn -acodec copy -f rtp rtp://127.0.0.1:11113 |
| 14 | +string ffmpegPath = config["FFmpegPath"]; |
| 15 | +string ffmpegArgs = config["FFmpegArgs"]; |
| 16 | +string videoUri = config["VideoUri"]; |
| 17 | +string audioUri = config["AudioUri"]; |
| 18 | + |
| 19 | +// ffmpeg.exe -re -stream_loop -1 -i frag_bunny.mp4 -vcodec copy -an -f rtp rtp://127.0.0.1:11111 -vn -acodec copy -f rtp rtp://127.0.0.1:11113 |
| 20 | +ProcessStartInfo info = new ProcessStartInfo(); |
| 21 | +info.FileName = ffmpegPath; |
| 22 | +info.Arguments = ffmpegArgs; |
| 23 | +info.RedirectStandardOutput = true; |
| 24 | +info.UseShellExecute = false; |
| 25 | + |
| 26 | +string lastLine = null; |
| 27 | +SemaphoreSlim semaphore = new SemaphoreSlim(0); |
| 28 | +StringBuilder sdp = new StringBuilder(); |
| 29 | +var process = Process.Start(info); |
| 30 | +process.OutputDataReceived += Process_OutputDataReceived; |
| 31 | +process.BeginOutputReadLine(); |
| 32 | + |
| 33 | +// wait until the SDP is read |
| 34 | +semaphore.Wait(); |
| 35 | + |
12 | 36 | using (var server = new RTSPServer(port, userName, password)) |
13 | 37 | { |
14 | | - server.AddVideoTrack(new ProxyTrack(0, "rtp://127.0.0.1:11111")); |
15 | | - server.AddAudioTrack(new ProxyTrack(1, "rtp://127.0.0.1:11113")); |
16 | | - // original |
17 | | - string sdp = |
18 | | - "v=0\r\n" + |
19 | | - "o=- 0 0 IN IP4 127.0.0.1\r\n" + |
20 | | - "s=No Name\r\n" + |
21 | | - "t=0 0\r\n" + |
22 | | - "a=tool:libavformat 60.3.100\r\n" + |
23 | | - "m=video 11111 RTP/AVP 96\r\n" + |
24 | | - "c=IN IP4 127.0.0.1\r\n" + |
25 | | - "b=AS:587\r\n" + |
26 | | - "a=rtpmap:96 H264/90000\r\n" + |
27 | | - "a=fmtp:96 packetization-mode=1; sprop-parameter-sets=Z2QAHqzZQKAv+WagwCDW4AAAAwAgAAAGAeLFssA=,aOvjyyLA; profile-level-id=64001E\r\n" + |
28 | | - "m=audio 11113 RTP/AVP 97\r\n" + |
29 | | - "c=IN IP4 127.0.0.1\r\n" + |
30 | | - "b=AS:69\r\n" + |
31 | | - "a=rtpmap:97 MPEG4-GENERIC/44100/1\r\n" + |
32 | | - "a=fmtp:97 profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3; config=120856E500\r\n\r\n"; |
33 | | - |
34 | | - server.OverrideSDP(sdp, true); |
| 38 | + if (!string.IsNullOrEmpty(videoUri)) |
| 39 | + server.AddVideoTrack(new ProxyTrack(0, videoUri)); |
| 40 | + |
| 41 | + if (!string.IsNullOrEmpty(audioUri)) |
| 42 | + server.AddAudioTrack(new ProxyTrack(1, audioUri)); |
| 43 | + |
| 44 | + server.OverrideSDP(sdp.ToString(), true); |
35 | 45 |
|
36 | 46 | try |
37 | 47 | { |
|
49 | 59 | { |
50 | 60 | System.Threading.Thread.Sleep(250); |
51 | 61 | } |
| 62 | + |
| 63 | + process.Kill(); |
| 64 | +} |
| 65 | + |
| 66 | +void Process_OutputDataReceived(object sender, DataReceivedEventArgs e) |
| 67 | +{ |
| 68 | + if(lastLine == "" && e.Data == "") |
| 69 | + { |
| 70 | + semaphore.Release(); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + lastLine = e.Data; |
| 75 | + |
| 76 | + if (!string.IsNullOrEmpty(e.Data) && !e.Data.StartsWith("SDP:")) |
| 77 | + { |
| 78 | + sdp.AppendLine(e.Data); |
| 79 | + } |
52 | 80 | } |
0 commit comments