OpenShot Library | libopenshot  0.1.1
QtPlayer.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for QtPlayer class
4  * @author Duzy Chan <code@duzy.info>
5  * @author Jonathan Thomas <jonathan@openshot.org>
6  *
7  * @section LICENSE
8  *
9  * Copyright (c) 2008-2014 OpenShot Studios, LLC
10  * <http://www.openshotstudios.com/>. This file is part of
11  * OpenShot Library (libopenshot), an open-source project dedicated to
12  * delivering high quality video editing and animation solutions to the
13  * world. For more information visit <http://www.openshot.org/>.
14  *
15  * OpenShot Library (libopenshot) is free software: you can redistribute it
16  * and/or modify it under the terms of the GNU Lesser General Public License
17  * as published by the Free Software Foundation, either version 3 of the
18  * License, or (at your option) any later version.
19  *
20  * OpenShot Library (libopenshot) is distributed in the hope that it will be
21  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public License
26  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
27  */
28 
29 #include "../include/Clip.h"
30 #include "../include/FFmpegReader.h"
31 #include "../include/Timeline.h"
32 #include "../include/QtPlayer.h"
33 #include "../include/Qt/PlayerPrivate.h"
34 #include "../include/Qt/VideoRenderer.h"
35 
36 using namespace openshot;
37 
38 QtPlayer::QtPlayer() : PlayerBase(), p(new PlayerPrivate(new VideoRenderer())), threads_started(false)
39 {
40  reader = NULL;
41 }
42 
43 QtPlayer::QtPlayer(RendererBase *rb) : PlayerBase(), p(new PlayerPrivate(rb)), threads_started(false)
44 {
45  reader = NULL;
46 }
47 
49 {
50  if (mode != PLAYBACK_STOPPED) {
51  Stop();
52  }
53  delete p;
54 }
55 
56 void QtPlayer::SetSource(const std::string &source)
57 {
58  FFmpegReader *ffreader = new FFmpegReader(source);
59  ffreader->debug = false;
60  ffreader->DisplayInfo();
61 
62  //reader = new FrameMapper(ffreader, ffreader->info.fps, PULLDOWN_NONE, ffreader->info.sample_rate, ffreader->info.channels, ffreader->info.channel_layout);
63  reader = new Timeline(ffreader->info.width, ffreader->info.height, ffreader->info.fps, ffreader->info.sample_rate, ffreader->info.channels, ffreader->info.channel_layout);
64  Clip *c = new Clip(source);
65 
66  Timeline* tm = (Timeline*)reader;
67  tm->AddClip(c);
68  tm->Open();
69  tm->debug = false;
70 
71  // Set the reader
72  Reader(reader);
73 }
74 
76 {
77  // Set mode to playing, and speed to normal
79  Speed(1);
80 
81  if (reader && !threads_started) {
82  // Start thread only once
83  p->startPlayback();
84  threads_started = true;
85  }
86 }
87 
89 {
91 }
92 
93 /// Get the current mode
95 {
96  return mode;
97 }
98 
100 {
102  Speed(0);
103 }
104 
106 {
107  return p->video_position;
108 }
109 
110 void QtPlayer::Seek(int new_frame)
111 {
112  // Check for seek
113  if (new_frame > 0) {
114  // Notify cache thread that seek has occurred
115  p->videoCache->Seek(new_frame);
116 
117  // Update current position
118  p->video_position = new_frame;
119 
120  // Clear last position (to force refresh)
121  p->last_video_position = 0;
122 
123  // Notify audio thread that seek has occurred
124  p->audioPlayback->Seek(new_frame);
125  }
126 }
127 
129 {
130  // Change mode to stopped
132 
133  // Notify threads of stopping
134  p->videoCache->Stop();
135  p->audioPlayback->Stop();
136 
137  // Kill all threads
138  p->stopPlayback();
139 
140  p->video_position = 0;
141  threads_started = false;
142 }
143 
144 // Set the reader object
145 void QtPlayer::Reader(ReaderBase *new_reader)
146 {
147  // Set new reader. Note: Be sure to close and dispose of the old reader after calling this
148  reader = new_reader;
149  p->reader = new_reader;
150  p->videoCache->Reader(new_reader);
151  p->audioPlayback->Reader(new_reader);
152 }
153 
154 // Get the current reader, such as a FFmpegReader
156  return reader;
157 }
158 
159 // Set the QWidget pointer to display the video on (as a LONG pointer id)
160 void QtPlayer::SetQWidget(long qwidget_address) {
161  // Update override QWidget address on the video renderer
162  p->renderer->OverrideWidget(qwidget_address);
163 }
164 
165 // Get the Renderer pointer address (for Python to cast back into a QObject)
167  return (long) (VideoRenderer*)p->renderer;
168 }
169 
170 // Get the Playback speed
172  return speed;
173 }
174 
175 // Set the Playback speed multiplier (1.0 = normal speed, <1.0 = slower, >1.0 faster)
176 void QtPlayer::Speed(float new_speed) {
177  speed = new_speed;
178  p->speed = new_speed;
179  p->videoCache->setSpeed(new_speed);
180  if (p->reader->info.has_audio)
181  p->audioPlayback->setSpeed(new_speed);
182 }
183 
184 // Get the Volume
186  return volume;
187 }
188 
189 // Set the Volume multiplier (1.0 = normal volume, <1.0 = quieter, >1.0 louder)
190 void QtPlayer::Volume(float new_volume) {
191  volume = new_volume;
192 }
ReaderBase * reader
Definition: PlayerBase.h:63
virtual ~QtPlayer()
Default destructor.
Definition: QtPlayer.cpp:48
ChannelLayout channel_layout
The channel layout (mono, stereo, 5 point surround, etc...)
Definition: ReaderBase.h:83
int width
The width of the video (in pixesl)
Definition: ReaderBase.h:67
Loading the video (display a loading animation)
Definition: PlayerBase.h:47
Stop playing the video (clear cache, done with player)
Definition: PlayerBase.h:48
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:95
PlaybackMode Mode()
Get the current mode.
Definition: QtPlayer.cpp:94
This is the base class of all Players in libopenshot.
Definition: PlayerBase.h:58
void Stop()
Stop the video player and clear the cached frames.
Definition: QtPlayer.cpp:128
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:62
This class uses the FFmpeg libraries, to open video files and audio files, and return openshot::Frame...
Definition: FFmpegReader.h:92
This class represents a clip (used to arrange readers on the timeline)
Definition: Clip.h:108
int height
The height of the video (in pixels)
Definition: ReaderBase.h:66
void Pause()
Pause the video.
Definition: QtPlayer.cpp:99
Pause the video (holding the last displayed frame)
Definition: PlayerBase.h:46
QtPlayer()
Default constructor.
Definition: QtPlayer.cpp:38
float Speed()
Get the Playback speed.
Definition: QtPlayer.cpp:171
The private part of QtPlayer class, which contains an audio thread and video thread, and controls the video timing and audio synchronization code.
Definition: PlayerPrivate.h:47
void Seek(int new_frame)
Seek to a specific frame in the player.
Definition: QtPlayer.cpp:110
void AddClip(Clip *clip)
Add an openshot::Clip to the timeline.
Definition: Timeline.cpp:64
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 void OverrideWidget(long qwidget_address)=0
Allow manual override of the QWidget that is used to display.
void Open()
Open the reader (and start consuming resources)
Definition: Timeline.cpp:577
int Position()
Get the current frame number being played.
Definition: QtPlayer.cpp:105
float Volume()
Get the Volume.
Definition: QtPlayer.cpp:185
void Play()
Play the video.
Definition: QtPlayer.cpp:75
long GetRendererQObject()
Get the Renderer pointer address (for Python to cast back into a QObject)
Definition: QtPlayer.cpp:166
PlaybackMode
This enumeration determines the mode of the video player (i.e. playing, paused, etc...)
Definition: PlayerBase.h:43
void SetSource(const std::string &source)
Set the source URL/path of this player (which will create an internal Reader)
Definition: QtPlayer.cpp:56
This is the base class of all Renderers in libopenshot.
Definition: RendererBase.h:45
void DisplayInfo()
Display file information in the standard output stream (stdout)
Definition: ReaderBase.cpp:113
void Loading()
Display a loading animation.
Definition: QtPlayer.cpp:88
int channels
The number of audio channels used in the audio stream.
Definition: ReaderBase.h:82
ReaderBase * Reader()
Get the current reader, such as a FFmpegReader.
Definition: QtPlayer.cpp:155
Play the video normally.
Definition: PlayerBase.h:45
void SetQWidget(long qwidget_address)
Definition: QtPlayer.cpp:160
int sample_rate
The number of audio samples per second (44100 is a common sample rate)
Definition: ReaderBase.h:81
PlaybackMode mode
Definition: PlayerBase.h:64
This class represents a timeline.
Definition: Timeline.h:141