Ejemplo n.º 1
0
        static async Task Main(string[] args)
        {
            string videoCodec = FFMPEG_DEFAULT_CODEC;

            if (args?.Length > 0)
            {
                switch (args[0].ToLower())
                {
                case FFMPEG_VP8_CODEC:
                case FFMPEG_VP9_CODEC:
                case FFMPEG_H264_CODEC:
                    videoCodec = args[0].ToLower();
                    break;

                default:
                    Console.WriteLine($"Video codec option not recognised. Valid values are {FFMPEG_VP8_CODEC}, {FFMPEG_VP9_CODEC} and {FFMPEG_H264_CODEC}. Using {videoCodec}.");
                    break;
                }
            }

            CancellationTokenSource exitCts = new CancellationTokenSource();

            logger = AddConsoleLogger();

            string ffmpegCommand = String.Format(FFMPEG_DEFAULT_COMMAND, videoCodec, FFMPEG_DEFAULT_RTP_PORT, 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))
            {
                var sdp      = SDP.ParseSDPDescription(File.ReadAllText(FFMPEG_SDP_FILE));
                var videoAnn = sdp.Media.Single(x => x.Media == SDPMediaTypesEnum.video);
                if (videoAnn.MediaFormats.Values.First().Name().ToLower() != videoCodec)
                {
                    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();
        }