BidirectionalSample.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Linq;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace Unity.RenderStreaming.Samples
  5. {
  6. class BidirectionalSample : MonoBehaviour
  7. {
  8. #pragma warning disable 0649
  9. [SerializeField] private SignalingManager renderStreaming;
  10. [SerializeField] private Dropdown webcamSelectDropdown;
  11. [SerializeField] private Dropdown microphoneSelectDropdown;
  12. [SerializeField] private Button startButton;
  13. [SerializeField] private Button setUpButton;
  14. [SerializeField] private Button hangUpButton;
  15. [SerializeField] private InputField connectionIdInput;
  16. [SerializeField] private RawImage localVideoImage;
  17. [SerializeField] private RawImage remoteVideoImage;
  18. [SerializeField] private AudioSource receiveAudioSource;
  19. [SerializeField] private VideoStreamSender webCamStreamer;
  20. [SerializeField] private VideoStreamReceiver receiveVideoViewer;
  21. [SerializeField] private AudioStreamSender microphoneStreamer;
  22. [SerializeField] private AudioStreamReceiver receiveAudioViewer;
  23. [SerializeField] private SingleConnection singleConnection;
  24. #pragma warning restore 0649
  25. private string connectionId;
  26. private RenderStreamingSettings settings;
  27. void Awake()
  28. {
  29. startButton.interactable = true;
  30. webcamSelectDropdown.interactable = true;
  31. setUpButton.interactable = false;
  32. hangUpButton.interactable = false;
  33. connectionIdInput.interactable = true;
  34. startButton.onClick.AddListener(() =>
  35. {
  36. webCamStreamer.enabled = true;
  37. startButton.interactable = false;
  38. webcamSelectDropdown.interactable = false;
  39. microphoneStreamer.enabled = true;
  40. microphoneSelectDropdown.interactable = false;
  41. setUpButton.interactable = true;
  42. });
  43. setUpButton.onClick.AddListener(SetUp);
  44. hangUpButton.onClick.AddListener(HangUp);
  45. connectionIdInput.onValueChanged.AddListener(input => connectionId = input);
  46. connectionIdInput.text = $"{Random.Range(0, 99999):D5}";
  47. webcamSelectDropdown.onValueChanged.AddListener(index => webCamStreamer.sourceDeviceIndex = index);
  48. webcamSelectDropdown.options = WebCamTexture.devices.Select(x => x.name).Select(x => new Dropdown.OptionData(x)).ToList();
  49. webCamStreamer.OnStartedStream += id => receiveVideoViewer.enabled = true;
  50. webCamStreamer.OnStartedStream += _ => localVideoImage.texture = webCamStreamer.sourceWebCamTexture;
  51. settings = SampleManager.Instance.Settings;
  52. if (settings != null)
  53. {
  54. webCamStreamer.width = (uint)settings.StreamSize.x;
  55. webCamStreamer.height = (uint)settings.StreamSize.y;
  56. }
  57. receiveVideoViewer.OnUpdateReceiveTexture += texture => remoteVideoImage.texture = texture;
  58. microphoneSelectDropdown.onValueChanged.AddListener(index => microphoneStreamer.sourceDeviceIndex = index);
  59. microphoneSelectDropdown.options =
  60. Microphone.devices.Select(x => new Dropdown.OptionData(x)).ToList();
  61. receiveAudioViewer.targetAudioSource = receiveAudioSource;
  62. receiveAudioViewer.OnUpdateReceiveAudioSource += source =>
  63. {
  64. source.loop = true;
  65. source.Play();
  66. };
  67. }
  68. void Start()
  69. {
  70. if (renderStreaming.runOnAwake)
  71. return;
  72. if (settings != null)
  73. renderStreaming.useDefaultSettings = settings.UseDefaultSettings;
  74. if (settings?.SignalingSettings != null)
  75. renderStreaming.SetSignalingSettings(settings.SignalingSettings);
  76. renderStreaming.Run();
  77. }
  78. private void SetUp()
  79. {
  80. setUpButton.interactable = false;
  81. hangUpButton.interactable = true;
  82. connectionIdInput.interactable = false;
  83. if(settings != null)
  84. {
  85. receiveVideoViewer.SetCodec(settings.ReceiverVideoCodec);
  86. webCamStreamer.SetCodec(settings.SenderVideoCodec);
  87. }
  88. singleConnection.CreateConnection(connectionId);
  89. }
  90. private void HangUp()
  91. {
  92. singleConnection.DeleteConnection(connectionId);
  93. remoteVideoImage.texture = null;
  94. setUpButton.interactable = true;
  95. hangUpButton.interactable = false;
  96. connectionIdInput.interactable = true;
  97. connectionIdInput.text = $"{Random.Range(0, 99999):D5}";
  98. localVideoImage.texture = null;
  99. }
  100. }
  101. }