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"
|
2022-09-15 18:33:06 -05:00
|
|
|
#include "Settings.h"
|
|
|
|
|
#include "Timeline.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-09-15 18:33:06 -05:00
|
|
|
min_frames_ahead(4), max_frames_ahead(8), should_pause_cache(false)
|
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)
|
|
|
|
|
{
|
2022-09-15 18:33:06 -05:00
|
|
|
// Determine previous frame number (depending on speed)
|
|
|
|
|
int64_t previous_frame = new_position;
|
|
|
|
|
if (last_speed < 0) {
|
|
|
|
|
// backwards
|
|
|
|
|
previous_frame++;
|
|
|
|
|
} else if (last_speed > 0) {
|
|
|
|
|
// forward
|
|
|
|
|
previous_frame--;
|
|
|
|
|
}
|
|
|
|
|
if (previous_frame <= 0) {
|
|
|
|
|
// min frame is 1
|
|
|
|
|
previous_frame = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clear cache if previous frame outside the cached range, which means we are
|
2022-09-16 10:21:54 -05:00
|
|
|
// requesting a non-contigous frame compared to our current cache range
|
2022-09-15 18:33:06 -05:00
|
|
|
if (!reader->GetCache()->Contains(previous_frame)) {
|
|
|
|
|
Timeline *t = (Timeline *) reader;
|
|
|
|
|
t->ClearAllCache();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reset pre-roll when requested frame is not currently cached
|
2022-09-16 10:21:54 -05:00
|
|
|
if (start_preroll && reader && reader->GetCache() && !reader->GetCache()->Contains(new_position)) {
|
2022-01-26 17:56:33 -06:00
|
|
|
cached_frame_count = 0;
|
2022-09-15 18:33:06 -05:00
|
|
|
if (speed == 0) {
|
|
|
|
|
should_pause_cache = false;
|
|
|
|
|
}
|
2022-09-16 10:21:54 -05:00
|
|
|
}
|
2022-01-26 17:56:33 -06:00
|
|
|
Seek(new_position);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-20 16:55:07 -05:00
|
|
|
// Get the size in bytes of a frame (rough estimate)
|
|
|
|
|
int64_t VideoCacheThread::getBytes(int width, int height, int sample_rate, int channels, float fps)
|
|
|
|
|
{
|
|
|
|
|
int64_t total_bytes = 0;
|
|
|
|
|
total_bytes += static_cast<int64_t>(width * height * sizeof(char) * 4);
|
|
|
|
|
|
|
|
|
|
// approximate audio size (sample rate / 24 fps)
|
|
|
|
|
total_bytes += ((sample_rate * channels) / fps) * sizeof(float);
|
|
|
|
|
|
|
|
|
|
// return size of this frame
|
|
|
|
|
return total_bytes;
|
|
|
|
|
}
|
|
|
|
|
|
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>;
|
2015-06-01 00:20:14 -07:00
|
|
|
|
2022-09-16 10:32:38 -05:00
|
|
|
while (!threadShouldExit() && is_playing) {
|
2022-09-20 16:55:07 -05:00
|
|
|
// Get settings
|
|
|
|
|
Settings *s = Settings::Instance();
|
|
|
|
|
|
2022-09-15 18:33:06 -05:00
|
|
|
// init local vars
|
|
|
|
|
min_frames_ahead = s->VIDEO_CACHE_MIN_PREROLL_FRAMES;
|
|
|
|
|
max_frames_ahead = s->VIDEO_CACHE_MAX_PREROLL_FRAMES;
|
|
|
|
|
|
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;
|
2022-09-15 18:33:06 -05:00
|
|
|
|
|
|
|
|
// Check for empty cache (and re-trigger preroll)
|
|
|
|
|
// This can happen when the user manually empties the timeline cache
|
|
|
|
|
if (reader->GetCache()->Count() == 0) {
|
|
|
|
|
should_pause_cache = false;
|
|
|
|
|
cached_frame_count = 0;
|
|
|
|
|
}
|
2015-06-01 00:20:14 -07:00
|
|
|
|
2022-09-15 18:33:06 -05:00
|
|
|
// Calculate increment (based on current_speed)
|
2022-01-14 15:16:04 -06:00
|
|
|
// Support caching in both directions
|
2022-09-15 18:33:06 -05:00
|
|
|
int16_t increment = current_speed;
|
|
|
|
|
|
|
|
|
|
if (current_speed == 0 && should_pause_cache || !s->ENABLE_PLAYBACK_CACHING) {
|
2022-02-28 15:45:46 -06:00
|
|
|
// Sleep during pause (after caching additional frames when paused)
|
2022-09-15 18:33:06 -05:00
|
|
|
// OR sleep when playback caching is disabled
|
|
|
|
|
current_display_frame = requested_display_frame;
|
|
|
|
|
std::this_thread::sleep_for(frame_duration / 2);
|
2022-02-24 15:56:39 -06:00
|
|
|
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
|
|
|
|
2022-09-20 16:55:07 -05:00
|
|
|
// Calculate bytes per frame
|
|
|
|
|
int64_t bytes_per_frame = getBytes(reader->info.width, reader->info.height,
|
|
|
|
|
reader->info.sample_rate, reader->info.channels,
|
|
|
|
|
reader->info.fps.ToFloat());
|
|
|
|
|
Timeline *t = (Timeline *) reader;
|
|
|
|
|
if (t->preview_width != reader->info.width || t->preview_height != reader->info.height) {
|
|
|
|
|
// If we have a different timeline preview size, use that instead (the preview
|
|
|
|
|
// window can be smaller, can thus reduce the bytes per frame)
|
|
|
|
|
bytes_per_frame = getBytes(t->preview_width, t->preview_height,
|
|
|
|
|
reader->info.sample_rate, reader->info.channels,
|
|
|
|
|
reader->info.fps.ToFloat());
|
2022-02-28 15:45:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calculate # of frames on Timeline cache (when paused)
|
|
|
|
|
if (reader->GetCache() && reader->GetCache()->GetMaxBytes() > 0) {
|
2022-09-20 16:55:07 -05:00
|
|
|
// When paused, limit the cached frames to the following % of total cache size.
|
|
|
|
|
// This allows for us to leave some cache behind the plahead, and some in front of the playhead.
|
|
|
|
|
max_frames_ahead = (reader->GetCache()->GetMaxBytes() / bytes_per_frame) * s->VIDEO_CACHE_PERCENT_AHEAD;
|
2022-09-15 18:33:06 -05:00
|
|
|
if (max_frames_ahead > s->VIDEO_CACHE_MAX_FRAMES) {
|
2022-02-28 15:45:46 -06:00
|
|
|
// Ignore values that are too large, and default to a safer value
|
2022-09-15 18:33:06 -05:00
|
|
|
max_frames_ahead = s->VIDEO_CACHE_MAX_FRAMES;
|
2022-02-28 15:45:46 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Overwrite the increment to our cache position
|
2022-09-15 18:33:06 -05:00
|
|
|
// to fully cache frames while paused (support forward and rewind caching)
|
2022-02-28 15:45:46 -06:00
|
|
|
if (last_speed > 0) {
|
|
|
|
|
increment = 1;
|
|
|
|
|
} else {
|
|
|
|
|
increment = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
2022-09-15 18:33:06 -05:00
|
|
|
// normal playback
|
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-09-15 18:33:06 -05:00
|
|
|
if (last_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-09-15 18:33:06 -05:00
|
|
|
if (starting_frame < 1) {
|
2022-02-09 17:29:04 -06:00
|
|
|
// Don't allow negative frame number caching
|
2022-09-15 18:33:06 -05:00
|
|
|
starting_frame = 1;
|
|
|
|
|
}
|
|
|
|
|
if (ending_frame < 1) {
|
|
|
|
|
// Don't allow negative frame number caching
|
|
|
|
|
ending_frame = 1;
|
2022-02-09 17:29:04 -06:00
|
|
|
}
|
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-09-15 18:33:06 -05:00
|
|
|
for (int64_t cache_frame = starting_frame; cache_frame != (ending_frame + increment); 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)) {
|
2022-09-15 18:33:06 -05:00
|
|
|
should_pause_cache = false;
|
2022-01-26 17:56:33 -06:00
|
|
|
break;
|
|
|
|
|
} else if (speed < 0 && (requested_display_frame > starting_frame || requested_display_frame < ending_frame)) {
|
2022-09-15 18:33:06 -05:00
|
|
|
should_pause_cache = false;
|
2022-01-26 17:56:33 -06:00
|
|
|
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-09-20 16:55:07 -05:00
|
|
|
// Check if thread has stopped
|
2022-09-15 18:33:06 -05:00
|
|
|
if (!is_playing) {
|
|
|
|
|
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
|
2022-09-15 18:33:06 -05:00
|
|
|
if (current_speed == 1 && cached_frame_count > max_frames_ahead && uncached_frame_count > min_frames_ahead) {
|
2022-02-09 17:29:04 -06:00
|
|
|
// 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-09-15 18:33:06 -05:00
|
|
|
std::this_thread::sleep_for(frame_duration / 2);
|
2020-10-30 18:23:45 -05:00
|
|
|
}
|
2015-06-01 00:20:14 -07:00
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|