add mpv demo
This commit is contained in:
76
src/main.cpp
76
src/main.cpp
@@ -1,6 +1,9 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
|
||||
#include <mpv/client.h>
|
||||
#include <mpv/render_gl.h>
|
||||
|
||||
#define GLFW_INCLUDE_NONE
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
@@ -20,24 +23,56 @@ extern "C" void userAppExit() {
|
||||
}
|
||||
#endif
|
||||
|
||||
static void die(const char* msg) {
|
||||
fprintf(stderr, "%s\n", msg);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Main code
|
||||
int main(int argc, char* argv[]) {
|
||||
if (!glfwInit()) return -1;
|
||||
if (argc < 2) die("pass a single media file as argument");
|
||||
|
||||
if (!glfwInit()) die("init glfw failed");
|
||||
|
||||
mpv_render_context* mpv_context = nullptr;
|
||||
mpv_handle* mpv = mpv_create();
|
||||
mpv_set_option_string(mpv, "subs-fallback", "yes");
|
||||
mpv_set_option_string(mpv, "video-timing-offset", "0"); // 60fps
|
||||
mpv_set_option_string(mpv, "vd-lavc-dr", "yes");
|
||||
mpv_set_option_string(mpv, "terminal", "yes");
|
||||
mpv_set_option_string(mpv, "ytdl", "no");
|
||||
mpv_set_option_string(mpv, "hwdec", "auto");
|
||||
#ifdef _DEBUG
|
||||
mpv_set_option_string(this->mpv, "msg-level", "all=v");
|
||||
#endif
|
||||
if (mpv_initialize(mpv) < 0) die("init mpv failed");
|
||||
|
||||
static int osdShow = 0;
|
||||
// Create window with graphics context
|
||||
GLFWwindow* window = glfwCreateWindow(1280, 720, "xPlayer", nullptr, nullptr);
|
||||
GLFWwindow* window = glfwCreateWindow(1280, 720, argv[1], nullptr, nullptr);
|
||||
if (!window) {
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
}
|
||||
glfwSetWindowUserPointer(window, mpv);
|
||||
glfwSetKeyCallback(window, [](GLFWwindow* window, int key, int scancode, int action, int mods) {
|
||||
mpv_handle* mpv = static_cast<mpv_handle*>(glfwGetWindowUserPointer(window));
|
||||
if (action == GLFW_PRESS) {
|
||||
if (key == GLFW_KEY_ESCAPE)
|
||||
glfwSetWindowShouldClose(window, 1);
|
||||
else if (key == GLFW_KEY_ENTER)
|
||||
osdShow = !osdShow;
|
||||
mpv_command_string(mpv, "script-binding stats/display-stats-toggle");
|
||||
else if (key == GLFW_KEY_SPACE)
|
||||
mpv_command_string(mpv, "cycle pause");
|
||||
else if (key == GLFW_KEY_LEFT)
|
||||
mpv_command_string(mpv, "seek -15");
|
||||
else if (key == GLFW_KEY_RIGHT)
|
||||
mpv_command_string(mpv, "seek 15");
|
||||
else if (key == GLFW_KEY_UP)
|
||||
mpv_command_string(mpv, "playlist-prev");
|
||||
else if (key == GLFW_KEY_DOWN)
|
||||
mpv_command_string(mpv, "playlist-next");
|
||||
else
|
||||
printf("glfwKeyCallback key press %d\n", key);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -45,14 +80,45 @@ int main(int argc, char* argv[]) {
|
||||
glfwSwapInterval(1);
|
||||
glfwSetTime(0);
|
||||
|
||||
mpv_opengl_init_params gl_init_params{
|
||||
[](void* fn_ctx, const char* name) { return (void*)glfwGetProcAddress(name); }, nullptr};
|
||||
mpv_render_param params[] = {
|
||||
{MPV_RENDER_PARAM_API_TYPE, const_cast<char*>(MPV_RENDER_API_TYPE_OPENGL)},
|
||||
{MPV_RENDER_PARAM_OPENGL_INIT_PARAMS, &gl_init_params},
|
||||
{MPV_RENDER_PARAM_INVALID, nullptr},
|
||||
};
|
||||
if (mpv_render_context_create(&mpv_context, mpv, params) < 0) die("init mpv context failed");
|
||||
mpv_render_context_set_update_callback(
|
||||
mpv_context,
|
||||
[](void* obj) {
|
||||
mpv_render_context* ctx = static_cast<mpv_render_context*>(obj);
|
||||
uint64_t flags = mpv_render_context_update(ctx);
|
||||
if (flags & MPV_RENDER_UPDATE_FRAME) glfwPostEmptyEvent();
|
||||
},
|
||||
mpv_context);
|
||||
|
||||
const char* args[] = {"loadfile", argv[1], nullptr};
|
||||
if (mpv_command_async(mpv, 0, args) < 0) die("load file failed");
|
||||
|
||||
int fbw = 0, fbh = 0;
|
||||
// Main loop
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
glfwGetFramebufferSize(window, &fbw, &fbh);
|
||||
|
||||
mpv_opengl_fbo mpv_fbo{0, fbw, fbh};
|
||||
int flip_y{1};
|
||||
mpv_render_param mpv_params[3] = {
|
||||
{MPV_RENDER_PARAM_OPENGL_FBO, &mpv_fbo},
|
||||
{MPV_RENDER_PARAM_FLIP_Y, &flip_y},
|
||||
{MPV_RENDER_PARAM_INVALID, nullptr},
|
||||
};
|
||||
mpv_render_context_render(mpv_context, mpv_params);
|
||||
glfwSwapBuffers(window);
|
||||
glfwWaitEvents();
|
||||
}
|
||||
|
||||
mpv_render_context_free(mpv_context);
|
||||
mpv_terminate_destroy(mpv);
|
||||
|
||||
glfwDestroyWindow(window);
|
||||
glfwTerminate();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user