OpenShot Library | libopenshot  0.1.9
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 
57 {
58  // Close audio device (only do this once, when all audio playback is finished)
60 }
61 
62 void QtPlayer::SetSource(const std::string &source)
63 {
64  FFmpegReader *ffreader = new FFmpegReader(source);
65  ffreader->DisplayInfo();
66 
67  //reader = new FrameMapper(ffreader, ffreader->info.fps, PULLDOWN_NONE, ffreader->info.sample_rate, ffreader->info.channels, ffreader->info.channel_layout);
68  reader = new Timeline(ffreader->info.width, ffreader->info.height, ffreader->info.fps, ffreader->info.sample_rate, ffreader->info.channels, ffreader->info.channel_layout);
69  Clip *c = new Clip(source);
70 
71  Timeline* tm = (Timeline*)reader;
72  tm->AddClip(c);
73  tm->Open();
74 
75 // ZmqLogger::Instance()->Path("/home/jonathan/.openshot_qt/libopenshot.log");
76 // ZmqLogger::Instance()->Enable(true);
77 
78  // Set the reader
79  Reader(reader);
80 }
81 
83 {
84  // Set mode to playing, and speed to normal
86  Speed(1);
87 
88  if (reader && !threads_started) {
89  // Start thread only once
90  p->startPlayback();
91  threads_started = true;
92  }
93 }
94 
96 {
98 }
99 
100 /// Get the current mode
102 {
103  return mode;
104 }
105 
107 {
109  Speed(0);
110 }
111 
113 {
114  return p->video_position;
115 }
116 
117 void QtPlayer::Seek(int64_t new_frame)
118 {
119  // Check for seek
120  if (new_frame > 0) {
121  // Notify cache thread that seek has occurred
122  p->videoCache->Seek(new_frame);
123 
124  // Update current position
125  p->video_position = new_frame;
126 
127  // Clear last position (to force refresh)
128  p->last_video_position = 0;
129 
130  // Notify audio thread that seek has occurred
131  p->audioPlayback->Seek(new_frame);
132  }
133 }
134 
136 {
137  // Change mode to stopped
139 
140  // Notify threads of stopping
141  p->videoCache->Stop();
142  p->audioPlayback->Stop();
143 
144  // Kill all threads
145  p->stopPlayback();
146 
147  p->video_position = 0;
148  threads_started = false;
149 }
150 
151 // Set the reader object
152 void QtPlayer::Reader(ReaderBase *new_reader)
153 {
154  // Set new reader. Note: Be sure to close and dispose of the old reader after calling this
155  reader = new_reader;
156  p->reader = new_reader;
157  p->videoCache->Reader(new_reader);
158  p->audioPlayback->Reader(new_reader);
159 }
160 
161 // Get the current reader, such as a FFmpegReader
163  return reader;
164 }
165 
166 // Set the QWidget pointer to display the video on (as a LONG pointer id)
167 void QtPlayer::SetQWidget(int64_t qwidget_address) {
168  // Update override QWidget address on the video renderer
169  p->renderer->OverrideWidget(qwidget_address);
170 }
171 
172 // Get the Renderer pointer address (for Python to cast back into a QObject)
174  return (int64_t)(VideoRenderer*)p->renderer;
175 }
176 
177 // Get the Playback speed
179  return speed;
180 }
181 
182 // Set the Playback speed multiplier (1.0 = normal speed, <1.0 = slower, >1.0 faster)
183 void QtPlayer::Speed(float new_speed) {
184  speed = new_speed;
185  p->speed = new_speed;
186  p->videoCache->setSpeed(new_speed);
187  if (p->reader->info.has_audio)
188  p->audioPlayback->setSpeed(new_speed);
189 }
190 
191 // Get the Volume
193  return volume;
194 }
195 
196 // Set the Volume multiplier (1.0 = normal volume, <1.0 = quieter, >1.0 louder)
197 void QtPlayer::Volume(float new_volume) {
198  volume = new_volume;
199 }
ReaderBase * reader
Definition: PlayerBase.h:63
virtual ~QtPlayer()
Default destructor.
Definition: QtPlayer.cpp:48
void Seek(int64_t new_frame)
Seek to a specific frame in the player.
Definition: QtPlayer.cpp:117
void CloseAudioDevice()
Close audio device.
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
int64_t GetRendererQObject()
Get the Renderer pointer address (for Python to cast back into a QObject)
Definition: QtPlayer.cpp:173
virtual void OverrideWidget(int64_t qwidget_address)=0
Allow manual override of the QWidget that is used to display.
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:101
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:135
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:109
int height
The height of the video (in pixels)
Definition: ReaderBase.h:66
void Pause()
Pause the video.
Definition: QtPlayer.cpp:106
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:178
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 AddClip(Clip *clip)
Add an openshot::Clip to the timeline.
Definition: Timeline.cpp:71
void SetQWidget(int64_t qwidget_address)
Definition: QtPlayer.cpp:167
ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:111
Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: ReaderBase.h:69
void Open()
Open the reader (and start consuming resources)
Definition: Timeline.cpp:627
int Position()
Get the current frame number being played.
Definition: QtPlayer.cpp:112
This namespace is the default namespace for all code in the openshot library.
float Volume()
Get the Volume.
Definition: QtPlayer.cpp:192
void CloseAudioDevice()
Close audio device.
Definition: QtPlayer.cpp:56
void Play()
Play the video.
Definition: QtPlayer.cpp:82
PlaybackMode
This enumeration determines the mode of the video player (i.e. playing, paused, etc...)
Definition: PlayerBase.h:43
static AudioDeviceManagerSingleton * Instance(int numChannels)
Create or get an instance of this singleton (invoke the class with this method)
void SetSource(const std::string &source)
Set the source URL/path of this player (which will create an internal Reader)
Definition: QtPlayer.cpp:62
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:66
void Loading()
Display a loading animation.
Definition: QtPlayer.cpp:95
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:162
Play the video normally.
Definition: PlayerBase.h:45
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:145