コード例 #1
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);
        }
コード例 #2
0
        /// <summary>
        /// Estabilishes a DataChannel with the parameter peer.
        /// </summary>
        private async Task OpenDataChannel(Peer peer)
        {
            Debug.WriteLine($"Opening data channel to peer: {peer.Name}");

            Conversation = string.Empty;

            Debug.WriteLine("Ice: Gathering candidates.");
            _gatherer.Gather(null);

            var iceParams = _gatherer.GetLocalParameters();
            await _signaler.SendToPeer(peer.Id, iceParams.ToJsonString());

            // this order guarantees: alice -> bob; bob.start(); bob -> alice; alice.start(); alice -> datachannel -> bob
            if (_isInitiator)
            {
                var sctpCaps = RTCSctpTransport.GetCapabilities();
                await _signaler.SendToPeer(peer.Id, sctpCaps.ToJsonString());
            }

            var dtlsParams = _dtls.GetLocalParameters();
            await _signaler.SendToPeer(peer.Id, dtlsParams.ToJsonString());
        }