public IEnumerator OnAnswer()
        {
            bool   startRaised1  = false;
            bool   startRaised2  = false;
            bool   offerRaised   = false;
            bool   answerRaised  = false;
            string connectionId1 = null;

            signaling1.OnStart += s => { startRaised1 = true; };
            signaling2.OnStart += s => { startRaised2 = true; };
            signaling1.Start();
            signaling2.Start();
            yield return(new WaitUntil(() => startRaised1 && startRaised2));

            signaling1.OnCreateConnection += (s, connectionId) => { connectionId1 = connectionId; };
            signaling1.CreateConnection();
            yield return(new WaitUntil(() => !string.IsNullOrEmpty(connectionId1)));

            signaling2.OnOffer += (s, e) => { offerRaised = true; };
            signaling1.SendOffer(connectionId1, m_DescOffer);
            yield return(new WaitUntil(() => offerRaised));

            signaling1.OnAnswer += (s, e) => { answerRaised = true; };
            signaling2.SendAnswer(connectionId1, m_DescAnswer);
            yield return(new WaitUntil(() => answerRaised));
        }
Beispiel #2
0
        public void OnAnswer()
        {
            bool   startRaised1  = false;
            bool   startRaised2  = false;
            bool   offerRaised   = false;
            bool   answerRaised  = false;
            string connectionId1 = null;

            signaling1.Start();
            signaling2.Start();
            signaling1.OnStart += s => { startRaised1 = true; };
            signaling2.OnStart += s => { startRaised2 = true; };
            Assert.True(Wait(() => startRaised1 && startRaised2));

            signaling1.OnCreateConnection += (s, connectionId) => { connectionId1 = connectionId; };
            signaling1.CreateConnection();
            Assert.True(Wait(() => !string.IsNullOrEmpty(connectionId1)));

            signaling2.OnOffer += (s, e) => { offerRaised = true; };
            signaling1.SendOffer(connectionId1, m_DescOffer);
            Assert.True(Wait(() => offerRaised));

            signaling1.OnAnswer += (s, e) => { answerRaised = true; };
            signaling2.SendAnswer(connectionId1, m_DescAnswer);
            Assert.True(Wait(() => answerRaised));
        }
Beispiel #3
0
        IEnumerator SendAnswerCoroutine(string connectionId, RTCPeerConnection pc)
        {
            RTCAnswerOptions options = default;
            var op = pc.CreateAnswer(ref options);

            yield return(op);

            if (op.IsError)
            {
                Debug.LogError($"Network Error: {op.Error.message}");
                yield break;
            }

            var desc        = op.Desc;
            var opLocalDesc = pc.SetLocalDescription(ref desc);

            yield return(opLocalDesc);

            if (opLocalDesc.IsError)
            {
                Debug.LogError($"Network Error: {opLocalDesc.Error.message}");
                yield break;
            }
            _signaling.SendAnswer(connectionId, desc);
        }
Beispiel #4
0
        IEnumerator OnOffer(ISignaling signaling, DescData e)
        {
            var connectionId     = e.connectionId;
            RTCPeerConnection pc = null;

            if (!m_mapConnectionIdAndPeer.TryGetValue(connectionId, out pc))
            {
                pc = CreatePeerConnection(signaling, connectionId, false);
            }

            RTCSessionDescription _desc;

            _desc.type = RTCSdpType.Offer;
            _desc.sdp  = e.sdp;

            var opRemoteDesc = pc.SetRemoteDescription(ref _desc);

            yield return(opRemoteDesc);

            if (opRemoteDesc.IsError)
            {
                Debug.LogError($"Network Error: {opRemoteDesc.Error.message}");
                yield break;
            }

            AddTracks(connectionId, pc);

            RTCAnswerOptions options = default;
            var op = pc.CreateAnswer(ref options);

            yield return(op);

            if (op.IsError)
            {
                Debug.LogError($"Network Error: {op.Error.message}");
                yield break;
            }

            var desc        = op.Desc;
            var opLocalDesc = pc.SetLocalDescription(ref desc);

            yield return(opLocalDesc);

            if (opLocalDesc.IsError)
            {
                Debug.LogError($"Network Error: {opLocalDesc.Error.message}");
                yield break;
            }

            signaling.SendAnswer(connectionId, desc);
        }
Beispiel #5
0
        public IEnumerator OnAnswer()
        {
            bool         startRaised1  = false;
            bool         startRaised2  = false;
            bool         offerRaised   = false;
            bool         answerRaised  = false;
            const string connectionId  = "12345";
            string       connectionId1 = null;
            string       connectionId2 = null;

            signaling1.OnStart += s => { startRaised1 = true; };
            signaling2.OnStart += s => { startRaised2 = true; };
            signaling1.Start();
            signaling2.Start();
            yield return(new WaitUntil(() => startRaised1 && startRaised2));

            signaling1.OnCreateConnection += (s, id, peerExists) => { connectionId1 = id; };
            signaling1.OpenConnection(connectionId);
            signaling2.OnCreateConnection += (s, id, peerExists) => { connectionId2 = id; };
            signaling2.OpenConnection(connectionId);
            yield return(new WaitUntil(() =>
                                       !string.IsNullOrEmpty(connectionId1) && !string.IsNullOrEmpty(connectionId2)));

            signaling2.OnOffer += (s, e) => { offerRaised = true; };
            signaling1.SendOffer(connectionId1, m_DescOffer);
            yield return(new WaitUntil(() => offerRaised));

            signaling1.OnAnswer += (s, e) => { answerRaised = true; };
            signaling2.SendAnswer(connectionId1, m_DescAnswer);
            yield return(new WaitUntil(() => answerRaised));

            signaling1.CloseConnection(connectionId1);
            signaling2.CloseConnection(connectionId2);
            signaling1.Stop();
            signaling2.Stop();
            yield return(new WaitForSeconds(1));
        }
        IEnumerator SendAnswerCoroutine(string connectionId, PeerConnection pc)
        {
            pc.makingAnswer = true;

            var opLocalDesc = pc.peer.SetLocalDescription();

            yield return(opLocalDesc);

            if (opLocalDesc.IsError)
            {
                Debug.LogError($"{pc} {opLocalDesc.Error.message}");
                pc.makingAnswer = false;
                yield break;
            }

            Assert.AreEqual(pc.peer.LocalDescription.type, RTCSdpType.Answer, $"{pc} onmessage SLD worked");
            Assert.AreEqual(pc.peer.SignalingState, RTCSignalingState.Stable,
                            $"{pc} onmessage not racing with negotiationneeded");
            pc.makingAnswer = false;

            _signaling.SendAnswer(connectionId, pc.peer.LocalDescription);
        }
Beispiel #7
0
        void OnOffer(ISignaling signaling, DescData e)
        {
            RTCSessionDescription _desc;

            _desc.type = RTCSdpType.Offer;
            _desc.sdp  = e.sdp;
            var connectionId = e.connectionId;

            if (m_mapConnectionIdAndPeer.ContainsKey(connectionId))
            {
                return;
            }
            var pc = new RTCPeerConnection();

            m_mapConnectionIdAndPeer.Add(e.connectionId, pc);

            pc.OnDataChannel = new DelegateOnDataChannel(channel => { OnDataChannel(pc, channel); });
            pc.SetConfiguration(ref m_conf);
            pc.OnIceCandidate = new DelegateOnIceCandidate(candidate =>
            {
                signaling.SendCandidate(e.connectionId, candidate);
            });
            pc.OnIceConnectionChange = new DelegateOnIceConnectionChange(state =>
            {
                if (state == RTCIceConnectionState.Disconnected)
                {
                    pc.Close();
                    m_mapConnectionIdAndPeer.Remove(e.connectionId);
                }
            });

            pc.SetRemoteDescription(ref _desc);
            foreach (var track in m_listVideoStreamTrack.Concat(m_audioStream.GetTracks()))
            {
                RTCRtpSender sender = pc.AddTrack(track);
                if (!m_mapTrackAndSenderList.TryGetValue(track, out List <RTCRtpSender> list))
                {
                    list = new List <RTCRtpSender>();
                    m_mapTrackAndSenderList.Add(track, list);
                }
                list.Add(sender);
            }

            RTCAnswerOptions options = default;
            var op = pc.CreateAnswer(ref options);

            while (op.MoveNext())
            {
            }
            if (op.IsError)
            {
                Debug.LogError($"Network Error: {op.Error}");
                return;
            }

            var desc        = op.Desc;
            var opLocalDesc = pc.SetLocalDescription(ref desc);

            while (opLocalDesc.MoveNext())
            {
            }
            if (opLocalDesc.IsError)
            {
                Debug.LogError($"Network Error: {opLocalDesc.Error}");
                return;
            }

            signaling.SendAnswer(connectionId, desc);
        }
Beispiel #8
0
        IEnumerator OnOffer(ISignaling signaling, DescData e)
        {
            var connectionId = e.connectionId;

            if (m_mapConnectionIdAndPeer.ContainsKey(connectionId))
            {
                Debug.LogError($"connection:{connectionId} peerConnection already exist");
                yield break;
            }

            var pc = CreatePeerConnection(signaling, connectionId, false);

            RTCSessionDescription _desc;

            _desc.type = RTCSdpType.Offer;
            _desc.sdp  = e.sdp;

            var opRemoteDesc = pc.SetRemoteDescription(ref _desc);

            yield return(opRemoteDesc);

            if (opRemoteDesc.IsError)
            {
                Debug.LogError($"Network Error: {opRemoteDesc.Error.message}");
                yield break;
            }

            // ToDo: need webrtc package version 2.3
            // foreach (var transceiver in pc.GetTransceivers()
            //     .Where(x => x.Receiver.Track.Kind == TrackKind.Video)
            //     .Select((x, index) => new {x, index})
            //     .Take(m_listVideoStreamTrack.Count))
            // {
            //     RTCRtpSender sender = transceiver.x.Sender;
            //     VideoStreamTrack track = m_listVideoStreamTrack[transceiver.index];
            //     transceiver.x.Sender.ReplaceTrack(track);
            //     transceiver.x.Direction = RTCRtpTransceiverDirection.SendOnly;
            //
            //     if (!m_mapTrackAndSenderList.TryGetValue(track, out List<RTCRtpSender> list))
            //     {
            //         list = new List<RTCRtpSender>();
            //         m_mapTrackAndSenderList.Add(track, list);
            //     }
            //
            //     list.Add(sender);
            // }

            foreach (var track in m_listVideoStreamTrack)
            {
                RTCRtpSender sender = pc.AddTrack(track);
                if (!m_mapTrackAndSenderList.TryGetValue(track, out List <RTCRtpSender> list))
                {
                    list = new List <RTCRtpSender>();
                    m_mapTrackAndSenderList.Add(track, list);
                }
                list.Add(sender);
            }

            foreach (var track in m_audioStream.GetTracks())
            {
                RTCRtpSender sender = pc.AddTrack(track);
                if (!m_mapTrackAndSenderList.TryGetValue(track, out List <RTCRtpSender> list))
                {
                    list = new List <RTCRtpSender>();
                    m_mapTrackAndSenderList.Add(track, list);
                }
                list.Add(sender);
            }

            RTCAnswerOptions options = default;
            var op = pc.CreateAnswer(ref options);

            yield return(op);

            if (op.IsError)
            {
                Debug.LogError($"Network Error: {op.Error.message}");
                yield break;
            }

            var desc        = op.Desc;
            var opLocalDesc = pc.SetLocalDescription(ref desc);

            yield return(opLocalDesc);

            if (opLocalDesc.IsError)
            {
                Debug.LogError($"Network Error: {opLocalDesc.Error.message}");
                yield break;
            }

            signaling.SendAnswer(connectionId, desc);
        }
        void OnOffer(ISignaling signaling, DescData e)
        {
            RTCSessionDescription _desc;

            _desc.type = RTCSdpType.Offer;
            _desc.sdp  = e.sdp;
            var connectionId = e.connectionId;

            if (m_mapConnectionIdAndPeer.ContainsKey(connectionId))
            {
                return;
            }
            var pc = new RTCPeerConnection();

            m_mapConnectionIdAndPeer.Add(e.connectionId, pc);

            pc.OnDataChannel = new DelegateOnDataChannel(channel => { OnDataChannel(pc, channel); });
            pc.SetConfiguration(ref m_conf);
            pc.OnIceCandidate = new DelegateOnIceCandidate(candidate =>
            {
                signaling.SendCandidate(e.connectionId, candidate);
            });
            pc.OnIceConnectionChange = new DelegateOnIceConnectionChange(state =>
            {
                if (state == RTCIceConnectionState.Disconnected)
                {
                    pc.Close();
                    m_mapConnectionIdAndPeer.Remove(e.connectionId);
                }
            });
            //make video bit rate starts at 16000kbits, and 160000kbits at max.
            string pattern = @"(a=fmtp:\d+ .*level-asymmetry-allowed=.*)\r\n";

            _desc.sdp = Regex.Replace(_desc.sdp, pattern, "$1;x-google-start-bitrate=16000;x-google-max-bitrate=160000\r\n");
            pc.SetRemoteDescription(ref _desc);
            foreach (var track in m_listVideoStreamTrack)
            {
                pc.AddTrack(track);
            }
            foreach (var track in m_audioStream.GetTracks())
            {
                pc.AddTrack(track);
            }

            RTCAnswerOptions options = default;
            var op = pc.CreateAnswer(ref options);

            while (op.MoveNext())
            {
            }
            if (op.IsError)
            {
                Debug.LogError($"Network Error: {op.Error}");
                return;
            }

            var desc        = op.Desc;
            var opLocalDesc = pc.SetLocalDescription(ref desc);

            while (opLocalDesc.MoveNext())
            {
            }
            if (opLocalDesc.IsError)
            {
                Debug.LogError($"Network Error: {opLocalDesc.Error}");
                return;
            }

            signaling.SendAnswer(connectionId, desc);
        }
Beispiel #10
0
        public IEnumerator OnAnswer()
        {
            bool         startRaised1    = false;
            bool         startRaised2    = false;
            bool         offerRaised     = false;
            bool         answerRaised    = false;
            const string connectionId    = "12345";
            bool         raiseOnDestroy1 = false;
            bool         raiseOnDestroy2 = false;
            string       connectionId1   = null;
            string       connectionId2   = null;

            signaling1.OnStart += s => { startRaised1 = true; };
            signaling2.OnStart += s => { startRaised2 = true; };
            signaling1.Start();
            signaling2.Start();
            yield return(new WaitUntil(() => startRaised1 && startRaised2));

            signaling1.OnCreateConnection += (s, id, polite) =>
            {
                connectionId1 = id;
            };
            signaling1.OnDestroyConnection += (signaling, id) =>
            {
                raiseOnDestroy1 = id == connectionId1;
            };
            signaling1.OpenConnection(connectionId);

            signaling2.OnCreateConnection += (s, id, polite) =>
            {
                connectionId2 = id;
            };
            signaling2.OnDestroyConnection += (signaling, id) =>
            {
                raiseOnDestroy2 = id == connectionId2;
            };
            signaling2.OpenConnection(connectionId);

            yield return(new WaitUntil(() =>
                                       !string.IsNullOrEmpty(connectionId1) && !string.IsNullOrEmpty(connectionId2)));

            Assert.That(connectionId1, Is.EqualTo(connectionId));
            Assert.That(connectionId2, Is.EqualTo(connectionId));

            signaling2.OnOffer += (s, e) => { offerRaised = true; };
            signaling1.SendOffer(connectionId1, m_DescOffer);
            yield return(new WaitUntil(() => offerRaised));

            signaling1.OnAnswer += (s, e) => { answerRaised = true; };
            signaling2.SendAnswer(connectionId1, m_DescAnswer);
            yield return(new WaitUntil(() => answerRaised));

            signaling1.CloseConnection(connectionId1);

            yield return(new WaitUntil(() => raiseOnDestroy1 && raiseOnDestroy2));

            Assert.That(raiseOnDestroy1, Is.True);
            Assert.That(raiseOnDestroy2, Is.True);

            signaling2.CloseConnection(connectionId2);
            signaling1.Stop();
            signaling2.Stop();
            yield return(new WaitForSeconds(1));
        }
        IEnumerator OnOffer(ISignaling signaling, DescData e)
        {
            var connectionId = e.connectionId;

            if (m_mapConnectionIdAndPeer.ContainsKey(connectionId))
            {
                Debug.LogError($"connection:{connectionId} peerConnection already exist");
                yield break;
            }

            var pc = CreatePeerConnection(signaling, connectionId, false);

            RTCSessionDescription _desc;

            _desc.type = RTCSdpType.Offer;
            _desc.sdp  = e.sdp;

            var opRemoteDesc = pc.SetRemoteDescription(ref _desc);

            yield return(opRemoteDesc);

            if (opRemoteDesc.IsError)
            {
                Debug.LogError($"Network Error: {opRemoteDesc.Error.message}");
                yield break;
            }

            foreach (var track in m_listVideoStreamTrack.Concat(m_audioStream.GetTracks()))
            {
                RTCRtpSender sender = pc.AddTrack(track);
                if (!m_mapTrackAndSenderList.TryGetValue(track, out List <RTCRtpSender> list))
                {
                    list = new List <RTCRtpSender>();
                    m_mapTrackAndSenderList.Add(track, list);
                }
                list.Add(sender);
            }

            RTCAnswerOptions options = default;
            var op = pc.CreateAnswer(ref options);

            yield return(op);

            if (op.IsError)
            {
                Debug.LogError($"Network Error: {op.Error.message}");
                yield break;
            }

            var desc        = op.Desc;
            var opLocalDesc = pc.SetLocalDescription(ref desc);

            yield return(opLocalDesc);

            if (opLocalDesc.IsError)
            {
                Debug.LogError($"Network Error: {opLocalDesc.Error.message}");
                yield break;
            }

            signaling.SendAnswer(connectionId, desc);
        }