GLPlayer.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #pragma once
  2. #include "MemoryStreamProtocol.h"
  3. #include "WallpaperEngine/Render/RenderContext.h"
  4. #include <GL/glew.h>
  5. #include <mpv/client.h>
  6. #include <mpv/render.h>
  7. #include <string>
  8. namespace WallpaperEngine::VideoPlayback::MPV {
  9. class GLPlayer : public Helpers::ContextAware {
  10. friend struct MemoryStreamProtocol;
  11. public:
  12. GLPlayer (
  13. RenderContext& context, GLuint outputTexture, const std::filesystem::path& file, int64_t baseWidth,
  14. int64_t baseHeight, GLuint fbo = GL_NONE
  15. );
  16. GLPlayer (
  17. RenderContext& context, GLuint outputTexture, MemoryStreamProtocolUniquePtr stream, int64_t baseWidth,
  18. int64_t baseHeight, GLuint fbo = GL_NONE
  19. );
  20. ~GLPlayer () override;
  21. /**
  22. * Increments the usage count of the player
  23. *
  24. * Directly controls playback, only started when at least one thing is using it
  25. * Initializes mpv if needed and starts playback
  26. */
  27. void incrementUsageCount ();
  28. /**
  29. * Decrements the usage count of the player
  30. *
  31. * Directly controls playback, only stopped when nothing is using it
  32. * De-initializes mpv if needed
  33. */
  34. void decrementUsageCount ();
  35. void setUntimed ();
  36. void clearUntimed ();
  37. void setMuted ();
  38. void clearMuted ();
  39. void setVolume (double volume);
  40. void setPaused ();
  41. void clearPaused ();
  42. void render () const;
  43. int getWidth () const;
  44. int getHeight () const;
  45. private:
  46. void prepareGL ();
  47. void init ();
  48. void play ();
  49. void setSource (MemoryStreamProtocolUniquePtr source);
  50. void setSource (const std::filesystem::path& file);
  51. void stop ();
  52. protected:
  53. bool m_doWeOwnFramebuffer;
  54. GLuint m_outputTexture;
  55. GLuint m_fbo = GL_NONE;
  56. mutable int64_t m_width;
  57. mutable int64_t m_height;
  58. mpv_handle* m_handle = nullptr;
  59. mpv_render_context* m_renderContext = nullptr;
  60. double m_volume = 0.0f;
  61. bool m_muted = false;
  62. bool m_untimed = false;
  63. bool m_paused = false;
  64. std::optional<std::filesystem::path> m_file;
  65. std::optional<MemoryStreamProtocolUniquePtr> m_stream;
  66. uint32_t m_usageCount = 0;
  67. };
  68. using GLPlayerUniquePtr = std::unique_ptr<GLPlayer>;
  69. using GLPlayerSharedPtr = std::shared_ptr<GLPlayer>;
  70. };