Ejemplo n.º 1
0
        public void StartPeerConnection()
        {
            ManualResetEvent exitMre = new ManualResetEvent(false);

            Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e)
            {
                e.Cancel = true;
                exitMre.Set();
            };

            // Wait for a signal saying the call failed, was cancelled with ctrl-c or completed.
            exitMre.WaitOne();
            pc?.Close("Closed by host");
            camera?.StopVideo();
            controller?.Close();
        }
Ejemplo n.º 2
0
        private void AddActionsToRTCPeer()
        {
            pc.onicecandidate += (candidate) =>
            {
                signaling.Send("{\"data\":{\"candidate\":" + candidate.toJSON() + "}}");
            };

            pc.oniceconnectionstatechange += (ice) =>
            {
                if (pc.iceConnectionState == RTCIceConnectionState.failed)
                {
                    pc.restartIce();
                }
            };

            pc.onnegotiationneeded += Negotiate;

            pc.onconnectionstatechange += (state) =>
            {
                logger.LogDebug($"Peer connection state change to {state}.");

                if (state == RTCPeerConnectionState.connected)
                {
                    logger.LogDebug($"Start streaming on peer");
                    camera.StartVideo();
                }
                else if (state == RTCPeerConnectionState.failed)
                {
                    camera.StopVideo();
                    RestartPeer();
                }
                else if (state == RTCPeerConnectionState.disconnected)
                {
                    camera.StopVideo();

                    RestartPeer();
                }
                else if (state == RTCPeerConnectionState.connecting)
                {
                    logger.LogDebug("Peer conneting");
                }
                else if (state == RTCPeerConnectionState.@new)
                {
                    logger.LogDebug("Peer new");
                }
                else
                {
                    logger.LogDebug("Peer closed");
                }
            };

            pc.OnTimeout += (mess) =>
            {
                Console.WriteLine($"Time end {mess}");
            };

            pc.createDataChannel("f", null);
            pc.createDataChannel("c", null);
            pc.ondatachannel += (datachannel) =>
            {
                if (datachannel.label != "CommandChannel")
                {
                    if (isFileStreamCreated == false)
                    {
                        FirmwareFile               = new FileStream(datachannel.label, FileMode.Append);
                        isFileStreamCreated        = true;
                        datachannel.onmessage     += EOFMessageHandle;
                        datachannel.onDatamessage += WriteFile;
                    }
                }
                else
                {
                    // Maybe onDataMessage should be checked
                    datachannel.onmessage += (string message) =>
                    {
                        try
                        {
                            datachannel.send(Convert.ToInt32(controller.SendCTP_Command(JsonSerializer.Deserialize <CTP_packet>(message))).ToString());
                        }
                        catch (Exception ex) {
                            logger.LogDebug($"Exception {ex.GetType()} reason/message: {ex.Message} ");
                        }
                    };
                }
            };
            // 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}.");
            pc.oniceconnectionstatechange += (state) => logger.LogDebug($"ICE connection state change to {state}.");
        }