123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- using UnityEngine;
- using System.Collections;
- namespace RenderHeads.Media.AVProVideo
- {
- public partial class MediaPlayer : MonoBehaviour
- {
- #region Extract Frame
- private bool ForceWaitForNewFrame(int lastFrameCount, float timeoutMs)
- {
- bool result = false;
-
- System.DateTime startTime = System.DateTime.Now;
- int iterationCount = 0;
- while (Control != null && (System.DateTime.Now - startTime).TotalMilliseconds < (double)timeoutMs)
- {
- _playerInterface.Update();
-
-
-
- if (lastFrameCount != TextureProducer.GetTextureFrameCount())
- {
- result = true;
- break;
- }
- iterationCount++;
-
-
-
-
- }
- _playerInterface.Render();
- return result;
- }
-
-
-
-
-
-
-
- private static Camera GetDummyCamera()
- {
- if (_dummyCamera == null)
- {
- const string goName = "AVPro Video Dummy Camera";
- GameObject go = GameObject.Find(goName);
- if (go == null)
- {
- go = new GameObject(goName);
- go.hideFlags = HideFlags.HideInHierarchy | HideFlags.DontSave;
- go.SetActive(false);
- Object.DontDestroyOnLoad(go);
- _dummyCamera = go.AddComponent<Camera>();
- _dummyCamera.hideFlags = HideFlags.HideInInspector | HideFlags.DontSave;
- _dummyCamera.cullingMask = 0;
- _dummyCamera.clearFlags = CameraClearFlags.Nothing;
- _dummyCamera.enabled = false;
- }
- else
- {
- _dummyCamera = go.GetComponent<Camera>();
- }
- }
-
- return _dummyCamera;
- }
- private IEnumerator ExtractFrameCoroutine(Texture2D target, ProcessExtractedFrame callback, double timeSeconds = -1.0, bool accurateSeek = true, int timeoutMs = 1000, int timeThresholdMs = 100)
- {
- #if (!UNITY_EDITOR && UNITY_ANDROID) || UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN || UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX || UNITY_IOS || UNITY_TVOS
- Texture2D result = target;
- Texture frame = null;
- if (_controlInterface != null)
- {
- if (timeSeconds >= 0f)
- {
- Pause();
-
- if (TextureProducer.GetTexture() != null && (System.Math.Abs(_controlInterface.GetCurrentTime() - timeSeconds) < (timeThresholdMs / 1000.0)))
- {
- frame = TextureProducer.GetTexture();
- }
- else
- {
- int preSeekFrameCount = _textureInterface.GetTextureFrameCount();
-
- if (accurateSeek)
- {
- _controlInterface.Seek(timeSeconds);
- }
- else
- {
- _controlInterface.SeekFast(timeSeconds);
- }
-
- if (!_controlInterface.WaitForNextFrame(GetDummyCamera(), preSeekFrameCount))
- {
-
- int currFc = TextureProducer.GetTextureFrameCount();
- int iterations = 0;
- int maxIterations = 50;
-
- while((currFc + 1) >= TextureProducer.GetTextureFrameCount() && iterations++ < maxIterations)
- {
- yield return null;
- }
- }
- frame = TextureProducer.GetTexture();
- }
- }
- else
- {
- frame = TextureProducer.GetTexture();
- }
- }
- if (frame != null)
- {
- result = Helper.GetReadableTexture(frame, TextureProducer.RequiresVerticalFlip(), Helper.GetOrientation(Info.GetTextureTransform()), target);
- }
- #else
- Texture2D result = ExtractFrame(target, timeSeconds, accurateSeek, timeoutMs, timeThresholdMs);
- #endif
- callback(result);
- yield return null;
- }
- public void ExtractFrameAsync(Texture2D target, ProcessExtractedFrame callback, double timeSeconds = -1.0, bool accurateSeek = true, int timeoutMs = 1000, int timeThresholdMs = 100)
- {
- StartCoroutine(ExtractFrameCoroutine(target, callback, timeSeconds, accurateSeek, timeoutMs, timeThresholdMs));
- }
-
- public Texture2D ExtractFrame(Texture2D target, double timeSeconds = -1.0, bool accurateSeek = true, int timeoutMs = 1000, int timeThresholdMs = 100)
- {
- Texture2D result = target;
-
- Texture frame = ExtractFrame(timeSeconds, accurateSeek, timeoutMs, timeThresholdMs);
- if (frame != null)
- {
- result = Helper.GetReadableTexture(frame, TextureProducer.RequiresVerticalFlip(), Helper.GetOrientation(Info.GetTextureTransform()), target);
- }
- return result;
- }
- private Texture ExtractFrame(double timeSeconds = -1.0, bool accurateSeek = true, int timeoutMs = 1000, int timeThresholdMs = 100)
- {
- Texture result = null;
- if (_controlInterface != null)
- {
- if (timeSeconds >= 0f)
- {
- Pause();
-
- if (TextureProducer.GetTexture() != null && (System.Math.Abs(_controlInterface.GetCurrentTime() - timeSeconds) < (timeThresholdMs / 1000.0)))
- {
- result = TextureProducer.GetTexture();
- }
- else
- {
-
- int frameCount = TextureProducer.GetTextureFrameCount();
-
- if (accurateSeek)
- {
- _controlInterface.Seek(timeSeconds);
- }
- else
- {
- _controlInterface.SeekFast(timeSeconds);
- }
-
- ForceWaitForNewFrame(frameCount, timeoutMs);
- result = TextureProducer.GetTexture();
- }
- }
- else
- {
- result = TextureProducer.GetTexture();
- }
- }
- return result;
- }
- #endregion // Extract Frame
- }
- }
|