MediaPlayer.cs 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  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; } internal 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. #if AVPRO_FEATURE_VIDEORESOLVE
  223. [SerializeField] bool _useVideoResolve = false;
  224. public bool UseVideoResolve { get { return _useVideoResolve; } set { _useVideoResolve = value; } }
  225. [SerializeField] VideoResolveOptions _videoResolveOptions = VideoResolveOptions.Create();
  226. public VideoResolveOptions VideoResolveOptions { get { return _videoResolveOptions; } set { _videoResolveOptions = value; } }
  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._audioMode == 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. SetPlaybackOptions();
  517. if (_controlInterface.OpenMedia(fullPath, fileOffset, customHttpHeaders, mediaHints, (int)_forceFileFormat, startWithHighestBitrate))
  518. {
  519. StartRenderCoroutine();
  520. result = true;
  521. }
  522. else
  523. {
  524. Debug.LogError("[AVProVideo] Failed to open " + fullPath, this);
  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.BeginRender();
  675. _playerInterface.Render();
  676. return true;
  677. }
  678. return false;
  679. }
  680. #endif
  681. protected virtual void Update()
  682. {
  683. if (_controlInterface != null)
  684. {
  685. // Auto start the playback
  686. if (_isMediaOpened && _autoPlayOnStart && !_autoPlayOnStartTriggered && _controlInterface.CanPlay())
  687. {
  688. _autoPlayOnStartTriggered = true;
  689. Play();
  690. }
  691. if (Application.isPlaying)
  692. {
  693. if (_renderingCoroutine == null && _controlInterface.CanPlay())
  694. {
  695. StartRenderCoroutine();
  696. }
  697. }
  698. if (_subtitlesInterface != null && _queueSubtitlePath != null && !string.IsNullOrEmpty(_queueSubtitlePath.Path))
  699. {
  700. EnableSubtitles(_queueSubtitlePath);
  701. _queueSubtitlePath = null;
  702. }
  703. #if AVPROVIDEO_BETA_SUPPORT_TIMESCALE
  704. UpdateTimeScale();
  705. #endif
  706. UpdateAudioHeadTransform();
  707. UpdateAudioFocus();
  708. _playerInterface.Update();
  709. // Render (done in co-routine)
  710. //_playerInterface.Render();
  711. UpdateErrors();
  712. UpdateEvents();
  713. _playerInterface.EndUpdate();
  714. }
  715. #if UNITY_EDITOR
  716. CheckEditorAudioMute();
  717. #endif
  718. }
  719. private void LateUpdate()
  720. {
  721. UpdateResampler();
  722. // RJT TODO: Better location?
  723. if (_playerInterface != null)
  724. {
  725. _playerInterface.BeginRender();
  726. }
  727. }
  728. private void UpdateResampler()
  729. {
  730. #if !UNITY_WEBGL
  731. if (_useResampler)
  732. {
  733. if (_resampler == null)
  734. {
  735. _resampler = new Resampler(this, gameObject.name, _resampleBufferSize, _resampleMode);
  736. }
  737. }
  738. #else
  739. _useResampler = false;
  740. #endif
  741. if (_resampler != null)
  742. {
  743. _resampler.Update();
  744. _resampler.UpdateTimestamp();
  745. }
  746. }
  747. void OnEnable()
  748. {
  749. if (_controlInterface != null && _wasPlayingOnPause)
  750. {
  751. _autoPlayOnStart = true;
  752. _autoPlayOnStartTriggered = false;
  753. _wasPlayingOnPause = false;
  754. }
  755. if(_playerInterface != null)
  756. {
  757. _playerInterface.OnEnable();
  758. StartRenderCoroutine();
  759. }
  760. }
  761. void OnDisable()
  762. {
  763. if (_controlInterface != null)
  764. {
  765. if (_controlInterface.IsPlaying())
  766. {
  767. Pause();
  768. // Force an update to ensure the player state is synchronised with the plugin
  769. Update();
  770. // Needs to follow Pause() otherwise it will be reset.
  771. _wasPlayingOnPause = true;
  772. }
  773. }
  774. StopRenderCoroutine();
  775. }
  776. protected virtual void OnDestroy()
  777. {
  778. CloseMedia();
  779. _baseMediaPlayer = null;
  780. _controlInterface = null;
  781. _textureInterface = null;
  782. _infoInterface = null;
  783. _playerInterface = null;
  784. _subtitlesInterface = null;
  785. _cacheInterface = null;
  786. _bufferedDisplayInterface = null;
  787. _videoTracksInterface = null;
  788. _audioTracksInterface = null;
  789. _textTracksInterface = null;
  790. if (_disposeInterface != null)
  791. {
  792. _disposeInterface.Dispose();
  793. _disposeInterface = null;
  794. }
  795. if (_resampler != null)
  796. {
  797. _resampler.Release();
  798. _resampler = null;
  799. }
  800. // TODO: possible bug if MediaPlayers are created and destroyed manually (instantiated), OnApplicationQuit won't be called!
  801. }
  802. public void ForceDispose()
  803. {
  804. OnDisable();
  805. OnDestroy();
  806. }
  807. #if UNITY_EDITOR
  808. public static void EditorAllPlayersDispose()
  809. {
  810. AllPlayersDispose();
  811. }
  812. #endif
  813. private static void AllPlayersDispose()
  814. {
  815. // Clean up any open media players
  816. MediaPlayer[] players = Resources.FindObjectsOfTypeAll<MediaPlayer>();
  817. if (players != null && players.Length > 0)
  818. {
  819. for (int i = 0; i < players.Length; i++)
  820. {
  821. players[i].ForceDispose();
  822. }
  823. }
  824. }
  825. void OnApplicationQuit()
  826. {
  827. if (s_GlobalStartup)
  828. {
  829. Helper.LogInfo("Shutdown");
  830. AllPlayersDispose();
  831. #if UNITY_EDITOR
  832. #if UNITY_EDITOR_WIN
  833. WindowsMediaPlayer.DeinitPlatform();
  834. WindowsRtMediaPlayer.DeinitPlatform();
  835. #endif
  836. #else
  837. #if (UNITY_STANDALONE_WIN)
  838. WindowsMediaPlayer.DeinitPlatform();
  839. WindowsRtMediaPlayer.DeinitPlatform();
  840. #elif (UNITY_ANDROID)
  841. #if false
  842. AndroidMediaPlayer.DeinitPlatform();
  843. #endif
  844. #endif
  845. #endif
  846. s_GlobalStartup = false;
  847. }
  848. }
  849. #region Rendering Coroutine
  850. private void StartRenderCoroutine()
  851. {
  852. if (_renderingCoroutine == null)
  853. {
  854. // Use the method instead of the method name string to prevent garbage
  855. _renderingCoroutine = StartCoroutine(FinalRenderCapture());
  856. }
  857. }
  858. private void StopRenderCoroutine()
  859. {
  860. if (_renderingCoroutine != null)
  861. {
  862. StopCoroutine(_renderingCoroutine);
  863. _renderingCoroutine = null;
  864. }
  865. }
  866. private IEnumerator FinalRenderCapture()
  867. {
  868. // Preallocate the YieldInstruction to prevent garbage
  869. YieldInstruction wait = new WaitForEndOfFrame();
  870. while (Application.isPlaying)
  871. {
  872. // NOTE: in editor, if the game view isn't visible then WaitForEndOfFrame will never complete
  873. yield return wait;
  874. if (this.enabled)
  875. {
  876. if (_playerInterface != null)
  877. {
  878. _playerInterface.Render();
  879. }
  880. }
  881. }
  882. }
  883. #endregion // Rendering Coroutine
  884. #region Platform and Path
  885. public static Platform GetPlatform()
  886. {
  887. Platform result = Platform.Unknown;
  888. // Setup for running in the editor (Either OSX, Windows or Linux)
  889. #if UNITY_EDITOR
  890. #if (UNITY_EDITOR_OSX && UNITY_EDITOR_64)
  891. result = Platform.macOS;
  892. #elif UNITY_EDITOR_WIN
  893. result = Platform.Windows;
  894. #endif
  895. #else
  896. // Setup for running builds
  897. #if (UNITY_STANDALONE_WIN)
  898. result = Platform.Windows;
  899. #elif (UNITY_STANDALONE_OSX)
  900. result = Platform.macOS;
  901. #elif (UNITY_IPHONE || UNITY_IOS)
  902. result = Platform.iOS;
  903. #elif (UNITY_TVOS)
  904. result = Platform.tvOS;
  905. #elif (UNITY_VISIONOS)
  906. result = Platform.visionOS;
  907. #elif (UNITY_ANDROID)
  908. result = Platform.Android;
  909. #elif (UNITY_WSA_10_0)
  910. result = Platform.WindowsUWP;
  911. #elif (UNITY_WEBGL)
  912. result = Platform.WebGL;
  913. #endif
  914. #endif
  915. return result;
  916. }
  917. public PlatformOptions GetCurrentPlatformOptions()
  918. {
  919. PlatformOptions result = null;
  920. #if UNITY_EDITOR
  921. #if (UNITY_EDITOR_OSX && UNITY_EDITOR_64)
  922. result = _options_macOS;
  923. #elif UNITY_EDITOR_WIN
  924. result = _optionsWindows;
  925. #endif
  926. #else
  927. // Setup for running builds
  928. #if (UNITY_STANDALONE_WIN)
  929. result = _optionsWindows;
  930. #elif (UNITY_STANDALONE_OSX)
  931. result = _options_macOS;
  932. #elif (UNITY_IPHONE || UNITY_IOS)
  933. result = _options_iOS;
  934. #elif (UNITY_TVOS)
  935. result = _options_tvOS;
  936. #elif (UNITY_VISIONOS)
  937. result = _options_visionOS;
  938. #elif (UNITY_ANDROID)
  939. result = _optionsAndroid;
  940. #elif (UNITY_WSA_10_0)
  941. result = _optionsWindowsUWP;
  942. #elif (UNITY_WEBGL)
  943. result = _optionsWebGL;
  944. #endif
  945. #endif
  946. return result;
  947. }
  948. #if UNITY_EDITOR
  949. public PlatformOptions GetPlatformOptions(Platform platform)
  950. {
  951. PlatformOptions result = null;
  952. switch (platform)
  953. {
  954. case Platform.Windows:
  955. result = _optionsWindows;
  956. break;
  957. case Platform.macOS:
  958. result = _options_macOS;
  959. break;
  960. case Platform.Android:
  961. result = _optionsAndroid;
  962. break;
  963. case Platform.iOS:
  964. result = _options_iOS;
  965. break;
  966. case Platform.tvOS:
  967. result = _options_tvOS;
  968. break;
  969. case Platform.visionOS:
  970. result = _options_visionOS;
  971. break;
  972. case Platform.WindowsUWP:
  973. result = _optionsWindowsUWP;
  974. break;
  975. case Platform.WebGL:
  976. result = _optionsWebGL;
  977. break;
  978. }
  979. return result;
  980. }
  981. public static string GetPlatformOptionsVariable(Platform platform)
  982. {
  983. string result = string.Empty;
  984. switch (platform)
  985. {
  986. case Platform.Windows:
  987. result = "_optionsWindows";
  988. break;
  989. case Platform.macOS:
  990. result = "_options_macOS";
  991. break;
  992. case Platform.iOS:
  993. result = "_options_iOS";
  994. break;
  995. case Platform.tvOS:
  996. result = "_options_tvOS";
  997. break;
  998. case Platform.visionOS:
  999. result = "_options_visionOS";
  1000. break;
  1001. case Platform.Android:
  1002. result = "_optionsAndroid";
  1003. break;
  1004. case Platform.WindowsUWP:
  1005. result = "_optionsWindowsUWP";
  1006. break;
  1007. case Platform.WebGL:
  1008. result = "_optionsWebGL";
  1009. break;
  1010. }
  1011. return result;
  1012. }
  1013. #endif
  1014. private string GetPlatformVideoApiString()
  1015. {
  1016. string result = string.Empty;
  1017. #if UNITY_EDITOR
  1018. #if UNITY_EDITOR_OSX
  1019. #elif UNITY_EDITOR_WIN
  1020. result = _optionsWindows.videoApi.ToString();
  1021. #elif UNITY_EDITOR_LINUX
  1022. #endif
  1023. #else
  1024. #if UNITY_STANDALONE_WIN
  1025. result = _optionsWindows.videoApi.ToString();
  1026. #elif UNITY_WSA_10_0
  1027. result = _optionsWindowsUWP.videoApi.ToString();
  1028. #elif UNITY_ANDROID
  1029. result = _optionsAndroid.videoApi.ToString();
  1030. #endif
  1031. #endif
  1032. return result;
  1033. }
  1034. private long GetPlatformFileOffset()
  1035. {
  1036. long result = 0;
  1037. #if UNITY_EDITOR
  1038. #if UNITY_EDITOR_OSX
  1039. #elif UNITY_EDITOR_WIN
  1040. #elif UNITY_EDITOR_LINUX
  1041. #endif
  1042. #else
  1043. #if UNITY_ANDROID
  1044. result = _optionsAndroid.fileOffset;
  1045. #endif
  1046. #endif
  1047. return result;
  1048. }
  1049. private string GetPlatformHttpHeadersAsString()
  1050. {
  1051. string result = null;
  1052. #if UNITY_EDITOR
  1053. #if UNITY_EDITOR_OSX
  1054. result = _options_macOS.httpHeaders.ToValidatedString();
  1055. #elif UNITY_EDITOR_WIN
  1056. result = _optionsWindows.httpHeaders.ToValidatedString();
  1057. #elif UNITY_EDITOR_LINUX
  1058. #endif
  1059. #else
  1060. #if UNITY_STANDALONE_OSX
  1061. result = _options_macOS.httpHeaders.ToValidatedString();
  1062. #elif UNITY_STANDALONE_WIN
  1063. result = _optionsWindows.httpHeaders.ToValidatedString();
  1064. #elif UNITY_WSA_10_0
  1065. result = _optionsWindowsUWP.httpHeaders.ToValidatedString();
  1066. #elif UNITY_IOS || UNITY_IPHONE
  1067. result = _options_iOS.httpHeaders.ToValidatedString();
  1068. #elif UNITY_TVOS
  1069. result = _options_tvOS.httpHeaders.ToValidatedString();
  1070. #elif UNITY_VISIONOS
  1071. result = _options_visionOS.httpHeaders.ToValidatedString();
  1072. #elif UNITY_ANDROID
  1073. result = _optionsAndroid.httpHeaders.ToValidatedString();
  1074. #elif UNITY_WEBGL
  1075. #endif
  1076. #endif
  1077. if (!string.IsNullOrEmpty(result))
  1078. {
  1079. result = result.Trim();
  1080. }
  1081. string globalHeaders = _httpHeaders.ToValidatedString();
  1082. if (!string.IsNullOrEmpty(globalHeaders))
  1083. {
  1084. result += globalHeaders;
  1085. result = result.Trim();
  1086. }
  1087. return result;
  1088. }
  1089. private string GetResolvedFilePath(string filePath, MediaPathType fileLocation)
  1090. {
  1091. string result = string.Empty;
  1092. result = Helper.GetFilePath(filePath, fileLocation);
  1093. #if (UNITY_EDITOR_WIN || (!UNITY_EDITOR && UNITY_STANDALONE_WIN))
  1094. if (result.Length > 200 && !result.Contains("://"))
  1095. {
  1096. result = Helper.ConvertLongPathToShortDOS83Path(result);
  1097. }
  1098. #endif
  1099. return result;
  1100. }
  1101. #endregion // Platform and Path
  1102. #region Create MediaPlayers
  1103. #if (UNITY_EDITOR_WIN) || (!UNITY_EDITOR && UNITY_STANDALONE_WIN)
  1104. private static BaseMediaPlayer CreateMediaPlayer(OptionsWindows options)
  1105. {
  1106. BaseMediaPlayer result = null;
  1107. if (options.videoApi == Windows.VideoApi.WinRT)
  1108. {
  1109. if (WindowsRtMediaPlayer.InitialisePlatform())
  1110. {
  1111. result = new WindowsRtMediaPlayer(options);
  1112. }
  1113. else
  1114. {
  1115. Debug.LogWarning(string.Format("[AVProVideo] Failed to initialise WinRT API - platform {0} may not support it. Trying another video API...", SystemInfo.operatingSystem));
  1116. }
  1117. }
  1118. if (result == null)
  1119. {
  1120. if (WindowsMediaPlayer.InitialisePlatform())
  1121. {
  1122. result = new WindowsMediaPlayer(options);
  1123. }
  1124. }
  1125. return result;
  1126. }
  1127. #endif
  1128. #if (!UNITY_EDITOR && UNITY_WSA_10_0)
  1129. private static BaseMediaPlayer CreateMediaPlayer(OptionsWindowsUWP options)
  1130. {
  1131. BaseMediaPlayer result = null;
  1132. if (options.videoApi == WindowsUWP.VideoApi.WinRT)
  1133. {
  1134. if (WindowsRtMediaPlayer.InitialisePlatform())
  1135. {
  1136. result = new WindowsRtMediaPlayer(options);
  1137. }
  1138. else
  1139. {
  1140. Debug.LogWarning(string.Format("[AVProVideo] Failed to initialise WinRT API - platform {0} may not support it. Trying another video API...", SystemInfo.operatingSystem));
  1141. }
  1142. }
  1143. if (result == null)
  1144. {
  1145. if (WindowsMediaPlayer.InitialisePlatform())
  1146. {
  1147. result = new WindowsMediaPlayer(options);
  1148. }
  1149. }
  1150. return result;
  1151. }
  1152. #endif
  1153. #if (!UNITY_EDITOR && UNITY_ANDROID)
  1154. private static BaseMediaPlayer CreateMediaPlayer(OptionsAndroid options)
  1155. {
  1156. PlatformMediaPlayer mediaPlayer = new PlatformMediaPlayer(options);
  1157. return mediaPlayer;
  1158. }
  1159. #endif
  1160. #if (UNITY_EDITOR_OSX) || (!UNITY_EDITOR && (UNITY_STANDALONE_OSX || UNITY_IPHONE || UNITY_IOS || UNITY_TVOS || UNITY_VISIONOS || UNITY_ANDROID))
  1161. private static BaseMediaPlayer CreateMediaPlayer(OptionsApple options)
  1162. {
  1163. PlatformMediaPlayer mediaPlayer = new PlatformMediaPlayer(options);
  1164. return mediaPlayer;
  1165. }
  1166. #endif
  1167. #if (!UNITY_EDITOR && UNITY_WEBGL)
  1168. private static BaseMediaPlayer CreateMediaPlayer(OptionsWebGL options)
  1169. {
  1170. BaseMediaPlayer result = null;
  1171. if (WebGLMediaPlayer.InitialisePlatform())
  1172. {
  1173. result = new WebGLMediaPlayer(options);
  1174. }
  1175. return result;
  1176. }
  1177. #endif
  1178. private static BaseMediaPlayer CreateMediaPlayerNull()
  1179. {
  1180. return new NullMediaPlayer();
  1181. }
  1182. public virtual BaseMediaPlayer CreateMediaPlayer()
  1183. {
  1184. BaseMediaPlayer mediaPlayer = null;
  1185. #if !AVPROVIDEO_FORCE_NULL_MEDIAPLAYER
  1186. #if (UNITY_EDITOR_OSX && UNITY_IOS) || (!UNITY_EDITOR && UNITY_IOS)
  1187. mediaPlayer = CreateMediaPlayer(_options_iOS);
  1188. #elif (UNITY_EDITOR_OSX && UNITY_TVOS) || (!UNITY_EDITOR && UNITY_TVOS)
  1189. mediaPlayer = CreateMediaPlayer(_options_tvOS);
  1190. #elif (UNITY_EDITOR_OSX && UNITY_VISIONOS) || (!UNITY_EDITOR && UNITY_VISIONOS)
  1191. mediaPlayer = CreateMediaPlayer(_options_visionOS);
  1192. #elif (UNITY_EDITOR_OSX || (!UNITY_EDITOR && UNITY_STANDALONE_OSX))
  1193. mediaPlayer = CreateMediaPlayer(_options_macOS);
  1194. #elif (UNITY_EDITOR_WIN) || (!UNITY_EDITOR && UNITY_STANDALONE_WIN)
  1195. mediaPlayer = CreateMediaPlayer(_optionsWindows);
  1196. #elif (!UNITY_EDITOR && UNITY_WSA_10_0)
  1197. mediaPlayer = CreateMediaPlayer(_optionsWindowsUWP);
  1198. #elif (!UNITY_EDITOR && UNITY_ANDROID)
  1199. mediaPlayer = CreateMediaPlayer(_optionsAndroid);
  1200. #elif (!UNITY_EDITOR && UNITY_WEBGL)
  1201. mediaPlayer = CreateMediaPlayer(_optionsWebGL);
  1202. #endif
  1203. #endif
  1204. // Fallback
  1205. if (mediaPlayer == null)
  1206. {
  1207. 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));
  1208. mediaPlayer = CreateMediaPlayerNull();
  1209. }
  1210. return mediaPlayer;
  1211. }
  1212. #endregion // Create MediaPlayers
  1213. private void UpdateAudioFocus()
  1214. {
  1215. // TODO: we could use gizmos to draw the focus area
  1216. _controlInterface.SetAudioFocusEnabled(_audioFocusEnabled);
  1217. _controlInterface.SetAudioFocusProperties(_audioFocusOffLevelDB, _audioFocusWidthDegrees);
  1218. _controlInterface.SetAudioFocusRotation(_audioFocusTransform == null ? Quaternion.identity : _audioFocusTransform.rotation);
  1219. }
  1220. private void UpdateAudioHeadTransform()
  1221. {
  1222. if (_audioHeadTransform != null)
  1223. {
  1224. _controlInterface.SetAudioHeadRotation(_audioHeadTransform.rotation);
  1225. }
  1226. else
  1227. {
  1228. _controlInterface.ResetAudioHeadRotation();
  1229. }
  1230. }
  1231. private void UpdateErrors()
  1232. {
  1233. ErrorCode errorCode = _controlInterface.GetLastError();
  1234. if (ErrorCode.None != errorCode)
  1235. {
  1236. Debug.LogError("[AVProVideo] Error: " + Helper.GetErrorMessage(errorCode));
  1237. // Display additional information for load failures
  1238. if (ErrorCode.LoadFailed == errorCode)
  1239. {
  1240. #if !UNITY_EDITOR && UNITY_ANDROID
  1241. // TODO: Update this to handle case where media is MediaReference
  1242. if (_mediaPath.Path.ToLower().Contains("http://"))
  1243. {
  1244. Debug.LogError("Android 8 and above require HTTPS by default, change to HTTPS or enable ClearText in the AndroidManifest.xml");
  1245. }
  1246. #endif
  1247. }
  1248. if (_events != null && _events.HasListeners() && IsHandleEvent(MediaPlayerEvent.EventType.Error))
  1249. {
  1250. _events.Invoke(this, MediaPlayerEvent.EventType.Error, errorCode);
  1251. }
  1252. }
  1253. }
  1254. public bool IsUsingAndroidOESPath()
  1255. {
  1256. // Android OES mode is not available in the trial
  1257. // bool result = (PlatformOptionsAndroid.useFastOesPath && !s_TrialVersion);
  1258. bool result = ((PlatformOptionsAndroid.textureFormat == MediaPlayer.OptionsAndroid.TextureFormat.YCbCr420_OES) && !s_TrialVersion);
  1259. #if (UNITY_EDITOR || !UNITY_ANDROID)
  1260. result = false;
  1261. #endif
  1262. return result;
  1263. }
  1264. #region Save Frame To PNG
  1265. #if UNITY_EDITOR || (!UNITY_EDITOR && (UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX))
  1266. [ContextMenu("Save Frame To PNG")]
  1267. public void SaveFrameToPng()
  1268. {
  1269. Texture2D frame = ExtractFrame(null);
  1270. if (frame != null)
  1271. {
  1272. byte[] imageBytes = frame.EncodeToPNG();
  1273. if (imageBytes != null)
  1274. {
  1275. string timecode = Mathf.FloorToInt((float)(Control.GetCurrentTime() * 1000.0)).ToString("D8");
  1276. System.IO.File.WriteAllBytes("frame-" + timecode + ".png", imageBytes);
  1277. }
  1278. Destroy(frame);
  1279. }
  1280. }
  1281. [ContextMenu("Save Frame To EXR")]
  1282. public void SaveFrameToExr()
  1283. {
  1284. Texture frame = (Texture)TextureProducer.GetTexture(0);
  1285. if (frame != null)
  1286. {
  1287. RenderTexture rt = new RenderTexture(frame.width, frame.height, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear);
  1288. rt.Create();
  1289. Graphics.Blit(frame, rt);
  1290. Texture2D frameRead = new Texture2D(frame.width, frame.height, TextureFormat.RGBAFloat, false, true);
  1291. RenderTexture.active = rt;
  1292. frameRead.ReadPixels(new Rect(0f, 0f, frame.width, frame.height), 0, 0, false);
  1293. frameRead.Apply(false, false);
  1294. RenderTexture.active = null;
  1295. byte[] imageBytes = frameRead.EncodeToEXR();
  1296. if (imageBytes != null)
  1297. {
  1298. string timecode = Mathf.FloorToInt((float)(Control.GetCurrentTime() * 1000.0)).ToString("D8");
  1299. System.IO.File.WriteAllBytes("frame-" + timecode + ".exr", imageBytes);
  1300. }
  1301. Destroy(frame);
  1302. Texture2D.Destroy(frameRead);
  1303. RenderTexture.Destroy(rt);
  1304. }
  1305. }
  1306. #endif
  1307. #endregion // Save Frame To PNG
  1308. }
  1309. }