|
| 1 | +using Microsoft.Extensions.Configuration; |
| 2 | +using SharpRTSPServer; |
| 3 | +using System; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.IO; |
| 6 | +using System.Text; |
| 7 | +using System.Threading; |
| 8 | + |
| 9 | +IConfiguration config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build(); |
| 10 | +string hostName = config["HostName"]; |
| 11 | +ushort port = ushort.Parse(config["Port"]); |
| 12 | +string userName = config["UserName"]; |
| 13 | +string password = config["Password"]; |
| 14 | + |
| 15 | +string ffmpegPath = config["FFmpegPath"]; // path to ffmpeg.exe |
| 16 | +string ffmpegArgs = config["FFmpegArgs"]; // Arguments that will be passed to the ffmpeg process |
| 17 | +string videoUri = config["VideoUri"]; // RTP video URI |
| 18 | +string audioUri = config["AudioUri"]; // RTP audio URI |
| 19 | +string sdpFile = config["SDPFile"]; // SDP file path (Optional in case ffmpegPath and ffmpegArgs are not specified. You have to launch ffmpeg before starting the server.) |
| 20 | + |
| 21 | +SemaphoreSlim semaphore = new SemaphoreSlim(0); |
| 22 | +StringBuilder sdpBuilder = new StringBuilder(); |
| 23 | +ProcessStartInfo info = new ProcessStartInfo(); |
| 24 | +Process process = null; |
| 25 | +string lastLine = null; |
| 26 | +string sdp = null; |
| 27 | + |
| 28 | +if (!string.IsNullOrEmpty(ffmpegPath) && !string.IsNullOrEmpty(ffmpegArgs)) |
| 29 | +{ |
| 30 | + // launch ffmpeg, parse the output and start streaming |
| 31 | + // 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 |
| 32 | + info.FileName = ffmpegPath; |
| 33 | + info.Arguments = ffmpegArgs; |
| 34 | + info.RedirectStandardOutput = true; |
| 35 | + info.UseShellExecute = false; |
| 36 | + |
| 37 | + process = Process.Start(info); |
| 38 | + process.OutputDataReceived += Process_OutputDataReceived; |
| 39 | + process.BeginOutputReadLine(); |
| 40 | + |
| 41 | + // wait until the SDP is read |
| 42 | + semaphore.Wait(); |
| 43 | + |
| 44 | + sdp = sdpBuilder.ToString(); |
| 45 | +} |
| 46 | +else if(!string.IsNullOrEmpty(sdpFile)) |
| 47 | +{ |
| 48 | + // optionally, read SDP from a file |
| 49 | + sdp = File.ReadAllText(sdpFile); |
| 50 | +} |
| 51 | +else |
| 52 | +{ |
| 53 | + throw new Exception("Invalid configuration! Either ffmpegPath and ffmpegArgs, or SDPFile must be specified!"); |
| 54 | +} |
| 55 | + |
| 56 | +if (string.IsNullOrEmpty(videoUri) && string.IsNullOrEmpty(audioUri)) |
| 57 | + throw new Exception("Invalid configuration! Either VideoUri, AudioUri or both must be specified!"); |
| 58 | + |
| 59 | +using (var server = new RTSPServer(port, userName, password)) |
| 60 | +{ |
| 61 | + if (!string.IsNullOrEmpty(videoUri)) |
| 62 | + server.AddVideoTrack(new ProxyTrack(ProxyTrackType.Video, videoUri)); |
| 63 | + |
| 64 | + if (!string.IsNullOrEmpty(audioUri)) |
| 65 | + server.AddAudioTrack(new ProxyTrack(ProxyTrackType.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) |
| 82 | + { |
| 83 | + Thread.Sleep(250); |
| 84 | + } |
| 85 | + |
| 86 | + if (process != null) |
| 87 | + { |
| 88 | + process.Kill(); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +void Process_OutputDataReceived(object sender, DataReceivedEventArgs e) |
| 93 | +{ |
| 94 | + if(lastLine == "" && e.Data == "") |
| 95 | + { |
| 96 | + semaphore.Release(); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + lastLine = e.Data; |
| 101 | + |
| 102 | + if (!string.IsNullOrEmpty(e.Data) && !e.Data.StartsWith("SDP:")) |
| 103 | + { |
| 104 | + sdpBuilder.AppendLine(e.Data); |
| 105 | + } |
| 106 | +} |
0 commit comments