Beispiel #1
0
        private void AddTracks()
        {
            pc1Senders.Add(_pc1.AddTrack(track));

            if (WebRTCSettings.UseVideoCodec != null)
            {
                var codecs = new[] { WebRTCSettings.UseVideoCodec };
                foreach (var transceiver in _pc1.GetTransceivers())
                {
                    if (pc1Senders.Contains(transceiver.Sender))
                    {
                        transceiver.SetCodecPreferences(codecs);
                    }
                }
            }

            if (!videoUpdateStarted)
            {
                StartCoroutine(WebRTC.Update());
                videoUpdateStarted = true;
            }

            addTracksButton.interactable    = false;
            removeTracksButton.interactable = true;
        }
        public IEnumerator TransceiverReturnsSender()
        {
            RTCConfiguration config = default;

            config.iceServers = new[] { new RTCIceServer {
                                            urls = new[] { "stun:stun.l.google.com:19302" }
                                        } };
            var peer1 = new RTCPeerConnection(ref config);
            var peer2 = new RTCPeerConnection(ref config);

            peer1.OnIceCandidate = candidate => { peer2.AddIceCandidate(candidate); };
            peer2.OnIceCandidate = candidate => { peer1.AddIceCandidate(candidate); };

            AudioStreamTrack track1 = new AudioStreamTrack();

            peer1.AddTrack(track1);

            yield return(SignalingOffer(peer1, peer2));

            Assert.That(peer2.GetTransceivers().Count(), Is.EqualTo(1));
            RTCRtpSender sender1 = peer2.GetTransceivers().First().Sender;

            Assert.That(sender1, Is.Not.Null);

            AudioStreamTrack track2  = new AudioStreamTrack();
            RTCRtpSender     sender2 = peer2.AddTrack(track2);

            Assert.That(sender2, Is.Not.Null);
            Assert.That(sender1, Is.EqualTo(sender2));

            track1.Dispose();
            track2.Dispose();
            peer1.Dispose();
            peer2.Dispose();
        }
        public void AddTransceiver()
        {
            var peer   = new RTCPeerConnection();
            var stream = Audio.CaptureStream();
            var track  = stream.GetAudioTracks().First();

            Assert.AreEqual(0, peer.GetTransceivers().Count());
            var transceiver = peer.AddTransceiver(track);

            Assert.NotNull(transceiver);
            Assert.IsNull(transceiver.CurrentDirection);
            RTCRtpSender sender = transceiver.Sender;

            Assert.NotNull(sender);
            Assert.AreEqual(track, sender.Track);

            RTCRtpSendParameters parameters = sender.GetParameters();

            Assert.NotNull(parameters);
            Assert.NotNull(parameters.encodings);
            foreach (var encoding in parameters.encodings)
            {
                Assert.True(encoding.active);
                Assert.Null(encoding.maxBitrate);
                Assert.Null(encoding.minBitrate);
                Assert.Null(encoding.maxFramerate);
                Assert.Null(encoding.scaleResolutionDownBy);
                Assert.IsNotEmpty(encoding.rid);
            }
            Assert.IsNotEmpty(parameters.transactionId);
            Assert.AreEqual(1, peer.GetTransceivers().Count());
            Assert.NotNull(peer.GetTransceivers().First());
            Assert.NotNull(parameters.codecs);
            foreach (var codec in parameters.codecs)
            {
                Assert.NotNull(codec);
                Assert.NotZero(codec.payloadType);
                Assert.IsNotEmpty(codec.mimeType);
                Assert.IsNotEmpty(codec.sdpFmtpLine);
                Assert.Null(codec.clockRate);
                Assert.Null(codec.channels);
            }
            Assert.NotNull(parameters.rtcp);
            Assert.NotNull(parameters.headerExtensions);
            foreach (var extension in parameters.headerExtensions)
            {
                Assert.NotNull(extension);
                Assert.IsNotEmpty(extension.uri);
                Assert.NotZero(extension.id);
            }

            track.Dispose();
            stream.Dispose();
            peer.Dispose();
        }
Beispiel #4
0
        public void GetTransceiversReturnsNotEmptyAfterDisposingTransceiver()
        {
            // `RTCPeerConnection.AddTransceiver` method is not intuitive. Moreover, we don't have the API to remove
            // the transceiver from RTCPeerConnection directly.
            var peer        = new RTCPeerConnection();
            var transceiver = peer.AddTransceiver(TrackKind.Video);

            Assert.That(peer.GetTransceivers(), Has.Count.EqualTo(1));
            transceiver.Dispose();
            Assert.That(peer.GetTransceivers(), Has.Count.EqualTo(1));
            peer.Dispose();
        }
        public void GetTransceivers()
        {
            var peer  = new RTCPeerConnection();
            var track = new AudioStreamTrack("audio");

            var sender = peer.AddTrack(track);

            Assert.That(peer.GetTransceivers().ToList(), Has.Count.EqualTo(1));
            Assert.That(peer.GetTransceivers().Select(t => t.Sender).ToList(), Has.Member(sender));

            track.Dispose();
            peer.Dispose();
        }
        public void AddTransceiver()
        {
            var peer   = new RTCPeerConnection();
            var stream = Audio.CaptureStream();
            var track  = stream.GetAudioTracks().First();

            Assert.AreEqual(0, peer.GetTransceivers().Count());
            var transceiver = peer.AddTransceiver(track);

            Assert.NotNull(transceiver);
            Assert.That(() => Assert.NotNull(transceiver.CurrentDirection), Throws.InvalidOperationException);
            Assert.NotNull(transceiver.Sender);
            Assert.AreEqual(1, peer.GetTransceivers().Count());
            Assert.NotNull(peer.GetTransceivers().First());
        }
    private void AddTracks()
    {
        var pc1VideoSenders = new List <RTCRtpSender>();

        foreach (var track in sendStream.GetTracks())
        {
            var sender = _pc1.AddTrack(track, sendStream);
            if (track.Kind == TrackKind.Video)
            {
                pc1VideoSenders.Add(sender);
            }
        }

        if (WebRTCSettings.UseVideoCodec != null)
        {
            var codecs = new[] { WebRTCSettings.UseVideoCodec };
            foreach (var transceiver in _pc1.GetTransceivers())
            {
                if (pc1VideoSenders.Contains(transceiver.Sender))
                {
                    transceiver.SetCodecPreferences(codecs);
                }
            }
        }
    }
Beispiel #8
0
    private void AddTracks()
    {
        foreach (var track in videoStream.GetTracks())
        {
            pc1Senders.Add(_pc1.AddTrack(track, videoStream));
        }

        if (WebRTCSettings.UseVideoCodec != null)
        {
            var codecs = new[] { WebRTCSettings.UseVideoCodec };
            foreach (var transceiver in _pc1.GetTransceivers())
            {
                if (pc1Senders.Contains(transceiver.Sender))
                {
                    transceiver.SetCodecPreferences(codecs);
                }
            }
        }

        if (!videoUpdateStarted)
        {
            StartCoroutine(WebRTC.Update());
            videoUpdateStarted = true;
        }
    }
Beispiel #9
0
    private void AddTracks()
    {
        foreach (var track in videoStream.GetTracks())
        {
            pc1Senders.Add(_pc1.AddTrack(track, videoStream));
        }

        if (!videoUpdateStarted)
        {
            StartCoroutine(WebRTC.Update());
            videoUpdateStarted = true;
        }

        RTCRtpCodecCapability[] codecs = null;
        if (codecSelector.value == 0)
        {
            codecs = RTCRtpSender.GetCapabilities(TrackKind.Video).codecs;
        }
        else
        {
            RTCRtpCodecCapability preferredCodec = availableCodecs[codecSelector.value - 1];
            codecs = new[] { preferredCodec };
        }
        RTCRtpTransceiver transceiver = _pc1.GetTransceivers().First();
        RTCErrorType      error       = transceiver.SetCodecPreferences(codecs);

        if (error != RTCErrorType.None)
        {
            Debug.LogErrorFormat("RTCRtpTransceiver.SetCodecPreferences failed. {0}", error);
        }
    }
    private void AddTracks()
    {
        foreach (var track in videoStream.GetTracks())
        {
            pc1Senders.Add(_pc1.AddTrack(track, videoStream));
        }

        if (WebRTCSettings.UseVideoCodec != null)
        {
            var codecs = new[] { WebRTCSettings.UseVideoCodec };
            foreach (var transceiver in _pc1.GetTransceivers())
            {
                if (pc1Senders.Contains(transceiver.Sender))
                {
                    transceiver.SetCodecPreferences(codecs);
                }
            }
        }

        if (!videoUpdateStarted)
        {
            StartCoroutine(WebRTC.Update());
            StartCoroutine(LoopStatsCoroutine());
            videoUpdateStarted = true;
        }

        bandwidthSelector.interactable           = false;
        scaleResolutionDownSelector.interactable = false;
    }
Beispiel #11
0
        public IEnumerator TransceiverReturnsSender()
        {
            RTCConfiguration config = default;

            config.iceServers = new[] { new RTCIceServer {
                                            urls = new[] { "stun:stun.l.google.com:19302" }
                                        } };
            var peer1 = new RTCPeerConnection(ref config);
            var peer2 = new RTCPeerConnection(ref config);

            peer1.OnIceCandidate = candidate => { peer2.AddIceCandidate(candidate); };
            peer2.OnIceCandidate = candidate => { peer1.AddIceCandidate(candidate); };

            var obj1    = new GameObject("audio1");
            var source1 = obj1.AddComponent <AudioSource>();

            source1.clip = AudioClip.Create("test1", 480, 2, 48000, false);
            AudioStreamTrack track1 = new AudioStreamTrack(source1);

            peer1.AddTrack(track1);

            yield return(SignalingOffer(peer1, peer2));

            Assert.That(peer2.GetTransceivers().Count(), Is.EqualTo(1));
            RTCRtpSender sender1 = peer2.GetTransceivers().First().Sender;

            Assert.That(sender1, Is.Not.Null);

            var obj2    = new GameObject("audio2");
            var source2 = obj2.AddComponent <AudioSource>();

            source2.clip = AudioClip.Create("test2", 480, 2, 48000, false);
            AudioStreamTrack track2  = new AudioStreamTrack(source2);
            RTCRtpSender     sender2 = peer2.AddTrack(track2);

            Assert.That(sender2, Is.Not.Null);
            Assert.That(sender1, Is.EqualTo(sender2));

            track1.Dispose();
            track2.Dispose();
            peer1.Dispose();
            peer2.Dispose();
            Object.DestroyImmediate(source1.clip);
            Object.DestroyImmediate(source2.clip);
            Object.DestroyImmediate(obj1);
            Object.DestroyImmediate(obj2);
        }
Beispiel #12
0
        private void AddTracks()
        {
            var videoSender = _pc1.AddTrack(videoStreamTrack);

            pc1Senders.Add(videoSender);
            pc1Senders.Add(_pc1.AddTrack(audioStreamTrack));

            if (WebRTCSettings.UseVideoCodec != null)
            {
                var codecs      = new[] { WebRTCSettings.UseVideoCodec };
                var transceiver = _pc1.GetTransceivers().First(t => t.Sender == videoSender);
                transceiver.SetCodecPreferences(codecs);
            }

            addTracksButton.interactable    = false;
            removeTracksButton.interactable = true;
        }
Beispiel #13
0
        public void AddTransceiver()
        {
            var peer   = new RTCPeerConnection();
            var width  = 256;
            var height = 256;
            var format = WebRTC.GetSupportedRenderTextureFormat(UnityEngine.SystemInfo.graphicsDeviceType);
            var rt     = new UnityEngine.RenderTexture(width, height, 0, format);

            rt.Create();

            var track = new VideoStreamTrack(rt);

            Assert.That(peer.GetTransceivers(), Is.Empty);
            var transceiver = peer.AddTransceiver(track);

            Assert.That(transceiver, Is.Not.Null);
            Assert.That(transceiver.Mid, Is.Null);
            Assert.That(transceiver.CurrentDirection, Is.Null);
            RTCRtpSender sender = transceiver.Sender;

            Assert.That(sender, Is.Not.Null);
            Assert.That(track, Is.EqualTo(sender.Track));

            RTCRtpSendParameters parameters = sender.GetParameters();

            Assert.That(parameters, Is.Not.Null);
            Assert.That(parameters.encodings, Is.Empty);
            Assert.That(parameters.transactionId, Is.Not.Empty);
            Assert.That(peer.GetTransceivers(), Has.Count.EqualTo(1));
            Assert.That(peer.GetTransceivers().First(), Is.Not.Null);
            Assert.That(parameters.codecs, Is.Empty);
            Assert.That(parameters.rtcp, Is.Not.Null);

            // Some platforms return an empty list
            Assert.That(parameters.headerExtensions, Is.Not.Null);
            foreach (var extension in parameters.headerExtensions)
            {
                Assert.That(extension, Is.Not.Null);
                Assert.That(extension.uri, Is.Not.Empty);
            }

            track.Dispose();
            peer.Dispose();
            Object.DestroyImmediate(rt);
        }
Beispiel #14
0
        public void GetTransceiversReturnsNotEmptyAfterCallingRemoveTrack()
        {
            // Also, `RTCPeerConnection.AddTrack` and `RTCPeerConnection.RemoveTrack` method is not intuitive.
            var peer   = new RTCPeerConnection();
            var width  = 256;
            var height = 256;
            var format = WebRTC.GetSupportedRenderTextureFormat(UnityEngine.SystemInfo.graphicsDeviceType);
            var rt     = new UnityEngine.RenderTexture(width, height, 0, format);

            rt.Create();
            var track  = new VideoStreamTrack(rt);
            var sender = peer.AddTrack(track);

            Assert.That(peer.GetTransceivers(), Has.Count.EqualTo(1));
            Assert.That(peer.RemoveTrack(sender), Is.EqualTo(RTCErrorType.None));
            Assert.That(peer.GetTransceivers(), Has.Count.EqualTo(1));
            peer.Dispose();
        }
 public void AddTrack()
 {
     sender = offerPc.AddTrack(SendVideoTrack);
     if (videoCodec != null)
     {
         var transceiver = offerPc.GetTransceivers().First(t => t.Sender == sender);
         transceiver.SetCodecPreferences(new[] { videoCodec });
     }
 }
Beispiel #16
0
        public IEnumerator CurrentDirection()
        {
            var config     = GetConfiguration();
            var peer1      = new RTCPeerConnection(ref config);
            var peer2      = new RTCPeerConnection(ref config);
            var audioTrack = new AudioStreamTrack("audio");

            var transceiver1 = peer1.AddTransceiver(TrackKind.Audio);

            transceiver1.Direction = RTCRtpTransceiverDirection.RecvOnly;
            Assert.IsNull(transceiver1.CurrentDirection);

            RTCOfferOptions options1 = new RTCOfferOptions {
                offerToReceiveAudio = true
            };
            RTCAnswerOptions options2 = default;
            var op1 = peer1.CreateOffer(ref options1);

            yield return(op1);

            var desc = op1.Desc;
            var op2  = peer1.SetLocalDescription(ref desc);

            yield return(op2);

            var op3 = peer2.SetRemoteDescription(ref desc);

            yield return(op3);

            var transceiver2 = peer2.GetTransceivers().First(x => x.Receiver.Track.Kind == TrackKind.Audio);

            Assert.True(transceiver2.Sender.ReplaceTrack(audioTrack));
            transceiver2.Direction = RTCRtpTransceiverDirection.SendOnly;

            var op4 = peer2.CreateAnswer(ref options2);

            yield return(op4);

            desc = op4.Desc;
            var op5 = peer2.SetLocalDescription(ref desc);

            yield return(op5);

            var op6 = peer1.SetRemoteDescription(ref desc);

            yield return(op6);

            Assert.AreEqual(transceiver1.CurrentDirection, RTCRtpTransceiverDirection.RecvOnly);
            Assert.AreEqual(transceiver2.CurrentDirection, RTCRtpTransceiverDirection.SendOnly);

            audioTrack.Dispose();
            peer1.Close();
            peer2.Close();
            peer1.Dispose();
            peer2.Dispose();
        }
Beispiel #17
0
        public void GetTransceivers()
        {
            var peer   = new RTCPeerConnection();
            var obj    = new GameObject("audio");
            var source = obj.AddComponent <AudioSource>();

            source.clip = AudioClip.Create("test", 480, 2, 48000, false);
            var track = new AudioStreamTrack(source);

            var sender = peer.AddTrack(track);

            Assert.That(peer.GetTransceivers().ToList(), Has.Count.EqualTo(1));
            Assert.That(peer.GetTransceivers().Select(t => t.Sender).ToList(), Has.Member(sender));

            track.Dispose();
            peer.Dispose();
            Object.DestroyImmediate(source.clip);
            Object.DestroyImmediate(obj);
        }
Beispiel #18
0
        public void AddTransceiverTrackKindAudio()
        {
            var peer        = new RTCPeerConnection();
            var transceiver = peer.AddTransceiver(TrackKind.Audio);

            Assert.NotNull(transceiver);
            Assert.That(() => Assert.NotNull(transceiver.CurrentDirection), Throws.InvalidOperationException);
            RTCRtpReceiver receiver = transceiver.Receiver;

            Assert.NotNull(receiver);
            MediaStreamTrack track = receiver.Track;

            Assert.NotNull(track);
            Assert.AreEqual(TrackKind.Audio, track.Kind);
            Assert.True(track is AudioStreamTrack);

            Assert.AreEqual(1, peer.GetTransceivers().Count());
            Assert.NotNull(peer.GetTransceivers().First());
        }
Beispiel #19
0
        public void AddTransceiverTrackKindVideo()
        {
            var peer        = new RTCPeerConnection();
            var transceiver = peer.AddTransceiver(TrackKind.Video);

            Assert.NotNull(transceiver);
            Assert.IsNull(transceiver.CurrentDirection);
            RTCRtpReceiver receiver = transceiver.Receiver;

            Assert.NotNull(receiver);
            MediaStreamTrack track = receiver.Track;

            Assert.NotNull(track);
            Assert.AreEqual(TrackKind.Video, track.Kind);
            Assert.True(track is VideoStreamTrack);

            Assert.AreEqual(1, peer.GetTransceivers().Count());
            Assert.NotNull(peer.GetTransceivers().First());
        }
Beispiel #20
0
        public void AddTransceiverTrackKindVideo()
        {
            var peer        = new RTCPeerConnection();
            var transceiver = peer.AddTransceiver(TrackKind.Video);

            Assert.That(transceiver, Is.Not.Null);
            Assert.That(transceiver.CurrentDirection, Is.Null);
            RTCRtpReceiver receiver = transceiver.Receiver;

            Assert.That(receiver, Is.Not.Null);
            MediaStreamTrack track = receiver.Track;

            Assert.That(track, Is.Not.Null);
            Assert.That(track.Kind, Is.EqualTo(TrackKind.Video));
            Assert.That(track, Is.TypeOf <VideoStreamTrack>());
            Assert.That(receiver.Streams, Has.Count.EqualTo(0));

            Assert.That(peer.GetTransceivers(), Has.Count.EqualTo(1));
            Assert.That(peer.GetTransceivers(), Has.All.Not.Null);

            peer.Dispose();
        }
        public void AddTransceiver()
        {
            var peer   = new RTCPeerConnection();
            var stream = Audio.CaptureStream();
            var track  = stream.GetAudioTracks().First();

            Assert.AreEqual(0, peer.GetTransceivers().Count());
            var transceiver = peer.AddTransceiver(track);

            Assert.NotNull(transceiver);
            Assert.That(() => Assert.NotNull(transceiver.CurrentDirection), Throws.InvalidOperationException);
            RTCRtpSender sender = transceiver.Sender;

            Assert.NotNull(sender);
            Assert.AreEqual(track, sender.Track);

            RTCRtpSendParameters parameters = sender.GetParameters();

            Assert.NotNull(parameters);
            Assert.IsNotEmpty(parameters.TransactionId);
            Assert.AreEqual(1, peer.GetTransceivers().Count());
            Assert.NotNull(peer.GetTransceivers().First());
        }
Beispiel #22
0
        public void AddTransceiver()
        {
            var peer   = new RTCPeerConnection();
            var stream = Audio.CaptureStream();
            var track  = stream.GetAudioTracks().First();

            Assert.AreEqual(0, peer.GetTransceivers().Count());
            var transceiver = peer.AddTransceiver(track);

            Assert.NotNull(transceiver);
            Assert.IsNull(transceiver.CurrentDirection);
            RTCRtpSender sender = transceiver.Sender;

            Assert.NotNull(sender);
            Assert.AreEqual(track, sender.Track);

            RTCRtpSendParameters parameters = sender.GetParameters();

            Assert.NotNull(parameters);
            Assert.NotNull(parameters.Encodings);
            foreach (var encoding in parameters.Encodings)
            {
                Assert.True(encoding.active);
                Assert.Null(encoding.maxBitrate);
                Assert.Null(encoding.minBitrate);
                Assert.Null(encoding.maxFramerate);
                Assert.Null(encoding.scaleResolutionDownBy);
                Assert.IsNotEmpty(encoding.rid);
            }
            Assert.IsNotEmpty(parameters.TransactionId);
            Assert.AreEqual(1, peer.GetTransceivers().Count());
            Assert.NotNull(peer.GetTransceivers().First());

            track.Dispose();
            stream.Dispose();
            peer.Dispose();
        }
        private void RemoveTracks()
        {
            var transceivers = _pc1.GetTransceivers();

            foreach (var transceiver in transceivers)
            {
                if (transceiver.Sender != null)
                {
                    transceiver.Stop();
                    _pc1.RemoveTrack(transceiver.Sender);
                }
            }

            pc1Senders.Clear();
            addTracksButton.interactable    = true;
            removeTracksButton.interactable = false;
        }
Beispiel #24
0
        public void Construct()
        {
            var peer = new RTCPeerConnection();

            Assert.AreEqual(0, peer.GetReceivers().Count());
            Assert.AreEqual(0, peer.GetSenders().Count());
            Assert.AreEqual(0, peer.GetTransceivers().Count());
            Assert.AreEqual(RTCPeerConnectionState.New, peer.ConnectionState);
            Assert.That(() => peer.LocalDescription, Throws.InvalidOperationException);
            Assert.That(() => peer.RemoteDescription, Throws.InvalidOperationException);
            Assert.That(() => peer.PendingLocalDescription, Throws.InvalidOperationException);
            Assert.That(() => peer.PendingRemoteDescription, Throws.InvalidOperationException);
            Assert.That(() => peer.CurrentLocalDescription, Throws.InvalidOperationException);
            Assert.That(() => peer.CurrentRemoteDescription, Throws.InvalidOperationException);
            peer.Close();

            Assert.AreEqual(RTCPeerConnectionState.Closed, peer.ConnectionState);
            peer.Dispose();
        }
Beispiel #25
0
    private void Call()
    {
        Debug.Log("Starting calls");

        pcLocal                 = new RTCPeerConnection(ref configuration);
        pcRemote                = new RTCPeerConnection(ref configuration);
        pcRemote.OnTrack        = e => receiveVideoStream.AddTrack(e.Track);
        pcLocal.OnIceCandidate  = candidate => pcRemote.AddIceCandidate(candidate);
        pcRemote.OnIceCandidate = candidate => pcLocal.AddIceCandidate(candidate);
        Debug.Log("pc1: created local and remote peer connection object");

        var senders = new List <RTCRtpSender>();

        foreach (var track in sourceVideoStream.GetTracks())
        {
            senders.Add(pcLocal.AddTrack(track, sourceVideoStream));
        }

        if (WebRTCSettings.UseVideoCodec != null)
        {
            var codecs = new[] { WebRTCSettings.UseVideoCodec };
            foreach (var transceiver in pcLocal.GetTransceivers())
            {
                if (senders.Contains(transceiver.Sender))
                {
                    transceiver.SetCodecPreferences(codecs);
                }
            }
        }

        Debug.Log("Adding local stream to pcLocal");

        callButton.interactable         = false;
        createOfferButton.interactable  = true;
        createAnswerButton.interactable = true;
        setOfferButton.interactable     = true;
        setAnswerButton.interactable    = true;
        hangUpButton.interactable       = true;
    }
Beispiel #26
0
        private void AddTracks()
        {
            Debug.Log("Add not added tracks");

            foreach (var track in videoStreamTrackList.Where(x =>
                                                             !sendingSenderList.Exists(y => y.Track.Id == x.Id)))
            {
                var sender = _pc1.AddTrack(track);
                sendingSenderList.Add(sender);
            }

            if (WebRTCSettings.UseVideoCodec != null)
            {
                var codecs = new[] { WebRTCSettings.UseVideoCodec };
                foreach (var transceiver in _pc1.GetTransceivers())
                {
                    if (sendingSenderList.Contains(transceiver.Sender))
                    {
                        transceiver.SetCodecPreferences(codecs);
                    }
                }
            }
        }
        private void Call()
        {
            Debug.Log("Starting calls");

            pc1Local          = new RTCPeerConnection(ref configuration);
            pc1Remote         = new RTCPeerConnection(ref configuration);
            pc1Remote.OnTrack = e =>
            {
                if (e.Track is VideoStreamTrack videoTrack)
                {
                    videoTrack.OnVideoReceived += tex =>
                    {
                        receiveImage1.texture = tex;
                    };
                }

                if (e.Track is AudioStreamTrack audioTrack)
                {
                    receiveAudio1.SetTrack(audioTrack);
                    receiveAudio1.loop = true;
                    receiveAudio1.Play();
                }
            };
            pc1Local.OnIceCandidate  = candidate => pc1Remote.AddIceCandidate(candidate);
            pc1Remote.OnIceCandidate = candidate => pc1Local.AddIceCandidate(candidate);
            Debug.Log("pc1: created local and remote peer connection object");

            pc2Local          = new RTCPeerConnection(ref configuration);
            pc2Remote         = new RTCPeerConnection(ref configuration);
            pc2Remote.OnTrack = e =>
            {
                if (e.Track is VideoStreamTrack videoTrack)
                {
                    videoTrack.OnVideoReceived += tex =>
                    {
                        receiveImage2.texture = tex;
                    };
                }

                if (e.Track is AudioStreamTrack audioTrack)
                {
                    receiveAudio2.SetTrack(audioTrack);
                    receiveAudio2.loop = true;
                    receiveAudio2.Play();
                }
            };
            pc2Local.OnIceCandidate  = candidate => pc2Remote.AddIceCandidate(candidate);
            pc2Remote.OnIceCandidate = candidate => pc2Local.AddIceCandidate(candidate);
            Debug.Log("pc2: created local and remote peer connection object");

            var pc1VideoSenders = new List <RTCRtpSender>();
            var pc2VideoSenders = new List <RTCRtpSender>();

            foreach (var track in sourceStream.GetTracks())
            {
                var pc1Sender = pc1Local.AddTrack(track, sourceStream);
                var pc2Sender = pc2Local.AddTrack(track, sourceStream);

                if (track.Kind == TrackKind.Video)
                {
                    pc1VideoSenders.Add(pc1Sender);
                    pc2VideoSenders.Add(pc2Sender);
                }
            }

            if (WebRTCSettings.UseVideoCodec != null)
            {
                var codecs = new[] { WebRTCSettings.UseVideoCodec };
                foreach (var transceiver in pc1Local.GetTransceivers())
                {
                    if (pc1VideoSenders.Contains(transceiver.Sender))
                    {
                        transceiver.SetCodecPreferences(codecs);
                    }
                }

                foreach (var transceiver in pc2Local.GetTransceivers())
                {
                    if (pc2VideoSenders.Contains(transceiver.Sender))
                    {
                        transceiver.SetCodecPreferences(codecs);
                    }
                }
            }

            Debug.Log("Adding local stream to pc1Local/pc2Local");

            StartCoroutine(NegotiationPeer(pc1Local, pc1Remote));
            StartCoroutine(NegotiationPeer(pc2Local, pc2Remote));

            callButton.interactable   = false;
            hangUpButton.interactable = true;
        }
Beispiel #28
0
        public IEnumerator CurrentDirection()
        {
            var config = GetDefaultConfiguration();
            var peer1  = new RTCPeerConnection(ref config);
            var peer2  = new RTCPeerConnection(ref config);
            var obj    = new GameObject("audio");
            var source = obj.AddComponent <AudioSource>();

            source.clip = AudioClip.Create("test", 480, 2, 48000, false);
            var audioTrack = new AudioStreamTrack(source);

            var transceiver1 = peer1.AddTransceiver(TrackKind.Audio);

            transceiver1.Direction = RTCRtpTransceiverDirection.RecvOnly;
            Assert.IsNull(transceiver1.CurrentDirection);

            var op1 = peer1.CreateOffer();

            yield return(op1);

            var desc = op1.Desc;
            var op2  = peer1.SetLocalDescription(ref desc);

            yield return(op2);

            var op3 = peer2.SetRemoteDescription(ref desc);

            yield return(op3);

            var transceiver2 = peer2.GetTransceivers().First(x => x.Receiver.Track.Kind == TrackKind.Audio);

            Assert.True(transceiver2.Sender.ReplaceTrack(audioTrack));
            transceiver2.Direction = RTCRtpTransceiverDirection.SendOnly;

            var op4 = peer2.CreateAnswer();

            yield return(op4);

            desc = op4.Desc;
            var op5 = peer2.SetLocalDescription(ref desc);

            yield return(op5);

            var op6 = peer1.SetRemoteDescription(ref desc);

            yield return(op6);

            Assert.AreEqual(transceiver1.CurrentDirection, RTCRtpTransceiverDirection.RecvOnly);
            Assert.AreEqual(transceiver2.CurrentDirection, RTCRtpTransceiverDirection.SendOnly);

            //Assert.That(transceiver2.Stop(), Is.EqualTo(RTCErrorType.None));
            //Assert.That(transceiver2.Direction, Is.EqualTo(RTCRtpTransceiverDirection.Stopped));

            // todo(kazuki):: Transceiver.CurrentDirection of Sender is not changed to "Stopped" even if waiting
            // yield return new WaitUntil(() => transceiver2.CurrentDirection == RTCRtpTransceiverDirection.Stopped);
            // Assert.That(transceiver2.CurrentDirection, Is.EqualTo(RTCRtpTransceiverDirection.Stopped));

            // todo(kazuki):: Transceiver.CurrentDirection of Receiver is not changed to "Stopped" even if waiting
            // yield return new WaitUntil(() => transceiver1.Direction == RTCRtpTransceiverDirection.Stopped);
            // Assert.That(transceiver1.Direction, Is.EqualTo(RTCRtpTransceiverDirection.Stopped));

            audioTrack.Dispose();
            peer1.Close();
            peer2.Close();
            peer1.Dispose();
            peer2.Dispose();
            Object.DestroyImmediate(source.clip);
            Object.DestroyImmediate(obj);
        }