VideoFrameObserverNative.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  5. using AOT;
  6. #endif
  7. namespace Agora.Rtc
  8. {
  9. internal static class VideoFrameObserverNative
  10. {
  11. internal static OBSERVER_MODE mode = OBSERVER_MODE.INTPTR;
  12. internal static IVideoFrameObserver VideoFrameObserver;
  13. private static class LocalVideoFrames
  14. {
  15. internal static readonly VideoFrame CaptureVideoFrame = new VideoFrame();
  16. internal static readonly VideoFrame PreEncodeVideoFrame = new VideoFrame();
  17. internal static readonly VideoFrame RenderVideoFrame = new VideoFrame();
  18. internal static readonly Dictionary<string, Dictionary<uint, VideoFrame>> RenderVideoFrameEx =
  19. new Dictionary<string, Dictionary<uint, VideoFrame>>();
  20. }
  21. private static VideoFrame ProcessVideoFrameReceived(IntPtr videoFramePtr, string channelId, uint uid)
  22. {
  23. var videoFrame = (IrisVideoFrame)(Marshal.PtrToStructure(videoFramePtr, typeof(IrisVideoFrame)) ??
  24. new IrisVideoFrame());
  25. var localVideoFrame = new VideoFrame();
  26. var ifConverted = VideoFrameObserver.GetVideoFormatPreference() != VIDEO_OBSERVER_FRAME_TYPE.FRAME_TYPE_YUV420;
  27. var videoFrameConverted = new IrisVideoFrame();
  28. videoFrameConverted.type = VideoFrameObserver.GetVideoFormatPreference();
  29. if (ifConverted)
  30. {
  31. AgoraRtcNative.ConvertVideoFrame(ref videoFrameConverted, ref videoFrame);
  32. }
  33. if (channelId == "")
  34. {
  35. switch(uid)
  36. {
  37. case 0:
  38. localVideoFrame = LocalVideoFrames.CaptureVideoFrame;
  39. break;
  40. case 1:
  41. localVideoFrame = LocalVideoFrames.PreEncodeVideoFrame;
  42. break;
  43. case 2:
  44. localVideoFrame = LocalVideoFrames.RenderVideoFrame;
  45. break;
  46. }
  47. }
  48. else
  49. {
  50. if (!LocalVideoFrames.RenderVideoFrameEx.ContainsKey(channelId))
  51. {
  52. LocalVideoFrames.RenderVideoFrameEx[channelId] = new Dictionary<uint, VideoFrame>();
  53. LocalVideoFrames.RenderVideoFrameEx[channelId][uid] = new VideoFrame();
  54. }
  55. else if (!LocalVideoFrames.RenderVideoFrameEx[channelId].ContainsKey(uid))
  56. {
  57. LocalVideoFrames.RenderVideoFrameEx[channelId][uid] = new VideoFrame();
  58. }
  59. localVideoFrame = LocalVideoFrames.RenderVideoFrameEx[channelId][uid];
  60. }
  61. if (mode == OBSERVER_MODE.RAW_DATA)
  62. {
  63. if (localVideoFrame.height != videoFrameConverted.height ||
  64. localVideoFrame.yStride != videoFrameConverted.y_stride ||
  65. localVideoFrame.uStride != videoFrameConverted.u_stride ||
  66. localVideoFrame.vStride != videoFrameConverted.v_stride)
  67. {
  68. localVideoFrame.yBuffer = new byte[videoFrameConverted.y_buffer_length];
  69. localVideoFrame.uBuffer = new byte[videoFrameConverted.u_buffer_length];
  70. localVideoFrame.vBuffer = new byte[videoFrameConverted.v_buffer_length];
  71. }
  72. if (videoFrameConverted.y_buffer != IntPtr.Zero)
  73. Marshal.Copy(videoFrameConverted.y_buffer, localVideoFrame.yBuffer, 0,
  74. (int)videoFrameConverted.y_buffer_length);
  75. if (videoFrameConverted.u_buffer != IntPtr.Zero)
  76. Marshal.Copy(videoFrameConverted.u_buffer, localVideoFrame.uBuffer, 0,
  77. (int)videoFrameConverted.u_buffer_length);
  78. if (videoFrameConverted.v_buffer != IntPtr.Zero)
  79. Marshal.Copy(videoFrameConverted.v_buffer, localVideoFrame.vBuffer, 0,
  80. (int)videoFrameConverted.v_buffer_length);
  81. }
  82. localVideoFrame.width = videoFrameConverted.width;
  83. localVideoFrame.height = videoFrameConverted.height;
  84. localVideoFrame.yBufferPtr = videoFrameConverted.y_buffer;
  85. localVideoFrame.yStride = videoFrameConverted.y_stride;
  86. localVideoFrame.uBufferPtr = videoFrameConverted.u_buffer;
  87. localVideoFrame.uStride = videoFrameConverted.u_stride;
  88. localVideoFrame.vBufferPtr = videoFrameConverted.v_buffer;
  89. localVideoFrame.vStride = videoFrameConverted.v_stride;
  90. localVideoFrame.rotation = videoFrameConverted.rotation;
  91. localVideoFrame.renderTimeMs = videoFrameConverted.render_time_ms;
  92. localVideoFrame.avsync_type = videoFrameConverted.av_sync_type;
  93. localVideoFrame.metadata_size = videoFrameConverted.metadata_size;
  94. localVideoFrame.metadata_buffer = videoFrameConverted.metadata_buffer;
  95. localVideoFrame.sharedContext = videoFrameConverted.sharedContext;
  96. localVideoFrame.matrix = videoFrameConverted.matrix;
  97. localVideoFrame.textureId = videoFrameConverted.textureId;
  98. if (ifConverted) AgoraRtcNative.ClearVideoFrame(ref videoFrameConverted);
  99. return localVideoFrame;
  100. }
  101. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  102. [MonoPInvokeCallback(typeof(Func_VideoCaptureLocal_Native))]
  103. #endif
  104. internal static bool OnCaptureVideoFrame(IntPtr videoFramePtr, IntPtr videoFrameConfig)
  105. {
  106. var videoFrameBufferConfig = (IrisVideoFrameBufferConfig) (Marshal.PtrToStructure(videoFrameConfig, typeof(IrisVideoFrameBufferConfig)) ??
  107. new IrisVideoFrameBufferConfig());
  108. var config = new VideoFrameBufferConfig();
  109. config.type = (VIDEO_SOURCE_TYPE) videoFrameBufferConfig.type;
  110. config.id = videoFrameBufferConfig.id;
  111. config.key = videoFrameBufferConfig.key;
  112. try
  113. {
  114. return VideoFrameObserver == null ||
  115. VideoFrameObserver.OnCaptureVideoFrame(ProcessVideoFrameReceived(videoFramePtr, "", 0), config);
  116. }
  117. catch(Exception e)
  118. {
  119. AgoraLog.LogError("[Exception] IVideoFrameObserver.OnCaptureVideoFrame: " + e);
  120. return true;
  121. }
  122. }
  123. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  124. [MonoPInvokeCallback(typeof(Func_VideoCaptureLocal_Native))]
  125. #endif
  126. internal static bool OnPreEncodeVideoFrame(IntPtr videoFramePtr, IntPtr videoFrameConfig)
  127. {
  128. var videoFrameBufferConfig = (IrisVideoFrameBufferConfig) (Marshal.PtrToStructure(videoFrameConfig, typeof(IrisVideoFrameBufferConfig)) ??
  129. new IrisVideoFrameBufferConfig());
  130. var config = new VideoFrameBufferConfig();
  131. config.type = (VIDEO_SOURCE_TYPE) videoFrameBufferConfig.type;
  132. config.id = videoFrameBufferConfig.id;
  133. config.key = videoFrameBufferConfig.key;
  134. try
  135. {
  136. return VideoFrameObserver == null ||
  137. VideoFrameObserver.OnPreEncodeVideoFrame(ProcessVideoFrameReceived(videoFramePtr, "", 1), config);
  138. }
  139. catch(Exception e)
  140. {
  141. AgoraLog.LogError("[Exception] IVideoFrameObserver.OnPreEncodeVideoFrame: " + e);
  142. return true;
  143. }
  144. }
  145. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  146. [MonoPInvokeCallback(typeof(Func_VideoFrameRemote_Native))]
  147. #endif
  148. internal static bool OnRenderVideoFrame(string channel_id, uint uid, IntPtr videoFramePtr)
  149. {
  150. try
  151. {
  152. return VideoFrameObserver == null ||
  153. VideoFrameObserver.OnRenderVideoFrame(channel_id, uid, ProcessVideoFrameReceived(videoFramePtr, "", 2));
  154. }
  155. catch(Exception e)
  156. {
  157. AgoraLog.LogError("[Exception] IVideoFrameObserver.OnRenderVideoFrame: " + e);
  158. return true;
  159. }
  160. }
  161. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  162. [MonoPInvokeCallback(typeof(Func_Uint32_t_Native))]
  163. #endif
  164. internal static uint GetObservedFramePosition()
  165. {
  166. if (VideoFrameObserver == null)
  167. return (uint) (VIDEO_OBSERVER_POSITION.POSITION_POST_CAPTURER |
  168. VIDEO_OBSERVER_POSITION.POSITION_PRE_RENDERER);
  169. try
  170. {
  171. return (uint) VideoFrameObserver.GetObservedFramePosition();
  172. }
  173. catch(Exception e)
  174. {
  175. AgoraLog.LogError("[Exception] IVideoFrameObserver.GetObservedFramePosition: " + e);
  176. return 0;
  177. }
  178. }
  179. }
  180. }