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-01-26 10:52:04 -05:00
|
|
|
#include "Exceptions.h"
|
2021-11-01 11:04:31 -04:00
|
|
|
#include "ZmqLogger.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
|
|
|
|
|
#include <chrono> // for std::chrono::milliseconds
|
|
|
|
|
|
2015-06-01 00:20:14 -07:00
|
|
|
namespace openshot
|
|
|
|
|
{
|
|
|
|
|
// Constructor
|
|
|
|
|
VideoCacheThread::VideoCacheThread()
|
|
|
|
|
: Thread("video-cache"), speed(1), is_playing(false), position(1)
|
2021-02-04 17:28:07 -06:00
|
|
|
, reader(NULL), max_concurrent_frames(OPEN_MP_NUM_PROCESSORS * 4), current_display_frame(1)
|
2015-06-01 00:20:14 -07:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Destructor
|
|
|
|
|
VideoCacheThread::~VideoCacheThread()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the currently playing frame number (if any)
|
2017-09-28 16:03:01 -05:00
|
|
|
int64_t VideoCacheThread::getCurrentFramePosition()
|
2015-06-01 00:20:14 -07:00
|
|
|
{
|
|
|
|
|
if (frame)
|
|
|
|
|
return frame->number;
|
|
|
|
|
else
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set the currently playing frame number (if any)
|
2017-09-28 16:03:01 -05:00
|
|
|
void VideoCacheThread::setCurrentFramePosition(int64_t current_frame_number)
|
2015-06-01 00:20:14 -07:00
|
|
|
{
|
|
|
|
|
current_display_frame = current_frame_number;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
|
position = 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start the thread
|
|
|
|
|
void VideoCacheThread::run()
|
|
|
|
|
{
|
2020-09-02 02:07:54 -04:00
|
|
|
// Types for storing time durations in whole and fractional milliseconds
|
2021-10-07 13:34:00 -05:00
|
|
|
std::shared_ptr<openshot::Frame> smallest_frame = NULL;
|
2020-09-02 02:07:54 -04:00
|
|
|
using ms = std::chrono::milliseconds;
|
|
|
|
|
using double_ms = std::chrono::duration<double, ms::period>;
|
2015-06-01 00:20:14 -07:00
|
|
|
|
2020-09-02 02:07:54 -04:00
|
|
|
// Calculate on-screen time for a single frame in milliseconds
|
|
|
|
|
const auto frame_duration = double_ms(1000.0 / reader->info.fps.ToDouble());
|
|
|
|
|
|
|
|
|
|
while (!threadShouldExit() && is_playing) {
|
2015-06-01 00:20:14 -07:00
|
|
|
|
2020-10-30 18:23:45 -05:00
|
|
|
// Cache frames before the other threads need them
|
|
|
|
|
// Cache frames up to the max frames. Reset to current position
|
|
|
|
|
// if cache gets too far away from display frame. Cache frames
|
|
|
|
|
// even when player is paused (i.e. speed 0).
|
2021-02-04 17:28:07 -06:00
|
|
|
while (((position - current_display_frame) < max_concurrent_frames) && is_playing)
|
2015-08-05 23:40:58 -05:00
|
|
|
{
|
2021-02-04 17:28:07 -06:00
|
|
|
// Only cache up till the max_concurrent_frames amount... then sleep
|
2020-10-30 18:23:45 -05:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (reader) {
|
2021-02-04 17:28:07 -06:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("VideoCacheThread::run (cache frame)", "position", position, "current_display_frame", current_display_frame, "max_concurrent_frames", max_concurrent_frames, "needed_frames", (position - current_display_frame));
|
2016-04-24 15:37:47 -05:00
|
|
|
|
2020-10-30 18:23:45 -05:00
|
|
|
// Force the frame to be generated
|
2021-10-07 13:34:00 -05:00
|
|
|
smallest_frame = reader->GetCache()->GetSmallestFrame();
|
|
|
|
|
if (smallest_frame && smallest_frame->number > current_display_frame) {
|
|
|
|
|
// Cache position has gotten too far away from current display frame.
|
|
|
|
|
// Reset the position to the current display frame.
|
|
|
|
|
position = current_display_frame;
|
2020-10-23 17:52:20 -05:00
|
|
|
}
|
2020-10-30 18:23:45 -05:00
|
|
|
reader->GetFrame(position);
|
2020-10-23 17:52:20 -05:00
|
|
|
}
|
2020-10-30 18:23:45 -05:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (const OutOfBoundsFrame & e)
|
|
|
|
|
{
|
|
|
|
|
// Ignore out of bounds frame exceptions
|
2016-04-24 15:37:47 -05:00
|
|
|
}
|
2015-08-05 23:40:58 -05:00
|
|
|
|
2020-10-30 18:23:45 -05:00
|
|
|
// Increment frame number
|
|
|
|
|
position++;
|
2015-08-05 23:40:58 -05:00
|
|
|
}
|
2015-06-01 00:20:14 -07:00
|
|
|
|
2020-10-30 18:23:45 -05:00
|
|
|
// Sleep for 1 frame length
|
|
|
|
|
std::this_thread::sleep_for(frame_duration);
|
|
|
|
|
}
|
2015-06-01 00:20:14 -07:00
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|