Beispiel #1
0
        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 the right frame is already available (or close enough) just grab it
                    if (TextureProducer.GetTexture() != null && (System.Math.Abs(_controlInterface.GetCurrentTime() - timeSeconds) < (timeThresholdMs / 1000.0)))
                    {
                        frame = TextureProducer.GetTexture();
                    }
                    else
                    {
                        int preSeekFrameCount = _textureInterface.GetTextureFrameCount();

                        // Seek to the frame
                        if (accurateSeek)
                        {
                            _controlInterface.Seek(timeSeconds);
                        }
                        else
                        {
                            _controlInterface.SeekFast(timeSeconds);
                        }

                        // Wait for the new frame to arrive
                        if (!_controlInterface.WaitForNextFrame(GetDummyCamera(), preSeekFrameCount))
                        {
                            // If WaitForNextFrame fails (e.g. in android single threaded), we run the below code to asynchronously wait for the frame
                            int currFc        = TextureProducer.GetTextureFrameCount();
                            int iterations    = 0;
                            int maxIterations = 50;

                            //+1 as often there will be an extra frame produced after pause (so we need to wait for the second frame instead)
                            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);
        }
Beispiel #2
0
 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));
 }