ReceiverSample.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace Unity.RenderStreaming.Samples
  5. {
  6. static class InputSenderExtension
  7. {
  8. public static (Rect, Vector2Int) GetRegionAndSize(this RawImage image)
  9. {
  10. // correct pointer position
  11. Vector3[] corners = new Vector3[4];
  12. image.rectTransform.GetWorldCorners(corners);
  13. Camera camera = image.canvas.worldCamera;
  14. var corner0 = RectTransformUtility.WorldToScreenPoint(camera, corners[0]);
  15. var corner2 = RectTransformUtility.WorldToScreenPoint(camera, corners[2]);
  16. var region = new Rect(
  17. corner0.x,
  18. corner0.y,
  19. corner2.x - corner0.x,
  20. corner2.y - corner0.y
  21. );
  22. var size = new Vector2Int(image.texture.width, image.texture.height);
  23. return (region, size);
  24. }
  25. }
  26. class ReceiverSample : MonoBehaviour
  27. {
  28. #pragma warning disable 0649
  29. [SerializeField] private SignalingManager renderStreaming;
  30. [SerializeField] private Button startButton;
  31. [SerializeField] private Button stopButton;
  32. [SerializeField] private InputField connectionIdInput;
  33. [SerializeField] private RawImage remoteVideoImage;
  34. [SerializeField] private AudioSource remoteAudioSource;
  35. [SerializeField] private VideoStreamReceiver receiveVideoViewer;
  36. [SerializeField] private AudioStreamReceiver receiveAudioViewer;
  37. [SerializeField] private SingleConnection connection;
  38. [SerializeField] private Text resolution;
  39. #pragma warning restore 0649
  40. private string connectionId;
  41. private InputSender inputSender;
  42. private RenderStreamingSettings settings;
  43. private Vector2 lastSize;
  44. void Awake()
  45. {
  46. startButton.onClick.AddListener(OnStart);
  47. stopButton.onClick.AddListener(OnStop);
  48. if(connectionIdInput != null)
  49. connectionIdInput.onValueChanged.AddListener(input => connectionId = input);
  50. receiveVideoViewer.OnUpdateReceiveTexture += OnUpdateReceiveTexture;
  51. receiveAudioViewer.OnUpdateReceiveAudioSource += source =>
  52. {
  53. source.loop = true;
  54. source.Play();
  55. };
  56. inputSender = GetComponent<InputSender>();
  57. inputSender.OnStartedChannel += OnStartedChannel;
  58. settings = SampleManager.Instance.Settings;
  59. }
  60. void Start()
  61. {
  62. if (renderStreaming.runOnAwake)
  63. return;
  64. if (settings != null)
  65. renderStreaming.useDefaultSettings = settings.UseDefaultSettings;
  66. if (settings?.SignalingSettings != null)
  67. renderStreaming.SetSignalingSettings(settings.SignalingSettings);
  68. renderStreaming.Run();
  69. }
  70. private void Update()
  71. {
  72. // Call SetInputChange if window size is changed.
  73. var size = remoteVideoImage.rectTransform.sizeDelta;
  74. if (lastSize == size)
  75. return;
  76. lastSize = size;
  77. CalculateInputRegion();
  78. }
  79. void OnUpdateReceiveTexture(Texture texture)
  80. {
  81. remoteVideoImage.texture = texture;
  82. CalculateInputRegion();
  83. }
  84. void OnStartedChannel(string connectionId)
  85. {
  86. CalculateInputRegion();
  87. }
  88. private void OnRectTransformDimensionsChange()
  89. {
  90. CalculateInputRegion();
  91. }
  92. void CalculateInputRegion()
  93. {
  94. if (inputSender == null || !inputSender.IsConnected || remoteVideoImage.texture == null)
  95. return;
  96. var (region, size) = remoteVideoImage.GetRegionAndSize();
  97. resolution.text = $"{(int)region.width} x {(int)region.height}";
  98. inputSender.CalculateInputResion(region, size);
  99. inputSender.EnableInputPositionCorrection(true);
  100. }
  101. private void OnStart()
  102. {
  103. if (string.IsNullOrEmpty(connectionId))
  104. {
  105. connectionId = System.Guid.NewGuid().ToString("N");
  106. connectionIdInput.text = connectionId;
  107. }
  108. connectionIdInput.interactable = false;
  109. if(settings != null)
  110. receiveVideoViewer.SetCodec(settings.ReceiverVideoCodec);
  111. receiveAudioViewer.targetAudioSource = remoteAudioSource;
  112. connection.CreateConnection(connectionId);
  113. startButton.gameObject.SetActive(false);
  114. stopButton.gameObject.SetActive(true);
  115. }
  116. private void OnStop()
  117. {
  118. connection.DeleteConnection(connectionId);
  119. connectionId = String.Empty;
  120. connectionIdInput.text = String.Empty;
  121. connectionIdInput.interactable = true;
  122. startButton.gameObject.SetActive(true);
  123. stopButton.gameObject.SetActive(false);
  124. }
  125. }
  126. }