AppleMediaPlayer.cs 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  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 void AudioConfigurationChanged(bool deviceChanged)
  551. {
  552. if (_playerSettings.audioOutputMode == Native.AVPPlayerAudioOutputMode.SystemDirect)
  553. return;
  554. _playerSettings.sampleRate = AudioSettings.outputSampleRate;
  555. int numBuffers;
  556. AudioSettings.GetDSPBufferSize(out _playerSettings.bufferLength, out numBuffers);
  557. Native.AVPPlayerSetPlayerSettings(_player, _playerSettings);
  558. }
  559. public override int GrabAudio(float[] buffer, int sampleCount, int channelCount)
  560. {
  561. return Native.AVPPlayerGetAudio(_player, buffer, buffer.Length);
  562. }
  563. public override int GetAudioBufferedSampleCount()
  564. {
  565. return _state.audioCaptureBufferedSamplesCount;
  566. }
  567. public override void SetAudioHeadRotation(Quaternion q)
  568. {
  569. // Unsupported
  570. }
  571. public override void ResetAudioHeadRotation()
  572. {
  573. // Unsupported
  574. }
  575. public override void SetAudioChannelMode(Audio360ChannelMode channelMode)
  576. {
  577. // Unsupported
  578. }
  579. public override void SetAudioFocusEnabled(bool enabled)
  580. {
  581. // Unsupported
  582. }
  583. public override void SetAudioFocusProperties(float offFocusLevel, float widthDegrees)
  584. {
  585. // Unsupported
  586. }
  587. public override void SetAudioFocusRotation(Quaternion q)
  588. {
  589. // Unsupported
  590. }
  591. public override void ResetAudioFocus()
  592. {
  593. // Unsupported
  594. }
  595. public override bool WaitForNextFrame(Camera camera, int previousFrameCount)
  596. {
  597. return false;
  598. }
  599. public override void SetKeyServerAuthToken(string token)
  600. {
  601. Native.AVPPlayerSetKeyServerAuthToken(_player, token);
  602. }
  603. public override void SetOverrideDecryptionKey(byte[] key)
  604. {
  605. int length = key != null ? key.Length : 0;
  606. Native.AVPPlayerSetDecryptionKey(_player, key, length);
  607. }
  608. public override bool IsExternalPlaybackActive()
  609. {
  610. return _state.status.IsExternalPlaybackActive();
  611. }
  612. public override void SetAllowsExternalPlayback(bool enable)
  613. {
  614. _flags.SetAllowExternalPlayback(enable);
  615. }
  616. public override void SetExternalPlaybackVideoGravity(ExternalPlaybackVideoGravity gravity_)
  617. {
  618. Native.AVPPlayerExternalPlaybackVideoGravity gravity;
  619. switch (gravity_)
  620. {
  621. case ExternalPlaybackVideoGravity.Resize:
  622. default:
  623. gravity = Native.AVPPlayerExternalPlaybackVideoGravity.Resize;
  624. break;
  625. case ExternalPlaybackVideoGravity.ResizeAspect:
  626. gravity = Native.AVPPlayerExternalPlaybackVideoGravity.ResizeAspect;
  627. break;
  628. case ExternalPlaybackVideoGravity.ResizeAspectFill:
  629. gravity = Native.AVPPlayerExternalPlaybackVideoGravity.ResizeAspectFill;
  630. break;
  631. }
  632. Native.AVPPlayerSetExternalPlaybackVideoGravity(_player, gravity);
  633. }
  634. }
  635. // IMediaInfo
  636. public sealed partial class AppleMediaPlayer
  637. {
  638. public override double GetDuration()
  639. {
  640. return _assetInfo.duration;
  641. }
  642. public override int GetVideoWidth()
  643. {
  644. int width = 0;
  645. if (_state.selectedVideoTrack >= 0)
  646. {
  647. width = (int)_videoTrackInfo[_state.selectedVideoTrack].dimensions.width;
  648. }
  649. return width;
  650. }
  651. public override int GetVideoHeight()
  652. {
  653. int height = 0;
  654. if (_state.selectedVideoTrack >= 0)
  655. {
  656. height = (int)_videoTrackInfo[_state.selectedVideoTrack].dimensions.height;
  657. }
  658. return height;
  659. }
  660. public override float GetVideoFrameRate()
  661. {
  662. float framerate = 0.0f;
  663. if (_state.selectedVideoTrack >= 0)
  664. {
  665. framerate = _videoTrackInfo[_state.selectedVideoTrack].frameRate;
  666. }
  667. return framerate;
  668. }
  669. public override bool HasVideo()
  670. {
  671. return _state.status.HasVideo();
  672. }
  673. public override bool HasAudio()
  674. {
  675. return _state.status.HasAudio();
  676. }
  677. public override bool PlayerSupportsLinearColorSpace()
  678. {
  679. return _playerTexture.flags.IsLinear();
  680. }
  681. public override bool IsPlaybackStalled()
  682. {
  683. return _state.status.IsStalled();
  684. }
  685. public override float[] GetTextureTransform()
  686. {
  687. if (_state.selectedVideoTrack >= 0)
  688. {
  689. Native.AVPAffineTransform transform = _videoTrackInfo[_state.selectedVideoTrack].transform;
  690. return new float[] { transform.a, transform.b, transform.c, transform.d, transform.tx, transform.ty };
  691. }
  692. else
  693. {
  694. return new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f };
  695. }
  696. }
  697. public override long GetEstimatedTotalBandwidthUsed()
  698. {
  699. return 0;
  700. }
  701. public override bool IsExternalPlaybackSupported()
  702. {
  703. return _assetInfo.flags.IsCompatibleWithAirPlay();
  704. }
  705. }
  706. // IMediaProducer
  707. public sealed partial class AppleMediaPlayer
  708. {
  709. public override int GetTextureCount()
  710. {
  711. return _playerTexture.planeCount;
  712. }
  713. public override Texture GetTexture(int index)
  714. {
  715. return _texturePlanes[index];
  716. }
  717. public override int GetTextureFrameCount()
  718. {
  719. return _playerTexture.frameCount;
  720. }
  721. public override bool SupportsTextureFrameCount()
  722. {
  723. return true;
  724. }
  725. public override long GetTextureTimeStamp()
  726. {
  727. return _playerTexture.itemTime;
  728. }
  729. public override bool RequiresVerticalFlip()
  730. {
  731. return _playerTexture.flags.IsFlipped();
  732. }
  733. public override TransparencyMode GetTextureTransparency()
  734. {
  735. if (_state.selectedVideoTrack >= 0)
  736. {
  737. Native.AVPPlayerVideoTrackInfo info = _videoTrackInfo[_state.selectedVideoTrack];
  738. if ((info.videoTrackFlags & Native.AVPPlayerVideoTrackFlags.HasAlpha) == Native.AVPPlayerVideoTrackFlags.HasAlpha)
  739. {
  740. return TransparencyMode.Transparent;
  741. }
  742. }
  743. return base.GetTextureTransparency();
  744. }
  745. public override Matrix4x4 GetYpCbCrTransform()
  746. {
  747. if (_videoTrackInfo.Length > 0 && _state.selectedVideoTrack >= 0)
  748. return _videoTrackInfo[_state.selectedVideoTrack].yCbCrTransform;
  749. else
  750. return Matrix4x4.identity;
  751. }
  752. internal override StereoPacking InternalGetTextureStereoPacking()
  753. {
  754. if (_state.selectedVideoTrack >= 0)
  755. {
  756. switch (_videoTrackInfo[_state.selectedVideoTrack].stereoMode)
  757. {
  758. case Native.AVPPlayerVideoTrackStereoMode.Unknown:
  759. return StereoPacking.Unknown;
  760. case Native.AVPPlayerVideoTrackStereoMode.Monoscopic:
  761. return StereoPacking.None;
  762. case Native.AVPPlayerVideoTrackStereoMode.StereoscopicLeftRight:
  763. return StereoPacking.LeftRight;
  764. case Native.AVPPlayerVideoTrackStereoMode.StereoscopicTopBottom:
  765. return StereoPacking.TopBottom;
  766. case Native.AVPPlayerVideoTrackStereoMode.StereoscopicRightLeft:
  767. return StereoPacking.Unknown;
  768. case Native.AVPPlayerVideoTrackStereoMode.StereoscopicCustom:
  769. return StereoPacking.CustomUV;
  770. }
  771. }
  772. return StereoPacking.Unknown;
  773. }
  774. }
  775. // IDispose
  776. public sealed partial class AppleMediaPlayer
  777. {
  778. public override void Dispose()
  779. {
  780. Native.AVPPlayerRelease(_player);
  781. }
  782. }
  783. // Version
  784. public sealed partial class AppleMediaPlayer
  785. {
  786. public override string GetVersion()
  787. {
  788. return Native.GetPluginVersion();
  789. }
  790. public override string GetExpectedVersion()
  791. {
  792. return Helper.ExpectedPluginVersion.Apple;
  793. }
  794. }
  795. // Media selection
  796. public sealed partial class AppleMediaPlayer
  797. {
  798. internal override bool InternalIsChangedTracks(TrackType trackType)
  799. {
  800. return _state.status.HasUpdatedAssetInfo();
  801. }
  802. internal override int InternalGetTrackCount(TrackType trackType)
  803. {
  804. switch (trackType)
  805. {
  806. case TrackType.Video:
  807. return _videoTrackInfo.Length;
  808. case TrackType.Audio:
  809. return _audioTrackInfo.Length;
  810. case TrackType.Text:
  811. return _textTrackInfo.Length;
  812. default:
  813. return 0;
  814. }
  815. }
  816. internal override bool InternalSetActiveTrack(TrackType trackType, int index)
  817. {
  818. switch (trackType)
  819. {
  820. case TrackType.Video:
  821. return Native.AVPPlayerSetTrack(_player, Native.AVPPlayerTrackType.Video, index);
  822. case TrackType.Audio:
  823. return Native.AVPPlayerSetTrack(_player, Native.AVPPlayerTrackType.Audio, index);
  824. case TrackType.Text:
  825. return Native.AVPPlayerSetTrack(_player, Native.AVPPlayerTrackType.Text, index);
  826. default:
  827. return false;
  828. }
  829. }
  830. internal override TrackBase InternalGetTrackInfo(TrackType type, int index, ref bool isActiveTrack)
  831. {
  832. TrackBase track = null;
  833. switch (type)
  834. {
  835. case TrackType.Video:
  836. if (index >= 0 && index < _videoTrackInfo.Length)
  837. {
  838. Native.AVPPlayerVideoTrackInfo trackInfo = _videoTrackInfo[index];
  839. track = new VideoTrack(index, trackInfo.name, trackInfo.language, trackInfo.flags.IsDefault());
  840. isActiveTrack = _state.selectedVideoTrack == index;
  841. }
  842. break;
  843. case TrackType.Audio:
  844. if (index >= 0 && index < _audioTrackInfo.Length)
  845. {
  846. Native.AVPPlayerAudioTrackInfo trackInfo = _audioTrackInfo[index];
  847. track = new AudioTrack(index, trackInfo.name, trackInfo.language, trackInfo.flags.IsDefault());
  848. isActiveTrack = _state.selectedAudioTrack == index;
  849. }
  850. break;
  851. case TrackType.Text:
  852. if (index >= 0 && index < _textTrackInfo.Length)
  853. {
  854. Native.AVPPlayerTextTrackInfo trackInfo = _textTrackInfo[index];
  855. track = new TextTrack(index, trackInfo.name, trackInfo.language, trackInfo.flags.IsDefault());
  856. isActiveTrack = _state.selectedTextTrack == index;
  857. }
  858. break;
  859. default:
  860. break;
  861. }
  862. return track;
  863. }
  864. internal override bool InternalIsChangedTextCue()
  865. {
  866. return _state.status.HasUpdatedText();
  867. }
  868. internal override string InternalGetCurrentTextCue()
  869. {
  870. if (_playerText.buffer != IntPtr.Zero)
  871. return Marshal.PtrToStringUni(_playerText.buffer, _playerText.length);
  872. else
  873. return null;
  874. }
  875. }
  876. #if !UNITY_EDITOR && UNITY_IOS
  877. // Media Caching
  878. public sealed partial class AppleMediaPlayer
  879. {
  880. public override bool IsMediaCachingSupported()
  881. {
  882. return true;
  883. }
  884. public override void AddMediaToCache(string url, string headers, MediaCachingOptions options)
  885. {
  886. Native.MediaCachingOptions nativeOptions = new Native.MediaCachingOptions();
  887. GCHandle artworkHandle = new GCHandle();
  888. if (options != null)
  889. {
  890. nativeOptions.minimumRequiredBitRate = options.minimumRequiredBitRate;
  891. nativeOptions.minimumRequiredResolution_width = options.minimumRequiredResolution.x;
  892. nativeOptions.minimumRequiredResolution_height = options.minimumRequiredResolution.y;
  893. nativeOptions.title = options.title;
  894. if (options.artwork != null && options.artwork.Length > 0)
  895. {
  896. artworkHandle = GCHandle.Alloc(options.artwork, GCHandleType.Pinned);
  897. nativeOptions.artwork = artworkHandle.AddrOfPinnedObject();
  898. nativeOptions.artworkLength = options.artwork.Length;
  899. }
  900. }
  901. Native.AVPPluginCacheMediaForURL(url, headers, nativeOptions);
  902. if (artworkHandle.IsAllocated)
  903. {
  904. artworkHandle.Free();
  905. }
  906. }
  907. public override void CancelDownloadOfMediaToCache(string url)
  908. {
  909. Native.AVPPluginCancelDownloadOfMediaForURL(url);
  910. }
  911. public override void RemoveMediaFromCache(string url)
  912. {
  913. Native.AVPPluginRemoveCachedMediaForURL(url);
  914. }
  915. public override CachedMediaStatus GetCachedMediaStatus(string url, ref float progress)
  916. {
  917. return (CachedMediaStatus)Native.AVPPluginGetCachedMediaStatusForURL(url, ref progress);
  918. }
  919. }
  920. #endif
  921. }
  922. #endif