private void InitializeAndRunStream(object state) { var sessionParams = (WebRtcSessionParams)state; var session = sessionParams.Session; ManagedConductor.InitializeSSL(); using (session.WebRtc) { session.WebRtc.AddServerConfig("stun:stun.l.google.com:19302", string.Empty, string.Empty); session.WebRtc.AddServerConfig("stun:stun.anyfirewall.com:3478", string.Empty, string.Empty); session.WebRtc.AddServerConfig("stun:stun.stunprotocol.org:3478", string.Empty, string.Empty); //session.WebRtc.AddServerConfig("turn:192.168.0.100:3478", "test", "test"); session.WebRtc.SetAudio(sessionParams.EnableAudio); session.WebRtc.SetVideoCapturer(sessionParams.ScreenWidth, sessionParams.ScreenHeight, sessionParams.CaptureFps, sessionParams.BarCodeScreen); var ok = session.WebRtc.InitializePeerConnection(); if (ok) { _logger.Info($"WebRTC peer connection initialized successfully ({GetClient(sessionParams.Socket)})"); sessionParams.WaitEvent.Set(); // javascript side makes the offer in this demo //session.WebRtc.CreateDataChannel("msgDataChannel"); while (!session.Cancellation.Token.IsCancellationRequested && session.WebRtc.ProcessMessages(1000)) { } session.WebRtc.ProcessMessages(1000); } else { _logger.Warn($"WebRTC peer connection failed ({GetClient(sessionParams.Socket)})"); sessionParams.Socket.Close(); } } }
private void OnReceive(IWebSocketConnection context, string msg) { Debug.WriteLine($"OnReceive {context.ConnectionInfo.Id}: {msg}"); if (!msg.Contains("command")) { return; } if (UserList.ContainsKey(context.ConnectionInfo.Id)) { JsonData msgJson = JsonMapper.ToObject(msg); string command = msgJson["command"].ToString(); switch (command) { case offer: { if (UserList.Count <= ClientLimit && !Streams.ContainsKey(context.ConnectionInfo.Id)) { var session = Streams[context.ConnectionInfo.Id] = new WebRtcSession(); { using (var go = new ManualResetEvent(false)) { var t = Task.Factory.StartNew(() => { ManagedConductor.InitializeSSL(); using (session.WebRtc) { session.WebRtc.AddServerConfig("stun:stun.l.google.com:19302", string.Empty, string.Empty); session.WebRtc.AddServerConfig("stun:stun.anyfirewall.com:3478", string.Empty, string.Empty); session.WebRtc.AddServerConfig("stun:stun.stunprotocol.org:3478", string.Empty, string.Empty); //session.WebRtc.AddServerConfig("turn:192.168.0.100:3478", "test", "test"); session.WebRtc.SetAudio(false); //if (!Form.checkBoxVirtualCam.Checked) //{ // if (!string.IsNullOrEmpty(Form.videoDevice)) // { // var vok = session.WebRtc.OpenVideoCaptureDevice(Form.videoDevice); // Trace.WriteLine($"OpenVideoCaptureDevice: {vok}, {Form.videoDevice}"); // } //} //else { session.WebRtc.SetVideoCapturer(FrameSize.Width, FrameSize.Height, 25, false); } var ok = session.WebRtc.InitializePeerConnection(); if (ok) { go.Set(); // javascript side makes the offer in this demo //session.WebRtc.CreateDataChannel("msgDataChannel"); while (!session.Cancel.Token.IsCancellationRequested && session.WebRtc.ProcessMessages(1000)) { Debug.Write("."); } session.WebRtc.ProcessMessages(1000); } else { Debug.WriteLine("InitializePeerConnection failed"); context.Close(); } } }, session.Cancel.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default); if (go.WaitOne(9999)) { session.WebRtc.OnIceCandidate += delegate(string sdp_mid, int sdp_mline_index, string sdp) { if (context.IsAvailable) { JsonData j = new JsonData(); j["command"] = "OnIceCandidate"; j["sdp_mid"] = sdp_mid; j["sdp_mline_index"] = sdp_mline_index; j["sdp"] = sdp; context.Send(j.ToJson()); } }; session.WebRtc.OnSuccessAnswer += delegate(string sdp) { if (context.IsAvailable) { JsonData j = new JsonData(); j["command"] = "OnSuccessAnswer"; j["sdp"] = sdp; context.Send(j.ToJson()); } }; session.WebRtc.OnFailure += delegate(string error) { Trace.WriteLine($"OnFailure: {error}"); }; session.WebRtc.OnError += delegate { Trace.WriteLine("OnError"); }; session.WebRtc.OnDataMessage += delegate(string dmsg) { Trace.WriteLine($"OnDataMessage: {dmsg}"); }; session.WebRtc.OnDataBinaryMessage += delegate(byte[] dmsg) { Trace.WriteLine($"OnDataBinaryMessage: {dmsg.Length}"); }; unsafe { session.WebRtc.OnRenderRemote += delegate(byte *frame_buffer, uint w, uint h) { OnRenderFrame(frame_buffer, w, h); }; } var d = msgJson["desc"]; var s = d["sdp"].ToString(); session.WebRtc.OnOfferRequest(s); } } } } } break; case onicecandidate: { var c = msgJson["candidate"]; var sdpMLineIndex = (int)c["sdpMLineIndex"]; var sdpMid = c["sdpMid"].ToString(); var candidate = c["candidate"].ToString(); var session = Streams[context.ConnectionInfo.Id]; { session.WebRtc.AddIceCandidate(sdpMid, sdpMLineIndex, candidate); } } break; } } }
private void PublishRemoteFeed(bool publishAudio) { { if (true) { session = new WebRtcSession(); { using (var go = new ManualResetEvent(false)) { var t = Task.Factory.StartNew(() => { ManagedConductor.InitializeSSL(); using (session.WebRtc) { session.WebRtc.AddServerConfig("stun:stun.l.google.com:19302", string.Empty, string.Empty); session.WebRtc.AddServerConfig("stun:stun.anyfirewall.com:3478", string.Empty, string.Empty); session.WebRtc.AddServerConfig("stun:stun.stunprotocol.org:3478", string.Empty, string.Empty); // session.WebRtc.AddServerConfig("turn:192.168.0.100:3478", "test", "test"); session.WebRtc.SetAudio(publishAudio); //if (!Form.checkBoxVirtualCam.Checked) { if (!string.IsNullOrEmpty(Form.videoDevice)) { var vok = session.WebRtc.OpenVideoCaptureDevice(Form.videoDevice); Trace.WriteLine($"OpenVideoCaptureDevice: {vok}, {Form.videoDevice}"); } if (!string.IsNullOrEmpty(Form.videoDevice_2)) { var vok_2 = session.WebRtc.OpenVideoCaptureDevice(Form.videoDevice_2); Trace.WriteLine($"OpenVideoCaptureDevice: {vok_2}, {Form.videoDevice_2}"); } } var ok = session.WebRtc.InitializePeerConnection(); if (ok) { session.WebRtc.OnSuccessAnswer += async delegate(string sdp) { var result = await client.CreateAnswer(false, false); if (result != null && (result.janus.Equals(Commands.SuccessCommand) || result.janus.Equals(Commands.AckCommand))) { } }; go.Set(); while (!session.Cancel.Token.IsCancellationRequested && session.WebRtc.ProcessMessages(1000)) { Debug.Write("."); } session.WebRtc.ProcessMessages(1000); } else { Debug.WriteLine("InitializePeerConnection failed"); //context.Close(); } } }, session.Cancel.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default); if (go.WaitOne(9999)) { session.WebRtc.OnIceCandidate += async delegate(string sdp_mid, int sdp_mline_index, string sdp) { //var result = await client.PollingEventMessage(); //if(result != null && result.janus.Equals(Commands.ErrorCommand)) // todo: check session //{ // InitAndAttach(); await client.SendCandidate(sdp_mid, sdp_mline_index, sdp); //} }; session.WebRtc.OnSuccessAnswer += async delegate(string sdp) { Debug.WriteLine("OnSuccessAnswer failed"); }; session.WebRtc.OnFailure += delegate(string error) { Trace.WriteLine($"OnFailure: {error}"); }; session.WebRtc.OnError += delegate { Trace.WriteLine("OnError"); }; session.WebRtc.OnDataMessage += delegate(string dmsg) { Trace.WriteLine($"OnDataMessage: {dmsg}"); }; session.WebRtc.OnDataBinaryMessage += delegate(byte[] dmsg) { Trace.WriteLine($"OnDataBinaryMessage: {dmsg.Length}"); }; unsafe { session.WebRtc.OnRenderRemote += delegate(byte *frame_buffer, uint w, uint h) { OnRenderRemote(frame_buffer, w, h); }; session.WebRtc.OnRenderLocal += delegate(byte *frame_buffer, uint w, uint h) { OnRenderLocal(frame_buffer, w, h); }; } } } } //} } } }
private void PublishOwnFeed(bool publishAudio) { { if (true) { session = new WebRtcSession(); { using (var go = new ManualResetEvent(false)) { var t = Task.Factory.StartNew(() => { ManagedConductor.InitializeSSL(); using (session.WebRtc) { session.WebRtc.AddServerConfig("stun:stun.l.google.com:19302", string.Empty, string.Empty); session.WebRtc.AddServerConfig("stun:stun.anyfirewall.com:3478", string.Empty, string.Empty); session.WebRtc.AddServerConfig("stun:stun.stunprotocol.org:3478", string.Empty, string.Empty); // session.WebRtc.AddServerConfig("turn:192.168.0.100:3478", "test", "test"); session.WebRtc.SetAudio(publishAudio); //if (!Form.checkBoxVirtualCam.Checked) { if (!string.IsNullOrEmpty(Form.videoDevice)) { var vok = session.WebRtc.OpenVideoCaptureDevice(Form.videoDevice); Trace.WriteLine($"OpenVideoCaptureDevice: {vok}, {Form.videoDevice}"); } if (!string.IsNullOrEmpty(Form.videoDevice_2)) { var vok_2 = session.WebRtc.OpenVideoCaptureDevice(Form.videoDevice_2); Trace.WriteLine($"OpenVideoCaptureDevice: {vok_2}, {Form.videoDevice_2}"); } } var ok = session.WebRtc.InitializePeerConnection(); if (ok) { session.WebRtc.OnSuccessOffer += async delegate(string sdp) { // string deviceid = null; if (Form.Devices != null) { foreach (string deviceid in Form.Devices) { Console.WriteLine(deviceid); audioDevice audiodevice = new audioDevice(true); videoDevice videodevice = new videoDevice(true, deviceid); var result = await client.CreateOffer(sdp, audiodevice, videodevice); if (result != null && (result.janus.Equals(Commands.SuccessCommand) || result.janus.Equals(Commands.AckCommand))) { // continue; Console.WriteLine("Offer send successfully"); } else if (result.janus.Equals(Commands.ErrorCommand)) { // InitAndAttach(); Console.WriteLine("Reconnecting again"); } /* * media_1 = new Media(true, true, deviceId, "123", "232"); * var Result = await client.CreateOffer(sdp, media_1); * * if (Result != null && (Result.janus.Equals(Commands.SuccessCommand) || Result.janus.Equals(Commands.AckCommand))) * { * Console.WriteLine("Offer send successfully"); * } * else if (Result.janus.Equals(Commands.ErrorCommand)) * { * InitAndAttach(); * Console.WriteLine("Reconnecting again"); * } */ } } else if (Form.Devices == null) { return; } }; session.WebRtc.CreateOffer(); go.Set(); // javascript side makes the offer in this demo // session.WebRtc.CreateDataChannel("msgDataChannel"); while (!session.Cancel.Token.IsCancellationRequested && session.WebRtc.ProcessMessages(1000)) { Debug.Write("."); } session.WebRtc.ProcessMessages(1000); } else { Debug.WriteLine("InitializePeerConnection failed"); //context.Close(); } } }, session.Cancel.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default); if (go.WaitOne(9999)) { session.WebRtc.OnIceCandidate += async delegate(string sdp_mid, int sdp_mline_index, string sdp) { // todo: check session await client.SendCandidate(sdp_mid, sdp_mline_index, sdp); var result = await client.PollingEventMessage(); if (result != null && result.janus.Equals(Commands.ErrorCommand)) { InitAndAttach(); } }; session.WebRtc.OnSuccessAnswer += async delegate(string sdp) { Debug.WriteLine("OnSuccessAnswer failed"); }; session.WebRtc.OnFailure += delegate(string error) { Trace.WriteLine($"OnFailure: {error}"); }; session.WebRtc.OnError += delegate { Trace.WriteLine("OnError"); }; session.WebRtc.OnDataMessage += delegate(string dmsg) { Trace.WriteLine($"OnDataMessage: {dmsg}"); }; session.WebRtc.OnDataBinaryMessage += delegate(byte[] dmsg) { Trace.WriteLine($"OnDataBinaryMessage: {dmsg.Length}"); }; unsafe { session.WebRtc.OnRenderRemote += delegate(byte *frame_buffer, uint w, uint h) { OnRenderRemote(frame_buffer, w, h); }; session.WebRtc.OnRenderLocal += delegate(byte *frame_buffer, uint w, uint h) { OnRenderLocal(frame_buffer, w, h); }; } } } } //} } } }
private void OnReceive(IWebSocketConnection context, string msg) { Debug.WriteLine($"OnReceive {context.ConnectionInfo.Id}: {msg}"); if (!msg.Contains("command")) { return; } if (UserList.ContainsKey(context.ConnectionInfo.Id)) { JsonData msgJson = JsonMapper.ToObject(msg); string command = msgJson["command"].ToString(); switch (command) { case offer: { if (UserList.Count <= ClientLimit && !Streams.ContainsKey(context.ConnectionInfo.Id)) { var session = Streams[context.ConnectionInfo.Id] = new WebRtcSession(); { using (var go = new ManualResetEvent(false)) { var t = Task.Factory.StartNew(() => { ManagedConductor.InitializeSSL(); using (session.WebRtc) { session.WebRtc.AddServerConfig("stun:stun.l.google.com:19302", string.Empty, string.Empty); session.WebRtc.AddServerConfig("stun:stun.anyfirewall.com:3478", string.Empty, string.Empty); session.WebRtc.AddServerConfig("stun:stun.stunprotocol.org:3478", string.Empty, string.Empty); var ok = session.WebRtc.InitializePeerConnection(); if (ok) { go.Set(); while (!session.Cancel.Token.IsCancellationRequested && session.WebRtc.ProcessMessages(1000)) { Debug.Write("."); } session.WebRtc.ProcessMessages(1000); } else { Debug.WriteLine("InitializePeerConnection failed"); context.Close(); } } ManagedConductor.CleanupSSL(); }, session.Cancel.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default); if (go.WaitOne(9999)) { session.WebRtc.OnIceCandidate += delegate(string sdp_mid, int sdp_mline_index, string sdp) { if (context.IsAvailable) { JsonData j = new JsonData(); j["command"] = "OnIceCandidate"; j["sdp_mid"] = sdp_mid; j["sdp_mline_index"] = sdp_mline_index; j["sdp"] = sdp; context.Send(j.ToJson()); } }; session.WebRtc.OnSuccessAnswer += delegate(string sdp) { if (context.IsAvailable) { JsonData j = new JsonData(); j["command"] = "OnSuccessAnswer"; j["sdp"] = sdp; context.Send(j.ToJson()); } }; session.WebRtc.OnFailure += delegate(string error) { Trace.WriteLine($"OnFailure: {error}"); }; session.WebRtc.OnError += delegate { Trace.WriteLine("OnError"); }; unsafe { session.WebRtc.OnFillBuffer += delegate(byte *frame_buffer, long yuvSize) { OnFillBuffer(frame_buffer, yuvSize); }; } var d = msgJson["desc"]; var s = d["sdp"].ToString(); session.WebRtc.OnOfferRequest(s); } } } } } break; case onicecandidate: { var c = msgJson["candidate"]; var sdpMLineIndex = (int)c["sdpMLineIndex"]; var sdpMid = c["sdpMid"].ToString(); var candidate = c["candidate"].ToString(); var session = Streams[context.ConnectionInfo.Id]; { session.WebRtc.AddIceCandidate(sdpMid, sdpMLineIndex, candidate); } } break; } } }
private void PublishOwnFeed(bool publishAudio) { { if (true) { session = new WebRtcSession(); { using (var go = new ManualResetEvent(false)) { var t = Task.Factory.StartNew(() => { ManagedConductor.InitializeSSL(); using (session.WebRtc) { session.WebRtc.AddServerConfig("stun:stun.l.google.com:19302", string.Empty, string.Empty); session.WebRtc.AddServerConfig("stun:stun.anyfirewall.com:3478", string.Empty, string.Empty); session.WebRtc.AddServerConfig("stun:stun.stunprotocol.org:3478", string.Empty, string.Empty); //session.WebRtc.AddServerConfig("turn:192.168.0.100:3478", "test", "test"); session.WebRtc.SetAudio(publishAudio); //if (!Form.checkBoxVirtualCam.Checked) { if (!string.IsNullOrEmpty(Form.videoDevice)) { var vok = session.WebRtc.OpenVideoCaptureDevice(Form.videoDevice); Trace.WriteLine($"OpenVideoCaptureDevice: {vok}, {Form.videoDevice}"); } } //else //{ // session.WebRtc.SetVideoCapturer(MainForm.screenWidth, // MainForm.screenHeight, // MainForm.captureFps, // MainForm.barCodeScreen); //} var ok = session.WebRtc.InitializePeerConnection(); if (ok) { session.WebRtc.OnSuccessOffer += async delegate(string sdp) { var result = await client.CreateOffer(sdp, true, true); if (result != null && (result.janus.Equals(Commands.SuccessCommand) || result.janus.Equals(Commands.AckCommand))) { //result = await client.CreateOffer(sdp, true, true); } }; session.WebRtc.CreateOffer(); go.Set(); // javascript side makes the offer in this demo //session.WebRtc.CreateDataChannel("msgDataChannel"); while (!session.Cancel.Token.IsCancellationRequested && session.WebRtc.ProcessMessages(1000)) { Debug.Write("."); } session.WebRtc.ProcessMessages(1000); } else { Debug.WriteLine("InitializePeerConnection failed"); //context.Close(); } } }, session.Cancel.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default); if (go.WaitOne(9999)) { session.WebRtc.OnIceCandidate += async delegate(string sdp_mid, int sdp_mline_index, string sdp) { //if (context.IsAvailable) // todo: check session { //JsonData j = new JsonData(); //j["command"] = "OnIceCandidate"; //j["sdp_mid"] = sdp_mid; //j["sdp_mline_index"] = sdp_mline_index; //j["sdp"] = sdp; await client.SendCandidate(sdp_mid, sdp_mline_index, sdp); //context.Send(j.ToJson()); } }; session.WebRtc.OnSuccessAnswer += delegate(string sdp) { Debug.WriteLine("OnSuccessAnswer failed"); //if (context.IsAvailable) //{ // JsonData j = new JsonData(); // j["command"] = "OnSuccessAnswer"; // j["sdp"] = sdp; // context.Send(j.ToJson()); //} }; session.WebRtc.OnFailure += delegate(string error) { Trace.WriteLine($"OnFailure: {error}"); }; session.WebRtc.OnError += delegate { Trace.WriteLine("OnError"); }; session.WebRtc.OnDataMessage += delegate(string dmsg) { Trace.WriteLine($"OnDataMessage: {dmsg}"); }; session.WebRtc.OnDataBinaryMessage += delegate(byte[] dmsg) { Trace.WriteLine($"OnDataBinaryMessage: {dmsg.Length}"); }; unsafe { session.WebRtc.OnRenderRemote += delegate(byte *frame_buffer, uint w, uint h) { OnRenderRemote(frame_buffer, w, h); }; session.WebRtc.OnRenderLocal += delegate(byte *frame_buffer, uint w, uint h) { OnRenderLocal(frame_buffer, w, h); }; } //var d = msgJson["desc"]; //var s = d["sdp"].ToString(); //session.WebRtc.OnOfferRequest(s); } } } } } }
private Task InitConnection() { TaskCompletionSource <bool> completion = new TaskCompletionSource <bool>(); _conductorThread = new Thread(() => { Thread.Sleep(5); //Wait for function to return at least! try { _conductor = new ManagedConductor(); ManagedConductor.InitializeSSL(); foreach (var server in IceServers) { _conductor.AddServerConfig(server.Url, server.UserName, server.Credentials); } _conductor.AddServerConfig("stun:stun1.l.google.com:19302", String.Empty, String.Empty); _conductor.AddServerConfig("stun:stun2.l.google.com:19302", String.Empty, String.Empty); _conductor.SetAudio(false); _conductor.SetVideoCapturer(640, 480, 5, false); if (!_conductor.InitializePeerConnection()) { completion.SetException(new ResonanceWebRTCConnectionFailedException(new Exception("Error initializing peer connection."))); return; } _conductor.CreateDataChannel("resonance"); _conductor.OnIceCandidate += _conductor_OnIceCandidate; _conductor.OnDataBinaryMessage += _conductor_OnDataBinaryMessage; _conductor.OnError += _conductor_OnError; _conductor.OnFailure += _conductor_OnFailure; _conductor.OnIceStateChanged += _conductor_OnIceStateChanged; _conductor.ProcessMessages(1000); } catch (Exception ex) { completion.SetException(ex); return; } _conductorInitialized = true; completion.SetResult(true); while (!_closeConnection) { _conductor.ProcessMessages(1000); Thread.Sleep(10); } _conductor.OnIceCandidate -= _conductor_OnIceCandidate; _conductor.OnDataBinaryMessage -= _conductor_OnDataBinaryMessage; _conductor.OnError -= _conductor_OnError; _conductor.OnFailure -= _conductor_OnFailure; _conductor.OnIceStateChanged -= _conductor_OnIceStateChanged; try { _conductor.Dispose(); } catch { } }); _conductorThread.SetApartmentState(ApartmentState.STA); _conductorThread.IsBackground = true; _conductorThread.Start(); return(completion.Task); }