OpenShot Library | libopenshot 0.2.7
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 * @ref License
8 */
9
10/* LICENSE
11 *
12 * Copyright (c) 2008-2019 OpenShot Studios, LLC
13 * <http://www.openshotstudios.com/>. This file is part of
14 * OpenShot Library (libopenshot), an open-source project dedicated to
15 * delivering high quality video editing and animation solutions to the
16 * world. For more information visit <http://www.openshot.org/>.
17 *
18 * OpenShot Library (libopenshot) is free software: you can redistribute it
19 * and/or modify it under the terms of the GNU Lesser General Public License
20 * as published by the Free Software Foundation, either version 3 of the
21 * License, or (at your option) any later version.
22 *
23 * OpenShot Library (libopenshot) is distributed in the hope that it will be
24 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU Lesser General Public License for more details.
27 *
28 * You should have received a copy of the GNU Lesser General Public License
29 * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
30 */
31
32#include "Clip.h"
33#include "FFmpegReader.h"
34#include "Timeline.h"
35#include "QtPlayer.h"
36#include "Qt/PlayerPrivate.h"
37#include "Qt/VideoRenderer.h"
38
39namespace openshot
40{
41 // Delegating constructor
44 { }
45
46 // Constructor
48 : PlayerBase()
49 , p(new openshot::PlayerPrivate(rb))
50 , threads_started(false)
51 {
52 reader = NULL;
53 }
54
56 {
58 Stop();
59
60 delete p;
61 }
62
64 {
65 // Close audio device (only do this once, when all audio playback is finished)
67 }
68
69 // Return any error string during initialization
70 std::string QtPlayer::GetError() {
71 if (reader && threads_started) {
72 // Get error from audio thread (if any)
73 return p->audioPlayback->getError();
74 } else {
75 return "";
76 }
77 }
78
79 /// Get Audio Devices from JUCE
80 std::vector<openshot::AudioDeviceInfo> QtPlayer::GetAudioDeviceNames() {
81 if (reader && threads_started) {
82 return p->audioPlayback->getAudioDeviceNames();
83 } else {
84 return std::vector<openshot::AudioDeviceInfo>();
85 }
86 }
87
88 void QtPlayer::SetSource(const std::string &source)
89 {
90 FFmpegReader *ffreader = new FFmpegReader(source);
91 ffreader->DisplayInfo();
92
93 // Use default sample rate (or use the FFmpegReader's audio settings if any)
94 int sample_rate = 44100;
95 if (ffreader->info.sample_rate > 0)
96 sample_rate = ffreader->info.sample_rate;
97
98 // Use default channels (or use the FFmpegReader's audio settings if any)
99 int channels = 2;
100 if (ffreader->info.channels > 0)
101 channels = ffreader->info.channels;
102
103 // Use default channel layout (or use the FFmpegReader's audio settings if any)
105 if (channels != 2)
106 channel_layout = ffreader->info.channel_layout;
107
108 // Create timeline instance (720p, since we have no re-scaling in this player yet)
109 reader = new Timeline(1280, 720, ffreader->info.fps, sample_rate, channels, channel_layout);
110 Clip *c = new Clip(source);
111
112 Timeline* tm = (Timeline*)reader;
113 tm->AddClip(c);
114 tm->Open();
115
116 // Set the reader
117 Reader(reader);
118 }
119
121 {
122 // Set mode to playing, and speed to normal
124 Speed(1);
125
126 if (reader && !threads_started) {
127 // Start thread only once
128 p->startPlayback();
129 threads_started = true;
130 }
131 }
132
134 {
136 }
137
138 /// Get the current mode
140 {
141 return mode;
142 }
143
145 {
147 Speed(0);
148 }
149
151 {
152 return p->video_position;
153 }
154
155 void QtPlayer::Seek(int64_t new_frame)
156 {
157 // Check for seek
158 if (reader && threads_started && new_frame > 0) {
159 // Notify cache thread that seek has occurred
160 p->videoCache->Seek(new_frame);
161
162 // Update current position
163 p->video_position = new_frame;
164
165 // Clear last position (to force refresh)
166 p->last_video_position = 0;
167
168 // Notify audio thread that seek has occurred
169 p->audioPlayback->Seek(new_frame);
170 }
171 }
172
174 {
175 // Change mode to stopped
177
178 // Notify threads of stopping
179 if (reader && threads_started) {
180 p->videoCache->Stop();
181 p->audioPlayback->Stop();
182
183 // Kill all threads
184 p->stopPlayback();
185 }
186
187 p->video_position = 0;
188 threads_started = false;
189 }
190
191 // Set the reader object
193 {
194 // Set new reader. Note: Be sure to close and dispose of the old reader after calling this
195 reader = new_reader;
196 p->reader = new_reader;
197 p->videoCache->Reader(new_reader);
198 p->audioPlayback->Reader(new_reader);
199 }
200
201 // Get the current reader, such as a FFmpegReader
203 return reader;
204 }
205
206 // Set the QWidget pointer to display the video on (as a LONG pointer id)
207 void QtPlayer::SetQWidget(int64_t qwidget_address) {
208 // Update override QWidget address on the video renderer
209 p->renderer->OverrideWidget(qwidget_address);
210 }
211
212 // Get the Renderer pointer address (for Python to cast back into a QObject)
214 return (int64_t)(VideoRenderer*)p->renderer;
215 }
216
217 // Get the Playback speed
219 return speed;
220 }
221
222 // Set the Playback speed multiplier (1.0 = normal speed, <1.0 = slower, >1.0 faster)
223 void QtPlayer::Speed(float new_speed) {
224 speed = new_speed;
225 p->speed = new_speed;
226 p->videoCache->setSpeed(new_speed);
227 if (p->reader->info.has_audio)
228 p->audioPlayback->setSpeed(new_speed);
229 }
230
231 // Get the Volume
233 return volume;
234 }
235
236 // Set the Volume multiplier (1.0 = normal volume, <1.0 = quieter, >1.0 louder)
237 void QtPlayer::Volume(float new_volume) {
238 volume = new_volume;
239 }
240}
Header file for Clip class.
Header file for FFmpegReader class.
Source file for PlayerPrivate class.
Header file for QtPlayer class.
Header file for Timeline class.
Header file for Video Renderer class.
void CloseAudioDevice()
Close audio device.
static AudioDeviceManagerSingleton * Instance()
Override with no channels and no preferred audio device.
This class represents a clip (used to arrange readers on the timeline)
Definition: Clip.h:109
This class uses the FFmpeg libraries, to open video files and audio files, and return openshot::Frame...
Definition: FFmpegReader.h:93
This is the base class of all Players in libopenshot.
Definition: PlayerBase.h:60
PlaybackMode mode
Definition: PlayerBase.h:65
openshot::ReaderBase * reader
Definition: PlayerBase.h:64
The private part of QtPlayer class, which contains an audio thread and video thread,...
Definition: PlayerPrivate.h:49
This class is used to playback a video from a reader.
Definition: QtPlayer.h:48
void Loading()
Display a loading animation.
Definition: QtPlayer.cpp:133
void Seek(int64_t new_frame)
Seek to a specific frame in the player.
Definition: QtPlayer.cpp:155
void SetSource(const std::string &source)
Set the source URL/path of this player (which will create an internal Reader)
Definition: QtPlayer.cpp:88
int64_t Position()
Get the current frame number being played.
Definition: QtPlayer.cpp:150
QtPlayer()
Default constructor.
Definition: QtPlayer.cpp:42
void SetQWidget(int64_t qwidget_address)
Definition: QtPlayer.cpp:207
std::vector< openshot::AudioDeviceInfo > GetAudioDeviceNames()
Get Audio Devices from JUCE.
Definition: QtPlayer.cpp:80
std::string GetError()
Get Error (if any)
Definition: QtPlayer.cpp:70
void CloseAudioDevice()
Close audio device.
Definition: QtPlayer.cpp:63
float Volume()
Get the Volume.
Definition: QtPlayer.cpp:232
virtual ~QtPlayer()
Default destructor.
Definition: QtPlayer.cpp:55
float Speed()
Get the Playback speed.
Definition: QtPlayer.cpp:218
void Play()
Play the video.
Definition: QtPlayer.cpp:120
openshot::PlaybackMode Mode()
Get the current mode.
Definition: QtPlayer.cpp:139
void Pause()
Pause the video.
Definition: QtPlayer.cpp:144
openshot::ReaderBase * Reader()
Get the current reader, such as a FFmpegReader.
Definition: QtPlayer.cpp:202
int64_t GetRendererQObject()
Get the Renderer pointer address (for Python to cast back into a QObject)
Definition: QtPlayer.cpp:213
void Stop()
Stop the video player and clear the cached frames.
Definition: QtPlayer.cpp:173
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:98
openshot::ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:111
void DisplayInfo()
Display file information in the standard output stream (stdout)
Definition: ReaderBase.cpp:70
This is the base class of all Renderers in libopenshot.
Definition: RendererBase.h:49
virtual void OverrideWidget(int64_t qwidget_address)=0
Allow manual override of the QWidget that is used to display.
This class represents a timeline.
Definition: Timeline.h:168
void AddClip(openshot::Clip *clip)
Add an openshot::Clip to the timeline.
Definition: Timeline.cpp:356
void Open() override
Open the reader (and start consuming resources)
Definition: Timeline.cpp:759
void setSpeed(int new_speed)
Set Speed (The speed and direction to playback a reader (1=normal, 2=fast, 3=faster,...
void Stop()
Stop the audio playback.
void Reader(ReaderBase *new_reader)
Set the current thread's reader.
void Seek(int64_t new_position)
Seek the reader to a particular frame number.
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:47
ChannelLayout
This enumeration determines the audio channel layout (such as stereo, mono, 5 point surround,...
PlaybackMode
This enumeration determines the mode of the video player (i.e. playing, paused, etc....
Definition: PlayerBase.h:45
@ PLAYBACK_LOADING
Loading the video (display a loading animation)
Definition: PlayerBase.h:48
@ PLAYBACK_PAUSED
Pause the video (holding the last displayed frame)
Definition: PlayerBase.h:47
@ PLAYBACK_STOPPED
Stop playing the video (clear cache, done with player)
Definition: PlayerBase.h:49
@ PLAYBACK_PLAY
Play the video normally.
Definition: PlayerBase.h:46
int channels
The number of audio channels used in the audio stream.
Definition: ReaderBase.h:83
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: ReaderBase.h:70
openshot::ChannelLayout channel_layout
The channel layout (mono, stereo, 5 point surround, etc...)
Definition: ReaderBase.h:84
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:63
int sample_rate
The number of audio samples per second (44100 is a common sample rate)
Definition: ReaderBase.h:82