SignalingSettings.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Unity.WebRTC;
  5. using UnityEngine;
  6. namespace Unity.RenderStreaming
  7. {
  8. /// <summary>
  9. /// The attribute is used for commandline argument of "-signalingType".
  10. /// </summary>
  11. public sealed class SignalingTypeAttribute : Attribute
  12. {
  13. /// <summary>
  14. ///
  15. /// </summary>
  16. public string typename => m_typename;
  17. private string m_typename;
  18. /// <summary>
  19. ///
  20. /// </summary>
  21. /// <param name="typename"></param>
  22. public SignalingTypeAttribute(string name)
  23. {
  24. m_typename = name;
  25. }
  26. }
  27. internal sealed class SignalingSettingsAttribute : PropertyAttribute { }
  28. /// <summary>
  29. ///
  30. /// </summary>
  31. public enum IceCredentialType
  32. {
  33. /// <summary>
  34. ///
  35. /// </summary>
  36. Password = 0,
  37. /// <summary>
  38. ///
  39. /// </summary>
  40. OAuth = 1
  41. }
  42. /// <summary>
  43. ///
  44. /// </summary>
  45. [Serializable]
  46. public class IceServer
  47. {
  48. /// <summary>
  49. ///
  50. /// </summary>
  51. public IReadOnlyCollection<string> urls => m_urls;
  52. /// <summary>
  53. ///
  54. /// </summary>
  55. public string username => m_username;
  56. /// <summary>
  57. ///
  58. /// </summary>
  59. public IceCredentialType credentialType => m_credentialType;
  60. /// <summary>
  61. ///
  62. /// </summary>
  63. public string credential => m_credential;
  64. [SerializeField]
  65. private string[] m_urls;
  66. [SerializeField]
  67. private string m_username;
  68. [SerializeField]
  69. private IceCredentialType m_credentialType;
  70. [SerializeField]
  71. private string m_credential;
  72. /// <summary>
  73. ///
  74. /// </summary>
  75. /// <param name="server"></param>
  76. public static implicit operator RTCIceServer(IceServer server)
  77. {
  78. var iceServer = new RTCIceServer
  79. {
  80. urls = server.urls.ToArray(),
  81. username = server.username,
  82. credential = server.credential,
  83. credentialType = (RTCIceCredentialType)server.credentialType
  84. };
  85. return iceServer;
  86. }
  87. /// <summary>
  88. ///
  89. /// </summary>
  90. /// <returns></returns>
  91. public IceServer Clone()
  92. {
  93. return new IceServer(this.urls.ToArray(), this.username, this.credentialType, this.credential);
  94. }
  95. public IceServer Clone(string[] urls = null, string username = null, IceCredentialType? credentialType = null, string credential = null)
  96. {
  97. var server = new IceServer(this.urls.ToArray(), this.username, this.credentialType, this.credential);
  98. if (urls != null)
  99. server.m_urls = urls;
  100. if (username != null)
  101. server.m_username = username;
  102. if (credentialType != null)
  103. server.m_credentialType = credentialType.Value;
  104. if (credential != null)
  105. server.m_credential = credential;
  106. return server;
  107. }
  108. /// <summary>
  109. ///
  110. /// </summary>
  111. /// <param name="urls"></param>
  112. /// <param name="username"></param>
  113. /// <param name="credentialType"></param>
  114. /// <param name="credential"></param>
  115. public IceServer(string[] urls = null, string username = null, IceCredentialType credentialType = IceCredentialType.Password, string credential = null)
  116. {
  117. m_urls = urls?.ToArray();
  118. m_username = username;
  119. m_credential = credential;
  120. m_credentialType = credentialType;
  121. }
  122. internal IceServer(RTCIceServer server)
  123. {
  124. m_urls = server.urls.ToArray();
  125. m_username = server.username;
  126. m_credential = server.credential;
  127. m_credentialType = (IceCredentialType)server.credentialType;
  128. }
  129. }
  130. /// <summary>
  131. ///
  132. /// </summary>
  133. public abstract class SignalingSettings
  134. {
  135. /// <summary>
  136. ///
  137. /// </summary>
  138. public abstract IReadOnlyCollection<IceServer> iceServers { get; }
  139. /// <summary>
  140. ///
  141. /// </summary>
  142. public abstract Type signalingClass { get; }
  143. /// <summary>
  144. ///
  145. /// </summary>
  146. /// <param name="arguments"></param>
  147. /// <returns></returns>
  148. public abstract bool ParseArguments(string[] arguments);
  149. }
  150. internal static class RuntimeTypeCache<T> where T : class
  151. {
  152. private static Type[] s_types;
  153. public static Type[] GetTypesDerivedFrom()
  154. {
  155. if (s_types != null)
  156. return s_types;
  157. s_types = AppDomain.CurrentDomain.GetAssemblies()
  158. .SelectMany(domainAssembly => domainAssembly.GetTypes())
  159. .Where(type => typeof(T).IsAssignableFrom(type) && !type.IsAbstract).ToArray();
  160. return s_types;
  161. }
  162. }
  163. }