OpenShot Library | libopenshot  0.2.5
VideoCacheThread.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for VideoCacheThread class
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  *
6  * @ref License
7  */
8 
9 /* LICENSE
10  *
11  * Copyright (c) 2008-2019 OpenShot Studios, LLC
12  * <http://www.openshotstudios.com/>. This file is part of
13  * OpenShot Library (libopenshot), an open-source project dedicated to
14  * delivering high quality video editing and animation solutions to the
15  * world. For more information visit <http://www.openshot.org/>.
16  *
17  * OpenShot Library (libopenshot) is free software: you can redistribute it
18  * and/or modify it under the terms of the GNU Lesser General Public License
19  * as published by the Free Software Foundation, either version 3 of the
20  * License, or (at your option) any later version.
21  *
22  * OpenShot Library (libopenshot) is distributed in the hope that it will be
23  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 #include "../../include/Qt/VideoCacheThread.h"
32 #include <algorithm>
33 
34 namespace openshot
35 {
36  // Constructor
37  VideoCacheThread::VideoCacheThread()
38  : Thread("video-cache"), speed(1), is_playing(false), position(1)
39  , reader(NULL), max_frames(std::min(OPEN_MP_NUM_PROCESSORS * 8, 64)), current_display_frame(1)
40  {
41  }
42 
43  // Destructor
44  VideoCacheThread::~VideoCacheThread()
45  {
46  }
47 
48  // Get the currently playing frame number (if any)
49  int64_t VideoCacheThread::getCurrentFramePosition()
50  {
51  if (frame)
52  return frame->number;
53  else
54  return 0;
55  }
56 
57  // Set the currently playing frame number (if any)
58  void VideoCacheThread::setCurrentFramePosition(int64_t current_frame_number)
59  {
60  current_display_frame = current_frame_number;
61  }
62 
63  // Seek the reader to a particular frame number
64  void VideoCacheThread::Seek(int64_t new_position)
65  {
66  position = new_position;
67  }
68 
69  // Play the video
70  void VideoCacheThread::Play() {
71  // Start playing
72  is_playing = true;
73  }
74 
75  // Stop the audio
76  void VideoCacheThread::Stop() {
77  // Stop playing
78  is_playing = false;
79  }
80 
81  // Start the thread
82  void VideoCacheThread::run()
83  {
84  while (!threadShouldExit() && is_playing) {
85 
86  // Calculate sleep time for frame rate
87  double frame_time = (1000.0 / reader->info.fps.ToDouble());
88 
89  // Cache frames before the other threads need them
90  // Cache frames up to the max frames
91  while (speed == 1 && (position - current_display_frame) < max_frames)
92  {
93  // Only cache up till the max_frames amount... then sleep
94  try
95  {
96  if (reader) {
97  ZmqLogger::Instance()->AppendDebugMethod("VideoCacheThread::run (cache frame)", "position", position, "current_display_frame", current_display_frame, "max_frames", max_frames, "needed_frames", (position - current_display_frame));
98 
99  // Force the frame to be generated
100  reader->GetFrame(position);
101  }
102 
103  }
104  catch (const OutOfBoundsFrame & e)
105  {
106  // Ignore out of bounds frame exceptions
107  }
108 
109  // Is cache position behind current display frame?
110  if (position < current_display_frame) {
111  // Jump ahead
112  position = current_display_frame;
113  }
114 
115  // Increment frame number
116  position++;
117  }
118 
119  // Sleep for 1 frame length
120  usleep(frame_time * 1000);
121  }
122 
123  return;
124  }
125 }
#define OPEN_MP_NUM_PROCESSORS
This namespace is the default namespace for all code in the openshot library.