OpenShot Library | libopenshot 0.4.0
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->startPlayback();
140 threads_started = true;
141 }
142 }
143
145 {
147 }
148
151 {
152 return mode;
153 }
154
156 {
158 Speed(0);
159 }
160
162 {
163 return p->video_position;
164 }
165
166 void QtPlayer::Seek(int64_t new_frame)
167 {
168 // Check for seek
169 if (reader && threads_started && new_frame > 0) {
170 // Notify cache thread that seek has occurred
171 p->videoCache->Seek(new_frame, true);
172
173 // Notify audio thread that seek has occurred
174 p->audioPlayback->Seek(new_frame);
175
176 // Update current position
177 p->Seek(new_frame);
178 }
179 }
180
182 {
183 // Change mode to stopped
185
186 // Notify threads of stopping
187 if (reader && threads_started) {
188 p->videoCache->Stop();
189 p->audioPlayback->Stop();
190
191 // Kill all threads
192 p->stopPlayback();
193 }
194
195 p->video_position = 0;
196 threads_started = false;
197 }
198
199 // Set the reader object
201 {
202 // Set new reader. Note: Be sure to close and dispose of the old reader after calling this
203 reader = new_reader;
204 p->reader = new_reader;
205 p->videoCache->Reader(new_reader);
206 p->audioPlayback->Reader(new_reader);
207 }
208
209 // Get the current reader, such as a FFmpegReader
211 return reader;
212 }
213
214 // Set the QWidget pointer to display the video on (as a LONG pointer id)
215 void QtPlayer::SetQWidget(int64_t qwidget_address) {
216 // Update override QWidget address on the video renderer
217 p->renderer->OverrideWidget(qwidget_address);
218 }
219
220 // Get the Renderer pointer address (for Python to cast back into a QObject)
222 return (int64_t)(VideoRenderer*)p->renderer;
223 }
224
225 // Get the Playback speed
227 return speed;
228 }
229
230 // Set the Playback speed multiplier (1.0 = normal speed, <1.0 = slower, >1.0 faster)
231 void QtPlayer::Speed(float new_speed) {
232 speed = new_speed;
233 p->speed = new_speed;
234 p->videoCache->setSpeed(new_speed);
235 if (p->reader && p->reader->info.has_audio) {
236 p->audioPlayback->setSpeed(new_speed);
237 }
238 }
239
240 // Get the Volume
242 return volume;
243 }
244
245 // Set the Volume multiplier (1.0 = normal volume, <1.0 = quieter, >1.0 louder)
246 void QtPlayer::Volume(float new_volume) {
247 volume = new_volume;
248 }
249}
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:101
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:31
This class is used to playback a video from a reader.
Definition: QtPlayer.h:33
void Loading()
Display a loading animation.
Definition: QtPlayer.cpp:144
void Seek(int64_t new_frame)
Seek to a specific frame in the player.
Definition: QtPlayer.cpp:166
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
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:161
QtPlayer()
Default constructor.
Definition: QtPlayer.cpp:29
void SetQWidget(int64_t qwidget_address)
Definition: QtPlayer.cpp:215
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:241
virtual ~QtPlayer()
Default destructor.
Definition: QtPlayer.cpp:42
float Speed()
Get the Playback speed.
Definition: QtPlayer.cpp:226
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:150
void Pause()
Pause the video.
Definition: QtPlayer.cpp:155
openshot::ReaderBase * Reader()
Get the current reader, such as a FFmpegReader.
Definition: QtPlayer.cpp:210
void SetTimelineSource(const std::string &json)
Set the source JSON of an openshot::Timelime.
Definition: QtPlayer.cpp:86
int64_t GetRendererQObject()
Get the Renderer pointer address (for Python to cast back into a QObject)
Definition: QtPlayer.cpp:221
void Stop()
Stop the video player and clear the cached frames.
Definition: QtPlayer.cpp:181
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:88
void DisplayInfo(std::ostream *out=&std::cout)
Display file information in the standard output stream (stdout)
Definition: ReaderBase.cpp:61
This is the base class of all Renderers in libopenshot.
Definition: RendererBase.h:31
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:148
void AddClip(openshot::Clip *clip)
Add an openshot::Clip to the timeline.
Definition: Timeline.cpp:332
void Open() override
Open the reader (and start consuming resources)
Definition: Timeline.cpp:919
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Timeline.cpp:1197
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:29
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