BaseMediaPlayer.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. #if UNITY_EDITOR || UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN || UNITY_IOS || UNITY_ANDROID
  2. #define UNITY_PLATFORM_SUPPORTS_LINEAR
  3. #endif
  4. using System;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. //-----------------------------------------------------------------------------
  8. // Copyright 2015-2021 RenderHeads Ltd. All rights reserved.
  9. //-----------------------------------------------------------------------------
  10. namespace RenderHeads.Media.AVProVideo
  11. {
  12. /// <summary>
  13. /// Base class for all platform specific MediaPlayers
  14. /// </summary>
  15. public abstract partial class BaseMediaPlayer : IMediaPlayer, IMediaControl, IMediaInfo, IMediaCache, ITextureProducer, IMediaSubtitles, IVideoTracks, IAudioTracks, ITextTracks, IBufferedDisplay, System.IDisposable
  16. {
  17. public BaseMediaPlayer()
  18. {
  19. InitTracks();
  20. }
  21. public abstract string GetVersion();
  22. public abstract string GetExpectedVersion();
  23. /// <inheritdoc/>
  24. public abstract bool OpenMedia(string path, long offset, string customHttpHeaders, MediaHints mediaHints, int forceFileFormat = 0, bool startWithHighestBitrate = false);
  25. #if NETFX_CORE
  26. /// <inheritdoc/>
  27. public virtual bool OpenMedia(Windows.Storage.Streams.IRandomAccessStream ras, string path, long offset, string customHttpHeaders) { return false; }
  28. #endif
  29. /// <inheritdoc/>
  30. public virtual bool OpenMediaFromBuffer(byte[] buffer) { return false; }
  31. /// <inheritdoc/>
  32. public virtual bool StartOpenMediaFromBuffer(ulong length) { return false; }
  33. /// <inheritdoc/>
  34. public virtual bool AddChunkToMediaBuffer(byte[] chunk, ulong offset, ulong length) { return false; }
  35. /// <inheritdoc/>
  36. public virtual bool EndOpenMediaFromBuffer() { return false; }
  37. /// <inheritdoc/>
  38. public virtual void CloseMedia()
  39. {
  40. #if UNITY_EDITOR
  41. _displayRateLastRealTime = 0f;
  42. #endif
  43. _displayRateTimer = 0f;
  44. _displayRateLastFrameCount = 0;
  45. _displayRate = 0f;
  46. _stallDetectionTimer = 0f;
  47. _stallDetectionFrame = 0;
  48. _lastError = ErrorCode.None;
  49. _textTracks.Clear();
  50. _audioTracks.Clear();
  51. _videoTracks.Clear();
  52. _currentTextCue = null;
  53. _mediaHints = new MediaHints();
  54. }
  55. /// <inheritdoc/>
  56. public abstract void SetLooping(bool looping);
  57. /// <inheritdoc/>
  58. public abstract bool IsLooping();
  59. /// <inheritdoc/>
  60. public abstract bool HasMetaData();
  61. /// <inheritdoc/>
  62. public abstract bool CanPlay();
  63. /// <inheritdoc/>
  64. public abstract void Play();
  65. /// <inheritdoc/>
  66. public abstract void Pause();
  67. /// <inheritdoc/>
  68. public abstract void Stop();
  69. /// <inheritdoc/>
  70. public virtual void Rewind() { SeekFast(0.0); }
  71. /// <inheritdoc/>
  72. public abstract void Seek(double time);
  73. /// <inheritdoc/>
  74. public abstract void SeekFast(double time);
  75. /// <inheritdoc/>
  76. public virtual void SeekWithTolerance(double time, double timeDeltaBefore, double timeDeltaAfter) { Seek(time); }
  77. /// <inheritdoc/>
  78. public abstract double GetCurrentTime();
  79. /// <inheritdoc/>
  80. public virtual DateTime GetProgramDateTime() { return DateTime.MinValue; }
  81. /// <inheritdoc/>
  82. public abstract float GetPlaybackRate();
  83. /// <inheritdoc/>
  84. public abstract void SetPlaybackRate(float rate);
  85. // Basic Properties
  86. /// <inheritdoc/>
  87. public abstract double GetDuration();
  88. /// <inheritdoc/>
  89. public abstract int GetVideoWidth();
  90. /// <inheritdoc/>
  91. public abstract int GetVideoHeight();
  92. /// <inheritdoc/>
  93. public abstract float GetVideoFrameRate();
  94. /// <inheritdoc/>
  95. public virtual float GetVideoDisplayRate() { return _displayRate; }
  96. /// <inheritdoc/>
  97. public abstract bool HasAudio();
  98. /// <inheritdoc/>
  99. public abstract bool HasVideo();
  100. /// <inheritdoc/>
  101. public bool IsVideoStereo() { return GetTextureStereoPacking() != StereoPacking.None; }
  102. // Basic State
  103. /// <inheritdoc/>
  104. public abstract bool IsSeeking();
  105. /// <inheritdoc/>
  106. public abstract bool IsPlaying();
  107. /// <inheritdoc/>
  108. public abstract bool IsPaused();
  109. /// <inheritdoc/>
  110. public abstract bool IsFinished();
  111. /// <inheritdoc/>
  112. public abstract bool IsBuffering();
  113. /// <inheritdoc/>
  114. public virtual bool WaitForNextFrame(Camera dummyCamera, int previousFrameCount) { return false; }
  115. // Textures
  116. /// <inheritdoc/>
  117. public virtual int GetTextureCount() { return 1; }
  118. /// <inheritdoc/>
  119. public abstract Texture GetTexture(int index = 0);
  120. /// <inheritdoc/>
  121. public abstract int GetTextureFrameCount();
  122. /// <inheritdoc/>
  123. public virtual bool SupportsTextureFrameCount() { return true; }
  124. /// <inheritdoc/>
  125. public virtual long GetTextureTimeStamp() { return long.MinValue; }
  126. /// <inheritdoc/>
  127. public abstract bool RequiresVerticalFlip();
  128. /// <inheritdoc/>
  129. public virtual float GetTexturePixelAspectRatio() { return 1f; }
  130. /// <inheritdoc/>
  131. public virtual Matrix4x4 GetYpCbCrTransform() { return Matrix4x4.identity; }
  132. /// <inheritdoc/>
  133. public virtual float[] GetAffineTransform() { return new float[] { 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }; }
  134. /// <inheritdoc/>
  135. public virtual float[] GetTextureTransform() { return GetAffineTransform(); }
  136. /// <inheritdoc/>
  137. public virtual Matrix4x4 GetTextureMatrix()
  138. {
  139. float[] transform = GetAffineTransform();
  140. if (transform == null || transform.Length != 6)
  141. return Matrix4x4.identity;
  142. Vector4 v0 = new Vector4(transform[0], transform[1], 0, 0);
  143. Vector4 v1 = new Vector4(transform[2], transform[3], 0, 0);
  144. Vector4 v2 = new Vector4( 0, 0, 1, 0);
  145. Vector4 v3 = new Vector4(transform[4], transform[5], 0, 1);
  146. Matrix4x4 xfrm = new Matrix4x4(v0, v1, v2, v3);
  147. return xfrm;
  148. }
  149. public StereoPacking GetTextureStereoPacking()
  150. {
  151. StereoPacking result = InternalGetTextureStereoPacking();
  152. if (result == StereoPacking.Unknown)
  153. {
  154. // If stereo is unknown, fall back to media hints or no packing
  155. result = _mediaHints.stereoPacking;
  156. }
  157. return result;
  158. }
  159. internal abstract StereoPacking InternalGetTextureStereoPacking();
  160. public virtual TransparencyMode GetTextureTransparency()
  161. {
  162. return _mediaHints.transparency;
  163. }
  164. public AlphaPacking GetTextureAlphaPacking()
  165. {
  166. if (GetTextureTransparency() == TransparencyMode.Transparent)
  167. {
  168. return _mediaHints.alphaPacking;
  169. }
  170. return AlphaPacking.None;
  171. }
  172. // Audio General
  173. /// <inheritdoc/>
  174. public abstract void MuteAudio(bool bMuted);
  175. /// <inheritdoc/>
  176. public abstract bool IsMuted();
  177. /// <inheritdoc/>
  178. public abstract void SetVolume(float volume);
  179. /// <inheritdoc/>
  180. public virtual void SetBalance(float balance) { }
  181. /// <inheritdoc/>
  182. public abstract float GetVolume();
  183. /// <inheritdoc/>
  184. public virtual float GetBalance() { return 0f; }
  185. // Audio Grabbing
  186. /// <inheritdoc/>
  187. public virtual int GetAudioChannelCount() { return -1; }
  188. /// <inheritdoc/>
  189. public virtual AudioChannelMaskFlags GetAudioChannelMask() { return 0; }
  190. /// <inheritdoc/>
  191. public virtual int GrabAudio(float[] audioData, int audioDataFloatCount, int channelCount) { return 0; }
  192. /// <inheritdoc/>
  193. public virtual int GetAudioBufferedSampleCount() { return 0; }
  194. /// <inheritdoc/>
  195. public virtual void AudioConfigurationChanged(bool deviceChanged) { }
  196. // 360 Audio
  197. /// <inheritdoc/>
  198. public virtual void SetAudioHeadRotation(Quaternion q) { }
  199. /// <inheritdoc/>
  200. public virtual void ResetAudioHeadRotation() { }
  201. /// <inheritdoc/>
  202. public virtual void SetAudioChannelMode(Audio360ChannelMode channelMode) { }
  203. /// <inheritdoc/>
  204. public virtual void SetAudioFocusEnabled(bool enabled) { }
  205. /// <inheritdoc/>
  206. public virtual void SetAudioFocusProperties(float offFocusLevel, float widthDegrees) { }
  207. /// <inheritdoc/>
  208. public virtual void SetAudioFocusRotation(Quaternion q) { }
  209. /// <inheritdoc/>
  210. public virtual void ResetAudioFocus() { }
  211. // Streaming
  212. /// <inheritdoc/>
  213. public virtual long GetEstimatedTotalBandwidthUsed() { return -1; }
  214. /// <inheritdoc/>
  215. public virtual void SetPlayWithoutBuffering(bool playWithoutBuffering) { }
  216. // Caching
  217. /// <inheritdoc/>
  218. public virtual bool IsMediaCachingSupported() { return false; }
  219. /// <inheritdoc/>
  220. public virtual void AddMediaToCache(string url, string headers, MediaCachingOptions options) { }
  221. /// <inheritdoc/>
  222. public virtual void CancelDownloadOfMediaToCache(string url) { }
  223. /// <inheritdoc/>
  224. public virtual void PauseDownloadOfMediaToCache(string url) { }
  225. /// <inheritdoc/>
  226. public virtual void ResumeDownloadOfMediaToCache(string url) { }
  227. /// <inheritdoc/>
  228. public virtual void RemoveMediaFromCache(string url) { }
  229. /// <inheritdoc/>
  230. public virtual CachedMediaStatus GetCachedMediaStatus(string url, ref float progress) { return CachedMediaStatus.NotCached; }
  231. // /// <inheritdoc/>
  232. // public virtual bool IsMediaCached() { return false; }
  233. // External playback
  234. /// <inheritdoc/>
  235. public virtual bool IsExternalPlaybackSupported() { return false; }
  236. /// <inheritdoc/>
  237. public virtual bool IsExternalPlaybackActive() { return false; }
  238. /// <inheritdoc/>
  239. public virtual void SetAllowsExternalPlayback(bool enable) { }
  240. /// <inheritdoc/>
  241. public virtual void SetExternalPlaybackVideoGravity(ExternalPlaybackVideoGravity gravity) { }
  242. // Authentication
  243. //public virtual void SetKeyServerURL(string url) { }
  244. /// <inheritdoc/>
  245. public virtual void SetKeyServerAuthToken(string token) { }
  246. /// <inheritdoc/>
  247. public virtual void SetOverrideDecryptionKey(byte[] key) { }
  248. // General
  249. /// <inheritdoc/>
  250. public abstract void Update();
  251. /// <inheritdoc/>
  252. public /*abstract*/virtual void BeginRender() { }
  253. /// <inheritdoc/>
  254. public abstract void Render();
  255. /// <inheritdoc/>
  256. public abstract void Dispose();
  257. // Internal method
  258. public virtual bool GetDecoderPerformance(ref int activeDecodeThreadCount, ref int decodedFrameCount, ref int droppedFrameCount) { return false; }
  259. #if false
  260. public void Update()
  261. {
  262. Native.Update(_instance);
  263. if (UpdateTracks())
  264. {
  265. }
  266. if (UpdateTextCue())
  267. {
  268. }
  269. }
  270. #endif
  271. public virtual void EndUpdate() { }
  272. public virtual IntPtr GetNativePlayerHandle() { return IntPtr.Zero; }
  273. public ErrorCode GetLastError()
  274. {
  275. ErrorCode errorCode = _lastError;
  276. _lastError = ErrorCode.None;
  277. return errorCode;
  278. }
  279. /// <inheritdoc/>
  280. public virtual long GetLastExtendedErrorCode()
  281. {
  282. return 0;
  283. }
  284. public string GetPlayerDescription()
  285. {
  286. return _playerDescription;
  287. }
  288. /// <inheritdoc/>
  289. public virtual bool PlayerSupportsLinearColorSpace()
  290. {
  291. #if UNITY_PLATFORM_SUPPORTS_LINEAR
  292. return true;
  293. #else
  294. return false;
  295. #endif
  296. }
  297. protected string _playerDescription = string.Empty;
  298. protected ErrorCode _lastError = ErrorCode.None;
  299. protected FilterMode _defaultTextureFilterMode = FilterMode.Bilinear;
  300. protected TextureWrapMode _defaultTextureWrapMode = TextureWrapMode.Clamp;
  301. protected int _defaultTextureAnisoLevel = 1;
  302. protected MediaHints _mediaHints;
  303. protected TimeRanges _seekableTimes = new TimeRanges();
  304. protected TimeRanges _bufferedTimes = new TimeRanges();
  305. public TimeRanges GetSeekableTimes() { return _seekableTimes; }
  306. public TimeRanges GetBufferedTimes() { return _bufferedTimes; }
  307. public void GetTextureProperties(out FilterMode filterMode, out TextureWrapMode wrapMode, out int anisoLevel)
  308. {
  309. filterMode = _defaultTextureFilterMode;
  310. wrapMode = _defaultTextureWrapMode;
  311. anisoLevel = _defaultTextureAnisoLevel;
  312. }
  313. public void SetTextureProperties(FilterMode filterMode = FilterMode.Bilinear, TextureWrapMode wrapMode = TextureWrapMode.Clamp, int anisoLevel = 0)
  314. {
  315. _defaultTextureFilterMode = filterMode;
  316. _defaultTextureWrapMode = wrapMode;
  317. _defaultTextureAnisoLevel = anisoLevel;
  318. for (int i = 0; i < GetTextureCount(); ++i)
  319. {
  320. ApplyTextureProperties(GetTexture(i));
  321. }
  322. }
  323. protected virtual void ApplyTextureProperties(Texture texture)
  324. {
  325. if (texture != null)
  326. {
  327. texture.filterMode = _defaultTextureFilterMode;
  328. texture.wrapMode = _defaultTextureWrapMode;
  329. texture.anisoLevel = _defaultTextureAnisoLevel;
  330. }
  331. }
  332. #region Video Display Rate
  333. #if UNITY_EDITOR
  334. private float _displayRateLastRealTime = 0f;
  335. #endif
  336. private float _displayRateTimer;
  337. private int _displayRateLastFrameCount;
  338. private float _displayRate = 1f;
  339. protected void UpdateDisplayFrameRate()
  340. {
  341. const float IntervalSeconds = 0.5f;
  342. if (_displayRateTimer >= IntervalSeconds)
  343. {
  344. int frameCount = GetTextureFrameCount();
  345. int frameDelta = (frameCount - _displayRateLastFrameCount);
  346. _displayRate = (float)frameDelta / _displayRateTimer;
  347. _displayRateTimer -= IntervalSeconds;
  348. if (_displayRateTimer >= IntervalSeconds) _displayRateTimer -= IntervalSeconds;
  349. if (_displayRateTimer >= IntervalSeconds) _displayRateTimer = 0f;
  350. _displayRateLastFrameCount = frameCount;
  351. }
  352. float deltaTime = Time.deltaTime;
  353. #if UNITY_EDITOR
  354. if (!Application.isPlaying)
  355. {
  356. // When not playing Time.deltaTime isn't valid so we have to derive it
  357. deltaTime = (Time.realtimeSinceStartup - _displayRateLastRealTime);
  358. _displayRateLastRealTime = Time.realtimeSinceStartup;
  359. }
  360. #endif
  361. _displayRateTimer += deltaTime;
  362. }
  363. #endregion // Video Display Rate
  364. #region Stall Detection
  365. protected bool IsExpectingNewVideoFrame()
  366. {
  367. if (HasVideo())
  368. {
  369. // If we're playing then we expect a new frame
  370. if (!IsFinished() && (!IsPaused() && IsPlaying() && GetPlaybackRate() != 0.0f))
  371. {
  372. // Check that the video is not a single frame and therefore there is no other frame to display
  373. bool isSingleFrame = (GetTextureFrameCount() > 0 && GetDurationFrames() == 1);
  374. if (!isSingleFrame)
  375. {
  376. // NOTE: if a new frame isn't available then we could either be seeking or stalled
  377. return true;
  378. }
  379. }
  380. }
  381. return false;
  382. }
  383. /// <inheritdoc/>
  384. public virtual bool IsPlaybackStalled()
  385. {
  386. const float StallDetectionDuration = 0.5f;
  387. // Manually detect stalled video if the platform doesn't have native support to detect it
  388. if (SupportsTextureFrameCount() && IsExpectingNewVideoFrame())
  389. {
  390. // Detect a new video frame
  391. int frameCount = GetTextureFrameCount();
  392. if (frameCount != _stallDetectionFrame)
  393. {
  394. _stallDetectionTimer = 0f;
  395. _stallDetectionFrame = frameCount;
  396. }
  397. else
  398. {
  399. // Update the detection timer, but never more than once a Unity frame
  400. if (_stallDetectionGuard != Time.frameCount)
  401. {
  402. _stallDetectionTimer += Time.deltaTime;
  403. }
  404. }
  405. _stallDetectionGuard = Time.frameCount;
  406. float thresholdDuration = StallDetectionDuration;
  407. // Scale by the playback rate, but should be at least StallDetectionDuration
  408. thresholdDuration = Mathf.Max(thresholdDuration / Mathf.Abs(GetPlaybackRate()), StallDetectionDuration);
  409. // If a valid FPS is available then make sure the thresholdDuration
  410. // is at least double that. This is mainly for very low FPS
  411. // content (eg 1 or 2 FPS)
  412. float fps = GetVideoFrameRate();
  413. if (fps > 0f && !float.IsNaN(fps))
  414. {
  415. thresholdDuration = Mathf.Max(thresholdDuration, 2f / fps);
  416. }
  417. return (_stallDetectionTimer > thresholdDuration);
  418. }
  419. else
  420. {
  421. _stallDetectionTimer = 0f;
  422. }
  423. return false;
  424. }
  425. private float _stallDetectionTimer;
  426. private int _stallDetectionFrame;
  427. private int _stallDetectionGuard;
  428. #endregion // Stall Detection
  429. protected List<Subtitle> _subtitles;
  430. protected Subtitle _currentSubtitle;
  431. /// <inheritdoc/>
  432. public bool LoadSubtitlesSRT(string data)
  433. {
  434. if (string.IsNullOrEmpty(data))
  435. {
  436. // Disable subtitles
  437. _subtitles = null;
  438. _currentSubtitle = null;
  439. }
  440. else
  441. {
  442. _subtitles = SubtitleUtils.ParseSubtitlesSRT(data);
  443. _currentSubtitle = null;
  444. }
  445. return (_subtitles != null);
  446. }
  447. /// <inheritdoc/>
  448. public virtual void UpdateSubtitles()
  449. {
  450. if (_subtitles != null)
  451. {
  452. double time = GetCurrentTime();
  453. // TODO: implement a more efficient subtitle index searcher
  454. int searchIndex = 0;
  455. if (_currentSubtitle != null)
  456. {
  457. if (!_currentSubtitle.IsTime(time))
  458. {
  459. if (time > _currentSubtitle.timeEnd)
  460. {
  461. searchIndex = _currentSubtitle.index + 1;
  462. }
  463. _currentSubtitle = null;
  464. }
  465. }
  466. if (_currentSubtitle == null)
  467. {
  468. for (int i = searchIndex; i < _subtitles.Count; i++)
  469. {
  470. if (_subtitles[i].IsTime(time))
  471. {
  472. _currentSubtitle = _subtitles[i];
  473. break;
  474. }
  475. }
  476. }
  477. }
  478. }
  479. /// <inheritdoc/>
  480. public virtual int GetSubtitleIndex()
  481. {
  482. int result = -1;
  483. if (_currentSubtitle != null)
  484. {
  485. result = _currentSubtitle.index;
  486. }
  487. return result;
  488. }
  489. /// <inheritdoc/>
  490. public virtual string GetSubtitleText()
  491. {
  492. string result = string.Empty;
  493. if (_currentSubtitle != null)
  494. {
  495. result = _currentSubtitle.text;
  496. }
  497. else if (_currentTextCue != null)
  498. {
  499. result = _currentTextCue.Text;
  500. }
  501. return result;
  502. }
  503. public virtual void OnEnable()
  504. {
  505. }
  506. /// <inheritdoc/>
  507. public int GetCurrentTimeFrames(float overrideFrameRate = 0f)
  508. {
  509. int result = 0;
  510. float frameRate = (overrideFrameRate > 0f) ? overrideFrameRate : GetVideoFrameRate();
  511. if (frameRate > 0f)
  512. {
  513. result = Helper.ConvertTimeSecondsToFrame(GetCurrentTime(), frameRate);
  514. result = Mathf.Min(result, GetMaxFrameNumber(overrideFrameRate));
  515. }
  516. return result;
  517. }
  518. /// <inheritdoc/>
  519. public int GetDurationFrames(float overrideFrameRate = 0f)
  520. {
  521. int result = 0;
  522. float frameRate = (overrideFrameRate > 0f) ? overrideFrameRate : GetVideoFrameRate();
  523. if (frameRate > 0f)
  524. {
  525. result = Helper.ConvertTimeSecondsToFrame(GetDuration(), frameRate);
  526. }
  527. return result;
  528. }
  529. /// <inheritdoc/>
  530. public int GetMaxFrameNumber(float overrideFrameRate = 0f)
  531. {
  532. int result = GetDurationFrames(overrideFrameRate);
  533. result = Mathf.Max(0, result - 1);
  534. return result;
  535. }
  536. /// <inheritdoc/>
  537. public void SeekToFrameRelative(int frameOffset, float overrideFrameRate = 0f)
  538. {
  539. float frameRate = (overrideFrameRate > 0f)?overrideFrameRate:GetVideoFrameRate();
  540. if (frameRate > 0f)
  541. {
  542. int frame = Helper.ConvertTimeSecondsToFrame(GetCurrentTime(), frameRate);
  543. frame += frameOffset;
  544. frame = Mathf.Clamp(frame, 0, GetMaxFrameNumber(frameRate));
  545. double time = Helper.ConvertFrameToTimeSeconds(frame, frameRate);
  546. Seek(time);
  547. }
  548. }
  549. /// <inheritdoc/>
  550. public void SeekToFrame(int frame, float overrideFrameRate = 0f)
  551. {
  552. float frameRate = (overrideFrameRate > 0f)?overrideFrameRate:GetVideoFrameRate();
  553. if (frameRate > 0f)
  554. {
  555. frame = Mathf.Clamp(frame, 0, GetMaxFrameNumber(frameRate));
  556. double time = Helper.ConvertFrameToTimeSeconds(frame, frameRate);
  557. Seek(time);
  558. }
  559. }
  560. #region IBufferedDisplay Implementation
  561. private int _unityFrameCountBufferedDisplayGuard = -1;
  562. /// <inheritdoc/>
  563. public long UpdateBufferedDisplay()
  564. {
  565. // Guard to make sure we're only updating the buffered frame once per Unity frame
  566. if (Time.frameCount == _unityFrameCountBufferedDisplayGuard) return GetTextureTimeStamp();
  567. _unityFrameCountBufferedDisplayGuard = Time.frameCount;
  568. return InternalUpdateBufferedDisplay();
  569. }
  570. internal virtual long InternalUpdateBufferedDisplay() { return 0; }
  571. /// <inheritdoc/>
  572. public virtual BufferedFramesState GetBufferedFramesState()
  573. {
  574. return new BufferedFramesState();
  575. }
  576. /// <inheritdoc/>
  577. public virtual void SetSlaves(IBufferedDisplay[] slaves) { }
  578. /// <inheritdoc/>
  579. public virtual void SetBufferedDisplayMode(BufferedFrameSelectionMode mode, IBufferedDisplay master = null) { }
  580. /// <inheritdoc/>
  581. public virtual void SetBufferedDisplayOptions(bool pauseOnPrerollComplete) { }
  582. #endregion // IBufferedDisplay Implementation
  583. protected PlaybackQualityStats _playbackQualityStats = new PlaybackQualityStats();
  584. public PlaybackQualityStats GetPlaybackQualityStats()
  585. {
  586. return _playbackQualityStats;
  587. }
  588. }
  589. }