-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathProgram.cs
311 lines (254 loc) · 13.4 KB
/
Program.cs
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
//-----------------------------------------------------------------------------
// Filename: Program.cs
//
// Description: An example WebRTC server application that streams the contents
// of a media file, such as an mp4, to a WebRTC enabled browser.
//
// Author(s):
// Aaron Clauson ([email protected])
// Christophe Irles ([email protected])
//
// History:
// 17 Sep 2020 Aaron Clauson Created, Dublin, Ireland.
// 27 Nov 2021 Christophe Irles Split Audio/Video, Add Camera support
//
// License:
// BSD 3-Clause "New" or "Revised" License, see included LICENSE.md file.
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Drawing;
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.Media;
using SIPSorcery.Net;
using SIPSorceryMedia.Abstractions;
using WebSocketSharp.Server;
namespace FFmpegFileAndDevicesTest
{
class Program
{
// /!\ TO DEFINE WHERE ffmpeg librairies are stored
private const string LIB_PATH = @"..\..\..\..\..\lib\x64"; // @"C:\ffmpeg-4.4.1-full_build-shared\bin";
private const int WEBSOCKET_PORT = 8081;
private const string STUN_URL = "stun:stun.sipsorcery.com";
private static Microsoft.Extensions.Logging.ILogger logger = NullLogger.Instance;
enum VIDEO_SOURCE
{
NONE,
FILE_OR_STREAM,
CAMERA,
SCREEN
}
enum AUDIO_SOURCE
{
NONE,
FILE_OR_STREAM,
MICROPHONE,
}
// /!\ Define some path/urls to some media files - to be set according your environment
static private string LOCAL_AUDIO_AND_VIDEO_FILE_MP4_BUNNY = @"C:\media\big_buck_bunny.mp4";
static private string LOCAL_AUDIO_AND_VIDEO_FILE_MP4_MAX = @"C:\media\max_intro.mp4";
static private string LOCAL_AUDIO_AND_VIDEO_FILE_WEBM = @"C:\media\Joy_and_Heron.webm";
static private string LOCAL_AUDIO_FILE_MP3 = @"C:\media\simplest_ffmpeg_audio_decoder_skycity1.mp3";
static private string LOCAL_AUDIO_FILE_WAV = @"C:\media\file_example_WAV_5MG.wav";
static private string DISTANT_AUDIO_AND_VIDEO_FILE_WEBM = @"https://upload.wikimedia.org/wikipedia/commons/3/36/Cosmos_Laundromat_-_First_Cycle_-_Official_Blender_Foundation_release.webm";
// Define variables according what you want to test
static private VIDEO_SOURCE VideoSourceType = VIDEO_SOURCE.FILE_OR_STREAM; // VIDEO_SOURCE.FILE_OR_STREAM;
static private AUDIO_SOURCE AudioSourceType = AUDIO_SOURCE.FILE_OR_STREAM;
static private VideoCodecsEnum VideoCodec = VideoCodecsEnum.H264; // or VideoCodecsEnum.VP8
static private AudioCodecsEnum AudioCodec = AudioCodecsEnum.PCMU;
static private String VideoSourceFile = DISTANT_AUDIO_AND_VIDEO_FILE_WEBM; // Used if VideoSource = VIDEO_SOURCE.FILE_OR_STREAM
static private String AudioSourceFile = DISTANT_AUDIO_AND_VIDEO_FILE_WEBM; // used if AudioSource = AUDIO_SOURCE.FILE_OR_STREAM;
static private string MicrophoneDevicePath = "audio=Microphone (HD Pro Webcam C920)"; // Specific info according end-user devices
static private string CameraDevicePath = "video=HD Pro Webcam C920"; // Specific info according end-user devices
static private bool RepeatVideoFile = true; // Used if VideoSource == VIDEO_SOURCE.FILE_OR_STREAM
static private bool RepeatAudioFile = true; // Used if AudioSource == AUDIO_SOURCE.FILE_OR_STREAM
static private RTCPeerConnection PeerConnection = null;
static private IAudioSink audioSink = null;
static private IVideoSource videoSource = null;
static private IAudioSource audioSource = null;
static void Main()
{
Console.WriteLine("WebRTC MP4 Source Demo");
logger = AddConsoleLogger();
// Initialise FFmpeg librairies
SIPSorceryMedia.FFmpeg.FFmpegInit.Initialise(SIPSorceryMedia.FFmpeg.FfmpegLogLevelEnum.AV_LOG_FATAL, LIB_PATH);
// Start web socket.
Console.WriteLine("Starting web socket server...");
var webSocketServer = new WebSocketServer(IPAddress.Any, WEBSOCKET_PORT);
webSocketServer.AddWebSocketService<WebRTCWebSocketPeer>("/", (peer) => peer.CreatePeerConnection = CreatePeerConnection);
webSocketServer.Start();
Console.WriteLine($"Waiting for web socket connections on {webSocketServer.Address}:{webSocketServer.Port}...");
Console.WriteLine("Press ctrl-c to exit.");
// Ctrl-c will gracefully exit the call at any point.
ManualResetEvent exitMe = new ManualResetEvent(false);
Console.CancelKeyPress += delegate (object sender, ConsoleCancelEventArgs e)
{
e.Cancel = true;
exitMe.Set();
};
// Wait for a signal saying the call failed, was cancelled with ctrl-c or completed.
exitMe.WaitOne();
}
static private Task<RTCPeerConnection> CreatePeerConnection()
{
RTCConfiguration config = new RTCConfiguration
{
iceServers = new List<RTCIceServer> { new RTCIceServer { urls = STUN_URL } }
};
PeerConnection = new RTCPeerConnection(config);
switch(VideoSourceType)
{
case VIDEO_SOURCE.FILE_OR_STREAM:
// Do we use same file for Audio ?
if ((AudioSourceType == AUDIO_SOURCE.FILE_OR_STREAM) && (AudioSourceFile == VideoSourceFile))
{
SIPSorceryMedia.FFmpeg.FFmpegFileSource fileSource = new SIPSorceryMedia.FFmpeg.FFmpegFileSource(VideoSourceFile, RepeatVideoFile, new AudioEncoder(), 960, true);
fileSource.OnAudioSourceError += (msg) => PeerConnection.Close(msg);
fileSource.OnVideoSourceError += (msg) => PeerConnection.Close(msg);
videoSource = fileSource as IVideoSource;
audioSource = fileSource as IAudioSource;
}
else
{
SIPSorceryMedia.FFmpeg.FFmpegFileSource fileSource = new SIPSorceryMedia.FFmpeg.FFmpegFileSource(VideoSourceFile, RepeatVideoFile, new AudioEncoder(), 960, true);
fileSource.OnVideoSourceError += (msg) => PeerConnection.Close(msg);
videoSource = fileSource as IVideoSource;
}
break;
case VIDEO_SOURCE.CAMERA:
List<SIPSorceryMedia.FFmpeg.Camera>? cameras = SIPSorceryMedia.FFmpeg.FFmpegCameraManager.GetCameraDevices();
SIPSorceryMedia.FFmpeg.Camera? camera = null;
if (cameras?.Count > 0 )
{
// Get last one
camera = cameras.Last();
}
if (camera != null)
{
videoSource = new SIPSorceryMedia.FFmpeg.FFmpegCameraSource(camera.Path);
videoSource.OnVideoSourceError += (msg) => PeerConnection.Close(msg);
}
else
throw new NotSupportedException($"Cannot find adequate camera ...");
break;
case VIDEO_SOURCE.SCREEN:
List<SIPSorceryMedia.FFmpeg.Monitor>? monitors = SIPSorceryMedia.FFmpeg.FFmpegMonitorManager.GetMonitorDevices();
SIPSorceryMedia.FFmpeg.Monitor? primaryMonitor = null;
if (monitors?.Count > 0)
{
foreach(SIPSorceryMedia.FFmpeg.Monitor monitor in monitors)
{
if (monitor.Primary)
{
primaryMonitor = monitor;
break;
}
}
if (primaryMonitor == null)
primaryMonitor = monitors[0];
}
if (primaryMonitor != null)
{
videoSource = new SIPSorceryMedia.FFmpeg.FFmpegScreenSource(primaryMonitor.Path, primaryMonitor.Rect, 10);
videoSource.OnVideoSourceError += (msg) => PeerConnection.Close(msg);
}
else
throw new NotSupportedException($"Cannot find adequate monitor ...");
break;
}
if(audioSource == null)
{
switch(AudioSourceType)
{
case AUDIO_SOURCE.FILE_OR_STREAM:
SIPSorceryMedia.FFmpeg.FFmpegFileSource fileSource = new SIPSorceryMedia.FFmpeg.FFmpegFileSource(AudioSourceFile, RepeatAudioFile, new AudioEncoder(), 960, false);
fileSource.OnAudioSourceError += (msg) => PeerConnection.Close(msg);
audioSource = fileSource as IAudioSource;
break;
case AUDIO_SOURCE.MICROPHONE:
audioSource = new SIPSorceryMedia.FFmpeg.FFmpegMicrophoneSource(MicrophoneDevicePath, new AudioEncoder());
break;
}
}
if(videoSource != null)
{
videoSource.RestrictFormats(x => x.Codec == VideoCodec);
MediaStreamTrack videoTrack = new MediaStreamTrack(videoSource.GetVideoSourceFormats(), MediaStreamStatusEnum.SendRecv);
PeerConnection.addTrack(videoTrack);
videoSource.OnVideoSourceEncodedSample += PeerConnection.SendVideo;
PeerConnection.OnVideoFormatsNegotiated += (videoFormats) => videoSource.SetVideoSourceFormat(videoFormats.First());
}
if(audioSource != null)
{
audioSource.RestrictFormats(x => x.Codec == AudioCodec);
MediaStreamTrack audioTrack = new MediaStreamTrack(audioSource.GetAudioSourceFormats(), MediaStreamStatusEnum.SendRecv);
PeerConnection.addTrack(audioTrack);
audioSource.OnAudioSourceEncodedSample += AudioSource_OnAudioSourceEncodedSample;
PeerConnection.OnAudioFormatsNegotiated += (audioFormats) => audioSource.SetAudioSourceFormat(audioFormats.First());
}
PeerConnection.onconnectionstatechange += async (state) =>
{
logger.LogDebug($"Peer connection state change to {state}.");
if (state == RTCPeerConnectionState.failed)
{
PeerConnection.Close("ice disconnection");
}
else if (state == RTCPeerConnectionState.closed)
{
if(videoSource != null)
await videoSource.CloseVideo();
if (audioSink != null)
await audioSink.CloseAudioSink();
if (audioSource != null)
await audioSource.CloseAudio();
}
else if (state == RTCPeerConnectionState.connected)
{
if (videoSource != null)
await videoSource.StartVideo();
if (audioSink != null)
{
await audioSink.StartAudioSink();
}
if (audioSource != null)
await audioSource.StartAudio();
}
};
// Diagnostics.
//pc.OnReceiveReport += (re, media, rr) => logger.LogDebug($"RTCP Receive for {media} from {re}\n{rr.GetDebugSummary()}");
//pc.OnSendReport += (media, sr) => logger.LogDebug($"RTCP Send for {media}\n{sr.GetDebugSummary()}");
//pc.GetRtpChannel().OnStunMessageReceived += (msg, ep, isRelay) => logger.LogDebug($"STUN {msg.Header.MessageType} received from {ep}.");
PeerConnection.oniceconnectionstatechange += (state) => logger.LogDebug($"ICE connection state change to {state}.");
return Task.FromResult(PeerConnection);
}
private static void AudioSource_OnAudioSourceEncodedSample(uint durationRtpUnits, byte[] sample)
{
PeerConnection.SendAudio(durationRtpUnits, sample);
if (audioSink != null)
audioSink.GotAudioRtp(null, 0, 0, 0, 0, false, sample);
}
/// <summary>
/// Adds a console logger. Can be omitted if internal SIPSorcery debug and warning messages are not required.
/// </summary>
static private Microsoft.Extensions.Logging.ILogger AddConsoleLogger()
{
var seriLogger = new LoggerConfiguration()
.Enrich.FromLogContext()
.MinimumLevel.Is(Serilog.Events.LogEventLevel.Debug)
.WriteTo.Console()
.CreateLogger();
var factory = new SerilogLoggerFactory(seriLogger);
SIPSorcery.LogFactory.Set(factory);
return factory.CreateLogger<Program>();
}
}
}