Exemple #1
0
        private static async void MessageReceived(WebSocketContext context, string msg)
        {
            //Console.WriteLine($"websocket recv: {msg}");
            var offerSDP = SDP.ParseSDPDescription(msg);

            Console.WriteLine($"offer sdp: {offerSDP}");

            var webRtcSession = new WebRtcSession(
                AddressFamily.InterNetwork,
                DTLS_CERTIFICATE_FINGERPRINT,
                null,
                null);

            webRtcSession.setRemoteDescription(new RTCSessionDescription {
                sdp = offerSDP, type = RTCSdpType.offer
            });

            webRtcSession.OnReceiveReport     += RtpSession_OnReceiveReport;
            webRtcSession.OnSendReport        += RtpSession_OnSendReport;
            webRtcSession.OnRtpPacketReceived += RtpSession_OnRtpPacketReceived;
            webRtcSession.OnClose             += (reason) =>
            {
                Console.WriteLine($"webrtc session closed: {reason}");
                _webRtcSessions.Remove(webRtcSession);
            };

            // Add local recvonly tracks. This ensures that the SDP answer includes only
            // the codecs we support.
            MediaStreamTrack audioTrack = new MediaStreamTrack(null, SDPMediaTypesEnum.audio, false, new List <SDPMediaFormat> {
                new SDPMediaFormat(SDPMediaFormatsEnum.PCMU)
            });

            audioTrack.Transceiver.SetStreamStatus(MediaStreamStatusEnum.RecvOnly);
            webRtcSession.addTrack(audioTrack);
            MediaStreamTrack videoTrack = new MediaStreamTrack(null, SDPMediaTypesEnum.video, false, new List <SDPMediaFormat> {
                new SDPMediaFormat(SDPMediaFormatsEnum.VP8)
            });

            videoTrack.Transceiver.SetStreamStatus(MediaStreamStatusEnum.RecvOnly);
            webRtcSession.addTrack(videoTrack);

            var answerSdp = await webRtcSession.createAnswer();

            webRtcSession.setLocalDescription(new RTCSessionDescription {
                sdp = answerSdp, type = RTCSdpType.answer
            });

            Console.WriteLine($"answer sdp: {answerSdp}");

            context.WebSocket.Send(answerSdp.ToString());

            if (DoDtlsHandshake(webRtcSession))
            {
                _webRtcSessions.Add(webRtcSession);
            }
            else
            {
                webRtcSession.Close("dtls handshake failed.");
            }
        }
Exemple #2
0
        private static async Task <WebRtcSession> SendSDPOffer(WebSocketContext context)
        {
            logger.LogDebug($"Web socket client connection from {context.UserEndPoint}.");

            var webRtcSession = new WebRtcSession(
                AddressFamily.InterNetwork,
                DTLS_CERTIFICATE_FINGERPRINT,
                null,
                null);

            MediaStreamTrack audioTrack = new MediaStreamTrack(null, SDPMediaTypesEnum.audio, false, new List <SDPMediaFormat> {
                new SDPMediaFormat(SDPMediaFormatsEnum.PCMU)
            });

            webRtcSession.addTrack(audioTrack);
            MediaStreamTrack videoTrack = new MediaStreamTrack(null, SDPMediaTypesEnum.video, false, new List <SDPMediaFormat> {
                new SDPMediaFormat(SDPMediaFormatsEnum.VP8)
            });

            webRtcSession.addTrack(videoTrack);

            //webRtcSession.RtpSession.OnReceiveReport += RtpSession_OnReceiveReport;
            webRtcSession.OnSendReport += RtpSession_OnSendReport;
            OnMediaSampleReady         += webRtcSession.SendMedia;

            webRtcSession.OnClose += (reason) =>
            {
                logger.LogDebug($"WebRtcSession was closed with reason {reason}");
                OnMediaSampleReady            -= webRtcSession.SendMedia;
                webRtcSession.OnReceiveReport -= RtpSession_OnReceiveReport;
                webRtcSession.OnSendReport    -= RtpSession_OnSendReport;
            };

            var offerSdp = await webRtcSession.createOffer(null);

            webRtcSession.setLocalDescription(new RTCSessionDescription {
                sdp = offerSdp, type = RTCSdpType.offer
            });

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

            context.WebSocket.Send(offerSdp.ToString());

            if (DoDtlsHandshake(webRtcSession))
            {
                if (!_isSampling)
                {
                    _ = Task.Run(StartMedia);
                }
            }
            else
            {
                webRtcSession.Close("dtls handshake failed.");
            }

            return(webRtcSession);
        }
Exemple #3
0
        private static WebRtcSession SendSDPOffer(WebSocketContext context)
        {
            Log.LogDebug($"Web socket client connection from {context.UserEndPoint}.");

            _webRtcSession = new WebRtcSession(
                AddressFamily.InterNetwork,
                DTLS_CERTIFICATE_FINGERPRINT,
                null,
                null);

            _webRtcSession.OnReceiveReport += RtpSession_OnReceiveReport;
            _webRtcSession.OnSendReport    += RtpSession_OnSendReport;

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

            _webRtcSession.OnClose += (reason) =>
            {
                Log.LogDebug($"WebRtcSession was closed with reason {reason}");
                _webRtcSession.OnReceiveReport -= RtpSession_OnReceiveReport;
                _webRtcSession.OnSendReport    -= RtpSession_OnSendReport;
            };

            MediaStreamTrack audioTrack = new MediaStreamTrack(null, SDPMediaTypesEnum.audio, false, new List <SDPMediaFormat> {
                new SDPMediaFormat(SDPMediaFormatsEnum.PCMU)
            });

            _webRtcSession.addTrack(audioTrack);

            var offer = _webRtcSession.createOffer(null).Result;

            _webRtcSession.setLocalDescription(new RTCSessionDescription {
                type = RTCSdpType.offer, sdp = offer
            });

            context.WebSocket.Send(offer.ToString());

            if (DoDtlsHandshake(_webRtcSession))
            {
                Log.LogInformation("DTLS handshake completed successfully.");
            }
            else
            {
                _webRtcSession.Close("dtls handshake failed.");
            }

            return(_webRtcSession);
        }
Exemple #4
0
        private static async Task <WebRtcSession> SendSDPOffer(WebSocketContext context)
        {
            logger.LogDebug($"Web socket client connection from {context.UserEndPoint}.");

            var webRtcSession = new WebRtcSession(
                AddressFamily.InterNetwork,
                DTLS_CERTIFICATE_FINGERPRINT,
                null,
                null);

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

            webRtcSession.RtpSession.OnReceiveReport += RtpSession_OnReceiveReport;
            webRtcSession.RtpSession.OnSendReport    += RtpSession_OnSendReport;

            webRtcSession.OnClose += (reason) =>
            {
                logger.LogDebug($"WebRtcSession was closed with reason {reason}");
                OnTestPatternSampleReady -= webRtcSession.SendMedia;
                webRtcSession.RtpSession.OnReceiveReport -= RtpSession_OnReceiveReport;
                webRtcSession.RtpSession.OnSendReport    -= RtpSession_OnSendReport;
            };

            var offerSdp = await webRtcSession.createOffer();

            webRtcSession.setLocalDescription(offerSdp);

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

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

            if (DoDtlsHandshake(webRtcSession))
            {
                OnTestPatternSampleReady += webRtcSession.SendMedia;
            }
            else
            {
                webRtcSession.Close("dtls handshake failed.");
            }

            return(webRtcSession);
        }