Skip to content

Commit 7355ff8

Browse files
committed
F Added readme, added SDP file, improved configuration, added comments
1 parent 23f8805 commit 7355ff8

5 files changed

Lines changed: 63 additions & 27 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ Simple RTSP client that supports H264, H265 for video and AAC for audio.
77
## SharpRTSPServer
88
Simple RTSP server that supports H264, H265 for video and AAC, PCMU and PCMA for audio.
99

10+
## FFmpeg RTSP Server
11+
Sample RTSP server for ffmpeg RTP streams. Fully configurable in appsettings.json.
12+
1013
## Remarks
1114
Still work in progress, APIs are subject to change.
1215

src/RTSPServerFFmpeg/Program.cs

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using SharpRTSPServer;
33
using System;
44
using System.Diagnostics;
5+
using System.IO;
56
using System.Text;
67
using System.Threading;
78

@@ -11,37 +12,59 @@
1112
string userName = config["UserName"];
1213
string password = config["Password"];
1314

14-
string ffmpegPath = config["FFmpegPath"];
15-
string ffmpegArgs = config["FFmpegArgs"];
16-
string videoUri = config["VideoUri"];
17-
string audioUri = config["AudioUri"];
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.)
1820

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
21+
SemaphoreSlim semaphore = new SemaphoreSlim(0);
22+
StringBuilder sdpBuilder = new StringBuilder();
2023
ProcessStartInfo info = new ProcessStartInfo();
21-
info.FileName = ffmpegPath;
22-
info.Arguments = ffmpegArgs;
23-
info.RedirectStandardOutput = true;
24-
info.UseShellExecute = false;
25-
24+
Process process = null;
2625
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();
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();
3240

33-
// wait until the SDP is read
34-
semaphore.Wait();
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!");
3558

3659
using (var server = new RTSPServer(port, userName, password))
3760
{
3861
if (!string.IsNullOrEmpty(videoUri))
39-
server.AddVideoTrack(new ProxyTrack(0, videoUri));
62+
server.AddVideoTrack(new ProxyTrack(ProxyTrackType.Video, videoUri));
4063

4164
if (!string.IsNullOrEmpty(audioUri))
42-
server.AddAudioTrack(new ProxyTrack(1, audioUri));
65+
server.AddAudioTrack(new ProxyTrack(ProxyTrackType.Audio, audioUri));
4366

44-
server.OverrideSDP(sdp.ToString(), true);
67+
server.OverrideSDP(sdp, true);
4568

4669
try
4770
{
@@ -57,10 +80,13 @@
5780
Console.WriteLine("Press any key to exit");
5881
while (!Console.KeyAvailable)
5982
{
60-
System.Threading.Thread.Sleep(250);
83+
Thread.Sleep(250);
6184
}
6285

63-
process.Kill();
86+
if (process != null)
87+
{
88+
process.Kill();
89+
}
6490
}
6591

6692
void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
@@ -75,6 +101,6 @@ void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
75101

76102
if (!string.IsNullOrEmpty(e.Data) && !e.Data.StartsWith("SDP:"))
77103
{
78-
sdp.AppendLine(e.Data);
104+
sdpBuilder.AppendLine(e.Data);
79105
}
80106
}

src/RTSPServerFFmpeg/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
"FFmpegPath": "C:\\Users\\lukas\\Downloads\\ffmpeg-6.0-full_build\\bin\\ffmpeg.exe",
77
"FFmpegArgs": "-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",
88
"VideoUri": "rtp://127.0.0.1:11111",
9-
"AudioUri": "rtp://127.0.0.1:11113"
9+
"AudioUri": "rtp://127.0.0.1:11113",
10+
"SDPFile": null
1011
}

src/SharpRTSPServer/ProxyTrack.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99

1010
namespace SharpRTSPServer
1111
{
12+
public enum ProxyTrackType : int
13+
{
14+
Video = 0,
15+
Audio = 1
16+
}
17+
1218
public class ProxyTrack : TrackBase
1319
{
1420
public override string Codec => "PROXY";
@@ -27,9 +33,9 @@ public override bool IsReady
2733

2834
public Uri Uri { get; }
2935

30-
public ProxyTrack(int id, string uri)
36+
public ProxyTrack(ProxyTrackType type, string uri)
3137
{
32-
this.ID = id;
38+
this.ID = (int)type;
3339
this.Uri = new Uri(uri, UriKind.Absolute);
3440
Connect(this.Uri);
3541
}

src/SharpRTSPServer/SharpRTSPServer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
1414
<Nullable>disable</Nullable>
1515
<Title>$(ProjectName)</Title>
16-
<Version>0.0.1</Version>
16+
<Version>0.0.2</Version>
1717
<Authors>Lukas Volf</Authors>
1818
<Copyright>MIT</Copyright>
1919
<PackageProjectUrl>https://github.com/jimm98y/SharpRealTimeStreaming</PackageProjectUrl>

0 commit comments

Comments
 (0)