MockSignaling.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Unity.RenderStreaming.Signaling;
  7. using Unity.WebRTC;
  8. using UnityEngine;
  9. namespace Unity.RenderStreaming.RuntimeTest.Signaling
  10. {
  11. internal class MockSignaling : ISignaling
  12. {
  13. interface IMockSignalingManager
  14. {
  15. Task Add(MockSignaling signaling);
  16. Task Remove(MockSignaling signaling);
  17. Task OpenConnection(MockSignaling signaling, string connectionId);
  18. Task CloseConnection(MockSignaling signaling, string connectionId);
  19. Task Offer(MockSignaling owner, DescData data);
  20. Task Answer(MockSignaling owner, DescData data);
  21. Task Candidate(MockSignaling owner, CandidateData data);
  22. }
  23. class MockPublicSignalingManager : IMockSignalingManager
  24. {
  25. private List<MockSignaling> list = new List<MockSignaling>();
  26. private const int MillisecondsDelay = 10;
  27. public async Task Add(MockSignaling signaling)
  28. {
  29. await Task.Delay(MillisecondsDelay);
  30. list.Add(signaling);
  31. signaling.OnStart?.Invoke(signaling);
  32. }
  33. public async Task Remove(MockSignaling signaling)
  34. {
  35. await Task.Delay(MillisecondsDelay);
  36. list.Remove(signaling);
  37. }
  38. public async Task OpenConnection(MockSignaling signaling, string connectionId)
  39. {
  40. await Task.Delay(MillisecondsDelay);
  41. signaling.OnCreateConnection?.Invoke(signaling, connectionId, true);
  42. }
  43. public async Task CloseConnection(MockSignaling signaling, string connectionId)
  44. {
  45. await Task.Delay(MillisecondsDelay);
  46. foreach (var element in list)
  47. {
  48. element.OnDestroyConnection?.Invoke(element, connectionId);
  49. }
  50. }
  51. public async Task Offer(MockSignaling owner, DescData data)
  52. {
  53. await Task.Delay(MillisecondsDelay);
  54. data.polite = false;
  55. foreach (var signaling in list.Where(e => e != owner))
  56. {
  57. signaling.OnOffer?.Invoke(signaling, data);
  58. }
  59. }
  60. public async Task Answer(MockSignaling owner, DescData data)
  61. {
  62. await Task.Delay(MillisecondsDelay);
  63. foreach (var signaling in list.Where(e => e != owner))
  64. {
  65. signaling.OnAnswer?.Invoke(signaling, data);
  66. }
  67. }
  68. public async Task Candidate(MockSignaling owner, CandidateData data)
  69. {
  70. await Task.Delay(MillisecondsDelay);
  71. foreach (var signaling in list.Where(e => e != owner))
  72. {
  73. signaling.OnIceCandidate?.Invoke(signaling, data);
  74. }
  75. }
  76. }
  77. class MockPrivateSignalingManager : IMockSignalingManager
  78. {
  79. private Dictionary<string, List<MockSignaling>> connectionIds = new Dictionary<string, List<MockSignaling>>();
  80. private const int MillisecondsDelay = 10;
  81. public async Task Add(MockSignaling signaling)
  82. {
  83. await Task.Delay(MillisecondsDelay);
  84. signaling.OnStart?.Invoke(signaling);
  85. }
  86. public async Task Remove(MockSignaling signaling)
  87. {
  88. await Task.Delay(MillisecondsDelay);
  89. }
  90. public async Task OpenConnection(MockSignaling signaling, string connectionId)
  91. {
  92. await Task.Delay(MillisecondsDelay);
  93. bool peerExists = connectionIds.TryGetValue(connectionId, out var list);
  94. if (!peerExists)
  95. {
  96. list = new List<MockSignaling>();
  97. connectionIds.Add(connectionId, list);
  98. }
  99. list.Add(signaling);
  100. signaling.OnCreateConnection?.Invoke(signaling, connectionId, peerExists);
  101. }
  102. public async Task CloseConnection(MockSignaling signaling, string connectionId)
  103. {
  104. await Task.Delay(MillisecondsDelay);
  105. bool peerExists = connectionIds.TryGetValue(connectionId, out var list);
  106. if (!peerExists || !list.Contains(signaling))
  107. {
  108. Debug.LogError($"{connectionId} This connection id is not used.");
  109. }
  110. foreach (var element in list)
  111. {
  112. element.OnDestroyConnection?.Invoke(element, connectionId);
  113. }
  114. list.Remove(signaling);
  115. if (list.Count == 0)
  116. {
  117. connectionIds.Remove(connectionId);
  118. }
  119. }
  120. List<MockSignaling> FindList(MockSignaling owner, string connectionId)
  121. {
  122. if (!connectionIds.TryGetValue(connectionId, out var list))
  123. {
  124. return null;
  125. }
  126. list = list.Where(e => e != owner).ToList();
  127. if (list.Count == 0)
  128. {
  129. return null;
  130. }
  131. return list;
  132. }
  133. public async Task Offer(MockSignaling owner, DescData data)
  134. {
  135. await Task.Delay(MillisecondsDelay);
  136. var list = FindList(owner, data.connectionId);
  137. if (list == null)
  138. {
  139. Debug.LogWarning($"{data.connectionId} This connection id is not ready other session.");
  140. return;
  141. }
  142. data.polite = true;
  143. foreach (var signaling in list.Where(x => x != owner))
  144. {
  145. signaling.OnOffer?.Invoke(signaling, data);
  146. }
  147. }
  148. public async Task Answer(MockSignaling owner, DescData data)
  149. {
  150. await Task.Delay(MillisecondsDelay);
  151. var list = FindList(owner, data.connectionId);
  152. if (list == null)
  153. {
  154. Debug.LogWarning($"{data.connectionId} This connection id is not ready other session.");
  155. return;
  156. }
  157. foreach (var signaling in list.Where(x => x != owner))
  158. {
  159. signaling.OnAnswer?.Invoke(signaling, data);
  160. }
  161. }
  162. public async Task Candidate(MockSignaling owner, CandidateData data)
  163. {
  164. await Task.Delay(MillisecondsDelay);
  165. var list = FindList(owner, data.connectionId);
  166. if (list == null)
  167. {
  168. Debug.LogWarning($"{data.connectionId} This connection id is not ready other session.");
  169. return;
  170. }
  171. foreach (var signaling in list.Where(e => e != owner))
  172. {
  173. signaling.OnIceCandidate?.Invoke(signaling, data);
  174. }
  175. }
  176. }
  177. private static IMockSignalingManager manager = null;
  178. public static void Reset(bool enablePrivateMode)
  179. {
  180. if (enablePrivateMode)
  181. {
  182. manager = new MockPrivateSignalingManager();
  183. }
  184. else
  185. {
  186. manager = new MockPublicSignalingManager();
  187. }
  188. }
  189. public string Url { get { return string.Empty; } }
  190. public float Interval { get { return 0.1f; } }
  191. static MockSignaling()
  192. {
  193. manager = new MockPublicSignalingManager();
  194. }
  195. public MockSignaling(SignalingSettings settings = null, SynchronizationContext context = null)
  196. {
  197. }
  198. public void Start()
  199. {
  200. manager.Add(this);
  201. }
  202. public void Stop()
  203. {
  204. manager.Remove(this);
  205. }
  206. public event OnStartHandler OnStart;
  207. public event OnConnectHandler OnCreateConnection;
  208. public event OnDisconnectHandler OnDestroyConnection;
  209. public event OnOfferHandler OnOffer;
  210. public event OnAnswerHandler OnAnswer;
  211. public event OnIceCandidateHandler OnIceCandidate;
  212. public void OpenConnection(string connectionId)
  213. {
  214. if (string.IsNullOrEmpty(connectionId))
  215. throw new ArgumentException("connectionId is null or empty.");
  216. manager.OpenConnection(this, connectionId);
  217. }
  218. public void CloseConnection(string connectionId)
  219. {
  220. if (string.IsNullOrEmpty(connectionId))
  221. throw new ArgumentException("connectionId is null or empty.");
  222. manager.CloseConnection(this, connectionId);
  223. }
  224. public void SendOffer(string connectionId, RTCSessionDescription offer)
  225. {
  226. if (string.IsNullOrEmpty(connectionId))
  227. throw new ArgumentException("connectionId is null or empty.");
  228. DescData data = new DescData
  229. {
  230. connectionId = connectionId,
  231. type = "offer",
  232. sdp = offer.sdp
  233. };
  234. manager.Offer(this, data);
  235. }
  236. public void SendAnswer(string connectionId, RTCSessionDescription answer)
  237. {
  238. if (string.IsNullOrEmpty(connectionId))
  239. throw new ArgumentException("connectionId is null or empty.");
  240. DescData data = new DescData
  241. {
  242. connectionId = connectionId,
  243. type = "answer",
  244. sdp = answer.sdp
  245. };
  246. manager.Answer(this, data);
  247. }
  248. public void SendCandidate(string connectionId, RTCIceCandidate candidate)
  249. {
  250. if (string.IsNullOrEmpty(connectionId))
  251. throw new ArgumentException("connectionId is null or empty.");
  252. CandidateData data = new CandidateData
  253. {
  254. connectionId = connectionId,
  255. candidate = candidate.Candidate,
  256. sdpMid = candidate.SdpMid,
  257. sdpMLineIndex = candidate.SdpMLineIndex.GetValueOrDefault()
  258. };
  259. manager.Candidate(this, data);
  260. }
  261. }
  262. }