OpenShot Library | libopenshot  0.1.1
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  int 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(int current_frame_number)
55  {
56  current_display_frame = current_frame_number;
57  }
58 
59  // Seek the audio thread
60  void VideoCacheThread::Seek(int new_position)
61  {
62  position = new_position;
63  }
64 
65  // Play the audio
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  // Force the frame to be generated
94  reader->GetFrame(position);
95 
96  }
97  catch (const OutOfBoundsFrame & e)
98  {
99  // Ignore out of bounds frame exceptions
100  }
101 
102  // Increment frame number
103  position++;
104  }
105 
106  // Sleep for 1 frame length
107  sleep(frame_time);
108  }
109 
110  return;
111  }
112 }
#define OPEN_MP_NUM_PROCESSORS
ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:120
Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: ReaderBase.h:69
virtual tr1::shared_ptr< Frame > GetFrame(long int number)=0
double ToDouble()
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:46