forked from sipsorcery-org/sipsorcery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
327 lines (272 loc) · 13.8 KB
/
Program.cs
File metadata and controls
327 lines (272 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//-----------------------------------------------------------------------------
// Filename: Program.cs
//
// Description: Listens on an RTP socket for a feed from ffmpeg and forwards it
// to a WebRTC peer.
//
// Author(s):
// Aaron Clauson (aaron@sipsorcery.com)
//
// History:
// 08 Jul 2020 Aaron Clauson Created, Dublin, Ireland.
//
// License:
// BSD 3-Clause "New" or "Revised" License, see included LICENSE.md file.
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Serilog;
using Serilog.Extensions.Logging;
using SIPSorcery.Net;
using WebSocketSharp;
using WebSocketSharp.Net.WebSockets;
using WebSocketSharp.Server;
namespace SIPSorcery.Examples
{
public class WebRtcClient : WebSocketBehavior
{
public RTCPeerConnection pc;
public event Func<WebSocketContext, Task<RTCPeerConnection>> WebSocketOpened;
public event Action<WebSocketContext, RTCPeerConnection, string> OnMessageReceived;
public WebRtcClient()
{ }
protected override void OnMessage(MessageEventArgs e)
{
OnMessageReceived(this.Context, pc, e.Data);
}
protected override async void OnOpen()
{
base.OnOpen();
pc = await WebSocketOpened(this.Context);
}
}
class Program
{
private const uint SSRC_REMOTE_VIDEO = 38106908;
private const int WEBSOCKET_PORT = 8081;
private const string FFMPEG_DEFAULT_COMMAND = "ffmpeg -re -f lavfi -i testsrc=size=640x480:rate=10 {0} -pix_fmt yuv420p -strict experimental -g 1 -ssrc {2} -f rtp rtp://127.0.0.1:{1} -sdp_file {3}";
private const string FFMPEG_SDP_FILE = "ffmpeg.sdp";
private const int FFMPEG_DEFAULT_RTP_PORT = 5020;
/// <summary>
/// The codec to pass to ffmpeg via the command line. WebRTC supported options are:
/// - vp8
/// - vp9
/// - h264
/// - libx265
/// Note if you change this option you will need to delete the ffmpeg.sdp file.
/// </summary>
private const string FFMPEG_VP8_CODEC = "-vcodec vp8";
private const string FFMPEG_VP9_CODEC = "-vcodec vp9";
private const string FFMPEG_H264_CODEC = "-vcodec h264";
private const string FFMPEG_H265_CODEC = "-c:v libx265";
private const string FFMPEG_DEFAULT_CODEC = FFMPEG_H265_CODEC;
private static Microsoft.Extensions.Logging.ILogger logger = NullLogger.Instance;
private static WebSocketServer _webSocketServer;
private static SDPAudioVideoMediaFormat _ffmpegVideoFormat;
private static RTPSession _ffmpegListener;
static async Task Main(string[] args)
{
string videoCodec = FFMPEG_DEFAULT_CODEC;
if (args?.Length > 0)
{
if (string.Equals(args[0], FFMPEG_VP8_CODEC, StringComparison.OrdinalIgnoreCase) ||
string.Equals(args[0], FFMPEG_VP9_CODEC, StringComparison.OrdinalIgnoreCase) ||
string.Equals(args[0], FFMPEG_H264_CODEC, StringComparison.OrdinalIgnoreCase) ||
string.Equals(args[0], FFMPEG_H265_CODEC, StringComparison.OrdinalIgnoreCase))
{
videoCodec = args[0].ToLower();
}
else
{
Console.WriteLine($"Video codec option not recognised. Valid values are {FFMPEG_VP8_CODEC}, {FFMPEG_VP9_CODEC} and {FFMPEG_H264_CODEC}. Using {videoCodec}.");
}
}
CancellationTokenSource exitCts = new CancellationTokenSource();
logger = AddConsoleLogger();
string ffmpegCommand = $"ffmpeg -re -f lavfi -i testsrc=size=640x480:rate=10 {videoCodec} -pix_fmt yuv420p -strict experimental -g 1 -ssrc {SSRC_REMOTE_VIDEO} -f rtp rtp://127.0.0.1:{FFMPEG_DEFAULT_RTP_PORT} -sdp_file {FFMPEG_SDP_FILE}";
// Start web socket.
Console.WriteLine("Starting web socket server...");
_webSocketServer = new WebSocketServer(IPAddress.Any, WEBSOCKET_PORT);
//_webSocketServer = new WebSocketServer(IPAddress.Any, WEBSOCKET_PORT, true);
//_webSocketServer.SslConfiguration.ServerCertificate = new X509Certificate2(LOCALHOST_CERTIFICATE_PATH);
//_webSocketServer.SslConfiguration.CheckCertificateRevocation = false;
//_webSocketServer.Log.Level = WebSocketSharp.LogLevel.Debug;
_webSocketServer.AddWebSocketService<WebRtcClient>("/", (client) =>
{
client.WebSocketOpened += SendOffer;
client.OnMessageReceived += WebSocketMessageReceived;
});
if (File.Exists(FFMPEG_SDP_FILE))
{
string codecName = GetCodecName();
if (!videoCodec.Contains(codecName))
{
logger.LogWarning($"Removing existing ffmpeg SDP file {FFMPEG_SDP_FILE} due to codec mismatch.");
File.Delete(FFMPEG_SDP_FILE);
}
}
Console.WriteLine("Start ffmpeg using the command below and then initiate a WebRTC connection from the browser");
Console.WriteLine(ffmpegCommand);
if (!File.Exists(FFMPEG_SDP_FILE))
{
Console.WriteLine();
Console.WriteLine($"Waiting for {FFMPEG_SDP_FILE} to appear...");
}
await Task.Run(() => StartFfmpegListener(FFMPEG_SDP_FILE, exitCts.Token));
Console.WriteLine($"ffmpeg listener successfully created on port {FFMPEG_DEFAULT_RTP_PORT} with video format {_ffmpegVideoFormat.Name()}.");
_webSocketServer.Start();
Console.WriteLine();
Console.WriteLine($"Waiting for browser web socket connection to {_webSocketServer.Address}:{_webSocketServer.Port}...");
// Wait for a signal saying the call failed, was cancelled with ctrl-c or completed.
await Task.Run(() => OnKeyPress(exitCts.Token));
_webSocketServer.Stop();
}
private static string GetCodecName()
{
var sdp = SDP.ParseSDPDescription(File.ReadAllText(FFMPEG_SDP_FILE));
var videoAnn = sdp.Media.Single(x => x.Media == SDPMediaTypesEnum.video);
var codec = videoAnn.MediaFormats.Values.First().Name().ToLower();
if (codec == "h265")
{
return "libx265";
}else
{
return codec;
}
}
private static async Task StartFfmpegListener(string sdpPath, CancellationToken cancel)
{
while (!File.Exists(FFMPEG_SDP_FILE) && !cancel.IsCancellationRequested)
{
await Task.Delay(500);
}
if (!cancel.IsCancellationRequested)
{
var sdp = SDP.ParseSDPDescription(File.ReadAllText(FFMPEG_SDP_FILE));
// The SDP is only expected to contain a single video media announcement.
var videoAnn = sdp.Media.Single(x => x.Media == SDPMediaTypesEnum.video);
_ffmpegVideoFormat = videoAnn.MediaFormats.Values.First();
_ffmpegListener = new RTPSession(false, false, false, IPAddress.Loopback, FFMPEG_DEFAULT_RTP_PORT);
_ffmpegListener.AcceptRtpFromAny = true;
MediaStreamTrack videoTrack = new MediaStreamTrack(SDPMediaTypesEnum.video, false, new List<SDPAudioVideoMediaFormat> { _ffmpegVideoFormat }, MediaStreamStatusEnum.RecvOnly);
videoTrack.Ssrc = SSRC_REMOTE_VIDEO; // /!\ Need to set the correct SSRC in order to accept RTP stream
_ffmpegListener.addTrack(videoTrack);
_ffmpegListener.SetRemoteDescription(SIP.App.SdpType.answer, sdp);
// Set a dummy destination end point or the RTP session will end up sending RTCP reports
// to itself.
var dummyIPEndPoint = new IPEndPoint(IPAddress.Loopback, 0);
_ffmpegListener.SetDestination(SDPMediaTypesEnum.video, dummyIPEndPoint, dummyIPEndPoint);
await _ffmpegListener.Start();
}
}
private static Task OnKeyPress(CancellationToken exit)
{
while (!exit.WaitHandle.WaitOne(0))
{
var keyProps = Console.ReadKey();
if (keyProps.KeyChar == 'q')
{
// Quit application.
Console.WriteLine("Quitting");
break;
}
}
return Task.CompletedTask;
}
private static async Task<RTCPeerConnection> SendOffer(WebSocketContext context)
{
logger.LogDebug($"Web socket client connection from {context.UserEndPoint}, sending offer.");
var pc = Createpc(context, _ffmpegVideoFormat);
var offerInit = pc.createOffer(null);
await pc.setLocalDescription(offerInit);
logger.LogDebug($"Sending SDP offer to client {context.UserEndPoint}.");
context.WebSocket.Send(offerInit.sdp);
return pc;
}
private static RTCPeerConnection Createpc(WebSocketContext context, SDPAudioVideoMediaFormat videoFormat)
{
var pc = new RTCPeerConnection(null);
MediaStreamTrack videoTrack = new MediaStreamTrack(SDPMediaTypesEnum.video, false, new List<SDPAudioVideoMediaFormat> { videoFormat }, MediaStreamStatusEnum.SendOnly);
pc.addTrack(videoTrack);
pc.onicecandidateerror += (candidate, error) => logger.LogWarning($"Error adding remote ICE candidate. {error} {candidate}");
pc.oniceconnectionstatechange += (state) => logger.LogDebug($"ICE connection state change to {state}.");
//pc.OnReceiveReport += (type, rtcp) => logger.LogDebug($"RTCP {type} report received.");
pc.OnRtcpBye += (reason) => logger.LogDebug($"RTCP BYE receive, reason: {(string.IsNullOrWhiteSpace(reason) ? "<none>" : reason)}.");
pc.OnRtpClosed += (reason) => logger.LogDebug($"Peer connection closed, reason: {(string.IsNullOrWhiteSpace(reason) ? "<none>" : reason)}.");
pc.onicecandidate += (candidate) =>
{
if (pc.signalingState == RTCSignalingState.have_local_offer ||
pc.signalingState == RTCSignalingState.have_remote_offer)
{
context.WebSocket.Send($"candidate:{candidate}");
}
};
pc.onconnectionstatechange += (state) =>
{
logger.LogDebug($"Peer connection state changed to {state}.");
if (state == RTCPeerConnectionState.connected)
{
logger.LogDebug("Creating RTP session to receive ffmpeg stream.");
_ffmpegListener.OnRtpPacketReceived += (ep, media, rtpPkt) =>
{
if (media == SDPMediaTypesEnum.video && pc.VideoDestinationEndPoint != null)
{
//logger.LogDebug($"Forwarding {media} RTP packet to webrtc peer timestamp {rtpPkt.Header.Timestamp}.");
pc.SendRtpRaw(media, rtpPkt.Payload, rtpPkt.Header.Timestamp, rtpPkt.Header.MarkerBit, rtpPkt.Header.PayloadType);
}
};
}
};
return pc;
}
private static void WebSocketMessageReceived(WebSocketContext context, RTCPeerConnection pc, string message)
{
try
{
if (pc.remoteDescription == null)
{
logger.LogDebug("Answer SDP: " + message);
pc.setRemoteDescription(new RTCSessionDescriptionInit { sdp = message, type = RTCSdpType.answer });
}
else
{
logger.LogDebug("ICE Candidate: " + message);
if (string.IsNullOrWhiteSpace(message) || message.AsSpan().Trim().Equals(SDP.END_ICE_CANDIDATES_ATTRIBUTE, StringComparison.OrdinalIgnoreCase))
{
logger.LogDebug("End of candidates message received.");
}
else
{
var candInit = Newtonsoft.Json.JsonConvert.DeserializeObject<RTCIceCandidateInit>(message);
pc.addIceCandidate(candInit);
}
}
}
catch (Exception excp)
{
logger.LogError("Exception WebSocketMessageReceived. " + excp.Message);
}
}
/// <summary>
/// Adds a console logger. Can be omitted if internal SIPSorcery debug and warning messages are not required.
/// </summary>
private static Microsoft.Extensions.Logging.ILogger AddConsoleLogger()
{
var serilogLogger = new LoggerConfiguration()
.Enrich.FromLogContext()
.MinimumLevel.Is(Serilog.Events.LogEventLevel.Debug)
.WriteTo.Console()
.CreateLogger();
var factory = new SerilogLoggerFactory(serilogLogger);
SIPSorcery.LogFactory.Set(factory);
return factory.CreateLogger<Program>();
}
}
}