OpenShot Library | libopenshot  0.2.5
QtPlayer.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for QtPlayer class
4  * @author Duzy Chan <code@duzy.info>
5  * @author Jonathan Thomas <jonathan@openshot.org>
6  *
7  * @ref License
8  */
9 
10 /* LICENSE
11  *
12  * Copyright (c) 2008-2019 OpenShot Studios, LLC
13  * <http://www.openshotstudios.com/>. This file is part of
14  * OpenShot Library (libopenshot), an open-source project dedicated to
15  * delivering high quality video editing and animation solutions to the
16  * world. For more information visit <http://www.openshot.org/>.
17  *
18  * OpenShot Library (libopenshot) is free software: you can redistribute it
19  * and/or modify it under the terms of the GNU Lesser General Public License
20  * as published by the Free Software Foundation, either version 3 of the
21  * License, or (at your option) any later version.
22  *
23  * OpenShot Library (libopenshot) is distributed in the hope that it will be
24  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU Lesser General Public License for more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public License
29  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
30  */
31 
32 #include "../include/Clip.h"
33 #include "../include/FFmpegReader.h"
34 #include "../include/Timeline.h"
35 #include "../include/QtPlayer.h"
36 #include "../include/Qt/PlayerPrivate.h"
37 #include "../include/Qt/VideoRenderer.h"
38 
39 namespace openshot
40 {
41  // Delegating constructor
44  { }
45 
46  // Constructor
48  : PlayerBase()
49  , p(new openshot::PlayerPrivate(rb))
50  , threads_started(false)
51  {
52  reader = NULL;
53  }
54 
56  {
57  if (mode != PLAYBACK_STOPPED)
58  Stop();
59 
60  delete p;
61  }
62 
64  {
65  // Close audio device (only do this once, when all audio playback is finished)
67  }
68 
69  // Return any error string during initialization
70  std::string QtPlayer::GetError() {
71  if (reader && threads_started) {
72  // Get error from audio thread (if any)
73  return p->audioPlayback->getError();
74  } else {
75  return "";
76  }
77  }
78 
79  /// Get Audio Devices from JUCE
80  std::vector<openshot::AudioDeviceInfo> QtPlayer::GetAudioDeviceNames() {
81  if (reader && threads_started) {
82  return p->audioPlayback->getAudioDeviceNames();
83  } else {
84  return std::vector<openshot::AudioDeviceInfo>();
85  }
86  }
87 
88  void QtPlayer::SetSource(const std::string &source)
89  {
90  FFmpegReader *ffreader = new FFmpegReader(source);
91  ffreader->DisplayInfo();
92 
93  reader = new Timeline(ffreader->info.width, ffreader->info.height, ffreader->info.fps, ffreader->info.sample_rate, ffreader->info.channels, ffreader->info.channel_layout);
94  Clip *c = new Clip(source);
95 
96  Timeline* tm = (Timeline*)reader;
97  tm->AddClip(c);
98  tm->Open();
99 
100  // Set the reader
101  Reader(reader);
102  }
103 
105  {
106  // Set mode to playing, and speed to normal
108  Speed(1);
109 
110  if (reader && !threads_started) {
111  // Start thread only once
112  p->startPlayback();
113  threads_started = true;
114  }
115  }
116 
118  {
120  }
121 
122  /// Get the current mode
124  {
125  return mode;
126  }
127 
129  {
131  Speed(0);
132  }
133 
135  {
136  return p->video_position;
137  }
138 
139  void QtPlayer::Seek(int64_t new_frame)
140  {
141  // Check for seek
142  if (reader && threads_started && new_frame > 0) {
143  // Notify cache thread that seek has occurred
144  p->videoCache->Seek(new_frame);
145 
146  // Update current position
147  p->video_position = new_frame;
148 
149  // Clear last position (to force refresh)
150  p->last_video_position = 0;
151 
152  // Notify audio thread that seek has occurred
153  p->audioPlayback->Seek(new_frame);
154  }
155  }
156 
158  {
159  // Change mode to stopped
161 
162  // Notify threads of stopping
163  if (reader && threads_started) {
164  p->videoCache->Stop();
165  p->audioPlayback->Stop();
166 
167  // Kill all threads
168  p->stopPlayback();
169  }
170 
171  p->video_position = 0;
172  threads_started = false;
173  }
174 
175  // Set the reader object
177  {
178  // Set new reader. Note: Be sure to close and dispose of the old reader after calling this
179  reader = new_reader;
180  p->reader = new_reader;
181  p->videoCache->Reader(new_reader);
182  p->audioPlayback->Reader(new_reader);
183  }
184 
185  // Get the current reader, such as a FFmpegReader
187  return reader;
188  }
189 
190  // Set the QWidget pointer to display the video on (as a LONG pointer id)
191  void QtPlayer::SetQWidget(int64_t qwidget_address) {
192  // Update override QWidget address on the video renderer
193  p->renderer->OverrideWidget(qwidget_address);
194  }
195 
196  // Get the Renderer pointer address (for Python to cast back into a QObject)
198  return (int64_t)(VideoRenderer*)p->renderer;
199  }
200 
201  // Get the Playback speed
202  float QtPlayer::Speed() {
203  return speed;
204  }
205 
206  // Set the Playback speed multiplier (1.0 = normal speed, <1.0 = slower, >1.0 faster)
207  void QtPlayer::Speed(float new_speed) {
208  speed = new_speed;
209  p->speed = new_speed;
210  p->videoCache->setSpeed(new_speed);
211  if (p->reader->info.has_audio)
212  p->audioPlayback->setSpeed(new_speed);
213  }
214 
215  // Get the Volume
217  return volume;
218  }
219 
220  // Set the Volume multiplier (1.0 = normal volume, <1.0 = quieter, >1.0 louder)
221  void QtPlayer::Volume(float new_volume) {
222  volume = new_volume;
223  }
224 }
void CloseAudioDevice()
Close audio device.
static AudioDeviceManagerSingleton * Instance()
Override with no channels and no preferred audio device.
This class represents a clip (used to arrange readers on the timeline)
Definition: Clip.h:95
This class uses the FFmpeg libraries, to open video files and audio files, and return openshot::Frame...
Definition: FFmpegReader.h:94
This is the base class of all Players in libopenshot.
Definition: PlayerBase.h:60
PlaybackMode mode
Definition: PlayerBase.h:65
openshot::ReaderBase * reader
Definition: PlayerBase.h:64
The private part of QtPlayer class, which contains an audio thread and video thread,...
Definition: PlayerPrivate.h:49
This class is used to playback a video from a reader.
Definition: QtPlayer.h:48
void Loading()
Display a loading animation.
Definition: QtPlayer.cpp:117
void Seek(int64_t new_frame)
Seek to a specific frame in the player.
Definition: QtPlayer.cpp:139
void SetSource(const std::string &source)
Set the source URL/path of this player (which will create an internal Reader)
Definition: QtPlayer.cpp:88
int64_t Position()
Get the current frame number being played.
Definition: QtPlayer.cpp:134
QtPlayer()
Default constructor.
Definition: QtPlayer.cpp:42
void SetQWidget(int64_t qwidget_address)
Definition: QtPlayer.cpp:191
std::vector< openshot::AudioDeviceInfo > GetAudioDeviceNames()
Get Audio Devices from JUCE.
Definition: QtPlayer.cpp:80
std::string GetError()
Get Error (if any)
Definition: QtPlayer.cpp:70
void CloseAudioDevice()
Close audio device.
Definition: QtPlayer.cpp:63
float Volume()
Get the Volume.
Definition: QtPlayer.cpp:216
virtual ~QtPlayer()
Default destructor.
Definition: QtPlayer.cpp:55
float Speed()
Get the Playback speed.
Definition: QtPlayer.cpp:202
void Play()
Play the video.
Definition: QtPlayer.cpp:104
openshot::PlaybackMode Mode()
Get the current mode.
Definition: QtPlayer.cpp:123
void Pause()
Pause the video.
Definition: QtPlayer.cpp:128
openshot::ReaderBase * Reader()
Get the current reader, such as a FFmpegReader.
Definition: QtPlayer.cpp:186
int64_t GetRendererQObject()
Get the Renderer pointer address (for Python to cast back into a QObject)
Definition: QtPlayer.cpp:197
void Stop()
Stop the video player and clear the cached frames.
Definition: QtPlayer.cpp:157
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:98
openshot::ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:111
void DisplayInfo()
Display file information in the standard output stream (stdout)
Definition: ReaderBase.cpp:70
This is the base class of all Renderers in libopenshot.
Definition: RendererBase.h:49
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 Open()
Open the reader (and start consuming resources)
Definition: Timeline.cpp:713
void AddClip(Clip *clip)
Add an openshot::Clip to the timeline.
Definition: Timeline.cpp:101
This namespace is the default namespace for all code in the openshot library.
PlaybackMode
This enumeration determines the mode of the video player (i.e. playing, paused, etc....
Definition: PlayerBase.h:45
@ PLAYBACK_LOADING
Loading the video (display a loading animation)
Definition: PlayerBase.h:48
@ PLAYBACK_PAUSED
Pause the video (holding the last displayed frame)
Definition: PlayerBase.h:47
@ PLAYBACK_STOPPED
Stop playing the video (clear cache, done with player)
Definition: PlayerBase.h:49
@ PLAYBACK_PLAY
Play the video normally.
Definition: PlayerBase.h:46
int width
The width of the video (in pixesl)
Definition: ReaderBase.h:68
int channels
The number of audio channels used in the audio stream.
Definition: ReaderBase.h:83
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: ReaderBase.h:70
int height
The height of the video (in pixels)
Definition: ReaderBase.h:67
openshot::ChannelLayout channel_layout
The channel layout (mono, stereo, 5 point surround, etc...)
Definition: ReaderBase.h:84
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:63
int sample_rate
The number of audio samples per second (44100 is a common sample rate)
Definition: ReaderBase.h:82