Receiver.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Unity.WebRTC;
  5. using Unity.Collections.LowLevel.Unsafe;
  6. using UnityEngine;
  7. using UnityEngine.InputSystem;
  8. using UnityEngine.InputSystem.LowLevel;
  9. using UnityEngine.InputSystem.Utilities;
  10. namespace Unity.RenderStreaming.InputSystem
  11. {
  12. using InputSystem = UnityEngine.InputSystem.InputSystem;
  13. /// <summary>
  14. ///
  15. /// </summary>
  16. class Receiver : InputManager, IDisposable
  17. {
  18. public override event Action<InputRemoting.Message> onMessage;
  19. public new event Action<InputDevice, InputDeviceChange> onDeviceChange;
  20. public new event Action<string, InputControlLayoutChange> onLayoutChange;
  21. private RTCDataChannel _channel;
  22. private readonly List<InputDevice> _remoteDevices = new List<InputDevice>();
  23. private readonly Dictionary<string, string> _remoteLayouts = new Dictionary<string, string>();
  24. private readonly List<string> _registeredRemoteLayout = new List<string>();
  25. private InputPositionCorrector _corrector;
  26. private Action<InputEventPtr, InputDevice> _onEvent;
  27. /// <summary>
  28. ///
  29. /// </summary>
  30. public bool EnableInputPositionCorrection { set; get; }
  31. /// <summary>
  32. ///
  33. /// </summary>
  34. /// <param name="channel"></param>
  35. public Receiver(RTCDataChannel channel)
  36. {
  37. _channel = channel ?? throw new ArgumentNullException("channel is null");
  38. _channel.OnMessage += OnMessage;
  39. _onEvent = (InputEventPtr ptr, InputDevice device) => { base.QueueEvent(ptr); };
  40. _corrector = new InputPositionCorrector(_onEvent);
  41. }
  42. ~Receiver()
  43. {
  44. this.Dispose();
  45. }
  46. public void Dispose()
  47. {
  48. RemoveAllRemoteDevices();
  49. RemoveAllRemoteLayouts();
  50. }
  51. private void OnMessage(byte[] bytes)
  52. {
  53. MessageSerializer.Deserialize(bytes, out var message);
  54. onMessage?.Invoke(message);
  55. }
  56. /// <summary>
  57. ///
  58. /// </summary>
  59. public override ReadOnlyArray<InputDevice> devices
  60. {
  61. get
  62. {
  63. // note:: InputRemoting class rejects remote devices when sending device information to the remote peer.
  64. // Avoid to get assert "Device being sent to remotes should be a local device, not a remote one"
  65. return new ReadOnlyArray<InputDevice>();
  66. }
  67. }
  68. /// <summary>
  69. ///
  70. /// </summary>
  71. public override IEnumerable<string> layouts
  72. {
  73. get
  74. {
  75. return Enumerable.Empty<string>();
  76. }
  77. }
  78. /// <summary>
  79. ///
  80. /// </summary>
  81. public ReadOnlyArray<InputDevice> remoteDevices
  82. {
  83. get
  84. {
  85. return new ReadOnlyArray<InputDevice>(_remoteDevices.ToArray());
  86. }
  87. }
  88. public ReadOnlyArray<string> remoteLayouts
  89. {
  90. get
  91. {
  92. return new ReadOnlyArray<string>(_remoteLayouts.Keys.ToArray());
  93. }
  94. }
  95. /// <summary>
  96. ///
  97. /// </summary>
  98. public void RemoveAllRemoteDevices()
  99. {
  100. while (_remoteDevices.Count > 0)
  101. {
  102. RemoveDevice(_remoteDevices[0]);
  103. }
  104. }
  105. public void RemoveAllRemoteLayouts()
  106. {
  107. while (_remoteLayouts.Count > 0)
  108. {
  109. RemoveLayout(_remoteLayouts.First().Key);
  110. }
  111. }
  112. public override InputDevice AddDevice(string layout, string name = null, string variants = null)
  113. {
  114. if (InputSystem.ListLayouts().Count(_ => _ == layout) == 0)
  115. {
  116. if (!_remoteLayouts.TryGetValue(layout, out string json))
  117. throw new InvalidOperationException();
  118. base.RegisterControlLayout(json, layout);
  119. _registeredRemoteLayout.Add(layout);
  120. }
  121. var device = base.AddDevice(layout, name, variants);
  122. _remoteDevices.Add(device);
  123. onDeviceChange?.Invoke(device, InputDeviceChange.Added);
  124. return device;
  125. }
  126. public override void RemoveDevice(InputDevice device)
  127. {
  128. base.RemoveDevice(device);
  129. _remoteDevices.Remove(device);
  130. onDeviceChange?.Invoke(device, InputDeviceChange.Removed);
  131. }
  132. public override void RegisterControlLayout(string json, string name = null, bool isOverride = false)
  133. {
  134. // todo(kazuki):: not call base class
  135. // base.RegisterControlLayout(json, name, isOverride);
  136. _remoteLayouts.Add(name, json);
  137. onLayoutChange?.Invoke(name, InputControlLayoutChange.Added);
  138. }
  139. public override void RemoveLayout(string name)
  140. {
  141. if(_registeredRemoteLayout.Contains(name))
  142. {
  143. base.RemoveLayout(name);
  144. _registeredRemoteLayout.Remove(name);
  145. }
  146. _remoteLayouts.Remove(name);
  147. onLayoutChange?.Invoke(name, InputControlLayoutChange.Removed);
  148. }
  149. public override void QueueEvent(InputEventPtr ptr)
  150. {
  151. InputDevice device = InputSystem.GetDeviceById(ptr.deviceId);
  152. // mapping sender coordinate system to receiver one.
  153. if (EnableInputPositionCorrection && device is Pointer && ptr.IsA<StateEvent>())
  154. {
  155. _corrector.Invoke(ptr, device);
  156. }
  157. else
  158. {
  159. base.QueueEvent(ptr);
  160. }
  161. }
  162. /// <summary>
  163. ///
  164. /// </summary>
  165. /// <param name="size">Texture Size.</param>
  166. /// <param name="region">Region of the texture in world coordinate system.</param>
  167. public void CalculateInputRegion(Rect inputRegion, Rect outputRegion)
  168. {
  169. _corrector.inputRegion = inputRegion;
  170. _corrector.outputRegion = outputRegion;
  171. }
  172. }
  173. }
  174. // #endif