TransceiverTest.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using UnityEngine;
  2. using UnityEngine.TestTools;
  3. using NUnit.Framework;
  4. using System.Collections;
  5. using System.Linq;
  6. namespace Unity.WebRTC.RuntimeTest
  7. {
  8. class TransceiverTest
  9. {
  10. [SetUp]
  11. public void SetUp()
  12. {
  13. WebRTC.Initialize(true);
  14. }
  15. [TearDown]
  16. public void TearDown()
  17. {
  18. WebRTC.Dispose();
  19. }
  20. [Test]
  21. public void SenderGetVideoCapabilities()
  22. {
  23. RTCRtpCapabilities capabilities = RTCRtpSender.GetCapabilities(TrackKind.Video);
  24. Assert.NotNull(capabilities);
  25. Assert.NotNull(capabilities.codecs);
  26. Assert.IsNotEmpty(capabilities.codecs);
  27. Assert.NotNull(capabilities.headerExtensions);
  28. Assert.IsNotEmpty(capabilities.headerExtensions);
  29. foreach (var codec in capabilities.codecs)
  30. {
  31. Assert.NotNull(codec);
  32. Assert.IsNotEmpty(codec.mimeType);
  33. }
  34. foreach (var extensions in capabilities.headerExtensions)
  35. {
  36. Assert.NotNull(extensions);
  37. Assert.IsNotEmpty(extensions.uri);
  38. }
  39. }
  40. [Test]
  41. public void SenderGetAudioCapabilities()
  42. {
  43. RTCRtpCapabilities capabilities = RTCRtpSender.GetCapabilities(TrackKind.Audio);
  44. Assert.NotNull(capabilities);
  45. Assert.NotNull(capabilities.codecs);
  46. Assert.IsNotEmpty(capabilities.codecs);
  47. Assert.NotNull(capabilities.headerExtensions);
  48. Assert.IsNotEmpty(capabilities.headerExtensions);
  49. foreach (var codec in capabilities.codecs)
  50. {
  51. Assert.NotNull(codec);
  52. Assert.IsNotEmpty(codec.mimeType);
  53. }
  54. foreach (var extensions in capabilities.headerExtensions)
  55. {
  56. Assert.NotNull(extensions);
  57. Assert.IsNotEmpty(extensions.uri);
  58. }
  59. }
  60. [Test]
  61. public void ReceiverGetVideoCapabilities()
  62. {
  63. RTCRtpCapabilities capabilities = RTCRtpReceiver.GetCapabilities(TrackKind.Video);
  64. Assert.NotNull(capabilities);
  65. Assert.NotNull(capabilities.codecs);
  66. Assert.IsNotEmpty(capabilities.codecs);
  67. Assert.NotNull(capabilities.headerExtensions);
  68. Assert.IsNotEmpty(capabilities.headerExtensions);
  69. foreach (var codec in capabilities.codecs)
  70. {
  71. Assert.NotNull(codec);
  72. Assert.IsNotEmpty(codec.mimeType);
  73. }
  74. foreach (var extensions in capabilities.headerExtensions)
  75. {
  76. Assert.NotNull(extensions);
  77. Assert.IsNotEmpty(extensions.uri);
  78. }
  79. }
  80. [Test]
  81. public void ReceiverGetAudioCapabilities()
  82. {
  83. RTCRtpCapabilities capabilities = RTCRtpReceiver.GetCapabilities(TrackKind.Audio);
  84. Assert.NotNull(capabilities);
  85. Assert.NotNull(capabilities.codecs);
  86. Assert.IsNotEmpty(capabilities.codecs);
  87. Assert.NotNull(capabilities.headerExtensions);
  88. Assert.IsNotEmpty(capabilities.headerExtensions);
  89. foreach (var codec in capabilities.codecs)
  90. {
  91. Assert.NotNull(codec);
  92. Assert.IsNotEmpty(codec.mimeType);
  93. }
  94. foreach (var extensions in capabilities.headerExtensions)
  95. {
  96. Assert.NotNull(extensions);
  97. Assert.IsNotEmpty(extensions.uri);
  98. }
  99. }
  100. [Test]
  101. [ConditionalIgnore(ConditionalIgnore.UnsupportedPlatformOpenGL, "Not support VideoStreamTrack for OpenGL")]
  102. public void TransceiverSetVideoCodecPreferences()
  103. {
  104. var peer = new RTCPeerConnection();
  105. var capabilities = RTCRtpSender.GetCapabilities(TrackKind.Video);
  106. var transceiver = peer.AddTransceiver(TrackKind.Video);
  107. var error = transceiver.SetCodecPreferences(capabilities.codecs);
  108. Assert.AreEqual(RTCErrorType.None, error);
  109. }
  110. [Test]
  111. public void TransceiverSetAudioCodecPreferences()
  112. {
  113. var peer = new RTCPeerConnection();
  114. var capabilities = RTCRtpSender.GetCapabilities(TrackKind.Audio);
  115. var transceiver = peer.AddTransceiver(TrackKind.Audio);
  116. var error = transceiver.SetCodecPreferences(capabilities.codecs);
  117. Assert.AreEqual(RTCErrorType.None, error);
  118. }
  119. [Test]
  120. [ConditionalIgnore(ConditionalIgnore.UnsupportedPlatformOpenGL, "Not support VideoStreamTrack for OpenGL")]
  121. public void ReceiverGetTrackReturnsVideoTrack()
  122. {
  123. var peer = new RTCPeerConnection();
  124. var transceiver = peer.AddTransceiver(TrackKind.Video);
  125. Assert.That(transceiver, Is.Not.Null);
  126. Assert.That(transceiver.CurrentDirection, Is.Null);
  127. // The receiver has a video track
  128. RTCRtpReceiver receiver = transceiver.Receiver;
  129. Assert.That(receiver, Is.Not.Null);
  130. Assert.That(receiver.Track, Is.Not.Null);
  131. Assert.That(receiver.Track, Is.TypeOf<VideoStreamTrack>());
  132. // The receiver has no track
  133. RTCRtpSender sender = transceiver.Sender;
  134. Assert.That(sender, Is.Not.Null);
  135. Assert.That(sender.Track, Is.Null);
  136. peer.Dispose();
  137. }
  138. [Test]
  139. public void ReceiverGetTrackReturnsAudioTrack()
  140. {
  141. var peer = new RTCPeerConnection();
  142. var transceiver = peer.AddTransceiver(TrackKind.Audio);
  143. Assert.That(transceiver, Is.Not.Null);
  144. Assert.That(transceiver.CurrentDirection, Is.Null);
  145. // The receiver has a audio track
  146. RTCRtpReceiver receiver = transceiver.Receiver;
  147. Assert.That(receiver, Is.Not.Null);
  148. Assert.That(receiver.Track, Is.Not.Null);
  149. Assert.That(receiver.Track, Is.TypeOf<AudioStreamTrack>());
  150. // The receiver has no track
  151. RTCRtpSender sender = transceiver.Sender;
  152. Assert.That(sender, Is.Not.Null);
  153. Assert.That(sender.Track, Is.Null);
  154. peer.Dispose();
  155. }
  156. [UnityTest]
  157. [Timeout(5000)]
  158. [ConditionalIgnore(ConditionalIgnore.UnsupportedPlatformOpenGL, "Not support VideoStreamTrack for OpenGL")]
  159. public IEnumerator TransceiverStop()
  160. {
  161. if (SystemInfo.processorType == "Apple M1")
  162. Assert.Ignore("todo:: This test will hang up on Apple M1");
  163. var go = new GameObject("Test");
  164. var cam = go.AddComponent<Camera>();
  165. var track = cam.CaptureStreamTrack(1280, 720);
  166. var test = new MonoBehaviourTest<SignalingPeers>();
  167. test.component.AddTransceiver(0, track);
  168. yield return test;
  169. test.component.CoroutineUpdate();
  170. var senderTransceivers = test.component.GetPeerTransceivers(0);
  171. Assert.That(senderTransceivers.Count(), Is.EqualTo(1));
  172. var transceiver1 = senderTransceivers.First();
  173. var receiverTransceivers = test.component.GetPeerTransceivers(1);
  174. Assert.That(receiverTransceivers.Count(), Is.EqualTo(1));
  175. var transceiver2 = receiverTransceivers.First();
  176. Assert.That(transceiver1.Stop(), Is.EqualTo(RTCErrorType.None));
  177. // wait for OnNegotiationNeeded callback in SignalingPeers class
  178. yield return 0;
  179. yield return new WaitUntil(() => test.component.NegotiationCompleted());
  180. Assert.That(transceiver1.Direction, Is.EqualTo(RTCRtpTransceiverDirection.Stopped));
  181. Assert.That(transceiver1.CurrentDirection, Is.EqualTo(RTCRtpTransceiverDirection.Stopped));
  182. Assert.That(transceiver2.Direction, Is.EqualTo(RTCRtpTransceiverDirection.Stopped));
  183. Assert.That(transceiver2.CurrentDirection, Is.EqualTo(RTCRtpTransceiverDirection.Stopped));
  184. //TODO:: Disposing process of MediaStreamTrack is unstable when using GC.
  185. //At the moment, Dispose methods needs to be called on the main thread for workaround.
  186. test.component.Dispose();
  187. track.Dispose();
  188. Object.DestroyImmediate(go);
  189. Object.DestroyImmediate(test.gameObject);
  190. }
  191. }
  192. }