Skip to content

Commit

Permalink
fix: Add SampleManager to manage global settings for sample (Unity-Te…
Browse files Browse the repository at this point in the history
…chnologies#772)

* Add scenemanager

* fix bug

* fix
  • Loading branch information
karasusan authored Sep 13, 2022
1 parent 6e110e7 commit 4118d94
Show file tree
Hide file tree
Showing 12 changed files with 196 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ internal class ARFoundationSample : MonoBehaviour
#pragma warning restore 0649

private string _connectionId;
private RenderStreamingSettings settings;

void Awake()
{
Expand All @@ -39,13 +40,15 @@ void Awake()
stopButton.gameObject.SetActive(false);

receiveVideoViewer.OnUpdateReceiveTexture += texture => remoteVideoImage.texture = texture;

settings = SampleManager.Instance.Settings;
}

IEnumerator Start()
{
if (!renderStreaming.runOnAwake)
{
renderStreaming.Run(signaling: RenderStreamingSettings.Signaling);
renderStreaming.Run(signaling: settings?.Signaling);
}

if ((ARSession.state == ARSessionState.None ) ||
Expand Down Expand Up @@ -117,7 +120,8 @@ private void UpdateQuaternion(InputAction.CallbackContext context)

void CreateConnection()
{
receiveVideoViewer.SetCodec(RenderStreamingSettings.ReceiverVideoCodec);
if(settings != null)
receiveVideoViewer.SetCodec(settings.ReceiverVideoCodec);

_connectionId = System.Guid.NewGuid().ToString("N");
connection.CreateConnection(_connectionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class BidirectionalSample : MonoBehaviour
#pragma warning restore 0649

private string connectionId;
private RenderStreamingSettings settings;

void Awake()
{
Expand All @@ -51,8 +52,12 @@ void Awake()
webCamStreamer.OnStartedStream += id => receiveVideoViewer.enabled = true;
webCamStreamer.OnStartedStream += _ => localVideoImage.texture = webCamStreamer.sourceWebCamTexture;

webCamStreamer.width = (uint)RenderStreamingSettings.StreamSize.x;
webCamStreamer.height = (uint)RenderStreamingSettings.StreamSize.y;
settings = SampleManager.Instance.Settings;
if (settings != null)
{
webCamStreamer.width = (uint)settings.StreamSize.x;
webCamStreamer.height = (uint)settings.StreamSize.y;
}

receiveVideoViewer.OnUpdateReceiveTexture += texture => remoteVideoImage.texture = texture;

Expand All @@ -71,16 +76,19 @@ void Start()
{
if (renderStreaming.runOnAwake)
return;
renderStreaming.Run(signaling: RenderStreamingSettings.Signaling);
renderStreaming.Run(signaling: settings?.Signaling);
}

private void SetUp()
{
setUpButton.interactable = false;
hangUpButton.interactable = true;
connectionIdInput.interactable = false;
receiveVideoViewer.SetCodec(RenderStreamingSettings.ReceiverVideoCodec);
webCamStreamer.SetCodec(RenderStreamingSettings.SenderVideoCodec);
if(settings != null)
{
receiveVideoViewer.SetCodec(settings.ReceiverVideoCodec);
webCamStreamer.SetCodec(settings.SenderVideoCodec);
}

singleConnection.CreateConnection(connectionId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class BroadcastSample : MonoBehaviour
{ "2560x1440", new Vector2Int(2560, 1440) },
};

private RenderStreamingSettings settings;

private void Awake()
{
Expand All @@ -78,13 +79,16 @@ private void Awake()
.WithInterface(XRUtilities.InterfaceMatchAnyVersion)
);
#endif

if (videoStreamSender.source != VideoStreamSource.Texture)
settings = SampleManager.Instance.Settings;
if (settings != null)
{
videoStreamSender.width = (uint)RenderStreamingSettings.StreamSize.x;
videoStreamSender.height = (uint)RenderStreamingSettings.StreamSize.y;
if (videoStreamSender.source != VideoStreamSource.Texture)
{
videoStreamSender.width = (uint)settings.StreamSize.x;
videoStreamSender.height = (uint)settings.StreamSize.y;
}
videoStreamSender.SetCodec(settings.SenderVideoCodec);
}
videoStreamSender.SetCodec(RenderStreamingSettings.SenderVideoCodec);

bandwidthSelector.options = bandwidthOptions
.Select(pair => new Dropdown.OptionData { text = pair.Key })
Expand Down Expand Up @@ -139,7 +143,7 @@ private void Start()
{
if (renderStreaming.runOnAwake)
return;
renderStreaming.Run(signaling: RenderStreamingSettings.Signaling);
renderStreaming.Run(signaling: settings?.Signaling);

inputReceiver.OnStartedChannel += OnStartedChannel;
}
Expand Down
8 changes: 6 additions & 2 deletions com.unity.renderstreaming/Samples~/Example/Gyro/GyroSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class GyroSample : MonoBehaviour
[SerializeField] private Text textVelocityZ;
[SerializeField] private InputAction vector3Action;
#pragma warning restore 0649
private RenderStreamingSettings settings;

void Awake()
{
Expand All @@ -30,6 +31,8 @@ void Awake()
Debug.LogError("Gyroscope is not supported on this device.");
sendOfferButton.onClick.AddListener(SendOffer);
receiveVideoViewer.OnUpdateReceiveTexture += texture => remoteVideoImage.texture = texture;

settings = SampleManager.Instance.Settings;
}

void OnDestroy()
Expand All @@ -43,7 +46,7 @@ void Start()
{
if (renderStreaming.runOnAwake)
return;
renderStreaming.Run(signaling: RenderStreamingSettings.Signaling);
renderStreaming.Run(signaling: settings?.Signaling);
}

void OnEnable()
Expand Down Expand Up @@ -75,7 +78,8 @@ private void UpdateVector3(InputAction.CallbackContext context)

void SendOffer()
{
receiveVideoViewer.SetCodec(RenderStreamingSettings.ReceiverVideoCodec);
if(settings != null)
receiveVideoViewer.SetCodec(settings.ReceiverVideoCodec);

var connectionId = System.Guid.NewGuid().ToString("N");
connection.CreateConnection(connectionId);
Expand Down
15 changes: 11 additions & 4 deletions com.unity.renderstreaming/Samples~/Example/Multiplay/Multiplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ class Multiplay : SignalingHandlerBase,
private List<Component> streams = new List<Component>();
private Dictionary<string, GameObject> dictObj = new Dictionary<string, GameObject>();

private RenderStreamingSettings settings;

void Awake()
{
settings = SampleManager.Instance.Settings;
}

public override IEnumerable<Component> Streams => streams;

public void OnDeletedConnection(SignalingEventData eventData)
Expand Down Expand Up @@ -66,11 +73,11 @@ public void OnOffer(SignalingEventData data)

var sender = newObj.GetComponentInChildren<StreamSenderBase>();

if (sender is VideoStreamSender videoStreamSender)
if (sender is VideoStreamSender videoStreamSender && settings != null)
{
videoStreamSender.width = (uint)RenderStreamingSettings.StreamSize.x;
videoStreamSender.height = (uint)RenderStreamingSettings.StreamSize.y;
videoStreamSender.SetCodec(RenderStreamingSettings.SenderVideoCodec);
videoStreamSender.width = (uint)settings.StreamSize.x;
videoStreamSender.height = (uint)settings.StreamSize.y;
videoStreamSender.SetCodec(settings.SenderVideoCodec);
}

var inputChannel = newObj.GetComponentInChildren<InputReceiver>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ enum Role
Guest = 1
}

private RenderStreamingSettings settings;


private void Awake()
{
settings = SampleManager.Instance.Settings;
}

void Start()
{
buttonStart.onClick.AddListener(OnClickButtonStart);
Expand Down Expand Up @@ -79,7 +87,7 @@ void SetUpHost(string username)
playerController.CheckPairedDevices();

statsUI.AddSignalingHandler(handler);
renderStreaming.Run(signaling: RenderStreamingSettings.Signaling,
renderStreaming.Run(signaling: settings?.Signaling,
handlers: new SignalingHandlerBase[] {handler}
);
}
Expand All @@ -90,7 +98,7 @@ IEnumerator SetUpGuest(string username, string connectionId)
var handler = guestPlayer.GetComponent<SingleConnection>();

statsUI.AddSignalingHandler(handler);
renderStreaming.Run(signaling: RenderStreamingSettings.Signaling,
renderStreaming.Run(signaling: settings?.Signaling,
handlers: new SignalingHandlerBase[] {handler}
);

Expand All @@ -101,7 +109,8 @@ IEnumerator SetUpGuest(string username, string connectionId)
var channel = guestPlayer.GetComponent<MultiplayChannel>();
channel.OnStartedChannel += _ => { StartCoroutine(ChangeLabel(channel, username)); };

receiveVideoViewer.SetCodec(RenderStreamingSettings.ReceiverVideoCodec);
if(settings != null)
receiveVideoViewer.SetCodec(settings.ReceiverVideoCodec);

// todo(kazuki):
yield return new WaitForSeconds(1f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class ReceiverSample : MonoBehaviour

private string connectionId;
private InputSender inputSender;
private RenderStreamingSettings settings;

void Awake()
{
Expand All @@ -59,13 +60,15 @@ void Awake()

inputSender = GetComponent<InputSender>();
inputSender.OnStartedChannel += OnStartedChannel;

settings = SampleManager.Instance.Settings;
}

void Start()
{
if (renderStreaming.runOnAwake)
return;
renderStreaming.Run(signaling: RenderStreamingSettings.Signaling);
renderStreaming.Run(signaling: settings?.Signaling);
}

void OnUpdateReceiveTexture(Texture texture)
Expand Down Expand Up @@ -95,7 +98,8 @@ private void OnStart()
connectionIdInput.text = connectionId;
}
connectionIdInput.interactable = false;
receiveVideoViewer.SetCodec(RenderStreamingSettings.ReceiverVideoCodec);
if(settings != null)
receiveVideoViewer.SetCodec(settings.ReceiverVideoCodec);
receiveAudioViewer.targetAudioSource = remoteAudioSource;

connection.CreateConnection(connectionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,18 @@ class RenderPipelineSample : MonoBehaviour
{ "2560x1440", new Vector2Int(2560, 1440) },
};

private RenderStreamingSettings settings;

private void Awake()
{
videoStreamSender.width = (uint)RenderStreamingSettings.StreamSize.x;
videoStreamSender.height = (uint)RenderStreamingSettings.StreamSize.y;
videoStreamSender.SetCodec(RenderStreamingSettings.SenderVideoCodec);
settings = SampleManager.Instance.Settings;

if(settings != null)
{
videoStreamSender.width = (uint)settings.StreamSize.x;
videoStreamSender.height = (uint)settings.StreamSize.y;
videoStreamSender.SetCodec(settings.SenderVideoCodec);
}

bandwidthSelector.options = bandwidthOptions
.Select(pair => new Dropdown.OptionData { text = pair.Key })
Expand Down Expand Up @@ -114,7 +121,7 @@ private void Start()
{
if (!renderStreaming.runOnAwake)
{
renderStreaming.Run(signaling: RenderStreamingSettings.Signaling);
renderStreaming.Run(signaling: settings?.Signaling);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace Unity.RenderStreaming.Samples
{

internal class SampleManager
{
static SampleManager s_instance;

public static SampleManager Instance
{
get
{
if (s_instance == null)
s_instance = new SampleManager();
return s_instance;
}
}

public RenderStreamingSettings Settings
{
get
{
return m_settings;
}
}

RenderStreamingSettings m_settings;

public void Initialize()
{
if(m_settings == null)
m_settings = new RenderStreamingSettings();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4118d94

Please sign in to comment.