-
Notifications
You must be signed in to change notification settings - Fork 5
/
mediaplayercontrol.cpp
218 lines (192 loc) · 5.58 KB
/
mediaplayercontrol.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
/*
* Copyright (C) 2018-2020 Wang Bin - wbsecg1 at gmail.com
* https://github.com/wang-bin/qtmultimedia-plugins-mdk
* MIT License
*/
#include "mediaplayercontrol.h"
#include "mdk/MediaInfo.h"
static QMediaPlayer::State toQt(State value) {
switch (value) {
case State::Playing: return QMediaPlayer::PlayingState;
case State::Paused: return QMediaPlayer::PausedState;
case State::Stopped: return QMediaPlayer::StoppedState;
}
return QMediaPlayer::StoppedState;
}
// TODO: mask value&xxx
static QMediaPlayer::MediaStatus toQt(MediaStatus value) {
switch (value) {
case MediaStatus::NoMedia: return QMediaPlayer::NoMedia;
case MediaStatus::Invalid: return QMediaPlayer::InvalidMedia;
default: break;
}
if (test_flag(value & MediaStatus::Loading))
return QMediaPlayer::LoadingMedia;
if (test_flag(value & MediaStatus::Stalled))
return QMediaPlayer::StalledMedia;
if (test_flag(value & MediaStatus::Buffering))
return QMediaPlayer::BufferingMedia;
if (test_flag(value & MediaStatus::Buffered))
return QMediaPlayer::BufferedMedia; // playing or paused
if (test_flag(value & MediaStatus::End))
return QMediaPlayer::EndOfMedia; // playback complete
if (test_flag(value & MediaStatus::Loaded))
return QMediaPlayer::LoadedMedia; // connected, stopped. so last
return QMediaPlayer::UnknownMediaStatus;
}
MediaPlayerControl::MediaPlayerControl(QObject* parent) : QMediaPlayerControl(parent)
{
player_.setVideoDecoders({
#if defined(Q_OS_WIN)
"MFT:d3d=11", "MFT:d3d=9", "D3D11",
#elif defined(Q_OS_DARWIN)
"VT", "VideoToolbox",
#elif defined(Q_OS_ANDROID)
"AMediaCodec:java=0:async=1",
#elif defined(Q_OS_LINUX)
"VAAPI", "VDPAU",
#endif
"CUDA", "FFmpeg"}); // no display for 2nd video
player_.onStateChanged([this](State value){
Q_EMIT stateChanged(toQt(value));
});
player_.onMediaStatusChanged([this](MediaStatus value){
Q_EMIT mediaStatusChanged(toQt(value));
return true;
});
player_.onEvent([this](const MediaEvent& e){
if (e.error < 0) {
if (e.category == "decoder.audio"
|| e.category == "decoder.video") {
Q_EMIT error(QMediaPlayer::FormatError, tr("Unsupported media, a codec is missing."));
}
}
return false;
});
player_.setRenderCallback([this](void*){
Q_EMIT frameAvailable();
});
}
QMediaPlayer::State MediaPlayerControl::state() const
{
return toQt(player_.state());
}
QMediaPlayer::MediaStatus MediaPlayerControl::mediaStatus() const
{
return toQt(player_.mediaStatus());
}
qint64 MediaPlayerControl::duration() const
{
return duration_;
}
qint64 MediaPlayerControl::position() const
{
return player_.position();
}
void MediaPlayerControl::setPosition(qint64 position)
{
player_.seek(position);
}
int MediaPlayerControl::volume() const
{
return vol_;
}
void MediaPlayerControl::setVolume(int volume)
{
vol_ = volume;
player_.setVolume(float(volume)/100.0f);
}
bool MediaPlayerControl::isMuted() const
{
return mute_;
}
void MediaPlayerControl::setMuted(bool muted)
{
mute_ = muted;
player_.setMute(muted);
}
int MediaPlayerControl::bufferStatus() const
{
return 0;
}
bool MediaPlayerControl::isAudioAvailable() const
{
return has_a_;
}
bool MediaPlayerControl::isVideoAvailable() const
{
return has_v_;
}
bool MediaPlayerControl::isSeekable() const
{
return true;
}
QMediaTimeRange MediaPlayerControl::availablePlaybackRanges() const
{
return QMediaTimeRange();
}
qreal MediaPlayerControl::playbackRate() const
{
return qreal(player_.playbackRate());
}
void MediaPlayerControl::setPlaybackRate(qreal rate)
{
const auto old = playbackRate();
if (playbackRate() == rate)
return;
player_.setPlaybackRate(float(rate));
if (playbackRate() != old) // success
Q_EMIT playbackRateChanged(playbackRate());
}
QMediaContent MediaPlayerControl::media() const
{
return QMediaContent();
}
const QIODevice* MediaPlayerControl::mediaStream() const
{
return nullptr;
}
void MediaPlayerControl::setMedia(const QMediaContent& media, QIODevice* io)
{
stop();
if (io) {
player_.setMedia(QString("qio:%1").arg(qintptr(io)).toUtf8().constData());
} else {
if (media.canonicalUrl().isLocalFile())
player_.setMedia(media.canonicalUrl().toLocalFile().toUtf8().constData()); // for windows
else
player_.setMedia(media.canonicalUrl().url().toUtf8().constData());
}
Q_EMIT positionChanged(0);
player_.waitFor(State::Stopped);
player_.prepare(0, [this](int64_t position, bool*){
if (position < 0) {
Q_EMIT error(QMediaPlayer::ResourceError, tr("Failed to load source."));
}
const auto& info = player_.mediaInfo();
duration_ = info.duration;
has_a_ = !info.audio.empty();
has_v_ = !info.video.empty();
Q_EMIT durationChanged(duration_);
Q_EMIT audioAvailableChanged(has_a_);
Q_EMIT videoAvailableChanged(has_v_);
Q_EMIT seekableChanged(position >= 0);
#if defined(MDK_VERSION_CHECK)
# if MDK_VERSION_CHECK(0, 5, 0)
return true;
# endif
#endif
});
}
void MediaPlayerControl::play()
{
player_.setState(State::Playing);
}
void MediaPlayerControl::pause()
{
player_.setState(State::Paused);
}
void MediaPlayerControl::stop()
{
player_.setState(State::Stopped);
}