using System;
using System.Collections.Generic;
using System.Linq;
using Unity.RenderStreaming.Signaling;
using UnityEngine;
namespace Unity.RenderStreaming
{
[Serializable, SignalingType("http")]
public class HttpSignalingSettings : SignalingSettings
{
///
///
///
public override Type signalingClass => typeof(HttpSignaling);
///
///
///
public override IReadOnlyCollection iceServers => m_iceServers;
///
///
///
public string url => m_url;
///
/// Polling interval
///
public int interval => m_interval;
[SerializeField]
private int m_interval;
[SerializeField]
protected string m_url;
[SerializeField]
protected IceServer[] m_iceServers;
///
///
///
///
///
///
public HttpSignalingSettings(string url, IceServer[] iceServers = null, int interval = 5000)
{
if (url == null)
throw new ArgumentNullException("url");
if (!Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
throw new ArgumentException("url is not well formed Uri");
m_url = url;
m_iceServers = iceServers == null ? Array.Empty() : iceServers.Select(server => server.Clone()).ToArray();
m_interval = interval;
}
///
///
///
public HttpSignalingSettings()
{
m_url = "http://127.0.0.1";
m_iceServers = new[]
{
new IceServer (urls: new[] {"stun:stun.l.google.com:19302"})
};
m_interval = 5000;
}
///
///
///
///
///
public override bool ParseArguments(string[] arguments)
{
if (arguments == null)
throw new ArgumentNullException("arguments");
if (arguments.Length == 0)
throw new ArgumentException("arguments is empty");
if (!CommandLineParser.TryParse(arguments))
return false;
if (CommandLineParser.ImportJson.Value != null)
{
CommandLineInfo info = CommandLineParser.ImportJson.Value.Value;
if (info.signalingUrl != null)
m_url = info.signalingUrl;
if (info.iceServers != null && info.iceServers.Length != 0)
m_iceServers = info.iceServers.Select(v => new IceServer(v)).ToArray();
}
if (CommandLineParser.SignalingUrl.Value != null)
m_url = CommandLineParser.SignalingUrl.Value;
var username = CommandLineParser.IceServerUsername != null
? CommandLineParser.IceServerUsername.Value
: null;
var credential = CommandLineParser.IceServerCredential != null
? CommandLineParser.IceServerCredential.Value
: null;
var credentialType = CommandLineParser.IceServerCredentialType != null
? CommandLineParser.IceServerCredentialType.Value
: null;
var urls = CommandLineParser.IceServerUrls != null
? CommandLineParser.IceServerUrls.Value
: null;
if (m_iceServers.Length > 0)
m_iceServers[0] = m_iceServers[0].Clone(
username: username,
credential: credential,
credentialType: credentialType,
urls: urls);
else
m_iceServers = new IceServer[]
{
new IceServer(
username: username,
credential: credential,
credentialType: credentialType.GetValueOrDefault(),
urls: urls)
};
if (CommandLineParser.PollingInterval.Value != null)
m_interval = CommandLineParser.PollingInterval.Value.Value;
return true;
}
}
}