OpenShot Library | libopenshot  1.0.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 
23 namespace 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  {
44  if (mode != PLAYBACK_STOPPED)
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
96  Reader(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  Seek(new_frame, true);
169  }
170 
171  void QtPlayer::Seek(int64_t new_frame, bool start_preroll)
172  {
173  // Check for seek
174  if (reader && threads_started && new_frame > 0) {
175  // Notify cache thread that seek has occurred
176  p->videoCache->Seek(new_frame, start_preroll);
177 
178  // Notify audio thread that seek has occurred
179  p->audioPlayback->Seek(new_frame);
180 
181  // Update current position
182  p->Seek(new_frame);
183  }
184  }
185 
187  {
188  // Change mode to stopped
190 
191  // Notify threads of stopping
192  if (reader && threads_started) {
193  p->videoCache->Stop();
194  p->audioPlayback->Stop();
195 
196  // Kill all threads
197  p->stopPlayback();
198  }
199 
200  p->video_position = 0;
201  threads_started = false;
202  }
203 
204  // Set the reader object
206  {
207  // Set new reader. Note: Be sure to close and dispose of the old reader after calling this
208  reader = new_reader;
209  p->reader = new_reader;
210  p->videoCache->Reader(new_reader);
211  p->audioPlayback->Reader(new_reader);
212  }
213 
214  // Get the current reader, such as a FFmpegReader
216  return reader;
217  }
218 
219  // Set the QWidget pointer to display the video on (as a pointer-sized unsigned id)
220  void QtPlayer::SetQWidget(uintptr_t qwidget_address) {
221  // Update override QWidget address on the video renderer
222  p->renderer->OverrideWidget(qwidget_address);
223  }
224 
225  // Set the QWidget pointer to display the video on (using a real QWidget pointer)
226  void QtPlayer::SetQWidget(QWidget *widget) {
227  SetQWidget(reinterpret_cast<uintptr_t>(widget));
228  }
229 
230  // Get the Renderer pointer address (for Python to cast back into a VideoRenderer)
232  auto* vr = static_cast<VideoRenderer*>(p->renderer);
233  uintptr_t addr = reinterpret_cast<uintptr_t>(vr);
234  return addr;
235  }
236 
237  // Get the Playback speed
238  float QtPlayer::Speed() {
239  return speed;
240  }
241 
242  // Set the Playback speed multiplier (1.0 = normal speed, <1.0 = slower, >1.0 faster)
243  void QtPlayer::Speed(float new_speed) {
244  speed = new_speed;
245  p->speed = new_speed;
246  p->videoCache->setSpeed(new_speed);
247  if (p->reader && p->reader->info.has_audio) {
248  p->audioPlayback->setSpeed(new_speed);
249  }
250  }
251 
252  // Get the Volume
254  return volume;
255  }
256 
257  // Set the Volume multiplier (1.0 = normal volume, <1.0 = quieter, >1.0 louder)
258  void QtPlayer::Volume(float new_volume) {
259  volume = new_volume;
260  }
261 }
262 #ifdef __ANDROID__
263 #include <android/log.h>
264 #ifndef ANDROID_LOG_WARN
265 #define ANDROID_LOG_WARN 5
266 #endif
267 #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:31
This class is used to playback a video from a reader.
Definition: QtPlayer.h:36
void Loading()
Display a loading animation.
Definition: QtPlayer.cpp:144
uintptr_t GetRendererQObject()
Get the Renderer pointer address (for Python to cast back into a VideoRenderer)
Definition: QtPlayer.cpp:231
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
void SetQWidget(uintptr_t qwidget_address)
Definition: QtPlayer.cpp:220
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
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:253
virtual ~QtPlayer()
Default destructor.
Definition: QtPlayer.cpp:42
float Speed()
Get the Playback speed.
Definition: QtPlayer.cpp:238
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:215
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:186
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:153
void AddClip(openshot::Clip *clip)
Add an openshot::Clip to the timeline.
Definition: Timeline.cpp:338
void Open() override
Open the reader (and start consuming resources)
Definition: Timeline.cpp:966
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Timeline.cpp:1273
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