OpenShot Library | libopenshot  0.3.3
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  // 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
226  float QtPlayer::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 }
openshot::QtPlayer::QtPlayer
QtPlayer()
Default constructor.
Definition: QtPlayer.cpp:29
openshot::ReaderBase::DisplayInfo
void DisplayInfo(std::ostream *out=&std::cout)
Display file information in the standard output stream (stdout)
Definition: ReaderBase.cpp:61
openshot::ReaderInfo::sample_rate
int sample_rate
The number of audio samples per second (44100 is a common sample rate)
Definition: ReaderBase.h:60
openshot::AudioDeviceInfo
This struct hold information about Audio Devices.
Definition: AudioDevices.h:26
openshot::AudioDeviceList
std::vector< std::pair< std::string, std::string > > AudioDeviceList
Definition: AudioDevices.h:42
openshot::PLAYBACK_PLAY
@ PLAYBACK_PLAY
Play the video normally.
Definition: PlayerBase.h:28
Clip.h
Header file for Clip class.
openshot::QtPlayer::Seek
void Seek(int64_t new_frame)
Seek to a specific frame in the player.
Definition: QtPlayer.cpp:166
VideoRenderer.h
Header file for Video Renderer class.
openshot::QtPlayer::GetDefaultSampleRate
double GetDefaultSampleRate()
Return the default audio sample rate (from the system)
Definition: QtPlayer.cpp:66
openshot::QtPlayer::GetRendererQObject
int64_t GetRendererQObject()
Get the Renderer pointer address (for Python to cast back into a QObject)
Definition: QtPlayer.cpp:221
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::QtPlayer::GetAudioDeviceNames
AudioDeviceList GetAudioDeviceNames()
Get Audio Devices from JUCE.
Definition: QtPlayer.cpp:75
openshot::Clip
This class represents a clip (used to arrange readers on the timeline)
Definition: Clip.h:89
openshot::Fraction
This class represents a fraction.
Definition: Fraction.h:30
openshot::AudioDeviceManagerSingleton::Instance
static AudioDeviceManagerSingleton * Instance()
Override with default sample rate & channels (44100, 2) and no preferred audio device.
Definition: AudioPlaybackThread.cpp:39
openshot::QtPlayer::Speed
float Speed()
Get the Playback speed.
Definition: QtPlayer.cpp:226
openshot::ReaderBase::info
openshot::ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:88
openshot::QtPlayer::Mode
openshot::PlaybackMode Mode()
Get the current mode.
Definition: QtPlayer.cpp:150
openshot::QtPlayer::SetQWidget
void SetQWidget(int64_t qwidget_address)
Definition: QtPlayer.cpp:215
Timeline.h
Header file for Timeline class.
openshot::QtPlayer::Position
int64_t Position()
Get the current frame number being played.
Definition: QtPlayer.cpp:161
openshot::LAYOUT_STEREO
@ LAYOUT_STEREO
Definition: ChannelLayouts.h:31
openshot::QtPlayer::~QtPlayer
virtual ~QtPlayer()
Default destructor.
Definition: QtPlayer.cpp:42
openshot::QtPlayer::GetError
std::string GetError()
Get Error (if any)
Definition: QtPlayer.cpp:57
openshot::QtPlayer::Pause
void Pause()
Pause the video.
Definition: QtPlayer.cpp:155
openshot::QtPlayer::GetCurrentAudioDevice
AudioDeviceInfo GetCurrentAudioDevice()
Get current audio device or last attempted.
Definition: QtPlayer.cpp:81
AudioDeviceList
std::vector< std::pair< std::string, std::string > > AudioDeviceList
Definition: AudioDevices.cpp:18
openshot::AudioDevices::getNames
AudioDeviceList getNames()
Definition: AudioDevices.cpp:21
openshot::PLAYBACK_STOPPED
@ PLAYBACK_STOPPED
Stop playing the video (clear cache, done with player)
Definition: PlayerBase.h:31
openshot::PlayerBase::volume
float volume
Definition: PlayerBase.h:45
openshot::PlayerPrivate
The private part of QtPlayer class, which contains an audio thread and video thread,...
Definition: PlayerPrivate.h:30
openshot::Timeline::Open
void Open() override
Open the reader (and start consuming resources)
Definition: Timeline.cpp:919
openshot::Timeline::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Timeline.cpp:1197
openshot::ReaderInfo::has_audio
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:41
openshot::Timeline
This class represents a timeline.
Definition: Timeline.h:148
openshot::VideoCacheThread::setSpeed
void setSpeed(int new_speed)
Set Speed (The speed and direction to playback a reader (1=normal, 2=fast, 3=faster,...
Definition: VideoCacheThread.cpp:101
openshot::QtPlayer::SetTimelineSource
void SetTimelineSource(const std::string &json)
Set the source JSON of an openshot::Timelime.
Definition: QtPlayer.cpp:86
openshot::AudioDevices
A class which probes the available audio devices.
Definition: AudioDevices.h:45
openshot::QtPlayer::Volume
float Volume()
Get the Volume.
Definition: QtPlayer.cpp:241
openshot::VideoCacheThread::Reader
void Reader(ReaderBase *new_reader)
Set the current thread's reader.
Definition: VideoCacheThread.h:76
VideoRenderer
Definition: VideoRenderer.h:24
openshot::VideoCacheThread::Stop
void Stop()
Stop the audio playback.
Definition: VideoCacheThread.cpp:129
openshot::PlaybackMode
PlaybackMode
This enumeration determines the mode of the video player (i.e. playing, paused, etc....
Definition: PlayerBase.h:26
PlayerPrivate.h
Source file for PlayerPrivate class.
openshot::FFmpegReader
This class uses the FFmpeg libraries, to open video files and audio files, and return openshot::Frame...
Definition: FFmpegReader.h:101
openshot::PlayerBase::speed
float speed
Definition: PlayerBase.h:44
openshot::Timeline::AddClip
void AddClip(openshot::Clip *clip)
Add an openshot::Clip to the timeline.
Definition: Timeline.cpp:332
openshot::QtPlayer
This class is used to playback a video from a reader.
Definition: QtPlayer.h:32
openshot::PLAYBACK_LOADING
@ PLAYBACK_LOADING
Loading the video (display a loading animation)
Definition: PlayerBase.h:30
openshot::QtPlayer::Reader
openshot::ReaderBase * Reader()
Get the current reader, such as a FFmpegReader.
Definition: QtPlayer.cpp:210
openshot::RendererBase
This is the base class of all Renderers in libopenshot.
Definition: RendererBase.h:30
openshot::ReaderInfo::channel_layout
openshot::ChannelLayout channel_layout
The channel layout (mono, stereo, 5 point surround, etc...)
Definition: ReaderBase.h:62
openshot::PLAYBACK_PAUSED
@ PLAYBACK_PAUSED
Pause the video (holding the last displayed frame)
Definition: PlayerBase.h:29
openshot::QtPlayer::CloseAudioDevice
void CloseAudioDevice()
Close audio device.
Definition: QtPlayer.cpp:50
openshot::PlayerBase
This is the base class of all Players in libopenshot.
Definition: PlayerBase.h:41
openshot::ReaderInfo::fps
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: ReaderBase.h:48
openshot::AudioDeviceManagerSingleton::CloseAudioDevice
void CloseAudioDevice()
Close audio device.
Definition: AudioPlaybackThread.cpp:178
openshot::ReaderBase
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:75
openshot::PlayerBase::mode
PlaybackMode mode
Definition: PlayerBase.h:47
openshot::QtPlayer::Play
void Play()
Play the video.
Definition: QtPlayer.cpp:131
openshot::ChannelLayout
ChannelLayout
This enumeration determines the audio channel layout (such as stereo, mono, 5 point surround,...
Definition: ChannelLayouts.h:28
openshot::PlayerBase::reader
openshot::ReaderBase * reader
Definition: PlayerBase.h:46
AudioDevices.h
Header file for Audio Device Info struct.
openshot::VideoCacheThread::Seek
void Seek(int64_t new_position)
Seek the reader to a particular frame number.
Definition: VideoCacheThread.cpp:43
openshot::QtPlayer::Stop
void Stop()
Stop the video player and clear the cached frames.
Definition: QtPlayer.cpp:181
openshot::ReaderInfo::channels
int channels
The number of audio channels used in the audio stream.
Definition: ReaderBase.h:61
QtPlayer.h
Header file for QtPlayer class.
openshot::RendererBase::OverrideWidget
virtual void OverrideWidget(int64_t qwidget_address)=0
Allow manual override of the QWidget that is used to display.
FFmpegReader.h
Header file for FFmpegReader class.
openshot::QtPlayer::SetSource
void SetSource(const std::string &source)
Set the source URL/path of this player (which will create an internal Reader)
Definition: QtPlayer.cpp:99
openshot::QtPlayer::Loading
void Loading()
Display a loading animation.
Definition: QtPlayer.cpp:144