| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "gui/mediaplayer/libmpv/libmpvbackend.h" |
| 4 | |
| 5 | #include "3rd-party/boolinq/boolinq.h" |
| 6 | #include "definitions/definitions.h" |
| 7 | #include "gui/mediaplayer/libmpv/libmpvwidget.h" |
| 8 | #include "gui/mediaplayer/libmpv/qthelper.h" |
| 9 | #include "miscellaneous/settings.h" |
| 10 | #include "miscellaneous/textfactory.h" |
| 11 | |
| 12 | #include <clocale> |
| 13 | #include <sstream> |
| 14 | #include <stdexcept> |
| 15 | |
| 16 | #include <QDir> |
| 17 | #include <QJsonArray> |
| 18 | #include <QJsonDocument> |
| 19 | #include <QKeyEvent> |
| 20 | #include <QLayout> |
| 21 | |
| 22 | #define EVENT_CODE_FS 2 |
| 23 | #define EVENT_CODE_VOLUME 3 |
| 24 | #define EVENT_CODE_DURATION 4 |
| 25 | #define EVENT_CODE_MUTE 5 |
| 26 | #define EVENT_CODE_POSITION 6 |
| 27 | #define EVENT_CODE_SPEED 7 |
| 28 | #define EVENT_CODE_SEEKABLE 8 |
| 29 | #define EVENT_CODE_TRACKS 9 |
| 30 | #define EVENT_CODE_PAUSE 10 |
| 31 | #define EVENT_CODE_IDLE 11 |
| 32 | #define EVENT_CODE_STOP 12 |
| 33 | |
| 34 | #define CONFIG_MAIN_NAME "mpv.conf" |
| 35 | #define CONFIG_INPUT_NAME "input.conf" |
| 36 | |
| 37 | LibMpvBackend::LibMpvBackend(Application* app, QWidget* parent) |
| 38 | : PlayerBackend(app, parent), m_mpvContainer(nullptr), m_mpvHandle(nullptr) { |
| 39 | installEventFilter(this); |
| 40 | loadSettings(); |
| 41 | |
| 42 | m_mpvHandle = mpv_create(); |
| 43 | m_mpvContainer = new LibMpvWidget(m_mpvHandle, this); |
| 44 | |
| 45 | if (m_mpvHandle == nullptr) { |
| 46 | qFatal("cannot create mpv instance"); |
| 47 | } |
| 48 | |
| 49 | setMouseTracking(true); |
| 50 | layout()->addWidget(m_mpvContainer); |
| 51 | m_mpvContainer->bind(); |
| 52 | |
| 53 | mpv_set_option_string(m_mpvHandle, "msg-level", "all=v"); |
| 54 | mpv_set_option_string(m_mpvHandle, "config", "yes"); |
| 55 | mpv_set_option_string(m_mpvHandle, "script-opts", "osc-idlescreen=no"); |
| 56 | mpv_set_option_string(m_mpvHandle, "hwdec", "auto"); |
| 57 | |
| 58 | #if defined(MEDIAPLAYER_LIBMPV_OPENGL) |
| 59 | mpv_set_option_string(m_mpvHandle, "vo", "libmpv"); |
| 60 | #endif |
| 61 | |
| 62 | mpv_set_option_string(m_mpvHandle, "osd-playing-msg", "${media-title}"); |
| 63 | mpv_set_option_string(m_mpvHandle, "osc", "yes"); |
| 64 | mpv_set_option_string(m_mpvHandle, "input-cursor", "yes"); |
| 65 | mpv_set_option_string(m_mpvHandle, "idle", "yes"); |
| 66 | mpv_set_option_string(m_mpvHandle, "save-position-on-quit", "no"); |
| 67 | mpv_set_option_string(m_mpvHandle, "no-resume-playback", "yes"); |
| 68 | |
| 69 | #if !defined(NDEBUG) |
| 70 | mpv_set_option_string(m_mpvHandle, "terminal", "yes"); |
| 71 | #endif |
| 72 | |
| 73 | if (!m_customConfigFolder.isEmpty()) { |
| 74 | QByteArray cfg_folder = QDir::toNativeSeparators(m_customConfigFolder).toLocal8Bit(); |
| 75 | |
| 76 | mpv_set_option_string(m_mpvHandle, "config-dir", cfg_folder.constData()); |
| 77 | } |
| 78 | else { |
| 79 | mpv_set_option_string(m_mpvHandle, "input-default-bindings", "yes"); |
| 80 | } |
| 81 | |
| 82 | // Observe some properties. |
| 83 | mpv_observe_property(m_mpvHandle, EVENT_CODE_FS, "fullscreen", MPV_FORMAT_FLAG); |
| 84 | mpv_observe_property(m_mpvHandle, EVENT_CODE_VOLUME, "volume", MPV_FORMAT_INT64); |
| 85 | mpv_observe_property(m_mpvHandle, EVENT_CODE_DURATION, "duration", MPV_FORMAT_INT64); |
| 86 | mpv_observe_property(m_mpvHandle, EVENT_CODE_MUTE, "mute", MPV_FORMAT_FLAG); |
| 87 | mpv_observe_property(m_mpvHandle, EVENT_CODE_POSITION, "time-pos", MPV_FORMAT_INT64); |
| 88 | mpv_observe_property(m_mpvHandle, EVENT_CODE_SPEED, "speed", MPV_FORMAT_DOUBLE); |
| 89 | mpv_observe_property(m_mpvHandle, EVENT_CODE_SEEKABLE, "seekable", MPV_FORMAT_FLAG); |
| 90 | mpv_observe_property(m_mpvHandle, EVENT_CODE_PAUSE, "pause", MPV_FORMAT_FLAG); |
| 91 | mpv_observe_property(m_mpvHandle, EVENT_CODE_IDLE, "idle-active", MPV_FORMAT_FLAG); |
| 92 | mpv_observe_property(m_mpvHandle, EVENT_CODE_TRACKS, "track-list", MPV_FORMAT_NODE); |
| 93 | |
| 94 | // From this point on, the wakeup function will be called. The callback |
| 95 | // can come from any thread, so we use the QueuedConnection mechanism to |
| 96 | // relay the wakeup in a thread-safe way. |
| 97 | connect(m_mpvContainer, |
| 98 | &LibMpvWidget::launchMpvEvents, |
| 99 | this, |
| 100 | &LibMpvBackend::onMpvEvents, |
| 101 | Qt::ConnectionType::QueuedConnection); |
| 102 | |
| 103 | if (mpv_initialize(m_mpvHandle) < 0) { |
| 104 | qFatal("cannot create mpv instance"); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void LibMpvBackend::destroyHandle() { |
| 109 | if (m_mpvHandle != nullptr) { |
| 110 | mpv_terminate_destroy(m_mpvHandle); |
| 111 | m_mpvHandle = nullptr; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void LibMpvBackend::loadSettings() { |
| 116 | if (m_app->settings()->value(GROUP(VideoPlayer), SETTING(VideoPlayer::MpvUseCustomConfigFolder)).toBool()) { |
| 117 | m_customConfigFolder = |
| 118 | m_app->replaceUserDataFolderPlaceholder(m_app->settings() |
| 119 | ->value(GROUP(VideoPlayer), SETTING(VideoPlayer::MpvCustomConfigFolder)) |
| 120 | .toString()); |
| 121 | |
| 122 | installCustomConfig(m_customConfigFolder); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | LibMpvBackend::~LibMpvBackend() { |
| 127 | m_mpvContainer->destroyHandle(); |
| 128 | destroyHandle(); |
| 129 | } |
| 130 | |
| 131 | void LibMpvBackend::handleMpvEvent(mpv_event* event) { |
| 132 | switch (event->event_id) { |
| 133 | case MPV_EVENT_PROPERTY_CHANGE: { |
| 134 | mpv_event_property* prop = (mpv_event_property*)event->data; |
| 135 | processPropertyChange(prop, event->reply_userdata); |
| 136 | break; |
| 137 | } |
| 138 | |
| 139 | case MPV_EVENT_FILE_LOADED: |
| 140 | emit statusChanged(tr("File loaded")); |
| 141 | emit playbackStateChanged(PlayerBackend::PlaybackState::PlayingState); |
| 142 | break; |
| 143 | |
| 144 | case MPV_EVENT_END_FILE: { |
| 145 | mpv_event_end_file* end_file = (mpv_event_end_file*)event->data; |
| 146 | processEndFile(end_file); |
| 147 | break; |
| 148 | } |
| 149 | |
| 150 | case MPV_EVENT_COMMAND_REPLY: |
| 151 | break; |
| 152 | |
| 153 | case MPV_EVENT_VIDEO_RECONFIG: |
| 154 | break; |
| 155 | |
| 156 | case MPV_EVENT_LOG_MESSAGE: { |
| 157 | mpv_event_log_message* msg = (mpv_event_log_message*)event->data; |
| 158 | processLogMessage(msg); |
| 159 | } |
| 160 | |
| 161 | case MPV_EVENT_SHUTDOWN: { |
| 162 | // destroyHandle(); |
| 163 | emit closed(); |
| 164 | break; |
| 165 | } |
| 166 | |
| 167 | default: |
| 168 | // Ignore uninteresting or unknown events. |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | const char* LibMpvBackend::mpvDecodeString(void* data) const { |
| 174 | return *(char**)data; |
| 175 | } |
| 176 | |
| 177 | bool LibMpvBackend::mpvDecodeBool(void* data) const { |
| 178 | return mpvDecodeInt(data) != 0; |
| 179 | } |
| 180 | |
| 181 | int LibMpvBackend::mpvDecodeInt(void* data) const { |
| 182 | return *(int*)data; |
| 183 | } |
| 184 | |
| 185 | double LibMpvBackend::mpvDecodeDouble(void* data) const { |
| 186 | return *(double*)data; |
| 187 | } |
| 188 | |
| 189 | QString LibMpvBackend::mpvEncodeKeyboardButton(int btn) const { |
| 190 | return QString((QChar)btn); |
| 191 | } |
| 192 | |
| 193 | QString LibMpvBackend::errorToString(mpv_error error) const { |
| 194 | switch (mpv_error(error)) { |
| 195 | case mpv_error::MPV_ERROR_EVENT_QUEUE_FULL: |
| 196 | return tr("App restart required"); |
| 197 | |
| 198 | case mpv_error::MPV_ERROR_NOMEM: |
| 199 | return tr("Out of memory"); |
| 200 | |
| 201 | case mpv_error::MPV_ERROR_UNINITIALIZED: |
| 202 | return tr("Not initialized yet"); |
| 203 | |
| 204 | case mpv_error::MPV_ERROR_INVALID_PARAMETER: |
| 205 | return tr("Invalid parameter"); |
| 206 | |
| 207 | case mpv_error::MPV_ERROR_OPTION_NOT_FOUND: |
| 208 | return tr("Option not found"); |
| 209 | |
| 210 | case mpv_error::MPV_ERROR_OPTION_FORMAT: |
| 211 | return tr("Option badly formatted"); |
| 212 | |
| 213 | case mpv_error::MPV_ERROR_OPTION_ERROR: |
| 214 | return tr("Cannot set option"); |
| 215 | |
| 216 | case mpv_error::MPV_ERROR_PROPERTY_NOT_FOUND: |
| 217 | return tr("Property does not existing"); |
| 218 | |
| 219 | case mpv_error::MPV_ERROR_PROPERTY_FORMAT: |
| 220 | return tr("Property badly formatted"); |
| 221 | |
| 222 | case mpv_error::MPV_ERROR_PROPERTY_UNAVAILABLE: |
| 223 | return tr("Property N/A"); |
| 224 | |
| 225 | case mpv_error::MPV_ERROR_PROPERTY_ERROR: |
| 226 | return tr("Cannot set property"); |
| 227 | |
| 228 | case mpv_error::MPV_ERROR_COMMAND: |
| 229 | return tr("Cannot run command"); |
| 230 | |
| 231 | case mpv_error::MPV_ERROR_LOADING_FAILED: |
| 232 | return tr("Loading failed"); |
| 233 | |
| 234 | case mpv_error::MPV_ERROR_AO_INIT_FAILED: |
| 235 | return tr("Cannot initialize audio"); |
| 236 | |
| 237 | case mpv_error::MPV_ERROR_VO_INIT_FAILED: |
| 238 | return tr("Cannot initialize video"); |
| 239 | |
| 240 | case mpv_error::MPV_ERROR_NOTHING_TO_PLAY: |
| 241 | return tr("Not a media file"); |
| 242 | |
| 243 | case mpv_error::MPV_ERROR_UNKNOWN_FORMAT: |
| 244 | return tr("Unknown file format"); |
| 245 | |
| 246 | case mpv_error::MPV_ERROR_UNSUPPORTED: |
| 247 | return tr("Unsupported file format"); |
| 248 | |
| 249 | case mpv_error::MPV_ERROR_NOT_IMPLEMENTED: |
| 250 | default: |
| 251 | return tr("Unknown error (%1)").arg(error); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | void LibMpvBackend::onMpvEvents() { |
| 256 | while (m_mpvHandle != nullptr) { |
| 257 | mpv_event* event = mpv_wait_event(m_mpvHandle, 0); |
| 258 | |
| 259 | if (event->event_id == MPV_EVENT_NONE) { |
| 260 | break; |
| 261 | } |
| 262 | |
| 263 | handleMpvEvent(event); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | void LibMpvBackend::processEndFile(mpv_event_end_file* end_file) { |
| 268 | switch (end_file->reason) { |
| 269 | case MPV_END_FILE_REASON_STOP: |
| 270 | emit statusChanged(tr("Stopped")); |
| 271 | emit playbackStateChanged(PlayerBackend::PlaybackState::StoppedState); |
| 272 | break; |
| 273 | |
| 274 | case MPV_END_FILE_REASON_EOF: |
| 275 | case MPV_END_FILE_REASON_QUIT: |
| 276 | emit statusChanged(tr("File ended")); |
| 277 | emit playbackStateChanged(PlayerBackend::PlaybackState::StoppedState); |
| 278 | break; |
| 279 | |
| 280 | case MPV_END_FILE_REASON_ERROR: |
| 281 | emit errorOccurred(errorToString(mpv_error(end_file->error))); |
| 282 | emit playbackStateChanged(PlayerBackend::PlaybackState::StoppedState); |
| 283 | break; |
| 284 | |
| 285 | case MPV_END_FILE_REASON_REDIRECT: |
| 286 | default: |
| 287 | break; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | void LibMpvBackend::processTracks(const QJsonDocument& json) { |
| 292 | QVariantList vars = json.array().toVariantList(); |
| 293 | auto linq = boolinq::from(vars); |
| 294 | |
| 295 | bool any_audio_track = linq.any([](const QVariant& var) { |
| 296 | return var.toHash().value("type") == QSL("audio"); |
| 297 | }); |
| 298 | |
| 299 | bool any_video_track = linq.any([](const QVariant& var) { |
| 300 | return var.toHash().value("type") == QSL("video"); |
| 301 | }); |
| 302 | |
| 303 | emit audioAvailable(any_audio_track); |
| 304 | emit videoAvailable(any_video_track); |
| 305 | } |
| 306 | |
| 307 | void LibMpvBackend::processPropertyChange(mpv_event_property* prop, uint64_t property_code) { |
| 308 | if (prop == nullptr || prop->data == nullptr) { |
| 309 | return; |
| 310 | } |
| 311 | |
| 312 | switch (property_code) { |
| 313 | case EVENT_CODE_FS: |
| 314 | emit fullscreenChanged(mpvDecodeBool(prop->data)); |
| 315 | break; |
| 316 | |
| 317 | case EVENT_CODE_VOLUME: { |
| 318 | int volume = mpvDecodeInt(prop->data); |
| 319 | emit volumeChanged(volume); |
| 320 | break; |
| 321 | } |
| 322 | |
| 323 | case EVENT_CODE_POSITION: { |
| 324 | int pos = mpvDecodeInt(prop->data); |
| 325 | emit positionChanged(pos); |
| 326 | break; |
| 327 | } |
| 328 | |
| 329 | case EVENT_CODE_DURATION: { |
| 330 | int dr = mpvDecodeInt(prop->data); |
| 331 | emit durationChanged(dr); |
| 332 | break; |
| 333 | } |
| 334 | |
| 335 | case EVENT_CODE_IDLE: { |
| 336 | /* |
| 337 | bool idle = mpvDecodeBool(prop->data); |
| 338 | |
| 339 | if (idle) { |
| 340 | emit playbackStateChanged(PlayerBackend::PlaybackState::StoppedState); |
| 341 | } |
| 342 | */ |
| 343 | break; |
| 344 | } |
| 345 | |
| 346 | case EVENT_CODE_PAUSE: { |
| 347 | bool paused = mpvDecodeBool(prop->data); |
| 348 | |
| 349 | if (paused) { |
| 350 | emit playbackStateChanged(PlayerBackend::PlaybackState::PausedState); |
| 351 | } |
| 352 | else { |
| 353 | emit playbackStateChanged(PlayerBackend::PlaybackState::PlayingState); |
| 354 | } |
| 355 | |
| 356 | break; |
| 357 | } |
| 358 | |
| 359 | case EVENT_CODE_SEEKABLE: |
| 360 | emit seekableChanged(mpvDecodeBool(prop->data)); |
| 361 | break; |
| 362 | |
| 363 | case EVENT_CODE_SPEED: { |
| 364 | double sp = mpvDecodeDouble(prop->data); |
| 365 | emit speedChanged(int(sp * 100)); |
| 366 | break; |
| 367 | } |
| 368 | |
| 369 | case EVENT_CODE_TRACKS: { |
| 370 | if (prop->format == MPV_FORMAT_NODE) { |
| 371 | QVariant v = mpv::qt::node_to_variant((mpv_node*)prop->data); |
| 372 | QJsonDocument d = QJsonDocument::fromVariant(v); |
| 373 | |
| 374 | processTracks(d); |
| 375 | } |
| 376 | |
| 377 | break; |
| 378 | } |
| 379 | |
| 380 | case EVENT_CODE_MUTE: |
| 381 | emit mutedChanged(mpvDecodeBool(prop->data)); |
| 382 | break; |
| 383 | |
| 384 | default: |
| 385 | break; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | void LibMpvBackend::processLogMessage(mpv_event_log_message* msg) { |
| 390 | std::stringstream ss; |
| 391 | ss << "[" << msg->prefix << "] " << msg->level << ": " << msg->text; |
| 392 | |
| 393 | appendLog(QString::fromStdString(ss.str())); |
| 394 | } |
| 395 | |
| 396 | void LibMpvBackend::appendLog(const QString& text) { |
| 397 | qDebugNN << LOGSEC_MPV << text; |
| 398 | } |
| 399 | |
| 400 | bool LibMpvBackend::eventFilter(QObject* watched, QEvent* event) { |
| 401 | Q_UNUSED(watched) |
| 402 | |
| 403 | if (event->type() == QEvent::Type::ShortcutOverride) { |
| 404 | // NOTE: If user presses key which is application-wide assigned to some |
| 405 | // action, do not propagate the shortcut to application. |
| 406 | event->accept(); |
| 407 | return true; |
| 408 | } |
| 409 | |
| 410 | if (m_mpvHandle != nullptr) { |
| 411 | if (event->type() == QEvent::Type::Wheel && watched == this) { |
| 412 | auto* mouse_event = dynamic_cast<QWheelEvent*>(event); |
| 413 | bool is_up = mouse_event->angleDelta().y() >= 0; |
| 414 | |
| 415 | qDebugNN << LOGSEC_MPV << "Wheel:" << QUOTE_W_SPACE_DOT(is_up); |
| 416 | |
| 417 | const char* args[] = {"keypress", is_up ? "MOUSE_BTN3" : "MOUSE_BTN4", nullptr}; |
| 418 | |
| 419 | mpv_command_async(m_mpvHandle, 0, args); |
| 420 | event->accept(); |
| 421 | return true; |
| 422 | } |
| 423 | |
| 424 | if ((event->type() == QEvent::Type::MouseButtonRelease || event->type() == QEvent::Type::MouseButtonPress) && |
| 425 | watched == this) { |
| 426 | bool press = event->type() == QEvent::Type::MouseButtonPress; |
| 427 | |
| 428 | qDebugNN << LOGSEC_MPV << "Mouse press/release."; |
| 429 | |
| 430 | const char* args[] = {press ? "keydown" : "keyup", "MOUSE_BTN0", nullptr}; |
| 431 | |
| 432 | mpv_command_async(m_mpvHandle, 0, args); |
| 433 | event->accept(); |
| 434 | return true; |
| 435 | } |
| 436 | |
| 437 | if (event->type() == QEvent::Type::MouseButtonDblClick && watched == this) { |
| 438 | qDebugNN << LOGSEC_MPV << "Mouse double-click."; |
| 439 | |
| 440 | const char* args[] = {"keypress", "MOUSE_BTN0_DBL", nullptr}; |
| 441 | |
| 442 | mpv_command_async(m_mpvHandle, 0, args); |
| 443 | event->accept(); |
| 444 | return true; |
| 445 | } |
| 446 | |
| 447 | if (event->type() == QEvent::Type::MouseMove && watched == this) { |
| 448 | auto* mouse_event = dynamic_cast<QMouseEvent*>(event); |
| 449 | auto scaling_ratio = window()->devicePixelRatio(); |
| 450 | auto position = mouse_event->pos() * scaling_ratio; |
| 451 | auto x_str = QString::number(position.x()).toLocal8Bit(); |
| 452 | auto y_str = QString::number(position.y()).toLocal8Bit(); |
| 453 | const char* x = x_str.constData(); |
| 454 | const char* y = y_str.constData(); |
| 455 | |
| 456 | const char* args[] = {"mouse", x, y, nullptr}; |
| 457 | |
| 458 | mpv_command_async(m_mpvHandle, 0, args); |
| 459 | } |
| 460 | |
| 461 | if (event->type() == QEvent::Type::KeyRelease) { |
| 462 | // We catch all keypresses (even from surrounding widgets). |
| 463 | QKeyEvent* key_event = dynamic_cast<QKeyEvent*>(event); |
| 464 | QString keys = |
| 465 | QKeySequence(key_event->key() | key_event->modifiers()).toString(QKeySequence::SequenceFormat::PortableText); |
| 466 | QByteArray byte_named_key = keys.toLocal8Bit(); |
| 467 | |
| 468 | const char* args[] = {"keypress", byte_named_key.constData(), nullptr}; |
| 469 | |
| 470 | mpv_command_async(m_mpvHandle, 0, args); |
| 471 | event->accept(); |
| 472 | return true; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | return false; |
| 477 | } |
| 478 | |
| 479 | void LibMpvBackend::playUrl(const QUrl& url) { |
| 480 | if (m_mpvHandle != nullptr) { |
| 481 | m_url = url; |
| 482 | |
| 483 | auto eb = url.toString().toLocal8Bit(); |
| 484 | const char* css = eb.data(); |
| 485 | const char* args[] = {"loadfile", css, nullptr}; |
| 486 | mpv_command_async(m_mpvHandle, 0, args); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | void LibMpvBackend::playPause() { |
| 491 | int idle; |
| 492 | mpv_get_property(m_mpvHandle, "idle-active", MPV_FORMAT_FLAG, &idle); |
| 493 | |
| 494 | if (idle) { |
| 495 | playUrl(m_url); |
| 496 | } |
| 497 | else { |
| 498 | int paused; |
| 499 | mpv_get_property(m_mpvHandle, "pause", MPV_FORMAT_FLAG, &paused); |
| 500 | |
| 501 | paused = paused == 0 ? 1 : 0; |
| 502 | |
| 503 | mpv_set_property_async(m_mpvHandle, EVENT_CODE_PAUSE, "pause", MPV_FORMAT_FLAG, &paused); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | void LibMpvBackend::pause() { |
| 508 | int in = 1; |
| 509 | mpv_set_property_async(m_mpvHandle, EVENT_CODE_PAUSE, "pause", MPV_FORMAT_FLAG, &in); |
| 510 | } |
| 511 | |
| 512 | void LibMpvBackend::stop() { |
| 513 | const char* args[] = {"stop", nullptr}; |
| 514 | mpv_command_async(m_mpvHandle, EVENT_CODE_STOP, args); |
| 515 | } |
| 516 | |
| 517 | void LibMpvBackend::setPlaybackSpeed(int speed) { |
| 518 | if (m_mpvHandle != nullptr) { |
| 519 | double sp = speed / 100.0; |
| 520 | mpv_set_property_async(m_mpvHandle, EVENT_CODE_SPEED, "speed", MPV_FORMAT_DOUBLE, &sp); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | void LibMpvBackend::setVolume(int volume) { |
| 525 | if (m_mpvHandle != nullptr) { |
| 526 | uint64_t vol = volume; |
| 527 | mpv_set_property_async(m_mpvHandle, EVENT_CODE_VOLUME, "volume", MPV_FORMAT_INT64, &vol); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | void LibMpvBackend::setPosition(int position) { |
| 532 | if (m_mpvHandle != nullptr) { |
| 533 | uint64_t pos = position; |
| 534 | mpv_set_property_async(m_mpvHandle, EVENT_CODE_POSITION, "time-pos", MPV_FORMAT_INT64, &pos); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | void LibMpvBackend::setFullscreen(bool fullscreen) { |
| 539 | if (m_mpvHandle != nullptr) { |
| 540 | const char* fs = fullscreen ? "yes" : "no"; |
| 541 | mpv_set_property_async(m_mpvHandle, EVENT_CODE_FS, "fullscreen", MPV_FORMAT_STRING, &fs); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | QUrl LibMpvBackend::url() const { |
| 546 | return m_url; |
| 547 | } |
| 548 | |
| 549 | int LibMpvBackend::position() const { |
| 550 | uint64_t out; |
| 551 | mpv_get_property(m_mpvHandle, "time-pos", MPV_FORMAT_INT64, &out); |
| 552 | |
| 553 | return out; |
| 554 | } |
| 555 | |
| 556 | int LibMpvBackend::duration() const { |
| 557 | uint64_t out; |
| 558 | mpv_get_property(m_mpvHandle, "duration", MPV_FORMAT_INT64, &out); |
| 559 | |
| 560 | return out; |
| 561 | } |
| 562 | |
| 563 | void LibMpvBackend::installCustomConfig(const QString& directory) { |
| 564 | QDir().mkpath(directory); |
| 565 | QDir config_dir(directory); |
| 566 | |
| 567 | for (const QString& cfg_file : QStringList{QSL(CONFIG_MAIN_NAME), QSL(CONFIG_INPUT_NAME)}) { |
| 568 | if (!config_dir.exists(cfg_file)) { |
| 569 | qDebugNN << LOGSEC_MPV << "Copying sample" << QUOTE_W_SPACE(cfg_file) << "to" |
| 570 | << QUOTE_W_SPACE_DOT(config_dir.absolutePath()); |
| 571 | IOFactory::copyFile(QSL(":/scripts/mpv/%1").arg(cfg_file), config_dir.absoluteFilePath(cfg_file)); |
| 572 | } |
| 573 | else { |
| 574 | qDebugNN << LOGSEC_MPV << "Configuration file" << QUOTE_W_SPACE(cfg_file) << "already exists."; |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | void LibMpvBackend::setMuted(bool muted) { |
| 580 | if (m_mpvHandle != nullptr) { |
| 581 | const char* mtd = muted ? "yes" : "no"; |
| 582 | mpv_set_property_async(m_mpvHandle, EVENT_CODE_MUTE, "mute", MPV_FORMAT_STRING, &mtd); |
| 583 | } |
| 584 | } |