2015-06-01 00:20:14 -07:00
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
* @brief Source file for VideoCacheThread class
|
|
|
|
|
* @author Jonathan Thomas <jonathan@openshot.org>
|
|
|
|
|
*
|
2019-06-09 08:31:04 -04:00
|
|
|
* @ref License
|
|
|
|
|
*/
|
|
|
|
|
|
2021-10-16 01:26:26 -04:00
|
|
|
// Copyright (c) 2008-2019 OpenShot Studios, LLC
|
|
|
|
|
//
|
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
2015-06-01 00:20:14 -07:00
|
|
|
|
2020-10-18 07:43:37 -04:00
|
|
|
#include "VideoCacheThread.h"
|
2021-10-27 14:34:05 -04:00
|
|
|
|
|
|
|
|
#include "CacheBase.h"
|
2021-01-26 10:52:04 -05:00
|
|
|
#include "Exceptions.h"
|
2021-10-27 14:34:05 -04:00
|
|
|
#include "Frame.h"
|
|
|
|
|
#include "OpenMPUtilities.h"
|
2015-06-01 00:20:14 -07:00
|
|
|
|
2021-11-01 11:04:31 -04:00
|
|
|
#include <algorithm>
|
2020-09-02 02:07:54 -04:00
|
|
|
#include <thread> // for std::this_thread::sleep_for
|
2022-01-18 13:08:32 -06:00
|
|
|
#include <chrono> // for std::chrono::microseconds
|
2020-09-02 02:07:54 -04:00
|
|
|
|
2015-06-01 00:20:14 -07:00
|
|
|
namespace openshot
|
|
|
|
|
{
|
|
|
|
|
// Constructor
|
|
|
|
|
VideoCacheThread::VideoCacheThread()
|
2022-02-09 17:29:04 -06:00
|
|
|
: Thread("video-cache"), speed(0), last_speed(1), is_playing(false),
|
2022-01-26 17:56:33 -06:00
|
|
|
reader(NULL), current_display_frame(1), cached_frame_count(0),
|
2022-02-24 15:56:39 -06:00
|
|
|
min_frames_ahead(4), max_frames_ahead(8)
|
2015-06-01 00:20:14 -07:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Destructor
|
|
|
|
|
VideoCacheThread::~VideoCacheThread()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 00:40:01 -05:00
|
|
|
// Seek the reader to a particular frame number
|
2017-09-28 16:03:01 -05:00
|
|
|
void VideoCacheThread::Seek(int64_t new_position)
|
2015-06-01 00:20:14 -07:00
|
|
|
{
|
2022-01-26 17:56:33 -06:00
|
|
|
requested_display_frame = new_position;
|
2015-06-01 00:20:14 -07:00
|
|
|
}
|
|
|
|
|
|
2022-01-26 17:56:33 -06:00
|
|
|
// Seek the reader to a particular frame number and optionally start the pre-roll
|
|
|
|
|
void VideoCacheThread::Seek(int64_t new_position, bool start_preroll)
|
|
|
|
|
{
|
|
|
|
|
if (start_preroll && reader && reader->GetCache() && !reader->GetCache()->Contains(new_position)) {
|
|
|
|
|
cached_frame_count = 0;
|
|
|
|
|
}
|
|
|
|
|
Seek(new_position);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 00:40:01 -05:00
|
|
|
// Play the video
|
2015-06-01 00:20:14 -07:00
|
|
|
void VideoCacheThread::Play() {
|
|
|
|
|
// Start playing
|
|
|
|
|
is_playing = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stop the audio
|
|
|
|
|
void VideoCacheThread::Stop() {
|
|
|
|
|
// Stop playing
|
|
|
|
|
is_playing = false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-26 17:56:33 -06:00
|
|
|
// Is cache ready for playback (pre-roll)
|
|
|
|
|
bool VideoCacheThread::isReady() {
|
|
|
|
|
return (cached_frame_count > min_frames_ahead);
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-01 00:20:14 -07:00
|
|
|
// Start the thread
|
|
|
|
|
void VideoCacheThread::run()
|
|
|
|
|
{
|
2022-01-18 13:08:32 -06:00
|
|
|
// Types for storing time durations in whole and fractional microseconds
|
|
|
|
|
using micro_sec = std::chrono::microseconds;
|
|
|
|
|
using double_micro_sec = std::chrono::duration<double, micro_sec::period>;
|
2022-03-01 13:00:53 -06:00
|
|
|
bool should_pause_cache = false;
|
2015-06-01 00:20:14 -07:00
|
|
|
|
2020-09-02 02:07:54 -04:00
|
|
|
while (!threadShouldExit() && is_playing) {
|
2022-01-18 13:08:32 -06:00
|
|
|
// Calculate on-screen time for a single frame
|
|
|
|
|
const auto frame_duration = double_micro_sec(1000000.0 / reader->info.fps.ToDouble());
|
2022-02-01 15:33:32 -06:00
|
|
|
int current_speed = speed;
|
2015-06-01 00:20:14 -07:00
|
|
|
|
2022-01-14 15:16:04 -06:00
|
|
|
// Calculate increment (based on speed)
|
|
|
|
|
// Support caching in both directions
|
2022-02-09 17:29:04 -06:00
|
|
|
int16_t increment = speed;
|
2022-03-01 13:00:53 -06:00
|
|
|
if (current_speed == 0 && should_pause_cache) {
|
2022-02-28 15:45:46 -06:00
|
|
|
// Sleep during pause (after caching additional frames when paused)
|
2022-02-24 15:56:39 -06:00
|
|
|
std::this_thread::sleep_for(frame_duration / 4);
|
|
|
|
|
continue;
|
2022-02-28 15:45:46 -06:00
|
|
|
|
|
|
|
|
} else if (current_speed == 0) {
|
|
|
|
|
// Allow 'max frames' to increase when pause is detected (based on cache)
|
|
|
|
|
// To allow the cache to fill-up only on the initial pause.
|
2022-03-01 13:00:53 -06:00
|
|
|
should_pause_cache = true;
|
2022-02-28 15:45:46 -06:00
|
|
|
|
|
|
|
|
// Calculate bytes per frame. If we have a reference openshot::Frame, use that instead (the preview
|
|
|
|
|
// window can be smaller, can thus reduce the bytes per frame)
|
|
|
|
|
int64_t bytes_per_frame = (reader->info.height * reader->info.width * 4) +
|
|
|
|
|
(reader->info.sample_rate * reader->info.channels * 4);
|
|
|
|
|
if (last_cached_frame && last_cached_frame->has_image_data && last_cached_frame->has_audio_data) {
|
|
|
|
|
bytes_per_frame = last_cached_frame->GetBytes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calculate # of frames on Timeline cache (when paused)
|
|
|
|
|
if (reader->GetCache() && reader->GetCache()->GetMaxBytes() > 0) {
|
|
|
|
|
// When paused, use 1/2 the cache size (so our cache will be 50% before the play-head, and 50% after it)
|
|
|
|
|
max_frames_ahead = (reader->GetCache()->GetMaxBytes() / bytes_per_frame) / 2;
|
|
|
|
|
if (max_frames_ahead > 300) {
|
|
|
|
|
// Ignore values that are too large, and default to a safer value
|
|
|
|
|
max_frames_ahead = 300;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Overwrite the increment to our cache position
|
|
|
|
|
// to fully cache frames while paused (support forward and rewind)
|
|
|
|
|
if (last_speed > 0) {
|
|
|
|
|
increment = 1;
|
|
|
|
|
} else {
|
|
|
|
|
increment = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
// Default max frames ahead (normal playback)
|
|
|
|
|
max_frames_ahead = 8;
|
2022-03-01 13:00:53 -06:00
|
|
|
should_pause_cache = false;
|
2022-01-14 15:16:04 -06:00
|
|
|
}
|
2015-08-05 23:40:58 -05:00
|
|
|
|
2022-01-14 15:16:04 -06:00
|
|
|
// Always cache frames from the current display position to our maximum (based on the cache size).
|
|
|
|
|
// Frames which are already cached are basically free. Only uncached frames have a big CPU cost.
|
|
|
|
|
// By always looping through the expected frame range, we can fill-in missing frames caused by a
|
|
|
|
|
// fragmented cache object (i.e. the user clicking all over the timeline).
|
|
|
|
|
int64_t starting_frame = current_display_frame;
|
|
|
|
|
int64_t ending_frame = starting_frame + max_frames_ahead;
|
2022-02-09 17:29:04 -06:00
|
|
|
|
|
|
|
|
// Adjust ending frame for cache loop
|
2022-01-14 15:16:04 -06:00
|
|
|
if (speed < 0) {
|
2022-02-09 17:29:04 -06:00
|
|
|
// Reverse loop (if we are going backwards)
|
2022-01-14 15:16:04 -06:00
|
|
|
ending_frame = starting_frame - max_frames_ahead;
|
|
|
|
|
}
|
2022-02-09 17:29:04 -06:00
|
|
|
if (ending_frame < 0) {
|
|
|
|
|
// Don't allow negative frame number caching
|
|
|
|
|
ending_frame = 0;
|
|
|
|
|
}
|
2022-01-14 15:16:04 -06:00
|
|
|
|
2022-01-26 17:56:33 -06:00
|
|
|
// Loop through range of frames (and cache them)
|
2022-02-09 17:29:04 -06:00
|
|
|
int64_t uncached_frame_count = 0;
|
|
|
|
|
int64_t already_cached_frame_count = 0;
|
2022-01-14 15:16:04 -06:00
|
|
|
for (int64_t cache_frame = starting_frame; cache_frame != ending_frame; cache_frame += increment) {
|
2022-01-26 17:56:33 -06:00
|
|
|
cached_frame_count++;
|
2022-01-14 15:16:04 -06:00
|
|
|
if (reader && reader->GetCache() && !reader->GetCache()->Contains(cache_frame)) {
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// This frame is not already cached... so request it again (to force the creation & caching)
|
|
|
|
|
// This will also re-order the missing frame to the front of the cache
|
|
|
|
|
last_cached_frame = reader->GetFrame(cache_frame);
|
2022-02-09 17:29:04 -06:00
|
|
|
uncached_frame_count++;
|
2022-01-14 15:16:04 -06:00
|
|
|
}
|
|
|
|
|
catch (const OutOfBoundsFrame & e) { }
|
2022-02-09 17:29:04 -06:00
|
|
|
} else if (reader && reader->GetCache() && reader->GetCache()->Contains(cache_frame)) {
|
|
|
|
|
already_cached_frame_count++;
|
2022-01-14 15:16:04 -06:00
|
|
|
}
|
2022-02-09 17:29:04 -06:00
|
|
|
|
2022-01-26 17:56:33 -06:00
|
|
|
// Check if the user has seeked outside the cache range
|
|
|
|
|
if (requested_display_frame != current_display_frame) {
|
|
|
|
|
// cache will restart at a new position
|
|
|
|
|
if (speed >= 0 && (requested_display_frame < starting_frame || requested_display_frame > ending_frame)) {
|
|
|
|
|
break;
|
|
|
|
|
} else if (speed < 0 && (requested_display_frame > starting_frame || requested_display_frame < ending_frame)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-01 15:33:32 -06:00
|
|
|
// Check if playback speed changed (if so, break out of cache loop)
|
|
|
|
|
if (current_speed != speed) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-01-14 15:16:04 -06:00
|
|
|
}
|
2015-06-01 00:20:14 -07:00
|
|
|
|
2022-02-09 17:29:04 -06:00
|
|
|
// Update cache counts
|
|
|
|
|
if (cached_frame_count > max_frames_ahead && uncached_frame_count > (min_frames_ahead / 4)) {
|
|
|
|
|
// start cached count again (we have too many uncached frames)
|
|
|
|
|
cached_frame_count = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update current display frame & last non-paused speed
|
2022-01-26 17:56:33 -06:00
|
|
|
current_display_frame = requested_display_frame;
|
2022-02-09 17:29:04 -06:00
|
|
|
if (current_speed != 0) {
|
|
|
|
|
last_speed = current_speed;
|
|
|
|
|
}
|
2022-01-26 17:56:33 -06:00
|
|
|
|
2022-01-18 13:08:32 -06:00
|
|
|
// Sleep for a fraction of frame duration
|
2022-02-12 13:31:21 -06:00
|
|
|
std::this_thread::sleep_for(frame_duration / 4);
|
2020-10-30 18:23:45 -05:00
|
|
|
}
|
2015-06-01 00:20:14 -07:00
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|