VideoStreamReceiver.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using Unity.WebRTC;
  7. namespace Unity.RenderStreaming
  8. {
  9. /// <summary>
  10. ///
  11. /// </summary>
  12. public enum VideoRenderMode
  13. {
  14. /// <summary>
  15. ///
  16. /// </summary>
  17. RenderTexture,
  18. /// <summary>
  19. ///
  20. /// </summary>
  21. APIOnly,
  22. }
  23. /// <summary>
  24. ///
  25. /// </summary>
  26. [AddComponentMenu("Render Streaming/Video Stream Receiver")]
  27. public class VideoStreamReceiver : StreamReceiverBase
  28. {
  29. /// <summary>
  30. ///
  31. /// </summary>
  32. /// <param name="receiveTexture"></param>
  33. public delegate void OnUpdateReceiveTextureHandler(Texture receiveTexture);
  34. /// <summary>
  35. ///
  36. /// </summary>
  37. public OnUpdateReceiveTextureHandler OnUpdateReceiveTexture;
  38. [SerializeField, Codec]
  39. private VideoCodecInfo m_Codec;
  40. [SerializeField]
  41. private VideoRenderMode m_RenderMode;
  42. [SerializeField]
  43. private RenderTexture m_TargetTexture;
  44. /// <summary>
  45. ///
  46. /// </summary>
  47. public VideoCodecInfo codec
  48. {
  49. get { return m_Codec; }
  50. }
  51. /// <summary>
  52. ///
  53. /// </summary>
  54. public int width => m_texture.width;
  55. /// <summary>
  56. ///
  57. /// </summary>
  58. public int height => m_texture.height;
  59. /// <summary>
  60. ///
  61. /// </summary>
  62. public Texture texture => m_texture;
  63. /// <summary>
  64. ///
  65. /// </summary>
  66. public RenderTexture targetTexture
  67. {
  68. get { return m_TargetTexture; }
  69. set { m_TargetTexture = value; }
  70. }
  71. private Texture m_texture;
  72. private Coroutine m_coroutine;
  73. /// <summary>
  74. ///
  75. /// </summary>
  76. /// <returns></returns>
  77. public static IEnumerable<VideoCodecInfo> GetAvailableCodecs()
  78. {
  79. string[] excludeCodecMimeType = { "video/red", "video/ulpfec", "video/rtx", "video/flexfec-03" };
  80. var capabilities = RTCRtpReceiver.GetCapabilities(TrackKind.Video);
  81. return capabilities.codecs.Where(codec => !excludeCodecMimeType.Contains(codec.mimeType)).Select(codec => VideoCodecInfo.Create(codec));
  82. }
  83. /// <summary>
  84. ///
  85. /// </summary>
  86. /// <param name="mimeType"></param>
  87. public void SetCodec(VideoCodecInfo codec)
  88. {
  89. m_Codec = codec;
  90. if (Transceiver == null)
  91. return;
  92. if (!string.IsNullOrEmpty(Transceiver.Mid))
  93. throw new InvalidOperationException("Transceiver is streaming. This operation is invalid during the track is in use.");
  94. if (Transceiver.Sender.Track.ReadyState == TrackState.Ended)
  95. throw new InvalidOperationException("Track has already been ended.");
  96. var codecs = new VideoCodecInfo[] { m_Codec };
  97. RTCErrorType error = Transceiver.SetCodecPreferences(SelectCodecCapabilities(codecs).ToArray());
  98. if (error != RTCErrorType.None)
  99. throw new InvalidOperationException($"Set codec is failed. errorCode={error}");
  100. }
  101. internal IEnumerable<RTCRtpCodecCapability> SelectCodecCapabilities(IEnumerable<VideoCodecInfo> codecs)
  102. {
  103. return RTCRtpReceiver.GetCapabilities(TrackKind.Video).SelectCodecCapabilities(codecs);
  104. }
  105. private protected virtual void Awake()
  106. {
  107. OnStartedStream += StartedStream;
  108. OnStoppedStream += StoppedStream;
  109. }
  110. private void StartedStream(string connectionId)
  111. {
  112. if (Track is VideoStreamTrack videoTrack)
  113. {
  114. videoTrack.OnVideoReceived += texture =>
  115. {
  116. m_texture = texture;
  117. OnUpdateReceiveTexture?.Invoke(m_texture);
  118. };
  119. }
  120. m_coroutine = StartCoroutine(Render());
  121. }
  122. private void StoppedStream(string connectionId)
  123. {
  124. m_texture = null;
  125. OnUpdateReceiveTexture?.Invoke(m_texture);
  126. if(m_coroutine != null)
  127. {
  128. StopCoroutine(m_coroutine);
  129. m_coroutine = null;
  130. }
  131. }
  132. private IEnumerator Render()
  133. {
  134. while (true)
  135. {
  136. yield return new WaitForEndOfFrame();
  137. if (m_RenderMode != VideoRenderMode.RenderTexture ||
  138. m_texture == null ||
  139. m_TargetTexture == null)
  140. continue;
  141. Graphics.Blit(m_texture, m_TargetTexture);
  142. }
  143. }
  144. }
  145. }