HttpSignalingSettings.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Unity.RenderStreaming.Signaling;
  5. using UnityEngine;
  6. namespace Unity.RenderStreaming
  7. {
  8. [Serializable, SignalingType("http")]
  9. public class HttpSignalingSettings : SignalingSettings
  10. {
  11. /// <summary>
  12. ///
  13. /// </summary>
  14. public override Type signalingClass => typeof(HttpSignaling);
  15. /// <summary>
  16. ///
  17. /// </summary>
  18. public override IReadOnlyCollection<IceServer> iceServers => m_iceServers;
  19. /// <summary>
  20. ///
  21. /// </summary>
  22. public string url => m_url;
  23. /// <summary>
  24. /// Polling interval
  25. /// </summary>
  26. public int interval => m_interval;
  27. [SerializeField]
  28. private int m_interval;
  29. [SerializeField]
  30. protected string m_url;
  31. [SerializeField]
  32. protected IceServer[] m_iceServers;
  33. /// <summary>
  34. ///
  35. /// </summary>
  36. /// <param name="url"></param>
  37. /// <param name="iceServers"></param>
  38. /// <param name="interval"></param>
  39. public HttpSignalingSettings(string url, IceServer[] iceServers = null, int interval = 5000)
  40. {
  41. if (url == null)
  42. throw new ArgumentNullException("url");
  43. if (!Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
  44. throw new ArgumentException("url is not well formed Uri");
  45. m_url = url;
  46. m_iceServers = iceServers == null ? Array.Empty<IceServer>() : iceServers.Select(server => server.Clone()).ToArray();
  47. m_interval = interval;
  48. }
  49. /// <summary>
  50. ///
  51. /// </summary>
  52. public HttpSignalingSettings()
  53. {
  54. m_url = "http://127.0.0.1";
  55. m_iceServers = new[]
  56. {
  57. new IceServer (urls: new[] {"stun:stun.l.google.com:19302"})
  58. };
  59. m_interval = 5000;
  60. }
  61. /// <summary>
  62. ///
  63. /// </summary>
  64. /// <param name="argumetns"></param>
  65. /// <returns></returns>
  66. public override bool ParseArguments(string[] arguments)
  67. {
  68. if (arguments == null)
  69. throw new ArgumentNullException("arguments");
  70. if (arguments.Length == 0)
  71. throw new ArgumentException("arguments is empty");
  72. if (!CommandLineParser.TryParse(arguments))
  73. return false;
  74. if (CommandLineParser.ImportJson.Value != null)
  75. {
  76. CommandLineInfo info = CommandLineParser.ImportJson.Value.Value;
  77. if (info.signalingUrl != null)
  78. m_url = info.signalingUrl;
  79. if (info.iceServers != null && info.iceServers.Length != 0)
  80. m_iceServers = info.iceServers.Select(v => new IceServer(v)).ToArray();
  81. }
  82. if (CommandLineParser.SignalingUrl.Value != null)
  83. m_url = CommandLineParser.SignalingUrl.Value;
  84. var username = CommandLineParser.IceServerUsername != null
  85. ? CommandLineParser.IceServerUsername.Value
  86. : null;
  87. var credential = CommandLineParser.IceServerCredential != null
  88. ? CommandLineParser.IceServerCredential.Value
  89. : null;
  90. var credentialType = CommandLineParser.IceServerCredentialType != null
  91. ? CommandLineParser.IceServerCredentialType.Value
  92. : null;
  93. var urls = CommandLineParser.IceServerUrls != null
  94. ? CommandLineParser.IceServerUrls.Value
  95. : null;
  96. if (m_iceServers.Length > 0)
  97. m_iceServers[0] = m_iceServers[0].Clone(
  98. username: username,
  99. credential: credential,
  100. credentialType: credentialType,
  101. urls: urls);
  102. else
  103. m_iceServers = new IceServer[]
  104. {
  105. new IceServer(
  106. username: username,
  107. credential: credential,
  108. credentialType: credentialType.GetValueOrDefault(),
  109. urls: urls)
  110. };
  111. if (CommandLineParser.PollingInterval.Value != null)
  112. m_interval = CommandLineParser.PollingInterval.Value.Value;
  113. return true;
  114. }
  115. }
  116. }