CVideoHandler.h 3.8 KB

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