AndroidMediaPlayer.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725
  1. // NOTE: We only allow this script to compile in editor so we can easily check for compilation issues
  2. #if (UNITY_EDITOR || UNITY_ANDROID)
  3. #define AVPROVIDEO_FIXREGRESSION_TEXTUREQUALITY_UNITY542
  4. #define DLL_METHODS
  5. using UnityEngine;
  6. using System;
  7. using System.Text;
  8. using System.Runtime.InteropServices;
  9. //-----------------------------------------------------------------------------
  10. // Copyright 2015-2021 RenderHeads Ltd. All rights reserved.
  11. //-----------------------------------------------------------------------------
  12. namespace RenderHeads.Media.AVProVideo
  13. {
  14. /// <summary>
  15. /// Android implementation of BaseMediaPlayer
  16. /// </summary>
  17. // TODO: seal this class
  18. public class AndroidMediaPlayer : BaseMediaPlayer
  19. {
  20. protected static AndroidJavaObject s_ActivityContext = null;
  21. protected static AndroidJavaObject s_Interface = null;
  22. protected static bool s_bInitialised = false;
  23. private static string s_Version = "Plug-in not yet initialised";
  24. private static System.IntPtr _nativeFunction_RenderEvent = System.IntPtr.Zero;
  25. protected AndroidJavaObject m_Video;
  26. private Texture2D m_Texture;
  27. private int m_TextureHandle;
  28. private bool m_UseFastOesPath;
  29. // private string m_AuthToken;
  30. private double m_Duration = 0.0;
  31. private int m_Width = 0;
  32. private int m_Height = 0;
  33. protected int m_iPlayerIndex = -1;
  34. private Android.VideoApi m_API;
  35. private bool m_HeadRotationEnabled = false;
  36. private bool m_FocusEnabled = false;
  37. private System.IntPtr m_Method_Update;
  38. private System.IntPtr m_Method_SetHeadRotation;
  39. private System.IntPtr m_Method_GetCurrentTimeS;
  40. private System.IntPtr m_Method_GetSourceVideoFrameRate;
  41. private System.IntPtr m_Method_IsPlaying;
  42. private System.IntPtr m_Method_IsPaused;
  43. private System.IntPtr m_Method_IsFinished;
  44. private System.IntPtr m_Method_IsSeeking;
  45. private System.IntPtr m_Method_IsBuffering;
  46. private System.IntPtr m_Method_IsLooping;
  47. private System.IntPtr m_Method_HasVideo;
  48. private System.IntPtr m_Method_HasAudio;
  49. private System.IntPtr m_Method_HasMetaData;
  50. private System.IntPtr m_Method_SetFocusProps;
  51. private System.IntPtr m_Method_SetFocusEnabled;
  52. private System.IntPtr m_Method_SetFocusRotation;
  53. private jvalue[] m_Value0 = new jvalue[0];
  54. private jvalue[] m_Value1 = new jvalue[1];
  55. private jvalue[] m_Value2 = new jvalue[2];
  56. private jvalue[] m_Value4 = new jvalue[4];
  57. private MediaPlayer.OptionsAndroid m_Options;
  58. private enum NativeStereoPacking : int
  59. {
  60. Unknown = -1, // Unknown
  61. Monoscopic = 0, // Monoscopic
  62. TopBottom = 1, // Top is the left eye, bottom is the right eye
  63. LeftRight = 2, // Left is the left eye, right is the right eye
  64. Mesh = 3, // Use the mesh UV to unpack, uv0=left eye, uv1=right eye
  65. }
  66. #if AVPROVIDEO_FIXREGRESSION_TEXTUREQUALITY_UNITY542
  67. #if UNITY_2022_2_OR_NEWER
  68. // Note: See https://docs.unity3d.com/2022.2/Documentation/ScriptReference/QualitySettings-masterTextureLimit.html
  69. private int _textureQuality = QualitySettings.globalTextureMipmapLimit;
  70. #else
  71. private int _textureQuality = QualitySettings.masterTextureLimit;
  72. #endif
  73. #endif
  74. static private System.Threading.Thread m_MainThread;
  75. public static bool InitialisePlatform()
  76. {
  77. m_MainThread = System.Threading.Thread.CurrentThread;
  78. // Get the activity context
  79. if (s_ActivityContext == null)
  80. {
  81. AndroidJavaClass activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  82. if (activityClass != null)
  83. {
  84. s_ActivityContext = activityClass.GetStatic<AndroidJavaObject>("currentActivity");
  85. }
  86. }
  87. if (!s_bInitialised)
  88. {
  89. s_Interface = new AndroidJavaObject("com.renderheads.AVPro.Video.Manager");
  90. if (s_Interface != null)
  91. {
  92. s_Version = s_Interface.Call<string>("GetPluginVersion");
  93. s_Interface.Call("SetContext", s_ActivityContext);
  94. // Calling this native function cause the .SO library to become loaded
  95. // This is important for Unity < 5.2.0 where GL.IssuePluginEvent works differently
  96. _nativeFunction_RenderEvent = Native.GetRenderEventFunc();
  97. if (_nativeFunction_RenderEvent != IntPtr.Zero)
  98. {
  99. s_bInitialised = true;
  100. }
  101. }
  102. }
  103. return s_bInitialised;
  104. }
  105. public static void DeinitPlatform()
  106. {
  107. if (s_bInitialised)
  108. {
  109. if (s_Interface != null)
  110. {
  111. s_Interface.CallStatic("Deinitialise");
  112. s_Interface = null;
  113. }
  114. s_ActivityContext = null;
  115. s_bInitialised = false;
  116. }
  117. }
  118. private static void IssuePluginEvent(Native.AVPPluginEvent type, int param)
  119. {
  120. // Build eventId from the type and param.
  121. int eventId = 0x5d5ac000 | ((int)type << 8);
  122. switch (type)
  123. {
  124. case Native.AVPPluginEvent.PlayerSetup:
  125. case Native.AVPPluginEvent.PlayerUpdate:
  126. case Native.AVPPluginEvent.PlayerDestroy:
  127. case Native.AVPPluginEvent.ExtractFrame:
  128. {
  129. eventId |= param & 0xff;
  130. }
  131. break;
  132. }
  133. GL.IssuePluginEvent(_nativeFunction_RenderEvent, eventId);
  134. }
  135. private System.IntPtr GetMethod(string methodName, string signature)
  136. {
  137. Debug.Assert(m_Video != null);
  138. System.IntPtr result = AndroidJNIHelper.GetMethodID(m_Video.GetRawClass(), methodName, signature, false);
  139. Debug.Assert(result != System.IntPtr.Zero);
  140. if (result == System.IntPtr.Zero)
  141. {
  142. Debug.LogError("[AVProVideo] Unable to get method " + methodName + " " + signature);
  143. throw new System.Exception("[AVProVideo] Unable to get method " + methodName + " " + signature);
  144. }
  145. return result;
  146. }
  147. public AndroidMediaPlayer(MediaPlayer.OptionsAndroid options) : base()
  148. {
  149. Debug.Assert(s_Interface != null);
  150. Debug.Assert(s_bInitialised);
  151. m_Options = options;
  152. m_API = options.videoApi;
  153. if (SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Vulkan)
  154. {
  155. Debug.LogWarning("[AVProVideo] Vulkan graphics API is not supported. Please select OpenGL ES 2.0 and 3.0 are supported on Android.");
  156. }
  157. #if UNITY_2017_2_OR_NEWER
  158. Vector2 vPreferredVideo = GetPreferredVideoResolution(options.preferredMaximumResolution, options.customPreferredMaximumResolution);
  159. #else
  160. Vector2 vPreferredVideo = GetPreferredVideoResolution(options.preferredMaximumResolution, new Vector2(0.0f, 0.0f));
  161. #endif
  162. // Create a java-size video class up front
  163. Debug.Log("s_Interface " + s_Interface);
  164. m_Video = s_Interface.Call<AndroidJavaObject>( "CreatePlayer", (int)(m_API), options.useFastOesPath, options.preferSoftwareDecoder, (int)(options.audioOutput), (int)(options.audio360ChannelMode), Helper.GetUnityAudioSampleRate(),
  165. options.StartWithHighestBandwidth(), options.minBufferMs, options.maxBufferMs, options.bufferForPlaybackMs, options.bufferForPlaybackAfterRebufferMs,
  166. (int)(options.GetPreferredPeakBitRateInBitsPerSecond()), (int)(vPreferredVideo.x), (int)(vPreferredVideo.y), (int)(options.blitTextureFiltering) );
  167. Debug.Log("m_Video " + m_Video);
  168. if (m_Video != null)
  169. {
  170. m_Method_Update = GetMethod("Update", "()V");
  171. m_Method_SetHeadRotation = GetMethod("SetHeadRotation", "(FFFF)V");
  172. m_Method_SetFocusProps = GetMethod("SetFocusProps", "(FF)V");
  173. m_Method_SetFocusEnabled = GetMethod("SetFocusEnabled", "(Z)V");
  174. m_Method_SetFocusRotation = GetMethod("SetFocusRotation", "(FFFF)V");
  175. m_Method_GetCurrentTimeS = GetMethod("GetCurrentTimeS", "()D");
  176. m_Method_GetSourceVideoFrameRate = GetMethod("GetSourceVideoFrameRate", "()F");
  177. m_Method_IsPlaying = GetMethod("IsPlaying", "()Z");
  178. m_Method_IsPaused = GetMethod("IsPaused", "()Z");
  179. m_Method_IsFinished = GetMethod("IsFinished", "()Z");
  180. m_Method_IsSeeking = GetMethod("IsSeeking", "()Z");
  181. m_Method_IsBuffering = GetMethod("IsBuffering", "()Z");
  182. m_Method_IsLooping = GetMethod("IsLooping", "()Z");
  183. m_Method_HasVideo = GetMethod("HasVideo", "()Z");
  184. m_Method_HasAudio = GetMethod("HasAudio", "()Z");
  185. m_Method_HasMetaData = GetMethod("HasMetaData", "()Z");
  186. m_iPlayerIndex = m_Video.Call<int>("GetPlayerIndex");
  187. Helper.LogInfo("Creating player " + m_iPlayerIndex);
  188. SetOptions(options.useFastOesPath, options.showPosterFrame);
  189. // Initialise renderer, on the render thread
  190. AndroidMediaPlayer.IssuePluginEvent(Native.AVPPluginEvent.PlayerSetup, m_iPlayerIndex);
  191. }
  192. else
  193. {
  194. Debug.LogError("[AVProVideo] Failed to create player instance");
  195. }
  196. }
  197. public void SetOptions(bool useFastOesPath, bool showPosterFrame)
  198. {
  199. m_UseFastOesPath = useFastOesPath;
  200. if (m_Video != null)
  201. {
  202. // Show poster frame is only needed when using the MediaPlayer API
  203. showPosterFrame = (m_API == Android.VideoApi.MediaPlayer) ? showPosterFrame:false;
  204. m_Video.Call("SetPlayerOptions", m_UseFastOesPath, showPosterFrame);
  205. }
  206. }
  207. public override long GetEstimatedTotalBandwidthUsed()
  208. {
  209. long result = -1;
  210. if (s_Interface != null)
  211. {
  212. result = m_Video.Call<long>("GetEstimatedBandwidthUsed");
  213. }
  214. return result;
  215. }
  216. public override string GetVersion()
  217. {
  218. return s_Version;
  219. }
  220. public override string GetExpectedVersion()
  221. {
  222. return Helper.ExpectedPluginVersion.Android;
  223. }
  224. public override bool OpenMedia(string path, long offset, string httpHeader, MediaHints mediaHints, int forceFileFormat = 0, bool startWithHighestBitrate = false)
  225. {
  226. bool bReturn = false;
  227. if (m_Video != null)
  228. {
  229. _mediaHints = mediaHints;
  230. Debug.Assert(m_Width == 0 && m_Height == 0 && m_Duration == 0.0);
  231. bReturn = m_Video.Call<bool>("OpenVideoFromFile", path, offset, httpHeader, forceFileFormat, (int)(m_Options.audioOutput), (int)(m_Options.audio360ChannelMode));
  232. if (!bReturn)
  233. {
  234. DisplayLoadFailureSuggestion(path);
  235. }
  236. }
  237. else
  238. {
  239. Debug.LogError("[AVProVideo] m_Video is null!");
  240. }
  241. return bReturn;
  242. }
  243. public override void SetKeyServerAuthToken(string token)
  244. {
  245. if (m_Video != null)
  246. {
  247. m_Video.Call("SetKeyServerAuthToken", token);
  248. }
  249. }
  250. public override void SetOverrideDecryptionKey(byte[] key)
  251. {
  252. if( m_Video != null )
  253. {
  254. m_Video.Call("SetOverrideDecryptionKey", key);
  255. }
  256. }
  257. private void DisplayLoadFailureSuggestion(string path)
  258. {
  259. if (path.ToLower().Contains("http://"))
  260. {
  261. Debug.LogError("Android 8 and above require HTTPS by default, change to HTTPS or enable ClearText in the AndroidManifest.xml");
  262. }
  263. }
  264. public override void CloseMedia()
  265. {
  266. if (m_Texture != null)
  267. {
  268. Texture2D.Destroy(m_Texture);
  269. m_Texture = null;
  270. }
  271. m_TextureHandle = 0;
  272. m_Duration = 0.0;
  273. m_Width = 0;
  274. m_Height = 0;
  275. if (m_Video != null)
  276. {
  277. m_Video.Call("CloseVideo");
  278. }
  279. base.CloseMedia();
  280. }
  281. public override void SetLooping( bool bLooping )
  282. {
  283. if( m_Video != null )
  284. {
  285. m_Video.Call("SetLooping", bLooping);
  286. }
  287. }
  288. public override bool IsLooping()
  289. {
  290. bool result = false;
  291. if( m_Video != null )
  292. {
  293. if (m_Method_IsLooping != System.IntPtr.Zero)
  294. {
  295. result = AndroidJNI.CallBooleanMethod(m_Video.GetRawObject(), m_Method_IsLooping, m_Value0);
  296. }
  297. else
  298. {
  299. result = m_Video.Call<bool>("IsLooping");
  300. }
  301. }
  302. return result;
  303. }
  304. public override bool HasVideo()
  305. {
  306. bool result = false;
  307. if( m_Video != null )
  308. {
  309. if (m_Method_HasVideo != System.IntPtr.Zero)
  310. {
  311. result = AndroidJNI.CallBooleanMethod(m_Video.GetRawObject(), m_Method_HasVideo, m_Value0);
  312. }
  313. else
  314. {
  315. result = m_Video.Call<bool>("HasVideo");
  316. }
  317. }
  318. return result;
  319. }
  320. public override bool HasAudio()
  321. {
  322. bool result = false;
  323. if( m_Video != null )
  324. {
  325. if (m_Method_HasAudio != System.IntPtr.Zero)
  326. {
  327. result = AndroidJNI.CallBooleanMethod(m_Video.GetRawObject(), m_Method_HasAudio, m_Value0);
  328. }
  329. else
  330. {
  331. result = m_Video.Call<bool>("HasAudio");
  332. }
  333. }
  334. return result;
  335. }
  336. public override bool HasMetaData()
  337. {
  338. bool result = false;
  339. if (m_Video != null)
  340. {
  341. if (m_Method_HasMetaData != System.IntPtr.Zero)
  342. {
  343. result = AndroidJNI.CallBooleanMethod(m_Video.GetRawObject(), m_Method_HasMetaData, m_Value0);
  344. }
  345. else
  346. {
  347. result = m_Video.Call<bool>("HasMetaData");
  348. }
  349. }
  350. return result;
  351. }
  352. public override bool CanPlay()
  353. {
  354. bool result = false;
  355. #if DLL_METHODS
  356. result = Native._CanPlay( m_iPlayerIndex );
  357. #else
  358. if (m_Video != null)
  359. {
  360. result = m_Video.Call<bool>("CanPlay");
  361. }
  362. #endif
  363. return result;
  364. }
  365. public override void Play()
  366. {
  367. if (m_Video != null)
  368. {
  369. m_Video.Call("Play");
  370. }
  371. }
  372. public override void Pause()
  373. {
  374. if (m_Video != null)
  375. {
  376. m_Video.Call("Pause");
  377. }
  378. }
  379. public override void Stop()
  380. {
  381. if (m_Video != null)
  382. {
  383. // On Android we never need to actually Stop the playback, pausing is fine
  384. m_Video.Call("Pause");
  385. }
  386. }
  387. public override void Seek(double time)
  388. {
  389. if (m_Video != null)
  390. {
  391. // time is in seconds
  392. m_Video.Call("Seek", time);
  393. }
  394. }
  395. public override void SeekFast(double time)
  396. {
  397. if (m_Video != null)
  398. {
  399. // time is in seconds
  400. m_Video.Call("SeekFast", time);
  401. }
  402. }
  403. public override double GetCurrentTime()
  404. {
  405. double result = 0.0;
  406. if (m_Video != null)
  407. {
  408. if (m_Method_GetCurrentTimeS != System.IntPtr.Zero)
  409. {
  410. result = AndroidJNI.CallDoubleMethod(m_Video.GetRawObject(), m_Method_GetCurrentTimeS, m_Value0);
  411. }
  412. else
  413. {
  414. result = (double)m_Video.Call<double>("GetCurrentTimeS");
  415. }
  416. }
  417. return result;
  418. }
  419. public override void SetPlaybackRate(float rate)
  420. {
  421. if (m_Video != null)
  422. {
  423. m_Video.Call("SetPlaybackRate", rate);
  424. }
  425. }
  426. public override float GetPlaybackRate()
  427. {
  428. float result = 0.0f;
  429. if (m_Video != null)
  430. {
  431. result = m_Video.Call<float>("GetPlaybackRate");
  432. }
  433. return result;
  434. }
  435. public override void SetAudioHeadRotation(Quaternion q)
  436. {
  437. if (m_Video != null)
  438. {
  439. if (!m_HeadRotationEnabled)
  440. {
  441. m_Video.Call("SetPositionTrackingEnabled", true);
  442. m_HeadRotationEnabled = true;
  443. }
  444. if (m_Method_SetHeadRotation != System.IntPtr.Zero)
  445. {
  446. m_Value4[0].f = q.x;
  447. m_Value4[1].f = q.y;
  448. m_Value4[2].f = q.z;
  449. m_Value4[3].f = q.w;
  450. AndroidJNI.CallVoidMethod(m_Video.GetRawObject(), m_Method_SetHeadRotation, m_Value4);
  451. }
  452. else
  453. {
  454. m_Video.Call("SetHeadRotation", q.x, q.y, q.z, q.w);
  455. }
  456. }
  457. }
  458. public override void ResetAudioHeadRotation()
  459. {
  460. if (m_Video != null && m_HeadRotationEnabled)
  461. {
  462. m_Video.Call("SetPositionTrackingEnabled", false);
  463. m_HeadRotationEnabled = false;
  464. }
  465. }
  466. public override void SetAudioFocusEnabled(bool enabled)
  467. {
  468. if (m_Video != null && enabled != m_FocusEnabled)
  469. {
  470. if (m_Method_SetFocusEnabled != System.IntPtr.Zero)
  471. {
  472. m_Value1[0].z = enabled;
  473. AndroidJNI.CallVoidMethod(m_Video.GetRawObject(), m_Method_SetFocusEnabled, m_Value1);
  474. }
  475. else
  476. {
  477. m_Video.Call("SetFocusEnabled", enabled);
  478. }
  479. m_FocusEnabled = enabled;
  480. }
  481. }
  482. public override void SetAudioFocusProperties(float offFocusLevel, float widthDegrees)
  483. {
  484. if(m_Video != null && m_FocusEnabled)
  485. {
  486. if (m_Method_SetFocusProps != System.IntPtr.Zero)
  487. {
  488. m_Value2[0].f = offFocusLevel;
  489. m_Value2[1].f = widthDegrees;
  490. AndroidJNI.CallVoidMethod(m_Video.GetRawObject(), m_Method_SetFocusProps, m_Value2);
  491. }
  492. else
  493. {
  494. m_Video.Call("SetFocusProps", offFocusLevel, widthDegrees);
  495. }
  496. }
  497. }
  498. public override void SetAudioFocusRotation(Quaternion q)
  499. {
  500. if (m_Video != null && m_FocusEnabled)
  501. {
  502. if (m_Method_SetFocusRotation != System.IntPtr.Zero)
  503. {
  504. m_Value4[0].f = q.x;
  505. m_Value4[1].f = q.y;
  506. m_Value4[2].f = q.z;
  507. m_Value4[3].f = q.w;
  508. AndroidJNI.CallVoidMethod(m_Video.GetRawObject(), m_Method_SetFocusRotation, m_Value4);
  509. }
  510. else
  511. {
  512. m_Video.Call("SetFocusRotation", q.x, q.y, q.z, q.w);
  513. }
  514. }
  515. }
  516. public override void ResetAudioFocus()
  517. {
  518. if (m_Video != null)
  519. {
  520. if (m_Method_SetFocusProps != System.IntPtr.Zero &&
  521. m_Method_SetFocusEnabled != System.IntPtr.Zero &&
  522. m_Method_SetFocusRotation != System.IntPtr.Zero)
  523. {
  524. m_Value2[0].f = 0f;
  525. m_Value2[1].f = 90f;
  526. AndroidJNI.CallVoidMethod(m_Video.GetRawObject(), m_Method_SetFocusProps, m_Value2);
  527. m_Value1[0].z = false;
  528. AndroidJNI.CallVoidMethod(m_Video.GetRawObject(), m_Method_SetFocusEnabled, m_Value1);
  529. m_Value4[0].f = 0f;
  530. m_Value4[1].f = 0f;
  531. m_Value4[2].f = 0f;
  532. m_Value4[3].f = 1f;
  533. AndroidJNI.CallVoidMethod(m_Video.GetRawObject(), m_Method_SetFocusRotation, m_Value4);
  534. }
  535. else
  536. {
  537. m_Video.Call("SetFocusProps", 0f, 90f);
  538. m_Video.Call("SetFocusEnabled", false);
  539. m_Video.Call("SetFocusRotation", 0f, 0f, 0f, 1f);
  540. }
  541. }
  542. }
  543. public override double GetDuration()
  544. {
  545. return m_Duration;
  546. }
  547. public override int GetVideoWidth()
  548. {
  549. return m_Width;
  550. }
  551. public override int GetVideoHeight()
  552. {
  553. return m_Height;
  554. }
  555. public override float GetVideoFrameRate()
  556. {
  557. float result = 0.0f;
  558. if( m_Video != null )
  559. {
  560. if (m_Method_GetSourceVideoFrameRate != System.IntPtr.Zero)
  561. {
  562. result = AndroidJNI.CallFloatMethod(m_Video.GetRawObject(), m_Method_GetSourceVideoFrameRate, m_Value0);
  563. }
  564. else
  565. {
  566. result = m_Video.Call<float>("GetSourceVideoFrameRate");
  567. }
  568. }
  569. return result;
  570. }
  571. public override float GetVideoDisplayRate()
  572. {
  573. float result = 0.0f;
  574. #if DLL_METHODS
  575. result = Native._GetVideoDisplayRate( m_iPlayerIndex );
  576. #else
  577. if (m_Video != null)
  578. {
  579. result = m_Video.Call<float>("GetDisplayRate");
  580. }
  581. #endif
  582. return result;
  583. }
  584. public override bool IsSeeking()
  585. {
  586. bool result = false;
  587. if (m_Video != null)
  588. {
  589. if (m_Method_IsSeeking != System.IntPtr.Zero)
  590. {
  591. result = AndroidJNI.CallBooleanMethod(m_Video.GetRawObject(), m_Method_IsSeeking, m_Value0);
  592. }
  593. else
  594. {
  595. result = m_Video.Call<bool>("IsSeeking");
  596. }
  597. }
  598. return result;
  599. }
  600. public override bool IsPlaying()
  601. {
  602. bool result = false;
  603. if (m_Video != null)
  604. {
  605. if (m_Method_IsPlaying != System.IntPtr.Zero)
  606. {
  607. result = AndroidJNI.CallBooleanMethod(m_Video.GetRawObject(), m_Method_IsPlaying, m_Value0);
  608. }
  609. else
  610. {
  611. result = m_Video.Call<bool>("IsPlaying");
  612. }
  613. }
  614. return result;
  615. }
  616. public override bool IsPaused()
  617. {
  618. bool result = false;
  619. if (m_Video != null)
  620. {
  621. if (m_Method_IsPaused != System.IntPtr.Zero)
  622. {
  623. result = AndroidJNI.CallBooleanMethod(m_Video.GetRawObject(), m_Method_IsPaused, m_Value0);
  624. }
  625. else
  626. {
  627. result = m_Video.Call<bool>("IsPaused");
  628. }
  629. }
  630. return result;
  631. }
  632. public override bool IsFinished()
  633. {
  634. bool result = false;
  635. if (m_Video != null)
  636. {
  637. if (m_Method_IsFinished != System.IntPtr.Zero)
  638. {
  639. result = AndroidJNI.CallBooleanMethod(m_Video.GetRawObject(), m_Method_IsFinished, m_Value0);
  640. }
  641. else
  642. {
  643. result = m_Video.Call<bool>("IsFinished");
  644. }
  645. }
  646. return result;
  647. }
  648. public override bool IsBuffering()
  649. {
  650. bool result = false;
  651. if (m_Video != null)
  652. {
  653. if (m_Method_IsBuffering != System.IntPtr.Zero)
  654. {
  655. result = AndroidJNI.CallBooleanMethod(m_Video.GetRawObject(), m_Method_IsBuffering, m_Value0);
  656. }
  657. else
  658. {
  659. result = m_Video.Call<bool>("IsBuffering");
  660. }
  661. }
  662. return result;
  663. }
  664. public override Texture GetTexture( int index )
  665. {
  666. Texture result = null;
  667. if (GetTextureFrameCount() > 0)
  668. {
  669. result = m_Texture;
  670. }
  671. return result;
  672. }
  673. public override int GetTextureFrameCount()
  674. {
  675. int result = 0;
  676. if( m_Texture != null )
  677. {
  678. #if DLL_METHODS
  679. result = Native._GetFrameCount( m_iPlayerIndex );
  680. #else
  681. if (m_Video != null)
  682. {
  683. result = m_Video.Call<int>("GetFrameCount");
  684. }
  685. #endif
  686. }
  687. return result;
  688. }
  689. internal override StereoPacking InternalGetTextureStereoPacking()
  690. {
  691. StereoPacking result = StereoPacking.Unknown;
  692. if (m_Video != null)
  693. {
  694. NativeStereoPacking internalStereoMode = (NativeStereoPacking)( m_Video.Call<int>("GetCurrentVideoTrackStereoMode") );
  695. switch( internalStereoMode )
  696. {
  697. case NativeStereoPacking.Monoscopic: result = StereoPacking.None; break;
  698. case NativeStereoPacking.TopBottom: result = StereoPacking.TopBottom; break;
  699. case NativeStereoPacking.LeftRight: result = StereoPacking.LeftRight; break;
  700. case NativeStereoPacking.Mesh: result = StereoPacking.Unknown; break;
  701. }
  702. }
  703. return result;
  704. }
  705. public override bool RequiresVerticalFlip()
  706. {
  707. return false;
  708. }
  709. public override void MuteAudio(bool bMuted)
  710. {
  711. if (m_Video != null)
  712. {
  713. m_Video.Call("MuteAudio", bMuted);
  714. }
  715. }
  716. public override bool IsMuted()
  717. {
  718. bool result = false;
  719. if( m_Video != null )
  720. {
  721. result = m_Video.Call<bool>("IsMuted");
  722. }
  723. return result;
  724. }
  725. public override void SetVolume(float volume)
  726. {
  727. if (m_Video != null)
  728. {
  729. m_Video.Call("SetVolume", volume);
  730. }
  731. }
  732. public override float GetVolume()
  733. {
  734. float result = 0.0f;
  735. if( m_Video != null )
  736. {
  737. result = m_Video.Call<float>("GetVolume");
  738. }
  739. return result;
  740. }
  741. public override void SetBalance(float balance)
  742. {
  743. if( m_Video != null )
  744. {
  745. m_Video.Call("SetAudioPan", balance);
  746. }
  747. }
  748. public override float GetBalance()
  749. {
  750. float result = 0.0f;
  751. if( m_Video != null )
  752. {
  753. result = m_Video.Call<float>("GetAudioPan");
  754. }
  755. return result;
  756. }
  757. public override bool WaitForNextFrame(Camera dummyCamera, int previousFrameCount)
  758. {
  759. // Mark as extracting
  760. bool isMultiThreaded = m_Video.Call<bool>("StartExtractFrame");
  761. // In single threaded Android this method won't work, so just return
  762. if (isMultiThreaded)
  763. {
  764. // Queue up render thread event to wait for the new frame
  765. IssuePluginEvent(Native.AVPPluginEvent.ExtractFrame, m_iPlayerIndex);
  766. // Force render thread to run
  767. dummyCamera.Render();
  768. // Wait for the frame to change
  769. m_Video.Call("WaitForExtract");
  770. // Return whether the frame changed
  771. return (previousFrameCount != GetTextureFrameCount());
  772. }
  773. return false;
  774. }
  775. public override long GetTextureTimeStamp()
  776. {
  777. long timeStamp = long.MinValue;
  778. if (m_Video != null)
  779. {
  780. timeStamp = m_Video.Call<long>("GetTextureTimeStamp");
  781. }
  782. return timeStamp;
  783. }
  784. public override void Render()
  785. {
  786. if (m_Video != null)
  787. {
  788. if (m_UseFastOesPath)
  789. {
  790. // This is needed for at least Unity 5.5.0, otherwise it just renders black in OES mode
  791. GL.InvalidateState();
  792. }
  793. AndroidMediaPlayer.IssuePluginEvent( Native.AVPPluginEvent.PlayerUpdate, m_iPlayerIndex );
  794. if (m_UseFastOesPath)
  795. {
  796. GL.InvalidateState();
  797. }
  798. }
  799. }
  800. public override void OnEnable()
  801. {
  802. base.OnEnable();
  803. #if DLL_METHODS
  804. int textureHandle = Native._GetTextureHandle(m_iPlayerIndex);
  805. #else
  806. int textureHandle = m_Video.Call<int>("GetTextureHandle");
  807. #endif
  808. if (m_Texture != null && textureHandle > 0 && m_Texture.GetNativeTexturePtr() == System.IntPtr.Zero)
  809. {
  810. //Debug.Log("RECREATING");
  811. m_Texture.UpdateExternalTexture(new System.IntPtr(textureHandle));
  812. }
  813. #if AVPROVIDEO_FIXREGRESSION_TEXTUREQUALITY_UNITY542
  814. #if UNITY_2022_2_OR_NEWER
  815. // Note: See https://docs.unity3d.com/2022.2/Documentation/ScriptReference/QualitySettings-masterTextureLimit.html
  816. _textureQuality = QualitySettings.globalTextureMipmapLimit;
  817. #else
  818. _textureQuality = QualitySettings.masterTextureLimit;
  819. #endif
  820. #endif
  821. }
  822. public override System.DateTime GetProgramDateTime()
  823. {
  824. System.DateTime result;
  825. if (m_Video != null)
  826. {
  827. double seconds = m_Video.Call<double>("GetCurrentAbsoluteTimestamp");
  828. result = Helper.ConvertSecondsSince1970ToDateTime(seconds);
  829. }
  830. else
  831. {
  832. result = base.GetProgramDateTime();
  833. }
  834. return result;
  835. }
  836. public override void Update()
  837. {
  838. if (m_Video != null)
  839. {
  840. if (m_Method_Update != System.IntPtr.Zero)
  841. {
  842. AndroidJNI.CallVoidMethod(m_Video.GetRawObject(), m_Method_Update, m_Value0);
  843. }
  844. else
  845. {
  846. m_Video.Call("Update");
  847. }
  848. // _lastError = (ErrorCode)( m_Video.Call<int>("GetLastErrorCode") );
  849. _lastError = (ErrorCode)( Native._GetLastErrorCode( m_iPlayerIndex ) );
  850. }
  851. if( m_Options.HasChanged( MediaPlayer.OptionsAndroid.ChangeFlags.All, true ) )
  852. {
  853. if (m_Video != null)
  854. {
  855. #if UNITY_2017_2_OR_NEWER
  856. Vector2 vPreferredVideo = GetPreferredVideoResolution(m_Options.preferredMaximumResolution, m_Options.customPreferredMaximumResolution);
  857. #else
  858. Vector2 vPreferredVideo = GetPreferredVideoResolution(m_Options.preferredMaximumResolution, new Vector2(0.0f, 0.0f));
  859. #endif
  860. int iNewBitrate = (int)(m_Options.GetPreferredPeakBitRateInBitsPerSecond());
  861. /*bool bSetMaxResolutionAndBitrate =*/ m_Video.Call<bool>("SetPreferredVideoResolutionAndBitrate", (int)(vPreferredVideo.x), (int)(vPreferredVideo.y), iNewBitrate);
  862. }
  863. }
  864. /*
  865. m_fTestTime += Time.deltaTime;
  866. if( m_fTestTime > 4.0f )
  867. {
  868. m_fTestTime = 0.0f;
  869. int iNumStreams = InternalGetAdaptiveStreamCount( TrackType.Video );
  870. int iNewStreamIndex = UnityEngine.Random.Range( -1, iNumStreams );
  871. SetVideoAdaptiveStreamIndex( TrackType.Video, iNewStreamIndex );
  872. }
  873. */
  874. // Call before the native update call
  875. UpdateTracks();
  876. UpdateTextCue();
  877. UpdateSubtitles();
  878. UpdateTimeRanges();
  879. UpdateDisplayFrameRate();
  880. if (Mathf.Approximately((float)m_Duration, 0f))
  881. {
  882. #if DLL_METHODS
  883. m_Duration = (double)( Native._GetDuration( m_iPlayerIndex ) );
  884. #else
  885. m_Duration = m_Video.Call<double>("GetDurationS");
  886. #endif
  887. // if( m_DurationMs > 0.0f ) { Helper.LogInfo("Duration: " + m_DurationMs); }
  888. }
  889. // Check if we can create the texture
  890. // Scan for a change in resolution
  891. int newWidth = -1;
  892. int newHeight = -1;
  893. if (m_Texture != null)
  894. {
  895. #if DLL_METHODS
  896. newWidth = Native._GetWidth(m_iPlayerIndex);
  897. newHeight = Native._GetHeight(m_iPlayerIndex);
  898. #else
  899. newWidth = m_Video.Call<int>("GetWidth");
  900. newHeight = m_Video.Call<int>("GetHeight");
  901. #endif
  902. if (newWidth != m_Width || newHeight != m_Height)
  903. {
  904. m_Texture = null;
  905. m_TextureHandle = 0;
  906. }
  907. }
  908. #if DLL_METHODS
  909. int textureHandle = Native._GetTextureHandle(m_iPlayerIndex);
  910. #else
  911. int textureHandle = m_Video.Call<int>("GetTextureHandle");
  912. #endif
  913. if (textureHandle != 0 && textureHandle != m_TextureHandle)
  914. {
  915. // Already got? (from above)
  916. if (newWidth == -1 || newHeight == -1)
  917. {
  918. #if DLL_METHODS
  919. newWidth = Native._GetWidth(m_iPlayerIndex);
  920. newHeight = Native._GetHeight(m_iPlayerIndex);
  921. #else
  922. newWidth = m_Video.Call<int>("GetWidth");
  923. newHeight = m_Video.Call<int>("GetHeight");
  924. #endif
  925. }
  926. if (Mathf.Max(newWidth, newHeight) > SystemInfo.maxTextureSize)
  927. {
  928. m_Width = newWidth;
  929. m_Height = newHeight;
  930. m_TextureHandle = textureHandle;
  931. Debug.LogError("[AVProVideo] Video dimensions larger than maxTextureSize");
  932. }
  933. else if (newWidth > 0 && newHeight > 0)
  934. {
  935. m_Width = newWidth;
  936. m_Height = newHeight;
  937. m_TextureHandle = textureHandle;
  938. switch (m_API)
  939. {
  940. case Android.VideoApi.MediaPlayer:
  941. _playerDescription = "MediaPlayer";
  942. break;
  943. case Android.VideoApi.ExoPlayer:
  944. _playerDescription = "ExoPlayer";
  945. break;
  946. default:
  947. _playerDescription = "UnknownPlayer";
  948. break;
  949. }
  950. if (m_UseFastOesPath)
  951. {
  952. _playerDescription += " OES";
  953. }
  954. else
  955. {
  956. _playerDescription += " NonOES";
  957. }
  958. Helper.LogInfo("Using playback path: " + _playerDescription + " (" + m_Width + "x" + m_Height + "@" + GetVideoFrameRate().ToString("F2") + ")");
  959. // NOTE: From Unity 5.4.x when using OES textures, an error "OPENGL NATIVE PLUG-IN ERROR: GL_INVALID_OPERATION: Operation illegal in current state" will be logged.
  960. // We assume this is because we're passing in TextureFormat.RGBA32 which isn't the true texture format. This error should be safe to ignore.
  961. m_Texture = Texture2D.CreateExternalTexture(m_Width, m_Height, TextureFormat.RGBA32, false, false, new System.IntPtr(textureHandle));
  962. if (m_Texture != null)
  963. {
  964. ApplyTextureProperties(m_Texture);
  965. }
  966. Helper.LogInfo("Texture ID: " + textureHandle);
  967. }
  968. }
  969. #if AVPROVIDEO_FIXREGRESSION_TEXTUREQUALITY_UNITY542
  970. #if UNITY_2022_2_OR_NEWER
  971. // Note: See https://docs.unity3d.com/2022.2/Documentation/ScriptReference/QualitySettings-masterTextureLimit.html
  972. int textureQualityToTestAgainst = QualitySettings.globalTextureMipmapLimit;
  973. #else
  974. int textureQualityToTestAgainst = QualitySettings.masterTextureLimit;
  975. #endif
  976. // In Unity 5.4.2 and above the video texture turns black when changing the TextureQuality in the Quality Settings
  977. // The code below gets around this issue. A bug report has been sent to Unity. So far we have tested and replicated the
  978. // "bug" in Windows only, but a user has reported it in Android too.
  979. // Texture.GetNativeTexturePtr() must sync with the rendering thread, so this is a large performance hit!
  980. if (_textureQuality != textureQualityToTestAgainst)
  981. {
  982. if (m_Texture != null && textureHandle > 0 && m_Texture.GetNativeTexturePtr() == System.IntPtr.Zero)
  983. {
  984. //Debug.Log("RECREATING");
  985. m_Texture.UpdateExternalTexture(new System.IntPtr(textureHandle));
  986. }
  987. _textureQuality = textureQualityToTestAgainst;
  988. }
  989. #endif
  990. }
  991. protected override void ApplyTextureProperties(Texture texture)
  992. {
  993. // NOTE: According to OES_EGL_image_external: For external textures, the default min filter is GL_LINEAR and the default S and T wrap modes are GL_CLAMP_TO_EDGE
  994. // See https://www.khronos.org/registry/gles/extensions/OES/OES_EGL_image_external_essl3.txt
  995. // But there is a new extension that allows some wrap modes:
  996. // https://www.khronos.org/registry/OpenGL/extensions/EXT/EXT_EGL_image_external_wrap_modes.txt
  997. // So really we need to detect whether these extensions exist when m_UseFastOesPath is true
  998. //if (!m_UseFastOesPath)
  999. {
  1000. base.ApplyTextureProperties(texture);
  1001. }
  1002. }
  1003. public override bool PlayerSupportsLinearColorSpace()
  1004. {
  1005. return false;
  1006. }
  1007. public override float[] GetTextureTransform()
  1008. {
  1009. float[] transform = null;
  1010. if (m_Video != null)
  1011. {
  1012. transform = m_Video.Call<float[]>("GetTextureTransform");
  1013. /*if (transform != null)
  1014. {
  1015. Debug.Log("xform: " + transform[0] + " " + transform[1] + " " + transform[2] + " " + transform[3] + " " + transform[4] + " " + transform[5]);
  1016. }*/
  1017. }
  1018. return transform;
  1019. }
  1020. public override void Dispose()
  1021. {
  1022. //Debug.LogError("DISPOSE");
  1023. if (m_Video != null)
  1024. {
  1025. m_Video.Call("SetDeinitialiseFlagged");
  1026. m_Video.Dispose();
  1027. m_Video = null;
  1028. }
  1029. if (s_Interface != null)
  1030. {
  1031. s_Interface.Call("DestroyPlayer", m_iPlayerIndex);
  1032. }
  1033. if (m_Texture != null)
  1034. {
  1035. Texture2D.Destroy(m_Texture);
  1036. m_Texture = null;
  1037. }
  1038. // Deinitialise player (replaces call directly as GL textures are involved)
  1039. AndroidMediaPlayer.IssuePluginEvent( Native.AVPPluginEvent.PlayerDestroy, m_iPlayerIndex );
  1040. }
  1041. private void UpdateTimeRanges()
  1042. {
  1043. if( m_Video != null )
  1044. {
  1045. // Seekable time ranges
  1046. AndroidJavaObject seekableReturnObject = m_Video.Call<AndroidJavaObject>("GetSeekableTimeRanges");
  1047. if( seekableReturnObject.GetRawObject().ToInt32() != 0 )
  1048. {
  1049. double[] aSeekableRanges = AndroidJNIHelper.ConvertFromJNIArray<double[]>( seekableReturnObject.GetRawObject() );
  1050. if( aSeekableRanges.Length > 1 )
  1051. {
  1052. int iNumRanges = aSeekableRanges.Length / 2;
  1053. _seekableTimes._ranges = new TimeRange[ iNumRanges ];
  1054. int iRangeIndex = 0;
  1055. for( int iRange = 0; iRange < iNumRanges; ++iRange )
  1056. {
  1057. _seekableTimes._ranges[ iRange ] = new TimeRange( aSeekableRanges[ iRangeIndex ], aSeekableRanges[ iRangeIndex + 1 ] );
  1058. iRangeIndex += 2;
  1059. }
  1060. _seekableTimes.CalculateRange();
  1061. }
  1062. seekableReturnObject.Dispose();
  1063. }
  1064. // Buffered time ranges
  1065. AndroidJavaObject bufferedReturnObject = m_Video.Call<AndroidJavaObject>("GetBufferedTimeRanges");
  1066. if( bufferedReturnObject.GetRawObject().ToInt32() != 0 )
  1067. {
  1068. double[] aBufferedRanges = AndroidJNIHelper.ConvertFromJNIArray<double[]>( bufferedReturnObject.GetRawObject() );
  1069. if( aBufferedRanges.Length > 1 )
  1070. {
  1071. int iNumRanges = aBufferedRanges.Length / 2;
  1072. _bufferedTimes._ranges = new TimeRange[ iNumRanges ];
  1073. int iRangeIndex = 0;
  1074. for( int iRange = 0; iRange < iNumRanges; ++iRange )
  1075. {
  1076. _bufferedTimes._ranges[iRange] = new TimeRange( aBufferedRanges[iRangeIndex], aBufferedRanges[iRangeIndex + 1] );
  1077. iRangeIndex += 2;
  1078. }
  1079. _bufferedTimes.CalculateRange();
  1080. }
  1081. bufferedReturnObject.Dispose();
  1082. }
  1083. }
  1084. }
  1085. bool isMainThread()
  1086. {
  1087. return m_MainThread.Equals(System.Threading.Thread.CurrentThread);
  1088. }
  1089. public override int GetAudioChannelCount()
  1090. {
  1091. return Native._GetCurrentAudioTrackNumChannels(m_iPlayerIndex);
  1092. }
  1093. /*
  1094. public override AudioChannelMaskFlags GetAudioChannelMask()
  1095. {
  1096. return (AudioChannelMaskFlags)Native.GetAudioChannelMask(_instance);
  1097. }
  1098. */
  1099. public override int GrabAudio(float[] buffer, int sampleCount, int channelCount)
  1100. {
  1101. int iReturn = 0;
  1102. // Get audio data
  1103. iReturn = Native._GrabAudioNative( buffer, m_iPlayerIndex, sampleCount, channelCount );
  1104. return iReturn;
  1105. }
  1106. public override int GetAudioBufferedSampleCount()
  1107. {
  1108. int iBufferedSampleCount = 0;
  1109. if (m_Video != null)
  1110. {
  1111. // Get audio data
  1112. iBufferedSampleCount = m_Video.Call<int>("GetAudioBufferedSampleCount");
  1113. }
  1114. return iBufferedSampleCount;
  1115. }
  1116. internal override bool InternalIsChangedTextCue()
  1117. {
  1118. // Has the text cue changed since the last frame 'tick'
  1119. if( m_Video != null )
  1120. {
  1121. return m_Video.Call<bool>("GetTextCueDirty");
  1122. }
  1123. return false;
  1124. }
  1125. internal override string InternalGetCurrentTextCue()
  1126. {
  1127. // Return a pointer to the current text cue string in UTF16 format
  1128. if ( m_Video != null )
  1129. {
  1130. return m_Video.Call<string>("GetCurrentTextCue");
  1131. }
  1132. return string.Empty;
  1133. }
  1134. internal override bool InternalIsChangedTracks(TrackType trackType)
  1135. {
  1136. // Has it changed since the last frame 'tick'
  1137. bool result = false;
  1138. switch (trackType)
  1139. {
  1140. case TrackType.Video:
  1141. {
  1142. result = ( m_Video != null ) ? m_Video.Call<bool>("GetVideoTracksDirty") : false;
  1143. break;
  1144. }
  1145. case TrackType.Audio:
  1146. {
  1147. result = ( m_Video != null ) ? m_Video.Call<bool>("GetAudioTracksDirty") : false;
  1148. break;
  1149. }
  1150. case TrackType.Text:
  1151. {
  1152. result = ( m_Video != null ) ? m_Video.Call<bool>("GetTextTracksDirty") : false;
  1153. break;
  1154. }
  1155. }
  1156. return result;
  1157. }
  1158. internal override int InternalGetTrackCount(TrackType trackType)
  1159. {
  1160. int result = 0;
  1161. switch (trackType)
  1162. {
  1163. case TrackType.Video:
  1164. {
  1165. result = ( m_Video != null ) ? m_Video.Call<int>("GetNumberVideoTracks") : 0;
  1166. break;
  1167. }
  1168. case TrackType.Audio:
  1169. {
  1170. result = ( m_Video != null ) ? m_Video.Call<int>("GetNumberAudioTracks") : 0;
  1171. break;
  1172. }
  1173. case TrackType.Text:
  1174. {
  1175. result = ( m_Video != null ) ? m_Video.Call<int>("GetNumberTextTracks") : 0;
  1176. break;
  1177. }
  1178. }
  1179. return result;
  1180. }
  1181. internal override bool InternalSetActiveTrack(TrackType trackType, int trackUid)
  1182. {
  1183. bool result = false;
  1184. switch (trackType)
  1185. {
  1186. case TrackType.Video:
  1187. {
  1188. result = ( m_Video != null ) ? m_Video.Call<bool>("SetVideoTrack", trackUid) : false;
  1189. break;
  1190. }
  1191. case TrackType.Audio:
  1192. {
  1193. result = ( m_Video != null ) ? m_Video.Call<bool>("SetAudioTrack", trackUid) : false;
  1194. break;
  1195. }
  1196. case TrackType.Text:
  1197. {
  1198. result = ( m_Video != null ) ? m_Video.Call<bool>("SetTextTrack", trackUid) : false;
  1199. break;
  1200. }
  1201. }
  1202. return result;
  1203. }
  1204. internal override TrackBase InternalGetTrackInfo(TrackType trackType, int trackIndex, ref bool isActiveTrack)
  1205. {
  1206. TrackBase result = null;
  1207. switch (trackType)
  1208. {
  1209. case TrackType.Video:
  1210. {
  1211. if (m_Video != null)
  1212. {
  1213. AndroidJavaObject returnObject = m_Video.Call<AndroidJavaObject>("GetVideoTrackInfo", trackIndex);
  1214. if (returnObject.GetRawObject().ToInt32() != 0)
  1215. {
  1216. String[] aReturn = AndroidJNIHelper.ConvertFromJNIArray<String[]>(returnObject.GetRawObject());
  1217. bool bReturn = (aReturn.Length > 0) ? (int.Parse(aReturn[0]) == 1) : false;
  1218. if (bReturn)
  1219. {
  1220. VideoTrack videoTrack = new VideoTrack(trackIndex, aReturn[1], aReturn[2], (aReturn[3] == "1"));
  1221. int bitrate = 0;
  1222. bool gotBitrate = Int32.TryParse(aReturn[4], out bitrate);
  1223. if( gotBitrate )
  1224. {
  1225. videoTrack.Bitrate = bitrate;
  1226. }
  1227. result = videoTrack;
  1228. isActiveTrack = (m_Video != null && m_Video.Call<int>("GetCurrentVideoTrackIndex") == trackIndex);
  1229. }
  1230. returnObject.Dispose();
  1231. }
  1232. }
  1233. }
  1234. break;
  1235. case TrackType.Audio:
  1236. {
  1237. if (m_Video != null)
  1238. {
  1239. AndroidJavaObject returnObject = m_Video.Call<AndroidJavaObject>("GetAudioTrackInfo", trackIndex);
  1240. if (returnObject.GetRawObject().ToInt32() != 0)
  1241. {
  1242. String[] aReturn = AndroidJNIHelper.ConvertFromJNIArray<String[]>( returnObject.GetRawObject() );
  1243. bool bReturn = ( aReturn.Length > 0 ) ? ( int.Parse( aReturn[ 0 ]) == 1 ) : false;
  1244. if (bReturn)
  1245. {
  1246. int iBitrate = 0;
  1247. int.TryParse( aReturn[ 4 ], out iBitrate );
  1248. int iChannelCount = 0;
  1249. int.TryParse(aReturn[ 5 ], out iChannelCount);
  1250. result = new AudioTrack( trackIndex, aReturn[ 1 ], aReturn[ 2 ], (aReturn[ 3 ] == "1") );
  1251. isActiveTrack = (m_Video != null && m_Video.Call<int>("GetCurrentAudioTrackIndex") == trackIndex);
  1252. }
  1253. returnObject.Dispose();
  1254. }
  1255. }
  1256. }
  1257. break;
  1258. case TrackType.Text:
  1259. {
  1260. if (m_Video != null)
  1261. {
  1262. AndroidJavaObject returnObject = m_Video.Call<AndroidJavaObject>( "GetTextTrackInfo", trackIndex );
  1263. if (returnObject.GetRawObject().ToInt32() != 0)
  1264. {
  1265. String[] aReturn = AndroidJNIHelper.ConvertFromJNIArray<String[]>( returnObject.GetRawObject() );
  1266. bool bReturn = ( aReturn.Length > 0 ) ? ( int.Parse(aReturn[ 0 ] ) == 1 ) : false;
  1267. int uid = -1;
  1268. if( bReturn )
  1269. {
  1270. int.TryParse(aReturn[1], out uid);
  1271. result = new TextTrack(uid, aReturn[ 2 ], aReturn[ 3 ], false);
  1272. isActiveTrack = (m_Video != null && m_Video.Call<int>("GetCurrentTextTrackIndex") == trackIndex);
  1273. }
  1274. returnObject.Dispose();
  1275. }
  1276. }
  1277. }
  1278. break;
  1279. }
  1280. return result;
  1281. }
  1282. internal /*override*/ int InternalGetAdaptiveStreamCount(TrackType trackType, int trackIndex = -1)
  1283. {
  1284. int result = 0;
  1285. switch (trackType)
  1286. {
  1287. case TrackType.Video:
  1288. {
  1289. result = (m_Video != null) ? m_Video.Call<int>("GetNumberVideoAdaptiveStreams", trackIndex) : 0;
  1290. Debug.Log("[AVProVideo]: InternalGetAdaptiveStreamCount return = " + result);
  1291. break;
  1292. }
  1293. case TrackType.Audio:
  1294. {
  1295. break;
  1296. }
  1297. case TrackType.Text:
  1298. {
  1299. break;
  1300. }
  1301. }
  1302. return result;
  1303. }
  1304. internal /*override*/ void InternalGetAdaptiveStreamInfo(TrackType trackType, int trackIndex = -1)
  1305. {
  1306. switch( trackType )
  1307. {
  1308. case TrackType.Video:
  1309. {
  1310. if( m_Video != null )
  1311. {
  1312. AndroidJavaObject returnObject = m_Video.Call<AndroidJavaObject>("GetVideoAdaptiveStreamInfo", trackIndex);
  1313. if( returnObject.GetRawObject().ToInt32() != 0 )
  1314. {
  1315. String[] aReturn = AndroidJNIHelper.ConvertFromJNIArray<String[]>(returnObject.GetRawObject());
  1316. bool bReturn = (aReturn.Length > 0) ? (int.Parse(aReturn[0]) == 1) : false;
  1317. string toLog = "";
  1318. foreach( string str in aReturn ) { toLog += str + ", "; }
  1319. Debug.Log( "[AVProVideo]: InternalGetAdaptiveStreamInfo return = " + toLog );
  1320. if ( bReturn )
  1321. {
  1322. }
  1323. returnObject.Dispose();
  1324. }
  1325. }
  1326. }
  1327. break;
  1328. case TrackType.Audio:
  1329. {
  1330. }
  1331. break;
  1332. case TrackType.Text:
  1333. {
  1334. }
  1335. break;
  1336. }
  1337. }
  1338. internal /*override*/ int SetVideoAdaptiveStreamIndex(TrackType trackType, int streamIndex)
  1339. {
  1340. int result = 0;
  1341. switch( trackType )
  1342. {
  1343. case TrackType.Video:
  1344. {
  1345. Debug.Log("[AVProVideo]: SetVideoAdaptiveStreamIndex : streamIndex = " + streamIndex);
  1346. result = (m_Video != null) ? m_Video.Call<int>("SetVideoAdaptiveStreams", streamIndex) : 0;
  1347. break;
  1348. }
  1349. case TrackType.Audio:
  1350. {
  1351. break;
  1352. }
  1353. case TrackType.Text:
  1354. {
  1355. break;
  1356. }
  1357. }
  1358. return result;
  1359. }
  1360. private Vector2 GetPreferredVideoResolution(MediaPlayer.OptionsAndroid.Resolution preferredMaximumResolution, Vector2 customPreferredMaximumResolution)
  1361. {
  1362. Vector2 vResolution = new Vector2( 0.0f, 0.0f );
  1363. switch( preferredMaximumResolution )
  1364. {
  1365. case MediaPlayer.OptionsAndroid.Resolution.NoPreference:
  1366. break;
  1367. case MediaPlayer.OptionsAndroid.Resolution._480p:
  1368. vResolution.x = 640;
  1369. vResolution.y = 480;
  1370. break;
  1371. case MediaPlayer.OptionsAndroid.Resolution._720p:
  1372. vResolution.x = 1280;
  1373. vResolution.y = 720;
  1374. break;
  1375. case MediaPlayer.OptionsAndroid.Resolution._1080p:
  1376. vResolution.x = 1920;
  1377. vResolution.y = 1080;
  1378. break;
  1379. case MediaPlayer.OptionsAndroid.Resolution._2160p:
  1380. vResolution.x = 3840;
  1381. vResolution.y = 2160;
  1382. break;
  1383. case MediaPlayer.OptionsAndroid.Resolution.Custom:
  1384. #if UNITY_2017_2_OR_NEWER
  1385. vResolution.x = customPreferredMaximumResolution.x;
  1386. vResolution.y = customPreferredMaximumResolution.y;
  1387. #endif
  1388. break;
  1389. }
  1390. return vResolution;
  1391. }
  1392. public override bool IsMediaCachingSupported()
  1393. {
  1394. if( m_Video != null )
  1395. {
  1396. return m_Video.Call<bool>("IsMediaCachingSupported");
  1397. }
  1398. return true;
  1399. }
  1400. public override void AddMediaToCache(string url, string headers, MediaCachingOptions options)
  1401. {
  1402. if (m_Video != null)
  1403. {
  1404. double dMinBitrate = -1.0f;
  1405. int iMinWidth = -1;
  1406. int iMinHeight = -1;
  1407. double dMaxBitrate = -1.0f;
  1408. int iMaxWidth = -1;
  1409. int iMaxHeight = -1;
  1410. if (options != null )
  1411. {
  1412. dMinBitrate = options.minimumRequiredBitRate;
  1413. iMinWidth = (int)( options.minimumRequiredResolution.x );
  1414. iMinHeight = (int)( options.minimumRequiredResolution.y );
  1415. dMaxBitrate = options.maximumRequiredBitRate;
  1416. iMaxWidth = (int)(options.maximumRequiredResolution.x);
  1417. iMaxHeight = (int)(options.maximumRequiredResolution.y);
  1418. }
  1419. m_Video.Call("AddMediaToCache", url, headers, dMinBitrate, iMinWidth, iMinHeight, dMaxBitrate, iMaxWidth, iMaxHeight);
  1420. }
  1421. }
  1422. public override void RemoveMediaFromCache(string url)
  1423. {
  1424. if(m_Video != null)
  1425. {
  1426. m_Video.Call("RemoveMediaFromCache", url);
  1427. }
  1428. }
  1429. public override void CancelDownloadOfMediaToCache(string url)
  1430. {
  1431. if (m_Video != null)
  1432. {
  1433. m_Video.Call("CancelDownloadOfMediaToCache", url);
  1434. }
  1435. }
  1436. public override void PauseDownloadOfMediaToCache(string url)
  1437. {
  1438. if (m_Video != null)
  1439. {
  1440. m_Video.Call("PauseDownloadOfMediaToCache", url);
  1441. }
  1442. }
  1443. public override void ResumeDownloadOfMediaToCache(string url)
  1444. {
  1445. if (m_Video != null)
  1446. {
  1447. m_Video.Call("ResumeDownloadOfMediaToCache", url);
  1448. }
  1449. }
  1450. public override CachedMediaStatus GetCachedMediaStatus(string url, ref float progress)
  1451. {
  1452. CachedMediaStatus eStatus = CachedMediaStatus.NotCached;
  1453. if (m_Video != null)
  1454. {
  1455. float[] afReturn = m_Video.Call<float[]>("GetCachedMediaStatus", url);
  1456. eStatus = (CachedMediaStatus)( afReturn[ 0 ] );
  1457. progress = afReturn[ 1 ] * 0.01f;
  1458. // if( eStatus != CachedMediaStatus.NotCached && progress < 1.0f )
  1459. // {
  1460. // Debug.Log("AVProVideo: GetCachedMediaStatus | url = " + url + " | eStatus = " + eStatus + " | progress = " + progress);
  1461. // }
  1462. }
  1463. return eStatus;
  1464. }
  1465. private struct Native
  1466. {
  1467. internal const string LibraryName = "AVProVideo2Native";
  1468. [DllImport (LibraryName)]
  1469. public static extern IntPtr GetRenderEventFunc();
  1470. [DllImport(LibraryName)]
  1471. public static extern bool JNIAttachCurrentThread();
  1472. [DllImport(LibraryName)]
  1473. public static extern void JNIDetachCurrentThread();
  1474. [DllImport(LibraryName)]
  1475. public static extern int _GetCurrentAudioTrackNumChannels( int iPlayerIndex );
  1476. [DllImport(LibraryName)]
  1477. // unsafe public static extern int _GrabAudioNative(float* pBuffer, int iPlayerIndex, int sampleCount, int channelCount);
  1478. public static extern int _GrabAudioNative(float[] pBuffer, int iPlayerIndex, int sampleCount, int channelCount);
  1479. [DllImport (LibraryName)]
  1480. public static extern int _GetWidth( int iPlayerIndex );
  1481. [DllImport (LibraryName)]
  1482. public static extern int _GetHeight( int iPlayerIndex );
  1483. [DllImport (LibraryName)]
  1484. public static extern int _GetTextureHandle( int iPlayerIndex );
  1485. [DllImport (LibraryName)]
  1486. public static extern double _GetDuration( int iPlayerIndex );
  1487. [DllImport (LibraryName)]
  1488. public static extern int _GetLastErrorCode( int iPlayerIndex );
  1489. [DllImport (LibraryName)]
  1490. public static extern int _GetFrameCount( int iPlayerIndex );
  1491. [DllImport (LibraryName)]
  1492. public static extern float _GetVideoDisplayRate( int iPlayerIndex );
  1493. [DllImport (LibraryName)]
  1494. public static extern bool _CanPlay( int iPlayerIndex );
  1495. public enum AVPPluginEvent
  1496. {
  1497. Nop,
  1498. PlayerSetup,
  1499. PlayerUpdate,
  1500. PlayerDestroy,
  1501. ExtractFrame,
  1502. }
  1503. }
  1504. }
  1505. }
  1506. #endif