GLPlayer.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #include <unordered_map>
  9. namespace WallpaperEngine::VideoPlayback::MPV {
  10. class GLPlayer : public Helpers::ContextAware {
  11. friend struct MemoryStreamProtocol;
  12. public:
  13. GLPlayer (
  14. RenderContext& context, GLuint outputTexture, const std::filesystem::path& file, int64_t baseWidth,
  15. int64_t baseHeight, GLuint fbo = GL_NONE
  16. );
  17. GLPlayer (
  18. RenderContext& context, GLuint outputTexture, MemoryStreamProtocolUniquePtr stream, int64_t baseWidth,
  19. int64_t baseHeight, GLuint fbo = GL_NONE
  20. );
  21. ~GLPlayer () override;
  22. /**
  23. * Increments the usage count of the player
  24. *
  25. * Directly controls playback, only started when at least one thing is using it
  26. * Initializes mpv if needed and starts playback
  27. */
  28. void incrementUsageCount ();
  29. /**
  30. * Decrements the usage count of the player
  31. *
  32. * Directly controls playback, only stopped when nothing is using it
  33. * De-initializes mpv if needed
  34. */
  35. void decrementUsageCount ();
  36. void setUntimed ();
  37. void clearUntimed ();
  38. void setMuted ();
  39. void clearMuted ();
  40. void setVolume (double volume);
  41. void setSpeed (double speed);
  42. void setPaused ();
  43. void clearPaused ();
  44. void render () const;
  45. int getWidth () const;
  46. int getHeight () const;
  47. /** Current playback position in seconds, or 0 if playback hasn't started yet */
  48. double getPlaybackPosition () const;
  49. private:
  50. void prepareGL ();
  51. void init ();
  52. void play ();
  53. void setSource (MemoryStreamProtocolUniquePtr source);
  54. void setSource (const std::filesystem::path& file);
  55. void stop ();
  56. protected:
  57. bool m_doWeOwnFramebuffer;
  58. GLuint m_outputTexture;
  59. GLuint m_fbo = GL_NONE;
  60. mutable int64_t m_width;
  61. mutable int64_t m_height;
  62. mpv_handle* m_handle = nullptr;
  63. mpv_render_context* m_renderContext = nullptr;
  64. double m_volume = 0.0f;
  65. double m_speed = 1.0;
  66. bool m_muted = false;
  67. bool m_untimed = false;
  68. bool m_paused = false;
  69. std::optional<std::filesystem::path> m_file;
  70. std::optional<MemoryStreamProtocolUniquePtr> m_stream;
  71. uint32_t m_usageCount = 0;
  72. // tracks the currently playing instance for each video path, so a player starting
  73. // the same video another monitor is already showing can pick up around the same spot
  74. // instead of always restarting from zero
  75. static std::unordered_map<std::filesystem::path, GLPlayer*> s_activePlayers;
  76. };
  77. using GLPlayerUniquePtr = std::unique_ptr<GLPlayer>;
  78. using GLPlayerSharedPtr = std::shared_ptr<GLPlayer>;
  79. };