forked from hzeller/rpi-rgb-led-matrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content-streamer.h
94 lines (72 loc) · 2.03 KB
/
content-streamer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
#include <stdint.h>
#include <stdlib.h>
#include <string>
namespace rgb_matrix {
class FrameCanvas;
// An abstraction of a data stream.
class StreamIO {
public:
virtual ~StreamIO() {}
// Rewind stream.
virtual void Rewind() = 0;
// Read bytes into buffer. Similar to Posix behavior that allows short reads.
virtual ssize_t Read(void *buf, size_t count) = 0;
// Write bytes from buffer. Similar to Posix behavior that allows short
// writes.
virtual ssize_t Append(const void *buf, size_t count) = 0;
};
class FileStreamIO : public StreamIO {
public:
explicit FileStreamIO(int fd);
~FileStreamIO();
virtual void Rewind();
virtual ssize_t Read(void *buf, size_t count);
virtual ssize_t Append(const void *buf, size_t count);
private:
const int fd_;
};
class MemStreamIO : public StreamIO {
public:
virtual void Rewind();
virtual ssize_t Read(void *buf, size_t count);
virtual ssize_t Append(const void *buf, size_t count);
private:
std::string buffer_; // super simplistic.
size_t pos_;
};
class StreamWriter {
public:
// Does not take ownership of StreamIO
StreamWriter(StreamIO *io);
// Stream out given canvas at the given time. "hold_time_us" indicates
// for how long this frame is to be shown in microseconds.
bool Stream(const FrameCanvas &frame, uint32_t hold_time_us);
private:
void WriteFileHeader(const FrameCanvas &frame, size_t len);
StreamIO *const io_;
bool header_written_;
};
class StreamReader {
public:
// Does not take ownership of StreamIO
StreamReader(StreamIO *io);
~StreamReader();
// Go back to the beginning.
void Rewind();
// Get next frame and its timestamp. Returns 'false' if there is an error
// or end of stream reached..
bool GetNext(FrameCanvas *frame, uint32_t* hold_time_us);
private:
enum State {
STREAM_AT_BEGIN,
STREAM_READING,
STREAM_ERROR,
};
bool ReadFileHeader(const FrameCanvas &frame);
StreamIO *io_;
size_t buf_size_;
State state_;
char *buffer_;
};
}