void OnDisable() { if (_session != null) { // Close and dispose the FFmpeg session. _session.Close(); _session.Dispose(); _session = null; } }
void Update() { // Lazy initialization if (_session == null) { // Start an FFmpeg session. _session = FFmpegSession.Create( gameObject.name, _source.width, _source.height, _frameRate, preset ); _startTime = Time.time; _frameCount = 0; _frameDropCount = 0; } var gap = Time.time - FrameTime; var delta = 1 / _frameRate; if (gap < 0) { // Update without frame data. _session.PushFrame(null); } else if (gap < delta) { // Single-frame behind from the current time: // Push the current frame to FFmpeg. _session.PushFrame(_source); _frameCount++; } else if (gap < delta * 2) { // Two-frame behind from the current time: // Push the current frame twice to FFmpeg. Actually this is not // an efficient way to catch up. We should think about // implementing frame duplication in a more proper way. #fixme _session.PushFrame(_source); _session.PushFrame(_source); _frameCount += 2; } else { // Show a warning message about the situation. WarnFrameDrop(); // Push the current frame to FFmpeg. _session.PushFrame(_source); // Compensate the time delay. _frameCount += Mathf.FloorToInt(gap * _frameRate); } }