WrapperAndroid.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using UnityEngine;
  4. namespace UMP.Wrappers
  5. {
  6. internal class WrapperAndroid : IWrapperNative, IWrapperPlayer, IWrapperSpu, IWrapperAudio
  7. {
  8. private const string CLASS_PATH_CORE = "unitydirectionkit/universalmediaplayer/core/UniversalMediaPlayer";
  9. private const string CLASS_PATH_NATIVE = "unitydirectionkit/universalmediaplayer/nativeplayer/MediaPlayerNative";
  10. private const string CLASS_PATH_LIBVLC = "unitydirectionkit/universalmediaplayer/libvlcplayer/MediaPlayerVLC";
  11. private const string CLASS_PATH_EXO = "unitydirectionkit/universalmediaplayer/exoplayer/MediaPlayerExo";
  12. private AndroidJavaObject _coreObj;
  13. private object _playerExpanded;
  14. private int _nativeIndex;
  15. #region Native Imports
  16. [DllImport(UMPSettings.ASSET_NAME)]
  17. private static extern int UMPNativeInit();
  18. [DllImport(UMPSettings.ASSET_NAME)]
  19. private static extern void UMPNativeUpdateIndex(int index);
  20. [DllImport(UMPSettings.ASSET_NAME)]
  21. private static extern void UMPNativeSetTexture(int index, IntPtr texture);
  22. [DllImport(UMPSettings.ASSET_NAME)]
  23. private static extern void UMPNativeSetPixelsBuffer(int index, IntPtr buffer, int width, int height);
  24. [DllImport(UMPSettings.ASSET_NAME)]
  25. private static extern void UMPNativeUpdateFrameBuffer(int index);
  26. [DllImport(UMPSettings.ASSET_NAME)]
  27. private static extern IntPtr UMPNativeGetUnityRenderCallback();
  28. [DllImport(UMPSettings.ASSET_NAME)]
  29. private static extern IntPtr UMPNativeGetPlayCallback();
  30. [DllImport(UMPSettings.ASSET_NAME)]
  31. private static extern IntPtr UMPNativeGetUpdateSurfaceTextureCallback();
  32. [DllImport(UMPSettings.ASSET_NAME)]
  33. private static extern IntPtr UMPNativeGetUpdateFrameBufferCallback();
  34. #endregion
  35. public WrapperAndroid(PlayerOptionsAndroid options)
  36. {
  37. _nativeIndex = UMPNativeInit();
  38. AndroidJavaObject player = null;
  39. if (options.PlayerType == PlayerOptionsAndroid.PlayerTypes.Native)
  40. {
  41. player = new AndroidJavaObject(CLASS_PATH_NATIVE, new object[0]);
  42. }
  43. else if (options.PlayerType == PlayerOptionsAndroid.PlayerTypes.LibVLC)
  44. {
  45. player = new AndroidJavaObject(CLASS_PATH_LIBVLC, new object[0]);
  46. _playerExpanded = new ExpandedAndroidLibVLC(player);
  47. }
  48. else if (options.PlayerType == PlayerOptionsAndroid.PlayerTypes.Exo)
  49. {
  50. player = new AndroidJavaObject(CLASS_PATH_EXO, new object[0]);
  51. }
  52. var arg = new object[3];
  53. arg[0] = _nativeIndex;
  54. arg[1] = player;
  55. arg[2] = options.GetOptions('\n');
  56. _coreObj = new AndroidJavaObject(CLASS_PATH_CORE, arg);
  57. }
  58. #region Native
  59. public int NativeIndex
  60. {
  61. get
  62. {
  63. return _nativeIndex;
  64. }
  65. }
  66. public void NativeUpdateIndex()
  67. {
  68. UMPNativeUpdateIndex(_nativeIndex);
  69. }
  70. public void NativeSetTexture(IntPtr texture)
  71. {
  72. UMPNativeSetTexture(_nativeIndex, texture);
  73. }
  74. public void NativeSetPixelsBuffer(IntPtr buffer, int width, int height)
  75. {
  76. UMPNativeSetPixelsBuffer(_nativeIndex, buffer, width, height);
  77. }
  78. public void NativeUpdatePixelsBuffer()
  79. {
  80. if (SystemInfo.graphicsMultiThreaded)
  81. GL.IssuePluginEvent(UMPNativeGetUpdateFrameBufferCallback(), _nativeIndex);
  82. else
  83. UMPNativeUpdateFrameBuffer(_nativeIndex);
  84. }
  85. public IntPtr NativeGetUnityRenderCallback()
  86. {
  87. return UMPNativeGetUnityRenderCallback();
  88. }
  89. #endregion
  90. #region Player
  91. public void PlayerSetDataSource(string path, object playerObject = null)
  92. {
  93. if (_coreObj != null)
  94. _coreObj.Call("exportSetDataSource", path);
  95. }
  96. public bool PlayerPlay(object playerObject = null)
  97. {
  98. var started = false;
  99. if (SystemInfo.graphicsMultiThreaded)// && !_isReady)
  100. {
  101. GL.IssuePluginEvent(UMPNativeGetPlayCallback(), (int)_coreObj.GetRawObject());
  102. started = true;
  103. }
  104. else
  105. {
  106. started = _coreObj.Call<bool>("exportPlay");
  107. }
  108. return started;
  109. }
  110. public void PlayerPause(object playerObject = null)
  111. {
  112. if (_coreObj != null)
  113. _coreObj.Call("exportPause");
  114. }
  115. public void PlayerStop(object playerObject = null)
  116. {
  117. if (_coreObj != null)
  118. _coreObj.Call("exportStop");
  119. }
  120. public void PlayerRelease(object playerObject = null)
  121. {
  122. if (_coreObj != null)
  123. _coreObj.Call("exportRelease");
  124. }
  125. public bool PlayerIsPlaying(object playerObject = null)
  126. {
  127. if (_coreObj != null)
  128. return _coreObj.Call<bool>("exportIsPlaying");
  129. return false;
  130. }
  131. public bool PlayerIsReady(object playerObject = null)
  132. {
  133. if (_coreObj != null)
  134. return _coreObj.Call<bool>("exportIsReady");
  135. return false;
  136. }
  137. public long PlayerGetLength(object playerObject = null)
  138. {
  139. if (_coreObj != null)
  140. return _coreObj.Call<long>("exportDuration");
  141. return 0;
  142. }
  143. public long PlayerGetTime(object playerObject = null)
  144. {
  145. if (_coreObj != null)
  146. return _coreObj.Call<long>("exportGetTime");
  147. return 0;
  148. }
  149. public void PlayerSetTime(long time, object playerObject = null)
  150. {
  151. if (_coreObj != null)
  152. _coreObj.Call("exportSetTime", time);
  153. }
  154. public float PlayerGetPosition(object playerObject = null)
  155. {
  156. if (_coreObj != null)
  157. return _coreObj.Call<float>("exportGetPosition");
  158. return 0;
  159. }
  160. public void PlayerSetPosition(float pos, object playerObject = null)
  161. {
  162. if (_coreObj != null)
  163. _coreObj.Call("exportSetPosition", pos);
  164. }
  165. public float PlayerGetRate(object playerObject = null)
  166. {
  167. if (_coreObj != null)
  168. return _coreObj.Call<float>("exportGetPlaybackRate");
  169. return 1;
  170. }
  171. public bool PlayerSetRate(float rate, object playerObject = null)
  172. {
  173. if (_coreObj != null)
  174. {
  175. _coreObj.Call("exportSetPlaybackRate", rate);
  176. return true;
  177. }
  178. return false;
  179. }
  180. public int PlayerGetVolume(object playerObject = null)
  181. {
  182. if (_coreObj != null)
  183. return _coreObj.Call<int>("exportGetVolume");
  184. return 0;
  185. }
  186. public int PlayerSetVolume(int volume, object playerObject = null)
  187. {
  188. if (_coreObj != null)
  189. _coreObj.Call("exportSetVolume", volume);
  190. return 1;
  191. }
  192. public bool PlayerGetMute(object playerObject = null)
  193. {
  194. // Not used in this implementation
  195. return false;
  196. }
  197. public void PlayerSetMute(bool mute, object playerObject = null)
  198. {
  199. // Not used in this implementation
  200. }
  201. public int PlayerVideoWidth(object playerObject = null)
  202. {
  203. if (_coreObj != null)
  204. return _coreObj.Call<int>("exportVideoWidth");
  205. return 0;
  206. }
  207. public int PlayerVideoHeight(object playerObject = null)
  208. {
  209. if (_coreObj != null)
  210. return _coreObj.Call<int>("exportVideoHeight");
  211. return 0;
  212. }
  213. public int PlayerVideoFramesCounter(object playerObject = null)
  214. {
  215. if (_coreObj != null)
  216. return _coreObj.Call<int>("exportVideoFramesCounter");
  217. return 0;
  218. }
  219. public PlayerState PlayerGetState()
  220. {
  221. var eventValue = 0;
  222. if (_coreObj != null)
  223. eventValue = _coreObj.Call<int>("exportGetState");
  224. return (PlayerState)eventValue;
  225. }
  226. public object PlayerGetStateValue()
  227. {
  228. object value = _coreObj.Call<float>("exportGetStateFloatValue");
  229. if ((float)value < 0)
  230. {
  231. value = _coreObj.Call<long>("exportGetStateLongValue");
  232. if ((long)value < 0)
  233. value = _coreObj.Call<string>("exportGetStateStringValue");
  234. }
  235. return value;
  236. }
  237. #endregion
  238. #region Player Spu
  239. public MediaTrackInfo[] PlayerSpuGetTracks(object playerObject = null)
  240. {
  241. var tracks = new System.Collections.Generic.List<MediaTrackInfo>();
  242. if (_coreObj != null)
  243. {
  244. int tracksLength = _coreObj.Call<int>("exportGetSpuTracksLength");
  245. for (int i = 0; i < tracksLength; i++)
  246. {
  247. int id = _coreObj.Call<int>("exportGetSpuTrackId", i);
  248. string name = _coreObj.Call<string>("exportGetSpuTrackName", i);
  249. tracks.Add(new MediaTrackInfo(id, name));
  250. }
  251. }
  252. return tracks.ToArray();
  253. }
  254. public int PlayerSpuGetTrack(object playerObject = null)
  255. {
  256. if (_coreObj != null)
  257. return _coreObj.Call<int>("exportGetSpuTrack");
  258. return 0;
  259. }
  260. public int PlayerSpuSetTrack(int spuIndex, object playerObject = null)
  261. {
  262. if (_coreObj != null)
  263. _coreObj.Call("exportSetSpuTrack", spuIndex);
  264. return 0;
  265. }
  266. #endregion
  267. #region Player Audio
  268. public MediaTrackInfo[] PlayerAudioGetTracks(object playerObject = null)
  269. {
  270. var tracks = new System.Collections.Generic.List<MediaTrackInfo>();
  271. if (_coreObj != null)
  272. {
  273. int tracksLength = _coreObj.Call<int>("exportGetAudioTracksLength");
  274. for (int i = 0; i < tracksLength; i++)
  275. {
  276. int id = _coreObj.Call<int>("exportGetAudioTrackId", i);
  277. string name = _coreObj.Call<string>("exportGetAudioTrackName", i);
  278. tracks.Add(new MediaTrackInfo(id, name));
  279. }
  280. }
  281. return tracks.ToArray();
  282. }
  283. public int PlayerAudioGetTrack(object playerObject = null)
  284. {
  285. if (_coreObj != null)
  286. return _coreObj.Call<int>("exportGetAudioTrack");
  287. return 0;
  288. }
  289. public int PlayerAudioSetTrack(int audioIndex, object playerObject = null)
  290. {
  291. if (_coreObj != null)
  292. _coreObj.Call("exportSetAudioTrack", audioIndex);
  293. return 0;
  294. }
  295. #endregion
  296. #region Platform dependent functionality
  297. public object PlayerExpanded
  298. {
  299. get
  300. {
  301. return _playerExpanded;
  302. }
  303. }
  304. public void PlayerUpdateSurfaceTexture()
  305. {
  306. if (_coreObj != null)
  307. {
  308. if (SystemInfo.graphicsMultiThreaded)
  309. GL.IssuePluginEvent(UMPNativeGetUpdateSurfaceTextureCallback(), (int)_coreObj.GetRawObject());
  310. else
  311. _coreObj.Call("exportUpdateSurfaceTexture");
  312. }
  313. }
  314. public bool PlayerSetSubtitleFile(Uri path)
  315. {
  316. if (_coreObj != null)
  317. {
  318. var arg = new object[1];
  319. arg[0] = path.AbsoluteUri;
  320. return _coreObj.Call<bool>("exportSetSubtitleFile", arg);
  321. }
  322. return false;
  323. }
  324. #endregion
  325. }
  326. }