123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897 |
- using System;
- using System.Linq;
- using System.Collections;
- using NUnit.Framework;
- using Unity.RenderStreaming.RuntimeTest.Signaling;
- using Unity.WebRTC;
- using UnityEngine;
- using UnityEngine.TestTools;
- namespace Unity.RenderStreaming.RuntimeTest
- {
- enum TestMode
- {
- PrivateMode,
- PublicMode
- }
- static class SignalingManagerInternalExtension
- {
- public static RTCRtpTransceiver AddSenderTrack(this SignalingManagerInternal target, string connectionId, MediaStreamTrack track)
- {
- RTCRtpTransceiverInit init = new RTCRtpTransceiverInit() { direction = RTCRtpTransceiverDirection.SendOnly };
- return target.AddTransceiver(connectionId, track, init);
- }
- }
- class SignalingManagerInternalTest
- {
- class MyMonoBehaviourTest : MonoBehaviour, IMonoBehaviourTest
- {
- public bool IsTestFinished
- {
- get { return true; }
- }
- }
- private MonoBehaviourTest<MyMonoBehaviourTest> test;
- [SetUp]
- public void SetUp()
- {
- test = new MonoBehaviourTest<MyMonoBehaviourTest>();
- }
- [TearDown]
- public void TearDown()
- {
- test.component.StopAllCoroutines();
- UnityEngine.Object.Destroy(test.gameObject);
- }
- // workaround: More time for SetDescription process
- const float ResendOfferInterval = 3f;
- private RenderStreamingDependencies CreateDependencies()
- {
- return new RenderStreamingDependencies
- {
- signaling = new MockSignaling(),
- config = new RTCConfiguration
- {
- iceServers = new[] { new RTCIceServer { urls = new[] { "stun:stun.l.google.com:19302" } } },
- },
- startCoroutine = test.component.StartCoroutine,
- stopCoroutine = test.component.StopCoroutine,
- resentOfferInterval = ResendOfferInterval,
- };
- }
- [TestCase(TestMode.PublicMode, ExpectedResult = null)]
- [TestCase(TestMode.PrivateMode, ExpectedResult = null)]
- [UnityTest, Timeout(10000)]
- public IEnumerator Construct(TestMode mode)
- {
- MockSignaling.Reset(mode == TestMode.PrivateMode);
- var dependencies = CreateDependencies();
- var target = new SignalingManagerInternal(ref dependencies);
- bool isStarted = false;
- target.onStart += () => { isStarted = true; };
- yield return new WaitUntil(() => isStarted);
- Assert.That(isStarted, Is.True);
- target.Dispose();
- }
- [TestCase(TestMode.PublicMode, ExpectedResult = null)]
- [TestCase(TestMode.PrivateMode, ExpectedResult = null)]
- [UnityTest, Timeout(10000)]
- public IEnumerator ConstructMultiple(TestMode mode)
- {
- MockSignaling.Reset(mode == TestMode.PrivateMode);
- var dependencies1 = CreateDependencies();
- var dependencies2 = CreateDependencies();
- var target1 = new SignalingManagerInternal(ref dependencies1);
- var target2 = new SignalingManagerInternal(ref dependencies2);
- bool isStarted1 = false;
- bool isStarted2 = false;
- target1.onStart += () => { isStarted1 = true; };
- target2.onStart += () => { isStarted2 = true; };
- yield return new WaitUntil(() => isStarted1);
- yield return new WaitUntil(() => isStarted2);
- Assert.That(isStarted1, Is.True);
- Assert.That(isStarted2, Is.True);
- target1.Dispose();
- target2.Dispose();
- }
- [TestCase(TestMode.PublicMode, ExpectedResult = null)]
- [TestCase(TestMode.PrivateMode, ExpectedResult = null)]
- [UnityTest, Timeout(10000)]
- public IEnumerator OpenConnection(TestMode mode)
- {
- MockSignaling.Reset(mode == TestMode.PrivateMode);
- var dependencies = CreateDependencies();
- var target = new SignalingManagerInternal(ref dependencies);
- bool isStarted = false;
- target.onStart += () => { isStarted = true; };
- yield return new WaitUntil(() => isStarted);
- Assert.That(isStarted, Is.True);
- string connectionId = "12345";
- Assert.That(target.ExistConnection(connectionId), Is.False);
- target.CreateConnection(connectionId);
- bool isCreatedConnection = false;
- target.onCreatedConnection += _ => { isCreatedConnection = true; };
- yield return new WaitUntil(() => isCreatedConnection);
- Assert.That(isCreatedConnection, Is.True);
- Assert.That(target.ExistConnection(connectionId), Is.True);
- target.DeleteConnection(connectionId);
- bool isDeletedConnection = false;
- target.onDeletedConnection += _ => { isDeletedConnection = true; };
- yield return new WaitUntil(() => isDeletedConnection);
- Assert.That(isDeletedConnection, Is.True);
- Assert.That(target.ExistConnection(connectionId), Is.False);
- target.Dispose();
- }
- [TestCase(TestMode.PublicMode, ExpectedResult = null)]
- [TestCase(TestMode.PrivateMode, ExpectedResult = null)]
- [UnityTest, Timeout(10000)]
- public IEnumerator OpenConnectionThrowException(TestMode mode)
- {
- MockSignaling.Reset(mode == TestMode.PrivateMode);
- var dependencies = CreateDependencies();
- var target = new SignalingManagerInternal(ref dependencies);
- bool isStarted = false;
- target.onStart += () => { isStarted = true; };
- yield return new WaitUntil(() => isStarted);
- Assert.That(isStarted, Is.True);
- Assert.That(() => target.CreateConnection(null), Throws.TypeOf<ArgumentException>());
- Assert.That(() => target.CreateConnection(string.Empty), Throws.TypeOf<ArgumentException>());
- target.Dispose();
- }
- //todo:: crash in dispose process on standalone linux
- [TestCase(TestMode.PublicMode, ExpectedResult = null)]
- [TestCase(TestMode.PrivateMode, ExpectedResult = null)]
- [UnityTest, Timeout(10000)]
- [UnityPlatform(exclude = new[] { RuntimePlatform.LinuxPlayer })]
- public IEnumerator AddTrack(TestMode mode)
- {
- MockSignaling.Reset(mode == TestMode.PrivateMode);
- var dependencies = CreateDependencies();
- var target = new SignalingManagerInternal(ref dependencies);
- bool isStarted = false;
- target.onStart += () => { isStarted = true; };
- yield return new WaitUntil(() => isStarted);
- Assert.That(isStarted, Is.True);
- var connectionId = "12345";
- bool isCreatedConnection = false;
- target.onCreatedConnection += _ => { isCreatedConnection = true; };
- target.CreateConnection(connectionId);
- yield return new WaitUntil(() => isCreatedConnection);
- Assert.That(isCreatedConnection, Is.True);
- Assert.That(target.GetTransceivers(connectionId).Count(), Is.EqualTo(0));
- var camObj = new GameObject("Camera");
- var camera = camObj.AddComponent<Camera>();
- VideoStreamTrack track = camera.CaptureStreamTrack(1280, 720);
- var transceiver = target.AddSenderTrack(connectionId, track);
- Assert.That(transceiver.Direction, Is.EqualTo(RTCRtpTransceiverDirection.SendOnly));
- Assert.That(target.GetTransceivers(connectionId).Count(), Is.EqualTo(1));
- target.RemoveSenderTrack(connectionId, track);
- bool isDeletedConnection = false;
- target.onDeletedConnection += _ => { isDeletedConnection = true; };
- target.DeleteConnection(connectionId);
- yield return new WaitUntil(() => isDeletedConnection);
- Assert.That(isDeletedConnection, Is.True);
- target.Dispose();
- track.Dispose();
- UnityEngine.Object.Destroy(camObj);
- }
- [TestCase(TestMode.PublicMode, ExpectedResult = null)]
- [TestCase(TestMode.PrivateMode, ExpectedResult = null)]
- [UnityTest]
- public IEnumerator AddTrackThrowException(TestMode mode)
- {
- MockSignaling.Reset(mode == TestMode.PrivateMode);
- var dependencies = CreateDependencies();
- var target = new SignalingManagerInternal(ref dependencies);
- bool isStarted = false;
- target.onStart += () => { isStarted = true; };
- yield return new WaitUntil(() => isStarted);
- Assert.That(isStarted, Is.True);
- var connectionId = "12345";
- target.CreateConnection(connectionId);
- bool isCreatedConnection = false;
- target.onCreatedConnection += _ => { isCreatedConnection = true; };
- yield return new WaitUntil(() => isCreatedConnection);
- Assert.That(isCreatedConnection, Is.True);
- Assert.That(() => target.AddSenderTrack(null, null), Throws.TypeOf<ArgumentNullException>());
- Assert.That(() => target.AddSenderTrack(connectionId, null), Throws.TypeOf<ArgumentNullException>());
- Assert.That(() => target.RemoveSenderTrack(null, null), Throws.TypeOf<ArgumentNullException>());
- Assert.That(() => target.RemoveSenderTrack(connectionId, null), Throws.TypeOf<InvalidOperationException>());
- target.DeleteConnection(connectionId);
- bool isDeletedConnection = false;
- target.onDeletedConnection += _ => { isDeletedConnection = true; };
- yield return new WaitUntil(() => isDeletedConnection);
- Assert.That(isDeletedConnection, Is.True);
- target.Dispose();
- }
- //todo:: crash in dispose process on standalone linux
- [TestCase(TestMode.PublicMode, ExpectedResult = null)]
- [TestCase(TestMode.PrivateMode, ExpectedResult = null)]
- [UnityTest, Timeout(10000)]
- [UnityPlatform(exclude = new[] { RuntimePlatform.LinuxPlayer })]
- public IEnumerator AddTrackMultiple(TestMode mode)
- {
- MockSignaling.Reset(mode == TestMode.PrivateMode);
- var dependencies = CreateDependencies();
- var target = new SignalingManagerInternal(ref dependencies);
- bool isStarted = false;
- target.onStart += () => { isStarted = true; };
- yield return new WaitUntil(() => isStarted);
- Assert.That(isStarted, Is.True);
- var connectionId = "12345";
- target.CreateConnection(connectionId);
- bool isCreatedConnection = false;
- target.onCreatedConnection += _ => { isCreatedConnection = true; };
- yield return new WaitUntil(() => isCreatedConnection);
- Assert.That(isCreatedConnection, Is.True);
- var camObj = new GameObject("Camera");
- var camera = camObj.AddComponent<Camera>();
- VideoStreamTrack track = camera.CaptureStreamTrack(1280, 720);
- var transceiver1 = target.AddSenderTrack(connectionId, track);
- Assert.That(transceiver1.Direction, Is.EqualTo(RTCRtpTransceiverDirection.SendOnly));
- var camObj2 = new GameObject("Camera2");
- var camera2 = camObj2.AddComponent<Camera>();
- VideoStreamTrack track2 = camera2.CaptureStreamTrack(1280, 720);
- var transceiver2 = target.AddSenderTrack(connectionId, track2);
- Assert.That(transceiver2.Direction, Is.EqualTo(RTCRtpTransceiverDirection.SendOnly));
- target.DeleteConnection(connectionId);
- bool isDeletedConnection = false;
- target.onDeletedConnection += _ => { isDeletedConnection = true; };
- yield return new WaitUntil(() => isDeletedConnection);
- Assert.That(isDeletedConnection, Is.True);
- target.Dispose();
- track.Dispose();
- track2.Dispose();
- UnityEngine.Object.Destroy(camObj);
- UnityEngine.Object.Destroy(camObj2);
- }
- [TestCase(TestMode.PublicMode, ExpectedResult = null)]
- [TestCase(TestMode.PrivateMode, ExpectedResult = null)]
- [UnityTest, Timeout(10000)]
- public IEnumerator CreateChannel(TestMode mode)
- {
- MockSignaling.Reset(mode == TestMode.PrivateMode);
- var dependencies = CreateDependencies();
- var target = new SignalingManagerInternal(ref dependencies);
- bool isStarted = false;
- target.onStart += () => { isStarted = true; };
- yield return new WaitUntil(() => isStarted);
- Assert.That(isStarted, Is.True);
- var connectionId = "12345";
- target.CreateConnection(connectionId);
- bool isCreatedConnection = false;
- target.onCreatedConnection += _ => { isCreatedConnection = true; };
- yield return new WaitUntil(() => isCreatedConnection);
- string channelName = "test";
- var channel = target.CreateChannel(connectionId, channelName);
- Assert.That(channel.Label, Is.EqualTo(channelName));
- target.DeleteConnection(connectionId);
- bool isDeletedConnection = false;
- target.onDeletedConnection += _ => { isDeletedConnection = true; };
- yield return new WaitUntil(() => isDeletedConnection);
- Assert.That(isDeletedConnection, Is.True);
- target.Dispose();
- channel.Dispose();
- }
- //todo:: crash in dispose process on standalone linux
- [UnityTest, Timeout(10000)]
- [UnityPlatform(exclude = new[] { RuntimePlatform.LinuxPlayer, RuntimePlatform.Android })]
- public IEnumerator OnAddReceiverPrivateMode()
- {
- MockSignaling.Reset(true);
- var dependencies1 = CreateDependencies();
- var dependencies2 = CreateDependencies();
- var target1 = new SignalingManagerInternal(ref dependencies1);
- var target2 = new SignalingManagerInternal(ref dependencies2);
- bool isStarted1 = false;
- bool isStarted2 = false;
- target1.onStart += () => { isStarted1 = true; };
- target2.onStart += () => { isStarted2 = true; };
- yield return new WaitUntil(() => isStarted1 && isStarted2);
- Assert.That(isStarted1, Is.True);
- Assert.That(isStarted2, Is.True);
- bool isCreatedConnection1 = false;
- bool isCreatedConnection2 = false;
- target1.onCreatedConnection += _ => { isCreatedConnection1 = true; };
- target2.onCreatedConnection += _ => { isCreatedConnection2 = true; };
- var connectionId = "12345";
- // target1 is Receiver in private mode
- target1.CreateConnection(connectionId);
- yield return new WaitUntil(() => isCreatedConnection1);
- Assert.That(isCreatedConnection1, Is.True);
- // target2 is sender in private mode
- target2.CreateConnection(connectionId);
- yield return new WaitUntil(() => isCreatedConnection2);
- Assert.That(isCreatedConnection2, Is.True);
- bool isAddReceiver1 = false;
- bool isGotAnswer2 = false;
- target1.onAddTransceiver += (_, receiver) => { isAddReceiver1 = true; };
- target1.onGotOffer += (_, sdp) => { target1.SendAnswer(connectionId); };
- target2.onGotAnswer += (_, sdp) => { isGotAnswer2 = true; };
- var camObj = new GameObject("Camera");
- var camera = camObj.AddComponent<Camera>();
- VideoStreamTrack track = camera.CaptureStreamTrack(1280, 720);
- // send offer automatically after adding a Track
- var transceiver = target2.AddSenderTrack(connectionId, track);
- Assert.That(transceiver, Is.Not.Null);
- Assert.That(transceiver.Direction, Is.EqualTo(RTCRtpTransceiverDirection.SendOnly));
- yield return new WaitUntil(() => isAddReceiver1 && isGotAnswer2);
- Assert.That(isAddReceiver1, Is.True);
- Assert.That(isGotAnswer2, Is.True);
- target1.DeleteConnection(connectionId);
- target2.DeleteConnection(connectionId);
- bool isDeletedConnection1 = false;
- bool isDeletedConnection2 = false;
- target1.onDeletedConnection += _ => { isDeletedConnection1 = true; };
- target2.onDeletedConnection += _ => { isDeletedConnection2 = true; };
- yield return new WaitUntil(() => isDeletedConnection1 && isDeletedConnection2);
- Assert.That(isDeletedConnection1, Is.True);
- Assert.That(isDeletedConnection2, Is.True);
- target1.Dispose();
- target2.Dispose();
- track.Dispose();
- UnityEngine.Object.Destroy(camObj);
- }
- //todo:: crash in dispose process on standalone linux
- [UnityTest, Timeout(10000)]
- [UnityPlatform(exclude = new[] { RuntimePlatform.LinuxPlayer, RuntimePlatform.Android })]
- public IEnumerator OnAddReceiverPublicMode()
- {
- MockSignaling.Reset(false);
- var dependencies1 = CreateDependencies();
- var dependencies2 = CreateDependencies();
- var target1 = new SignalingManagerInternal(ref dependencies1);
- var target2 = new SignalingManagerInternal(ref dependencies2);
- bool isStarted1 = false;
- bool isStarted2 = false;
- target1.onStart += () => { isStarted1 = true; };
- target2.onStart += () => { isStarted2 = true; };
- yield return new WaitUntil(() => isStarted1 && isStarted2);
- Assert.That(isStarted1, Is.True);
- Assert.That(isStarted2, Is.True);
- bool isCreatedConnection1 = false;
- bool isOnGotOffer2 = false;
- target1.onCreatedConnection += _ => { isCreatedConnection1 = true; };
- target2.onGotOffer += (_, sdp) => { isOnGotOffer2 = true; };
- var connectionId = "12345";
- // target1 is Receiver in public mode
- target1.CreateConnection(connectionId);
- yield return new WaitUntil(() => isCreatedConnection1);
- Assert.That(isCreatedConnection1, Is.True);
- RTCRtpTransceiverInit init = new RTCRtpTransceiverInit()
- {
- direction = RTCRtpTransceiverDirection.RecvOnly
- };
- target1.AddTransceiver(connectionId, TrackKind.Video, init);
- // target2 is sender in private mode
- yield return new WaitUntil(() => isOnGotOffer2);
- Assert.That(isOnGotOffer2, Is.True);
- bool isAddReceiver1 = false;
- bool isGotAnswer1 = false;
- target1.onAddTransceiver += (_, receiver) => { isAddReceiver1 = true; };
- target1.onGotAnswer += (_, sdp) => { isGotAnswer1 = true; };
- var camObj = new GameObject("Camera");
- var camera = camObj.AddComponent<Camera>();
- VideoStreamTrack track = camera.CaptureStreamTrack(1280, 720);
- var transceiver2 = target2.AddSenderTrack(connectionId, track);
- Assert.That(transceiver2.Direction, Is.EqualTo(RTCRtpTransceiverDirection.SendOnly));
- target2.SendAnswer(connectionId);
- yield return new WaitUntil(() => isAddReceiver1 & isGotAnswer1);
- target1.DeleteConnection(connectionId);
- target2.DeleteConnection(connectionId);
- bool isDeletedConnection1 = false;
- bool isDeletedConnection2 = false;
- target1.onDeletedConnection += _ => { isDeletedConnection1 = true; };
- target2.onDeletedConnection += _ => { isDeletedConnection2 = true; };
- yield return new WaitUntil(() => isDeletedConnection1 && isDeletedConnection2);
- Assert.That(isDeletedConnection1, Is.True);
- Assert.That(isDeletedConnection2, Is.True);
- target1.Dispose();
- target2.Dispose();
- track.Dispose();
- UnityEngine.Object.DestroyImmediate(camObj);
- }
- [UnityTest, Timeout(10000)]
- public IEnumerator OnAddChannelPrivateMode()
- {
- MockSignaling.Reset(true);
- var dependencies1 = CreateDependencies();
- var dependencies2 = CreateDependencies();
- var target1 = new SignalingManagerInternal(ref dependencies1);
- var target2 = new SignalingManagerInternal(ref dependencies2);
- bool isStarted1 = false;
- bool isStarted2 = false;
- target1.onStart += () => { isStarted1 = true; };
- target2.onStart += () => { isStarted2 = true; };
- yield return new WaitUntil(() => isStarted1 && isStarted2);
- bool isCreatedConnection1 = false;
- bool isCreatedConnection2 = false;
- target1.onCreatedConnection += _ => { isCreatedConnection1 = true; };
- target2.onCreatedConnection += _ => { isCreatedConnection2 = true; };
- var connectionId = "12345";
- // target1 is Receiver in private mode
- target1.CreateConnection(connectionId);
- yield return new WaitUntil(() => isCreatedConnection1);
- // target2 is sender in private mode
- target2.CreateConnection(connectionId);
- yield return new WaitUntil(() => isCreatedConnection2);
- bool isAddChannel1 = false;
- bool isGotOffer1 = false;
- bool isGotAnswer2 = false;
- target1.onAddChannel += (_, _channel) => { isAddChannel1 = true; };
- target1.onGotOffer += (_, sdp) => { isGotOffer1 = true; };
- target2.onGotAnswer += (_, sdp) => { isGotAnswer2 = true; };
- // send offer automatically after creating channel
- RTCDataChannel channel = target2.CreateChannel(connectionId, "test");
- Assert.That(channel, Is.Not.Null);
- yield return new WaitUntil(() => isGotOffer1);
- Assert.That(isGotOffer1, Is.True);
- target1.SendAnswer(connectionId);
- yield return new WaitUntil(() => isAddChannel1 && isGotAnswer2);
- Assert.That(isAddChannel1, Is.True);
- Assert.That(isGotAnswer2, Is.True);
- target1.DeleteConnection(connectionId);
- target2.DeleteConnection(connectionId);
- bool isDeletedConnection1 = false;
- bool isDeletedConnection2 = false;
- target1.onDeletedConnection += _ => { isDeletedConnection1 = true; };
- target2.onDeletedConnection += _ => { isDeletedConnection2 = true; };
- yield return new WaitUntil(() => isDeletedConnection1 && isDeletedConnection2);
- Assert.That(isDeletedConnection1, Is.True);
- Assert.That(isDeletedConnection2, Is.True);
- target1.Dispose();
- target2.Dispose();
- }
- [UnityTest, Timeout(10000), LongRunning]
- public IEnumerator SendOfferThrowExceptionPrivateMode()
- {
- MockSignaling.Reset(true);
- var dependencies1 = CreateDependencies();
- var dependencies2 = CreateDependencies();
- var target1 = new SignalingManagerInternal(ref dependencies1);
- var target2 = new SignalingManagerInternal(ref dependencies2);
- bool isStarted1 = false;
- bool isStarted2 = false;
- target1.onStart += () => { isStarted1 = true; };
- target2.onStart += () => { isStarted2 = true; };
- yield return new WaitUntil(() => isStarted1 && isStarted2);
- bool isCreatedConnection1 = false;
- bool isCreatedConnection2 = false;
- target1.onCreatedConnection += _ => { isCreatedConnection1 = true; };
- target2.onCreatedConnection += _ => { isCreatedConnection2 = true; };
- var connectionId = "12345";
- // target1 is Receiver in private mode
- target1.CreateConnection(connectionId);
- yield return new WaitUntil(() => isCreatedConnection1);
- // target2 is sender in private mode
- target2.CreateConnection(connectionId);
- yield return new WaitUntil(() => isCreatedConnection2);
- bool isGotOffer1 = false;
- bool isGotAnswer2 = false;
- target1.onGotOffer += (_, sdp) => { isGotOffer1 = true; };
- target2.onGotAnswer += (_, sdp) => { isGotAnswer2 = true; };
- target2.SendOffer(connectionId);
- // each peer are not stable, signaling process not complete.
- yield return new WaitUntil(() => isGotOffer1);
- Assert.That(target1.IsStable(connectionId), Is.False);
- Assert.That(target2.IsStable(connectionId), Is.False);
- Assert.That(() => target1.SendOffer(connectionId), Throws.TypeOf<InvalidOperationException>());
- target1.SendAnswer(connectionId);
- yield return new WaitUntil(() => isGotAnswer2);
- Assert.That(isGotAnswer2, Is.True);
- // If target1 processes resent Offer from target2, target1 is not stable.
- Assert.That(target2.IsStable(connectionId), Is.True);
- target1.DeleteConnection(connectionId);
- target2.DeleteConnection(connectionId);
- bool isDeletedConnection1 = false;
- bool isDeletedConnection2 = false;
- target1.onDeletedConnection += _ => { isDeletedConnection1 = true; };
- target2.onDeletedConnection += _ => { isDeletedConnection2 = true; };
- yield return new WaitUntil(() => isDeletedConnection1 && isDeletedConnection2);
- Assert.That(isDeletedConnection1, Is.True);
- Assert.That(isDeletedConnection2, Is.True);
- target1.Dispose();
- target2.Dispose();
- }
- [UnityTest, Timeout(30000), LongRunning]
- public IEnumerator SwapTransceiverPrivateMode()
- {
- MockSignaling.Reset(true);
- var dependencies1 = CreateDependencies();
- var dependencies2 = CreateDependencies();
- var target1 = new SignalingManagerInternal(ref dependencies1);
- var target2 = new SignalingManagerInternal(ref dependencies2);
- bool isStarted1 = false;
- bool isStarted2 = false;
- target1.onStart += () => { isStarted1 = true; };
- target2.onStart += () => { isStarted2 = true; };
- yield return new WaitUntil(() => isStarted1 && isStarted2);
- Assert.That(isStarted1, Is.True);
- Assert.That(isStarted2, Is.True);
- bool isCreatedConnection1 = false;
- bool isCreatedConnection2 = false;
- target1.onCreatedConnection += _ => { isCreatedConnection1 = true; };
- target2.onCreatedConnection += _ => { isCreatedConnection2 = true; };
- var connectionId = "12345";
- // target1 has impolite peer (request first)
- target1.CreateConnection(connectionId);
- yield return new WaitUntil(() => isCreatedConnection1);
- Assert.That(isCreatedConnection1, Is.True);
- // target2 has polite peer (request second)
- target2.CreateConnection(connectionId);
- yield return new WaitUntil(() => isCreatedConnection2);
- Assert.That(isCreatedConnection2, Is.True);
- bool isGotOffer1 = false;
- bool isGotOffer2 = false;
- bool isGotAnswer1 = false;
- target1.onGotOffer += (_, sdp) => { isGotOffer1 = true; };
- target2.onGotOffer += (_, sdp) => { isGotOffer2 = true; };
- target1.onGotAnswer += (_, sdp) => { isGotAnswer1 = true; };
- RTCRtpTransceiverInit init1 = new RTCRtpTransceiverInit()
- {
- direction = RTCRtpTransceiverDirection.SendOnly
- };
- RTCRtpTransceiverInit init2 = new RTCRtpTransceiverInit()
- {
- direction = RTCRtpTransceiverDirection.SendOnly
- };
- target1.AddTransceiver(connectionId, TrackKind.Audio, init1);
- target2.AddTransceiver(connectionId, TrackKind.Audio, init2);
- // check each target invoke onGotOffer
- yield return new WaitForSeconds(ResendOfferInterval * 5);
- // ignore offer because impolite peer
- Assert.That(isGotOffer1, Is.False, $"{nameof(isGotOffer1)} is not False.");
- // accept offer because polite peer
- Assert.That(isGotOffer2, Is.True, $"{nameof(isGotOffer2)} is not True.");
- target2.SendAnswer(connectionId);
- yield return new WaitUntil(() => isGotAnswer1);
- Assert.That(isGotAnswer1, Is.True, $"{nameof(isGotAnswer1)} is not True.");
- target1.DeleteConnection(connectionId);
- target2.DeleteConnection(connectionId);
- bool isDeletedConnection1 = false;
- bool isDeletedConnection2 = false;
- target1.onDeletedConnection += _ => { isDeletedConnection1 = true; };
- target2.onDeletedConnection += _ => { isDeletedConnection2 = true; };
- yield return new WaitUntil(() => isDeletedConnection1 && isDeletedConnection2);
- Assert.That(isDeletedConnection1, Is.True, $"{nameof(isDeletedConnection1)} is not True.");
- Assert.That(isDeletedConnection2, Is.True, $"{nameof(isDeletedConnection2)} is not True.");
- target1.Dispose();
- target2.Dispose();
- }
- [TestCase(TestMode.PublicMode, ExpectedResult = null)]
- [TestCase(TestMode.PrivateMode, ExpectedResult = null)]
- [UnityTest, Timeout(30000), LongRunning]
- public IEnumerator ResendOfferUntilGotAnswer(TestMode mode)
- {
- MockSignaling.Reset(mode == TestMode.PrivateMode);
- var dependencies1 = CreateDependencies();
- var dependencies2 = CreateDependencies();
- var target1 = new SignalingManagerInternal(ref dependencies1);
- var target2 = new SignalingManagerInternal(ref dependencies2);
- bool isStarted1 = false;
- bool isStarted2 = false;
- target1.onStart += () => { isStarted1 = true; };
- target2.onStart += () => { isStarted2 = true; };
- yield return new WaitUntil(() => isStarted1 && isStarted2);
- bool isCreatedConnection1 = false;
- bool isCreatedConnection2 = false;
- target1.onCreatedConnection += _ => { isCreatedConnection1 = true; };
- target2.onCreatedConnection += _ => { isCreatedConnection2 = true; };
- var connectionId = "12345";
- target1.CreateConnection(connectionId);
- yield return new WaitUntil(() => isCreatedConnection1);
- Assert.That(isCreatedConnection1, Is.True);
- target2.CreateConnection(connectionId);
- yield return new WaitUntil(() => isCreatedConnection2);
- Assert.That(isCreatedConnection2, Is.True);
- int countGotOffer2 = 0;
- target2.onGotOffer += (_, sdp) => { countGotOffer2++; };
- target1.SendOffer(connectionId);
- yield return new WaitUntil(() => countGotOffer2 > 1);
- Assert.That(countGotOffer2, Is.GreaterThan(1));
- bool isGotAnswer1 = false;
- target1.onGotAnswer += (_, sdp) => { isGotAnswer1 = true; };
- target2.SendAnswer(connectionId);
- yield return new WaitUntil(() => isGotAnswer1);
- Assert.That(isGotAnswer1, Is.True);
- yield return new WaitForSeconds(ResendOfferInterval * 2);
- var currentCount = countGotOffer2;
- yield return new WaitForSeconds(ResendOfferInterval * 2);
- Assert.That(countGotOffer2, Is.EqualTo(currentCount),
- $"{nameof(currentCount)} is not Equal {nameof(countGotOffer2)}");
- target1.DeleteConnection(connectionId);
- target2.DeleteConnection(connectionId);
- bool isDeletedConnection1 = false;
- bool isDeletedConnection2 = false;
- target1.onDeletedConnection += _ => { isDeletedConnection1 = true; };
- target2.onDeletedConnection += _ => { isDeletedConnection2 = true; };
- yield return new WaitUntil(() => isDeletedConnection1 && isDeletedConnection2);
- Assert.That(isDeletedConnection1, Is.True, $"{nameof(isDeletedConnection1)} is not True.");
- Assert.That(isDeletedConnection2, Is.True, $"{nameof(isDeletedConnection2)} is not True.");
- target1.Dispose();
- target2.Dispose();
- }
- [TestCase(TestMode.PublicMode, ExpectedResult = null)]
- [TestCase(TestMode.PrivateMode, ExpectedResult = null)]
- [UnityTest, Timeout(30000), LongRunning]
- public IEnumerator DeleteFailedPeers(TestMode mode)
- {
- MockSignaling.Reset(mode == TestMode.PrivateMode);
- var dependencies1 = CreateDependencies();
- var dependencies2 = CreateDependencies();
- var target1 = new SignalingManagerInternal(ref dependencies1);
- var target2 = new SignalingManagerInternal(ref dependencies2);
- bool isStarted1 = false;
- bool isStarted2 = false;
- target1.onStart += () => { isStarted1 = true; };
- target2.onStart += () => { isStarted2 = true; };
- yield return new WaitUntil(() => isStarted1 && isStarted2);
- Assert.That(isStarted1, Is.True);
- Assert.That(isStarted2, Is.True);
- bool isCreatedConnection1 = false;
- bool isCreatedConnection2 = false;
- target1.onCreatedConnection += _ => { isCreatedConnection1 = true; };
- target2.onCreatedConnection += _ => { isCreatedConnection2 = true; };
- var connectionId = "12345";
- // target1 has impolite peer (request first)
- target1.CreateConnection(connectionId);
- yield return new WaitUntil(() => isCreatedConnection1);
- Assert.That(isCreatedConnection1, Is.True);
- target2.CreateConnection(connectionId);
- yield return new WaitUntil(() => isCreatedConnection2);
- Assert.That(isCreatedConnection2, Is.True);
- bool isGotOffer2 = false;
- bool isGotAnswer1 = false;
- target2.onGotOffer += (_, sdp) => { isGotOffer2 = true; };
- target1.onGotAnswer += (_, sdp) => { isGotAnswer1 = true; };
- RTCRtpTransceiverInit init1 = new RTCRtpTransceiverInit()
- {
- direction = RTCRtpTransceiverDirection.SendOnly
- };
- target1.AddTransceiver(connectionId, TrackKind.Video, init1);
- yield return new WaitUntil(() => isGotOffer2);
- Assert.That(isGotOffer2, Is.True, $"{nameof(isGotOffer2)} is not True.");
- target2.SendAnswer(connectionId);
- yield return new WaitUntil(() => isGotAnswer1);
- Assert.That(isGotAnswer1, Is.True, $"{nameof(isGotAnswer1)} is not True.");
- // Improperly dispose of target1 to force failed state on target2
- target1.Dispose();
- bool isDeletedConnection2 = false;
- target2.onDeletedConnection += _ => { isDeletedConnection2 = true; };
- yield return new WaitUntil(() => isDeletedConnection2);
- Assert.That(isDeletedConnection2, Is.True, $"{nameof(isDeletedConnection2)} is not True.");
- target2.Dispose();
- }
- [UnityTest, Timeout(10000)]
- public IEnumerator ReNegotiationAfterReceivingFirstOffer()
- {
- MockSignaling.Reset(true);
- var dependencies1 = CreateDependencies();
- var dependencies2 = CreateDependencies();
- var target1 = new SignalingManagerInternal(ref dependencies1);
- var target2 = new SignalingManagerInternal(ref dependencies2);
- bool isStarted1 = false;
- bool isStarted2 = false;
- target1.onStart += () => { isStarted1 = true; };
- target2.onStart += () => { isStarted2 = true; };
- yield return new WaitUntil(() => isStarted1 && isStarted2);
- bool isCreatedConnection1 = false;
- bool isCreatedConnection2 = false;
- target1.onCreatedConnection += _ => { isCreatedConnection1 = true; };
- target2.onCreatedConnection += _ => { isCreatedConnection2 = true; };
- var connectionId = "12345";
- // target1 has impolite peer (request first)
- target1.CreateConnection(connectionId);
- yield return new WaitUntil(() => isCreatedConnection1);
- // target2 has polite peer (request second)
- target2.CreateConnection(connectionId);
- yield return new WaitUntil(() => isCreatedConnection2);
- bool isGotOffer1 = false;
- bool isGotOffer2 = false;
- bool isGotAnswer1 = false;
- bool isGotAnswer2 = false;
- target1.onGotOffer += (_, sdp) => { isGotOffer1 = true; };
- target2.onGotOffer += (_, sdp) => { isGotOffer2 = true; };
- target1.onGotAnswer += (_, sdp) => { isGotAnswer1 = true; };
- target2.onGotAnswer += (_, sdp) => { isGotAnswer2 = true; };
- var init1 = new RTCRtpTransceiverInit() { direction = RTCRtpTransceiverDirection.SendOnly };
- var init2 = new RTCRtpTransceiverInit() { direction = RTCRtpTransceiverDirection.RecvOnly };
- var init3 = new RTCRtpTransceiverInit() { direction = RTCRtpTransceiverDirection.SendOnly };
- var init4 = new RTCRtpTransceiverInit() { direction = RTCRtpTransceiverDirection.RecvOnly };
- target1.AddTransceiver(connectionId, TrackKind.Video, init1);
- target1.AddTransceiver(connectionId, TrackKind.Video, init2);
- target2.AddTransceiver(connectionId, TrackKind.Video, init3);
- target2.AddTransceiver(connectionId, TrackKind.Video, init4);
- yield return new WaitUntil(() => isGotOffer2);
- Assert.That(isGotOffer2, Is.True, $"{nameof(isGotOffer2)} is not True.");
- target2.SendAnswer(connectionId);
- yield return new WaitUntil(() => isGotAnswer1);
- Assert.That(isGotAnswer1, Is.True, $"{nameof(isGotAnswer1)} is not True.");
- yield return new WaitUntil(() => isGotOffer1);
- Assert.That(isGotOffer1, Is.True, $"{nameof(isGotOffer1)} is not True.");
- target1.SendAnswer(connectionId);
- yield return new WaitUntil(() => isGotAnswer2);
- Assert.That(isGotAnswer2, Is.True, $"{nameof(isGotAnswer2)} is not True.");
- target1.DeleteConnection(connectionId);
- target2.DeleteConnection(connectionId);
- bool isDeletedConnection1 = false;
- bool isDeletedConnection2 = false;
- target1.onDeletedConnection += _ => { isDeletedConnection1 = true; };
- target2.onDeletedConnection += _ => { isDeletedConnection2 = true; };
- yield return new WaitUntil(() => isDeletedConnection1 && isDeletedConnection2);
- Assert.That(isDeletedConnection1, Is.True, $"{nameof(isDeletedConnection1)} is not True.");
- Assert.That(isDeletedConnection2, Is.True, $"{nameof(isDeletedConnection1)} is not True.");
- target1.Dispose();
- target2.Dispose();
- }
- }
- }
|