4 #include <mpv/qthelper.hpp>
6 #ifndef MEDIA_AUDIOONLY
11 void wakeup(void *ctx) {
12 // This callback is invoked from any mpv thread (but possibly also
13 // recursively from a thread that is calling the mpv API). Just notify
14 // the Qt GUI thread to wake up (so that it can process events with
15 // mpv_wait_event()), and return as quickly as possible.
16 MediaMPV *mediaMPV = (MediaMPV *)ctx;
17 emit mediaMPV->mpvEvents();
22 MediaMPV::MediaMPV(QObject *parent) : Media(parent), widget(nullptr) {
23 QThread *thread = new QThread(this);
26 connect(this, &QObject::destroyed, thread, &QThread::quit);
29 // Qt sets the locale in the QApplication constructor, but libmpv requires
30 // the LC_NUMERIC category to be set to "C", so change it back.
31 std::setlocale(LC_NUMERIC, "C");
35 if (!mpv) qFatal("Cannot create MPV instance");
37 mpv_set_option_string(mpv, "config", "no");
38 mpv_set_option_string(mpv, "audio-display", "no");
39 mpv_set_option_string(mpv, "gapless-audio", "weak");
40 mpv_set_option_string(mpv, "idle", "yes");
41 mpv_set_option_string(mpv, "input-default-bindings", "no");
42 mpv_set_option_string(mpv, "input-vo-keyboard", "no");
43 mpv_set_option_string(mpv, "input-cursor", "no");
44 mpv_set_option_string(mpv, "input-media-keys", "no");
45 mpv_set_option_string(mpv, "ytdl", "no");
46 mpv_set_option_string(mpv, "fs", "no");
47 mpv_set_option_string(mpv, "osd-level", "0");
48 mpv_set_option_string(mpv, "quiet", "yes");
49 mpv_set_option_string(mpv, "load-scripts", "no");
50 mpv_set_option_string(mpv, "audio-client-name",
51 QCoreApplication::applicationName().toUtf8().data());
52 mpv_set_option_string(mpv, "hwdec", "auto");
53 mpv_set_option_string(mpv, "vo", "libmpv");
55 mpv_set_option_string(mpv, "cache", "no");
56 mpv_set_option_string(mpv, "demuxer-max-bytes", "10485760");
57 mpv_set_option_string(mpv, "demuxer-max-back-bytes", "10485760");
60 widget = new QWidget();
61 widget->setAttribute(Qt::WA_DontCreateNativeAncestors);
62 widget->setAttribute(Qt::WA_NativeWindow);
63 // If you have a HWND, use: int64_t wid = (intptr_t)hwnd;
64 int64_t wid = (intptr_t)widget->winId();
65 mpv_set_option(mpv, "wid", MPV_FORMAT_INT64, &wid);
68 #ifndef QT_NO_DEBUG_OUTPUT
69 // Request log messages
70 // They are received as MPV_EVENT_LOG_MESSAGE.
71 mpv_request_log_messages(mpv, "info");
74 // From this point on, the wakeup function will be called. The callback
75 // can come from any thread, so we use the QueuedConnection mechanism to
76 // relay the wakeup in a thread-safe way.
77 connect(this, &MediaMPV::mpvEvents, this, &MediaMPV::onMpvEvents, Qt::QueuedConnection);
78 mpv_set_wakeup_callback(mpv, wakeup, this);
80 if (mpv_initialize(mpv) < 0) qFatal("mpv failed to initialize");
82 // Let us receive property change events with MPV_EVENT_PROPERTY_CHANGE if
83 // this property changes.
84 mpv_observe_property(mpv, 0, "time-pos", MPV_FORMAT_DOUBLE);
85 mpv_observe_property(mpv, 0, "duration", MPV_FORMAT_DOUBLE);
86 mpv_observe_property(mpv, 0, "volume", MPV_FORMAT_DOUBLE);
87 mpv_observe_property(mpv, 0, "mute", MPV_FORMAT_FLAG);
90 // This slot is invoked by wakeup() (through the mpvEvents signal).
91 void MediaMPV::onMpvEvents() {
92 // Process all events, until the event queue is empty.
94 mpv_event *event = mpv_wait_event(mpv, 0);
95 if (event->event_id == MPV_EVENT_NONE) break;
96 handleMpvEvent(event);
100 void MediaMPV::checkAboutToFinish(qint64 position) {
101 if (!aboutToFinishEmitted && currentState == Media::PlayingState) {
102 const qint64 dur = duration();
103 if (dur > 0 && dur - position < 5000) {
104 aboutToFinishEmitted = true;
105 qDebug() << "aboutToFinish" << position << dur;
106 emit aboutToFinish();
111 void MediaMPV::handleMpvEvent(mpv_event *event) {
112 // qDebug() << event->data;
113 switch (event->event_id) {
114 case MPV_EVENT_START_FILE:
116 emit sourceChanged();
117 setState(Media::LoadingState);
121 setState(Media::BufferingState);
124 case MPV_EVENT_FILE_LOADED:
125 setState(Media::PlayingState);
128 case MPV_EVENT_PLAYBACK_RESTART:
129 case MPV_EVENT_UNPAUSE:
130 setState(Media::PlayingState);
133 case MPV_EVENT_END_FILE: {
134 struct mpv_event_end_file *eof_event = (struct mpv_event_end_file *)event->data;
135 if (eof_event->reason == MPV_END_FILE_REASON_EOF ||
136 eof_event->reason == MPV_END_FILE_REASON_ERROR) {
137 qDebug() << "Finished";
138 setState(Media::StoppedState);
144 case MPV_EVENT_PAUSE:
145 setState(Media::PausedState);
148 case MPV_EVENT_PROPERTY_CHANGE: {
149 mpv_event_property *prop = (mpv_event_property *)event->data;
150 // qDebug() << prop->name << prop->data;
152 if (strcmp(prop->name, "time-pos") == 0) {
153 if (prop->format == MPV_FORMAT_DOUBLE) {
154 double seconds = *(double *)prop->data;
155 qint64 ms = seconds * 1000.;
156 emit positionChanged(ms);
157 checkAboutToFinish(ms);
161 else if (strcmp(prop->name, "volume") == 0) {
162 if (prop->format == MPV_FORMAT_DOUBLE) {
163 double vol = *(double *)prop->data;
164 emit volumeChanged(vol / 100.);
168 else if (strcmp(prop->name, "mute") == 0) {
169 if (prop->format == MPV_FORMAT_FLAG) {
170 int mute = *(int *)prop->data;
171 emit volumeMutedChanged(mute == 1);
178 case MPV_EVENT_LOG_MESSAGE: {
179 struct mpv_event_log_message *msg = (struct mpv_event_log_message *)event->data;
180 qDebug() << "[" << msg->prefix << "] " << msg->level << ": " << msg->text;
182 if (msg->log_level == MPV_LOG_LEVEL_ERROR) {
183 lastErrorString = QString::fromUtf8(msg->text);
184 emit error(lastErrorString);
190 case MPV_EVENT_SHUTDOWN: {
191 mpv_terminate_destroy(mpv);
201 void MediaMPV::sendCommand(const char *args[]) {
202 // mpv_command_async(mpv, 0, args);
203 mpv_command(mpv, args);
206 void MediaMPV::setState(Media::State value) {
207 if (value != currentState) {
208 currentState = value;
209 emit stateChanged(currentState);
213 void MediaMPV::clearTrackState() {
214 lastErrorString.clear();
215 aboutToFinishEmitted = false;
218 void MediaMPV::setAudioOnly(bool value) {
222 #ifndef MEDIA_AUDIOONLY
224 void MediaMPV::setRenderer(const QString &name) {
225 mpv_set_option_string(mpv, "vo", name.toUtf8().data());
228 QWidget *MediaMPV::videoWidget() {
230 widget = new MpvWidget(mpv);
235 void MediaMPV::playSeparateAudioAndVideo(const QString &video, const QString &audio) {
236 const QByteArray fileUtf8 = video.toUtf8();
237 const char *args[] = {"loadfile", fileUtf8.constData(), nullptr};
240 qApp->processEvents();
242 const QByteArray audioUtf8 = audio.toUtf8();
243 const char *args2[] = {"audio-add", audioUtf8.constData(), nullptr};
249 void MediaMPV::snapshot() {
250 if (currentState == State::StoppedState) return;
252 const QVariantList args = {"screenshot-raw", "video"};
253 mpv::qt::node_builder nodeBuilder(args);
255 const int ret = mpv_command_node(mpv, nodeBuilder.node(), &node);
257 emit error("Cannot take snapshot");
261 mpv::qt::node_autofree auto_free(&node);
262 if (node.format != MPV_FORMAT_NODE_MAP) {
263 emit error("Cannot take snapshot");
270 mpv_node_list *list = node.u.list;
271 uchar *data = nullptr;
273 for (int i = 0; i < list->num; ++i) {
274 const char *key = list->keys[i];
275 if (strcmp(key, "w") == 0) {
276 width = static_cast<int>(list->values[i].u.int64);
277 } else if (strcmp(key, "h") == 0) {
278 height = static_cast<int>(list->values[i].u.int64);
279 } else if (strcmp(key, "stride") == 0) {
280 stride = static_cast<int>(list->values[i].u.int64);
281 } else if (strcmp(key, "data") == 0) {
282 data = static_cast<uchar *>(list->values[i].u.ba->data);
286 if (data != nullptr) {
287 QImage img = QImage(data, width, height, stride, QImage::Format_RGB32);
289 emit snapshotReady(img);
295 void MediaMPV::init() {}
297 Media::State MediaMPV::state() const {
301 void MediaMPV::play(const QString &file) {
302 const QByteArray fileUtf8 = file.toUtf8();
303 const char *args[] = {"loadfile", fileUtf8.constData(), nullptr};
307 if (currentState == Media::PausedState) play();
310 void MediaMPV::play() {
312 mpv_set_property_async(mpv, 0, "pause", MPV_FORMAT_FLAG, &flag);
315 void MediaMPV::pause() {
317 mpv_set_property_async(mpv, 0, "pause", MPV_FORMAT_FLAG, &flag);
320 void MediaMPV::stop() {
321 const char *args[] = {"stop", nullptr};
325 void MediaMPV::seek(qint64 ms) {
326 double seconds = ms / 1000.;
327 QByteArray ba = QString::number(seconds).toUtf8();
328 const char *args[] = {"seek", ba.constData(), "absolute", nullptr};
332 QString MediaMPV::file() const {
334 mpv_get_property(mpv, "path", MPV_FORMAT_STRING, &path);
335 return QString::fromUtf8(path);
338 void MediaMPV::setBufferMilliseconds(qint64 value) {
343 void MediaMPV::setUserAgent(const QString &value) {
344 mpv_set_option_string(mpv, "user-agent", value.toUtf8());
347 void MediaMPV::enqueue(const QString &file) {
348 const QByteArray fileUtf8 = file.toUtf8();
349 const char *args[] = {"loadfile", fileUtf8.constData(), "append", nullptr};
353 void MediaMPV::clearQueue() {
354 const char *args[] = {"playlist-clear", nullptr};
358 bool MediaMPV::hasQueue() const {
360 int r = mpv_get_property(mpv, "playlist", MPV_FORMAT_NODE, &node);
361 if (r < 0) return false;
362 QVariant v = mpv::qt::node_to_variant(&node);
363 mpv_free_node_contents(&node);
364 QVariantList list = v.toList();
365 return list.count() > 1;
368 qint64 MediaMPV::position() const {
370 mpv_get_property(mpv, "time-pos", MPV_FORMAT_DOUBLE, &seconds);
371 return seconds * 1000.;
374 qint64 MediaMPV::duration() const {
376 mpv_get_property(mpv, "duration", MPV_FORMAT_DOUBLE, &seconds);
377 return seconds * 1000.;
380 qint64 MediaMPV::remainingTime() const {
382 mpv_get_property(mpv, "time-remaining", MPV_FORMAT_DOUBLE, &seconds);
383 return seconds * 1000.;
386 qreal MediaMPV::volume() const {
388 mpv_get_property(mpv, "volume", MPV_FORMAT_DOUBLE, &vol);
392 void MediaMPV::setVolume(qreal value) {
393 double percent = value * 100.;
394 mpv_set_property_async(mpv, 0, "volume", MPV_FORMAT_DOUBLE, &percent);
397 bool MediaMPV::volumeMuted() const {
399 mpv_get_property(mpv, "mute", MPV_FORMAT_FLAG, &mute);
403 void MediaMPV::setVolumeMuted(bool value) {
404 int mute = value ? 1 : 0;
405 mpv_set_property(mpv, "mute", MPV_FORMAT_FLAG, &mute);
408 QString MediaMPV::errorString() const {
409 return lastErrorString;