-
Notifications
You must be signed in to change notification settings - Fork 261
/
QtPlayer.cpp
240 lines (202 loc) · 6.54 KB
/
QtPlayer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**
* @file
* @brief Source file for QtPlayer class
* @author Duzy Chan <code@duzy.info>
* @author Jonathan Thomas <jonathan@openshot.org>
*
* @ref License
*/
/* LICENSE
*
* Copyright (c) 2008-2019 OpenShot Studios, LLC
* <http://www.openshotstudios.com/>. This file is part of
* OpenShot Library (libopenshot), an open-source project dedicated to
* delivering high quality video editing and animation solutions to the
* world. For more information visit <http://www.openshot.org/>.
*
* OpenShot Library (libopenshot) is free software: you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* OpenShot Library (libopenshot) is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Clip.h"
#include "FFmpegReader.h"
#include "Timeline.h"
#include "QtPlayer.h"
#include "Qt/PlayerPrivate.h"
#include "Qt/VideoRenderer.h"
namespace openshot
{
// Delegating constructor
QtPlayer::QtPlayer()
: QtPlayer::QtPlayer(new VideoRenderer())
{ }
// Constructor
QtPlayer::QtPlayer(openshot::RendererBase *rb)
: PlayerBase()
, p(new openshot::PlayerPrivate(rb))
, threads_started(false)
{
reader = NULL;
}
QtPlayer::~QtPlayer()
{
if (mode != PLAYBACK_STOPPED)
Stop();
delete p;
}
void QtPlayer::CloseAudioDevice()
{
// Close audio device (only do this once, when all audio playback is finished)
openshot::AudioDeviceManagerSingleton::Instance()->CloseAudioDevice();
}
// Return any error string during initialization
std::string QtPlayer::GetError() {
if (reader && threads_started) {
// Get error from audio thread (if any)
return p->audioPlayback->getError();
} else {
return "";
}
}
/// Get Audio Devices from JUCE
std::vector<openshot::AudioDeviceInfo> QtPlayer::GetAudioDeviceNames() {
if (reader && threads_started) {
return p->audioPlayback->getAudioDeviceNames();
} else {
return std::vector<openshot::AudioDeviceInfo>();
}
}
void QtPlayer::SetSource(const std::string &source)
{
FFmpegReader *ffreader = new FFmpegReader(source);
ffreader->DisplayInfo();
// Use default sample rate (or use the FFmpegReader's audio settings if any)
int sample_rate = 44100;
if (ffreader->info.sample_rate > 0)
sample_rate = ffreader->info.sample_rate;
// Use default channels (or use the FFmpegReader's audio settings if any)
int channels = 2;
if (ffreader->info.channels > 0)
channels = ffreader->info.channels;
// Use default channel layout (or use the FFmpegReader's audio settings if any)
openshot::ChannelLayout channel_layout = openshot::LAYOUT_STEREO;
if (channels != 2)
channel_layout = ffreader->info.channel_layout;
// Create timeline instance (720p, since we have no re-scaling in this player yet)
reader = new Timeline(1280, 720, ffreader->info.fps, sample_rate, channels, channel_layout);
Clip *c = new Clip(source);
Timeline* tm = (Timeline*)reader;
tm->AddClip(c);
tm->Open();
// Set the reader
Reader(reader);
}
void QtPlayer::Play()
{
// Set mode to playing, and speed to normal
mode = PLAYBACK_PLAY;
Speed(1);
if (reader && !threads_started) {
// Start thread only once
p->startPlayback();
threads_started = true;
}
}
void QtPlayer::Loading()
{
mode = PLAYBACK_LOADING;
}
/// Get the current mode
openshot::PlaybackMode QtPlayer::Mode()
{
return mode;
}
void QtPlayer::Pause()
{
mode = PLAYBACK_PAUSED;
Speed(0);
}
int64_t QtPlayer::Position()
{
return p->video_position;
}
void QtPlayer::Seek(int64_t new_frame)
{
// Check for seek
if (reader && threads_started && new_frame > 0) {
// Notify cache thread that seek has occurred
p->videoCache->Seek(new_frame);
// Update current position
p->video_position = new_frame;
// Clear last position (to force refresh)
p->last_video_position = 0;
// Notify audio thread that seek has occurred
p->audioPlayback->Seek(new_frame);
}
}
void QtPlayer::Stop()
{
// Change mode to stopped
mode = PLAYBACK_STOPPED;
// Notify threads of stopping
if (reader && threads_started) {
p->videoCache->Stop();
p->audioPlayback->Stop();
// Kill all threads
p->stopPlayback();
}
p->video_position = 0;
threads_started = false;
}
// Set the reader object
void QtPlayer::Reader(openshot::ReaderBase *new_reader)
{
// Set new reader. Note: Be sure to close and dispose of the old reader after calling this
reader = new_reader;
p->reader = new_reader;
p->videoCache->Reader(new_reader);
p->audioPlayback->Reader(new_reader);
}
// Get the current reader, such as a FFmpegReader
openshot::ReaderBase* QtPlayer::Reader() {
return reader;
}
// Set the QWidget pointer to display the video on (as a LONG pointer id)
void QtPlayer::SetQWidget(int64_t qwidget_address) {
// Update override QWidget address on the video renderer
p->renderer->OverrideWidget(qwidget_address);
}
// Get the Renderer pointer address (for Python to cast back into a QObject)
int64_t QtPlayer::GetRendererQObject() {
return (int64_t)(VideoRenderer*)p->renderer;
}
// Get the Playback speed
float QtPlayer::Speed() {
return speed;
}
// Set the Playback speed multiplier (1.0 = normal speed, <1.0 = slower, >1.0 faster)
void QtPlayer::Speed(float new_speed) {
speed = new_speed;
p->speed = new_speed;
p->videoCache->setSpeed(new_speed);
if (p->reader->info.has_audio)
p->audioPlayback->setSpeed(new_speed);
}
// Get the Volume
float QtPlayer::Volume() {
return volume;
}
// Set the Volume multiplier (1.0 = normal volume, <1.0 = quieter, >1.0 louder)
void QtPlayer::Volume(float new_volume) {
volume = new_volume;
}
}