CVideoHandler.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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)
  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. };
  38. class CEmptyVideoPlayer final : public IMainVideoPlayer
  39. {
  40. public:
  41. int curFrame() const override {return -1;};
  42. int frameCount() const override {return -1;};
  43. void redraw( int x, int y, SDL_Surface *dst, bool update = true ) override {};
  44. void show( int x, int y, SDL_Surface *dst, bool update = true ) override {};
  45. bool nextFrame() override {return false;};
  46. void close() override {};
  47. bool wait() override {return false;};
  48. bool open(const VideoPath & name, bool scale = false) override {return false;};
  49. };
  50. #ifndef DISABLE_VIDEO
  51. struct AVFormatContext;
  52. struct AVCodecContext;
  53. struct AVCodec;
  54. struct AVFrame;
  55. struct AVIOContext;
  56. VCMI_LIB_NAMESPACE_BEGIN
  57. class CInputStream;
  58. VCMI_LIB_NAMESPACE_END
  59. class CVideoPlayer final : public IMainVideoPlayer
  60. {
  61. int stream; // stream index in video
  62. AVFormatContext *format;
  63. AVCodecContext *codecContext; // codec context for stream
  64. const AVCodec *codec;
  65. AVFrame *frame;
  66. struct SwsContext *sws;
  67. AVIOContext * context;
  68. VideoPath fname; //name of current video file (empty if idle)
  69. // Destination. Either overlay or dest.
  70. SDL_Texture *texture;
  71. SDL_Surface *dest;
  72. Rect destRect; // valid when dest is used
  73. Rect pos; // destination on screen
  74. /// video playback currnet progress, in seconds
  75. double frameTime;
  76. bool doLoop; // loop through video
  77. bool playVideo(int x, int y, bool stopOnKey);
  78. bool open(const VideoPath & fname, bool loop, bool useOverlay = false, bool scale = false);
  79. public:
  80. CVideoPlayer();
  81. ~CVideoPlayer();
  82. bool init();
  83. bool open(const VideoPath & fname, bool scale = false) override;
  84. void close() override;
  85. bool nextFrame() override; // display next frame
  86. void show(int x, int y, SDL_Surface *dst, bool update = true) override; //blit current frame
  87. void redraw(int x, int y, SDL_Surface *dst, bool update = true) override; //reblits buffer
  88. 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
  89. // Opens video, calls playVideo, closes video; returns playVideo result (if whole video has been played)
  90. bool openAndPlayVideo(const VideoPath & name, int x, int y, bool stopOnKey = false, bool scale = false) override;
  91. std::pair<std::unique_ptr<ui8 []>, si64> getAudio(const VideoPath & videoToOpen) override;
  92. //TODO:
  93. bool wait() override {return false;};
  94. int curFrame() const override {return -1;};
  95. int frameCount() const override {return -1;};
  96. // public to allow access from ffmpeg IO functions
  97. std::unique_ptr<CInputStream> data;
  98. std::unique_ptr<CInputStream> dataAudio;
  99. };
  100. #endif