From bdd95325ce3b8b3f95316b206b3c4c210ef81c80 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 2 Oct 2022 07:40:14 +0200 Subject: Add mpv option --- .gitignore | 1 + README.md | 18 +++- TODO | 7 +- build.sh | 5 +- include/mpv.hpp | 31 ++++++ project.conf | 3 +- src/main.cpp | 187 ++++++++++++++++++++++++++++++------ src/mpv.cpp | 266 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/window_texture.c | 2 +- tests/main.cpp | 7 -- 10 files changed, 483 insertions(+), 44 deletions(-) create mode 100644 include/mpv.hpp create mode 100644 src/mpv.cpp delete mode 100644 tests/main.cpp diff --git a/.gitignore b/.gitignore index 1f413f9..439cf44 100644 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,6 @@ tests/sibs-build/ tests/compile_commands.json vr-video-player window_texture.o +mpv.o main.o .clangd/ diff --git a/README.md b/README.md index 13e008c..577e856 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,22 @@ Might now work when using a compositor such as picom when using the glx backend. # Building Run `./build.sh` or if you are running Arch Linux, then you can find it on aur under the name vr-video-player-git (`yay -S vr-video-player-git`).\ -Dependencies needed when building using `build.sh`: `glm, glew, sdl2, openvr, libx11, libxcomposite, libfixes`. +Dependencies needed when building using `build.sh`: `glm, glew, sdl2, openvr, libx11, libxcomposite, libxfixes, libmpv`. # How to use +vr-video-player has two options. Either capture a window and view it in vr or an work-in-progress built-in mpv option (slower at the moment). +# Using the built-in video player +To play a video with the built-in mpv player, run vr-video-player like so: +``` +./vr-video-player --sphere --video +``` +for example: +``` +./vr-video-player --sphere --video /home/adam/Videos/my-cool-vr-video.mp4 +``` +`--sphere` can be replaced with `--flat`, `plane` or `--sphere360` for different display modes. + +# Capturing a window Install xdotool and launch a video in a video player (I recommend mpv, because browsers, smplayer and vlc player remove the vr for 360 videos) and resize it to fit your monitor or larger for best quality and then, if you want to watch 180 degree stereoscopic videos then run: @@ -49,6 +62,7 @@ Alternatively, you can run: ``` and vr-video-player will automatically select the focused window (and update when the focused window changes). +# Input options The video might not be in front of you, so to move the video in front of you, you can do any of the following: * Pull the trigger on the vr controller * Press "Alt + F1" @@ -58,6 +72,8 @@ The video might not be in front of you, so to move the video in front of you, yo You can zoom the view with alt + Q/E. +When using the built-in video player and the vr window is focused you can then use left/right arrow keys to move back/forward in the video and space to pause. In the future vr-video-player will show a graphical interface inside vr to manipulate the video. + You can launch vr-video-player without any arguments to show a list of all arguments. Note: If the cursor position is weird and does not match what you are seeing in stereoscopic vr mode, then try running the vr video player with the --cursor-wrap option: diff --git a/TODO b/TODO index e60dcdc..44a8ed1 100644 --- a/TODO +++ b/TODO @@ -1 +1,6 @@ -Use libmpv. Make stereo audio follow headset rotation in 180/360 mode (assume facing forward is the default audio mode). +Use pointer/button motion event instead of XQueryPointer every frame. +Use directional audio when using mpv. +Optimize mpv rendering option. Causes stuttering for some reason while capturing a mpv window does not. +Dynamically load mpv (with dlopen) when using the --video option instead of linking to it at compile time. +Show mpv gui. +Allow setting/changing video at runtime. diff --git a/build.sh b/build.sh index 7bf6226..1f2d44b 100755 --- a/build.sh +++ b/build.sh @@ -1,8 +1,9 @@ #!/bin/sh -e -dependencies="glm glew sdl2 openvr x11 xcomposite xfixes" +dependencies="glm glew sdl2 openvr x11 xcomposite xfixes mpv" includes=$(pkg-config --cflags $dependencies) libs=$(pkg-config --libs $dependencies) gcc -c src/window_texture.c -O2 $includes +g++ -c src/mpv.cpp -O2 $includes g++ -c src/main.cpp -O2 $includes -g++ -o vr-video-player -O2 window_texture.o main.o -s $libs +g++ -o vr-video-player -O2 window_texture.o mpv.o main.o -s $libs diff --git a/include/mpv.hpp b/include/mpv.hpp new file mode 100644 index 0000000..6f3b559 --- /dev/null +++ b/include/mpv.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include +#include + +typedef struct mpv_handle mpv_handle; +typedef struct mpv_render_context mpv_render_context; + +class Mpv { +public: + Mpv() = default; + ~Mpv(); + + bool create(); + bool destroy(); + + bool load_file(const char *path); + // |width| and |ħeight| are set to 0 unless there is an event to reconfigure video size + void on_event(SDL_Event &event, bool *render_update, int64_t *width, int64_t *height, bool *quit); + void seek(double seconds); + void toggle_pause(); + void draw(unsigned int framebuffer_id, int width, int height); + + bool created = false; + uint32_t wakeup_on_mpv_render_update = -1; + uint32_t wakeup_on_mpv_events = -1; + + mpv_handle *mpv = nullptr; + mpv_render_context *mpv_gl = nullptr; + bool paused = false; +}; \ No newline at end of file diff --git a/project.conf b/project.conf index 154a611..701b959 100644 --- a/project.conf +++ b/project.conf @@ -11,4 +11,5 @@ sdl2 = "2" openvr = "1" x11 = "1" xcomposite = ">=0.2" -xfixes = ">=5" \ No newline at end of file +xfixes = ">=5" +mpv = ">=1" \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 0278da1..268a86d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -33,6 +33,7 @@ #include #include "../include/window_texture.h" +#include "../include/mpv.hpp" #include #include @@ -224,6 +225,8 @@ private: // OpenGL bookkeeping FramebufferDesc leftEyeDesc; FramebufferDesc rightEyeDesc; + FramebufferDesc mpvDesc; + bool CreateFrameBuffer( int nWidth, int nHeight, FramebufferDesc &framebufferDesc ); uint32_t m_nRenderWidth; @@ -244,11 +247,17 @@ private: // X compositor bool follow_focused = false; bool focused_window_changed = true; bool focused_window_set = false; - - int mouse_x; - int mouse_y; - int window_width; - int window_height; + const char *mpv_file = nullptr; + Mpv mpv; + bool mpv_render_update = false; + int64_t mpv_video_width = 0; + int64_t mpv_video_height = 0; + bool mpv_video_loaded = false; + + int mouse_x = 0; + int mouse_y = 0; + int window_width = 1; + int window_height = 1; Uint32 window_resize_time; bool window_resized = false; @@ -258,8 +267,8 @@ private: // X compositor int x_fixes_error_base; int prev_visibility_state = VisibilityFullyObscured; - GLint pixmap_texture_width = 0; - GLint pixmap_texture_height = 0; + GLint pixmap_texture_width = 1; + GLint pixmap_texture_height = 1; enum class ViewMode { LEFT_RIGHT, @@ -383,25 +392,26 @@ void dprintf( const char *fmt, ... ) } static void usage() { - fprintf(stderr, "usage: vr-video-player [--sphere|--sphere360|--flat|--plane] [--left-right|--right-left] [--stretch|--no-stretch] [--zoom zoom-level] [--cursor-scale scale] [--cursor-wrap|--no-cursor-wrap] [--follow-focused] \n"); + fprintf(stderr, "usage: vr-video-player [--sphere|--sphere360|--flat|--plane] [--left-right|--right-left] [--stretch|--no-stretch] [--zoom zoom-level] [--cursor-scale scale] [--cursor-wrap|--no-cursor-wrap] [--follow-focused] [--video video] \n"); fprintf(stderr, "\n"); fprintf(stderr, "OPTIONS\n"); - fprintf(stderr, " --sphere View the window as a stereoscopic 180 degrees screen (half sphere). The view will be attached to your head in vr. This is recommended for 180 degrees videos. This is the default value\n"); - fprintf(stderr, " --sphere360 View the window as an equirectangular cube map. This is what is mostly used on youtube, where the video is split into top and bottom as a cubemap. The view will be attached to your head in vr\n"); - fprintf(stderr, " --flat View the window as a stereoscopic flat screen. This is recommended for stereoscopic videos and games\n"); - fprintf(stderr, " --left-right This option is used together with --flat, to specify if the left side of the window is meant to be viewed with the left eye and the right side is meant to be viewed by the right eye. This is the default value\n"); - fprintf(stderr, " --right-left This option is used together with --flat, to specify if the left side of the window is meant to be viewed with the right eye and the right side is meant to be viewed by the left eye\n"); - fprintf(stderr, " --plane View the window as a slightly curved screen. This is recommended for non-stereoscopic content\n"); - fprintf(stderr, " --stretch This option is used together with --flat, To specify if the size of both sides of the window should be combined and stretch to that size when viewed in vr. This is the default value\n"); - fprintf(stderr, " --no-stretch This option is used together with --flat, To specify if the size of one side of the window should be the size of the whole window when viewed in vr. This is the option you want if the window looks too wide\n"); - fprintf(stderr, " --zoom Change the distance to the window. This should be a positive value. In flat and plane modes, this is the distance to the window when the window is reset (with W key or controller trigger button). The default value is 0 for all modes except sphere mode, where the default value is 1. This value is unused for sphere360 mode\n"); - fprintf(stderr, " --cursor-scale Change the size of the cursor. This should be a positive value. If set to 0, then the cursor is hidden. The default value is 1 for all modes except sphere mode, where the default value is 0. The cursor is always hidden in sphere360 mode\n"); - fprintf(stderr, " --cursor-wrap If this option is set, then the cursor position in the vr view will wrap around when it reached the center of the window (i.e when it reaches the edge of one side of the stereoscopic view). This option is only valid for stereoscopic view (flat and sphere modes)\n"); - fprintf(stderr, " --no-cursor-wrap If this option is set, then the cursor position in the vr view will match the the real cursor position inside the window\n"); - fprintf(stderr, " --follow-focused If this option is set, then the selected window will be the focused window. vr-video-player will automatically update when the focused window changes. Either this option or window_id should be used\n"); - fprintf(stderr, " --free-camera If this option is set, then the camera wont follow your position. This option is only applicable when not using --sphere or --sphere360\n"); - fprintf(stderr, " --reduce-flicker A hack to reduce flickering in low resolution text when the headset is not moving by moving the window around quickly by a few pixels\n"); - fprintf(stderr, " window_id The X11 window id of the window to view in vr. This option or --follow-focused is required\n"); + fprintf(stderr, " --sphere View the window as a stereoscopic 180 degrees screen (half sphere). The view will be attached to your head in vr. This is recommended for 180 degrees videos. This is the default value\n"); + fprintf(stderr, " --sphere360 View the window as an equirectangular cube map. This is what is mostly used on youtube, where the video is split into top and bottom as a cubemap. The view will be attached to your head in vr\n"); + fprintf(stderr, " --flat View the window as a stereoscopic flat screen. This is recommended for stereoscopic videos and games\n"); + fprintf(stderr, " --left-right This option is used together with --flat, to specify if the left side of the window is meant to be viewed with the left eye and the right side is meant to be viewed by the right eye. This is the default value\n"); + fprintf(stderr, " --right-left This option is used together with --flat, to specify if the left side of the window is meant to be viewed with the right eye and the right side is meant to be viewed by the left eye\n"); + fprintf(stderr, " --plane View the window as a slightly curved screen. This is recommended for non-stereoscopic content\n"); + fprintf(stderr, " --stretch This option is used together with --flat, To specify if the size of both sides of the window should be combined and stretch to that size when viewed in vr. This is the default value\n"); + fprintf(stderr, " --no-stretch This option is used together with --flat, To specify if the size of one side of the window should be the size of the whole window when viewed in vr. This is the option you want if the window looks too wide\n"); + fprintf(stderr, " --zoom Change the distance to the window. This should be a positive value. In flat and plane modes, this is the distance to the window when the window is reset (with W key or controller trigger button). The default value is 0 for all modes except sphere mode, where the default value is 1. This value is unused for sphere360 mode\n"); + fprintf(stderr, " --cursor-scale Change the size of the cursor. This should be a positive value. If set to 0, then the cursor is hidden. The default value is 1 for all modes except sphere mode, where the default value is 0. The cursor is always hidden in sphere360 mode\n"); + fprintf(stderr, " --cursor-wrap If this option is set, then the cursor position in the vr view will wrap around when it reached the center of the window (i.e when it reaches the edge of one side of the stereoscopic view). This option is only valid for stereoscopic view (flat and sphere modes)\n"); + fprintf(stderr, " --no-cursor-wrap If this option is set, then the cursor position in the vr view will match the the real cursor position inside the window\n"); + fprintf(stderr, " --reduce-flicker A hack to reduce flickering in low resolution text when the headset is not moving by moving the window around quickly by a few pixels\n"); + fprintf(stderr, " --free-camera If this option is set, then the camera wont follow your position\n"); + fprintf(stderr, " --follow-focused If this option is set, then the selected window will be the focused window. vr-video-player will automatically update when the focused window changes. Either this option, --video or window_id should be used\n"); + fprintf(stderr, " --video