GLPlayer.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #include "GLPlayer.h"
  2. #include "WallpaperEngine/Logging/Log.h"
  3. #include <mpv/render_gl.h>
  4. #include <mpv/stream_cb.h>
  5. using namespace WallpaperEngine::VideoPlayback::MPV;
  6. void* get_proc_address (void* ctx, const char* name) {
  7. return static_cast<GLPlayer*> (ctx)->getContext ().getDriver ().getProcAddress (name);
  8. }
  9. GLPlayer::GLPlayer (
  10. RenderContext& context, GLuint outputTexture, const std::filesystem::path& file, const int64_t baseWidth,
  11. const int64_t baseHeight, const GLuint fbo
  12. ) : ContextAware (context), m_outputTexture (outputTexture), m_width (baseWidth), m_height (baseHeight) {
  13. this->m_fbo = fbo;
  14. this->m_doWeOwnFramebuffer = this->m_fbo == GL_NONE;
  15. this->prepareGL ();
  16. this->setSource (file);
  17. }
  18. GLPlayer::GLPlayer (
  19. RenderContext& context, const GLuint outputTexture, MemoryStreamProtocolUniquePtr stream, const int64_t baseWidth,
  20. const int64_t baseHeight, const GLuint fbo
  21. ) : ContextAware (context), m_outputTexture (outputTexture), m_width (baseWidth), m_height (baseHeight) {
  22. this->m_fbo = fbo;
  23. this->m_doWeOwnFramebuffer = this->m_fbo == GL_NONE;
  24. this->prepareGL ();
  25. this->setSource (std::move (stream));
  26. }
  27. GLPlayer::~GLPlayer () {
  28. // clean up any kept resources
  29. this->stop ();
  30. // only clean up framebuffer if we own it
  31. if (this->m_doWeOwnFramebuffer) {
  32. // free gl resources too
  33. glDeleteFramebuffers (1, &this->m_fbo);
  34. }
  35. }
  36. void GLPlayer::incrementUsageCount () {
  37. this->m_usageCount++;
  38. if (this->m_usageCount == 1) {
  39. this->play ();
  40. }
  41. }
  42. void GLPlayer::decrementUsageCount () {
  43. if (this->m_usageCount == 0) {
  44. sLog.exception ("GLPlayer usage count would underflow");
  45. }
  46. this->m_usageCount--;
  47. if (this->m_usageCount == 0) {
  48. this->stop ();
  49. }
  50. }
  51. void GLPlayer::setUntimed () {
  52. if (this->m_handle) {
  53. sLog.exception ("Cannot set untimed mode after playback has started");
  54. }
  55. this->m_untimed = true;
  56. }
  57. void GLPlayer::clearUntimed () {
  58. if (this->m_handle) {
  59. sLog.exception ("Cannot set untimed mode after playback has started");
  60. }
  61. this->m_untimed = false;
  62. }
  63. void GLPlayer::setMuted () {
  64. this->m_muted = true;
  65. if (this->m_handle) {
  66. mpv_set_property_string (this->m_handle, "mute", "yes");
  67. }
  68. }
  69. void GLPlayer::clearMuted () {
  70. this->m_muted = false;
  71. if (this->m_handle) {
  72. mpv_set_property_string (this->m_handle, "mute", "no");
  73. }
  74. }
  75. void GLPlayer::setVolume (double volume) {
  76. this->m_volume = volume;
  77. if (this->m_handle) {
  78. mpv_set_property (this->m_handle, "volume", MPV_FORMAT_DOUBLE, &this->m_volume);
  79. }
  80. }
  81. void GLPlayer::setPaused () {
  82. this->m_paused = true;
  83. if (this->m_handle) {
  84. mpv_set_property_string (this->m_handle, "pause", "yes");
  85. }
  86. }
  87. void GLPlayer::clearPaused () {
  88. this->m_paused = false;
  89. if (this->m_handle) {
  90. mpv_set_property_string (this->m_handle, "pause", "no");
  91. }
  92. }
  93. void GLPlayer::render () const {
  94. // rendering should only happen if the texture is in use
  95. if (this->m_handle == nullptr) {
  96. return;
  97. }
  98. // read all the events available
  99. while (true) {
  100. const mpv_event* event = mpv_wait_event (this->m_handle, 0);
  101. if (event == nullptr || event->event_id == MPV_EVENT_NONE) {
  102. break;
  103. }
  104. if (event->event_id != MPV_EVENT_VIDEO_RECONFIG) {
  105. continue;
  106. }
  107. int64_t width, height;
  108. if (mpv_get_property (this->m_handle, "dwidth", MPV_FORMAT_INT64, &width) < 0) {
  109. continue;
  110. }
  111. if (mpv_get_property (this->m_handle, "dheight", MPV_FORMAT_INT64, &height) < 0) {
  112. continue;
  113. }
  114. if (width < 0 || height < 0) {
  115. continue;
  116. }
  117. this->m_width = width;
  118. this->m_height = height;
  119. // reconfigure the texture
  120. glBindTexture (GL_TEXTURE_2D, this->m_outputTexture);
  121. glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA8, this->m_width, this->m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  122. }
  123. // render the next
  124. glViewport (0, 0, this->m_width, this->m_height);
  125. mpv_opengl_fbo fbo { static_cast<int> (this->m_fbo), static_cast<int> (this->m_width),
  126. static_cast<int> (this->m_height), GL_RGBA8 };
  127. // no need to flip as it'll be handled by the wallpaper rendering code
  128. int flip_y = 0;
  129. // The host loop is paced by the compositor's frame callbacks (vsync). Do not
  130. // let libmpv block until the frame's target time: with several outputs each
  131. // owning a player on one thread, that serialises every screen onto the
  132. // video's frame grid and judders 24fps content on 60Hz outputs.
  133. int block_for_target_time = 0;
  134. mpv_render_param params[] = { { MPV_RENDER_PARAM_OPENGL_FBO, &fbo },
  135. { MPV_RENDER_PARAM_FLIP_Y, &flip_y },
  136. { MPV_RENDER_PARAM_BLOCK_FOR_TARGET_TIME, &block_for_target_time },
  137. { MPV_RENDER_PARAM_INVALID, nullptr } };
  138. mpv_render_context_render (this->m_renderContext, params);
  139. }
  140. int GLPlayer::getWidth () const { return this->m_width; }
  141. int GLPlayer::getHeight () const { return this->m_height; }
  142. void GLPlayer::prepareGL () {
  143. if (!this->m_doWeOwnFramebuffer || this->m_fbo != GL_NONE) {
  144. return;
  145. }
  146. glGenFramebuffers (1, &this->m_fbo);
  147. glBindFramebuffer (GL_FRAMEBUFFER, this->m_fbo);
  148. glBindTexture (GL_TEXTURE_2D, this->m_outputTexture);
  149. // reset texture's contents
  150. glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA8, this->m_width, this->m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  151. constexpr GLenum drawBuffers[1] = { GL_COLOR_ATTACHMENT0 };
  152. glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->m_outputTexture, 0);
  153. glDrawBuffers (1, drawBuffers);
  154. // ensure first framebuffer is okay
  155. if (glCheckFramebufferStatus (GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
  156. sLog.exception ("Framebuffers are not properly set");
  157. }
  158. // clear the framebuffer
  159. glClear (GL_COLOR_BUFFER_BIT);
  160. }
  161. void GLPlayer::init () {
  162. this->m_handle = mpv_create ();
  163. if (this->m_handle == nullptr) {
  164. sLog.exception ("Cannot create mpv context for video texture");
  165. }
  166. // setup mpv options for playback
  167. mpv_set_option_string (this->m_handle, "terminal", "yes");
  168. #if NDEBUG
  169. mpv_set_option_string (this->m_handle, "msg-level", "all=status,statusline=no");
  170. #else
  171. mpv_set_option_string (this->m_handle, "msg-level", "all=v");
  172. #endif
  173. mpv_set_option_string (this->m_handle, "input-cursor", "no");
  174. mpv_set_option_string (this->m_handle, "cursor-autohide", "no");
  175. mpv_set_option_string (this->m_handle, "config", "no");
  176. mpv_set_option_string (this->m_handle, "fbo-format", "rgba8");
  177. mpv_set_option_string (this->m_handle, "vo", "libmpv");
  178. mpv_set_option_string (this->m_handle, "profile", "fast");
  179. mpv_set_option_string (this->m_handle, "untimed", this->m_untimed ? "yes" : "no");
  180. if (mpv_initialize (this->m_handle) < 0) {
  181. sLog.exception ("Could not initialize mpv context");
  182. }
  183. // ensure video is muted and plays in a loop
  184. mpv_set_property_string (this->m_handle, "hwdec", "auto");
  185. mpv_set_property_string (this->m_handle, "loop", "inf");
  186. mpv_set_property (this->m_handle, "volume", MPV_FORMAT_DOUBLE, &this->m_volume);
  187. // initialize gl context for mpv
  188. mpv_opengl_init_params gl_init_params { get_proc_address, this };
  189. mpv_render_param params[] { { MPV_RENDER_PARAM_API_TYPE, const_cast<char*> (MPV_RENDER_API_TYPE_OPENGL) },
  190. { MPV_RENDER_PARAM_OPENGL_INIT_PARAMS, &gl_init_params },
  191. { MPV_RENDER_PARAM_INVALID, nullptr } };
  192. if (mpv_render_context_create (&this->m_renderContext, this->m_handle, params) < 0) {
  193. sLog.exception ("Failed to initialize MPV's GL context");
  194. }
  195. // mute the video if required
  196. mpv_set_property_string (this->m_handle, "mute", this->m_muted ? "yes" : "no");
  197. // ensure play/pause status is respected too
  198. mpv_set_property_string (this->m_handle, "pause", this->m_paused ? "yes" : "no");
  199. }
  200. void GLPlayer::setSource (const std::filesystem::path& file) { this->m_file = file; }
  201. void GLPlayer::setSource (MemoryStreamProtocolUniquePtr source) { this->m_stream = std::move (source); }
  202. void GLPlayer::play () {
  203. if (this->m_handle != nullptr) {
  204. sLog.exception ("Cannot play the same GLPlayer twice");
  205. }
  206. if (!this->m_file.has_value () && !this->m_stream.has_value ()) {
  207. sLog.exception ("Cannot play a GLPlayer without a source");
  208. }
  209. this->init ();
  210. if (this->m_file.has_value ()) {
  211. // build the path to the video file
  212. const char* command[] = { "loadfile", this->m_file.value ().c_str (), nullptr };
  213. if (mpv_command (this->m_handle, command) < 0) {
  214. sLog.exception ("Cannot load video to play");
  215. }
  216. } else if (this->m_stream) {
  217. this->m_stream.value ()->registerReadCallback (this->m_handle);
  218. // start playing the video
  219. const char* command[] = { "loadfile", "buffer://", nullptr };
  220. if (mpv_command (this->m_handle, command) < 0) {
  221. sLog.exception ("Cannot load video texture to play");
  222. }
  223. }
  224. }
  225. void GLPlayer::stop () {
  226. // clean up mpv and get it ready to start again at some point
  227. if (this->m_renderContext) {
  228. mpv_render_context_free (this->m_renderContext);
  229. this->m_renderContext = nullptr;
  230. }
  231. if (this->m_handle) {
  232. mpv_terminate_destroy (this->m_handle);
  233. this->m_handle = nullptr;
  234. }
  235. }