arduino-audio-tools
Loading...
Searching...
No Matches
OutputFPSMeter.h
Go to the documentation of this file.
1#pragma once
3
4namespace audio_tools {
5
66 public:
71 OutputFPSMeter(VideoOutput &target) : p_target(&target) {}
72
73 size_t write(const uint8_t *data, size_t len) override {
74 if (len == 0) return 0;
75 // Cached for flush() to finalize with - some decoders (MJPEGDecoder)
76 // only decode on flush(), by which point the raw encoded bytes
77 // isKeyFrame() needs are already gone. OR'd across however many
78 // write() calls precede the next flush(), in case a keyframe marker
79 // isn't in the very first chunk.
81 have_pending = true;
82 uint32_t t0 = millis();
83 size_t result = p_target->write(data, len);
84 call_ms += millis() - t0;
85 return result;
86 }
87
88 void flush() override {
89 uint32_t t0 = millis();
90 p_target->flush();
91 call_ms += millis() - t0;
93 if (frame_count == 0) start_ms = t0;
94 last_ms = millis();
97 if (pending_is_key) {
98 i_count++;
99 i_ms += call_ms;
100 } else {
101 p_count++;
102 p_ms += call_ms;
103 }
104 }
105 call_ms = 0;
106 have_pending = false;
107 pending_is_key = false;
108 }
109
110 void setSkipRender(bool skip) override { p_target->setSkipRender(skip); }
111 bool isKeyFrame(const uint8_t *data, size_t len) override {
112 return p_target->isKeyFrame(data, len);
113 }
114
117 uint32_t frameCount() const { return frame_count; }
119 uint32_t frameCountI() const { return i_count; }
121 uint32_t frameCountP() const { return p_count; }
122
134 void logTo(Print &out) {
135 uint32_t elapsed = last_ms - start_ms;
136 out.print("frames decoded: ");
137 out.print((int)frame_count);
138 out.print(" (I: ");
139 out.print((int)i_count);
140 out.print(" / P: ");
141 out.print((int)p_count);
142 out.println(")");
143 out.print("elapsed: ");
144 out.print((int)elapsed);
145 out.println(" ms");
146 out.print("avg fps (measured, full speed): ");
147 out.println(elapsed > 0 ? 1000.0f * frame_count / elapsed : 0.0f);
148 out.print("avg write ms (decode + whatever the target's own output "
149 "does) - I: ");
150 out.print(i_count > 0 ? (float)i_ms / i_count : 0.0f);
151 out.print(" / P: ");
152 out.print(p_count > 0 ? (float)p_ms / p_count : 0.0f);
153 out.print(" / overall: ");
154 out.println(frame_count > 0 ? (float)total_ms / frame_count : 0.0f);
155 out.print("implied max fps - I: ");
156 out.print(i_ms > 0 ? 1000.0f * i_count / i_ms : 0.0f);
157 out.print(" / P: ");
158 out.print(p_ms > 0 ? 1000.0f * p_count / p_ms : 0.0f);
159 out.print(" / overall: ");
160 out.println(total_ms > 0 ? 1000.0f * frame_count / total_ms : 0.0f);
161 // Only nonzero for a target that tracks decode time separately from
162 // everything after it (currently H264Decoder) - see VideoOutput::
163 // totalDecodeMs()'s own comment. This is the one number here that IS
164 // decode-only, regardless of what the target's own output does.
165 uint64_t decodeMs = p_target->totalDecodeMs();
166 if (decodeMs > 0 && frame_count > 0) {
167 float avgDecodeMs = (float)decodeMs / frame_count;
168 out.print("of which pure decode ms - avg: ");
169 out.print(avgDecodeMs);
170 out.print(" / implied max fps: ");
171 out.println(1000.0f * frame_count / decodeMs);
172 // Everything write() did minus the decode-only share above -
173 // convert/render/SPI, i.e. exactly the display cost this class's
174 // own comment warns is otherwise baked into "avg write ms" above.
175 out.print("of which convert+render ms - avg: ");
176 out.println(((float)total_ms / frame_count) - avgDecodeMs);
177 }
178 }
179
180 protected:
182 // write()-then-flush() pending state - see flush()'s own comment for
183 // why finalization happens there instead of in write().
184 bool have_pending = false;
185 bool pending_is_key = false;
186 uint32_t call_ms = 0;
187 uint32_t frame_count = 0, i_count = 0, p_count = 0;
188 uint32_t total_ms = 0, i_ms = 0, p_ms = 0;
189 uint32_t start_ms = 0, last_ms = 0;
190};
191
192} // namespace audio_tools
Definition Arduino.h:56
Wraps a VideoOutput (typically a codec decoder, e.g. H264Decoder/ MJPEGDecoder/MPGDecoder or a MultiV...
Definition OutputFPSMeter.h:65
void flush() override
Definition OutputFPSMeter.h:88
bool pending_is_key
Definition OutputFPSMeter.h:185
uint32_t total_ms
Definition OutputFPSMeter.h:188
uint32_t i_ms
Definition OutputFPSMeter.h:188
uint32_t start_ms
Definition OutputFPSMeter.h:189
OutputFPSMeter(VideoOutput &target)
Definition OutputFPSMeter.h:71
uint32_t frameCount() const
Definition OutputFPSMeter.h:117
uint32_t frameCountI() const
Number of those classified as keyframes (I-frames).
Definition OutputFPSMeter.h:119
bool have_pending
Definition OutputFPSMeter.h:184
uint32_t p_count
Definition OutputFPSMeter.h:187
size_t write(const uint8_t *data, size_t len) override
Definition OutputFPSMeter.h:73
uint32_t i_count
Definition OutputFPSMeter.h:187
void setSkipRender(bool skip) override
Definition OutputFPSMeter.h:110
uint32_t frameCountP() const
Number of those classified as non-keyframes (P-frames).
Definition OutputFPSMeter.h:121
VideoOutput * p_target
Definition OutputFPSMeter.h:181
uint32_t p_ms
Definition OutputFPSMeter.h:188
uint32_t call_ms
Definition OutputFPSMeter.h:186
bool isKeyFrame(const uint8_t *data, size_t len) override
Definition OutputFPSMeter.h:111
void logTo(Print &out)
Definition OutputFPSMeter.h:134
uint32_t last_ms
Definition OutputFPSMeter.h:189
uint32_t frame_count
Definition OutputFPSMeter.h:187
Abstract class for video playback. This class is used to assemble a complete video frame in memory....
Definition VideoOutput.h:107
virtual size_t write(const uint8_t *data, size_t len)=0
virtual uint64_t totalDecodeMs() const
Definition VideoOutput.h:170
virtual bool hadOutput() const
Definition VideoOutput.h:152
virtual void setSkipRender(bool skip)
Definition VideoOutput.h:121
virtual void flush()
Definition VideoOutput.h:113
virtual bool isKeyFrame(const uint8_t *data, size_t len)
Definition VideoOutput.h:135
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
uint32_t millis()
Returns the milliseconds since the start.
Definition Arduino.h:260