コード例 #1
0
        private async Task InitializeORTC()
        {
            var gatherOptions = new RTCIceGatherOptions()
            {
                IceServers = new List <RTCIceServer>()
                {
                    new RTCIceServer {
                        Urls = new string[] { "stun.l.google.com:19302" }
                    },
                    new RTCIceServer {
                        Username = "******", Credential = "redmond123", CredentialType = RTCIceGathererCredentialType.Password, Urls = new string[] { "turn:turn-testdrive.cloudapp.net:3478?transport=udp" }
                    }
                }
            };

            _gatherer = new RTCIceGatherer(gatherOptions);
            _gatherer.OnStateChange += IceGatherer_OnStateChange;

            _gatherer.OnLocalCandidate += async(candidate) =>
            {
                await _signaler.SendToPeer(RemotePeer.Id, candidate.Candidate.ToJsonString());
            };

            var cert = await RTCCertificate.GenerateCertificate();

            _ice = new RTCIceTransport(_gatherer);
            _ice.OnStateChange += IceTransport_OnStateChange;

            _dtls = new RTCDtlsTransport(_ice, new RTCCertificate[] { cert });
            _dtls.OnStateChange += Dtls_OnStateChange;

            _sctp = new RTCSctpTransport(_dtls);
        }
コード例 #2
0
        private async Task InitializeORTC()
        {
            var gatherOptions = new RTCIceGatherOptions()
            {
                IceServers = new List <RTCIceServer>()
                {
                    new RTCIceServer
                    {
                        Urls = new string[] { _stunUrl }
                    },
                    new RTCIceServer
                    {
                        Username       = _turnUsername,
                        Credential     = _turnCredential,
                        CredentialType = RTCIceCredentialType.Password,
                        Urls           = new string[] { _turnUrl }
                    }
                }
            };

            _gatherer = new RTCIceGatherer(gatherOptions);

            _gatherer.OnStateChange += IceGatherer_OnStateChange;

            _gatherer.OnLocalCandidate += (@event) =>
            {
                OnSignalMessageToPeer(@event.Candidate.ToJson().ToString());
            };
            _gatherer.OnLocalCandidateComplete += (@event) =>
            {
                OnSignalMessageToPeer(@event.Candidate.ToJson().ToString());
            };

            _ice = new RTCIceTransport(_gatherer);
            _ice.OnStateChange += IceTransport_OnStateChange;

            _dtls = new RTCDtlsTransport(_ice, new RTCCertificate[]
            {
                await RTCCertificate.GenerateCertificate()
            });
            _dtls.OnStateChange += Dtls_OnStateChange;

            _sctp = new RTCSctpTransport(_dtls);

            _gatherer.Gather(null);
        }
コード例 #3
0
        /// <summary>
        /// Creates a peer connection.
        /// </summary>
        /// <returns>True if connection to a peer is successfully created.</returns>
        private async Task <bool> CreatePeerConnection(CancellationToken cancelationToken)
        {
            Debug.Assert(_peerConnection == null);
            if (cancelationToken.IsCancellationRequested)
            {
                return(false);
            }

            var config = new RTCConfiguration()
            {
                BundlePolicy = RTCBundlePolicy.Balanced,
#if ORTCLIB
                SignalingMode = _signalingMode,
                GatherOptions = new RTCIceGatherOptions()
                {
                    IceServers = new List <RTCIceServer>(_iceServers),
                }
#else
                IceTransportPolicy = RTCIceTransportPolicy.All,
                IceServers         = _iceServers
#endif
            };

            Debug.WriteLine("Conductor: Creating peer connection.");
            _peerConnection = new RTCPeerConnection(config);

            if (_peerConnection == null)
            {
                throw new NullReferenceException("Peer connection is not created.");
            }

#if !ORTCLIB
            _peerConnection.EtwStatsEnabled = _etwStatsEnabled;
            _peerConnection.ConnectionHealthStatsEnabled = _peerConnectionStatsEnabled;
#endif
            if (cancelationToken.IsCancellationRequested)
            {
                return(false);
            }
#if ORTCLIB
            OrtcStatsManager.Instance.Initialize(_peerConnection);
#endif
            OnPeerConnectionCreated?.Invoke();

            _peerConnection.OnIceCandidate += PeerConnection_OnIceCandidate;
#if ORTCLIB
            _peerConnection.OnTrack     += PeerConnection_OnAddTrack;
            _peerConnection.OnTrackGone += PeerConnection_OnRemoveTrack;
            _peerConnection.OnIceConnectionStateChange += () => { Debug.WriteLine("Conductor: Ice connection state change, state=" + (null != _peerConnection ? _peerConnection.IceConnectionState.ToString() : "closed")); };
#else
            _peerConnection.OnAddStream             += PeerConnection_OnAddStream;
            _peerConnection.OnRemoveStream          += PeerConnection_OnRemoveStream;
            _peerConnection.OnConnectionHealthStats += PeerConnection_OnConnectionHealthStats;
#endif
            Debug.WriteLine("Conductor: Getting user media.");
            RTCMediaStreamConstraints mediaStreamConstraints = new RTCMediaStreamConstraints
            {
                // Always include audio/video enabled in the media stream,
                // so it will be possible to enable/disable audio/video if
                // the call was initiated without microphone/camera
                audioEnabled = true,
                videoEnabled = true
            };

            if (cancelationToken.IsCancellationRequested)
            {
                return(false);
            }

#if ORTCLIB
            var tracks = await _media.GetUserMedia(mediaStreamConstraints);

            if (tracks != null)
            {
                RTCRtpCapabilities audioCapabilities = RTCRtpSender.GetCapabilities("audio");
                RTCRtpCapabilities videoCapabilities = RTCRtpSender.GetCapabilities("video");

                _mediaStream = new MediaStream(tracks);
                Debug.WriteLine("Conductor: Adding local media stream.");
                IList <MediaStream> mediaStreamList = new List <MediaStream>();
                mediaStreamList.Add(_mediaStream);
                foreach (var mediaStreamTrack in tracks)
                {
                    //Create stream track configuration based on capabilities
                    RTCMediaStreamTrackConfiguration configuration = null;
                    if (mediaStreamTrack.Kind == MediaStreamTrackKind.Audio && audioCapabilities != null)
                    {
                        configuration =
                            await Helper.GetTrackConfigurationForCapabilities(audioCapabilities, AudioCodec);
                    }
                    else if (mediaStreamTrack.Kind == MediaStreamTrackKind.Video && videoCapabilities != null)
                    {
                        configuration =
                            await Helper.GetTrackConfigurationForCapabilities(videoCapabilities, VideoCodec);
                    }
                    if (configuration != null)
                    {
                        _peerConnection.AddTrack(mediaStreamTrack, mediaStreamList, configuration);
                    }
                }
            }
#else
            _mediaStream = await _media.GetUserMedia(mediaStreamConstraints);
#endif

            if (cancelationToken.IsCancellationRequested)
            {
                return(false);
            }

#if !ORTCLIB
            Debug.WriteLine("Conductor: Adding local media stream.");
            _peerConnection.AddStream(_mediaStream);
#endif
            OnAddLocalStream?.Invoke(new MediaStreamEvent()
            {
                Stream = _mediaStream
            });

            if (cancelationToken.IsCancellationRequested)
            {
                return(false);
            }
            return(true);
        }
コード例 #4
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            RTCIceGatherOptions options = new RTCIceGatherOptions();
            RTCIceServer        server  = new RTCIceServer();

            server.UserName   = "******";
            server.Credential = "12345";
            server.Urls       = new List <String>();
            server.Urls.Add("stun:stun.vline.com");
            options.IceServers = new List <RTCIceServer>();
            options.IceServers.Add(server);

            _iceGatherer = new RTCIceGatherer(options);
            _iceGatherer.OnStateChange            += this.RTCIceGatherer_onICEGathererStateChanged;
            _iceGatherer.OnLocalCandidate         += this.RTCIceGatherer_onICEGathererLocalCandidate;
            _iceGatherer.OnLocalCandidateComplete += this.RTCIceGatherer_onICEGathererCandidateComplete;
            _iceGatherer.OnLocalCandidateGone     += this.RTCIceGatherer_onICEGathererLocalCandidateGone;
            _iceGatherer.OnError += this.RTCIceGatherer_onICEGathererError;


            _iceGatherer2 = new RTCIceGatherer(options);
            _iceGatherer2.OnStateChange            += this.RTCIceGatherer_onICEGathererStateChanged2;
            _iceGatherer2.OnLocalCandidate         += this.RTCIceGatherer_onICEGathererLocalCandidate2;
            _iceGatherer2.OnLocalCandidateComplete += this.RTCIceGatherer_onICEGathererCandidateComplete2;
            _iceGatherer2.OnLocalCandidateGone     += this.RTCIceGatherer_onICEGathererLocalCandidateGone2;
            _iceGatherer2.OnError += this.RTCIceGatherer_onICEGathererError;

            _iceTransport = new RTCIceTransport(_iceGatherer);
            _iceTransport.OnStateChange            += RTCIceTransport_onICETransportStateChanged;
            _iceTransport.OnCandidatePairAvailable += RTCIceTransport_onICETransportCandidatePairAvailable;
            _iceTransport.OnCandidatePairGone      += RTCIceTransport_onICETransportCandidatePairGone;
            _iceTransport.OnCandidatePairChange    += RTCIceTransport_onICETransportCandidatePairChanged;

            _iceTransport2 = new RTCIceTransport(_iceGatherer);
            _iceTransport2.OnStateChange            += RTCIceTransport_onICETransportStateChanged2;
            _iceTransport2.OnCandidatePairAvailable += RTCIceTransport_onICETransportCandidatePairAvailable2;
            _iceTransport2.OnCandidatePairGone      += RTCIceTransport_onICETransportCandidatePairGone2;
            _iceTransport2.OnCandidatePairChange    += RTCIceTransport_onICETransportCandidatePairChanged2;

            RTCCertificate.GenerateCertificate().AsTask <RTCCertificate>().ContinueWith((cert) =>
            {
                String str = cert.Result.Expires.ToString();
                List <RTCCertificate> certs = new List <RTCCertificate>();
                certs.Add(cert.Result);
                _dtlsTransport = new RTCDtlsTransport(_iceTransport, certs);
                MediaStreamConstraints constraints = new MediaStreamConstraints();

                constraints.Audio = new MediaTrackConstraints();
                constraints.Video = new MediaTrackConstraints();

                MediaDevices.GetUserMedia(constraints).AsTask().ContinueWith <IList <MediaStreamTrack> >((temp) =>
                {
                    if (temp.Result != null && temp.Result.Count() > 0)
                    {
                        List <MediaStreamTrack> ret = new List <MediaStreamTrack>(temp.Result);
                        List <RTCRtpSender> senders = new List <RTCRtpSender>();
                        foreach (MediaStreamTrack track in temp.Result)
                        {
                            RTCRtpSender rtpSender = new RTCRtpSender(track, _dtlsTransport);
                            senders.Add(rtpSender);
                        }

                        return(ret);
                    }

                    return(null);
                });
            });

            RTCCertificate.GenerateCertificate().AsTask <RTCCertificate>().ContinueWith((cert) =>
            {
                var certs = new List <RTCCertificate>();
                certs.Add(cert.Result);
                _dtlsTransport2 = new RTCDtlsTransport(_iceTransport2, certs);
            });

            MediaDevices.EnumerateDevices().AsTask().ContinueWith <MediaDeviceInfo>((temp) =>
            {
                foreach (MediaDeviceInfo info in temp.Result)
                {
                    if (info.DeviceId != null)
                    {
                        System.Diagnostics.Debug.WriteLine("DeviceID: {0}", info.DeviceId);
                    }
                }
                return(null);
            });
        }