MediaPlayer.cs 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  1. //#define AVPROVIDEO_BETA_SUPPORT_TIMESCALE // BETA FEATURE: comment this in if you want to support frame stepping based on changes in Time.timeScale or Time.captureFramerate
  2. //#define AVPROVIDEO_FORCE_NULL_MEDIAPLAYER // DEV FEATURE: comment this out to make all mediaplayers use the null mediaplayer
  3. //#define AVPROVIDEO_DISABLE_LOGGING // DEV FEATURE: disables Debug.Log from AVPro Video
  4. #define AVPROVIDEO_SUPPORT_LIVEEDITMODE
  5. using UnityEngine;
  6. using UnityEngine.Serialization;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. //-----------------------------------------------------------------------------
  10. // Copyright 2015-2022 RenderHeads Ltd. All rights reserved.
  11. //-----------------------------------------------------------------------------
  12. namespace RenderHeads.Media.AVProVideo
  13. {
  14. /// <summary>
  15. /// This is the primary AVPro Video component and handles all media loading,
  16. /// seeking, information retrieving etc. This component does not do any display
  17. /// of the video. Instead this is handled by other components such as
  18. /// ApplyToMesh, ApplyToMaterial, DisplayIMGUI, DisplayUGUI.
  19. /// </summary>
  20. #if AVPROVIDEO_SUPPORT_LIVEEDITMODE
  21. [ExecuteInEditMode]
  22. #endif
  23. [AddComponentMenu("AVPro Video/Media Player", -100)]
  24. [HelpURL("https://www.renderheads.com/products/avpro-video/")]
  25. public partial class MediaPlayer : MonoBehaviour
  26. {
  27. // These fields are just used to setup the default properties for a new video that is about to be loaded
  28. // Once a video has been loaded you should use the interfaces exposed in the properties to
  29. // change playback properties (eg volume, looping, mute)
  30. // Media source
  31. [SerializeField] MediaSource _mediaSource = MediaSource.Reference;
  32. public MediaSource MediaSource { get { return _mediaSource; } internal set { _mediaSource = value; } }
  33. [SerializeField] MediaReference _mediaReference = null;
  34. public MediaReference MediaReference { get { return _mediaReference; } internal set { _mediaReference = value; } }
  35. [SerializeField] MediaPath _mediaPath = new MediaPath();
  36. public MediaPath MediaPath { get { return _mediaPath; } set { _mediaPath = value; } }
  37. [SerializeField] MediaHints _fallbackMediaHints = MediaHints.Default;
  38. public MediaHints FallbackMediaHints { get { return _fallbackMediaHints; } set { _fallbackMediaHints = value; } }
  39. [FormerlySerializedAs("m_AutoOpen")]
  40. [SerializeField] bool _autoOpen = true;
  41. public bool AutoOpen { get { return _autoOpen; } set { _autoOpen = value; } }
  42. [FormerlySerializedAs("m_AutoStart")]
  43. [SerializeField] bool _autoPlayOnStart = true;
  44. public bool AutoStart { get { return _autoPlayOnStart; } set { _autoPlayOnStart = value; } }
  45. // Basic controls
  46. [FormerlySerializedAs("m_Loop")]
  47. [SerializeField] bool _loop = false;
  48. public bool Loop
  49. {
  50. get
  51. {
  52. return (_controlInterface != null) ? _controlInterface.IsLooping() : _loop;
  53. }
  54. set
  55. {
  56. _loop = value;
  57. if (_controlInterface != null)
  58. _controlInterface.SetLooping(_loop);
  59. }
  60. }
  61. [FormerlySerializedAs("m_Volume")]
  62. [Range(0.0f, 1.0f)]
  63. [SerializeField] float _audioVolume = 1.0f;
  64. public virtual float AudioVolume
  65. {
  66. get
  67. {
  68. return (_controlInterface != null) ? _controlInterface.GetVolume() : _audioVolume;
  69. }
  70. set
  71. {
  72. _audioVolume = Mathf.Clamp01(value);
  73. if (_controlInterface != null)
  74. _controlInterface.SetVolume(_audioVolume);
  75. }
  76. }
  77. [FormerlySerializedAs("m_Balance")]
  78. [Range(-1.0f, 1.0f)]
  79. [SerializeField] float _audioBalance = 0.0f;
  80. public float AudioBalance
  81. {
  82. get
  83. {
  84. return (_controlInterface != null) ? _controlInterface.GetBalance() : _audioBalance;
  85. }
  86. set
  87. {
  88. _audioBalance = Mathf.Clamp(value, -1f, 1f);
  89. if (_controlInterface != null)
  90. _controlInterface.SetBalance(_audioBalance);
  91. }
  92. }
  93. [FormerlySerializedAs("m_Muted")]
  94. [SerializeField] bool _audioMuted = false;
  95. public virtual bool AudioMuted
  96. {
  97. get
  98. {
  99. return (_controlInterface != null) ? _controlInterface.IsMuted() : _audioMuted;
  100. }
  101. set
  102. {
  103. _audioMuted = value;
  104. if (_controlInterface != null)
  105. {
  106. #if !UNITY_EDITOR
  107. _controlInterface.MuteAudio(_audioMuted);
  108. #else
  109. _controlInterface.MuteAudio(_audioMuted || UnityEditor.EditorUtility.audioMasterMute);
  110. #endif
  111. }
  112. }
  113. }
  114. private AudioSource _audioSource = null;
  115. public AudioSource AudioSource { get { return _audioSource; } internal set { _audioSource = value; } }
  116. [FormerlySerializedAs("m_PlaybackRate")]
  117. [Range(-4.0f, 4.0f)]
  118. [SerializeField] float _playbackRate = 1.0f;
  119. public float PlaybackRate
  120. {
  121. get
  122. {
  123. return (_controlInterface != null) ? _controlInterface.GetPlaybackRate() : _playbackRate;
  124. }
  125. set
  126. {
  127. _playbackRate = value;
  128. if (_controlInterface != null)
  129. _controlInterface.SetPlaybackRate(_playbackRate);
  130. }
  131. }
  132. // Resampler
  133. [FormerlySerializedAs("m_Resample")]
  134. [SerializeField] bool _useResampler = false;
  135. public bool UseResampler { get { return _useResampler; } set { _useResampler = value; } }
  136. [FormerlySerializedAs("m_ResampleMode")]
  137. [SerializeField] Resampler.ResampleMode _resampleMode = Resampler.ResampleMode.POINT;
  138. public Resampler.ResampleMode ResampleMode { get { return _resampleMode; } set { _resampleMode = value; } }
  139. [FormerlySerializedAs("m_ResampleBufferSize")]
  140. [Range(3, 10)]
  141. [SerializeField] int _resampleBufferSize = 5;
  142. public int ResampleBufferSize { get { return _resampleBufferSize; } set { _resampleBufferSize = value; } }
  143. private Resampler _resampler = null;
  144. public Resampler FrameResampler { get { return _resampler; } }
  145. // Visual options
  146. [FormerlySerializedAs("m_videoMapping")]
  147. [SerializeField] VideoMapping _videoMapping = VideoMapping.Unknown;
  148. public VideoMapping VideoLayoutMapping { get { return _videoMapping; } set { _videoMapping = value; } }
  149. [FormerlySerializedAs("m_FilterMode")]
  150. [SerializeField] FilterMode _textureFilterMode = FilterMode.Bilinear;
  151. public FilterMode TextureFilterMode
  152. {
  153. get
  154. {
  155. if (_controlInterface != null)
  156. {
  157. FilterMode filterMode = FilterMode.Point;
  158. TextureWrapMode textureWrapMode = TextureWrapMode.Repeat;
  159. int anisoLevel = 0;
  160. _controlInterface.GetTextureProperties(out filterMode, out textureWrapMode, out anisoLevel);
  161. return filterMode;
  162. }
  163. else
  164. return _textureFilterMode;
  165. }
  166. set
  167. {
  168. _textureFilterMode = value;
  169. if (_controlInterface != null)
  170. _controlInterface.SetTextureProperties(_textureFilterMode, _textureWrapMode, _textureAnisoLevel);
  171. }
  172. }
  173. [FormerlySerializedAs("m_WrapMode")]
  174. [SerializeField] TextureWrapMode _textureWrapMode = TextureWrapMode.Clamp;
  175. public TextureWrapMode TextureWrapMode
  176. {
  177. get
  178. {
  179. if (_controlInterface != null)
  180. {
  181. FilterMode filterMode = FilterMode.Point;
  182. TextureWrapMode textureWrapMode = TextureWrapMode.Repeat;
  183. int anisoLevel = 0;
  184. _controlInterface.GetTextureProperties(out filterMode, out textureWrapMode, out anisoLevel);
  185. return textureWrapMode;
  186. }
  187. else
  188. return _textureWrapMode;
  189. }
  190. set
  191. {
  192. _textureWrapMode = value;
  193. if (_controlInterface != null)
  194. _controlInterface.SetTextureProperties(_textureFilterMode, _textureWrapMode, _textureAnisoLevel);
  195. }
  196. }
  197. [FormerlySerializedAs("m_AnisoLevel")]
  198. [Range(0, 16)]
  199. [SerializeField] int _textureAnisoLevel = 0;
  200. public int TextureAnisoLevel
  201. {
  202. get
  203. {
  204. if (_controlInterface != null)
  205. {
  206. FilterMode filterMode = FilterMode.Point;
  207. TextureWrapMode textureWrapMode = TextureWrapMode.Repeat;
  208. int anisoLevel = 0;
  209. _controlInterface.GetTextureProperties(out filterMode, out textureWrapMode, out anisoLevel);
  210. return anisoLevel;
  211. }
  212. else
  213. return _textureAnisoLevel;
  214. }
  215. set
  216. {
  217. _textureAnisoLevel = value;
  218. if (_controlInterface != null)
  219. _controlInterface.SetTextureProperties(_textureFilterMode, _textureWrapMode, _textureAnisoLevel);
  220. }
  221. }
  222. [SerializeField] bool _useVideoResolve = false;
  223. public bool UseVideoResolve { get { return _useVideoResolve; } set { _useVideoResolve = value; } }
  224. [SerializeField] VideoResolveOptions _videoResolveOptions = VideoResolveOptions.Create();
  225. public VideoResolveOptions VideoResolveOptions { get { return _videoResolveOptions; } set { _videoResolveOptions = value; } }
  226. #if AVPRO_FEATURE_VIDEORESOLVE
  227. [SerializeField] VideoResolve _videoResolve = new VideoResolve();
  228. #endif
  229. // Sideloaded subtitles
  230. [FormerlySerializedAs("m_LoadSubtitles")]
  231. [SerializeField] bool _sideloadSubtitles;
  232. public bool SideloadSubtitles { get { return _sideloadSubtitles; } set { _sideloadSubtitles = value; } }
  233. [SerializeField] MediaPath _subtitlePath;
  234. public MediaPath SubtitlePath { get { return _subtitlePath; } set { _subtitlePath = value; } }
  235. // Audio 360
  236. [FormerlySerializedAs("m_AudioHeadTransform")]
  237. [SerializeField] Transform _audioHeadTransform;
  238. public Transform AudioHeadTransform { set { _audioHeadTransform = value; } get { return _audioHeadTransform; } }
  239. [FormerlySerializedAs("m_AudioFocusEnabled")]
  240. [SerializeField] bool _audioFocusEnabled;
  241. public bool AudioFocusEnabled { get { return _audioFocusEnabled; } set { _audioFocusEnabled = value; } }
  242. [FormerlySerializedAs("m_AudioFocusTransform")]
  243. [SerializeField] Transform _audioFocusTransform;
  244. public Transform AudioFocusTransform { get { return _audioFocusTransform; } set { _audioFocusTransform = value; } }
  245. [FormerlySerializedAs("m_AudioFocusWidthDegrees")]
  246. [SerializeField, Range(40f, 120f)] float _audioFocusWidthDegrees = 90f;
  247. public float AudioFocusWidthDegrees { get { return _audioFocusWidthDegrees; } set { _audioFocusWidthDegrees = value; } }
  248. [FormerlySerializedAs("m_AudioFocusOffLevelDB")]
  249. [SerializeField, Range(-24f, 0f)] float _audioFocusOffLevelDB = 0f;
  250. public float AudioFocusOffLevelDB { get { return _audioFocusOffLevelDB; } set { _audioFocusOffLevelDB = value; } }
  251. // Network
  252. [SerializeField] HttpHeaderData _httpHeaders = new HttpHeaderData();
  253. public HttpHeaderData HttpHeaders { get { return _httpHeaders; } set { _httpHeaders = value; } }
  254. [SerializeField] KeyAuthData _keyAuth = new KeyAuthData();
  255. public KeyAuthData KeyAuth { get { return _keyAuth; } set { _keyAuth = value; } }
  256. // Events
  257. [FormerlySerializedAs("m_events")]
  258. [SerializeField] MediaPlayerEvent _events = null;
  259. public MediaPlayerEvent Events
  260. {
  261. get
  262. {
  263. if (_events == null)
  264. {
  265. _events = new MediaPlayerEvent();
  266. }
  267. return _events;
  268. }
  269. }
  270. [FormerlySerializedAs("m_eventMask")]
  271. [SerializeField] int _eventMask = -1;
  272. public int EventMask { get { return _eventMask; } set { _eventMask = value; } }
  273. [SerializeField] bool _pauseMediaOnAppPause = true;
  274. public bool PauseMediaOnAppPause { get { return _pauseMediaOnAppPause; } set { _pauseMediaOnAppPause = value; } }
  275. [SerializeField] bool _playMediaOnAppUnpause = true;
  276. public bool PlayMediaOnAppUnpause { get { return _playMediaOnAppUnpause; } set { _playMediaOnAppUnpause = value; } }
  277. // Misc options
  278. [FormerlySerializedAs("m_Persistent")]
  279. [SerializeField] bool _persistent = false;
  280. public bool Persistent { get { return _persistent; } set { _persistent = value; } }
  281. [FormerlySerializedAs("m_forceFileFormat")]
  282. [SerializeField] FileFormat _forceFileFormat = FileFormat.Unknown;
  283. public FileFormat ForceFileFormat { get { return _forceFileFormat; } set { _forceFileFormat = value; } }
  284. // Interfaces
  285. private BaseMediaPlayer _baseMediaPlayer;
  286. private IMediaControl _controlInterface;
  287. private ITextureProducer _textureInterface;
  288. private IMediaInfo _infoInterface;
  289. private IMediaPlayer _playerInterface;
  290. private IMediaSubtitles _subtitlesInterface;
  291. private IMediaCache _cacheInterface;
  292. private IBufferedDisplay _bufferedDisplayInterface;
  293. private IVideoTracks _videoTracksInterface;
  294. private IAudioTracks _audioTracksInterface;
  295. private ITextTracks _textTracksInterface;
  296. private System.IDisposable _disposeInterface;
  297. public virtual IMediaInfo Info { get { return _infoInterface; } }
  298. public virtual IMediaControl Control { get { return _controlInterface; } }
  299. public virtual IMediaPlayer Player { get { return _playerInterface; } }
  300. public virtual ITextureProducer TextureProducer { get { return _textureInterface; } }
  301. public virtual IMediaSubtitles Subtitles { get { return _subtitlesInterface; } }
  302. public virtual IVideoTracks VideoTracks { get { return _videoTracksInterface; } }
  303. public virtual IAudioTracks AudioTracks { get { return _audioTracksInterface; } }
  304. public virtual ITextTracks TextTracks { get { return _textTracksInterface; } }
  305. public virtual IMediaCache Cache { get { return _cacheInterface; } }
  306. public virtual IBufferedDisplay BufferedDisplay { get { return _bufferedDisplayInterface; } }
  307. // State
  308. private bool _isMediaOpened = false;
  309. public bool MediaOpened { get { return _isMediaOpened; } }
  310. private bool _autoPlayOnStartTriggered = false;
  311. private bool _wasPlayingOnPause = false;
  312. private Coroutine _renderingCoroutine = null;
  313. // Global init
  314. private static bool s_GlobalStartup = false;
  315. private static bool s_TrialVersion = false;
  316. // Subtitle state
  317. private MediaPath _queueSubtitlePath;
  318. private Coroutine _loadSubtitlesRoutine;
  319. // Extract frame
  320. private static Camera _dummyCamera = null;
  321. public delegate void ProcessExtractedFrame(Texture2D extractedFrame);
  322. /// <summary>
  323. /// Methods
  324. /// </summary>
  325. #if UNITY_EDITOR
  326. static MediaPlayer()
  327. {
  328. SetupEditorPlayPauseSupport();
  329. }
  330. #endif
  331. void Awake()
  332. {
  333. if (_persistent)
  334. {
  335. // TODO: set "this.transform.root.gameObject" to also DontDestroyOnLoad?
  336. DontDestroyOnLoad(this.gameObject);
  337. }
  338. }
  339. protected void Initialise()
  340. {
  341. BaseMediaPlayer mediaPlayer = CreateMediaPlayer();
  342. if (mediaPlayer != null)
  343. {
  344. // Set-up interface
  345. _baseMediaPlayer = mediaPlayer;
  346. _controlInterface = mediaPlayer;
  347. _textureInterface = mediaPlayer;
  348. _infoInterface = mediaPlayer;
  349. _playerInterface = mediaPlayer;
  350. _subtitlesInterface = mediaPlayer;
  351. _videoTracksInterface = mediaPlayer;
  352. _audioTracksInterface = mediaPlayer;
  353. _textTracksInterface = mediaPlayer;
  354. _disposeInterface = mediaPlayer;
  355. _cacheInterface = mediaPlayer;
  356. _bufferedDisplayInterface = mediaPlayer;
  357. string nativePluginVersion = mediaPlayer.GetVersion();
  358. string expectedNativePluginVersion = mediaPlayer.GetExpectedVersion();
  359. // Check that the plugin version number is not too old
  360. if (!nativePluginVersion.StartsWith(expectedNativePluginVersion))
  361. {
  362. Debug.LogError("[AVProVideo] Plugin version number " + nativePluginVersion + " doesn't match the expected version number " + expectedNativePluginVersion + ". It looks like the plugin didn't upgrade correctly. To resolve this please restart Unity and try to upgrade the package again.");
  363. }
  364. s_TrialVersion = nativePluginVersion.Contains("-trial");
  365. if (!s_GlobalStartup)
  366. {
  367. Helper.LogInfo(string.Format("Initialising AVPro Video v{0} (native plugin v{1}) on {2}/{3} (MT {4}) on {5}", Helper.AVProVideoVersion, nativePluginVersion, SystemInfo.graphicsDeviceName, SystemInfo.graphicsDeviceVersion, SystemInfo.graphicsMultiThreaded, Application.platform));
  368. #if AVPROVIDEO_BETA_SUPPORT_TIMESCALE
  369. Debug.LogWarning("[AVProVideo] TimeScale support used. This could affect performance when changing Time.timeScale or Time.captureFramerate. This feature is useful for supporting video capture system that adjust time scale during capturing.");
  370. #endif
  371. s_GlobalStartup = true;
  372. }
  373. }
  374. }
  375. void Start()
  376. {
  377. #if UNITY_WEBGL
  378. _useResampler = false;
  379. #endif
  380. if (_controlInterface == null)
  381. {
  382. if (Application.isPlaying)
  383. {
  384. Initialise();
  385. if (_controlInterface != null)
  386. {
  387. if (_autoOpen)
  388. {
  389. OpenMedia(_autoPlayOnStart);
  390. if (_sideloadSubtitles && _subtitlesInterface != null && _subtitlePath != null && !string.IsNullOrEmpty(_subtitlePath.Path))
  391. {
  392. EnableSubtitles(_subtitlePath);
  393. }
  394. }
  395. StartRenderCoroutine();
  396. }
  397. }
  398. }
  399. }
  400. public bool OpenMedia(MediaPath path, bool autoPlay = true)
  401. {
  402. return OpenMedia(path.PathType, path.Path, autoPlay);
  403. }
  404. public bool OpenMedia(MediaPathType pathType, string path, bool autoPlay = true)
  405. {
  406. _mediaSource = MediaSource.Path;
  407. _mediaPath.Path = path;
  408. _mediaPath.PathType = pathType;
  409. return OpenMedia(autoPlay);
  410. }
  411. public bool OpenMedia(MediaReference mediaReference, bool autoPlay = true)
  412. {
  413. _mediaSource = MediaSource.Reference;
  414. _mediaReference = mediaReference;
  415. return OpenMedia(autoPlay);
  416. }
  417. public bool OpenMedia(bool autoPlay = true)
  418. {
  419. _autoPlayOnStart = autoPlay;
  420. if (_controlInterface == null)
  421. {
  422. //_autoOpen = false; // If OpenVideoFromFile() is called before Start() then set _autoOpen to false so that it doesn't load the video a second time during Start()
  423. Initialise();
  424. }
  425. return InternalOpenMedia();
  426. }
  427. private bool InternalOpenMedia()
  428. {
  429. bool result = false;
  430. // Open the video file
  431. if (_controlInterface != null)
  432. {
  433. CloseMedia();
  434. _isMediaOpened = true;
  435. _autoPlayOnStartTriggered = !_autoPlayOnStart;
  436. _finishedFrameOpenCheck = true;
  437. long fileOffset = GetPlatformFileOffset(); // TODO: replace this with MediaReference
  438. MediaPath mediaPath = null;
  439. MediaHints mediaHints = _fallbackMediaHints;
  440. if (_mediaSource == MediaSource.Reference)
  441. {
  442. if (_mediaReference != null)
  443. {
  444. mediaPath = _mediaReference.GetCurrentPlatformMediaReference().MediaPath;
  445. mediaHints = _mediaReference.GetCurrentPlatformMediaReference().Hints;
  446. if (string.IsNullOrEmpty(mediaPath.Path))
  447. {
  448. mediaPath = null;
  449. }
  450. }
  451. else
  452. {
  453. Debug.LogError("[AVProVideo] No MediaReference specified", this);
  454. }
  455. }
  456. else if (_mediaSource == MediaSource.Path)
  457. {
  458. if (!string.IsNullOrEmpty(_mediaPath.Path))
  459. {
  460. mediaPath = _mediaPath;
  461. }
  462. else
  463. {
  464. Debug.LogError("[AVProVideo] No file path specified", this);
  465. }
  466. }
  467. if (null != mediaPath)
  468. {
  469. string fullPath = mediaPath.GetResolvedFullPath();
  470. string customHttpHeaders = null;
  471. bool checkForFileExist = true;
  472. bool isURL = fullPath.Contains("://");
  473. if (isURL)
  474. {
  475. checkForFileExist = false;
  476. customHttpHeaders = GetPlatformHttpHeadersAsString();
  477. }
  478. #if (!UNITY_EDITOR && UNITY_ANDROID)
  479. checkForFileExist = false;
  480. #endif
  481. if (checkForFileExist && !System.IO.File.Exists(fullPath))
  482. {
  483. Debug.LogError("[AVProVideo] File not found: " + fullPath, this);
  484. }
  485. else
  486. {
  487. Helper.LogInfo(string.Format("Opening {0} (offset {1}) with API {2}", fullPath, fileOffset, GetPlatformVideoApiString()), this);
  488. #if UNITY_EDITOR_WIN || (!UNITY_EDITOR && UNITY_STANDALONE_WIN)
  489. // NOTE: We don't need to call SetAudioChannelMode on Android,
  490. // as it's set when the AndroidMediaPlayer object is created
  491. if (_optionsWindows.audioOutput == Windows.AudioOutput.FacebookAudio360)
  492. {
  493. _controlInterface.SetAudioChannelMode(_optionsWindows.audio360ChannelMode);
  494. }
  495. else
  496. {
  497. _controlInterface.SetAudioChannelMode(Audio360ChannelMode.INVALID);
  498. }
  499. #elif (!UNITY_EDITOR && UNITY_WSA_10_0)
  500. if (_optionsWindowsUWP.audioOutput == WindowsUWP.AudioOutput.FacebookAudio360)
  501. {
  502. _controlInterface.SetAudioChannelMode(_optionsWindowsUWP.audio360ChannelMode);
  503. }
  504. else
  505. {
  506. _controlInterface.SetAudioChannelMode(Audio360ChannelMode.INVALID);
  507. }
  508. #endif
  509. PlatformOptions options = GetCurrentPlatformOptions();
  510. bool startWithHighestBitrate = false;
  511. if (options != null)
  512. {
  513. startWithHighestBitrate = options.StartWithHighestBandwidth();
  514. }
  515. SetLoadOptions();
  516. if (!_controlInterface.OpenMedia(fullPath, fileOffset, customHttpHeaders, mediaHints, (int)_forceFileFormat, startWithHighestBitrate))
  517. {
  518. Debug.LogError("[AVProVideo] Failed to open " + fullPath, this);
  519. }
  520. else
  521. {
  522. SetPlaybackOptions();
  523. result = true;
  524. StartRenderCoroutine();
  525. }
  526. }
  527. }
  528. else
  529. {
  530. Debug.LogError("[AVProVideo] No file path specified", this);
  531. }
  532. }
  533. return result;
  534. }
  535. private void SetLoadOptions()
  536. {
  537. // On some platforms we can update the loading options without having to recreate the player
  538. #if !AVPROVIDEO_FORCE_NULL_MEDIAPLAYER
  539. #if (UNITY_EDITOR_OSX && UNITY_IOS) || (!UNITY_EDITOR && UNITY_IOS)
  540. #elif (UNITY_EDITOR_OSX && UNITY_TVOS) || (!UNITY_EDITOR && UNITY_TVOS)
  541. #elif (UNITY_EDITOR_OSX || (!UNITY_EDITOR && UNITY_STANDALONE_OSX))
  542. #elif (UNITY_EDITOR_WIN) || (!UNITY_EDITOR && UNITY_STANDALONE_WIN)
  543. #elif (!UNITY_EDITOR && UNITY_WSA_10_0)
  544. #elif (!UNITY_EDITOR && UNITY_ANDROID)
  545. #elif (!UNITY_EDITOR && UNITY_WEBGL)
  546. ((WebGLMediaPlayer)_baseMediaPlayer).SetOptions(_optionsWebGL);
  547. #endif
  548. #endif
  549. // Encryption support
  550. PlatformOptions options = GetCurrentPlatformOptions();
  551. if (options != null)
  552. {
  553. _controlInterface.SetKeyServerAuthToken(options.GetKeyServerAuthToken());
  554. //_controlInterface.SetKeyServerURL(options.GetKeyServerURL());
  555. _controlInterface.SetOverrideDecryptionKey(options.GetOverrideDecryptionKey());
  556. }
  557. }
  558. private void SetPlaybackOptions()
  559. {
  560. // Set playback options
  561. if (_controlInterface != null)
  562. {
  563. _controlInterface.SetLooping(_loop);
  564. _controlInterface.SetPlaybackRate(_playbackRate);
  565. _controlInterface.SetVolume(_audioVolume);
  566. _controlInterface.SetBalance(_audioBalance);
  567. #if !UNITY_EDITOR
  568. _controlInterface.MuteAudio(_audioMuted);
  569. #else
  570. _controlInterface.MuteAudio(_audioMuted || UnityEditor.EditorUtility.audioMasterMute);
  571. #endif
  572. _controlInterface.SetTextureProperties(_textureFilterMode, _textureWrapMode, _textureAnisoLevel);
  573. }
  574. }
  575. public void CloseMedia()
  576. {
  577. // Close the media file
  578. if (_controlInterface != null)
  579. {
  580. if (_events != null && _isMediaOpened && _events.HasListeners() && IsHandleEvent(MediaPlayerEvent.EventType.Closing))
  581. {
  582. _events.Invoke(this, MediaPlayerEvent.EventType.Closing, ErrorCode.None);
  583. }
  584. _autoPlayOnStartTriggered = false;
  585. _isMediaOpened = false;
  586. ResetEvents();
  587. if (_loadSubtitlesRoutine != null)
  588. {
  589. StopCoroutine(_loadSubtitlesRoutine);
  590. _loadSubtitlesRoutine = null;
  591. }
  592. _controlInterface.CloseMedia();
  593. }
  594. if (_resampler != null)
  595. {
  596. _resampler.Reset();
  597. }
  598. StopRenderCoroutine();
  599. }
  600. public void RewindPrerollPause()
  601. {
  602. PlatformOptionsWindows.pauseOnPrerollComplete = true;
  603. if (BufferedDisplay != null)
  604. {
  605. BufferedDisplay.SetBufferedDisplayOptions(true);
  606. }
  607. Rewind(false);
  608. Play();
  609. }
  610. public virtual void Play()
  611. {
  612. if (_controlInterface != null && _controlInterface.CanPlay())
  613. {
  614. _controlInterface.Play();
  615. // Mark this event as done because it's irrelevant once playback starts
  616. _eventFired_ReadyToPlay = true;
  617. }
  618. else
  619. {
  620. // Can't play, perhaps it's still loading? Queuing play using _autoPlayOnStart to play after loading
  621. _autoPlayOnStart = true;
  622. _autoPlayOnStartTriggered = false;
  623. }
  624. }
  625. public virtual void Pause()
  626. {
  627. if (_controlInterface != null && _controlInterface.IsPlaying())
  628. {
  629. _controlInterface.Pause();
  630. }
  631. _wasPlayingOnPause = false;
  632. #if AVPROVIDEO_BETA_SUPPORT_TIMESCALE
  633. _timeScaleIsControlling = false;
  634. #endif
  635. }
  636. public void Stop()
  637. {
  638. if (_controlInterface != null)
  639. {
  640. _controlInterface.Stop();
  641. }
  642. #if AVPROVIDEO_BETA_SUPPORT_TIMESCALE
  643. _timeScaleIsControlling = false;
  644. #endif
  645. }
  646. public void Rewind(bool pause)
  647. {
  648. if (_controlInterface != null)
  649. {
  650. if (pause)
  651. {
  652. Pause();
  653. }
  654. _controlInterface.Rewind();
  655. }
  656. }
  657. public void SeekToLiveTime(double offset = 0.0)
  658. {
  659. if (_controlInterface != null)
  660. {
  661. double liveTime = _controlInterface.GetBufferedTimes().MaxTime;
  662. if (liveTime > 0.0)
  663. {
  664. _controlInterface.Seek(liveTime - offset);
  665. }
  666. }
  667. }
  668. #if UNITY_EDITOR && AVPROVIDEO_SUPPORT_LIVEEDITMODE
  669. public bool EditorUpdate()
  670. {
  671. if (_playerInterface != null)
  672. {
  673. Update();
  674. _playerInterface.Render();
  675. return true;
  676. }
  677. return false;
  678. }
  679. #endif
  680. protected virtual void Update()
  681. {
  682. if (_controlInterface != null)
  683. {
  684. // Auto start the playback
  685. if (_isMediaOpened && _autoPlayOnStart && !_autoPlayOnStartTriggered && _controlInterface.CanPlay())
  686. {
  687. _autoPlayOnStartTriggered = true;
  688. Play();
  689. }
  690. if (Application.isPlaying)
  691. {
  692. if (_renderingCoroutine == null && _controlInterface.CanPlay())
  693. {
  694. StartRenderCoroutine();
  695. }
  696. }
  697. if (_subtitlesInterface != null && _queueSubtitlePath != null && !string.IsNullOrEmpty(_queueSubtitlePath.Path))
  698. {
  699. EnableSubtitles(_queueSubtitlePath);
  700. _queueSubtitlePath = null;
  701. }
  702. #if AVPROVIDEO_BETA_SUPPORT_TIMESCALE
  703. UpdateTimeScale();
  704. #endif
  705. UpdateAudioHeadTransform();
  706. UpdateAudioFocus();
  707. _playerInterface.Update();
  708. // Render (done in co-routine)
  709. //_playerInterface.Render();
  710. UpdateErrors();
  711. UpdateEvents();
  712. _playerInterface.EndUpdate();
  713. }
  714. #if UNITY_EDITOR
  715. CheckEditorAudioMute();
  716. #endif
  717. }
  718. private void LateUpdate()
  719. {
  720. UpdateResampler();
  721. }
  722. private void UpdateResampler()
  723. {
  724. #if !UNITY_WEBGL
  725. if (_useResampler)
  726. {
  727. if (_resampler == null)
  728. {
  729. _resampler = new Resampler(this, gameObject.name, _resampleBufferSize, _resampleMode);
  730. }
  731. }
  732. #else
  733. _useResampler = false;
  734. #endif
  735. if (_resampler != null)
  736. {
  737. _resampler.Update();
  738. _resampler.UpdateTimestamp();
  739. }
  740. }
  741. void OnEnable()
  742. {
  743. if (_controlInterface != null && _wasPlayingOnPause)
  744. {
  745. _autoPlayOnStart = true;
  746. _autoPlayOnStartTriggered = false;
  747. _wasPlayingOnPause = false;
  748. }
  749. if(_playerInterface != null)
  750. {
  751. _playerInterface.OnEnable();
  752. StartRenderCoroutine();
  753. }
  754. }
  755. void OnDisable()
  756. {
  757. if (_controlInterface != null)
  758. {
  759. if (_controlInterface.IsPlaying())
  760. {
  761. Pause();
  762. // Force an update to ensure the player state is synchronised with the plugin
  763. Update();
  764. // Needs to follow Pause() otherwise it will be reset.
  765. _wasPlayingOnPause = true;
  766. }
  767. }
  768. StopRenderCoroutine();
  769. }
  770. protected virtual void OnDestroy()
  771. {
  772. CloseMedia();
  773. _baseMediaPlayer = null;
  774. _controlInterface = null;
  775. _textureInterface = null;
  776. _infoInterface = null;
  777. _playerInterface = null;
  778. _subtitlesInterface = null;
  779. _cacheInterface = null;
  780. _bufferedDisplayInterface = null;
  781. _videoTracksInterface = null;
  782. _audioTracksInterface = null;
  783. _textTracksInterface = null;
  784. if (_disposeInterface != null)
  785. {
  786. _disposeInterface.Dispose();
  787. _disposeInterface = null;
  788. }
  789. if (_resampler != null)
  790. {
  791. _resampler.Release();
  792. _resampler = null;
  793. }
  794. // TODO: possible bug if MediaPlayers are created and destroyed manually (instantiated), OnApplicationQuit won't be called!
  795. }
  796. public void ForceDispose()
  797. {
  798. OnDisable();
  799. OnDestroy();
  800. }
  801. #if UNITY_EDITOR
  802. public static void EditorAllPlayersDispose()
  803. {
  804. AllPlayersDispose();
  805. }
  806. #endif
  807. private static void AllPlayersDispose()
  808. {
  809. // Clean up any open media players
  810. MediaPlayer[] players = Resources.FindObjectsOfTypeAll<MediaPlayer>();
  811. if (players != null && players.Length > 0)
  812. {
  813. for (int i = 0; i < players.Length; i++)
  814. {
  815. players[i].ForceDispose();
  816. }
  817. }
  818. }
  819. void OnApplicationQuit()
  820. {
  821. if (s_GlobalStartup)
  822. {
  823. Helper.LogInfo("Shutdown");
  824. AllPlayersDispose();
  825. #if UNITY_EDITOR
  826. #if UNITY_EDITOR_WIN
  827. WindowsMediaPlayer.DeinitPlatform();
  828. WindowsRtMediaPlayer.DeinitPlatform();
  829. #endif
  830. #else
  831. #if (UNITY_STANDALONE_WIN)
  832. WindowsMediaPlayer.DeinitPlatform();
  833. WindowsRtMediaPlayer.DeinitPlatform();
  834. #elif (UNITY_ANDROID)
  835. AndroidMediaPlayer.DeinitPlatform();
  836. #endif
  837. #endif
  838. s_GlobalStartup = false;
  839. }
  840. }
  841. #region Rendering Coroutine
  842. private void StartRenderCoroutine()
  843. {
  844. if (_renderingCoroutine == null)
  845. {
  846. // Use the method instead of the method name string to prevent garbage
  847. _renderingCoroutine = StartCoroutine(FinalRenderCapture());
  848. }
  849. }
  850. private void StopRenderCoroutine()
  851. {
  852. if (_renderingCoroutine != null)
  853. {
  854. StopCoroutine(_renderingCoroutine);
  855. _renderingCoroutine = null;
  856. }
  857. }
  858. private IEnumerator FinalRenderCapture()
  859. {
  860. // Preallocate the YieldInstruction to prevent garbage
  861. YieldInstruction wait = new WaitForEndOfFrame();
  862. while (Application.isPlaying)
  863. {
  864. // NOTE: in editor, if the game view isn't visible then WaitForEndOfFrame will never complete
  865. yield return wait;
  866. if (this.enabled)
  867. {
  868. if (_playerInterface != null)
  869. {
  870. _playerInterface.Render();
  871. }
  872. }
  873. }
  874. }
  875. #endregion // Rendering Coroutine
  876. #region Platform and Path
  877. public static Platform GetPlatform()
  878. {
  879. Platform result = Platform.Unknown;
  880. // Setup for running in the editor (Either OSX, Windows or Linux)
  881. #if UNITY_EDITOR
  882. #if (UNITY_EDITOR_OSX && UNITY_EDITOR_64)
  883. result = Platform.MacOSX;
  884. #elif UNITY_EDITOR_WIN
  885. result = Platform.Windows;
  886. #endif
  887. #else
  888. // Setup for running builds
  889. #if (UNITY_STANDALONE_WIN)
  890. result = Platform.Windows;
  891. #elif (UNITY_STANDALONE_OSX)
  892. result = Platform.MacOSX;
  893. #elif (UNITY_IPHONE || UNITY_IOS)
  894. result = Platform.iOS;
  895. #elif (UNITY_TVOS)
  896. result = Platform.tvOS;
  897. #elif (UNITY_ANDROID)
  898. result = Platform.Android;
  899. #elif (UNITY_WSA_10_0)
  900. result = Platform.WindowsUWP;
  901. #elif (UNITY_WEBGL)
  902. result = Platform.WebGL;
  903. #endif
  904. #endif
  905. return result;
  906. }
  907. public PlatformOptions GetCurrentPlatformOptions()
  908. {
  909. PlatformOptions result = null;
  910. #if UNITY_EDITOR
  911. #if (UNITY_EDITOR_OSX && UNITY_EDITOR_64)
  912. result = _optionsMacOSX;
  913. #elif UNITY_EDITOR_WIN
  914. result = _optionsWindows;
  915. #endif
  916. #else
  917. // Setup for running builds
  918. #if (UNITY_STANDALONE_WIN)
  919. result = _optionsWindows;
  920. #elif (UNITY_STANDALONE_OSX)
  921. result = _optionsMacOSX;
  922. #elif (UNITY_IPHONE || UNITY_IOS)
  923. result = _optionsIOS;
  924. #elif (UNITY_TVOS)
  925. result = _optionsTVOS;
  926. #elif (UNITY_ANDROID)
  927. result = _optionsAndroid;
  928. #elif (UNITY_WSA_10_0)
  929. result = _optionsWindowsUWP;
  930. #elif (UNITY_WEBGL)
  931. result = _optionsWebGL;
  932. #endif
  933. #endif
  934. return result;
  935. }
  936. #if UNITY_EDITOR
  937. public PlatformOptions GetPlatformOptions(Platform platform)
  938. {
  939. PlatformOptions result = null;
  940. switch (platform)
  941. {
  942. case Platform.Windows:
  943. result = _optionsWindows;
  944. break;
  945. case Platform.MacOSX:
  946. result = _optionsMacOSX;
  947. break;
  948. case Platform.Android:
  949. result = _optionsAndroid;
  950. break;
  951. case Platform.iOS:
  952. result = _optionsIOS;
  953. break;
  954. case Platform.tvOS:
  955. result = _optionsTVOS;
  956. break;
  957. case Platform.WindowsUWP:
  958. result = _optionsWindowsUWP;
  959. break;
  960. case Platform.WebGL:
  961. result = _optionsWebGL;
  962. break;
  963. }
  964. return result;
  965. }
  966. public static string GetPlatformOptionsVariable(Platform platform)
  967. {
  968. string result = string.Empty;
  969. switch (platform)
  970. {
  971. case Platform.Windows:
  972. result = "_optionsWindows";
  973. break;
  974. case Platform.MacOSX:
  975. result = "_optionsMacOSX";
  976. break;
  977. case Platform.iOS:
  978. result = "_optionsIOS";
  979. break;
  980. case Platform.tvOS:
  981. result = "_optionsTVOS";
  982. break;
  983. case Platform.Android:
  984. result = "_optionsAndroid";
  985. break;
  986. case Platform.WindowsUWP:
  987. result = "_optionsWindowsUWP";
  988. break;
  989. case Platform.WebGL:
  990. result = "_optionsWebGL";
  991. break;
  992. }
  993. return result;
  994. }
  995. #endif
  996. private string GetPlatformVideoApiString()
  997. {
  998. string result = string.Empty;
  999. #if UNITY_EDITOR
  1000. #if UNITY_EDITOR_OSX
  1001. #elif UNITY_EDITOR_WIN
  1002. result = _optionsWindows.videoApi.ToString();
  1003. #elif UNITY_EDITOR_LINUX
  1004. #endif
  1005. #else
  1006. #if UNITY_STANDALONE_WIN
  1007. result = _optionsWindows.videoApi.ToString();
  1008. #elif UNITY_WSA_10_0
  1009. result = _optionsWindowsUWP.videoApi.ToString();
  1010. #elif UNITY_ANDROID
  1011. result = _optionsAndroid.videoApi.ToString();
  1012. #endif
  1013. #endif
  1014. return result;
  1015. }
  1016. private long GetPlatformFileOffset()
  1017. {
  1018. long result = 0;
  1019. #if UNITY_EDITOR
  1020. #if UNITY_EDITOR_OSX
  1021. #elif UNITY_EDITOR_WIN
  1022. #elif UNITY_EDITOR_LINUX
  1023. #endif
  1024. #else
  1025. #if UNITY_ANDROID
  1026. result = _optionsAndroid.fileOffset;
  1027. #endif
  1028. #endif
  1029. return result;
  1030. }
  1031. private string GetPlatformHttpHeadersAsString()
  1032. {
  1033. string result = null;
  1034. #if UNITY_EDITOR
  1035. #if UNITY_EDITOR_OSX
  1036. result = _optionsMacOSX.httpHeaders.ToValidatedString();
  1037. #elif UNITY_EDITOR_WIN
  1038. result = _optionsWindows.httpHeaders.ToValidatedString();
  1039. #elif UNITY_EDITOR_LINUX
  1040. #endif
  1041. #else
  1042. #if UNITY_STANDALONE_OSX
  1043. result = _optionsMacOSX.httpHeaders.ToValidatedString();
  1044. #elif UNITY_STANDALONE_WIN
  1045. result = _optionsWindows.httpHeaders.ToValidatedString();
  1046. #elif UNITY_WSA_10_0
  1047. result = _optionsWindowsUWP.httpHeaders.ToValidatedString();
  1048. #elif UNITY_IOS || UNITY_IPHONE
  1049. result = _optionsIOS.httpHeaders.ToValidatedString();
  1050. #elif UNITY_TVOS
  1051. result = _optionsTVOS.httpHeaders.ToValidatedString();
  1052. #elif UNITY_ANDROID
  1053. result = _optionsAndroid.httpHeaders.ToValidatedString();
  1054. #elif UNITY_WEBGL
  1055. #endif
  1056. #endif
  1057. if (!string.IsNullOrEmpty(result))
  1058. {
  1059. result = result.Trim();
  1060. }
  1061. string globalHeaders = _httpHeaders.ToValidatedString();
  1062. if (!string.IsNullOrEmpty(globalHeaders))
  1063. {
  1064. result += globalHeaders;
  1065. result = result.Trim();
  1066. }
  1067. return result;
  1068. }
  1069. private string GetResolvedFilePath(string filePath, MediaPathType fileLocation)
  1070. {
  1071. string result = string.Empty;
  1072. result = Helper.GetFilePath(filePath, fileLocation);
  1073. #if (UNITY_EDITOR_WIN || (!UNITY_EDITOR && UNITY_STANDALONE_WIN))
  1074. if (result.Length > 200 && !result.Contains("://"))
  1075. {
  1076. result = Helper.ConvertLongPathToShortDOS83Path(result);
  1077. }
  1078. #endif
  1079. return result;
  1080. }
  1081. #endregion // Platform and Path
  1082. #region Create MediaPlayers
  1083. #if (UNITY_EDITOR_WIN) || (!UNITY_EDITOR && UNITY_STANDALONE_WIN)
  1084. private static BaseMediaPlayer CreateMediaPlayer(OptionsWindows options)
  1085. {
  1086. BaseMediaPlayer result = null;
  1087. if (options.videoApi == Windows.VideoApi.WinRT)
  1088. {
  1089. if (WindowsRtMediaPlayer.InitialisePlatform())
  1090. {
  1091. result = new WindowsRtMediaPlayer(options);
  1092. }
  1093. else
  1094. {
  1095. Debug.LogWarning(string.Format("[AVProVideo] Failed to initialise WinRT API - platform {0} may not support it. Trying another video API...", SystemInfo.operatingSystem));
  1096. }
  1097. }
  1098. if (result == null)
  1099. {
  1100. if (WindowsMediaPlayer.InitialisePlatform())
  1101. {
  1102. result = new WindowsMediaPlayer(options);
  1103. }
  1104. }
  1105. return result;
  1106. }
  1107. #endif
  1108. #if (!UNITY_EDITOR && UNITY_WSA_10_0)
  1109. private static BaseMediaPlayer CreateMediaPlayer(OptionsWindowsUWP options)
  1110. {
  1111. BaseMediaPlayer result = null;
  1112. if (options.videoApi == WindowsUWP.VideoApi.WinRT)
  1113. {
  1114. if (WindowsRtMediaPlayer.InitialisePlatform())
  1115. {
  1116. result = new WindowsRtMediaPlayer(options);
  1117. }
  1118. else
  1119. {
  1120. Debug.LogWarning(string.Format("[AVProVideo] Failed to initialise WinRT API - platform {0} may not support it. Trying another video API...", SystemInfo.operatingSystem));
  1121. }
  1122. }
  1123. if (result == null)
  1124. {
  1125. if (WindowsMediaPlayer.InitialisePlatform())
  1126. {
  1127. result = new WindowsMediaPlayer(options);
  1128. }
  1129. }
  1130. return result;
  1131. }
  1132. #endif
  1133. #if (!UNITY_EDITOR && UNITY_ANDROID)
  1134. private static BaseMediaPlayer CreateMediaPlayer(OptionsAndroid options)
  1135. {
  1136. BaseMediaPlayer result = null;
  1137. // Initialise platform (also unpacks videos from StreamingAsset folder (inside a jar), to the persistent data path)
  1138. if (AndroidMediaPlayer.InitialisePlatform())
  1139. {
  1140. result = new AndroidMediaPlayer(options);
  1141. }
  1142. return result;
  1143. }
  1144. #endif
  1145. #if (UNITY_EDITOR_OSX) || (!UNITY_EDITOR && (UNITY_STANDALONE_OSX || UNITY_IPHONE || UNITY_IOS || UNITY_TVOS))
  1146. private static BaseMediaPlayer CreateMediaPlayer(OptionsApple options)
  1147. {
  1148. AppleMediaPlayer mediaPlayer = new AppleMediaPlayer(options);
  1149. return mediaPlayer;
  1150. }
  1151. #endif
  1152. #if (!UNITY_EDITOR && UNITY_WEBGL)
  1153. private static BaseMediaPlayer CreateMediaPlayer(OptionsWebGL options)
  1154. {
  1155. BaseMediaPlayer result = null;
  1156. if (WebGLMediaPlayer.InitialisePlatform())
  1157. {
  1158. result = new WebGLMediaPlayer(options);
  1159. }
  1160. return result;
  1161. }
  1162. #endif
  1163. private static BaseMediaPlayer CreateMediaPlayerNull()
  1164. {
  1165. return new NullMediaPlayer();
  1166. }
  1167. public virtual BaseMediaPlayer CreateMediaPlayer()
  1168. {
  1169. BaseMediaPlayer mediaPlayer = null;
  1170. #if !AVPROVIDEO_FORCE_NULL_MEDIAPLAYER
  1171. #if (UNITY_EDITOR_OSX && UNITY_IOS) || (!UNITY_EDITOR && UNITY_IOS)
  1172. mediaPlayer = CreateMediaPlayer(_optionsIOS);
  1173. #elif (UNITY_EDITOR_OSX && UNITY_TVOS) || (!UNITY_EDITOR && UNITY_TVOS)
  1174. mediaPlayer = CreateMediaPlayer(_optionsTVOS);
  1175. #elif (UNITY_EDITOR_OSX || (!UNITY_EDITOR && UNITY_STANDALONE_OSX))
  1176. mediaPlayer = CreateMediaPlayer(_optionsMacOSX);
  1177. #elif (UNITY_EDITOR_WIN) || (!UNITY_EDITOR && UNITY_STANDALONE_WIN)
  1178. mediaPlayer = CreateMediaPlayer(_optionsWindows);
  1179. #elif (!UNITY_EDITOR && UNITY_WSA_10_0)
  1180. mediaPlayer = CreateMediaPlayer(_optionsWindowsUWP);
  1181. #elif (!UNITY_EDITOR && UNITY_ANDROID)
  1182. mediaPlayer = CreateMediaPlayer(_optionsAndroid);
  1183. #elif (!UNITY_EDITOR && UNITY_WEBGL)
  1184. mediaPlayer = CreateMediaPlayer(_optionsWebGL);
  1185. #endif
  1186. #endif
  1187. // Fallback
  1188. if (mediaPlayer == null)
  1189. {
  1190. Debug.LogError(string.Format("[AVProVideo] Not supported on this platform {0} {1} {2} {3}. Using null media player!", Application.platform, SystemInfo.deviceModel, SystemInfo.processorType, SystemInfo.operatingSystem));
  1191. mediaPlayer = CreateMediaPlayerNull();
  1192. }
  1193. return mediaPlayer;
  1194. }
  1195. #endregion // Create MediaPlayers
  1196. private void UpdateAudioFocus()
  1197. {
  1198. // TODO: we could use gizmos to draw the focus area
  1199. _controlInterface.SetAudioFocusEnabled(_audioFocusEnabled);
  1200. _controlInterface.SetAudioFocusProperties(_audioFocusOffLevelDB, _audioFocusWidthDegrees);
  1201. _controlInterface.SetAudioFocusRotation(_audioFocusTransform == null ? Quaternion.identity : _audioFocusTransform.rotation);
  1202. }
  1203. private void UpdateAudioHeadTransform()
  1204. {
  1205. if (_audioHeadTransform != null)
  1206. {
  1207. _controlInterface.SetAudioHeadRotation(_audioHeadTransform.rotation);
  1208. }
  1209. else
  1210. {
  1211. _controlInterface.ResetAudioHeadRotation();
  1212. }
  1213. }
  1214. private void UpdateErrors()
  1215. {
  1216. ErrorCode errorCode = _controlInterface.GetLastError();
  1217. if (ErrorCode.None != errorCode)
  1218. {
  1219. Debug.LogError("[AVProVideo] Error: " + Helper.GetErrorMessage(errorCode));
  1220. // Display additional information for load failures
  1221. if (ErrorCode.LoadFailed == errorCode)
  1222. {
  1223. #if !UNITY_EDITOR && UNITY_ANDROID
  1224. // TODO: Update this to handle case where media is MediaReference
  1225. if (_mediaPath.Path.ToLower().Contains("http://"))
  1226. {
  1227. Debug.LogError("Android 8 and above require HTTPS by default, change to HTTPS or enable ClearText in the AndroidManifest.xml");
  1228. }
  1229. #endif
  1230. }
  1231. if (_events != null && _events.HasListeners() && IsHandleEvent(MediaPlayerEvent.EventType.Error))
  1232. {
  1233. _events.Invoke(this, MediaPlayerEvent.EventType.Error, errorCode);
  1234. }
  1235. }
  1236. }
  1237. public bool IsUsingAndroidOESPath()
  1238. {
  1239. // Android OES mode is not available in the trial
  1240. bool result = (PlatformOptionsAndroid.useFastOesPath && !s_TrialVersion);
  1241. #if (UNITY_EDITOR || !UNITY_ANDROID)
  1242. result = false;
  1243. #endif
  1244. return result;
  1245. }
  1246. #region Save Frame To PNG
  1247. #if UNITY_EDITOR || (!UNITY_EDITOR && (UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX))
  1248. [ContextMenu("Save Frame To PNG")]
  1249. public void SaveFrameToPng()
  1250. {
  1251. Texture2D frame = ExtractFrame(null);
  1252. if (frame != null)
  1253. {
  1254. byte[] imageBytes = frame.EncodeToPNG();
  1255. if (imageBytes != null)
  1256. {
  1257. string timecode = Mathf.FloorToInt((float)(Control.GetCurrentTime() * 1000.0)).ToString("D8");
  1258. System.IO.File.WriteAllBytes("frame-" + timecode + ".png", imageBytes);
  1259. }
  1260. Destroy(frame);
  1261. }
  1262. }
  1263. [ContextMenu("Save Frame To EXR")]
  1264. public void SaveFrameToExr()
  1265. {
  1266. Texture frame = (Texture)TextureProducer.GetTexture(0);
  1267. if (frame != null)
  1268. {
  1269. RenderTexture rt = new RenderTexture(frame.width, frame.height, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear);
  1270. rt.Create();
  1271. Graphics.Blit(frame, rt);
  1272. Texture2D frameRead = new Texture2D(frame.width, frame.height, TextureFormat.RGBAFloat, false, true);
  1273. RenderTexture.active = rt;
  1274. frameRead.ReadPixels(new Rect(0f, 0f, frame.width, frame.height), 0, 0, false);
  1275. frameRead.Apply(false, false);
  1276. RenderTexture.active = null;
  1277. byte[] imageBytes = frameRead.EncodeToEXR();
  1278. if (imageBytes != null)
  1279. {
  1280. string timecode = Mathf.FloorToInt((float)(Control.GetCurrentTime() * 1000.0)).ToString("D8");
  1281. System.IO.File.WriteAllBytes("frame-" + timecode + ".exr", imageBytes);
  1282. }
  1283. Destroy(frame);
  1284. Texture2D.Destroy(frameRead);
  1285. RenderTexture.Destroy(rt);
  1286. }
  1287. }
  1288. #endif
  1289. #endregion // Save Frame To PNG
  1290. }
  1291. }