WebSocketSignalingSettings.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. /// <summary>
  9. ///
  10. /// </summary>
  11. [Serializable, SignalingType("websocket")]
  12. public class WebSocketSignalingSettings : SignalingSettings
  13. {
  14. /// <summary>
  15. ///
  16. /// </summary>
  17. public override Type signalingClass => typeof(WebSocketSignaling);
  18. /// <summary>
  19. ///
  20. /// </summary>
  21. public override IReadOnlyCollection<IceServer> iceServers => m_iceServers;
  22. /// <summary>
  23. ///
  24. /// </summary>
  25. public string url => m_url;
  26. [SerializeField]
  27. protected string m_url;
  28. [SerializeField]
  29. protected IceServer[] m_iceServers;
  30. /// <summary>
  31. ///
  32. /// </summary>
  33. /// <param name="url"></param>
  34. /// <param name="iceServers"></param>
  35. public WebSocketSignalingSettings(string url, IceServer[] iceServers = null)
  36. {
  37. if (url == null)
  38. throw new ArgumentNullException("url");
  39. if (!Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
  40. throw new ArgumentException("url is not well formed Uri");
  41. m_url = url;
  42. m_iceServers = iceServers == null ? Array.Empty<IceServer>() : iceServers.Select(server => server.Clone()).ToArray();
  43. }
  44. /// <summary>
  45. ///
  46. /// </summary>
  47. public WebSocketSignalingSettings()
  48. {
  49. m_url = "ws://127.0.0.1";
  50. m_iceServers = new[]
  51. {
  52. new IceServer (urls: new[] {"stun:stun.l.google.com:19302"})
  53. };
  54. }
  55. public override bool ParseArguments(string[] arguments)
  56. {
  57. if (arguments == null)
  58. throw new ArgumentNullException("arguments");
  59. if (arguments.Length == 0)
  60. throw new ArgumentException("arguments is empty");
  61. if (!CommandLineParser.TryParse(arguments))
  62. return false;
  63. if (CommandLineParser.ImportJson.Value != null)
  64. {
  65. CommandLineInfo info = CommandLineParser.ImportJson.Value.Value;
  66. if(info.signalingUrl != null)
  67. m_url = info.signalingUrl;
  68. if(info.iceServers != null && info.iceServers.Length != 0)
  69. m_iceServers = info.iceServers.Select(v => new IceServer(v)).ToArray();
  70. }
  71. if (CommandLineParser.SignalingUrl.Value != null)
  72. m_url = CommandLineParser.SignalingUrl.Value;
  73. var username = CommandLineParser.IceServerUsername != null
  74. ? CommandLineParser.IceServerUsername.Value
  75. : null;
  76. var credential = CommandLineParser.IceServerCredential != null
  77. ? CommandLineParser.IceServerCredential.Value
  78. : null;
  79. var credentialType = CommandLineParser.IceServerCredentialType != null
  80. ? CommandLineParser.IceServerCredentialType.Value
  81. : null;
  82. var urls = CommandLineParser.IceServerUrls != null
  83. ? CommandLineParser.IceServerUrls.Value
  84. : null;
  85. if(m_iceServers.Length > 0)
  86. m_iceServers[0] = m_iceServers[0].Clone(
  87. username:username,
  88. credential:credential,
  89. credentialType: credentialType,
  90. urls:urls);
  91. else
  92. m_iceServers = new IceServer[]
  93. {
  94. new IceServer(
  95. username: username,
  96. credential: credential,
  97. credentialType: credentialType.GetValueOrDefault(),
  98. urls: urls)
  99. };
  100. return true;
  101. }
  102. }
  103. }