Skip to content

Commit

Permalink
Game ID Added to VR Broadcast (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
vmohan7 authored Aug 10, 2021
1 parent d75c0e0 commit ed59b5b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
29 changes: 27 additions & 2 deletions Runtime/Scripts/VRBroadcast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Linq;
using UnityEngine;
using Unity.RenderStreaming;
using Newtonsoft.Json;

namespace FusedVR.VRStreaming {
/// <summary>
Expand All @@ -21,6 +22,14 @@ namespace FusedVR.VRStreaming {
public class VRBroadcast : SignalingHandlerBase,
IOfferHandler, IAddChannelHandler, IDisconnectHandler, IDeletedConnectionHandler {

#region Variables
/// <summary>
/// Check if client is listening for the same GameID
/// </summary>
[SerializeField]
[Tooltip("This ID should match the input from the Web Browser. Leave blank to match against anything")]
private string gameID = "";

/// <summary>
/// Streams (video, audio, data) that need to be sent to the client
/// </summary>
Expand All @@ -31,6 +40,7 @@ public class VRBroadcast : SignalingHandlerBase,
/// List of all connectionIds that are connected
/// </summary>
private List<string> connectionIds = new List<string>();
#endregion

#region Disconnect
public void OnDeletedConnection(SignalingEventData eventData) {
Expand Down Expand Up @@ -63,13 +73,20 @@ private void Disconnect(string connectionId) {
/// Determines whether to accept offer and if so that to apply sources and submit answer
/// </summary>
public void OnOffer(SignalingEventData data) {

string inputGID = GetGameID(data.sdp);
if ( gameID.Length != 0 && inputGID != gameID) {
Debug.Log($"Offer Doesn't Match My GameID : {inputGID}");
return;
}

if (connectionIds.Count >= 1) { //if there is more than 1 connection, let's skip this offer
Debug.Log($"Already answered this connectionId : {connectionIds[0]}");
Debug.LogWarning($"Already answered this connectionId : {connectionIds[0]}");
return;
}

if (connectionIds.Contains(data.connectionId)) { //if connection is already connected, skip offer
Debug.Log($"Already answered this connectionId : {data.connectionId}");
Debug.LogWarning($"Already answered this connectionId : {data.connectionId}");
return;
}
connectionIds.Add(data.connectionId); //confirm we will use this connection
Expand All @@ -93,5 +110,13 @@ public void OnAddChannel(SignalingEventData data) {
FirstOrDefault(r => r.Channel == null && !r.IsLocal);
channel?.SetChannel(data.connectionId, data.channel);
}

private string GetGameID(string sdp) {
string[] parameters = sdp.Split('\n'); //split on new line
string customParam = parameters[parameters.Length - 2].Split('=')[1]; //get line before last new line and split on =
string value = ""; //get dictionary value
JsonConvert.DeserializeObject<Dictionary<string, string>>(customParam).TryGetValue("user", out value);
return value;
}
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"displayName": "FusedVR Streaming",
"description": "This VR Streaming Package contains a minimal SDK & prefabs to implement VR Streaming (CloudXR) using WebRTC and Unity Remote Rendering",
"dependencies": {
"com.unity.renderstreaming": "3.0.1-preview"
"com.unity.renderstreaming": "3.0.1-preview",
"com.unity.nuget.newtonsoft-json": "2.0.0"
},
"keywords": [
"vr",
Expand Down

0 comments on commit ed59b5b

Please sign in to comment.