示例#1
0
        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);
                                    };
                                }
                            }
                        }
                    }
                    //}
                }
            }
        }
示例#2
0
        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);
                            }
                        }
                    }
                }
            }
        }