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