Exemplo n.º 1
0
        public IEnumerator OnAddTrackDelegatesWithEvent()
        {
            var camObj      = new GameObject("Camera");
            var cam         = camObj.AddComponent <Camera>();
            var videoStream = cam.CaptureStream(1280, 720);

            var test = new MonoBehaviourTest <SignalingPeers>();

            test.component.AddStream(0, videoStream);
            yield return(test);

            test.component.CoroutineUpdate();
            yield return(new WaitForSeconds(0.1f));

            bool isCalledOnAddTrack    = false;
            bool isCalledOnRemoveTrack = false;

            videoStream.OnAddTrack = e =>
            {
                Assert.That(e.Track, Is.Not.Null);
                isCalledOnAddTrack = true;
            };
            videoStream.OnRemoveTrack = e =>
            {
                Assert.That(e.Track, Is.Not.Null);
                isCalledOnRemoveTrack = true;
            };

            var width  = 256;
            var height = 256;
            var format = WebRTC.GetSupportedRenderTextureFormat(SystemInfo.graphicsDeviceType);
            var rt     = new UnityEngine.RenderTexture(width, height, 0, format);

            rt.Create();
            var track2 = new VideoStreamTrack(rt);

            yield return(0);

            Assert.That(videoStream.AddTrack(track2), Is.True);
            var op1 = new WaitUntilWithTimeout(() => isCalledOnAddTrack, 5000);

            yield return(op1);

            Assert.That(videoStream.RemoveTrack(track2), Is.True);
            var op2 = new WaitUntilWithTimeout(() => isCalledOnRemoveTrack, 5000);

            yield return(op2);

            test.component.Dispose();
            track2.Dispose();
            foreach (var track in videoStream.GetTracks())
            {
                track.Dispose();
            }
            // wait for disposing video track.
            yield return(0);

            videoStream.Dispose();
            Object.DestroyImmediate(camObj);
            Object.DestroyImmediate(rt);
            Object.DestroyImmediate(test.gameObject);
        }
Exemplo n.º 2
0
        public IEnumerator GetStatsReturnsReport()
        {
            if (SystemInfo.processorType == "Apple M1")
            {
                Assert.Ignore("todo:: This test will hang up on Apple M1");
            }

            var stream = new MediaStream();

            var go  = new GameObject("Test");
            var cam = go.AddComponent <Camera>();

            stream.AddTrack(cam.CaptureStreamTrack(1280, 720));

            var source = go.AddComponent <AudioSource>();

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

            yield return(new WaitForSeconds(0.1f));

            var test = new MonoBehaviourTest <SignalingPeers>();

            test.component.AddStream(0, stream);
            yield return(test);

            test.component.CoroutineUpdate();
            yield return(new WaitForSeconds(0.1f));

            var op = test.component.GetPeerStats(0);

            yield return(op);

            Assert.That(op.IsDone, Is.True);
            Assert.That(op.Value.Stats, Is.Not.Empty);
            Assert.That(op.Value.Stats.Keys, Is.Not.Empty);
            Assert.That(op.Value.Stats.Values, Is.Not.Empty);
            Assert.That(op.Value.Stats.Count, Is.GreaterThan(0));

            foreach (RTCStats stats in op.Value.Stats.Values)
            {
                Assert.That(stats, Is.Not.Null);
                Assert.That(stats.Timestamp, Is.GreaterThan(0));
                Assert.That(stats.Id, Is.Not.Empty);
                foreach (var pair in stats.Dict)
                {
                    Assert.That(pair.Key, Is.Not.Empty);
                }
                StatsCheck.Test(stats);
            }
            op.Value.Dispose();

            test.component.Dispose();
            foreach (var track in stream.GetTracks())
            {
                track.Dispose();
            }
            stream.Dispose();
            Object.DestroyImmediate(go);
            Object.DestroyImmediate(test.gameObject);
        }
Exemplo n.º 3
0
 public void SetUp()
 {
     test = new MonoBehaviourTest <MyMonoBehaviourTest>();
 }
Exemplo n.º 4
0
        public IEnumerator SendAndReceiveMessageWithExecuteTasks()
        {
            var test  = new MonoBehaviourTest <SignalingPeers>();
            var label = "test";

            RTCDataChannel channel1 = test.component.CreateDataChannel(0, label);

            Assert.That(channel1, Is.Not.Null);
            yield return(test);

            var op1 = new WaitUntilWithTimeout(() => test.component.GetDataChannelList(1).Count > 0, 5000);

            yield return(op1);

            RTCDataChannel channel2 = test.component.GetDataChannelList(1)[0];

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

            Assert.That(channel1.ReadyState, Is.EqualTo(RTCDataChannelState.Open));
            Assert.That(channel2.ReadyState, Is.EqualTo(RTCDataChannelState.Open));
            Assert.That(channel1.Label, Is.EqualTo(channel2.Label));
            Assert.That(channel1.Id, Is.EqualTo(channel2.Id));

            // send string
            const int    millisecondTimeout = 5000;
            const string message1           = "hello";
            string       message2           = null;

            channel2.OnMessage = bytes => { message2 = System.Text.Encoding.UTF8.GetString(bytes); };
            channel1.Send(message1);
            ExecutePendingTasksWithTimeout(ref message2, millisecondTimeout);
            Assert.That(message1, Is.EqualTo(message2));

            // send byte array
            byte[] message3 = { 1, 2, 3 };
            byte[] message4 = null;
            channel2.OnMessage = bytes => { message4 = bytes; };
            channel1.Send(message3);
            ExecutePendingTasksWithTimeout(ref message4, millisecondTimeout);
            Assert.That(message3, Is.EqualTo(message4));

            // Native Collections Tests
            Vector3[] structData = { Vector3.one, Vector3.zero, Vector3.up, Vector3.down };
            using (var nativeArray = new NativeArray <Vector3>(structData, Allocator.Temp))
            {
                var nativeArrayTestMessageReceiver = default(byte[]);
                channel2.OnMessage = bytes => { nativeArrayTestMessageReceiver = bytes; };

                // Native Array
                var message5 = nativeArray;
                Assert.That(message5.IsCreated, Is.True);
                nativeArrayTestMessageReceiver = null;
                channel1.Send(message5);
                ExecutePendingTasksWithTimeout(ref nativeArrayTestMessageReceiver, millisecondTimeout);
                Assert.That(NativeArrayMemCmp(message5, nativeArrayTestMessageReceiver), Is.True, "Elements of the received message are not the same as the original message.");

                // Native Slice
                var message6 = nativeArray.Slice();
                nativeArrayTestMessageReceiver = null;
                channel1.Send(message6);
                ExecutePendingTasksWithTimeout(ref nativeArrayTestMessageReceiver, millisecondTimeout);
                Assert.That(NativeArrayMemCmp(message6, nativeArrayTestMessageReceiver), Is.True, "Elements of the received message are not the same as the original message.");

#if UNITY_2021_1_OR_NEWER
                // NativeArray.ReadOnly
                var message7 = nativeArray.AsReadOnly();
                nativeArrayTestMessageReceiver = null;
                channel1.Send(message7);
                ExecutePendingTasksWithTimeout(ref nativeArrayTestMessageReceiver, millisecondTimeout);
                Assert.That(NativeArrayMemCmp(message7, nativeArrayTestMessageReceiver), Is.True, "Elements of the received message are not the same as the original message.");
#endif // UNITY_2021_1_OR_NEWER
            }

            test.component.Dispose();
            Object.DestroyImmediate(test.gameObject);
        }
Exemplo n.º 5
0
        public IEnumerator SendAndReceiveMessage()
        {
            var test  = new MonoBehaviourTest <SignalingPeers>();
            var label = "test";

            RTCDataChannel channel1 = test.component.CreateDataChannel(0, label);

            Assert.That(channel1, Is.Not.Null);
            yield return(test);

            var op1 = new WaitUntilWithTimeout(() => test.component.GetDataChannelList(1).Count > 0, 5000);

            yield return(op1);

            RTCDataChannel channel2 = test.component.GetDataChannelList(1)[0];

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

            Assert.That(channel1.ReadyState, Is.EqualTo(RTCDataChannelState.Open));
            Assert.That(channel2.ReadyState, Is.EqualTo(RTCDataChannelState.Open));
            Assert.That(channel1.Label, Is.EqualTo(channel2.Label));
            Assert.That(channel1.Id, Is.EqualTo(channel2.Id));

            // send string
            const string message1 = "hello";
            string       message2 = null;

            channel2.OnMessage = bytes => { message2 = System.Text.Encoding.UTF8.GetString(bytes); };
            channel1.Send(message1);
            var op10 = new WaitUntilWithTimeout(() => !string.IsNullOrEmpty(message2), 5000);

            yield return(op10);

            Assert.That(op10.IsCompleted, Is.True);
            Assert.That(message1, Is.EqualTo(message2));

            // send byte array
            byte[] message3 = { 1, 2, 3 };
            byte[] message4 = null;
            channel2.OnMessage = bytes => { message4 = bytes; };
            channel1.Send(message3);
            var op11 = new WaitUntilWithTimeout(() => message4 != null, 5000);

            yield return(op11);

            Assert.That(op11.IsCompleted, Is.True);
            Assert.That(message3, Is.EqualTo(message4));

            // Native Array

            // Native Arrays that are declared in tests that use IEnumerator seem to have some oddities about them
            // they tend to dispose themselves on yields so we recreate the array as needed.

            byte[] comparisonBuffer = { 1, 2, 3 };
            var    nativeArrayTestMessageReceiver = default(byte[]);

            using (var message5 = new NativeArray <byte>(comparisonBuffer, Allocator.Temp))
            {
                Assert.That(message5.IsCreated, Is.True);
                // Only needs to be set once as it will be reused.
                channel2.OnMessage = bytes => { nativeArrayTestMessageReceiver = bytes; };
                channel1.Send(message5);
            }
            var op12 = new WaitUntilWithTimeout(() => nativeArrayTestMessageReceiver != null, 5000);

            yield return(op12);

            Assert.That(op12.IsCompleted, Is.True);
            Assert.That(comparisonBuffer, Is.EqualTo(nativeArrayTestMessageReceiver));

            // Native Slice
            using (var nativeArray = new NativeArray <byte>(comparisonBuffer, Allocator.Temp))
            {
                Assert.That(nativeArray.IsCreated, Is.True);
                var message6 = nativeArray.Slice();
                nativeArrayTestMessageReceiver = null;
                channel1.Send(message6);
            }
            var op13 = new WaitUntilWithTimeout(() => nativeArrayTestMessageReceiver != null, 5000);

            yield return(op13);

            Assert.That(op13.IsCompleted, Is.True);
            Assert.That(comparisonBuffer, Is.EqualTo(nativeArrayTestMessageReceiver));

#if UNITY_2021_1_OR_NEWER
            // NativeArray.ReadOnly
            using (var nativeArray = new NativeArray <byte>(comparisonBuffer, Allocator.Temp))
            {
                Assert.That(nativeArray.IsCreated, Is.True);
                var message7 = nativeArray.AsReadOnly();
                nativeArrayTestMessageReceiver = null;
                channel1.Send(message7);
            }
            var op14 = new WaitUntilWithTimeout(() => nativeArrayTestMessageReceiver != null, 5000);
            yield return(op14);

            Assert.That(op14.IsCompleted, Is.True);
            Assert.That(comparisonBuffer, Is.EqualTo(nativeArrayTestMessageReceiver));
#endif // UNITY_2020_1_OR_NEWER

            test.component.Dispose();
            Object.DestroyImmediate(test.gameObject);
        }
Exemplo n.º 6
0
 public IEnumerator AllChildrenNonRequired_LosingAllData_TriggersGroupLoss_AndReacquire()
 {
     var test = new MonoBehaviourTest<AllChildrenNonRequiredDataLossTest>();
     test.component.ReAcquireOnLoss = true;
     yield return test;
 }