VideoRender.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  2. using System;
  3. namespace Agora.Rtc
  4. {
  5. using IrisVideoFrameBufferHandle = IntPtr;
  6. internal abstract class IVideoStreamManager : IDisposable
  7. {
  8. internal abstract int EnableVideoFrameBuffer(VIDEO_SOURCE_TYPE sourceType, uint uid, string key = "");
  9. internal abstract void DisableVideoFrameBuffer(VIDEO_SOURCE_TYPE sourceType, uint uid = 0, string key = "");
  10. internal abstract IRIS_VIDEO_PROCESS_ERR GetVideoFrame(ref IrisVideoFrame video_frame,
  11. ref bool is_new_frame, VIDEO_SOURCE_TYPE sourceType, uint uid, string key = "");
  12. public abstract void Dispose();
  13. }
  14. internal class VideoStreamManager : IVideoStreamManager
  15. {
  16. private RtcEngineImpl _agoraRtcEngine;
  17. private IrisCVideoFrameBufferNative _videoFrameBuffer;
  18. private IrisVideoFrameBufferHandle _irisVideoFrameBufferHandle = IntPtr.Zero;
  19. private IrisVideoFrameBufferConfig _videoFrameBufferConfig;
  20. private bool _disposed;
  21. public VideoStreamManager(RtcEngineImpl agoraRtcEngine)
  22. {
  23. _agoraRtcEngine = agoraRtcEngine;
  24. _agoraRtcEngine.OnRtcEngineImpleWillDispose += RtcEngineImplWillDispose;
  25. _videoFrameBufferConfig = new IrisVideoFrameBufferConfig();
  26. }
  27. ~VideoStreamManager()
  28. {
  29. Dispose();
  30. }
  31. internal override int EnableVideoFrameBuffer(VIDEO_SOURCE_TYPE sourceType, uint uid, string channel_id = "")
  32. {
  33. if (_agoraRtcEngine == null)
  34. {
  35. AgoraLog.LogError(string.Format("EnableVideoFrameCache ret: ${0}", ERROR_CODE_TYPE.ERR_NOT_INITIALIZED));
  36. return (int)ERROR_CODE_TYPE.ERR_NOT_INITIALIZED;
  37. }
  38. IntPtr irisEngine = (_agoraRtcEngine as RtcEngineImpl).GetNativeHandler();
  39. IntPtr videoFrameBufferManagerPtr = (_agoraRtcEngine as RtcEngineImpl).GetVideoFrameBufferManager();
  40. if (irisEngine != IntPtr.Zero && _irisVideoFrameBufferHandle == IntPtr.Zero)
  41. {
  42. _videoFrameBuffer = new IrisCVideoFrameBufferNative
  43. {
  44. type = (int)VIDEO_OBSERVER_FRAME_TYPE.FRAME_TYPE_RGBA,
  45. OnVideoFrameReceived = IntPtr.Zero,
  46. bytes_per_row_alignment = 2
  47. };
  48. _videoFrameBufferConfig.type = (int)sourceType;
  49. _videoFrameBufferConfig.id = uid;
  50. _videoFrameBufferConfig.key = channel_id;
  51. _irisVideoFrameBufferHandle = AgoraRtcNative.EnableVideoFrameBufferByConfig(videoFrameBufferManagerPtr, ref _videoFrameBuffer, ref _videoFrameBufferConfig);
  52. AgoraLog.Log("EnableVideoFrameBufferByConfig");
  53. return (int)ERROR_CODE_TYPE.ERR_OK;
  54. }
  55. return (int)ERROR_CODE_TYPE.ERR_NOT_INITIALIZED;
  56. }
  57. internal override void DisableVideoFrameBuffer(VIDEO_SOURCE_TYPE sourceType, uint uid = 0, string key = "")
  58. {
  59. if (_agoraRtcEngine == null)
  60. {
  61. AgoraLog.LogError(string.Format("EnableVideoFrameCache ret: ${0}", ERROR_CODE_TYPE.ERR_NOT_INITIALIZED));
  62. return;
  63. }
  64. IntPtr irisEngine = (_agoraRtcEngine as RtcEngineImpl).GetNativeHandler();
  65. IntPtr videoFrameBufferManagerPtr = (_agoraRtcEngine as RtcEngineImpl).GetVideoFrameBufferManager();
  66. if (irisEngine != IntPtr.Zero && _irisVideoFrameBufferHandle != IntPtr.Zero)
  67. {
  68. _videoFrameBufferConfig.type = (int)sourceType;
  69. _videoFrameBufferConfig.id = uid;
  70. _videoFrameBufferConfig.key = key;
  71. AgoraRtcNative.DisableVideoFrameBufferByConfig(videoFrameBufferManagerPtr, ref _videoFrameBufferConfig, _irisVideoFrameBufferHandle);
  72. AgoraLog.Log("DisableVideoFrameBufferByConfig");
  73. _irisVideoFrameBufferHandle = IntPtr.Zero;
  74. }
  75. }
  76. internal override IRIS_VIDEO_PROCESS_ERR GetVideoFrame(ref IrisVideoFrame video_frame, ref bool is_new_frame, VIDEO_SOURCE_TYPE sourceType, uint uid, string key = "")
  77. {
  78. if (_agoraRtcEngine == null)
  79. {
  80. AgoraLog.LogError(string.Format("EnableVideoFrameCache ret: ${0}", ERROR_CODE_TYPE.ERR_NOT_INITIALIZED));
  81. return IRIS_VIDEO_PROCESS_ERR.ERR_NULL_POINTER;
  82. }
  83. IntPtr irisEngine = (_agoraRtcEngine as RtcEngineImpl).GetNativeHandler();
  84. IntPtr videoFrameBufferManagerPtr = (_agoraRtcEngine as RtcEngineImpl).GetVideoFrameBufferManager();
  85. if (irisEngine != IntPtr.Zero)
  86. {
  87. _videoFrameBufferConfig.type = (int)sourceType;
  88. _videoFrameBufferConfig.id = uid;
  89. _videoFrameBufferConfig.key = key;
  90. return AgoraRtcNative.GetVideoFrameByConfig(videoFrameBufferManagerPtr, ref video_frame, out is_new_frame, ref _videoFrameBufferConfig);
  91. }
  92. return IRIS_VIDEO_PROCESS_ERR.ERR_NULL_POINTER;
  93. }
  94. internal void RtcEngineImplWillDispose(RtcEngineImpl impl)
  95. {
  96. IntPtr irisEngine = (_agoraRtcEngine as RtcEngineImpl).GetNativeHandler();
  97. IntPtr videoFrameBufferManagerPtr = (_agoraRtcEngine as RtcEngineImpl).GetVideoFrameBufferManager();
  98. if (irisEngine != IntPtr.Zero && _irisVideoFrameBufferHandle != IntPtr.Zero)
  99. {
  100. AgoraRtcNative.DisableVideoFrameBufferByConfig(videoFrameBufferManagerPtr, ref _videoFrameBufferConfig, _irisVideoFrameBufferHandle);
  101. AgoraLog.Log("DisableVideoFrameBufferByConfig on RtcEngineImplWillDispose");
  102. _irisVideoFrameBufferHandle = IntPtr.Zero;
  103. }
  104. }
  105. internal void Dispose(bool disposing)
  106. {
  107. if (_disposed) return;
  108. if (disposing)
  109. {
  110. _agoraRtcEngine.OnRtcEngineImpleWillDispose -= RtcEngineImplWillDispose;
  111. _agoraRtcEngine = null;
  112. _disposed = true;
  113. }
  114. }
  115. public override void Dispose()
  116. {
  117. Dispose(true);
  118. GC.SuppressFinalize(this);
  119. }
  120. }
  121. }
  122. #endif