DanmakuDelayGetter::DanmakuDelayGetter(QStringList &names, QStringList &urls, const QString &danmakuUrl, bool download, QObject *parent) : QObject(parent), names(names), urls(urls), danmakuUrl(danmakuUrl), download(download) { mpv = mpv_create(); if (!mpv) { qDebug("Fails to create mpv instance."); deleteLater(); return; } mpv_set_option(mpv, "no-video", MPV_FORMAT_NONE, NULL); mpv_set_option(mpv, "pause", MPV_FORMAT_NONE, NULL); mpv_set_option_string(mpv, "ao", "null"); mpv_set_option_string(mpv, "user-agent", generateUA(urls.first())); QString host = QUrl(urls.first()).host(); if (referer_table.contains(host)) mpv_set_option_string(mpv, "referrer", referer_table[host].constData()); mpv_observe_property(mpv, 0, "duration", MPV_FORMAT_DOUBLE); mpv_set_wakeup_callback(mpv, postEvent, this); if (mpv_initialize(mpv) < 0) { qDebug("Fails to initialaze mpv instance."); deleteLater(); return; } delay = 0; start(); }
MpvHandler::MpvHandler(int64_t wid, QObject *parent): QObject(parent), baka(static_cast<BakaEngine*>(parent)) { // create mpv mpv = mpv_create(); if(!mpv) throw "Could not create mpv object"; // set mpv options mpv_set_option(mpv, "wid", MPV_FORMAT_INT64, &wid); mpv_set_option_string(mpv, "input-cursor", "no"); // no mouse handling mpv_set_option_string(mpv, "cursor-autohide", "no");// no cursor-autohide, we handle that mpv_set_option_string(mpv, "ytdl", "yes"); // youtube-dl support // get updates when these properties change mpv_observe_property(mpv, 0, "playback-time", MPV_FORMAT_DOUBLE); mpv_observe_property(mpv, 0, "volume", MPV_FORMAT_DOUBLE); mpv_observe_property(mpv, 0, "sid", MPV_FORMAT_INT64); mpv_observe_property(mpv, 0, "aid", MPV_FORMAT_INT64); mpv_observe_property(mpv, 0, "sub-visibility", MPV_FORMAT_FLAG); mpv_observe_property(mpv, 0, "mute", MPV_FORMAT_FLAG); mpv_observe_property(mpv, 0, "core-idle", MPV_FORMAT_FLAG); mpv_observe_property(mpv, 0, "paused-for-cache", MPV_FORMAT_FLAG); // setup callback event handling mpv_set_wakeup_callback(mpv, wakeup, this); }
int main(int argc, char *argv[]) { if (argc != 2) { printf("pass a single media file as argument\n"); return 1; } mpv_handle *ctx = mpv_create(); if (!ctx) { printf("failed creating context\n"); return 1; } // Enable default key bindings, so the user can actually interact with // the player (and e.g. close the window). check_error(mpv_set_option_string(ctx, "input-default-bindings", "yes")); mpv_set_option_string(ctx, "input-vo-keyboard", "yes"); int val = 1; check_error(mpv_set_option(ctx, "osc", MPV_FORMAT_FLAG, &val)); // Done setting up options. check_error(mpv_initialize(ctx)); check_error(mpv_request_log_messages(ctx, "v")); check_error(mpv_stream_cb_add_ro(ctx, "myprotocol", argv[1], open_fn)); // Play this file. const char *cmd[] = {"loadfile", "myprotocol://fake", NULL}; check_error(mpv_command(ctx, cmd)); // Let it play, and wait until the user quits. while (1) { mpv_event *event = mpv_wait_event(ctx, 10000); if (event->event_id == MPV_EVENT_LOG_MESSAGE) { struct mpv_event_log_message *msg = (struct mpv_event_log_message *)event->data; printf("[%s] %s: %s", msg->prefix, msg->level, msg->text); continue; } printf("event: %s\n", mpv_event_name(event->event_id)); if (event->event_id == MPV_EVENT_SHUTDOWN) break; } mpv_terminate_destroy(ctx); return 0; }
void MpvHandler::Volume(int level, bool osd) { if(level > 100) level = 100; else if(level < 0) level = 0; double v = level; if(playState > 0) { mpv_set_property_async(mpv, MPV_REPLY_PROPERTY, "volume", MPV_FORMAT_DOUBLE, &v); if(osd) ShowText(tr("Volume: %0%").arg(QString::number(level))); } else { mpv_set_option(mpv, "volume", MPV_FORMAT_DOUBLE, &v); setVolume(level); } }
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { QMenu *menu = menuBar()->addMenu(tr("&File")); QAction *on_open = new QAction(tr("&Open"), this); on_open->setShortcuts(QKeySequence::Open); on_open->setStatusTip(tr("Open a file")); connect(on_open, SIGNAL(triggered()), this, SLOT(on_file_open())); menu->addAction(on_open); statusBar(); mpv = mpv_create(); if (!mpv) throw "can't create mpv instance"; // Create a video child window. Force Qt to create a native window, and // pass the window ID to the mpv wid option. This doesn't work on OSX, // because Cocoa doesn't support this form of embedding. mpv_container = new QWidget(this); setCentralWidget(mpv_container); mpv_container->setAttribute(Qt::WA_NativeWindow); // If you have a HWND, use: int64_t wid = (intptr_t)hwnd; int64_t wid = mpv_container->winId(); mpv_set_option(mpv, "wid", MPV_FORMAT_INT64, &wid); // Enable default bindings, because we're lazy. Normally, a player using // mpv as backend would implement its own key bindings. mpv_set_option_string(mpv, "input-default-bindings", "yes"); // Let us receive property change events with MPV_EVENT_PROPERTY_CHANGE if // this property changes. mpv_observe_property(mpv, 0, "time-pos", MPV_FORMAT_DOUBLE); // From this point on, the wakeup function will be called. The callback // can come from any thread, so we use the Qt QEvent mechanism to relay // the wakeup in a thread-safe way. mpv_set_wakeup_callback(mpv, wakeup, this); if (mpv_initialize(mpv) < 0) throw "mpv failed to initialize"; }
int main(int argc, char *argv[]) { if (argc != 2) { printf("pass a single media file as argument\n"); return 1; } mpv_handle *ctx = mpv_create(); if (!ctx) { printf("failed creating context\n"); return 1; } // Enable default key bindings, so the user can actually interact with // the player (and e.g. close the window). check_error(mpv_set_option_string(ctx, "input-default-bindings", "yes")); mpv_set_option_string(ctx, "input-x11-keyboard", "yes"); int val = 1; check_error(mpv_set_option(ctx, "osc", MPV_FORMAT_FLAG, &val)); // Done setting up options. check_error(mpv_initialize(ctx)); // Play this file. const char *cmd[] = {"loadfile", argv[1], NULL}; check_error(mpv_command(ctx, cmd)); // Let it play, and wait until the user quits. while (1) { mpv_event *event = mpv_wait_event(ctx, 10000); printf("event: %s\n", mpv_event_name(event->event_id)); if (event->event_id == MPV_EVENT_SHUTDOWN) break; } mpv_terminate_destroy(ctx); return 0; }
void gmpv_mpv_obj_initialize(GmpvMpvObj *mpv) { GSettings *main_settings = g_settings_new(CONFIG_ROOT); gchar *config_dir = get_config_dir_path(); gchar *mpvopt = NULL; gchar *current_vo = NULL; gchar *mpv_version = NULL; const struct { const gchar *name; const gchar *value; } options[] = { {"osd-level", "1"}, {"softvol", "yes"}, {"force-window", "yes"}, {"input-default-bindings", "yes"}, {"audio-client-name", ICON_NAME}, {"title", "${media-title}"}, {"autofit-larger", "75%"}, {"window-scale", "1"}, {"pause", "no"}, {"ytdl", "yes"}, {"osd-bar", "no"}, {"input-cursor", "no"}, {"cursor-autohide", "no"}, {"softvol-max", "100"}, {"config", "yes"}, {"screenshot-template", "gnome-mpv-shot%n"}, {"config-dir", config_dir}, {NULL, NULL} }; g_assert(mpv->mpv_ctx); for(gint i = 0; options[i].name; i++) { g_debug( "Applying default option --%s=%s", options[i].name, options[i].value ); mpv_set_option_string( mpv->mpv_ctx, options[i].name, options[i].value ); } if(g_settings_get_boolean(main_settings, "mpv-config-enable")) { gchar *mpv_conf = g_settings_get_string (main_settings, "mpv-config-file"); g_info("Loading config file: %s", mpv_conf); mpv_load_config_file(mpv->mpv_ctx, mpv_conf); g_free(mpv_conf); } if(g_settings_get_boolean(main_settings, "mpv-input-config-enable")) { gchar *input_conf = g_settings_get_string (main_settings, "mpv-input-config-file"); g_info("Loading input config file: %s", input_conf); load_input_conf(mpv, input_conf); g_free(input_conf); } else { load_input_conf(mpv, NULL); } mpvopt = g_settings_get_string(main_settings, "mpv-options"); g_debug("Applying extra mpv options: %s", mpvopt); /* Apply extra options */ if(mpv_obj_apply_args(mpv->mpv_ctx, mpvopt) < 0) { const gchar *msg = _("Failed to apply one or more MPV options."); g_signal_emit_by_name(mpv, "mpv-error", msg); } if(mpv->force_opengl) { g_info("Forcing --vo=opengl-cb"); mpv_set_option_string(mpv->mpv_ctx, "vo", "opengl-cb"); } else { g_debug( "Attaching mpv window to wid %#x", (guint)mpv->wid ); mpv_set_option(mpv->mpv_ctx, "wid", MPV_FORMAT_INT64, &mpv->wid); } mpv_observe_property(mpv->mpv_ctx, 0, "aid", MPV_FORMAT_INT64); mpv_observe_property(mpv->mpv_ctx, 0, "chapters", MPV_FORMAT_INT64); mpv_observe_property(mpv->mpv_ctx, 0, "core-idle", MPV_FORMAT_FLAG); mpv_observe_property(mpv->mpv_ctx, 0, "fullscreen", MPV_FORMAT_FLAG); mpv_observe_property(mpv->mpv_ctx, 0, "pause", MPV_FORMAT_FLAG); mpv_observe_property(mpv->mpv_ctx, 0, "length", MPV_FORMAT_DOUBLE); mpv_observe_property(mpv->mpv_ctx, 0, "media-title", MPV_FORMAT_STRING); mpv_observe_property(mpv->mpv_ctx, 0, "playlist-pos", MPV_FORMAT_INT64); mpv_observe_property(mpv->mpv_ctx, 0, "track-list", MPV_FORMAT_NODE); mpv_observe_property(mpv->mpv_ctx, 0, "volume", MPV_FORMAT_DOUBLE); mpv_set_wakeup_callback(mpv->mpv_ctx, wakeup_callback, mpv); mpv_check_error(mpv_initialize(mpv->mpv_ctx)); mpv_version = gmpv_mpv_obj_get_property_string(mpv, "mpv-version"); current_vo = gmpv_mpv_obj_get_property_string(mpv, "current-vo"); g_info("Using %s", mpv_version); if(current_vo && !GDK_IS_X11_DISPLAY(gdk_display_get_default())) { g_info( "The chosen vo is %s but the display is not X11; " "forcing --vo=opengl-cb and resetting", current_vo ); mpv->force_opengl = TRUE; mpv->state.paused = FALSE; gmpv_mpv_obj_reset(mpv); } else { GSettings *win_settings; gdouble volume; win_settings = g_settings_new(CONFIG_WIN_STATE); volume = g_settings_get_double(win_settings, "volume")*100; g_debug("Setting volume to %f", volume); mpv_set_property( mpv->mpv_ctx, "volume", MPV_FORMAT_DOUBLE, &volume ); /* The vo should be opengl-cb if current_vo is NULL*/ if(!current_vo) { mpv->opengl_ctx = mpv_get_sub_api ( mpv->mpv_ctx, MPV_SUB_API_OPENGL_CB ); } gmpv_mpv_opt_handle_msg_level(mpv); gmpv_mpv_opt_handle_fs(mpv); gmpv_mpv_opt_handle_geometry(mpv); mpv->force_opengl = FALSE; mpv->state.ready = TRUE; g_signal_emit_by_name(mpv, "mpv-init"); g_clear_object(&win_settings); } g_clear_object(&main_settings); g_free(config_dir); g_free(mpvopt); mpv_free(current_vo); mpv_free(mpv_version); }