CVideo.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "CVideo.h"
  2. #include "common.h"
  3. #include <GL/glew.h>
  4. using namespace WallpaperEngine;
  5. using namespace WallpaperEngine::Render;
  6. void* get_proc_address (void* ctx, const char* name) {
  7. return static_cast<CVideo*> (ctx)->getContext ().getDriver ().getProcAddress (name);
  8. }
  9. CVideo::CVideo (Core::CVideo* video, CRenderContext& context, CAudioContext& audioContext,
  10. const CWallpaperState::TextureUVsScaling& scalingMode) :
  11. CWallpaper (video, Type, context, audioContext, scalingMode),
  12. m_width (16),
  13. m_height (16),
  14. m_mpvGl (nullptr) {
  15. double volume = this->getContext ().getApp ().getContext ().settings.audio.volume * 100.0 / 128.0;
  16. // create mpv contexts
  17. this->m_mpv = mpv_create ();
  18. if (this->m_mpv == nullptr)
  19. sLog.exception ("Could not create mpv context");
  20. mpv_set_option_string (this->m_mpv, "terminal", "yes");
  21. mpv_set_option_string (this->m_mpv, "msg-level", "all=v");
  22. mpv_set_option_string (this->m_mpv, "input-cursor", "no");
  23. mpv_set_option_string (this->m_mpv, "cursor-autohide", "no");
  24. mpv_set_option_string (this->m_mpv, "config", "no");
  25. mpv_set_option_string (this->m_mpv, "fbo-format", "rgba8");
  26. mpv_set_option_string (this->m_mpv, "vo", "libmpv");
  27. if (mpv_initialize (this->m_mpv) < 0)
  28. sLog.exception ("Could not initialize mpv context");
  29. mpv_set_option_string (this->m_mpv, "hwdec", "auto");
  30. mpv_set_option_string (this->m_mpv, "loop", "inf");
  31. mpv_set_option (this->m_mpv, "volume", MPV_FORMAT_DOUBLE, &volume);
  32. // initialize gl context for mpv
  33. mpv_opengl_init_params gl_init_params {get_proc_address, this};
  34. mpv_render_param params [] {{MPV_RENDER_PARAM_API_TYPE, const_cast<char*> (MPV_RENDER_API_TYPE_OPENGL)},
  35. {MPV_RENDER_PARAM_OPENGL_INIT_PARAMS, &gl_init_params},
  36. {MPV_RENDER_PARAM_INVALID, nullptr}};
  37. if (mpv_render_context_create (&this->m_mpvGl, this->m_mpv, params) < 0)
  38. sLog.exception ("Failed to initialize MPV's GL context");
  39. const std::filesystem::path videopath =
  40. this->getVideo ()->getProject ().getContainer ()->resolveRealFile (this->getVideo ()->getFilename ());
  41. // build the path to the video file
  42. const char* command [] = {"loadfile", videopath.c_str (), nullptr};
  43. if (mpv_command (this->m_mpv, command) < 0)
  44. sLog.exception ("Cannot load video to play");
  45. if (!this->getContext ().getApp ().getContext ().settings.audio.enabled) {
  46. const char* mutecommand [] = {"set", "mute", "yes", nullptr};
  47. mpv_command (this->m_mpv, mutecommand);
  48. }
  49. // setup framebuffers
  50. this->setupFramebuffers ();
  51. }
  52. void CVideo::setSize (int64_t width, int64_t height) {
  53. this->m_width = width > 0 ? width : this->m_width;
  54. this->m_height = height > 0 ? height : this->m_height;
  55. // do not refresh the texture if any of the sizes are invalid
  56. if (this->m_width <= 0 || this->m_height <= 0)
  57. return;
  58. // reconfigure the texture
  59. glBindTexture (GL_TEXTURE_2D, this->getWallpaperTexture ());
  60. glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA8, this->m_width, this->m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  61. }
  62. void CVideo::renderFrame (glm::ivec4 viewport) {
  63. // read any and all the events available
  64. while (this->m_mpv) {
  65. const mpv_event* event = mpv_wait_event (this->m_mpv, 0);
  66. if (event == nullptr || event->event_id == MPV_EVENT_NONE)
  67. break;
  68. // we do not care about any of the events
  69. if (event->event_id == MPV_EVENT_VIDEO_RECONFIG) {
  70. int64_t width, height;
  71. if (mpv_get_property (this->m_mpv, "dwidth", MPV_FORMAT_INT64, &width) >= 0 &&
  72. mpv_get_property (this->m_mpv, "dheight", MPV_FORMAT_INT64, &height) >= 0)
  73. this->setSize (width, height);
  74. }
  75. }
  76. // render the next
  77. glViewport (0, 0, this->getWidth (), this->getHeight ());
  78. mpv_opengl_fbo fbo {static_cast<int> (this->getWallpaperFramebuffer ()), static_cast<int> (this->m_width),
  79. static_cast<int> (this->m_height), GL_RGBA8};
  80. // no need to flip as it'll be handled by the wallpaper rendering code
  81. int flip_y = 0;
  82. mpv_render_param params [] = {
  83. {MPV_RENDER_PARAM_OPENGL_FBO, &fbo}, {MPV_RENDER_PARAM_FLIP_Y, &flip_y}, {MPV_RENDER_PARAM_INVALID, nullptr}};
  84. mpv_render_context_render (this->m_mpvGl, params);
  85. }
  86. Core::CVideo* CVideo::getVideo () {
  87. return this->getWallpaperData ()->as<Core::CVideo> ();
  88. }
  89. uint32_t CVideo::getWidth () const {
  90. return this->m_width;
  91. }
  92. uint32_t CVideo::getHeight () const {
  93. return this->m_height;
  94. }
  95. const std::string CVideo::Type = "video";