Multiplay.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System.Linq;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Unity.RenderStreaming.Samples
  5. {
  6. class Multiplay : SignalingHandlerBase,
  7. IOfferHandler, IAddChannelHandler, IDisconnectHandler, IDeletedConnectionHandler
  8. {
  9. [SerializeField] GameObject prefab;
  10. private List<string> connectionIds = new List<string>();
  11. private List<Component> streams = new List<Component>();
  12. private Dictionary<string, GameObject> dictObj = new Dictionary<string, GameObject>();
  13. private RenderStreamingSettings settings;
  14. void Awake()
  15. {
  16. settings = SampleManager.Instance.Settings;
  17. }
  18. public override IEnumerable<Component> Streams => streams;
  19. public void OnDeletedConnection(SignalingEventData eventData)
  20. {
  21. Disconnect(eventData.connectionId);
  22. }
  23. public void OnDisconnect(SignalingEventData eventData)
  24. {
  25. Disconnect(eventData.connectionId);
  26. }
  27. private void Disconnect(string connectionId)
  28. {
  29. if (!connectionIds.Contains(connectionId))
  30. return;
  31. connectionIds.Remove(connectionId);
  32. var obj = dictObj[connectionId];
  33. var sender = obj.GetComponentInChildren<StreamSenderBase>();
  34. var inputChannel = obj.GetComponentInChildren<InputReceiver>();
  35. var multiplayChannel = obj.GetComponentInChildren<MultiplayChannel>();
  36. dictObj.Remove(connectionId);
  37. Object.Destroy(obj);
  38. RemoveSender(connectionId, sender);
  39. RemoveChannel(connectionId, inputChannel);
  40. RemoveChannel(connectionId, multiplayChannel);
  41. streams.Remove(sender);
  42. streams.Remove(inputChannel);
  43. streams.Remove(multiplayChannel);
  44. if (ExistConnection(connectionId))
  45. DeleteConnection(connectionId);
  46. }
  47. public void OnOffer(SignalingEventData data)
  48. {
  49. if (connectionIds.Contains(data.connectionId))
  50. {
  51. Debug.Log($"Already answered this connectionId : {data.connectionId}");
  52. return;
  53. }
  54. connectionIds.Add(data.connectionId);
  55. var initialPosition = new Vector3(0, 3, 0);
  56. var newObj = Instantiate(prefab, initialPosition, Quaternion.identity);
  57. dictObj.Add(data.connectionId, newObj);
  58. var sender = newObj.GetComponentInChildren<StreamSenderBase>();
  59. if (sender is VideoStreamSender videoStreamSender && settings != null)
  60. {
  61. videoStreamSender.width = (uint)settings.StreamSize.x;
  62. videoStreamSender.height = (uint)settings.StreamSize.y;
  63. videoStreamSender.SetCodec(settings.SenderVideoCodec);
  64. }
  65. var inputChannel = newObj.GetComponentInChildren<InputReceiver>();
  66. var multiplayChannel = newObj.GetComponentInChildren<MultiplayChannel>();
  67. var playerController = newObj.GetComponentInChildren<PlayerController>();
  68. if (multiplayChannel.OnChangeLabel == null)
  69. multiplayChannel.OnChangeLabel = new ChangeLabelEvent();
  70. multiplayChannel.OnChangeLabel.AddListener(playerController.SetLabel);
  71. streams.Add(sender);
  72. streams.Add(inputChannel);
  73. streams.Add(multiplayChannel);
  74. AddSender(data.connectionId, sender);
  75. AddChannel(data.connectionId, inputChannel);
  76. AddChannel(data.connectionId, multiplayChannel);
  77. SendAnswer(data.connectionId);
  78. }
  79. /// todo(kazuki)::
  80. public void OnAddChannel(SignalingEventData data)
  81. {
  82. var obj = dictObj[data.connectionId];
  83. var channels = obj.GetComponentsInChildren<IDataChannel>();
  84. var channel = channels.FirstOrDefault(_ => !_.IsLocal && !_.IsConnected);
  85. channel?.SetChannel(data);
  86. }
  87. }
  88. }