Skip to content

Commit a67f8dd

Browse files
authored
Add a VR Broadcaster Component that only broadcasts to first connected client (#1)
* Add Broadcast Logic to accept only 1 connection * Attach VR Broadcaster to Render Streaming Tested with 2 Unity instances running and both client successfully connected to the respective instance using just one WebRTC server
1 parent cdd3a7e commit a67f8dd

File tree

3 files changed

+112
-4
lines changed

3 files changed

+112
-4
lines changed

Runtime/Prefabs/Render Streaming Services.prefab

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ GameObject:
662662
m_Component:
663663
- component: {fileID: 1793859183747734309}
664664
- component: {fileID: 1793859183747734314}
665-
- component: {fileID: 1793859183747734308}
665+
- component: {fileID: 48972029}
666666
m_Layer: 0
667667
m_Name: RenderStreaming
668668
m_TagString: Untagged
@@ -707,9 +707,9 @@ MonoBehaviour:
707707
interval: 5
708708
hardwareEncoderSupport: 1
709709
handlers:
710-
- {fileID: 1793859183747734308}
710+
- {fileID: 48972029}
711711
runOnAwake: 1
712-
--- !u!114 &1793859183747734308
712+
--- !u!114 &48972029
713713
MonoBehaviour:
714714
m_ObjectHideFlags: 0
715715
m_CorrespondingSourceObject: {fileID: 0}
@@ -718,7 +718,7 @@ MonoBehaviour:
718718
m_GameObject: {fileID: 1793859183747734311}
719719
m_Enabled: 1
720720
m_EditorHideFlags: 0
721-
m_Script: {fileID: 11500000, guid: c2307e7ad91222841b562bebd81a69a0, type: 3}
721+
m_Script: {fileID: 11500000, guid: 31b94e633688a294abc5410d3190b6f5, type: 3}
722722
m_Name:
723723
m_EditorClassIdentifier:
724724
streams:

Runtime/Scripts/VRBroadcast.cs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* Copyright 2021 Vasanth Mohan. All rights and licenses reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*/
10+
11+
using System.Collections.Generic;
12+
using System.Linq;
13+
using UnityEngine;
14+
using Unity.RenderStreaming;
15+
16+
namespace FusedVR.VRStreaming {
17+
/// <summary>
18+
/// VRBroadcast is very similar to the Broadcast script included with Render Streaming.
19+
/// This script is responsible for listening for offers from the client and choosing when to respond.
20+
/// </summary>
21+
public class VRBroadcast : SignalingHandlerBase,
22+
IOfferHandler, IAddChannelHandler, IDisconnectHandler, IDeletedConnectionHandler {
23+
24+
/// <summary>
25+
/// Streams (video, audio, data) that need to be sent to the client
26+
/// </summary>
27+
[SerializeField]
28+
private List<Component> streams = new List<Component>();
29+
30+
/// <summary>
31+
/// List of all connectionIds that are connected
32+
/// </summary>
33+
private List<string> connectionIds = new List<string>();
34+
35+
#region Disconnect
36+
public void OnDeletedConnection(SignalingEventData eventData) {
37+
Disconnect(eventData.connectionId);
38+
}
39+
40+
public void OnDisconnect(SignalingEventData eventData) {
41+
Disconnect(eventData.connectionId);
42+
}
43+
44+
private void Disconnect(string connectionId) {
45+
if (!connectionIds.Contains(connectionId))
46+
return;
47+
connectionIds.Remove(connectionId);
48+
49+
foreach (var source in streams.OfType<IStreamSource>()) {
50+
source.SetSender(connectionId, null);
51+
}
52+
foreach (var receiver in streams.OfType<IStreamReceiver>()) {
53+
receiver.SetReceiver(connectionId, null);
54+
}
55+
foreach (var channel in streams.OfType<IDataChannel>()) {
56+
channel.SetChannel(connectionId, null);
57+
}
58+
}
59+
#endregion
60+
61+
/// <summary>
62+
/// Event that is called when an Offer is made by a client
63+
/// Determines whether to accept offer and if so that to apply sources and submit answer
64+
/// </summary>
65+
public void OnOffer(SignalingEventData data) {
66+
if (connectionIds.Count >= 1) { //if there is more than 1 connection, let's skip this offer
67+
Debug.Log($"Already answered this connectionId : {connectionIds[0]}");
68+
return;
69+
}
70+
71+
if (connectionIds.Contains(data.connectionId)) { //if connection is already connected, skip offer
72+
Debug.Log($"Already answered this connectionId : {data.connectionId}");
73+
return;
74+
}
75+
connectionIds.Add(data.connectionId); //confirm we will use this connection
76+
77+
foreach (var source in streams.OfType<IStreamSource>()) {
78+
var transceiver = AddTrack(data.connectionId, source.Track);
79+
source.SetSender(data.connectionId, transceiver.Sender);
80+
}
81+
foreach (var channel in streams.OfType<IDataChannel>().Where(c => c.IsLocal)) {
82+
var _channel = CreateChannel(data.connectionId, channel.Label);
83+
channel.SetChannel(data.connectionId, _channel);
84+
}
85+
SendAnswer(data.connectionId); //accept offer with an answer
86+
}
87+
88+
/// <summary>
89+
/// Apply Data Channel
90+
/// </summary>
91+
public void OnAddChannel(SignalingEventData data) {
92+
var channel = streams.OfType<IDataChannel>().
93+
FirstOrDefault(r => r.Channel == null && !r.IsLocal);
94+
channel?.SetChannel(data.connectionId, data.channel);
95+
}
96+
}
97+
}

Runtime/Scripts/VRBroadcast.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)