Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Events - No way to know when connection state changed

Discussion in 'Unity Render Streaming' started by gnp89, Mar 25, 2022.

  1. gnp89

    gnp89

    Joined:
    Jun 25, 2012
    Posts:
    36
    We're using UnityRenderStreaming to cast from VR to mobile.
    We need our UI and system to be robust and react accordingly to connection state changes.

    We want to show some loading animations while connecting and when connection is complete, change the screen to show the video feed and other stuff.
    There are events for when the video stream starts, but we can't use this as an event for connection established.
    The PeerConnection objects are hidden in the RenderStreaming internal class.
    Can the IceStateChanged event be exposed somehow so that we can manage our app with more fine tweaked responses to WebRTC state?
     
  2. kazuki_unity729

    kazuki_unity729

    Unity Technologies

    Joined:
    Aug 2, 2018
    Posts:
    803
    IConnectHandler interface call the "OnConnect" method when the connection established.
    Could you make the class to implement the interface to catch the event?
     
  3. LiamWalsh_IA

    LiamWalsh_IA

    Joined:
    Oct 5, 2021
    Posts:
    1
    Can you elaborate on this at all? I can't see any examples using this interface in the example code
     
  4. kannan-xiao4

    kannan-xiao4

    Unity Technologies

    Joined:
    Nov 5, 2020
    Posts:
    76
    @LiamWalsh_IA
    How about copying Broadcast.cs, implementing the IConnectHandler interface, and using it instead of the existing Broadcast?

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using System.Linq;
    3. using UnityEngine;
    4.  
    5. namespace Unity.RenderStreaming
    6. {
    7.     public class BroadcastCopy : SignalingHandlerBase,
    8.         IOfferHandler, IAddChannelHandler, IDisconnectHandler, IDeletedConnectionHandler,
    9.         IAddReceiverHandler, IConnectHandler
    10.     {
    11.         [SerializeField] private List<Component> streams = new List<Component>();
    12.  
    13.         private List<string> connectionIds = new List<string>();
    14.  
    15.         public override IEnumerable<Component> Streams => streams;
    16.  
    17.         public void AddComponent(Component component)
    18.         {
    19.             streams.Add(component);
    20.         }
    21.  
    22.         public void RemoveComponent(Component component)
    23.         {
    24.             streams.Remove(component);
    25.         }
    26.  
    27.         public void OnDeletedConnection(SignalingEventData eventData)
    28.         {
    29.             Disconnect(eventData.connectionId);
    30.         }
    31.  
    32.         public void OnDisconnect(SignalingEventData eventData)
    33.         {
    34.             Disconnect(eventData.connectionId);
    35.         }
    36.  
    37.         private void Disconnect(string connectionId)
    38.         {
    39.             if (!connectionIds.Contains(connectionId))
    40.                 return;
    41.             connectionIds.Remove(connectionId);
    42.  
    43.             foreach (var sender in streams.OfType<IStreamSender>())
    44.             {
    45.                 RemoveSender(connectionId, sender);
    46.             }
    47.             foreach (var receiver in streams.OfType<IStreamReceiver>())
    48.             {
    49.                 RemoveReceiver(connectionId, receiver);
    50.             }
    51.             foreach (var channel in streams.OfType<IDataChannel>())
    52.             {
    53.                 RemoveChannel(connectionId, channel);
    54.             }
    55.         }
    56.  
    57.         public void OnAddReceiver(SignalingEventData data)
    58.         {
    59.             var track = data.transceiver.Receiver.Track;
    60.             IStreamReceiver receiver = GetReceiver(track.Kind);
    61.             SetReceiver(data.connectionId, receiver, data.transceiver);
    62.         }
    63.  
    64.         public void OnOffer(SignalingEventData data)
    65.         {
    66.             if (connectionIds.Contains(data.connectionId))
    67.             {
    68.                 Debug.Log($"Already answered this connectionId : {data.connectionId}");
    69.                 return;
    70.             }
    71.             connectionIds.Add(data.connectionId);
    72.  
    73.             foreach (var source in streams.OfType<IStreamSender>())
    74.             {
    75.                 AddSender(data.connectionId, source);
    76.             }
    77.             foreach (var channel in streams.OfType<IDataChannel>().Where(c => c.IsLocal))
    78.             {
    79.                 AddChannel(data.connectionId, channel);
    80.             }
    81.             SendAnswer(data.connectionId);
    82.         }
    83.  
    84.         public void OnAddChannel(SignalingEventData data)
    85.         {
    86.             var channel = streams.OfType<IDataChannel>().
    87.                 FirstOrDefault(r => !r.IsConnected && !r.IsLocal);
    88.             channel?.SetChannel(data.connectionId, data.channel);
    89.         }
    90.  
    91.         IStreamReceiver GetReceiver(WebRTC.TrackKind kind)
    92.         {
    93.             if (kind == WebRTC.TrackKind.Audio)
    94.                 return streams.OfType<AudioStreamReceiver>().First();
    95.             if (kind == WebRTC.TrackKind.Video)
    96.                 return streams.OfType<VideoStreamReceiver>().First();
    97.             throw new System.ArgumentException();
    98.         }
    99.  
    100.         public void OnConnect(SignalingEventData eventData)
    101.         {
    102.             // todo something on connect
    103.         }
    104.     }
    105. }
    106.