OpenShot Library | libopenshot  0.2.0
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 (reader && threads_started && 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  if (reader && threads_started) {
142  p->videoCache->Stop();
143  p->audioPlayback->Stop();
144 
145  // Kill all threads
146  p->stopPlayback();
147  }
148 
149  p->video_position = 0;
150  threads_started = false;
151 }
152 
153 // Set the reader object
154 void QtPlayer::Reader(ReaderBase *new_reader)
155 {
156  // Set new reader. Note: Be sure to close and dispose of the old reader after calling this
157  reader = new_reader;
158  p->reader = new_reader;
159  p->videoCache->Reader(new_reader);
160  p->audioPlayback->Reader(new_reader);
161 }
162 
163 // Get the current reader, such as a FFmpegReader
165  return reader;
166 }
167 
168 // Set the QWidget pointer to display the video on (as a LONG pointer id)
169 void QtPlayer::SetQWidget(int64_t qwidget_address) {
170  // Update override QWidget address on the video renderer
171  p->renderer->OverrideWidget(qwidget_address);
172 }
173 
174 // Get the Renderer pointer address (for Python to cast back into a QObject)
176  return (int64_t)(VideoRenderer*)p->renderer;
177 }
178 
179 // Get the Playback speed
181  return speed;
182 }
183 
184 // Set the Playback speed multiplier (1.0 = normal speed, <1.0 = slower, >1.0 faster)
185 void QtPlayer::Speed(float new_speed) {
186  speed = new_speed;
187  p->speed = new_speed;
188  p->videoCache->setSpeed(new_speed);
189  if (p->reader->info.has_audio)
190  p->audioPlayback->setSpeed(new_speed);
191 }
192 
193 // Get the Volume
195  return volume;
196 }
197 
198 // Set the Volume multiplier (1.0 = normal volume, <1.0 = quieter, >1.0 louder)
199 void QtPlayer::Volume(float new_volume) {
200  volume = new_volume;
201 }
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:175
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:96
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:180
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:169
ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:112
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:628
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:194
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:164
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