content-streamer.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
  2. //
  3. // Abstractions to read and write FrameCanvas objects to streams. This allows
  4. // you to create canned streams of content with minimal overhead at runtime
  5. // to play with extreme pixel-throughput which also minimizes overheads in
  6. // the Pi to avoid stuttering or brightness glitches.
  7. //
  8. // The disadvantage is, that this represents the full expanded internal
  9. // representation of a frame, so is very large memory wise.
  10. //
  11. // These abstractions are used in util/led-image-viewer.cc to read and
  12. // write such animations to disk. It is also used in util/video-viewer.cc
  13. // to write a version to disk that then can be played with the led-image-viewer.
  14. #include <stddef.h>
  15. #include <stdint.h>
  16. #include <stdlib.h>
  17. #include <sys/types.h>
  18. #include <string>
  19. namespace rgb_matrix {
  20. class FrameCanvas;
  21. // An abstraction of a data stream.
  22. class StreamIO {
  23. public:
  24. virtual ~StreamIO() {}
  25. // Rewind stream.
  26. virtual void Rewind() = 0;
  27. // Read bytes into buffer. Similar to Posix behavior that allows short reads.
  28. virtual ssize_t Read(void *buf, size_t count) = 0;
  29. // Write bytes from buffer. Similar to Posix behavior that allows short
  30. // writes.
  31. virtual ssize_t Append(const void *buf, size_t count) = 0;
  32. };
  33. class FileStreamIO : public StreamIO {
  34. public:
  35. explicit FileStreamIO(int fd);
  36. ~FileStreamIO();
  37. virtual void Rewind();
  38. virtual ssize_t Read(void *buf, size_t count);
  39. virtual ssize_t Append(const void *buf, size_t count);
  40. private:
  41. const int fd_;
  42. };
  43. class MemStreamIO : public StreamIO {
  44. public:
  45. virtual void Rewind();
  46. virtual ssize_t Read(void *buf, size_t count);
  47. virtual ssize_t Append(const void *buf, size_t count);
  48. private:
  49. std::string buffer_; // super simplistic.
  50. size_t pos_;
  51. };
  52. class StreamWriter {
  53. public:
  54. // Does not take ownership of StreamIO
  55. StreamWriter(StreamIO *io);
  56. // Stream out given canvas at the given time. "hold_time_us" indicates
  57. // for how long this frame is to be shown in microseconds.
  58. bool Stream(const FrameCanvas &frame, uint32_t hold_time_us);
  59. private:
  60. void WriteFileHeader(const FrameCanvas &frame, size_t len);
  61. StreamIO *const io_;
  62. bool header_written_;
  63. };
  64. class StreamReader {
  65. public:
  66. // Does not take ownership of StreamIO
  67. StreamReader(StreamIO *io);
  68. ~StreamReader();
  69. // Go back to the beginning.
  70. void Rewind();
  71. // Get next frame and its timestamp. Returns 'false' if there is an error
  72. // or end of stream reached..
  73. bool GetNext(FrameCanvas *frame, uint32_t* hold_time_us);
  74. private:
  75. enum State {
  76. STREAM_AT_BEGIN,
  77. STREAM_READING,
  78. STREAM_ERROR,
  79. };
  80. bool ReadFileHeader(const FrameCanvas &frame);
  81. StreamIO *io_;
  82. size_t frame_buf_size_;
  83. State state_;
  84. char *header_frame_buffer_;
  85. };
  86. }