2#include "fx/detail/data_stream.h"
4#include "fx/storage/filebuffer.h"
7#define INT32_MAX 0x7fffffff
12FASTLED_NAMESPACE_BEGIN
14DataStream::DataStream(
int bytes_per_frame) : mbytesPerFrame(bytes_per_frame), mUsingByteStream(false) {
17DataStream::~DataStream() {
21bool DataStream::begin(FileHandleRef h) {
24 mFileBuffer = FileBufferRef::New(h);
25 mUsingByteStream =
false;
26 return mFileBuffer->available();
29bool DataStream::beginStream(ByteStreamRef s) {
32 mUsingByteStream =
true;
33 return mByteStream->available(mbytesPerFrame);
36void DataStream::close() {
37 if (!mUsingByteStream && mFileBuffer) {
44int32_t DataStream::bytesPerFrame() {
45 return mbytesPerFrame;
48bool DataStream::readPixel(
CRGB* dst) {
49 if (mUsingByteStream) {
50 return mByteStream->read(&dst->
r, 1) && mByteStream->read(&dst->
g, 1) && mByteStream->read(&dst->
b, 1);
52 return mFileBuffer->read(&dst->
r, 1) && mFileBuffer->read(&dst->
g, 1) && mFileBuffer->read(&dst->
b, 1);
56bool DataStream::available()
const {
57 if (mUsingByteStream) {
58 return mByteStream->available(mbytesPerFrame);
60 return mFileBuffer->available();
64bool DataStream::readFrame(
Frame* frame) {
66 if (!framesRemaining() || !frame) {
69 if (mUsingByteStream) {
70 mByteStream->read(frame->rgb(), mbytesPerFrame);
72 mFileBuffer->read(frame->rgb(), mbytesPerFrame);
77int32_t DataStream::framesRemaining()
const {
78 if (mbytesPerFrame == 0)
return 0;
79 int32_t bytes_left = bytesRemaining();
80 if (bytes_left <= 0) {
83 return bytes_left / mbytesPerFrame;
86int32_t DataStream::framesDisplayed()
const {
87 if (mUsingByteStream) {
91 int32_t bytes_played = mFileBuffer->FileSize() - mFileBuffer->BytesLeft();
92 return bytes_played / mbytesPerFrame;
96int32_t DataStream::bytesRemaining()
const {
97 if (mUsingByteStream) {
100 return mFileBuffer->BytesLeft();
104int32_t DataStream::bytesRemainingInFrame()
const {
105 return bytesRemaining() % mbytesPerFrame;
108bool DataStream::rewind() {
109 if (mUsingByteStream) {
113 mFileBuffer->rewindToStart();
118DataStream::Type DataStream::getType()
const {
119 return mUsingByteStream ? Type::kStreaming : Type::kFile;
122size_t DataStream::readBytes(uint8_t* dst,
size_t len) {
123 uint16_t bytesRead = 0;
124 if (mUsingByteStream) {
125 while (bytesRead < len && mByteStream->available(len)) {
127 if (mByteStream->read(dst + bytesRead, 1)) {
134 while (bytesRead < len && mFileBuffer->available()) {
135 if (mFileBuffer->read(dst + bytesRead, 1)) {
uint8_t r
Red channel value.
uint8_t g
Green channel value.
uint8_t b
Blue channel value.
Representation of an RGB pixel (Red, Green, Blue)