Skip to content

Commit 23f8805

Browse files
committed
F Added ffmpeg process start and SDP parsing from the output
1 parent 0d71ced commit 23f8805

2 files changed

Lines changed: 60 additions & 22 deletions

File tree

src/RTSPServerFFmpeg/Program.cs

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,47 @@
11
using Microsoft.Extensions.Configuration;
22
using SharpRTSPServer;
33
using System;
4+
using System.Diagnostics;
5+
using System.Text;
6+
using System.Threading;
47

58
IConfiguration config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
69
string hostName = config["HostName"];
710
ushort port = ushort.Parse(config["Port"]);
811
string userName = config["UserName"];
912
string password = config["Password"];
1013

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+
1236
using (var server = new RTSPServer(port, userName, password))
1337
{
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);
3545

3646
try
3747
{
@@ -49,4 +59,22 @@
4959
{
5060
System.Threading.Thread.Sleep(250);
5161
}
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+
}
5280
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"HostName": "127.0.0.1",
3+
"Port": 8554,
4+
"UserName": "admin",
5+
"Password": "password",
6+
"FFmpegPath": "C:\\Users\\lukas\\Downloads\\ffmpeg-6.0-full_build\\bin\\ffmpeg.exe",
7+
"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",
8+
"VideoUri": "rtp://127.0.0.1:11111",
9+
"AudioUri": "rtp://127.0.0.1:11113"
10+
}

0 commit comments

Comments
 (0)