MediaPlayerSourceObserverNative.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 MediaPlayerSourceObserverNative
  10. {
  11. internal static Dictionary<int, IMediaPlayerSourceObserver> RtcMediaPlayerEventHandlerDic = new Dictionary<int, IMediaPlayerSourceObserver>();
  12. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  13. internal static AgoraCallbackObject CallbackObject = null;
  14. #endif
  15. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  16. [MonoPInvokeCallback(typeof(Func_Event_Native))]
  17. #endif
  18. internal static void OnEvent(string @event, string data, IntPtr buffer, IntPtr length, uint buffer_count)
  19. {
  20. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  21. if (CallbackObject == null || CallbackObject._CallbackQueue == null) return;
  22. #endif
  23. var jsonData = AgoraJson.ToObject(data);
  24. int playerId = (int)AgoraJson.GetData<int>(jsonData, "playerId");
  25. switch (@event)
  26. {
  27. case "MediaPlayerSourceObserver_onPlayerSourceStateChanged":
  28. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  29. CallbackObject._CallbackQueue.EnQueue(() =>
  30. {
  31. #endif
  32. if (!RtcMediaPlayerEventHandlerDic.ContainsKey(playerId)) return;
  33. RtcMediaPlayerEventHandlerDic[playerId].OnPlayerSourceStateChanged(
  34. (MEDIA_PLAYER_STATE)AgoraJson.GetData<int>(jsonData, "state"),
  35. (MEDIA_PLAYER_ERROR)AgoraJson.GetData<int>(jsonData, "ec")
  36. );
  37. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  38. });
  39. #endif
  40. break;
  41. case "MediaPlayerSourceObserver_onPositionChanged":
  42. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  43. CallbackObject._CallbackQueue.EnQueue(() =>
  44. {
  45. #endif
  46. if (!RtcMediaPlayerEventHandlerDic.ContainsKey(playerId)) return;
  47. RtcMediaPlayerEventHandlerDic[playerId].OnPositionChanged(
  48. (Int64)AgoraJson.GetData<Int64>(jsonData, "position_ms")
  49. );
  50. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  51. });
  52. #endif
  53. break;
  54. case "MediaPlayerSourceObserver_onPlayerEvent":
  55. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  56. CallbackObject._CallbackQueue.EnQueue(() =>
  57. {
  58. #endif
  59. if (!RtcMediaPlayerEventHandlerDic.ContainsKey(playerId)) return;
  60. RtcMediaPlayerEventHandlerDic[playerId].OnPlayerEvent(
  61. (MEDIA_PLAYER_EVENT)AgoraJson.GetData<int>(jsonData, "eventCode"),
  62. (Int64)AgoraJson.GetData<Int64>(jsonData, "elapsedTime"),
  63. (string)AgoraJson.GetData<string>(jsonData, "message")
  64. );
  65. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  66. });
  67. #endif
  68. break;
  69. case "MediaPlayerSourceObserver_onPlayBufferUpdated":
  70. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  71. CallbackObject._CallbackQueue.EnQueue(() =>
  72. {
  73. #endif
  74. if (!RtcMediaPlayerEventHandlerDic.ContainsKey(playerId)) return;
  75. RtcMediaPlayerEventHandlerDic[playerId].OnPlayBufferUpdated(
  76. (Int64)AgoraJson.GetData<Int64>(jsonData, "playCachedBuffer")
  77. );
  78. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  79. });
  80. #endif
  81. break;
  82. case "MediaPlayerSourceObserver_onPreloadEvent":
  83. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  84. CallbackObject._CallbackQueue.EnQueue(() =>
  85. {
  86. #endif
  87. if (!RtcMediaPlayerEventHandlerDic.ContainsKey(playerId)) return;
  88. RtcMediaPlayerEventHandlerDic[playerId].OnPreloadEvent(
  89. (string)AgoraJson.GetData<string>(jsonData, "src"),
  90. (PLAYER_PRELOAD_EVENT)AgoraJson.GetData<int>(jsonData, "event")
  91. );
  92. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  93. });
  94. #endif
  95. break;
  96. case "MediaPlayerSourceObserver_onCompleted":
  97. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  98. CallbackObject._CallbackQueue.EnQueue(() =>
  99. {
  100. #endif
  101. if (!RtcMediaPlayerEventHandlerDic.ContainsKey(playerId)) return;
  102. RtcMediaPlayerEventHandlerDic[playerId].OnCompleted();
  103. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  104. });
  105. #endif
  106. break;
  107. case "MediaPlayerSourceObserver_onAgoraCDNTokenWillExpire":
  108. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  109. CallbackObject._CallbackQueue.EnQueue(() =>
  110. {
  111. #endif
  112. if (!RtcMediaPlayerEventHandlerDic.ContainsKey(playerId)) return;
  113. RtcMediaPlayerEventHandlerDic[playerId].OnAgoraCDNTokenWillExpire();
  114. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  115. });
  116. #endif
  117. break;
  118. case "MediaPlayerSourceObserver_onPlayerInfoUpdated":
  119. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  120. CallbackObject._CallbackQueue.EnQueue(() =>
  121. {
  122. #endif
  123. if (!RtcMediaPlayerEventHandlerDic.ContainsKey(playerId)) return;
  124. RtcMediaPlayerEventHandlerDic[playerId].OnPlayerInfoUpdated(
  125. AgoraJson.JsonToStruct<PlayerUpdatedInfo>(jsonData, "info")
  126. );
  127. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  128. });
  129. #endif
  130. break;
  131. case "MediaPlayerSourceObserver_onPlayerSrcInfoChanged":
  132. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  133. CallbackObject._CallbackQueue.EnQueue(() =>
  134. {
  135. #endif
  136. if (!RtcMediaPlayerEventHandlerDic.ContainsKey(playerId)) return;
  137. RtcMediaPlayerEventHandlerDic[playerId].OnPlayerSrcInfoChanged(
  138. AgoraJson.JsonToStruct<SrcInfo>(jsonData, "from"),
  139. AgoraJson.JsonToStruct<SrcInfo>(jsonData, "to")
  140. );
  141. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  142. });
  143. #endif
  144. break;
  145. case "MediaPlayerSourceObserver_onAudioVolumeIndication":
  146. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  147. CallbackObject._CallbackQueue.EnQueue(() =>
  148. {
  149. #endif
  150. if (!RtcMediaPlayerEventHandlerDic.ContainsKey(playerId)) return;
  151. RtcMediaPlayerEventHandlerDic[playerId].OnAudioVolumeIndication(
  152. (int)AgoraJson.GetData<int>(jsonData, "volume")
  153. );
  154. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  155. });
  156. #endif
  157. break;
  158. case "MediaPlayerSourceObserver_onMetaData":
  159. var byteLength = (int)AgoraJson.GetData<int>(jsonData, "length");
  160. var bufferPtr = (IntPtr)(UInt64)AgoraJson.GetData<UInt64>(jsonData, "data");
  161. var byteData = new byte[byteLength];
  162. if (byteLength != 0)
  163. {
  164. Marshal.Copy(bufferPtr, byteData, 0, (int)byteLength);
  165. }
  166. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  167. CallbackObject._CallbackQueue.EnQueue(() =>
  168. {
  169. #endif
  170. if (!RtcMediaPlayerEventHandlerDic.ContainsKey(playerId)) return;
  171. RtcMediaPlayerEventHandlerDic[playerId].OnMetaData(byteData, byteLength);
  172. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  173. });
  174. #endif
  175. break;
  176. }
  177. }
  178. }
  179. }