seekable_buffer.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #pragma once
  2. #include "defs.h"
  3. namespace ffmpeg {
  4. /**
  5. * Class uses internal buffer to store initial size bytes as a seekable cache
  6. * from Media provider and let ffmpeg to seek and read bytes from cache
  7. * and beyond - reading bytes directly from Media provider
  8. */
  9. enum class ImageType {
  10. UNKNOWN = 0,
  11. JPEG = 1,
  12. PNG = 2,
  13. TIFF = 3,
  14. };
  15. class SeekableBuffer {
  16. public:
  17. // @type is optional, not nullptr only is image detection required
  18. // \returns 1 is buffer seekable, 0 - if not seekable, < 0 on error
  19. int init(
  20. DecoderInCallback&& in,
  21. uint64_t timeoutMs,
  22. size_t maxSeekableBytes,
  23. ImageType* type);
  24. int read(uint8_t* buf, int size, uint64_t timeoutMs);
  25. int64_t seek(int64_t offset, int whence, uint64_t timeoutMs);
  26. void shutdown();
  27. private:
  28. bool readBytes(DecoderInCallback& in, size_t maxBytes, uint64_t timeoutMs);
  29. void setImageType(ImageType* type);
  30. private:
  31. DecoderInCallback inCallback_;
  32. std::vector<uint8_t> buffer_; // resized at init time
  33. long pos_{0}; // current position (SEEK_CUR iff pos_ < end_)
  34. long end_{0}; // current buffer size
  35. bool eof_{0}; // indicates the EOF
  36. bool isSeekable_{false}; // is callback seekable
  37. };
  38. } // namespace ffmpeg