123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using Unity.RenderStreaming.Signaling;
- using Unity.WebRTC;
- using UnityEngine;
- namespace Unity.RenderStreaming.RuntimeTest.Signaling
- {
- internal class MockSignaling : ISignaling
- {
- interface IMockSignalingManager
- {
- Task Add(MockSignaling signaling);
- Task Remove(MockSignaling signaling);
- Task OpenConnection(MockSignaling signaling, string connectionId);
- Task CloseConnection(MockSignaling signaling, string connectionId);
- Task Offer(MockSignaling owner, DescData data);
- Task Answer(MockSignaling owner, DescData data);
- Task Candidate(MockSignaling owner, CandidateData data);
- }
- class MockPublicSignalingManager : IMockSignalingManager
- {
- private List<MockSignaling> list = new List<MockSignaling>();
- private const int MillisecondsDelay = 10;
- public async Task Add(MockSignaling signaling)
- {
- await Task.Delay(MillisecondsDelay);
- list.Add(signaling);
- signaling.OnStart?.Invoke(signaling);
- }
- public async Task Remove(MockSignaling signaling)
- {
- await Task.Delay(MillisecondsDelay);
- list.Remove(signaling);
- }
- public async Task OpenConnection(MockSignaling signaling, string connectionId)
- {
- await Task.Delay(MillisecondsDelay);
- signaling.OnCreateConnection?.Invoke(signaling, connectionId, true);
- }
- public async Task CloseConnection(MockSignaling signaling, string connectionId)
- {
- await Task.Delay(MillisecondsDelay);
- foreach (var element in list)
- {
- element.OnDestroyConnection?.Invoke(element, connectionId);
- }
- }
- public async Task Offer(MockSignaling owner, DescData data)
- {
- await Task.Delay(MillisecondsDelay);
- data.polite = false;
- foreach (var signaling in list.Where(e => e != owner))
- {
- signaling.OnOffer?.Invoke(signaling, data);
- }
- }
- public async Task Answer(MockSignaling owner, DescData data)
- {
- await Task.Delay(MillisecondsDelay);
- foreach (var signaling in list.Where(e => e != owner))
- {
- signaling.OnAnswer?.Invoke(signaling, data);
- }
- }
- public async Task Candidate(MockSignaling owner, CandidateData data)
- {
- await Task.Delay(MillisecondsDelay);
- foreach (var signaling in list.Where(e => e != owner))
- {
- signaling.OnIceCandidate?.Invoke(signaling, data);
- }
- }
- }
- class MockPrivateSignalingManager : IMockSignalingManager
- {
- private Dictionary<string, List<MockSignaling>> connectionIds = new Dictionary<string, List<MockSignaling>>();
- private const int MillisecondsDelay = 10;
- public async Task Add(MockSignaling signaling)
- {
- await Task.Delay(MillisecondsDelay);
- signaling.OnStart?.Invoke(signaling);
- }
- public async Task Remove(MockSignaling signaling)
- {
- await Task.Delay(MillisecondsDelay);
- }
- public async Task OpenConnection(MockSignaling signaling, string connectionId)
- {
- await Task.Delay(MillisecondsDelay);
- bool peerExists = connectionIds.TryGetValue(connectionId, out var list);
- if (!peerExists)
- {
- list = new List<MockSignaling>();
- connectionIds.Add(connectionId, list);
- }
- list.Add(signaling);
- signaling.OnCreateConnection?.Invoke(signaling, connectionId, peerExists);
- }
- public async Task CloseConnection(MockSignaling signaling, string connectionId)
- {
- await Task.Delay(MillisecondsDelay);
- bool peerExists = connectionIds.TryGetValue(connectionId, out var list);
- if (!peerExists || !list.Contains(signaling))
- {
- Debug.LogError($"{connectionId} This connection id is not used.");
- }
- foreach (var element in list)
- {
- element.OnDestroyConnection?.Invoke(element, connectionId);
- }
- list.Remove(signaling);
- if (list.Count == 0)
- {
- connectionIds.Remove(connectionId);
- }
- }
- List<MockSignaling> FindList(MockSignaling owner, string connectionId)
- {
- if (!connectionIds.TryGetValue(connectionId, out var list))
- {
- return null;
- }
- list = list.Where(e => e != owner).ToList();
- if (list.Count == 0)
- {
- return null;
- }
- return list;
- }
- public async Task Offer(MockSignaling owner, DescData data)
- {
- await Task.Delay(MillisecondsDelay);
- var list = FindList(owner, data.connectionId);
- if (list == null)
- {
- Debug.LogWarning($"{data.connectionId} This connection id is not ready other session.");
- return;
- }
- data.polite = true;
- foreach (var signaling in list.Where(x => x != owner))
- {
- signaling.OnOffer?.Invoke(signaling, data);
- }
- }
- public async Task Answer(MockSignaling owner, DescData data)
- {
- await Task.Delay(MillisecondsDelay);
- var list = FindList(owner, data.connectionId);
- if (list == null)
- {
- Debug.LogWarning($"{data.connectionId} This connection id is not ready other session.");
- return;
- }
- foreach (var signaling in list.Where(x => x != owner))
- {
- signaling.OnAnswer?.Invoke(signaling, data);
- }
- }
- public async Task Candidate(MockSignaling owner, CandidateData data)
- {
- await Task.Delay(MillisecondsDelay);
- var list = FindList(owner, data.connectionId);
- if (list == null)
- {
- Debug.LogWarning($"{data.connectionId} This connection id is not ready other session.");
- return;
- }
- foreach (var signaling in list.Where(e => e != owner))
- {
- signaling.OnIceCandidate?.Invoke(signaling, data);
- }
- }
- }
- private static IMockSignalingManager manager = null;
- public static void Reset(bool enablePrivateMode)
- {
- if (enablePrivateMode)
- {
- manager = new MockPrivateSignalingManager();
- }
- else
- {
- manager = new MockPublicSignalingManager();
- }
- }
- public string Url { get { return string.Empty; } }
- public float Interval { get { return 0.1f; } }
- static MockSignaling()
- {
- manager = new MockPublicSignalingManager();
- }
- public MockSignaling(SignalingSettings settings = null, SynchronizationContext context = null)
- {
- }
- public void Start()
- {
- manager.Add(this);
- }
- public void Stop()
- {
- manager.Remove(this);
- }
- public event OnStartHandler OnStart;
- public event OnConnectHandler OnCreateConnection;
- public event OnDisconnectHandler OnDestroyConnection;
- public event OnOfferHandler OnOffer;
- public event OnAnswerHandler OnAnswer;
- public event OnIceCandidateHandler OnIceCandidate;
- public void OpenConnection(string connectionId)
- {
- if (string.IsNullOrEmpty(connectionId))
- throw new ArgumentException("connectionId is null or empty.");
- manager.OpenConnection(this, connectionId);
- }
- public void CloseConnection(string connectionId)
- {
- if (string.IsNullOrEmpty(connectionId))
- throw new ArgumentException("connectionId is null or empty.");
- manager.CloseConnection(this, connectionId);
- }
- public void SendOffer(string connectionId, RTCSessionDescription offer)
- {
- if (string.IsNullOrEmpty(connectionId))
- throw new ArgumentException("connectionId is null or empty.");
- DescData data = new DescData
- {
- connectionId = connectionId,
- type = "offer",
- sdp = offer.sdp
- };
- manager.Offer(this, data);
- }
- public void SendAnswer(string connectionId, RTCSessionDescription answer)
- {
- if (string.IsNullOrEmpty(connectionId))
- throw new ArgumentException("connectionId is null or empty.");
- DescData data = new DescData
- {
- connectionId = connectionId,
- type = "answer",
- sdp = answer.sdp
- };
- manager.Answer(this, data);
- }
- public void SendCandidate(string connectionId, RTCIceCandidate candidate)
- {
- if (string.IsNullOrEmpty(connectionId))
- throw new ArgumentException("connectionId is null or empty.");
- CandidateData data = new CandidateData
- {
- connectionId = connectionId,
- candidate = candidate.Candidate,
- sdpMid = candidate.SdpMid,
- sdpMLineIndex = candidate.SdpMLineIndex.GetValueOrDefault()
- };
- manager.Candidate(this, data);
- }
- }
- }
|