예제 #1
0
        private void WebRtcAnswerReceived(WebSocketSharp.Net.WebSockets.WebSocketContext context, string webSocketID, string sdpAnswer)
        {
            try
            {
                logger.LogDebug("Answer SDP: " + sdpAnswer);

                var answerSDP = SDP.ParseSDPDescription(sdpAnswer);

                var conn = _webRtcConnections.Where(x => x.Key == webSocketID).Select(x => x.Value).SingleOrDefault();

                if (conn.WebRtcSession == null)
                {
                    logger.LogWarning("No WebRTC client entry exists for web socket ID " + webSocketID + ", ignoring.");
                }
                else
                {
                    logger.LogDebug("New WebRTC client SDP answer for web socket ID " + webSocketID + ".");
                    conn.WebRtcSession.setRemoteDescription(SdpType.answer, answerSDP);
                }

                context.WebSocket.CloseAsync();
            }
            catch (Exception excp)
            {
                logger.LogError("Exception WebRtcAnswerReceived. " + excp.Message);
            }
        }
예제 #2
0
        private void WebRtcStartCall(WebSocketSharp.Net.WebSockets.WebSocketContext context, string webSocketID)
        {
            logger.Debug("New WebRTC client added for web socket connection " + webSocketID + ".");

            lock (_webRtcSessions)
            {
                if (!_webRtcSessions.Any(x => x.Key == webSocketID))
                {
                    var webRtcSession = new WebRtcSession(webSocketID);

                    if (_webRtcSessions.TryAdd(webSocketID, webRtcSession))
                    {
                        webRtcSession.Peer.OnSdpOfferReady += (sdp) => { logger.Debug("Offer SDP: " + sdp); context.WebSocket.Send(sdp); };
                        webRtcSession.Peer.OnDtlsPacket    += webRtcSession.DtlsPacketReceived;
                        webRtcSession.Peer.OnMediaPacket   += webRtcSession.MediaPacketReceived;
                        webRtcSession.Peer.Initialise(DTLS_CERTIFICATE_THUMBRPINT, null);
                        webRtcSession.Peer.OnClose += () => { PeerClosed(webSocketID); };
                    }
                    else
                    {
                        logger.Error("Failed to add new WebRTC client to sessions dictionary.");
                    }
                }
            }
        }
예제 #3
0
        private void WebRtcStartCall(WebSocketSharp.Net.WebSockets.WebSocketContext context, string webSocketID, IPAddress defaultIPAddress, bool isEncryptionDisabled, MediaSourceEnum mediaSource)
        {
            logger.Debug($"New WebRTC client added for web socket connection {webSocketID} and local IP address {defaultIPAddress}, encryption disabled {isEncryptionDisabled}.");

            var mediaTypes = new List <RtpMediaTypesEnum> {
                RtpMediaTypesEnum.Video, RtpMediaTypesEnum.Audio
            };

            _mfSampleGrabber.Start();  // Does nothing if media session is not paused.

            lock (_webRtcSessions)
            {
                if (!_webRtcSessions.Any(x => x.Key == webSocketID))
                {
                    var webRtcSession = new WebRtcSession(_dtlsCertificatePath, _dtlsKeyPath, webSocketID, isEncryptionDisabled, mediaSource);

                    string dtlsThumbrpint = (isEncryptionDisabled == false) ? _dtlsCertificateThumbprint : null;

                    if (_webRtcSessions.TryAdd(webSocketID, webRtcSession))
                    {
                        webRtcSession.Peer.OnSdpOfferReady += (sdp) => { logger.Debug("Offer SDP: " + sdp); context.WebSocket.Send(sdp); };
                        webRtcSession.Peer.OnMediaPacket   += webRtcSession.MediaPacketReceived;
                        webRtcSession.Peer.Initialise(dtlsThumbrpint, null, mediaTypes, defaultIPAddress, isEncryptionDisabled);
                        webRtcSession.Peer.OnClose += () => { PeerClosed(webSocketID); };

                        if (isEncryptionDisabled == false)
                        {
                            webRtcSession.Peer.OnDtlsPacket += webRtcSession.DtlsPacketReceived;
                        }
                        else
                        {
                            webRtcSession.Peer.OnIceConnected += webRtcSession.InitEncryptionDisabledSession;
                        }
                    }
                    else
                    {
                        logger.Error("Failed to add new WebRTC client.");
                    }
                }
            }
        }
예제 #4
0
        private async void WebRtcStartCall(WebSocketSharp.Net.WebSockets.WebSocketContext context, string webSocketID, MediaSourceEnum mediaSource)
        {
            logger.LogDebug($"New WebRTC client added for web socket connection {webSocketID}.");

            if (!_webRtcConnections.Any(x => x.Key == webSocketID))
            {
                var webRtcSession = new WebRtcSession(AddressFamily.InterNetwork, _dtlsCertificateThumbprint, null, null);

                webRtcSession.addTrack(SDPMediaTypesEnum.video, new List <SDPMediaFormat> {
                    new SDPMediaFormat(SDPMediaFormatsEnum.VP8)
                });

                // Don't need an audio track for the test pattern feed.
                if (mediaSource == MediaSourceEnum.Max)
                {
                    webRtcSession.addTrack(SDPMediaTypesEnum.audio, new List <SDPMediaFormat> {
                        new SDPMediaFormat(SDPMediaFormatsEnum.PCMU)
                    });
                }

                WebRtcConnection conn = new WebRtcConnection(webRtcSession);

                if (_webRtcConnections.TryAdd(webSocketID, conn))
                {
                    webRtcSession.OnClose += (reason) => PeerClosed(webSocketID, reason);
                    webRtcSession.RtpSession.OnRtcpBye += (reason) => PeerClosed(webSocketID, reason);

                    var offerSdp = await webRtcSession.createOffer();

                    webRtcSession.setLocalDescription(offerSdp);

                    logger.LogDebug($"Sending SDP offer to client {context.UserEndPoint}.");

                    context.WebSocket.Send(webRtcSession.SDP.ToString());

                    if (DoDtlsHandshake(webRtcSession))
                    {
                        if (mediaSource == MediaSourceEnum.Max)
                        {
                            OnMp4MediaSampleReady += conn.SendMedia;
                            if (!_isMp4Sampling)
                            {
                                _ = Task.Run(SampleMp4Media);
                            }
                        }
                        else if (mediaSource == MediaSourceEnum.TestPattern)
                        {
                            OnTestPatternSampleReady += conn.SendMedia;
                            if (!_isTestPatternSampling)
                            {
                                _ = Task.Run(SampleTestPattern);
                            }
                        }
                    }
                    else
                    {
                        PeerClosed(webSocketID, "dtls handshake failed");
                    }
                }
                else
                {
                    logger.LogError("Failed to add new WebRTC client.");
                }
            }
        }