OpenShot Library | libopenshot 1.1.1
QtPlayer.cpp
Go to the documentation of this file.
1
10// Copyright (c) 2008-2019 OpenShot Studios, LLC
11//
12// SPDX-License-Identifier: LGPL-3.0-or-later
13
14#include "QtPlayer.h"
15
16#include "AudioDevices.h"
17#include "Clip.h"
18#include "FFmpegReader.h"
19#include "Timeline.h"
20#include "Qt/PlayerPrivate.h"
21#include "Qt/VideoRenderer.h"
22
23namespace openshot
24{
25
26 using AudioDeviceList = std::vector<std::pair<std::string, std::string>>;
27
28 // Delegating constructor
31 { }
32
33 // Constructor
35 : PlayerBase()
36 , p(new openshot::PlayerPrivate(rb))
37 , threads_started(false)
38 {
39 reader = NULL;
40 }
41
43 {
45 Stop();
46
47 delete p;
48 }
49
51 {
52 // Close audio device (only do this once, when all audio playback is finished)
54 }
55
56 // Return any error string during initialization
57 std::string QtPlayer::GetError() {
58 if (reader && threads_started) {
59 return p->audioPlayback->getError();
60 } else {
61 return "";
62 }
63 }
64
65 // Return the default audio sample rate (from the system)
67 if (reader && threads_started) {
68 return p->audioPlayback->getDefaultSampleRate();
69 } else {
70 return 0;
71 }
72 }
73
76 AudioDevices devs;
77 return devs.getNames();
78 }
79
80 // Get current audio device or last attempted (if none succeeded)
82 return p->audioPlayback->getCurrentAudioDevice();
83 }
84
85 // Set the source JSON of an openshot::Timelime
86 void QtPlayer::SetTimelineSource(const std::string &json) {
87 // Create timeline instance (720p, since we have no re-scaling in this player yet)
88 reader = new Timeline(1280, 720, openshot::Fraction(30, 1), 44100, 2, openshot::LAYOUT_STEREO);
89
90 Timeline* tm = (Timeline*)reader;
91 tm->SetJson(json);
92 tm->DisplayInfo();
93 tm->Open();
94
95 // Set the reader
97 }
98
99 void QtPlayer::SetSource(const std::string &source)
100 {
101 FFmpegReader *ffreader = new FFmpegReader(source);
102 ffreader->DisplayInfo();
103
104 // Use default sample rate (or use the FFmpegReader's audio settings if any)
105 int sample_rate = 44100;
106 if (ffreader->info.sample_rate > 0)
107 sample_rate = ffreader->info.sample_rate;
108
109 // Use default channels (or use the FFmpegReader's audio settings if any)
110 int channels = 2;
111 if (ffreader->info.channels > 0)
112 channels = ffreader->info.channels;
113
114 // Use default channel layout (or use the FFmpegReader's audio settings if any)
116 if (channels != 2)
117 channel_layout = ffreader->info.channel_layout;
118
119 // Create timeline instance (720p, since we have no re-scaling in this player yet)
120 reader = new Timeline(1280, 720, ffreader->info.fps, sample_rate, channels, channel_layout);
121 Clip *c = new Clip(source);
122
123 Timeline* tm = (Timeline*)reader;
124 tm->AddClip(c);
125 tm->Open();
126
127 // Set the reader
128 Reader(reader);
129 }
130
132 {
133 // Set mode to playing, and speed to normal
135 Speed(1);
136
137 if (reader && !threads_started) {
138 // Start thread only once
139 p->audioPlayback->Play();
140 p->startPlayback();
141 threads_started = true;
142 }
143 }
144
146 {
148 }
149
152 {
153 return mode;
154 }
155
157 {
159 Speed(0);
160 }
161
163 {
164 return p->video_position;
165 }
166
167 void QtPlayer::Seek(int64_t new_frame)
168 {
169 Seek(new_frame, true);
170 }
171
172 void QtPlayer::Seek(int64_t new_frame, bool start_preroll)
173 {
174 // Check for seek
175 if (reader && threads_started && new_frame > 0) {
176 // Notify cache thread that seek has occurred
177 p->videoCache->Seek(new_frame, start_preroll);
178
179 // Notify audio thread that seek has occurred
180 p->audioPlayback->Seek(new_frame);
181
182 // Update current position
183 p->Seek(new_frame);
184 }
185 }
186
188 {
189 // Change mode to stopped
191
192 // Notify threads of stopping
193 if (reader && threads_started) {
194 p->videoCache->Stop();
195 p->audioPlayback->Stop();
196
197 // Kill all threads
198 p->stopPlayback();
199 }
200
201 p->video_position = 0;
202 threads_started = false;
203 }
204
205 // Set the reader object
207 {
208 // ReaderBase is caller-owned. Drain every old-reader user before the
209 // caller may close/delete it after this method returns.
210 const bool restart = threads_started;
211 p->stopPlayback();
212 threads_started = false;
213 // Release every reference/event associated with the old reader before
214 // restarting, including a paused same-position preview.
215 p->Seek(p->video_position.load());
216 p->videoPlayback->SetFrame({});
217 p->videoPlayback->render.reset();
218 p->videoPlayback->rendered.reset();
219 reader = new_reader;
220 p->reader = new_reader;
221 p->videoCache->Reader(new_reader);
222 p->audioPlayback->Reader(new_reader);
223 if (restart && new_reader) {
224 p->startPlayback();
225 threads_started = true;
226 }
227 }
228
229 // Get the current reader, such as a FFmpegReader
231 return reader;
232 }
233
234 // Set the QWidget pointer to display the video on (as a pointer-sized unsigned id)
235 void QtPlayer::SetQWidget(uintptr_t qwidget_address) {
236 // Update override QWidget address on the video renderer
237 p->renderer->OverrideWidget(qwidget_address);
238 }
239
240 // Set the QWidget pointer to display the video on (using a real QWidget pointer)
241 void QtPlayer::SetQWidget(QWidget *widget) {
242 SetQWidget(reinterpret_cast<uintptr_t>(widget));
243 }
244
245 // Get the Renderer pointer address (for Python to cast back into a VideoRenderer)
247 auto* vr = static_cast<VideoRenderer*>(p->renderer);
248 uintptr_t addr = reinterpret_cast<uintptr_t>(vr);
249 return addr;
250 }
251
252 // Get the Playback speed
254 return speed;
255 }
256
257 // Set the Playback speed multiplier (1.0 = normal speed, <1.0 = slower, >1.0 faster)
258 void QtPlayer::Speed(float new_speed) {
259 speed = new_speed;
260 p->speed = new_speed;
261 p->videoCache->setSpeed(new_speed);
262 if (p->reader && p->reader->info.has_audio) {
263 p->audioPlayback->setSpeed(new_speed);
264 }
265 }
266
267 // Get the Volume
269 return volume;
270 }
271
272 // Set the Volume multiplier (1.0 = normal volume, <1.0 = quieter, >1.0 louder)
273 void QtPlayer::Volume(float new_volume) {
274 volume = new_volume;
275 }
276}
277#ifdef __ANDROID__
278#include <android/log.h>
279#ifndef ANDROID_LOG_WARN
280#define ANDROID_LOG_WARN 5
281#endif
282#endif
std::vector< std::pair< std::string, std::string > > AudioDeviceList
Header file for Audio Device Info struct.
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.
static AudioDeviceManagerSingleton * Instance()
Override with default sample rate & channels (44100, 2) and no preferred audio device.
A class which probes the available audio devices.
Definition: AudioDevices.h:46
AudioDeviceList getNames()
This class represents a clip (used to arrange readers on the timeline)
Definition: Clip.h:89
This class uses the FFmpeg libraries, to open video files and audio files, and return openshot::Frame...
Definition: FFmpegReader.h:103
This class represents a fraction.
Definition: Fraction.h:30
This is the base class of all Players in libopenshot.
Definition: PlayerBase.h:42
PlaybackMode mode
Definition: PlayerBase.h:47
openshot::ReaderBase * reader
Definition: PlayerBase.h:46
The private part of QtPlayer class, which contains an audio thread and video thread,...
Definition: PlayerPrivate.h:33
This class is used to playback a video from a reader.
Definition: QtPlayer.h:36
void Loading()
Display a loading animation.
Definition: QtPlayer.cpp:145
uintptr_t GetRendererQObject()
Get the Renderer pointer address (for Python to cast back into a VideoRenderer)
Definition: QtPlayer.cpp:246
void Seek(int64_t new_frame)
Seek to a specific frame in the player.
Definition: QtPlayer.cpp:167
void SetSource(const std::string &source)
Set the source URL/path of this player (which will create an internal Reader)
Definition: QtPlayer.cpp:99
AudioDeviceList GetAudioDeviceNames()
Get Audio Devices from JUCE.
Definition: QtPlayer.cpp:75
void SetQWidget(uintptr_t qwidget_address)
Definition: QtPlayer.cpp:235
AudioDeviceInfo GetCurrentAudioDevice()
Get current audio device or last attempted.
Definition: QtPlayer.cpp:81
int64_t Position()
Get the current frame number being played.
Definition: QtPlayer.cpp:162
QtPlayer()
Default constructor.
Definition: QtPlayer.cpp:29
std::string GetError()
Get Error (if any)
Definition: QtPlayer.cpp:57
void CloseAudioDevice()
Close audio device.
Definition: QtPlayer.cpp:50
float Volume()
Get the Volume.
Definition: QtPlayer.cpp:268
virtual ~QtPlayer()
Default destructor.
Definition: QtPlayer.cpp:42
float Speed()
Get the Playback speed.
Definition: QtPlayer.cpp:253
void Play()
Play the video.
Definition: QtPlayer.cpp:131
double GetDefaultSampleRate()
Return the default audio sample rate (from the system)
Definition: QtPlayer.cpp:66
openshot::PlaybackMode Mode()
Get the current mode.
Definition: QtPlayer.cpp:151
void Pause()
Pause the video.
Definition: QtPlayer.cpp:156
openshot::ReaderBase * Reader()
Get the current reader, such as a FFmpegReader.
Definition: QtPlayer.cpp:230
void SetTimelineSource(const std::string &json)
Set the source JSON of an openshot::Timelime.
Definition: QtPlayer.cpp:86
void Stop()
Stop the video player and clear the cached frames.
Definition: QtPlayer.cpp:187
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:76
openshot::ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:91
void DisplayInfo(std::ostream *out=&std::cout)
Display file information in the standard output stream (stdout)
Definition: ReaderBase.cpp:64
This is the base class of all Renderers in libopenshot.
Definition: RendererBase.h:32
virtual void OverrideWidget(uintptr_t qwidget_address)=0
Allow manual override of the QWidget that is used to display.
This class represents a timeline.
Definition: Timeline.h:154
void AddClip(openshot::Clip *clip)
Add an openshot::Clip to the timeline.
Definition: Timeline.cpp:340
void Open() override
Open the reader (and start consuming resources)
Definition: Timeline.cpp:968
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Timeline.cpp:1281
void setSpeed(int new_speed)
Set playback speed/direction. Positive = forward, negative = rewind, zero = pause.
void Stop()
Stop method is unimplemented.
void Reader(ReaderBase *new_reader)
Attach a ReaderBase (e.g. Timeline, FFmpegReader) and begin caching.
void Seek(int64_t new_position)
Backward-compatible alias for playback position updates (no seek side effects).
This namespace is the default namespace for all code in the openshot library.
Definition: AnimatedCurve.h:24
ChannelLayout
This enumeration determines the audio channel layout (such as stereo, mono, 5 point surround,...
std::vector< std::pair< std::string, std::string > > AudioDeviceList
Definition: AudioDevices.h:42
PlaybackMode
This enumeration determines the mode of the video player (i.e. playing, paused, etc....
Definition: PlayerBase.h:27
@ PLAYBACK_LOADING
Loading the video (display a loading animation)
Definition: PlayerBase.h:30
@ PLAYBACK_PAUSED
Pause the video (holding the last displayed frame)
Definition: PlayerBase.h:29
@ PLAYBACK_STOPPED
Stop playing the video (clear cache, done with player)
Definition: PlayerBase.h:31
@ PLAYBACK_PLAY
Play the video normally.
Definition: PlayerBase.h:28
This struct hold information about Audio Devices.
Definition: AudioDevices.h:27
int channels
The number of audio channels used in the audio stream.
Definition: ReaderBase.h:61
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: ReaderBase.h:48
openshot::ChannelLayout channel_layout
The channel layout (mono, stereo, 5 point surround, etc...)
Definition: ReaderBase.h:62
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:41
int sample_rate
The number of audio samples per second (44100 is a common sample rate)
Definition: ReaderBase.h:60