CVideoHandler.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * CVideoHandler.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../lib/Rect.h"
  12. #include "../lib/filesystem/ResourcePath.h"
  13. struct SDL_Surface;
  14. struct SDL_Texture;
  15. class IVideoPlayer : boost::noncopyable
  16. {
  17. public:
  18. virtual bool open(const VideoPath & name, bool scale = false)=0; //true - succes
  19. virtual void close()=0;
  20. virtual bool nextFrame()=0;
  21. virtual void show(int x, int y, SDL_Surface *dst, bool update = true)=0;
  22. virtual void redraw(int x, int y, SDL_Surface *dst, bool update = true)=0; //reblits buffer
  23. virtual bool wait()=0;
  24. virtual int curFrame() const =0;
  25. virtual int frameCount() const =0;
  26. };
  27. class IMainVideoPlayer : public IVideoPlayer
  28. {
  29. public:
  30. virtual ~IMainVideoPlayer() = default;
  31. virtual void update(int x, int y, SDL_Surface *dst, bool forceRedraw, bool update = true, std::function<void()> restart = nullptr){}
  32. virtual bool openAndPlayVideo(const VideoPath & name, int x, int y, bool stopOnKey = false, bool scale = false, bool overlay = true)
  33. {
  34. return false;
  35. }
  36. virtual std::pair<std::unique_ptr<ui8 []>, si64> getAudio(const VideoPath & videoToOpen) { return std::make_pair(nullptr, 0); };
  37. virtual Point size() { return Point(0, 0); };
  38. };
  39. class CEmptyVideoPlayer final : public IMainVideoPlayer
  40. {
  41. public:
  42. int curFrame() const override {return -1;};
  43. int frameCount() const override {return -1;};
  44. void redraw( int x, int y, SDL_Surface *dst, bool update = true ) override {};
  45. void show( int x, int y, SDL_Surface *dst, bool update = true ) override {};
  46. bool nextFrame() override {return false;};
  47. void close() override {};
  48. bool wait() override {return false;};
  49. bool open(const VideoPath & name, bool scale = false) override {return false;};
  50. };
  51. #ifndef DISABLE_VIDEO
  52. struct AVFormatContext;
  53. struct AVCodecContext;
  54. struct AVCodec;
  55. struct AVFrame;
  56. struct AVIOContext;
  57. VCMI_LIB_NAMESPACE_BEGIN
  58. class CInputStream;
  59. VCMI_LIB_NAMESPACE_END
  60. class CVideoPlayer final : public IMainVideoPlayer
  61. {
  62. int stream; // stream index in video
  63. AVFormatContext *format;
  64. AVCodecContext *codecContext; // codec context for stream
  65. const AVCodec *codec;
  66. AVFrame *frame;
  67. struct SwsContext *sws;
  68. AVIOContext * context;
  69. VideoPath fname; //name of current video file (empty if idle)
  70. // Destination. Either overlay or dest.
  71. SDL_Texture *texture;
  72. SDL_Surface *dest;
  73. Rect destRect; // valid when dest is used
  74. Rect pos; // destination on screen
  75. /// video playback currnet progress, in seconds
  76. double frameTime;
  77. bool doLoop; // loop through video
  78. bool overlayVideo;
  79. bool playVideo(int x, int y, bool stopOnKey);
  80. bool open(const VideoPath & fname, bool loop, bool useOverlay = false, bool scale = false);
  81. public:
  82. CVideoPlayer();
  83. ~CVideoPlayer();
  84. bool init();
  85. bool open(const VideoPath & fname, bool scale = false) override;
  86. void close() override;
  87. bool nextFrame() override; // display next frame
  88. void show(int x, int y, SDL_Surface *dst, bool update = true) override; //blit current frame
  89. void redraw(int x, int y, SDL_Surface *dst, bool update = true) override; //reblits buffer
  90. void update(int x, int y, SDL_Surface *dst, bool forceRedraw, bool update = true, std::function<void()> onVideoRestart = nullptr) override; //moves to next frame if appropriate, and blits it or blits only if redraw parameter is set true
  91. // Opens video, calls playVideo, closes video; returns playVideo result (if whole video has been played)
  92. bool openAndPlayVideo(const VideoPath & name, int x, int y, bool stopOnKey = false, bool scale = false, bool overlay = true) override;
  93. std::pair<std::unique_ptr<ui8 []>, si64> getAudio(const VideoPath & videoToOpen) override;
  94. Point size() override;
  95. //TODO:
  96. bool wait() override {return false;};
  97. int curFrame() const override {return -1;};
  98. int frameCount() const override {return -1;};
  99. // public to allow access from ffmpeg IO functions
  100. std::unique_ptr<CInputStream> data;
  101. std::unique_ptr<CInputStream> dataAudio;
  102. };
  103. #endif