OpenShot Library | libopenshot  0.3.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  // Set the source JSON of an openshot::Timelime
81  void QtPlayer::SetTimelineSource(const std::string &json) {
82  // Create timeline instance (720p, since we have no re-scaling in this player yet)
83  reader = new Timeline(1280, 720, openshot::Fraction(30, 1), 44100, 2, openshot::LAYOUT_STEREO);
84 
85  Timeline* tm = (Timeline*)reader;
86  tm->SetJson(json);
87  tm->DisplayInfo();
88  tm->Open();
89 
90  // Set the reader
91  Reader(reader);
92  }
93 
94  void QtPlayer::SetSource(const std::string &source)
95  {
96  FFmpegReader *ffreader = new FFmpegReader(source);
97  ffreader->DisplayInfo();
98 
99  // Use default sample rate (or use the FFmpegReader's audio settings if any)
100  int sample_rate = 44100;
101  if (ffreader->info.sample_rate > 0)
102  sample_rate = ffreader->info.sample_rate;
103 
104  // Use default channels (or use the FFmpegReader's audio settings if any)
105  int channels = 2;
106  if (ffreader->info.channels > 0)
107  channels = ffreader->info.channels;
108 
109  // Use default channel layout (or use the FFmpegReader's audio settings if any)
111  if (channels != 2)
112  channel_layout = ffreader->info.channel_layout;
113 
114  // Create timeline instance (720p, since we have no re-scaling in this player yet)
115  reader = new Timeline(1280, 720, ffreader->info.fps, sample_rate, channels, channel_layout);
116  Clip *c = new Clip(source);
117 
118  Timeline* tm = (Timeline*)reader;
119  tm->AddClip(c);
120  tm->Open();
121 
122  // Set the reader
123  Reader(reader);
124  }
125 
127  {
128  // Set mode to playing, and speed to normal
130  Speed(1);
131 
132  if (reader && !threads_started) {
133  // Start thread only once
134  p->startPlayback();
135  threads_started = true;
136  }
137  }
138 
140  {
142  }
143 
146  {
147  return mode;
148  }
149 
151  {
153  Speed(0);
154  }
155 
157  {
158  return p->video_position;
159  }
160 
161  void QtPlayer::Seek(int64_t new_frame)
162  {
163  // Check for seek
164  if (reader && threads_started && new_frame > 0) {
165  // Notify cache thread that seek has occurred
166  p->videoCache->Seek(new_frame, true);
167 
168  // Notify audio thread that seek has occurred
169  p->audioPlayback->Seek(new_frame);
170 
171  // Update current position
172  p->Seek(new_frame);
173  }
174  }
175 
177  {
178  // Change mode to stopped
180 
181  // Notify threads of stopping
182  if (reader && threads_started) {
183  p->videoCache->Stop();
184  p->audioPlayback->Stop();
185 
186  // Kill all threads
187  p->stopPlayback();
188  }
189 
190  p->video_position = 0;
191  threads_started = false;
192  }
193 
194  // Set the reader object
196  {
197  // Set new reader. Note: Be sure to close and dispose of the old reader after calling this
198  reader = new_reader;
199  p->reader = new_reader;
200  p->videoCache->Reader(new_reader);
201  p->audioPlayback->Reader(new_reader);
202  }
203 
204  // Get the current reader, such as a FFmpegReader
206  return reader;
207  }
208 
209  // Set the QWidget pointer to display the video on (as a LONG pointer id)
210  void QtPlayer::SetQWidget(int64_t qwidget_address) {
211  // Update override QWidget address on the video renderer
212  p->renderer->OverrideWidget(qwidget_address);
213  }
214 
215  // Get the Renderer pointer address (for Python to cast back into a QObject)
217  return (int64_t)(VideoRenderer*)p->renderer;
218  }
219 
220  // Get the Playback speed
221  float QtPlayer::Speed() {
222  return speed;
223  }
224 
225  // Set the Playback speed multiplier (1.0 = normal speed, <1.0 = slower, >1.0 faster)
226  void QtPlayer::Speed(float new_speed) {
227  speed = new_speed;
228  p->speed = new_speed;
229  p->videoCache->setSpeed(new_speed);
230  if (p->reader && p->reader->info.has_audio) {
231  p->audioPlayback->setSpeed(new_speed);
232  }
233  }
234 
235  // Get the Volume
237  return volume;
238  }
239 
240  // Set the Volume multiplier (1.0 = normal volume, <1.0 = quieter, >1.0 louder)
241  void QtPlayer::Volume(float new_volume) {
242  volume = new_volume;
243  }
244 }
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:35
AudioDeviceList getNames()
This class represents a clip (used to arrange readers on the timeline)
Definition: Clip.h:90
This class uses the FFmpeg libraries, to open video files and audio files, and return openshot::Frame...
Definition: FFmpegReader.h:113
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:139
void Seek(int64_t new_frame)
Seek to a specific frame in the player.
Definition: QtPlayer.cpp:161
void SetSource(const std::string &source)
Set the source URL/path of this player (which will create an internal Reader)
Definition: QtPlayer.cpp:94
AudioDeviceList GetAudioDeviceNames()
Get Audio Devices from JUCE.
Definition: QtPlayer.cpp:75
int64_t Position()
Get the current frame number being played.
Definition: QtPlayer.cpp:156
QtPlayer()
Default constructor.
Definition: QtPlayer.cpp:29
void SetQWidget(int64_t qwidget_address)
Definition: QtPlayer.cpp:210
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:236
virtual ~QtPlayer()
Default destructor.
Definition: QtPlayer.cpp:42
float Speed()
Get the Playback speed.
Definition: QtPlayer.cpp:221
void Play()
Play the video.
Definition: QtPlayer.cpp:126
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:145
void Pause()
Pause the video.
Definition: QtPlayer.cpp:150
openshot::ReaderBase * Reader()
Get the current reader, such as a FFmpegReader.
Definition: QtPlayer.cpp:205
void SetTimelineSource(const std::string &json)
Set the source JSON of an openshot::Timelime.
Definition: QtPlayer.cpp:81
int64_t GetRendererQObject()
Get the Renderer pointer address (for Python to cast back into a QObject)
Definition: QtPlayer.cpp:216
void Stop()
Stop the video player and clear the cached frames.
Definition: QtPlayer.cpp:176
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:150
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:877
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Timeline.cpp:1152
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:31
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
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