CVideoHandler.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. enum class EVideoType : ui8
  16. {
  17. INTRO = 0, // use entire window: stopOnKey = true, scale = true, overlay = false
  18. SPELLBOOK // overlay video: stopOnKey = false, scale = false, overlay = true
  19. };
  20. class IVideoPlayer : boost::noncopyable
  21. {
  22. public:
  23. virtual bool open(const VideoPath & name, bool scale = false)=0; //true - succes
  24. virtual void close()=0;
  25. virtual bool nextFrame()=0;
  26. virtual void show(int x, int y, SDL_Surface *dst, bool update = true)=0;
  27. virtual void redraw(int x, int y, SDL_Surface *dst, bool update = true)=0; //reblits buffer
  28. virtual bool wait()=0;
  29. virtual int curFrame() const =0;
  30. virtual int frameCount() const =0;
  31. };
  32. class IMainVideoPlayer : public IVideoPlayer
  33. {
  34. public:
  35. virtual ~IMainVideoPlayer() = default;
  36. virtual void update(int x, int y, SDL_Surface *dst, bool forceRedraw, bool update = true, std::function<void()> restart = nullptr){}
  37. virtual bool openAndPlayVideo(const VideoPath & name, int x, int y, EVideoType videoType)
  38. {
  39. return false;
  40. }
  41. virtual std::pair<std::unique_ptr<ui8 []>, si64> getAudio(const VideoPath & videoToOpen) { return std::make_pair(nullptr, 0); };
  42. virtual Point size() { return Point(0, 0); };
  43. };
  44. class CEmptyVideoPlayer final : public IMainVideoPlayer
  45. {
  46. public:
  47. int curFrame() const override {return -1;};
  48. int frameCount() const override {return -1;};
  49. void redraw( int x, int y, SDL_Surface *dst, bool update = true ) override {};
  50. void show( int x, int y, SDL_Surface *dst, bool update = true ) override {};
  51. bool nextFrame() override {return false;};
  52. void close() override {};
  53. bool wait() override {return false;};
  54. bool open(const VideoPath & name, bool scale = false) override {return false;};
  55. };
  56. #ifndef DISABLE_VIDEO
  57. struct AVFormatContext;
  58. struct AVCodecContext;
  59. struct AVCodec;
  60. struct AVFrame;
  61. struct AVIOContext;
  62. VCMI_LIB_NAMESPACE_BEGIN
  63. class CInputStream;
  64. VCMI_LIB_NAMESPACE_END
  65. class CVideoPlayer final : public IMainVideoPlayer
  66. {
  67. int stream; // stream index in video
  68. AVFormatContext *format;
  69. AVCodecContext *codecContext; // codec context for stream
  70. const AVCodec *codec;
  71. AVFrame *frame;
  72. struct SwsContext *sws;
  73. AVIOContext * context;
  74. VideoPath fname; //name of current video file (empty if idle)
  75. // Destination. Either overlay or dest.
  76. SDL_Texture *texture;
  77. SDL_Surface *dest;
  78. Rect destRect; // valid when dest is used
  79. Rect pos; // destination on screen
  80. /// video playback currnet progress, in seconds
  81. double frameTime;
  82. bool doLoop; // loop through video
  83. bool overlay;
  84. bool playVideo(int x, int y, bool stopOnKey);
  85. bool open(const VideoPath & fname, bool loop, bool useOverlay = false, bool scale = false);
  86. public:
  87. CVideoPlayer();
  88. ~CVideoPlayer();
  89. bool init();
  90. bool open(const VideoPath & fname, bool scale = false) override;
  91. void close() override;
  92. bool nextFrame() override; // display next frame
  93. void show(int x, int y, SDL_Surface *dst, bool update = true) override; //blit current frame
  94. void redraw(int x, int y, SDL_Surface *dst, bool update = true) override; //reblits buffer
  95. 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
  96. // Opens video, calls playVideo, closes video; returns playVideo result (if whole video has been played)
  97. bool openAndPlayVideo(const VideoPath & name, int x, int y, EVideoType videoType) override;
  98. std::pair<std::unique_ptr<ui8 []>, si64> getAudio(const VideoPath & videoToOpen) override;
  99. Point size() override;
  100. //TODO:
  101. bool wait() override {return false;};
  102. int curFrame() const override {return -1;};
  103. int frameCount() const override {return -1;};
  104. // public to allow access from ffmpeg IO functions
  105. std::unique_ptr<CInputStream> data;
  106. std::unique_ptr<CInputStream> dataAudio;
  107. };
  108. #endif