WebGLMediaPlayer.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. //#define AVPRO_WEBGL_USE_RENDERTEXTURE
  2. // NOTE: We only allow this script to compile in editor so we can easily check for compilation issues
  3. #if (UNITY_EDITOR || UNITY_WEBGL)
  4. using UnityEngine;
  5. using System;
  6. using System.Text;
  7. using System.Runtime.InteropServices;
  8. //-----------------------------------------------------------------------------
  9. // Copyright 2015-2021 RenderHeads Ltd. All rights reserved.
  10. //-----------------------------------------------------------------------------
  11. namespace RenderHeads.Media.AVProVideo
  12. {
  13. /// <summary>
  14. /// WebGL implementation of BaseMediaPlayer
  15. /// </summary>
  16. public sealed class WebGLMediaPlayer : BaseMediaPlayer
  17. {
  18. //private enum AVPPlayerStatus
  19. //{
  20. // Unknown,
  21. // ReadyToPlay,
  22. // Playing,
  23. // Finished,
  24. // Seeking,
  25. // Failed
  26. //}
  27. [DllImport("__Internal")]
  28. private static extern bool AVPPlayerInsertVideoElement(string path, int[] idValues, int externalLibrary);
  29. [DllImport("__Internal")]
  30. private static extern int AVPPlayerWidth(int player);
  31. [DllImport("__Internal")]
  32. private static extern int AVPPlayerHeight(int player);
  33. [DllImport("__Internal")]
  34. private static extern int AVPPlayerGetLastError(int player);
  35. [DllImport("__Internal")]
  36. private static extern int AVPPlayerGetVideoTrackCount(int player);
  37. [DllImport("__Internal")]
  38. private static extern int AVPPlayerGetAudioTrackCount(int player);
  39. [DllImport("__Internal")]
  40. private static extern int AVPPlayerGetTextTrackCount(int player);
  41. [DllImport("__Internal")]
  42. private static extern bool AVPPlayerSetActiveVideoTrack(int player, int trackId);
  43. [DllImport("__Internal")]
  44. private static extern bool AVPPlayerSetActiveAudioTrack(int player, int trackId);
  45. [DllImport("__Internal")]
  46. private static extern bool AVPPlayerSetActiveTextTrack(int player, int trackId);
  47. [DllImport("__Internal")]
  48. private static extern void AVPPlayerClose(int player);
  49. [DllImport("__Internal")]
  50. private static extern bool AVPPlayerReady(int player);
  51. [DllImport("__Internal")]
  52. private static extern void AVPPlayerSetLooping(int player, bool loop);
  53. [DllImport("__Internal")]
  54. private static extern bool AVPPlayerIsLooping(int player);
  55. [DllImport("__Internal")]
  56. private static extern bool AVPPlayerIsSeeking(int player);
  57. [DllImport("__Internal")]
  58. private static extern bool AVPPlayerIsPlaying(int player);
  59. [DllImport("__Internal")]
  60. private static extern bool AVPPlayerIsPaused(int player);
  61. [DllImport("__Internal")]
  62. private static extern bool AVPPlayerIsFinished(int player);
  63. [DllImport("__Internal")]
  64. private static extern bool AVPPlayerIsBuffering(int player);
  65. [DllImport("__Internal")]
  66. private static extern bool AVPPlayerIsPlaybackStalled(int player);
  67. [DllImport("__Internal")]
  68. private static extern bool AVPPlayerPlay(int player);
  69. [DllImport("__Internal")]
  70. private static extern void AVPPlayerPause(int player);
  71. [DllImport("__Internal")]
  72. private static extern void AVPPlayerSeekToTime(int player, double time, bool fast);
  73. [DllImport("__Internal")]
  74. private static extern double AVPPlayerGetCurrentTime(int player);
  75. [DllImport("__Internal")]
  76. private static extern float AVPPlayerGetDuration(int player);
  77. [DllImport("__Internal")]
  78. private static extern float AVPPlayerGetPlaybackRate(int player);
  79. [DllImport("__Internal")]
  80. private static extern void AVPPlayerSetPlaybackRate(int player, float rate);
  81. [DllImport("__Internal")]
  82. private static extern void AVPPlayerSetMuted(int player, bool muted);
  83. [DllImport("__Internal")]
  84. private static extern bool AVPPlayerIsMuted(int player);
  85. [DllImport("__Internal")]
  86. private static extern float AVPPlayerGetVolume(int player);
  87. [DllImport("__Internal")]
  88. private static extern void AVPPlayerSetVolume(int player, float volume);
  89. [DllImport("__Internal")]
  90. private static extern bool AVPPlayerHasVideo(int player);
  91. [DllImport("__Internal")]
  92. private static extern bool AVPPlayerHasAudio(int player);
  93. [DllImport("__Internal")]
  94. private static extern void AVPPlayerCreateVideoTexture(int textureId);
  95. [DllImport("__Internal")]
  96. private static extern void AVPPlayerDestroyVideoTexture(int textureId);
  97. [DllImport("__Internal")]
  98. private static extern void AVPPlayerFetchVideoTexture(int player, IntPtr texture, bool init);
  99. [DllImport("__Internal")]
  100. private static extern int AVPPlayerGetDecodedFrameCount(int player);
  101. [DllImport("__Internal")]
  102. private static extern bool AVPPlayerSupportedDecodedFrameCount(int player);
  103. [DllImport("__Internal")]
  104. private static extern bool AVPPlayerHasMetadata(int player);
  105. [DllImport("__Internal")]
  106. private static extern int AVPPlayerUpdatePlayerIndex(int id);
  107. [DllImport("__Internal")]
  108. private static extern int AVPPlayerGetNumBufferedTimeRanges(int id);
  109. [DllImport("__Internal")]
  110. private static extern double AVPPlayerGetTimeRangeStart(int id, int timeRangeIndex);
  111. [DllImport("__Internal")]
  112. private static extern double AVPPlayerGetTimeRangeEnd(int id, int timeRangeIndex);
  113. [DllImport("__Internal")]
  114. private static extern string AVPPlayerGetVideoTrackName(int player, int trackIndex);
  115. [DllImport("__Internal")]
  116. private static extern string AVPPlayerGetAudioTrackName(int player, int trackIndex);
  117. [DllImport("__Internal")]
  118. private static extern string AVPPlayerGetTextTrackName(int player, int trackIndex);
  119. [DllImport("__Internal")]
  120. private static extern string AVPPlayerGetVideoTrackLanguage(int player, int trackIndex);
  121. [DllImport("__Internal")]
  122. private static extern string AVPPlayerGetAudioTrackLanguage(int player, int trackIndex);
  123. [DllImport("__Internal")]
  124. private static extern string AVPPlayerGetTextTrackLanguage(int player, int trackIndex);
  125. [DllImport("__Internal")]
  126. private static extern bool AVPPlayerIsVideoTrackActive(int player, int trackIndex);
  127. [DllImport("__Internal")]
  128. private static extern bool AVPPlayerIsAudioTrackActive(int player, int trackIndex);
  129. [DllImport("__Internal")]
  130. private static extern bool AVPPlayerIsTextTrackActive(int player, int trackIndex);
  131. private WebGL.ExternalLibrary _externalLibrary = WebGL.ExternalLibrary.None;
  132. private int _playerIndex = -1;
  133. private int _playerID = -1;
  134. #if AVPRO_WEBGL_USE_RENDERTEXTURE
  135. private RenderTexture _texture = null;
  136. #else
  137. private Texture2D _texture = null;
  138. #endif
  139. private int _width = 0;
  140. private int _height = 0;
  141. private int _cachedVideoTrackCount = 0;
  142. private int _cachedAudioTrackCount = 0;
  143. private int _cachedTextTrackCount = 0;
  144. private bool _isDirtyVideoTracks = false;
  145. private bool _isDirtyAudioTracks = false;
  146. private bool _isDirtyTextTracks = false;
  147. private bool _useTextureMips = false;
  148. private System.IntPtr _cachedTextureNativePtr = System.IntPtr.Zero;
  149. private static bool _isWebGL1 = false;
  150. public static bool InitialisePlatform()
  151. {
  152. _isWebGL1 = (SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.OpenGLES2);
  153. return true;
  154. }
  155. public WebGLMediaPlayer(MediaPlayer.OptionsWebGL options)
  156. {
  157. SetOptions(options);
  158. }
  159. public void SetOptions(MediaPlayer.OptionsWebGL options)
  160. {
  161. _externalLibrary = options.externalLibrary;
  162. _useTextureMips = options.useTextureMips;
  163. }
  164. public override string GetVersion()
  165. {
  166. return "2.1.6";
  167. }
  168. public override string GetExpectedVersion()
  169. {
  170. return GetVersion();
  171. }
  172. public override bool OpenMedia(string path, long offset, string httpHeader, MediaHints mediaHints, int forceFileFormat = 0, bool startWithHighestBitrate = false)
  173. {
  174. bool result = false;
  175. if (path.StartsWith("http://") ||
  176. path.StartsWith("https://") ||
  177. path.StartsWith("file://") ||
  178. path.StartsWith("blob:") ||
  179. path.StartsWith("chrome-extension://"))
  180. {
  181. int[] idValues = new int[2];
  182. idValues[0] = -1;
  183. AVPPlayerInsertVideoElement(path, idValues, (int)_externalLibrary);
  184. {
  185. int playerIndex = idValues[0];
  186. _playerID = idValues[1];
  187. if (playerIndex > -1)
  188. {
  189. _playerIndex = playerIndex;
  190. _mediaHints = mediaHints;
  191. result = true;
  192. }
  193. }
  194. }
  195. else
  196. {
  197. Debug.LogError("[AVProVideo] Unknown URL protocol");
  198. }
  199. return result;
  200. }
  201. public override void CloseMedia()
  202. {
  203. if (_playerIndex != -1)
  204. {
  205. Pause();
  206. _width = 0;
  207. _height = 0;
  208. _cachedVideoTrackCount = 0;
  209. _cachedAudioTrackCount = 0;
  210. _cachedTextTrackCount = 0;
  211. _isDirtyVideoTracks = false;
  212. _isDirtyAudioTracks = false;
  213. _isDirtyTextTracks = false;
  214. AVPPlayerClose(_playerIndex);
  215. if (_texture != null)
  216. {
  217. DestroyTexture();
  218. }
  219. _playerIndex = -1;
  220. _playerID = -1;
  221. base.CloseMedia();
  222. }
  223. }
  224. public override bool IsLooping()
  225. {
  226. //Debug.Assert(_player != -1, "no player IsLooping");
  227. bool result = false;
  228. if (_playerIndex != -1)
  229. {
  230. result = AVPPlayerIsLooping(_playerIndex);
  231. }
  232. return result;
  233. }
  234. public override void SetLooping(bool looping)
  235. {
  236. //Debug.Assert(_playerIndex != -1, "no player SetLooping");
  237. AVPPlayerSetLooping(_playerIndex, looping);
  238. }
  239. public override bool HasAudio()
  240. {
  241. //Debug.Assert(_player != -1, "no player HasAudio");
  242. bool result = false;
  243. if (_playerIndex != -1)
  244. {
  245. result = AVPPlayerHasAudio(_playerIndex);
  246. }
  247. return result;
  248. }
  249. public override bool HasVideo()
  250. {
  251. //Debug.Assert(_player != -1, "no player HasVideo");
  252. bool result = false;
  253. if (_playerIndex != -1)
  254. {
  255. result = AVPPlayerHasVideo(_playerIndex);
  256. }
  257. return result;
  258. }
  259. public override bool HasMetaData()
  260. {
  261. //Debug.Assert(_player != -1, "no player HasMetaData");
  262. bool result = false;
  263. if (_playerIndex != -1)
  264. {
  265. result = AVPPlayerHasMetadata(_playerIndex);
  266. }
  267. return result;
  268. }
  269. public override bool CanPlay()
  270. {
  271. //Debug.Assert(_player != -1, "no player CanPlay");
  272. bool result = false;
  273. if (_playerIndex != -1)
  274. {
  275. result = AVPPlayerReady(_playerIndex);
  276. }
  277. return result;
  278. }
  279. public override void Play()
  280. {
  281. Debug.Assert(_playerIndex != -1, "no player Play");
  282. if (!AVPPlayerPlay(_playerIndex))
  283. {
  284. Debug.LogWarning("[AVProVideo] Browser permission prevented video playback");
  285. }
  286. }
  287. public override void Pause()
  288. {
  289. Debug.Assert(_playerIndex != -1, "no player Pause");
  290. AVPPlayerPause(_playerIndex);
  291. }
  292. public override void Stop()
  293. {
  294. Debug.Assert(_playerIndex != -1, "no player Stop");
  295. AVPPlayerPause(_playerIndex);
  296. }
  297. public override void Seek(double time)
  298. {
  299. Debug.Assert(_playerIndex != -1, "no player Seek");
  300. AVPPlayerSeekToTime(_playerIndex, time, false);
  301. }
  302. public override void SeekFast(double time)
  303. {
  304. Debug.Assert(_playerIndex != -1, "no player SeekFast");
  305. AVPPlayerSeekToTime(_playerIndex, time, true);
  306. }
  307. public override double GetCurrentTime()
  308. {
  309. //Debug.Assert(_player != -1, "no player GetCurrentTime");
  310. double result = 0.0;
  311. if (_playerIndex != -1)
  312. {
  313. result = AVPPlayerGetCurrentTime(_playerIndex);
  314. }
  315. return result;
  316. }
  317. public override void SetPlaybackRate(float rate)
  318. {
  319. Debug.Assert(_playerIndex != -1, "no player SetPlaybackRate");
  320. // No HTML implementations allow negative rate yet
  321. rate = Mathf.Clamp(rate, 0.25f, 8f);
  322. AVPPlayerSetPlaybackRate(_playerIndex, rate);
  323. }
  324. public override float GetPlaybackRate()
  325. {
  326. //Debug.Assert(_player != -1, "no player GetPlaybackRate");
  327. float result = 0.0f;
  328. if (_playerIndex != -1)
  329. {
  330. result = AVPPlayerGetPlaybackRate(_playerIndex);
  331. }
  332. return result;
  333. }
  334. public override double GetDuration()
  335. {
  336. //Debug.Assert(_player != -1, "no player GetDuration");
  337. double result = 0.0;
  338. if (_playerIndex != -1)
  339. {
  340. result = AVPPlayerGetDuration(_playerIndex);
  341. }
  342. return result;
  343. }
  344. public override int GetVideoWidth()
  345. {
  346. if (_width == 0)
  347. {
  348. _width = AVPPlayerWidth(_playerIndex);
  349. }
  350. return _width;
  351. }
  352. public override int GetVideoHeight()
  353. {
  354. if (_height == 0)
  355. {
  356. _height = AVPPlayerHeight(_playerIndex);
  357. }
  358. return _height;
  359. }
  360. public override float GetVideoFrameRate()
  361. {
  362. // There is no way in HTML5 yet to get the frame rate of the video
  363. return 0f;
  364. }
  365. public override bool IsSeeking()
  366. {
  367. //Debug.Assert(_player != -1, "no player IsSeeking");
  368. bool result = false;
  369. if (_playerIndex != -1)
  370. {
  371. result = AVPPlayerIsSeeking(_playerIndex);
  372. }
  373. return result;
  374. }
  375. public override bool IsPlaying()
  376. {
  377. //Debug.Assert(_player != -1, "no player IsPlaying");
  378. bool result = false;
  379. if (_playerIndex != -1)
  380. {
  381. result = AVPPlayerIsPlaying(_playerIndex);
  382. }
  383. return result;
  384. }
  385. public override bool IsPaused()
  386. {
  387. //Debug.Assert(_player != -1, "no player IsPaused");
  388. bool result = false;
  389. if (_playerIndex != -1)
  390. {
  391. result = AVPPlayerIsPaused(_playerIndex);
  392. }
  393. return result;
  394. }
  395. public override bool IsFinished()
  396. {
  397. //Debug.Assert(_player != -1, "no player IsFinished");
  398. bool result = false;
  399. if (_playerIndex != -1)
  400. {
  401. result = AVPPlayerIsFinished(_playerIndex);
  402. }
  403. return result;
  404. }
  405. public override bool IsBuffering()
  406. {
  407. //Debug.Assert(_player != -1, "no player IsBuffering");
  408. bool result = false;
  409. if (_playerIndex != -1)
  410. {
  411. result = AVPPlayerIsBuffering(_playerIndex);
  412. }
  413. return result;
  414. }
  415. public override Texture GetTexture( int index )
  416. {
  417. return _texture;
  418. }
  419. public override int GetTextureFrameCount()
  420. {
  421. //Debug.Assert(_player != -1, "no player GetTextureFrameCount");
  422. int result = 0;
  423. if (_playerIndex != -1)
  424. {
  425. result = AVPPlayerGetDecodedFrameCount(_playerIndex);
  426. }
  427. return result;
  428. }
  429. internal override StereoPacking InternalGetTextureStereoPacking()
  430. {
  431. return StereoPacking.Unknown;
  432. }
  433. public override bool SupportsTextureFrameCount()
  434. {
  435. bool result = false;
  436. if (_playerIndex != -1)
  437. {
  438. result = AVPPlayerSupportedDecodedFrameCount(_playerIndex);
  439. }
  440. return result;
  441. }
  442. public override bool RequiresVerticalFlip()
  443. {
  444. return true;
  445. }
  446. public override bool IsMuted()
  447. {
  448. //Debug.Assert(_player != -1, "no player IsMuted");
  449. bool result = false;
  450. if (_playerIndex != -1)
  451. {
  452. result = AVPPlayerIsMuted(_playerIndex);
  453. }
  454. return result;
  455. }
  456. public override void MuteAudio(bool bMute)
  457. {
  458. Debug.Assert(_playerIndex != -1, "no player MuteAudio");
  459. AVPPlayerSetMuted(_playerIndex, bMute);
  460. }
  461. public override void SetVolume(float volume)
  462. {
  463. Debug.Assert(_playerIndex != -1, "no player SetVolume");
  464. AVPPlayerSetVolume(_playerIndex, volume);
  465. }
  466. public override float GetVolume()
  467. {
  468. //Debug.Assert(_player != -1, "no player GetVolume");
  469. float result = 0.0f;
  470. if (_playerIndex != -1)
  471. {
  472. result = AVPPlayerGetVolume(_playerIndex);
  473. }
  474. return result;
  475. }
  476. public override void Render()
  477. {
  478. }
  479. private void UpdateLastErrorCode()
  480. {
  481. var code = AVPPlayerGetLastError(_playerIndex);
  482. switch(code){
  483. case 0:
  484. _lastError = ErrorCode.None;
  485. break;
  486. case 1:
  487. _lastError = ErrorCode.LoadFailed;
  488. break;
  489. case 2:
  490. _lastError = ErrorCode.LoadFailed;
  491. break;
  492. case 3:
  493. _lastError = ErrorCode.DecodeFailed;
  494. break;
  495. case 4:
  496. _lastError = ErrorCode.LoadFailed;
  497. break;
  498. default:
  499. break;
  500. }
  501. }
  502. private bool IsMipMapGenerationSupported(int videoWidth, int videoHeight)
  503. {
  504. if (!_isWebGL1 || (Mathf.IsPowerOfTwo(videoWidth) && Mathf.IsPowerOfTwo(videoHeight)))
  505. {
  506. // Mip generation only supported in WebGL 2.0, or WebGL 1.0 when using power-of-two textures
  507. return true;
  508. }
  509. return false;
  510. }
  511. private void CreateTexture()
  512. {
  513. //Debug.Log("creating texture " + _width + " X " + _height);
  514. #if AVPRO_WEBGL_USE_RENDERTEXTURE
  515. _texture = new RenderTexture(_width, _height, 0, RenderTextureFormat.Default);
  516. _texture.autoGenerateMips = false;
  517. _texture.useMipMap = (_useTextureMips && IsMipMapGenerationSupported(_width, _height));
  518. _texture.Create();
  519. _cachedTextureNativePtr = _texture.GetNativeTexturePtr();
  520. #else
  521. int textureId = 80000 + _playerIndex;
  522. _cachedTextureNativePtr = new System.IntPtr(textureId);
  523. AVPPlayerCreateVideoTexture(textureId);
  524. // TODO: add support for mip generation
  525. _texture = Texture2D.CreateExternalTexture(_width, _height, TextureFormat.RGBA32, false, false, _cachedTextureNativePtr);
  526. if (_useTextureMips)
  527. {
  528. Debug.LogWarning("[AVProVideo] Texture Mips not yet implemented in this WebGL rendering path");
  529. }
  530. //Debug.Log("created texture1 " + _texture);
  531. //Debug.Log("created texture2 " + _texture.GetNativeTexturePtr().ToInt32());
  532. #endif
  533. ApplyTextureProperties(_texture);
  534. bool initTexture = true;
  535. #if AVPRO_WEBGL_USE_RENDERTEXTURE
  536. // Textures in WebGL 2.0 don't require texImage2D as they are already recreated with texStorage2D
  537. initTexture = _isWebGL1;
  538. #endif
  539. AVPPlayerFetchVideoTexture(_playerIndex, _cachedTextureNativePtr, initTexture);
  540. }
  541. private void DestroyTexture()
  542. {
  543. // Have to update with zero to release Metal textures!
  544. //_texture.UpdateExternalTexture(0);
  545. if (_texture != null)
  546. {
  547. #if AVPRO_WEBGL_USE_RENDERTEXTURE
  548. RenderTexture.Destroy(_texture);
  549. #else
  550. Texture2D.Destroy(_texture);
  551. AVPPlayerDestroyVideoTexture(_cachedTextureNativePtr.ToInt32());
  552. #endif
  553. _texture = null;
  554. }
  555. _cachedTextureNativePtr = System.IntPtr.Zero;
  556. }
  557. public override void Update()
  558. {
  559. if(_playerID >= 0) // CheckPlayer's index and update it
  560. {
  561. _playerIndex = AVPPlayerUpdatePlayerIndex(_playerID);
  562. }
  563. if(_playerIndex >= 0)
  564. {
  565. CheckTracksDirty();
  566. UpdateTracks();
  567. UpdateTextCue();
  568. UpdateSubtitles();
  569. UpdateLastErrorCode();
  570. if (AVPPlayerReady(_playerIndex))
  571. {
  572. UpdateTimeRanges();
  573. if (AVPPlayerHasVideo(_playerIndex))
  574. {
  575. _width = AVPPlayerWidth(_playerIndex);
  576. _height = AVPPlayerHeight(_playerIndex);
  577. if (_texture != null && (_texture.width != _width || _texture.height != _height))
  578. {
  579. DestroyTexture();
  580. }
  581. if (_texture == null && _width > 0 && _height > 0)
  582. {
  583. CreateTexture();
  584. }
  585. // Update the texture
  586. if (_cachedTextureNativePtr != System.IntPtr.Zero)
  587. {
  588. // TODO: only update the texture when the frame count changes
  589. // (actually this will break the update for certain browsers such as edge and possibly safari - Sunrise)
  590. AVPPlayerFetchVideoTexture(_playerIndex, _cachedTextureNativePtr, false);
  591. #if AVPRO_WEBGL_USE_RENDERTEXTURE
  592. if (_texture.useMipMap)
  593. {
  594. _texture.GenerateMips();
  595. }
  596. #endif
  597. }
  598. UpdateDisplayFrameRate();
  599. }
  600. }
  601. }
  602. }
  603. private void CheckTracksDirty()
  604. {
  605. _isDirtyVideoTracks = false;
  606. _isDirtyAudioTracks = false;
  607. _isDirtyTextTracks = false;
  608. // TODO: replace this crude polling check with events, or only do it once metadataReady
  609. // Need to add event support as tracks can be added via HTML (especially text)
  610. int videoTrackCount = AVPPlayerGetVideoTrackCount(_playerIndex);
  611. int audioTrackCount = AVPPlayerGetAudioTrackCount(_playerIndex);
  612. int textTrackCount = AVPPlayerGetTextTrackCount(_playerIndex);
  613. _isDirtyVideoTracks = (_cachedVideoTrackCount != videoTrackCount);
  614. _isDirtyAudioTracks = (_cachedAudioTrackCount != audioTrackCount);
  615. _isDirtyTextTracks = ( _cachedTextTrackCount != textTrackCount);
  616. _cachedVideoTrackCount = videoTrackCount;
  617. _cachedAudioTrackCount = audioTrackCount;
  618. _cachedTextTrackCount = textTrackCount;
  619. }
  620. private void UpdateTimeRanges()
  621. {
  622. {
  623. int rangeCount = AVPPlayerGetNumBufferedTimeRanges(_playerIndex);
  624. if (rangeCount != _bufferedTimes.Count)
  625. {
  626. _bufferedTimes._ranges = new TimeRange[rangeCount];
  627. }
  628. for (int i = 0; i < rangeCount; i++)
  629. {
  630. double startTime = AVPPlayerGetTimeRangeStart(_playerIndex, i);
  631. double endTime = AVPPlayerGetTimeRangeEnd(_playerIndex, i);
  632. _bufferedTimes._ranges[i] = new TimeRange(startTime, endTime - startTime);
  633. }
  634. _bufferedTimes.CalculateRange();
  635. }
  636. {
  637. double duration = GetDuration();
  638. if (duration > 0.0)
  639. {
  640. _seekableTimes._ranges = new TimeRange[1];
  641. _seekableTimes._ranges[0] = new TimeRange(0.0, duration);
  642. }
  643. else
  644. {
  645. _seekableTimes._ranges = new TimeRange[0];
  646. }
  647. _seekableTimes.CalculateRange();
  648. }
  649. }
  650. public override void Dispose()
  651. {
  652. CloseMedia();
  653. }
  654. public override bool IsPlaybackStalled()
  655. {
  656. bool result = false;
  657. if (_playerIndex > -1)
  658. {
  659. result = AVPPlayerIsPlaybackStalled(_playerIndex) && IsPlaying();
  660. }
  661. return result;
  662. }
  663. // Tracks
  664. internal override int InternalGetTrackCount(TrackType trackType)
  665. {
  666. int result = 0;
  667. switch (trackType)
  668. {
  669. case TrackType.Video:
  670. result = AVPPlayerGetVideoTrackCount(_playerIndex);
  671. break;
  672. case TrackType.Audio:
  673. result = AVPPlayerGetAudioTrackCount(_playerIndex);
  674. break;
  675. case TrackType.Text:
  676. result = AVPPlayerGetTextTrackCount(_playerIndex);
  677. break;
  678. }
  679. return result;
  680. }
  681. internal override bool InternalIsChangedTracks(TrackType trackType)
  682. {
  683. bool result = false;
  684. switch (trackType)
  685. {
  686. case TrackType.Video:
  687. result = _isDirtyVideoTracks;
  688. break;
  689. case TrackType.Audio:
  690. result = _isDirtyAudioTracks;
  691. break;
  692. case TrackType.Text:
  693. result = _isDirtyTextTracks;
  694. break;
  695. }
  696. return result;
  697. }
  698. internal override bool InternalSetActiveTrack(TrackType trackType, int trackId)
  699. {
  700. bool result = false;
  701. switch (trackType)
  702. {
  703. case TrackType.Video:
  704. result = AVPPlayerSetActiveVideoTrack(_playerIndex, trackId);
  705. break;
  706. case TrackType.Audio:
  707. result = AVPPlayerSetActiveAudioTrack(_playerIndex, trackId);
  708. break;
  709. case TrackType.Text:
  710. result = AVPPlayerSetActiveTextTrack(_playerIndex, trackId);
  711. break;
  712. }
  713. return result;
  714. }
  715. internal override TrackBase InternalGetTrackInfo(TrackType trackType, int trackIndex, ref bool isActiveTrack)
  716. {
  717. TrackBase result = null;
  718. switch (trackType)
  719. {
  720. case TrackType.Video:
  721. {
  722. string trackName = AVPPlayerGetVideoTrackName(_playerIndex, trackIndex);
  723. string trackLanguage = AVPPlayerGetVideoTrackLanguage(_playerIndex, trackIndex);
  724. bool isActive = AVPPlayerIsVideoTrackActive(_playerIndex, trackIndex);
  725. result = new VideoTrack(trackIndex, trackName, trackLanguage, isActive);
  726. break;
  727. }
  728. case TrackType.Audio:
  729. {
  730. string trackName = AVPPlayerGetAudioTrackName(_playerIndex, trackIndex);
  731. string trackLanguage = AVPPlayerGetAudioTrackLanguage(_playerIndex, trackIndex);
  732. bool isActive = AVPPlayerIsAudioTrackActive(_playerIndex, trackIndex);
  733. result = new AudioTrack(trackIndex, trackName, trackLanguage, isActive);
  734. break;
  735. }
  736. case TrackType.Text:
  737. {
  738. string trackName = AVPPlayerGetTextTrackName(_playerIndex, trackIndex);
  739. string trackLanguage = AVPPlayerGetTextTrackLanguage(_playerIndex, trackIndex);
  740. bool isActive = AVPPlayerIsTextTrackActive(_playerIndex, trackIndex);
  741. result = new TextTrack(trackIndex, trackName, trackLanguage, isActive);
  742. break;
  743. }
  744. }
  745. return result;
  746. }
  747. // Text Cue stub methods
  748. internal override bool InternalIsChangedTextCue() { return false; }
  749. internal override string InternalGetCurrentTextCue() { return string.Empty; }
  750. }
  751. }
  752. #endif