VideoReceiveTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using System.Collections;
  2. using System.Linq;
  3. using NUnit.Framework;
  4. using UnityEngine;
  5. using UnityEngine.TestTools;
  6. namespace Unity.WebRTC.RuntimeTest
  7. {
  8. [TestFixture]
  9. [UnityPlatform(exclude = new[] { RuntimePlatform.Android, RuntimePlatform.IPhonePlayer, RuntimePlatform.OSXPlayer, RuntimePlatform.LinuxPlayer })]
  10. [ConditionalIgnore(ConditionalIgnore.UnsupportedPlatformOpenGL, "Not support VideoStreamTrack for OpenGL")]
  11. class VideoReceiveTestWithH264Codec : VideoReceiveTestBase
  12. {
  13. protected override void SetUpCodecCapability()
  14. {
  15. var mimetype = "H264";
  16. videoCodec = RTCRtpSender.GetCapabilities(TrackKind.Video).codecs.FirstOrDefault(c => c.mimeType.Contains(mimetype));
  17. if (videoCodec == null)
  18. Assert.Ignore($"{mimetype} codec is not supported this platform.");
  19. }
  20. }
  21. [TestFixture]
  22. [UnityPlatform(exclude = new[] { RuntimePlatform.IPhonePlayer, RuntimePlatform.OSXPlayer, RuntimePlatform.LinuxPlayer })]
  23. [ConditionalIgnore(ConditionalIgnore.UnsupportedPlatformOpenGL, "Not support VideoStreamTrack for OpenGL")]
  24. class VideoReceiveTestWithVP8Codec : VideoReceiveTestBase
  25. {
  26. protected override void SetUpCodecCapability()
  27. {
  28. var mimetype = "VP8";
  29. videoCodec = RTCRtpSender.GetCapabilities(TrackKind.Video).codecs.FirstOrDefault(c => c.mimeType.Contains(mimetype));
  30. if (videoCodec == null)
  31. Assert.Ignore($"{mimetype} codec is not supported this platform.");
  32. }
  33. }
  34. abstract class VideoReceiveTestBase
  35. {
  36. protected RTCRtpCodecCapability videoCodec;
  37. protected abstract void SetUpCodecCapability();
  38. [SetUp]
  39. public void SetUp()
  40. {
  41. WebRTC.Initialize(true);
  42. SetUpCodecCapability();
  43. }
  44. [TearDown]
  45. public void TearDown()
  46. {
  47. WebRTC.Dispose();
  48. }
  49. internal class TestValue
  50. {
  51. public int width;
  52. public int height;
  53. public int count;
  54. }
  55. internal static TestValue[] testValues = new TestValue[]
  56. {
  57. new TestValue{width = 256, height = 256, count = 1},
  58. new TestValue{width = 256, height = 256, count = 2},
  59. new TestValue{width = 256, height = 256, count = 3},
  60. new TestValue{width = 1280, height = 720, count = 1},
  61. new TestValue{width = 1280, height = 720, count = 2},
  62. new TestValue{width = 1280, height = 720, count = 3},
  63. new TestValue{width = 1920, height = 1080, count = 1},
  64. new TestValue{width = 1920, height = 1080, count = 2},
  65. new TestValue{width = 1920, height = 1080, count = 3},
  66. };
  67. internal static int[] range = Enumerable.Range(0, 9).ToArray();
  68. // not supported TestCase attribute on UnityTest
  69. // refer to https://docs.unity3d.com/Packages/com.unity.test-framework@1.1/manual/reference-tests-parameterized.html
  70. [UnityTest, LongRunning]
  71. [Timeout(15000)]
  72. [ConditionalIgnoreMultiple(ConditionalIgnore.UnsupportedPlatformVideoDecoder,
  73. "VideoStreamTrack.UpdateReceiveTexture is not supported on Direct3D12")]
  74. [ConditionalIgnoreMultiple(ConditionalIgnore.UnsupportedPlatformOpenGL,
  75. "Not support VideoStreamTrack for OpenGL")]
  76. public IEnumerator VideoReceive([ValueSource(nameof(range))]int index)
  77. {
  78. if(SystemInfo.processorType == "Apple M1")
  79. Assert.Ignore("todo:: This test will hang up on Apple M1");
  80. var value = testValues[index];
  81. var test = new MonoBehaviourTest<VideoReceivePeers>();
  82. test.component.SetResolution(value.width, value.height);
  83. test.component.SetCodec(videoCodec);
  84. yield return test;
  85. IEnumerator VideoReceive()
  86. {
  87. test.component.CreatePeers();
  88. test.component.CreateVideoStreamTrack();
  89. test.component.AddTrack();
  90. yield return test.component.Signaling();
  91. var receiveVideoTrack = test.component.RecvVideoTrack;
  92. yield return new WaitUntilWithTimeout(() => receiveVideoTrack != null, 5000);
  93. yield return new WaitUntilWithTimeout(() => receiveVideoTrack.Texture != null, 5000);
  94. Assert.That(receiveVideoTrack.Texture, Is.Not.Null);
  95. test.component.Clear();
  96. }
  97. for (int i = 0; i < value.count; i++)
  98. {
  99. yield return VideoReceive();
  100. }
  101. Object.DestroyImmediate(test.gameObject);
  102. }
  103. }
  104. class VideoReceivePeers : MonoBehaviour, IMonoBehaviourTest
  105. {
  106. public bool IsTestFinished { get; private set; }
  107. public VideoStreamTrack SendVideoTrack { get; private set; }
  108. public VideoStreamTrack RecvVideoTrack { get; private set; }
  109. public Texture SendTexture { get; private set; }
  110. public Texture RecvTexture { get; private set; }
  111. RTCPeerConnection offerPc;
  112. RTCPeerConnection answerPc;
  113. RTCRtpSender sender;
  114. GameObject camObj;
  115. Camera cam;
  116. int width;
  117. int height;
  118. RTCRtpCodecCapability videoCodec;
  119. void Start()
  120. {
  121. camObj = new GameObject("Camera");
  122. cam = camObj.AddComponent<Camera>();
  123. IsTestFinished = true;
  124. }
  125. public void SetResolution(int width, int height)
  126. {
  127. this.width = width;
  128. this.height = height;
  129. }
  130. public void SetCodec(RTCRtpCodecCapability codec)
  131. {
  132. this.videoCodec = codec;
  133. }
  134. public void CreatePeers()
  135. {
  136. var config = new RTCConfiguration
  137. {
  138. iceServers = new[] { new RTCIceServer { urls = new[] { "stun:stun.l.google.com:19302" } } }
  139. };
  140. offerPc = new RTCPeerConnection(ref config);
  141. answerPc = new RTCPeerConnection(ref config);
  142. answerPc.OnTrack = e =>
  143. {
  144. if (e.Track is VideoStreamTrack track)
  145. {
  146. RecvVideoTrack = track;
  147. track.OnVideoReceived += tex => RecvTexture = tex;
  148. }
  149. };
  150. }
  151. public void CreateVideoStreamTrack()
  152. {
  153. SendVideoTrack = cam.CaptureStreamTrack(width, height);
  154. SendTexture = cam.targetTexture;
  155. }
  156. public void AddTrack()
  157. {
  158. sender = offerPc.AddTrack(SendVideoTrack);
  159. if (videoCodec != null)
  160. {
  161. var transceiver = offerPc.GetTransceivers().First(t => t.Sender == sender);
  162. transceiver.SetCodecPreferences(new[] {videoCodec});
  163. }
  164. }
  165. public void RemoveTrack()
  166. {
  167. offerPc.RemoveTrack(sender);
  168. }
  169. public void Clear()
  170. {
  171. RenderTexture.active = null;
  172. SendVideoTrack?.Dispose();
  173. SendVideoTrack = null;
  174. offerPc?.Close();
  175. offerPc?.Dispose();
  176. offerPc = null;
  177. answerPc?.Close();
  178. answerPc?.Dispose();
  179. answerPc = null;
  180. SendTexture = null;
  181. RecvTexture = null;
  182. }
  183. void OnDestroy()
  184. {
  185. Clear();
  186. DestroyImmediate(camObj);
  187. camObj = null;
  188. }
  189. private void Update()
  190. {
  191. foreach (var reference in VideoStreamTrack.s_tracks.Values)
  192. {
  193. if (!reference.TryGetTarget(out var track))
  194. continue;
  195. track.UpdateSendTexture();
  196. track.UpdateReceiveTexture();
  197. }
  198. }
  199. public IEnumerator Signaling()
  200. {
  201. offerPc.OnIceCandidate = candidate => answerPc.AddIceCandidate(candidate);
  202. answerPc.OnIceCandidate = candidate => offerPc.AddIceCandidate(candidate);
  203. var pc1CreateOffer = offerPc.CreateOffer();
  204. yield return pc1CreateOffer;
  205. Assert.That(pc1CreateOffer.IsError, Is.False, () => $"Failed {nameof(pc1CreateOffer)}, error:{pc1CreateOffer.Error.message}");
  206. var offerDesc = pc1CreateOffer.Desc;
  207. var pc1SetLocalDescription = offerPc.SetLocalDescription(ref offerDesc);
  208. yield return pc1SetLocalDescription;
  209. Assert.That(pc1SetLocalDescription.IsError, Is.False, () => $"Failed {nameof(pc1SetLocalDescription)}, error:{pc1SetLocalDescription.Error.message}");
  210. var pc2SetRemoteDescription = answerPc.SetRemoteDescription(ref offerDesc);
  211. yield return pc2SetRemoteDescription;
  212. Assert.That(pc2SetRemoteDescription.IsError, Is.False, () => $"Failed {nameof(pc2SetRemoteDescription)}, error:{pc2SetRemoteDescription.Error.message}");
  213. var pc2CreateAnswer = answerPc.CreateAnswer();
  214. yield return pc2CreateAnswer;
  215. Assert.That(pc2CreateAnswer.IsError, Is.False, () => $"Failed {nameof(pc2CreateAnswer)}, error:{pc2CreateAnswer.Error.message}");
  216. var answerDesc = pc2CreateAnswer.Desc;
  217. var pc2SetLocalDescription = answerPc.SetLocalDescription(ref answerDesc);
  218. yield return pc2SetLocalDescription;
  219. Assert.That(pc2SetLocalDescription.IsError, Is.False, () => $"Failed {nameof(pc2SetLocalDescription)}, error:{pc2SetLocalDescription.Error.message}");
  220. var pc1SetRemoteDescription = offerPc.SetRemoteDescription(ref answerDesc);
  221. yield return pc1SetRemoteDescription;
  222. Assert.That(pc1SetRemoteDescription.IsError, Is.False, () => $"Failed {nameof(pc1SetRemoteDescription)}, error:{pc1SetRemoteDescription.Error.message}");
  223. var waitConnectOfferPc = new WaitUntilWithTimeout(() =>
  224. offerPc.IceConnectionState == RTCIceConnectionState.Connected ||
  225. offerPc.IceConnectionState == RTCIceConnectionState.Completed, 5000);
  226. yield return waitConnectOfferPc;
  227. Assert.That(waitConnectOfferPc.IsCompleted, Is.True);
  228. var waitConnectAnswerPc = new WaitUntilWithTimeout(() =>
  229. answerPc.IceConnectionState == RTCIceConnectionState.Connected ||
  230. answerPc.IceConnectionState == RTCIceConnectionState.Completed, 5000);
  231. yield return waitConnectAnswerPc;
  232. Assert.That(waitConnectAnswerPc.IsCompleted, Is.True);
  233. var checkSenders = new WaitUntilWithTimeout(() => offerPc.GetSenders().Any(), 5000);
  234. yield return checkSenders;
  235. Assert.That(checkSenders.IsCompleted, Is.True);
  236. var checkReceivers = new WaitUntilWithTimeout(() => answerPc.GetReceivers().Any(), 5000);
  237. yield return checkReceivers;
  238. Assert.That(checkReceivers.IsCompleted, Is.True);
  239. }
  240. }
  241. }