PeerConnectionTest.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. using System.Collections;
  2. using NUnit.Framework;
  3. using Unity.WebRTC;
  4. using UnityEngine;
  5. using UnityEngine.TestTools;
  6. namespace Unity.RenderStreaming.RuntimeTest
  7. {
  8. class PeerConnectionTest
  9. {
  10. class MyMonoBehaviourTest : MonoBehaviour, IMonoBehaviourTest
  11. {
  12. public bool IsTestFinished
  13. {
  14. get { return true; }
  15. }
  16. }
  17. private const float ResendOfferInterval = 1.0f;
  18. private MonoBehaviourTest<MyMonoBehaviourTest> test;
  19. private RTCConfiguration config;
  20. [SetUp]
  21. public void SetUp()
  22. {
  23. test = new MonoBehaviourTest<MyMonoBehaviourTest>();
  24. config = new RTCConfiguration
  25. {
  26. iceServers = new[] { new RTCIceServer { urls = new[] { "stun:stun.l.google.com:19302" } } },
  27. };
  28. }
  29. [TearDown]
  30. public void TearDown()
  31. {
  32. test.component.StopAllCoroutines();
  33. Object.Destroy(test.gameObject);
  34. }
  35. [Test, Timeout(5000)]
  36. public void Construct()
  37. {
  38. var peer = new PeerConnection(true, config, ResendOfferInterval,
  39. test.component.StartCoroutine, test.component.StopCoroutine);
  40. Assert.That(peer, Is.Not.Null);
  41. var rtcPeer = peer.peer;
  42. Assert.That(rtcPeer, Is.Not.Null);
  43. Assert.That(rtcPeer.OnTrack, Is.Not.Null);
  44. Assert.That(rtcPeer.OnIceCandidate, Is.Not.Null);
  45. Assert.That(rtcPeer.OnNegotiationNeeded, Is.Not.Null);
  46. Assert.That(rtcPeer.OnDataChannel, Is.Not.Null);
  47. Assert.That(rtcPeer.OnConnectionStateChange, Is.Not.Null);
  48. peer.Dispose();
  49. }
  50. [UnityTest, Timeout(5000)]
  51. public IEnumerator CalledSendOfferWhenAddTrack()
  52. {
  53. var peer = new PeerConnection(true, config, ResendOfferInterval,
  54. test.component.StartCoroutine, test.component.StopCoroutine);
  55. Assert.That(peer, Is.Not.Null);
  56. var isGotSendOffer = false;
  57. RTCSessionDescription offerDesc = default;
  58. peer.SendOfferHandler += description =>
  59. {
  60. isGotSendOffer = true;
  61. offerDesc = description;
  62. };
  63. var track = new AudioStreamTrack();
  64. peer.peer.AddTrack(track);
  65. yield return new WaitUntil(() => isGotSendOffer);
  66. Assert.That(isGotSendOffer, Is.True);
  67. Assert.That(offerDesc.type, Is.EqualTo(RTCSdpType.Offer));
  68. Assert.That(offerDesc.sdp, Is.Not.Null.Or.Empty);
  69. track.Dispose();
  70. peer.Dispose();
  71. }
  72. [UnityTest, Timeout(5000)]
  73. public IEnumerator CalledSendOfferWhenAddTransceiver()
  74. {
  75. var peer = new PeerConnection(true, config, ResendOfferInterval,
  76. test.component.StartCoroutine, test.component.StopCoroutine);
  77. Assert.That(peer, Is.Not.Null);
  78. var isGotSendOffer = false;
  79. RTCSessionDescription offerDesc = default;
  80. peer.SendOfferHandler += description =>
  81. {
  82. isGotSendOffer = true;
  83. offerDesc = description;
  84. };
  85. var transceiver = peer.peer.AddTransceiver(TrackKind.Video);
  86. Assert.That(transceiver, Is.Not.Null);
  87. yield return new WaitUntil(() => isGotSendOffer);
  88. Assert.That(isGotSendOffer, Is.True);
  89. Assert.That(offerDesc.type, Is.EqualTo(RTCSdpType.Offer));
  90. Assert.That(offerDesc.sdp, Is.Not.Null.Or.Empty);
  91. transceiver.Dispose();
  92. peer.Dispose();
  93. }
  94. [UnityTest, Timeout(5000)]
  95. public IEnumerator CalledSendOfferWhenCreateChannel()
  96. {
  97. var peer = new PeerConnection(
  98. true,
  99. config,
  100. ResendOfferInterval,
  101. test.component.StartCoroutine,
  102. test.component.StopCoroutine
  103. );
  104. var isGotSendOffer = false;
  105. RTCSessionDescription offerDesc = default;
  106. peer.SendOfferHandler += description =>
  107. {
  108. isGotSendOffer = true;
  109. offerDesc = description;
  110. };
  111. var channel = peer.peer.CreateDataChannel("test");
  112. Assert.That(channel, Is.Not.Null);
  113. yield return new WaitUntil(() => isGotSendOffer);
  114. Assert.That(isGotSendOffer, Is.True);
  115. Assert.That(offerDesc.type, Is.EqualTo(RTCSdpType.Offer));
  116. Assert.That(offerDesc.sdp, Is.Not.Null.Or.Empty);
  117. channel.Dispose();
  118. peer.Dispose();
  119. }
  120. [UnityTest, Timeout(5000), LongRunning]
  121. public IEnumerator CalledSendOfferTwiceIfGetAnswerNotYet()
  122. {
  123. var peer = new PeerConnection(true, config, ResendOfferInterval,
  124. test.component.StartCoroutine, test.component.StopCoroutine);
  125. Assert.That(peer, Is.Not.Null);
  126. var sendOfferCount = 0;
  127. RTCSessionDescription offerDesc = default;
  128. peer.SendOfferHandler += description =>
  129. {
  130. sendOfferCount++;
  131. offerDesc = description;
  132. };
  133. var track = new AudioStreamTrack();
  134. peer.peer.AddTrack(track);
  135. while (sendOfferCount <= 2)
  136. {
  137. yield return new WaitForSeconds(ResendOfferInterval);
  138. Assert.That(peer.waitingAnswer, Is.True);
  139. peer.SendOffer();
  140. }
  141. yield return new WaitUntil(() => sendOfferCount > 2);
  142. Assert.That(sendOfferCount, Is.GreaterThan(2));
  143. Assert.That(offerDesc.type, Is.EqualTo(RTCSdpType.Offer));
  144. Assert.That(offerDesc.sdp, Is.Not.Null.Or.Empty);
  145. track.Dispose();
  146. peer.Dispose();
  147. }
  148. [UnityTest, Timeout(5000)]
  149. [TestCase(true, ExpectedResult = null)]
  150. [TestCase(false, ExpectedResult = null)]
  151. public IEnumerator CalledSendAnswerWhenGotDescriptionWhenStable(bool polite)
  152. {
  153. var peer1 = new PeerConnection(true, config, ResendOfferInterval,
  154. test.component.StartCoroutine, test.component.StopCoroutine);
  155. Assert.That(peer1, Is.Not.Null);
  156. var peer2 = new PeerConnection(polite, config, ResendOfferInterval,
  157. test.component.StartCoroutine, test.component.StopCoroutine);
  158. Assert.That(peer2, Is.Not.Null);
  159. var isGotSendOffer = false;
  160. RTCSessionDescription offerDesc = default;
  161. peer1.SendOfferHandler += description =>
  162. {
  163. isGotSendOffer = true;
  164. offerDesc = description;
  165. };
  166. var isGotSendAnswer = false;
  167. RTCSessionDescription answerDesc = default;
  168. peer2.SendAnswerHandler += description =>
  169. {
  170. isGotSendAnswer = true;
  171. answerDesc = description;
  172. };
  173. var track = new AudioStreamTrack();
  174. peer1.peer.AddTrack(track);
  175. yield return new WaitUntil(() => isGotSendOffer);
  176. Assert.That(isGotSendOffer, Is.True);
  177. Assert.That(offerDesc.type, Is.EqualTo(RTCSdpType.Offer));
  178. Assert.That(offerDesc.sdp, Is.Not.Null.Or.Empty);
  179. var isComplete = false;
  180. yield return peer2.OnGotDescription(offerDesc, () => isComplete = true);
  181. Assert.That(isComplete, Is.True);
  182. peer2.SendAnswer();
  183. yield return new WaitUntil(() => isGotSendAnswer);
  184. Assert.That(isGotSendAnswer, Is.True);
  185. Assert.That(answerDesc.type, Is.EqualTo(RTCSdpType.Answer));
  186. Assert.That(answerDesc.sdp, Is.Not.Null.Or.Empty);
  187. track.Dispose();
  188. peer1.Dispose();
  189. peer2.Dispose();
  190. }
  191. [UnityTest, Timeout(5000)]
  192. public IEnumerator CalledSendAnswerWhenGotDescriptionThatHaveLocalOfferInPolite()
  193. {
  194. var peer1 = new PeerConnection(true, config, ResendOfferInterval,
  195. test.component.StartCoroutine, test.component.StopCoroutine);
  196. Assert.That(peer1, Is.Not.Null);
  197. var peer2 = new PeerConnection(true, config, ResendOfferInterval,
  198. test.component.StartCoroutine, test.component.StopCoroutine);
  199. Assert.That(peer2, Is.Not.Null);
  200. var isGotSendOffer1 = false;
  201. RTCSessionDescription offerDesc1 = default;
  202. peer1.SendOfferHandler += description =>
  203. {
  204. isGotSendOffer1 = true;
  205. offerDesc1 = description;
  206. };
  207. var isGotSendOffer2 = false;
  208. RTCSessionDescription offerDesc2 = default;
  209. peer2.SendOfferHandler += description =>
  210. {
  211. isGotSendOffer2 = true;
  212. offerDesc2 = description;
  213. };
  214. var isGotSendAnswer = false;
  215. RTCSessionDescription answerDesc = default;
  216. peer2.SendAnswerHandler += description =>
  217. {
  218. isGotSendAnswer = true;
  219. answerDesc = description;
  220. };
  221. var track1 = new AudioStreamTrack();
  222. peer1.peer.AddTrack(track1);
  223. var track2 = new AudioStreamTrack();
  224. peer2.peer.AddTrack(track2);
  225. yield return new WaitUntil(() => isGotSendOffer1);
  226. Assert.That(isGotSendOffer1, Is.True);
  227. Assert.That(offerDesc1.type, Is.EqualTo(RTCSdpType.Offer));
  228. Assert.That(offerDesc1.sdp, Is.Not.Null.Or.Empty);
  229. yield return new WaitUntil(() => isGotSendOffer2);
  230. Assert.That(isGotSendOffer1, Is.True);
  231. Assert.That(offerDesc2.type, Is.EqualTo(RTCSdpType.Offer));
  232. Assert.That(offerDesc2.sdp, Is.Not.Null.Or.Empty);
  233. Assert.That(peer2.peer.SignalingState, Is.EqualTo(RTCSignalingState.HaveLocalOffer));
  234. var isComplete = false;
  235. yield return peer2.OnGotDescription(offerDesc1, () => isComplete = true);
  236. Assert.That(isComplete, Is.True);
  237. peer2.SendAnswer();
  238. yield return new WaitUntil(() => isGotSendAnswer);
  239. Assert.That(isGotSendAnswer, Is.True);
  240. Assert.That(answerDesc.type, Is.EqualTo(RTCSdpType.Answer));
  241. Assert.That(answerDesc.sdp, Is.Not.Null.Or.Empty);
  242. Assert.That(peer2.peer.SignalingState, Is.EqualTo(RTCSignalingState.Stable));
  243. track1.Dispose();
  244. track2.Dispose();
  245. peer1.Dispose();
  246. peer2.Dispose();
  247. }
  248. [UnityTest, Timeout(5000)]
  249. public IEnumerator NotCalledSendAnswerWhenGotDescriptionThatHaveLocalOfferInImpolite()
  250. {
  251. var peer1 = new PeerConnection(true, config, ResendOfferInterval,
  252. test.component.StartCoroutine, test.component.StopCoroutine);
  253. Assert.That(peer1, Is.Not.Null);
  254. var peer2 = new PeerConnection(false, config, ResendOfferInterval,
  255. test.component.StartCoroutine, test.component.StopCoroutine);
  256. Assert.That(peer2, Is.Not.Null);
  257. var isGotSendOffer1 = false;
  258. RTCSessionDescription offerDesc1 = default;
  259. peer1.SendOfferHandler += description =>
  260. {
  261. isGotSendOffer1 = true;
  262. offerDesc1 = description;
  263. };
  264. var isGotSendOffer2 = false;
  265. RTCSessionDescription offerDesc2 = default;
  266. peer2.SendOfferHandler += description =>
  267. {
  268. isGotSendOffer2 = true;
  269. offerDesc2 = description;
  270. };
  271. var track1 = new AudioStreamTrack();
  272. peer1.peer.AddTrack(track1);
  273. var track2 = new AudioStreamTrack();
  274. peer2.peer.AddTrack(track2);
  275. yield return new WaitUntil(() => isGotSendOffer1);
  276. Assert.That(isGotSendOffer1, Is.True);
  277. Assert.That(offerDesc1.type, Is.EqualTo(RTCSdpType.Offer));
  278. Assert.That(offerDesc1.sdp, Is.Not.Null.Or.Empty);
  279. yield return new WaitUntil(() => isGotSendOffer2);
  280. Assert.That(isGotSendOffer1, Is.True);
  281. Assert.That(offerDesc2.type, Is.EqualTo(RTCSdpType.Offer));
  282. Assert.That(offerDesc2.sdp, Is.Not.Null.Or.Empty);
  283. Assert.That(peer2.peer.SignalingState, Is.EqualTo(RTCSignalingState.HaveLocalOffer));
  284. var isComplete = false;
  285. yield return peer2.OnGotDescription(offerDesc1, () => isComplete = true);
  286. Assert.That(isComplete, Is.False);
  287. Assert.That(peer2.peer.SignalingState, Is.EqualTo(RTCSignalingState.HaveLocalOffer));
  288. track1.Dispose();
  289. track2.Dispose();
  290. peer1.Dispose();
  291. peer2.Dispose();
  292. }
  293. [UnityTest, Timeout(5000)]
  294. public IEnumerator CalledTrackEventWhenGotSdpIncludeTrack()
  295. {
  296. var peer1 = new PeerConnection(true, config, ResendOfferInterval,
  297. test.component.StartCoroutine, test.component.StopCoroutine);
  298. Assert.That(peer1, Is.Not.Null);
  299. var peer2 = new PeerConnection(true, config, ResendOfferInterval,
  300. test.component.StartCoroutine, test.component.StopCoroutine);
  301. Assert.That(peer2, Is.Not.Null);
  302. var isGotSendOffer = false;
  303. RTCSessionDescription offerDesc = default;
  304. peer1.SendOfferHandler += description =>
  305. {
  306. isGotSendOffer = true;
  307. offerDesc = description;
  308. };
  309. var isGotTrackEvent = false;
  310. RTCTrackEvent trackEvent = null;
  311. peer2.OnTrackEventHandler += e =>
  312. {
  313. isGotTrackEvent = true;
  314. trackEvent = e;
  315. };
  316. var track = new AudioStreamTrack();
  317. peer1.peer.AddTrack(track);
  318. yield return new WaitUntil(() => isGotSendOffer);
  319. Assert.That(isGotSendOffer, Is.True);
  320. Assert.That(offerDesc.type, Is.EqualTo(RTCSdpType.Offer));
  321. Assert.That(offerDesc.sdp, Is.Not.Null.Or.Empty);
  322. var isComplete = false;
  323. yield return peer2.OnGotDescription(offerDesc, () => isComplete = true);
  324. Assert.That(isComplete, Is.True);
  325. yield return new WaitUntil(() => isGotTrackEvent);
  326. Assert.That(isGotTrackEvent, Is.True);
  327. Assert.That(trackEvent, Is.Not.Null);
  328. Assert.That(trackEvent.Track.Id, Is.EqualTo(track.Id));
  329. track.Dispose();
  330. peer1.Dispose();
  331. peer2.Dispose();
  332. }
  333. [UnityTest, Timeout(5000)]
  334. public IEnumerator CalledOnDataChannelWhenGotSdpIncludeDataChannel()
  335. {
  336. var peer1 = new PeerConnection(true, config, ResendOfferInterval,
  337. test.component.StartCoroutine, test.component.StopCoroutine);
  338. Assert.That(peer1, Is.Not.Null);
  339. var peer2 = new PeerConnection(true, config, ResendOfferInterval,
  340. test.component.StartCoroutine, test.component.StopCoroutine);
  341. Assert.That(peer2, Is.Not.Null);
  342. var isGotSendOffer = false;
  343. RTCSessionDescription offerDesc = default;
  344. peer1.SendOfferHandler += description =>
  345. {
  346. isGotSendOffer = true;
  347. offerDesc = description;
  348. };
  349. var isGotSendAnswer = false;
  350. RTCSessionDescription answerDesc = default;
  351. peer2.SendAnswerHandler += description =>
  352. {
  353. isGotSendAnswer = true;
  354. answerDesc = description;
  355. };
  356. var isGotDataChannel = false;
  357. RTCDataChannel dataChannel = null;
  358. peer2.OnDataChannelHandler += e =>
  359. {
  360. isGotDataChannel = true;
  361. dataChannel = e;
  362. };
  363. var channel = peer1.peer.CreateDataChannel("test");
  364. Assert.That(channel, Is.Not.Null);
  365. yield return new WaitUntil(() => isGotSendOffer);
  366. Assert.That(isGotSendOffer, Is.True);
  367. Assert.That(offerDesc.type, Is.EqualTo(RTCSdpType.Offer));
  368. Assert.That(offerDesc.sdp, Is.Not.Null.Or.Empty);
  369. var isComplete = false;
  370. yield return peer2.OnGotDescription(offerDesc, () => isComplete = true);
  371. Assert.That(isComplete, Is.True);
  372. peer2.SendAnswer();
  373. yield return new WaitUntil(() => isGotSendAnswer);
  374. Assert.That(isGotSendAnswer, Is.True);
  375. Assert.That(answerDesc.type, Is.EqualTo(RTCSdpType.Answer));
  376. Assert.That(answerDesc.sdp, Is.Not.Null.Or.Empty);
  377. isComplete = false;
  378. yield return peer1.OnGotDescription(answerDesc, () => isComplete = true);
  379. Assert.That(isComplete, Is.True);
  380. yield return new WaitUntil(() => isGotDataChannel);
  381. Assert.That(isGotDataChannel, Is.True);
  382. Assert.That(dataChannel, Is.Not.Null);
  383. Assert.That(dataChannel.Id, Is.EqualTo(channel.Id));
  384. channel.Dispose();
  385. peer1.Dispose();
  386. peer2.Dispose();
  387. }
  388. [UnityTest, Timeout(5000)]
  389. public IEnumerator CalledSendCandidateWhenAddTransceiver()
  390. {
  391. var peer = new PeerConnection(true, config, ResendOfferInterval,
  392. test.component.StartCoroutine, test.component.StopCoroutine);
  393. Assert.That(peer, Is.Not.Null);
  394. var isGotSendCandidate = false;
  395. RTCIceCandidate candidate = null;
  396. peer.SendCandidateHandler += e =>
  397. {
  398. isGotSendCandidate = true;
  399. candidate = e;
  400. };
  401. var transceiver = peer.peer.AddTransceiver(TrackKind.Video);
  402. Assert.That(transceiver, Is.Not.Null);
  403. yield return new WaitUntil(() => isGotSendCandidate);
  404. Assert.That(isGotSendCandidate, Is.True);
  405. Assert.That(candidate, Is.Not.Null);
  406. transceiver.Dispose();
  407. peer.Dispose();
  408. }
  409. [UnityTest, Timeout(5000)]
  410. public IEnumerator AcceptWhenGotCandidateThatHaveRemoteDescription()
  411. {
  412. var peer1 = new PeerConnection(true, config, ResendOfferInterval,
  413. test.component.StartCoroutine, test.component.StopCoroutine);
  414. Assert.That(peer1, Is.Not.Null);
  415. var peer2 = new PeerConnection(true, config, ResendOfferInterval,
  416. test.component.StartCoroutine, test.component.StopCoroutine);
  417. Assert.That(peer2, Is.Not.Null);
  418. var isGotSendOffer1 = false;
  419. RTCSessionDescription offerDesc1 = default;
  420. peer1.SendOfferHandler += description =>
  421. {
  422. isGotSendOffer1 = true;
  423. offerDesc1 = description;
  424. };
  425. var isGotSendCandidate = false;
  426. RTCIceCandidate candidate = null;
  427. peer1.SendCandidateHandler += e =>
  428. {
  429. isGotSendCandidate = true;
  430. candidate = e;
  431. };
  432. var transceiver = peer1.peer.AddTransceiver(TrackKind.Video);
  433. Assert.That(transceiver, Is.Not.Null);
  434. yield return new WaitUntil(() => isGotSendOffer1);
  435. Assert.That(isGotSendOffer1, Is.True);
  436. Assert.That(offerDesc1.type, Is.EqualTo(RTCSdpType.Offer));
  437. Assert.That(offerDesc1.sdp, Is.Not.Null.Or.Empty);
  438. yield return new WaitUntil(() => isGotSendCandidate);
  439. Assert.That(isGotSendCandidate, Is.True);
  440. Assert.That(candidate, Is.Not.Null);
  441. var isComplete = false;
  442. yield return peer2.OnGotDescription(offerDesc1, () => isComplete = true);
  443. Assert.That(isComplete, Is.True);
  444. Assert.That(peer2.OnGotIceCandidate(candidate), Is.True);
  445. transceiver.Dispose();
  446. peer1.Dispose();
  447. peer2.Dispose();
  448. }
  449. [UnityTest, Timeout(5000)]
  450. public IEnumerator NotAcceptWhenGotCandidateThatDontHaveRemoteDescription()
  451. {
  452. var peer1 = new PeerConnection(true, config, ResendOfferInterval,
  453. test.component.StartCoroutine, test.component.StopCoroutine);
  454. Assert.That(peer1, Is.Not.Null);
  455. var peer2 = new PeerConnection(true, config, ResendOfferInterval,
  456. test.component.StartCoroutine, test.component.StopCoroutine);
  457. Assert.That(peer2, Is.Not.Null);
  458. var isGotSendOffer1 = false;
  459. RTCSessionDescription offerDesc1 = default;
  460. peer1.SendOfferHandler += description =>
  461. {
  462. isGotSendOffer1 = true;
  463. offerDesc1 = description;
  464. };
  465. var isGotSendCandidate = false;
  466. RTCIceCandidate candidate = null;
  467. peer1.SendCandidateHandler += e =>
  468. {
  469. isGotSendCandidate = true;
  470. candidate = e;
  471. };
  472. var transceiver = peer1.peer.AddTransceiver(TrackKind.Video);
  473. Assert.That(transceiver, Is.Not.Null);
  474. yield return new WaitUntil(() => isGotSendOffer1);
  475. Assert.That(isGotSendOffer1, Is.True);
  476. Assert.That(offerDesc1.type, Is.EqualTo(RTCSdpType.Offer));
  477. Assert.That(offerDesc1.sdp, Is.Not.Null.Or.Empty);
  478. yield return new WaitUntil(() => isGotSendCandidate);
  479. Assert.That(isGotSendCandidate, Is.True);
  480. Assert.That(candidate, Is.Not.Null);
  481. Assert.That(peer2.OnGotIceCandidate(candidate), Is.False);
  482. transceiver.Dispose();
  483. peer1.Dispose();
  484. peer2.Dispose();
  485. }
  486. [UnityTest, Timeout(5000)]
  487. public IEnumerator CalledOnConnect()
  488. {
  489. var peer1 = new PeerConnection(true, config, ResendOfferInterval,
  490. test.component.StartCoroutine, test.component.StopCoroutine);
  491. Assert.That(peer1, Is.Not.Null);
  492. var peer2 = new PeerConnection(true, config, ResendOfferInterval,
  493. test.component.StartCoroutine, test.component.StopCoroutine);
  494. Assert.That(peer2, Is.Not.Null);
  495. var isGotSendOffer = false;
  496. RTCSessionDescription offerDesc = default;
  497. peer1.SendOfferHandler += description =>
  498. {
  499. isGotSendOffer = true;
  500. offerDesc = description;
  501. };
  502. var isGotConnect1 = false;
  503. peer1.OnConnectHandler += () => isGotConnect1 = true;
  504. var isGotSendAnswer = false;
  505. RTCSessionDescription answerDesc = default;
  506. peer2.SendAnswerHandler += description =>
  507. {
  508. isGotSendAnswer = true;
  509. answerDesc = description;
  510. };
  511. var isGotConnect2 = false;
  512. peer2.OnConnectHandler += () => isGotConnect2 = true;
  513. var transceiver = peer1.peer.AddTransceiver(TrackKind.Video);
  514. Assert.That(transceiver, Is.Not.Null);
  515. yield return new WaitUntil(() => isGotSendOffer);
  516. Assert.That(isGotSendOffer, Is.True);
  517. Assert.That(offerDesc.type, Is.EqualTo(RTCSdpType.Offer));
  518. Assert.That(offerDesc.sdp, Is.Not.Null.Or.Empty);
  519. var isComplete = false;
  520. yield return peer2.OnGotDescription(offerDesc, () => isComplete = true);
  521. Assert.That(isComplete, Is.True);
  522. peer2.SendAnswer();
  523. yield return new WaitUntil(() => isGotSendAnswer);
  524. Assert.That(isGotSendAnswer, Is.True);
  525. Assert.That(answerDesc.type, Is.EqualTo(RTCSdpType.Answer));
  526. Assert.That(answerDesc.sdp, Is.Not.Null.Or.Empty);
  527. isComplete = false;
  528. yield return peer1.OnGotDescription(answerDesc, () => isComplete = true);
  529. Assert.That(isComplete, Is.True);
  530. yield return new WaitUntil(() => isGotConnect1 && isGotConnect2);
  531. Assert.That(isGotConnect1, Is.True);
  532. Assert.That(isGotConnect2, Is.True);
  533. transceiver.Dispose();
  534. peer1.Dispose();
  535. peer2.Dispose();
  536. }
  537. [UnityTest, Timeout(5000)]
  538. public IEnumerator SendOfferTwiceImmediately()
  539. {
  540. var peer1 = new PeerConnection(true, config, ResendOfferInterval,
  541. test.component.StartCoroutine, test.component.StopCoroutine);
  542. Assert.That(peer1, Is.Not.Null);
  543. var isGotSendOffer = false;
  544. RTCSessionDescription offerDesc = default;
  545. peer1.SendOfferHandler += description =>
  546. {
  547. isGotSendOffer = true;
  548. offerDesc = description;
  549. };
  550. var transceiver = peer1.peer.AddTransceiver(TrackKind.Video);
  551. Assert.That(transceiver, Is.Not.Null);
  552. peer1.SendOffer();
  553. peer1.SendOffer();
  554. yield return new WaitUntil(() => isGotSendOffer);
  555. Assert.That(isGotSendOffer, Is.True);
  556. Assert.That(offerDesc.type, Is.EqualTo(RTCSdpType.Offer));
  557. Assert.That(offerDesc.sdp, Is.Not.Null.Or.Empty);
  558. transceiver.Dispose();
  559. peer1.Dispose();
  560. }
  561. [UnityTest, Timeout(5000)]
  562. public IEnumerator SendAnswerTwiceImmediately()
  563. {
  564. var peer1 = new PeerConnection(true, config, ResendOfferInterval,
  565. test.component.StartCoroutine, test.component.StopCoroutine);
  566. Assert.That(peer1, Is.Not.Null);
  567. var peer2 = new PeerConnection(true, config, ResendOfferInterval,
  568. test.component.StartCoroutine, test.component.StopCoroutine);
  569. Assert.That(peer2, Is.Not.Null);
  570. var isGotSendOffer = false;
  571. RTCSessionDescription offerDesc = default;
  572. peer1.SendOfferHandler += description =>
  573. {
  574. isGotSendOffer = true;
  575. offerDesc = description;
  576. };
  577. var isGotSendAnswer = false;
  578. RTCSessionDescription answerDesc = default;
  579. peer2.SendAnswerHandler += description =>
  580. {
  581. isGotSendAnswer = true;
  582. answerDesc = description;
  583. };
  584. var transceiver = peer1.peer.AddTransceiver(TrackKind.Video);
  585. Assert.That(transceiver, Is.Not.Null);
  586. yield return new WaitUntil(() => isGotSendOffer);
  587. Assert.That(isGotSendOffer, Is.True);
  588. Assert.That(offerDesc.type, Is.EqualTo(RTCSdpType.Offer));
  589. Assert.That(offerDesc.sdp, Is.Not.Null.Or.Empty);
  590. var isComplete = false;
  591. yield return peer2.OnGotDescription(offerDesc, () => isComplete = true);
  592. Assert.That(isComplete, Is.True);
  593. peer2.SendAnswer();
  594. peer2.SendAnswer();
  595. yield return new WaitUntil(() => isGotSendAnswer);
  596. Assert.That(isGotSendAnswer, Is.True);
  597. Assert.That(answerDesc.type, Is.EqualTo(RTCSdpType.Answer));
  598. Assert.That(answerDesc.sdp, Is.Not.Null.Or.Empty);
  599. transceiver.Dispose();
  600. peer1.Dispose();
  601. peer2.Dispose();
  602. }
  603. [UnityTest, Timeout(5000)]
  604. public IEnumerator OnGotOfferDescriptionAfterSendOfferImmediatelyInPolite()
  605. {
  606. var peer1 = new PeerConnection(true, config, ResendOfferInterval,
  607. test.component.StartCoroutine, test.component.StopCoroutine);
  608. Assert.That(peer1, Is.Not.Null);
  609. var peer2 = new PeerConnection(true, config, ResendOfferInterval,
  610. test.component.StartCoroutine, test.component.StopCoroutine);
  611. Assert.That(peer2, Is.Not.Null);
  612. var isGotSendOffer1 = false;
  613. RTCSessionDescription offerDesc1 = default;
  614. peer1.SendOfferHandler += description =>
  615. {
  616. isGotSendOffer1 = true;
  617. offerDesc1 = description;
  618. };
  619. var isGotSendOffer2 = false;
  620. RTCSessionDescription offerDesc2 = default;
  621. peer2.SendOfferHandler += description =>
  622. {
  623. isGotSendOffer2 = true;
  624. offerDesc2 = description;
  625. };
  626. var isGotSendAnswer = false;
  627. RTCSessionDescription answerDesc = default;
  628. peer2.SendAnswerHandler += description =>
  629. {
  630. isGotSendAnswer = true;
  631. answerDesc = description;
  632. };
  633. var transceiver1 = peer1.peer.AddTransceiver(TrackKind.Video);
  634. Assert.That(transceiver1, Is.Not.Null);
  635. yield return new WaitUntil(() => isGotSendOffer1);
  636. Assert.That(isGotSendOffer1, Is.True);
  637. Assert.That(offerDesc1.type, Is.EqualTo(RTCSdpType.Offer));
  638. Assert.That(offerDesc1.sdp, Is.Not.Null.Or.Empty);
  639. var transceiver2 = peer2.peer.AddTransceiver(TrackKind.Video);
  640. Assert.That(transceiver2, Is.Not.Null);
  641. //workaround: Need to wait 1 frame for negotiationneeded to be processed
  642. yield return 0;
  643. var isComplete = false;
  644. yield return peer2.OnGotDescription(offerDesc1, () => isComplete = true);
  645. Assert.That(isComplete, Is.True);
  646. yield return new WaitForSeconds(ResendOfferInterval);
  647. Assert.That(isGotSendOffer2, Is.False, "need waiting offer cause receive remote offer");
  648. peer2.SendAnswer();
  649. yield return new WaitUntil(() => isGotSendAnswer);
  650. Assert.That(isGotSendAnswer, Is.True);
  651. Assert.That(answerDesc.type, Is.EqualTo(RTCSdpType.Answer));
  652. Assert.That(answerDesc.sdp, Is.Not.Null.Or.Empty);
  653. yield return new WaitUntil(() => isGotSendOffer2);
  654. Assert.That(isGotSendOffer2, Is.True);
  655. Assert.That(offerDesc2.type, Is.EqualTo(RTCSdpType.Offer));
  656. Assert.That(offerDesc2.sdp, Is.Not.Null.Or.Empty);
  657. transceiver1.Dispose();
  658. transceiver2.Dispose();
  659. peer1.Dispose();
  660. peer2.Dispose();
  661. }
  662. [UnityTest, Timeout(5000)]
  663. public IEnumerator OnGotOfferDescriptionAfterSendOfferImmediatelyInImPolite()
  664. {
  665. var peer1 = new PeerConnection(true, config, ResendOfferInterval,
  666. test.component.StartCoroutine, test.component.StopCoroutine);
  667. Assert.That(peer1, Is.Not.Null);
  668. var peer2 = new PeerConnection(false, config, ResendOfferInterval,
  669. test.component.StartCoroutine, test.component.StopCoroutine);
  670. Assert.That(peer2, Is.Not.Null);
  671. var isGotSendOffer1 = false;
  672. RTCSessionDescription offerDesc1 = default;
  673. peer1.SendOfferHandler += description =>
  674. {
  675. isGotSendOffer1 = true;
  676. offerDesc1 = description;
  677. };
  678. var isGotSendOffer2 = false;
  679. RTCSessionDescription offerDesc2 = default;
  680. peer2.SendOfferHandler += description =>
  681. {
  682. isGotSendOffer2 = true;
  683. offerDesc2 = description;
  684. };
  685. var transceiver1 = peer1.peer.AddTransceiver(TrackKind.Video);
  686. Assert.That(transceiver1, Is.Not.Null);
  687. yield return new WaitUntil(() => isGotSendOffer1);
  688. Assert.That(isGotSendOffer1, Is.True);
  689. Assert.That(offerDesc1.type, Is.EqualTo(RTCSdpType.Offer));
  690. Assert.That(offerDesc1.sdp, Is.Not.Null.Or.Empty);
  691. var transceiver2 = peer2.peer.AddTransceiver(TrackKind.Video);
  692. Assert.That(transceiver2, Is.Not.Null);
  693. //workaround: Need to wait 1 frame for negotiationneeded to be processed
  694. yield return 0;
  695. var isComplete = false;
  696. yield return peer2.OnGotDescription(offerDesc1, () => isComplete = true);
  697. Assert.That(isComplete, Is.False, "need ignore offer cause peer2 is impolite");
  698. yield return new WaitUntil(() => isGotSendOffer2);
  699. Assert.That(isGotSendOffer2, Is.True);
  700. Assert.That(offerDesc2.type, Is.EqualTo(RTCSdpType.Offer));
  701. Assert.That(offerDesc2.sdp, Is.Not.Null.Or.Empty);
  702. transceiver1.Dispose();
  703. transceiver2.Dispose();
  704. peer1.Dispose();
  705. peer2.Dispose();
  706. }
  707. [UnityTest, Timeout(5000), LongRunning]
  708. [TestCase(true, ExpectedResult = null)]
  709. [TestCase(false, ExpectedResult = null)]
  710. public IEnumerator OnGotAnswerDescriptionAfterSendOfferImmediately(bool polite)
  711. {
  712. var peer1 = new PeerConnection(polite, config, ResendOfferInterval,
  713. test.component.StartCoroutine, test.component.StopCoroutine);
  714. Assert.That(peer1, Is.Not.Null);
  715. var peer2 = new PeerConnection(true, config, ResendOfferInterval,
  716. test.component.StartCoroutine, test.component.StopCoroutine);
  717. Assert.That(peer2, Is.Not.Null);
  718. var isGotSendOffer1 = false;
  719. RTCSessionDescription offerDesc1 = default;
  720. peer1.SendOfferHandler += description =>
  721. {
  722. isGotSendOffer1 = true;
  723. offerDesc1 = description;
  724. };
  725. var isGotSendAnswer = false;
  726. RTCSessionDescription answerDesc = default;
  727. peer2.SendAnswerHandler += description =>
  728. {
  729. isGotSendAnswer = true;
  730. answerDesc = description;
  731. };
  732. var transceiver1 = peer1.peer.AddTransceiver(TrackKind.Video);
  733. Assert.That(transceiver1, Is.Not.Null);
  734. yield return new WaitUntil(() => isGotSendOffer1);
  735. Assert.That(isGotSendOffer1, Is.True);
  736. Assert.That(offerDesc1.type, Is.EqualTo(RTCSdpType.Offer));
  737. Assert.That(offerDesc1.sdp, Is.Not.Null.Or.Empty);
  738. var isComplete = false;
  739. yield return peer2.OnGotDescription(offerDesc1, () => isComplete = true);
  740. Assert.That(isComplete, Is.True);
  741. peer2.SendAnswer();
  742. yield return new WaitUntil(() => isGotSendAnswer);
  743. Assert.That(isGotSendAnswer, Is.True);
  744. Assert.That(answerDesc.type, Is.EqualTo(RTCSdpType.Answer));
  745. Assert.That(answerDesc.sdp, Is.Not.Null.Or.Empty);
  746. yield return new WaitForSeconds(ResendOfferInterval);
  747. peer1.SendOffer();
  748. isComplete = false;
  749. yield return peer1.OnGotDescription(answerDesc, () => isComplete = true);
  750. Assert.That(isComplete, Is.True);
  751. transceiver1.Dispose();
  752. peer1.Dispose();
  753. peer2.Dispose();
  754. }
  755. [UnityTest, Timeout(5000)]
  756. [TestCase(true, ExpectedResult = null)]
  757. [TestCase(false, ExpectedResult = null)]
  758. public IEnumerator OnGotOfferDescriptionAfterSendAnswerImmediately(bool polite)
  759. {
  760. var peer1 = new PeerConnection(polite, config, ResendOfferInterval,
  761. test.component.StartCoroutine, test.component.StopCoroutine);
  762. Assert.That(peer1, Is.Not.Null);
  763. var peer2 = new PeerConnection(true, config, ResendOfferInterval,
  764. test.component.StartCoroutine, test.component.StopCoroutine);
  765. Assert.That(peer2, Is.Not.Null);
  766. var isGotSendOffer1 = false;
  767. RTCSessionDescription offerDesc1 = default;
  768. peer1.SendOfferHandler += description =>
  769. {
  770. isGotSendOffer1 = true;
  771. offerDesc1 = description;
  772. };
  773. var isGotSendAnswer = false;
  774. RTCSessionDescription answerDesc = default;
  775. peer2.SendAnswerHandler += description =>
  776. {
  777. isGotSendAnswer = true;
  778. answerDesc = description;
  779. };
  780. var transceiver1 = peer1.peer.AddTransceiver(TrackKind.Video);
  781. Assert.That(transceiver1, Is.Not.Null);
  782. yield return new WaitUntil(() => isGotSendOffer1);
  783. Assert.That(isGotSendOffer1, Is.True);
  784. Assert.That(offerDesc1.type, Is.EqualTo(RTCSdpType.Offer));
  785. Assert.That(offerDesc1.sdp, Is.Not.Null.Or.Empty);
  786. var isComplete = false;
  787. yield return peer2.OnGotDescription(offerDesc1, () => isComplete = true);
  788. Assert.That(isComplete, Is.True);
  789. peer2.SendAnswer();
  790. isComplete = false;
  791. yield return peer2.OnGotDescription(offerDesc1, () => isComplete = true);
  792. Assert.That(isComplete, Is.True);
  793. yield return new WaitUntil(() => isGotSendAnswer);
  794. Assert.That(isGotSendAnswer, Is.True);
  795. Assert.That(answerDesc.type, Is.EqualTo(RTCSdpType.Answer));
  796. Assert.That(answerDesc.sdp, Is.Not.Null.Or.Empty);
  797. transceiver1.Dispose();
  798. peer1.Dispose();
  799. peer2.Dispose();
  800. }
  801. }
  802. }