TextureManager.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  2. using System;
  3. using System.Runtime.InteropServices;
  4. using UnityEngine;
  5. namespace Agora.Rtc
  6. {
  7. internal class TextureManager : MonoBehaviour
  8. {
  9. // texture identity
  10. private int _videoPixelWidth = 0;
  11. private int _videoPixelHeight = 0;
  12. private uint _uid = 0;
  13. private string _channelId = "";
  14. private VIDEO_SOURCE_TYPE _sourceType = VIDEO_SOURCE_TYPE.VIDEO_SOURCE_CAMERA_PRIMARY;
  15. private bool _needResize = false;
  16. private bool _needUpdateInfo = true;
  17. private bool isFresh = false;
  18. private IVideoStreamManager _videoStreamManager;
  19. private IrisVideoFrame _cachedVideoFrame = new IrisVideoFrame();
  20. // reference count
  21. private int _refCount = 0;
  22. private bool _canAttach = false;
  23. //texture width and height
  24. public int Width = 0;
  25. public int Height = 0;
  26. private Texture2D _texture;
  27. public Texture2D Texture
  28. {
  29. get
  30. {
  31. _refCount++;
  32. AgoraLog.Log("TextureManager refCount Add, Now is: " + _refCount);
  33. return _texture;
  34. }
  35. }
  36. private void Awake()
  37. {
  38. InitTexture();
  39. InitIrisVideoFrame();
  40. }
  41. private void Update()
  42. {
  43. if (_needUpdateInfo) return;
  44. ReFreshTexture();
  45. }
  46. private void OnDestroy()
  47. {
  48. AgoraLog.Log(string.Format("VideoSurface channel: ${0}, user:{1} destroy", _channelId, _uid));
  49. if (_videoStreamManager != null)
  50. {
  51. _videoStreamManager.DisableVideoFrameBuffer(_sourceType, _uid, _channelId);
  52. _videoStreamManager.Dispose();
  53. _videoStreamManager = null;
  54. }
  55. FreeMemory();
  56. DestroyTexture();
  57. }
  58. private void InitTexture()
  59. {
  60. try
  61. {
  62. _texture = new Texture2D(_videoPixelWidth, _videoPixelHeight, TextureFormat.RGBA32, false);
  63. _texture.Apply();
  64. }
  65. catch (Exception e)
  66. {
  67. AgoraLog.LogError("Exception e = " + e);
  68. }
  69. }
  70. private void InitIrisVideoFrame()
  71. {
  72. _cachedVideoFrame = new IrisVideoFrame
  73. {
  74. type = VIDEO_OBSERVER_FRAME_TYPE.FRAME_TYPE_RGBA,
  75. y_stride = _videoPixelWidth * 4,
  76. height = _videoPixelHeight,
  77. width = _videoPixelWidth,
  78. y_buffer = Marshal.AllocHGlobal(_videoPixelWidth * _videoPixelHeight * 4)
  79. };
  80. }
  81. internal int GetRefCount()
  82. {
  83. return _refCount;
  84. }
  85. internal bool CanTextureAttach()
  86. {
  87. return _canAttach;
  88. }
  89. internal void EnableVideoFrameWithIdentity()
  90. {
  91. var engine = RtcEngineImpl.Get();
  92. if (engine != null)
  93. {
  94. if (_videoStreamManager == null)
  95. {
  96. _videoStreamManager = ((RtcEngineImpl)engine).GetVideoStreamManager();
  97. }
  98. if (_videoStreamManager != null)
  99. {
  100. _videoStreamManager.EnableVideoFrameBuffer(_sourceType, _uid, _channelId);
  101. _needUpdateInfo = false;
  102. }
  103. }
  104. }
  105. internal void ReFreshTexture()
  106. {
  107. var ret = _videoStreamManager.GetVideoFrame(ref _cachedVideoFrame, ref isFresh, _sourceType, _uid, _channelId);
  108. this.Width = _cachedVideoFrame.width;
  109. this.Height = _cachedVideoFrame.height;
  110. if (ret == IRIS_VIDEO_PROCESS_ERR.ERR_BUFFER_EMPTY || ret == IRIS_VIDEO_PROCESS_ERR.ERR_NULL_POINTER)
  111. {
  112. _canAttach = false;
  113. //AgoraLog.LogWarning(string.Format("no video frame for user channel: {0} uid: {1}", _channelId, _uid));
  114. return;
  115. }
  116. else if (ret == IRIS_VIDEO_PROCESS_ERR.ERR_SIZE_NOT_MATCHING)
  117. {
  118. _needResize = true;
  119. _videoPixelWidth = _cachedVideoFrame.width;
  120. _videoPixelHeight = _cachedVideoFrame.height;
  121. FreeMemory();
  122. _cachedVideoFrame.type = VIDEO_OBSERVER_FRAME_TYPE.FRAME_TYPE_RGBA;
  123. _cachedVideoFrame.y_stride = _videoPixelWidth * 4;
  124. _cachedVideoFrame.height = _videoPixelHeight;
  125. _cachedVideoFrame.width = _videoPixelWidth;
  126. _cachedVideoFrame.y_buffer = Marshal.AllocHGlobal(_videoPixelWidth * _videoPixelHeight * 4);
  127. }
  128. else
  129. {
  130. _canAttach = true;
  131. }
  132. if (isFresh)
  133. {
  134. try
  135. {
  136. if (_needResize)
  137. {
  138. _texture.Resize(_videoPixelWidth, _videoPixelHeight);
  139. _texture.Apply();
  140. _needResize = false;
  141. }
  142. else
  143. {
  144. _texture.LoadRawTextureData(_cachedVideoFrame.y_buffer,
  145. (int)_videoPixelWidth * (int)_videoPixelHeight * 4);
  146. _texture.Apply();
  147. }
  148. }
  149. catch (Exception e)
  150. {
  151. AgoraLog.Log("Exception e = " + e);
  152. }
  153. }
  154. }
  155. internal void SetVideoStreamIdentity(uint uid = 0, string channelId = "",
  156. VIDEO_SOURCE_TYPE source_type = VIDEO_SOURCE_TYPE.VIDEO_SOURCE_CAMERA_PRIMARY)
  157. {
  158. _uid = uid;
  159. _channelId = channelId;
  160. _sourceType = source_type;
  161. }
  162. internal void Detach()
  163. {
  164. if (_refCount > 0)
  165. {
  166. _refCount--;
  167. AgoraLog.Log("TextureManager refCount Minus, Now is: " + _refCount);
  168. }
  169. return;
  170. }
  171. private void DestroyTexture()
  172. {
  173. if (_texture != null)
  174. {
  175. GameObject.Destroy(_texture);
  176. _texture = null;
  177. }
  178. }
  179. private void FreeMemory()
  180. {
  181. if (_cachedVideoFrame.y_buffer != IntPtr.Zero)
  182. {
  183. Marshal.FreeHGlobal(_cachedVideoFrame.y_buffer);
  184. }
  185. }
  186. }
  187. }
  188. #endif