AppleMediaPlayer.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. //-----------------------------------------------------------------------------
  2. // Copyright 2015-2022 RenderHeads Ltd. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. #if UNITY_2017_2_OR_NEWER && (UNITY_EDITOR_OSX || (!UNITY_EDITOR && (UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_TVOS)))
  5. using System;
  6. using System.Runtime.InteropServices;
  7. using System.Text.RegularExpressions;
  8. using UnityEngine;
  9. namespace RenderHeads.Media.AVProVideo
  10. {
  11. public sealed partial class AppleMediaPlayer : BaseMediaPlayer
  12. {
  13. private static Regex RxSupportedSchema = new Regex("^(https?|file)://", RegexOptions.None);
  14. private static DateTime Epoch = new DateTime(2001, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  15. static AppleMediaPlayer()
  16. {
  17. #if !UNITY_EDITOR && (UNITY_IOS || UNITY_TVOS)
  18. Native.AVPPluginBootstrap();
  19. #endif
  20. }
  21. private IntPtr _player;
  22. Native.AVPPlayerSettings _playerSettings;
  23. private MediaPlayer.OptionsApple _options;
  24. public AppleMediaPlayer(MediaPlayer.OptionsApple options)
  25. {
  26. // Keep a handle on the options
  27. _options = options;
  28. // Alert the user to OpenGL renderer being used
  29. if (SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.OpenGLCore)
  30. {
  31. Debug.LogWarning("[AVProVideo] OpenGL is not supported.");
  32. Debug.Log("[AVProVideo] The video will play but no video frames will be displayed. Please switch to using the Metal rendering API.");
  33. }
  34. // Configure the video output settings
  35. _playerSettings = new Native.AVPPlayerSettings();
  36. switch (options.textureFormat)
  37. {
  38. case MediaPlayer.OptionsApple.TextureFormat.BGRA:
  39. default:
  40. _playerSettings.pixelFormat = Native.AVPPlayerVideoPixelFormat.Bgra;
  41. break;
  42. case MediaPlayer.OptionsApple.TextureFormat.YCbCr420:
  43. _playerSettings.pixelFormat = Native.AVPPlayerVideoPixelFormat.YCbCr420;
  44. break;
  45. }
  46. if (options.flags.GenerateMipmaps())
  47. _playerSettings.videoFlags |= Native.AVPPlayerVideoOutputSettingsFlags.GenerateMipmaps;
  48. if (QualitySettings.activeColorSpace == ColorSpace.Linear)
  49. _playerSettings.videoFlags |= Native.AVPPlayerVideoOutputSettingsFlags.LinearColorSpace;
  50. GetWidthHeightFromResolution(
  51. options.preferredMaximumResolution,
  52. options.customPreferredMaximumResolution,
  53. out _playerSettings.preferredMaximumResolution_width,
  54. out _playerSettings.preferredMaximumResolution_height
  55. );
  56. _playerSettings.maximumPlaybackRate = options.maximumPlaybackRate;
  57. // Configure the audio output settings
  58. _playerSettings.audioOutputMode = (Native.AVPPlayerAudioOutputMode)options.audioMode;
  59. if (options.audioMode == MediaPlayer.OptionsApple.AudioMode.Unity)
  60. {
  61. _playerSettings.sampleRate = AudioSettings.outputSampleRate;
  62. int numBuffers;
  63. AudioSettings.GetDSPBufferSize(out _playerSettings.bufferLength, out numBuffers);
  64. }
  65. // Configure any network settings
  66. _playerSettings.preferredPeakBitRate = options.GetPreferredPeakBitRateInBitsPerSecond();
  67. _playerSettings.preferredForwardBufferDuration = options.preferredForwardBufferDuration;
  68. if (options.flags.PlayWithoutBuffering())
  69. _playerSettings.networkFlags |= Native.AVPPlayerNetworkSettingsFlags.PlayWithoutBuffering;
  70. if (options.flags.UseSinglePlayerItem())
  71. _playerSettings.networkFlags |= Native.AVPPlayerNetworkSettingsFlags.UseSinglePlayerItem;
  72. // Make the player
  73. _player = Native.AVPPluginMakePlayer(_playerSettings);
  74. // Setup any other flags from the options
  75. _flags = _flags.SetAllowExternalPlayback(options.flags.AllowExternalPlayback());
  76. _flags = _flags.SetResumePlayback(options.flags.ResumePlaybackAfterAudioSessionRouteChange());
  77. // Force an update to get our state in sync with the native
  78. Update();
  79. }
  80. private static void GetWidthHeightFromResolution(MediaPlayer.OptionsApple.Resolution resolution, Vector2Int custom, out float width, out float height)
  81. {
  82. switch (resolution)
  83. {
  84. case MediaPlayer.OptionsApple.Resolution.NoPreference:
  85. default:
  86. width = 0;
  87. height = 0;
  88. break;
  89. case MediaPlayer.OptionsApple.Resolution._480p:
  90. width = 640;
  91. height = 480;
  92. break;
  93. case MediaPlayer.OptionsApple.Resolution._720p:
  94. width = 1280;
  95. height = 720;
  96. break;
  97. case MediaPlayer.OptionsApple.Resolution._1080p:
  98. width = 1920;
  99. height = 1080;
  100. break;
  101. case MediaPlayer.OptionsApple.Resolution._1440p:
  102. width = 2560;
  103. height = 1440;
  104. break;
  105. case MediaPlayer.OptionsApple.Resolution._2160p:
  106. width = 3840;
  107. height = 2160;
  108. break;
  109. case MediaPlayer.OptionsApple.Resolution.Custom:
  110. width = custom.x;
  111. height = custom.y;
  112. break;
  113. }
  114. }
  115. }
  116. // IMediaPlayer
  117. public sealed partial class AppleMediaPlayer
  118. {
  119. private const int MaxTexturePlanes = 2;
  120. private Native.AVPPlayerState _state = new Native.AVPPlayerState();
  121. private Native.AVPPlayerFlags _flags = Native.AVPPlayerFlags.None;
  122. private Native.AVPPlayerAssetInfo _assetInfo = new Native.AVPPlayerAssetInfo();
  123. private Native.AVPPlayerVideoTrackInfo[] _videoTrackInfo = new Native.AVPPlayerVideoTrackInfo[0];
  124. private Native.AVPPlayerAudioTrackInfo[] _audioTrackInfo = new Native.AVPPlayerAudioTrackInfo[0];
  125. private Native.AVPPlayerTextTrackInfo[] _textTrackInfo = new Native.AVPPlayerTextTrackInfo[0];
  126. private Native.AVPPlayerTexture _playerTexture;
  127. private Native.AVPPlayerText _playerText;
  128. private Texture2D[] _texturePlanes = new Texture2D[MaxTexturePlanes];
  129. private float _volume = 1.0f;
  130. private float _rate = 1.0f;
  131. public override void OnEnable()
  132. {
  133. }
  134. public override void Update()
  135. {
  136. Native.AVPPlayerStatus prevStatus = _state.status;
  137. Native.AVPPlayerGetState(_player, ref _state);
  138. Native.AVPPlayerStatus changedStatus = prevStatus ^ _state.status;
  139. // Need to make sure that lastError is set when status is failed so that the Error event is triggered
  140. if (/*BaseMediaPlayer.*/_lastError == ErrorCode.None && changedStatus.HasFailed() && _state.status.HasFailed())
  141. {
  142. /*BaseMediaPlayer.*/_lastError = ErrorCode.LoadFailed;
  143. }
  144. if (_state.status.HasUpdatedAssetInfo())
  145. {
  146. Native.AVPPlayerGetAssetInfo(_player, ref _assetInfo);
  147. if (_state.status.HasVideo())
  148. {
  149. _videoTrackInfo = new Native.AVPPlayerVideoTrackInfo[_assetInfo.videoTrackCount];
  150. for (int i = 0; i < _assetInfo.videoTrackCount; ++i)
  151. {
  152. _videoTrackInfo[i] = new Native.AVPPlayerVideoTrackInfo();
  153. Native.AVPPlayerGetVideoTrackInfo(_player, i, ref _videoTrackInfo[i]);
  154. }
  155. }
  156. if (_state.status.HasAudio())
  157. {
  158. _audioTrackInfo = new Native.AVPPlayerAudioTrackInfo[_assetInfo.audioTrackCount];
  159. for (int i = 0; i < _assetInfo.audioTrackCount; ++i)
  160. {
  161. _audioTrackInfo[i] = new Native.AVPPlayerAudioTrackInfo();
  162. Native.AVPPlayerGetAudioTrackInfo(_player, i, ref _audioTrackInfo[i]);
  163. }
  164. }
  165. if (_state.status.HasText())
  166. {
  167. _textTrackInfo = new Native.AVPPlayerTextTrackInfo[_assetInfo.textTrackCount];
  168. for (int i = 0; i < _assetInfo.textTrackCount; ++i)
  169. {
  170. _textTrackInfo[i] = new Native.AVPPlayerTextTrackInfo();
  171. Native.AVPPlayerGetTextTrackInfo(_player, i, ref _textTrackInfo[i]);
  172. }
  173. }
  174. /*BaseMediaPlayer.*/UpdateTracks();
  175. }
  176. if (_state.status.HasUpdatedBufferedTimeRanges())
  177. {
  178. if (_state.bufferedTimeRangesCount > 0)
  179. {
  180. Native.AVPPlayerTimeRange[] timeRanges = new Native.AVPPlayerTimeRange[_state.bufferedTimeRangesCount];
  181. Native.AVPPlayerGetBufferedTimeRanges(_player, timeRanges, timeRanges.Length);
  182. _bufferedTimes = ConvertNativeTimeRangesToTimeRanges(timeRanges);
  183. }
  184. else
  185. {
  186. _bufferedTimes = new TimeRanges();
  187. }
  188. }
  189. if (_state.status.HasUpdatedSeekableTimeRanges())
  190. {
  191. if (_state.seekableTimeRangesCount > 0)
  192. {
  193. Native.AVPPlayerTimeRange[] timeRanges = new Native.AVPPlayerTimeRange[_state.seekableTimeRangesCount];
  194. Native.AVPPlayerGetSeekableTimeRanges(_player, timeRanges, timeRanges.Length);
  195. _seekableTimes = ConvertNativeTimeRangesToTimeRanges(timeRanges);
  196. }
  197. else
  198. {
  199. _seekableTimes = new TimeRanges();
  200. }
  201. }
  202. if (_state.status.HasUpdatedTexture())
  203. {
  204. Native.AVPPlayerGetTexture(_player, ref _playerTexture);
  205. for (int i = 0; i < _playerTexture.planeCount; ++i)
  206. {
  207. TextureFormat textureFormat = TextureFormat.BGRA32;
  208. switch (_playerTexture.planes[i].textureFormat)
  209. {
  210. case Native.AVPPlayerTextureFormat.R8:
  211. textureFormat = TextureFormat.R8;
  212. break;
  213. case Native.AVPPlayerTextureFormat.RG8:
  214. textureFormat = TextureFormat.RG16;
  215. break;
  216. case Native.AVPPlayerTextureFormat.BC1:
  217. textureFormat = TextureFormat.DXT1;
  218. break;
  219. case Native.AVPPlayerTextureFormat.BC3:
  220. textureFormat = TextureFormat.DXT5;
  221. break;
  222. case Native.AVPPlayerTextureFormat.BC4:
  223. textureFormat = TextureFormat.BC4;
  224. break;
  225. case Native.AVPPlayerTextureFormat.BC5:
  226. textureFormat = TextureFormat.BC5;
  227. break;
  228. case Native.AVPPlayerTextureFormat.BC7:
  229. textureFormat = TextureFormat.BC7;
  230. break;
  231. case Native.AVPPlayerTextureFormat.BGRA8:
  232. default:
  233. break;
  234. }
  235. if (_texturePlanes[i] == null ||
  236. _texturePlanes[i].width != _playerTexture.planes[i].width ||
  237. _texturePlanes[i].height != _playerTexture.planes[i].height ||
  238. _texturePlanes[i].format != textureFormat)
  239. {
  240. // Ensure any existing texture is released.
  241. if (_texturePlanes[i] != null)
  242. {
  243. _texturePlanes[i].UpdateExternalTexture(IntPtr.Zero);
  244. _texturePlanes[i] = null;
  245. }
  246. _texturePlanes[i] = Texture2D.CreateExternalTexture(
  247. _playerTexture.planes[i].width,
  248. _playerTexture.planes[i].height,
  249. textureFormat,
  250. _playerTexture.flags.IsMipmapped(),
  251. _playerTexture.flags.IsLinear(),
  252. _playerTexture.planes[i].plane
  253. );
  254. base.ApplyTextureProperties(_texturePlanes[i]);
  255. }
  256. else
  257. {
  258. _texturePlanes[i].UpdateExternalTexture(_playerTexture.planes[i].plane);
  259. }
  260. }
  261. }
  262. if (_state.status.HasUpdatedText())
  263. {
  264. Native.AVPPlayerGetText(_player, ref _playerText);
  265. /*BaseMediaPlayer.*/UpdateTextCue();
  266. }
  267. if (_flags.IsDirty())
  268. {
  269. _flags = _flags.SetDirty(false);
  270. Native.AVPPlayerSetFlags(_player, (int)_flags);
  271. }
  272. if (_options.HasChanged())
  273. {
  274. if (_options.HasChanged(MediaPlayer.OptionsApple.ChangeFlags.PreferredPeakBitRate))
  275. {
  276. _playerSettings.preferredPeakBitRate = _options.GetPreferredPeakBitRateInBitsPerSecond();
  277. }
  278. if (_options.HasChanged(MediaPlayer.OptionsApple.ChangeFlags.PreferredForwardBufferDuration))
  279. {
  280. _playerSettings.preferredForwardBufferDuration = _options.preferredForwardBufferDuration;
  281. }
  282. if (_options.HasChanged(MediaPlayer.OptionsApple.ChangeFlags.PlayWithoutBuffering))
  283. {
  284. bool enabled = (_options.flags & MediaPlayer.OptionsApple.Flags.PlayWithoutBuffering) == MediaPlayer.OptionsApple.Flags.PlayWithoutBuffering;
  285. _playerSettings.networkFlags = enabled ? _playerSettings.networkFlags | Native.AVPPlayerNetworkSettingsFlags.PlayWithoutBuffering
  286. : _playerSettings.networkFlags & ~Native.AVPPlayerNetworkSettingsFlags.PlayWithoutBuffering;
  287. }
  288. if (_options.HasChanged(MediaPlayer.OptionsApple.ChangeFlags.PreferredMaximumResolution))
  289. {
  290. GetWidthHeightFromResolution(
  291. _options.preferredMaximumResolution,
  292. _options.customPreferredMaximumResolution,
  293. out _playerSettings.preferredMaximumResolution_width,
  294. out _playerSettings.preferredMaximumResolution_height);
  295. }
  296. if (_options.HasChanged(MediaPlayer.OptionsApple.ChangeFlags.AudioMode))
  297. {
  298. if (_state.status.IsReadyToPlay() == false)
  299. {
  300. _playerSettings.audioOutputMode = (Native.AVPPlayerAudioOutputMode)_options.audioMode;
  301. }
  302. else
  303. {
  304. Debug.LogWarning("[AVProVideo] Unable to change audio mode after media has been loaded and is ready to play");
  305. _options.audioMode = _options.previousAudioMode;
  306. }
  307. }
  308. Native.AVPPlayerSetPlayerSettings(_player, _playerSettings);
  309. _options.ClearChanges();
  310. }
  311. /*BaseMediaPlayer.*/UpdateDisplayFrameRate();
  312. /*BaseMediaPlayer.*/UpdateSubtitles();
  313. }
  314. public override void Render()
  315. {
  316. }
  317. public override IntPtr GetNativePlayerHandle()
  318. {
  319. return _player;
  320. }
  321. private static TimeRanges ConvertNativeTimeRangesToTimeRanges(Native.AVPPlayerTimeRange[] ranges)
  322. {
  323. TimeRange[] targetRanges = new TimeRange[ranges.Length];
  324. for (int i = 0; i < ranges.Length; i++)
  325. {
  326. targetRanges[i].startTime = ranges[i].start;
  327. targetRanges[i].duration = ranges[i].duration;
  328. }
  329. return new TimeRanges(targetRanges);
  330. }
  331. }
  332. // IMediaControl
  333. public sealed partial class AppleMediaPlayer
  334. {
  335. public override bool OpenMedia(string path, long offset, string headers, MediaHints mediaHints, int forceFileFormat, bool startWithHighestBitrate)
  336. {
  337. _mediaHints = mediaHints;
  338. bool b = false;
  339. Match match = RxSupportedSchema.Match(path);
  340. if (match.Success)
  341. {
  342. string schema = match.Value;
  343. if (schema == "http://" || schema == "https://")
  344. {
  345. b = Native.AVPPlayerOpenURL(_player, path, headers);
  346. }
  347. else if (schema == "file://")
  348. {
  349. b = Native.AVPPlayerOpenURL(_player, path, null);
  350. }
  351. else
  352. {
  353. Debug.LogWarningFormat("[AVProVideo] Unsupported schema '{0}'", schema);
  354. }
  355. }
  356. else if (path.StartsWith("/"))
  357. {
  358. b = Native.AVPPlayerOpenURL(_player, "file://" + path, null);
  359. }
  360. else
  361. {
  362. Debug.LogWarning("[AVProVideo] Path is not a URL nor is it absolute.");
  363. }
  364. if (b)
  365. {
  366. Update();
  367. }
  368. return b;
  369. }
  370. public override bool OpenMediaFromBuffer(byte[] buffer)
  371. {
  372. // Unsupported
  373. return false;
  374. }
  375. public override bool StartOpenMediaFromBuffer(ulong length)
  376. {
  377. // Unsupported
  378. return false;
  379. }
  380. public override bool AddChunkToMediaBuffer(byte[] chunk, ulong offset, ulong length)
  381. {
  382. // Unsupported
  383. return false;
  384. }
  385. public override bool EndOpenMediaFromBuffer()
  386. {
  387. // Unsupported
  388. return false;
  389. }
  390. public override void CloseMedia()
  391. {
  392. Native.AVPPlayerClose(_player);
  393. Update();
  394. // Clean up the textures
  395. for (int i = 0; i < MaxTexturePlanes; ++i)
  396. {
  397. if (_texturePlanes[i] != null)
  398. {
  399. _texturePlanes[i].UpdateExternalTexture(IntPtr.Zero);
  400. _texturePlanes[i] = null;
  401. }
  402. }
  403. _playerTexture.frameCount = 0;
  404. }
  405. public override void SetLooping(bool b)
  406. {
  407. _flags = _flags.SetLooping(b);
  408. }
  409. public override bool IsLooping()
  410. {
  411. return _flags.IsLooping();
  412. }
  413. public override bool HasMetaData()
  414. {
  415. return _state.status.HasMetadata();
  416. }
  417. public override bool CanPlay()
  418. {
  419. return _state.status.IsReadyToPlay();
  420. }
  421. public override bool IsPlaying()
  422. {
  423. return _state.status.IsPlaying();
  424. }
  425. public override bool IsSeeking()
  426. {
  427. return _state.status.IsSeeking() || _state.status.HasFinishedSeeking();
  428. }
  429. public override bool IsPaused()
  430. {
  431. return _state.status.IsPaused();
  432. }
  433. public override bool IsFinished()
  434. {
  435. return _state.status.IsFinished();
  436. }
  437. public override bool IsBuffering()
  438. {
  439. return _state.status.IsBuffering();
  440. }
  441. public override void Play()
  442. {
  443. Native.AVPPlayerSetRate(_player, _rate);
  444. Update();
  445. }
  446. public override void Pause()
  447. {
  448. Native.AVPPlayerSetRate(_player, 0.0f);
  449. Update();
  450. }
  451. public override void Stop()
  452. {
  453. Pause();
  454. }
  455. public override void Rewind()
  456. {
  457. SeekWithTolerance(0.0, 0.0, 0.0);
  458. }
  459. public override void Seek(double toTime)
  460. {
  461. SeekWithTolerance(toTime, 0.0, 0.0);
  462. }
  463. public override void SeekFast(double toTime)
  464. {
  465. SeekWithTolerance(toTime, double.PositiveInfinity, double.PositiveInfinity);
  466. }
  467. public override void SeekWithTolerance(double toTime, double toleranceBefore, double toleranceAfter)
  468. {
  469. Native.AVPPlayerSeek(_player, toTime, toleranceBefore, toleranceAfter);
  470. Update();
  471. }
  472. public override double GetCurrentTime()
  473. {
  474. return _state.currentTime;
  475. }
  476. public override DateTime GetProgramDateTime()
  477. {
  478. return Epoch.AddSeconds(_state.currentDate);
  479. }
  480. public override float GetPlaybackRate()
  481. {
  482. return _rate;
  483. }
  484. public override void SetPlaybackRate(float rate)
  485. {
  486. if (rate != _rate)
  487. {
  488. _rate = rate;
  489. Native.AVPPlayerSetRate(_player, rate);
  490. Update();
  491. }
  492. }
  493. public override void MuteAudio(bool mute)
  494. {
  495. _flags = _flags.SetMuted(mute);
  496. }
  497. public override bool IsMuted()
  498. {
  499. return _flags.IsMuted();
  500. }
  501. public override void SetVolume(float volume)
  502. {
  503. if (volume != _volume)
  504. {
  505. _volume = volume;
  506. Native.AVPPlayerSetVolume(_player, volume);
  507. }
  508. }
  509. public override void SetBalance(float balance)
  510. {
  511. // Unsupported
  512. }
  513. public override float GetVolume()
  514. {
  515. return _volume;
  516. }
  517. public override float GetBalance()
  518. {
  519. // Unsupported
  520. return 0.0f;
  521. }
  522. public override long GetLastExtendedErrorCode()
  523. {
  524. return 0;
  525. }
  526. public override int GetAudioChannelCount()
  527. {
  528. int channelCount = -1;
  529. if (_state.selectedAudioTrack > -1 && _state.selectedAudioTrack < _audioTrackInfo.Length)
  530. {
  531. channelCount = (int)_audioTrackInfo[_state.selectedAudioTrack].channelCount;
  532. #if !UNITY_EDITOR && UNITY_IOS
  533. if (_options.audioMode == MediaPlayer.OptionsApple.AudioMode.Unity)
  534. {
  535. // iOS audio capture will convert down to two channel stereo
  536. channelCount = Math.Min(channelCount, 2);
  537. }
  538. #endif
  539. }
  540. return channelCount;
  541. }
  542. public override AudioChannelMaskFlags GetAudioChannelMask()
  543. {
  544. if (_state.selectedAudioTrack != -1 && _state.selectedAudioTrack < _audioTrackInfo.Length)
  545. {
  546. return _audioTrackInfo[_state.selectedAudioTrack].channelBitmap;
  547. }
  548. return AudioChannelMaskFlags.Unspecified;
  549. }
  550. public override int GrabAudio(float[] buffer, int sampleCount, int channelCount)
  551. {
  552. return Native.AVPPlayerGetAudio(_player, buffer, buffer.Length);
  553. }
  554. public override int GetAudioBufferedSampleCount()
  555. {
  556. return _state.audioCaptureBufferedSamplesCount;
  557. }
  558. public override void SetAudioHeadRotation(Quaternion q)
  559. {
  560. // Unsupported
  561. }
  562. public override void ResetAudioHeadRotation()
  563. {
  564. // Unsupported
  565. }
  566. public override void SetAudioChannelMode(Audio360ChannelMode channelMode)
  567. {
  568. // Unsupported
  569. }
  570. public override void SetAudioFocusEnabled(bool enabled)
  571. {
  572. // Unsupported
  573. }
  574. public override void SetAudioFocusProperties(float offFocusLevel, float widthDegrees)
  575. {
  576. // Unsupported
  577. }
  578. public override void SetAudioFocusRotation(Quaternion q)
  579. {
  580. // Unsupported
  581. }
  582. public override void ResetAudioFocus()
  583. {
  584. // Unsupported
  585. }
  586. public override bool WaitForNextFrame(Camera camera, int previousFrameCount)
  587. {
  588. return false;
  589. }
  590. public override void SetKeyServerAuthToken(string token)
  591. {
  592. Native.AVPPlayerSetKeyServerAuthToken(_player, token);
  593. }
  594. public override void SetOverrideDecryptionKey(byte[] key)
  595. {
  596. int length = key != null ? key.Length : 0;
  597. Native.AVPPlayerSetDecryptionKey(_player, key, length);
  598. }
  599. public override bool IsExternalPlaybackActive()
  600. {
  601. return _state.status.IsExternalPlaybackActive();
  602. }
  603. public override void SetAllowsExternalPlayback(bool enable)
  604. {
  605. _flags.SetAllowExternalPlayback(enable);
  606. }
  607. public override void SetExternalPlaybackVideoGravity(ExternalPlaybackVideoGravity gravity_)
  608. {
  609. Native.AVPPlayerExternalPlaybackVideoGravity gravity;
  610. switch (gravity_)
  611. {
  612. case ExternalPlaybackVideoGravity.Resize:
  613. default:
  614. gravity = Native.AVPPlayerExternalPlaybackVideoGravity.Resize;
  615. break;
  616. case ExternalPlaybackVideoGravity.ResizeAspect:
  617. gravity = Native.AVPPlayerExternalPlaybackVideoGravity.ResizeAspect;
  618. break;
  619. case ExternalPlaybackVideoGravity.ResizeAspectFill:
  620. gravity = Native.AVPPlayerExternalPlaybackVideoGravity.ResizeAspectFill;
  621. break;
  622. }
  623. Native.AVPPlayerSetExternalPlaybackVideoGravity(_player, gravity);
  624. }
  625. }
  626. // IMediaInfo
  627. public sealed partial class AppleMediaPlayer
  628. {
  629. public override double GetDuration()
  630. {
  631. return _assetInfo.duration;
  632. }
  633. public override int GetVideoWidth()
  634. {
  635. int width = 0;
  636. if (_state.selectedVideoTrack >= 0)
  637. {
  638. width = (int)_videoTrackInfo[_state.selectedVideoTrack].dimensions.width;
  639. }
  640. return width;
  641. }
  642. public override int GetVideoHeight()
  643. {
  644. int height = 0;
  645. if (_state.selectedVideoTrack >= 0)
  646. {
  647. height = (int)_videoTrackInfo[_state.selectedVideoTrack].dimensions.height;
  648. }
  649. return height;
  650. }
  651. public override float GetVideoFrameRate()
  652. {
  653. float framerate = 0.0f;
  654. if (_state.selectedVideoTrack >= 0)
  655. {
  656. framerate = _videoTrackInfo[_state.selectedVideoTrack].frameRate;
  657. }
  658. return framerate;
  659. }
  660. public override bool HasVideo()
  661. {
  662. return _state.status.HasVideo();
  663. }
  664. public override bool HasAudio()
  665. {
  666. return _state.status.HasAudio();
  667. }
  668. public override bool PlayerSupportsLinearColorSpace()
  669. {
  670. return _playerTexture.flags.IsLinear();
  671. }
  672. public override bool IsPlaybackStalled()
  673. {
  674. return _state.status.IsStalled();
  675. }
  676. public override float[] GetTextureTransform()
  677. {
  678. if (_state.selectedVideoTrack >= 0)
  679. {
  680. Native.AVPAffineTransform transform = _videoTrackInfo[_state.selectedVideoTrack].transform;
  681. return new float[] { transform.a, transform.b, transform.c, transform.d, transform.tx, transform.ty };
  682. }
  683. else
  684. {
  685. return new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f };
  686. }
  687. }
  688. public override long GetEstimatedTotalBandwidthUsed()
  689. {
  690. return 0;
  691. }
  692. public override bool IsExternalPlaybackSupported()
  693. {
  694. return _assetInfo.flags.IsCompatibleWithAirPlay();
  695. }
  696. }
  697. // IMediaProducer
  698. public sealed partial class AppleMediaPlayer
  699. {
  700. public override int GetTextureCount()
  701. {
  702. return _playerTexture.planeCount;
  703. }
  704. public override Texture GetTexture(int index)
  705. {
  706. return _texturePlanes[index];
  707. }
  708. public override int GetTextureFrameCount()
  709. {
  710. return _playerTexture.frameCount;
  711. }
  712. public override bool SupportsTextureFrameCount()
  713. {
  714. return true;
  715. }
  716. public override long GetTextureTimeStamp()
  717. {
  718. return _playerTexture.itemTime;
  719. }
  720. public override bool RequiresVerticalFlip()
  721. {
  722. return _playerTexture.flags.IsFlipped();
  723. }
  724. public override TransparencyMode GetTextureTransparency()
  725. {
  726. if (_state.selectedVideoTrack >= 0)
  727. {
  728. Native.AVPPlayerVideoTrackInfo info = _videoTrackInfo[_state.selectedVideoTrack];
  729. if ((info.videoTrackFlags & Native.AVPPlayerVideoTrackFlags.HasAlpha) == Native.AVPPlayerVideoTrackFlags.HasAlpha)
  730. {
  731. return TransparencyMode.Transparent;
  732. }
  733. }
  734. return base.GetTextureTransparency();
  735. }
  736. public override Matrix4x4 GetYpCbCrTransform()
  737. {
  738. if (_videoTrackInfo.Length > 0 && _state.selectedVideoTrack >= 0)
  739. return _videoTrackInfo[_state.selectedVideoTrack].yCbCrTransform;
  740. else
  741. return Matrix4x4.identity;
  742. }
  743. internal override StereoPacking InternalGetTextureStereoPacking()
  744. {
  745. if (_state.selectedVideoTrack >= 0)
  746. {
  747. switch (_videoTrackInfo[_state.selectedVideoTrack].stereoMode)
  748. {
  749. case Native.AVPPlayerVideoTrackStereoMode.Unknown:
  750. return StereoPacking.Unknown;
  751. case Native.AVPPlayerVideoTrackStereoMode.Monoscopic:
  752. return StereoPacking.None;
  753. case Native.AVPPlayerVideoTrackStereoMode.StereoscopicLeftRight:
  754. return StereoPacking.LeftRight;
  755. case Native.AVPPlayerVideoTrackStereoMode.StereoscopicTopBottom:
  756. return StereoPacking.TopBottom;
  757. case Native.AVPPlayerVideoTrackStereoMode.StereoscopicRightLeft:
  758. return StereoPacking.Unknown;
  759. case Native.AVPPlayerVideoTrackStereoMode.StereoscopicCustom:
  760. return StereoPacking.CustomUV;
  761. }
  762. }
  763. return StereoPacking.Unknown;
  764. }
  765. }
  766. // IDispose
  767. public sealed partial class AppleMediaPlayer
  768. {
  769. public override void Dispose()
  770. {
  771. Native.AVPPlayerRelease(_player);
  772. }
  773. }
  774. // Version
  775. public sealed partial class AppleMediaPlayer
  776. {
  777. public override string GetVersion()
  778. {
  779. return Native.GetPluginVersion();
  780. }
  781. public override string GetExpectedVersion()
  782. {
  783. return Helper.ExpectedPluginVersion.Apple;
  784. }
  785. }
  786. // Media selection
  787. public sealed partial class AppleMediaPlayer
  788. {
  789. internal override bool InternalIsChangedTracks(TrackType trackType)
  790. {
  791. return _state.status.HasUpdatedAssetInfo();
  792. }
  793. internal override int InternalGetTrackCount(TrackType trackType)
  794. {
  795. switch (trackType)
  796. {
  797. case TrackType.Video:
  798. return _videoTrackInfo.Length;
  799. case TrackType.Audio:
  800. return _audioTrackInfo.Length;
  801. case TrackType.Text:
  802. return _textTrackInfo.Length;
  803. default:
  804. return 0;
  805. }
  806. }
  807. internal override bool InternalSetActiveTrack(TrackType trackType, int index)
  808. {
  809. switch (trackType)
  810. {
  811. case TrackType.Video:
  812. return Native.AVPPlayerSetTrack(_player, Native.AVPPlayerTrackType.Video, index);
  813. case TrackType.Audio:
  814. return Native.AVPPlayerSetTrack(_player, Native.AVPPlayerTrackType.Audio, index);
  815. case TrackType.Text:
  816. return Native.AVPPlayerSetTrack(_player, Native.AVPPlayerTrackType.Text, index);
  817. default:
  818. return false;
  819. }
  820. }
  821. internal override TrackBase InternalGetTrackInfo(TrackType type, int index, ref bool isActiveTrack)
  822. {
  823. TrackBase track = null;
  824. switch (type)
  825. {
  826. case TrackType.Video:
  827. if (index >= 0 && index < _videoTrackInfo.Length)
  828. {
  829. Native.AVPPlayerVideoTrackInfo trackInfo = _videoTrackInfo[index];
  830. track = new VideoTrack(index, trackInfo.name, trackInfo.language, trackInfo.flags.IsDefault());
  831. isActiveTrack = _state.selectedVideoTrack == index;
  832. }
  833. break;
  834. case TrackType.Audio:
  835. if (index >= 0 && index < _audioTrackInfo.Length)
  836. {
  837. Native.AVPPlayerAudioTrackInfo trackInfo = _audioTrackInfo[index];
  838. track = new AudioTrack(index, trackInfo.name, trackInfo.language, trackInfo.flags.IsDefault());
  839. isActiveTrack = _state.selectedAudioTrack == index;
  840. }
  841. break;
  842. case TrackType.Text:
  843. if (index >= 0 && index < _textTrackInfo.Length)
  844. {
  845. Native.AVPPlayerTextTrackInfo trackInfo = _textTrackInfo[index];
  846. track = new TextTrack(index, trackInfo.name, trackInfo.language, trackInfo.flags.IsDefault());
  847. isActiveTrack = _state.selectedTextTrack == index;
  848. }
  849. break;
  850. default:
  851. break;
  852. }
  853. return track;
  854. }
  855. internal override bool InternalIsChangedTextCue()
  856. {
  857. return _state.status.HasUpdatedText();
  858. }
  859. internal override string InternalGetCurrentTextCue()
  860. {
  861. if (_playerText.buffer != IntPtr.Zero)
  862. return Marshal.PtrToStringUni(_playerText.buffer, _playerText.length);
  863. else
  864. return null;
  865. }
  866. }
  867. #if !UNITY_EDITOR && UNITY_IOS
  868. // Media Caching
  869. public sealed partial class AppleMediaPlayer
  870. {
  871. public override bool IsMediaCachingSupported()
  872. {
  873. return true;
  874. }
  875. public override void AddMediaToCache(string url, string headers, MediaCachingOptions options)
  876. {
  877. Native.MediaCachingOptions nativeOptions = new Native.MediaCachingOptions();
  878. GCHandle artworkHandle = new GCHandle();
  879. if (options != null)
  880. {
  881. nativeOptions.minimumRequiredBitRate = options.minimumRequiredBitRate;
  882. nativeOptions.minimumRequiredResolution_width = options.minimumRequiredResolution.x;
  883. nativeOptions.minimumRequiredResolution_height = options.minimumRequiredResolution.y;
  884. nativeOptions.title = options.title;
  885. if (options.artwork != null && options.artwork.Length > 0)
  886. {
  887. artworkHandle = GCHandle.Alloc(options.artwork, GCHandleType.Pinned);
  888. nativeOptions.artwork = artworkHandle.AddrOfPinnedObject();
  889. nativeOptions.artworkLength = options.artwork.Length;
  890. }
  891. }
  892. Native.AVPPluginCacheMediaForURL(url, headers, nativeOptions);
  893. if (artworkHandle.IsAllocated)
  894. {
  895. artworkHandle.Free();
  896. }
  897. }
  898. public override void CancelDownloadOfMediaToCache(string url)
  899. {
  900. Native.AVPPluginCancelDownloadOfMediaForURL(url);
  901. }
  902. public override void RemoveMediaFromCache(string url)
  903. {
  904. Native.AVPPluginRemoveCachedMediaForURL(url);
  905. }
  906. public override CachedMediaStatus GetCachedMediaStatus(string url, ref float progress)
  907. {
  908. return (CachedMediaStatus)Native.AVPPluginGetCachedMediaStatusForURL(url, ref progress);
  909. }
  910. }
  911. #endif
  912. }
  913. #endif