|
| 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 | +} |
0 commit comments