arduino-audio-tools
Loading...
Searching...
No Matches
VideoPlayer.h
Go to the documentation of this file.
1#pragma once
2
11
12namespace audio_tools {
13
126 public:
127 VideoPlayer() = default;
128
136 setVideoOutput(videoOutput);
137 }
138
145 VideoPlayer(Demuxer& demuxer, VideoOutput& videoOutput, AudioOutput& audioOutput) {
147 setVideoOutput(videoOutput);
148 setAudioOutput(audioOutput);
149 }
150
154 VideoPlayer(Demuxer& demuxer, VideoOutput& videoOutput, Print& audioOutput) {
156 setVideoOutput(videoOutput);
157 setAudioOutput(audioOutput);
158 }
159
163 VideoPlayer(Demuxer& demuxer, VideoOutput& videoOutput, AudioStream& audioOutput) {
165 setVideoOutput(videoOutput);
166 setAudioOutput(audioOutput);
167 }
168
172 VideoPlayer(VideoPlayer const&) = delete;
174
182
190 }
191
197
203 void addAudioDecoder(AudioDecoder& decoder, const char* mime = nullptr) {
204 default_audio_decoder.addDecoder(decoder, mime);
205 }
206
221 p_audio_output = &out;
222 p_audio_output_typed = nullptr;
223 p_audio_stream = nullptr;
224 }
226 p_audio_output = &out;
228 p_audio_stream = nullptr;
229 }
231 p_audio_output = &out;
232 p_audio_output_typed = nullptr;
233 p_audio_stream = &out;
234 }
235
240 bool useAudioClock() const { return use_audio_clock; }
241
244 void setBufferSize(int size) { buffer_size = size; }
245
252 bool begin(Stream& source) {
253 if (p_video_output == nullptr) {
254 LOGE("VideoPlayer: no VideoOutput set - call setVideoOutput() first");
255 return false;
256 }
257 p_source = &source;
258
259 // video: decoder -> display, buffered/paced through video_sync
260 // (video_sync's target is fixed at construction - see its own member
261 // declaration below)
265 LOGE("VideoPlayer: MultiVideoDecoder begin() failed");
266 return false;
267 }
269
270 // audio (optional - see the class comment's "Audio clock" section)
271 if (p_audio_output != nullptr) {
274 if (use_audio_clock) {
275 // Route through the most specific overload available (see
276 // setAudioOutput()'s own comment) so audio-info change
277 // notifications still reach the real output.
278 if (p_audio_output_typed != nullptr) {
280 } else if (p_audio_stream != nullptr) {
282 } else {
284 }
287 } else if (p_audio_output_typed != nullptr) {
289 } else if (p_audio_stream != nullptr) {
291 } else {
293 }
294 if (!audio_out.begin()) {
295 LOGE("VideoPlayer: EncodedAudioStream begin() failed");
296 return false;
297 }
299 }
300
301 if (!default_demuxer.begin()) {
302 LOGE("VideoPlayer: MultiVideoDemuxer begin() failed");
303 return false;
304 }
305 active = true;
306 return true;
307 }
308
312 void end() {
313 active = false;
314 video_sync.end();
316 if (p_audio_output != nullptr) audio_out.end();
318 }
319
331 size_t copy() {
332 if (!active || p_source == nullptr) return 0;
333 float fps = default_demuxer.getVideoInfo().fps;
334 if (fps > 0) video_sync.setFps(fps);
335 uint8_t buffer[buffer_size];
336 size_t len = p_source->readBytes(buffer, buffer_size);
337 if (len == 0) return 0;
338 // Retry on a partial accept instead of dropping the remainder, which
339 // would desync the demuxer from the real byte stream (see
340 // CodecCopy::writeAll()'s own comment - duplicated here rather than
341 // reused since CodecCopy binds its Stream& at construction time,
342 // before begin(Stream&)'s source is known).
343 size_t written = 0;
344 while (written < len) {
345 size_t n = default_demuxer.write(buffer + written, len - written);
346 if (n == 0) break;
347 written += n;
348 }
349 return written;
350 }
351
353 size_t copyAll() {
354 size_t result = 0;
355 size_t step = copy();
356 while (step > 0) {
357 result += step;
358 step = copy();
359 }
360 return result;
361 }
362
365 bool isActive() const { return active; }
366 operator bool() const { return active; }
367
371
390 Stream* getStream() { return p_source; }
391
392 protected:
395 // Set alongside p_audio_output by the matching setAudioOutput()
396 // overload - see its own comment for why begin() prefers these over
397 // the generic Print* above when wiring audio_clock/audio_out.
400 Stream* p_source = nullptr;
401
402 // Empty by default (see the class comment) - populated via
403 // addDemuxer().
405 // Declared before video_sync below, which takes this address at
406 // construction time.
408 // Empty by default (see the class comment) - populated via
409 // addAudioDecoder().
411
415
416 bool use_audio_clock = false;
417 int buffer_size = 1024;
418 bool active = false;
419};
420
421} // namespace audio_tools
#define LOGE(...)
Definition AudioLoggerIDF.h:30
Definition Arduino.h:56
Definition Arduino.h:136
virtual size_t readBytes(uint8_t *data, size_t len)
Definition Arduino.h:140
Decoding of encoded audio into PCM data.
Definition AudioCodecsBase.h:19
Abstract Audio Ouptut class.
Definition AudioOutput.h:25
Base class for all Audio Streams. It support the boolean operator to test if the object is ready with...
Definition BaseStream.h:120
AudioTimeSourceStream: A stream that provides time information based on the audio data actually proce...
Definition AudioIO.h:1024
void setStream(Stream &stream)
Definition AudioIO.h:1094
void setOutput(Print &out)
Definition AudioIO.h:1087
Common interface for demuxers (DemuxerAVI, DemuxerMP4) that split a container's video and (optional) ...
Definition ContainerCommon.h:164
A more natural Stream class to process encoded data (aac, wav, mp3...) which also supports the decodi...
Definition AudioEncoded.h:282
void end() override
Definition AudioEncoded.h:377
void setDecoder(AudioDecoder *decoder)
Definition AudioEncoded.h:319
void setOutput(AudioOutput *stream)
Definition AudioEncoded.h:335
void setStream(Stream *stream)
Definition AudioEncoded.h:331
bool begin(AudioInfo info)
Definition AudioEncoded.h:364
Manage multiple AudioDecoders with automatic format detection.
Definition MultiDecoder.h:43
void setMimeSource(MimeSource &mimeSource)
Sets an external MIME source for format detection.
Definition MultiDecoder.h:162
void addDecoder(AudioDecoder &decoder, const char *mime=nullptr)
Adds a decoder that will be selected by its MIME type.
Definition MultiDecoder.h:127
Manages multiple VideoDecoders with automatic format detection - the video-side counterpart of MultiD...
Definition MultiVideoDecoder.h:59
void setOutput(Print &out) override
Defines the target each decoded picture is written to.
Definition MultiVideoDecoder.h:97
void end() override
Releases the decoder's resources.
Definition MultiVideoDecoder.h:134
void addDecoder(VideoDecoder &decoder)
Definition MultiVideoDecoder.h:74
void setVideoInfoSource(VideoInfoSource &source)
Definition MultiVideoDecoder.h:93
bool begin() override
Definition MultiVideoDecoder.h:127
Manages multiple Demuxers with automatic container-format detection - the container-side counterpart ...
Definition MultiVideoDemuxer.h:52
void addDemuxer(Demuxer &demuxer)
Definition MultiVideoDemuxer.h:64
VideoInfo getVideoInfo() override
Definition MultiVideoDemuxer.h:120
void setOutputAudio(Print &out) override
Definition MultiVideoDemuxer.h:78
void end() override
Definition MultiVideoDemuxer.h:139
size_t write(const uint8_t *data, size_t len) override
Definition MultiVideoDemuxer.h:145
bool begin() override
Definition MultiVideoDemuxer.h:133
void setOutputVideo(Print &out) override
Definition MultiVideoDemuxer.h:88
Buffers a small, configurable amount of video (see setQueueBytes()) and renders it frame by frame fro...
Definition PacedVideoOutput.h:62
void setFps(float fps)
Definition PacedVideoOutput.h:81
void end()
Stops the background render task.
Definition PacedVideoOutput.h:230
void setAudioClock(TimeSource &clock)
Definition PacedVideoOutput.h:89
Common interface for video decoders (e.g. H264Decoder, H264DecoderESP32S3 - CodecH264....
Definition CodecVideo.h:18
Abstract class for video playback. This class is used to assemble a complete video frame in memory....
Definition Video.h:210
High-level video playback pipeline and controller - the video counterpart of AudioPlayer (CoreAudio/A...
Definition VideoPlayer.h:125
bool active
Definition VideoPlayer.h:418
AudioStream * p_audio_stream
Definition VideoPlayer.h:399
void addDemuxer(Demuxer &demuxer)
Definition VideoPlayer.h:181
AudioOutput * p_audio_output_typed
Definition VideoPlayer.h:398
AudioTimeSourceStream audio_clock
Definition VideoPlayer.h:413
size_t copyAll()
Copies until the source is exhausted (blocking).
Definition VideoPlayer.h:353
PacedVideoOutput & videoSyncTask()
Definition VideoPlayer.h:388
MultiVideoDecoder default_video_decoder
Definition VideoPlayer.h:407
MultiVideoDecoder & videoDecoder()
Definition VideoPlayer.h:379
bool isActive() const
Definition VideoPlayer.h:365
VideoPlayer(Demuxer &demuxer, VideoOutput &videoOutput, AudioOutput &audioOutput)
Definition VideoPlayer.h:145
Stream * p_source
Definition VideoPlayer.h:400
VideoPlayer(Demuxer &demuxer, VideoOutput &videoOutput)
Definition VideoPlayer.h:134
VideoPlayer(Demuxer &demuxer, VideoOutput &videoOutput, AudioStream &audioOutput)
Definition VideoPlayer.h:163
bool useAudioClock() const
Definition VideoPlayer.h:240
Stream * getStream()
The Stream given to begin() - nullptr before the first begin() call.
Definition VideoPlayer.h:390
void setVideoOutput(VideoOutput &out)
Definition VideoPlayer.h:196
void setAudioOutput(Print &out)
Definition VideoPlayer.h:220
PacedVideoOutput video_sync
Definition VideoPlayer.h:412
MultiVideoDemuxer default_demuxer
Definition VideoPlayer.h:404
bool use_audio_clock
Definition VideoPlayer.h:416
EncodedAudioStream audio_out
Definition VideoPlayer.h:414
void addVideoDecoder(VideoDecoder &decoder)
Definition VideoPlayer.h:188
void setAudioOutput(AudioStream &out)
Definition VideoPlayer.h:230
void addAudioDecoder(AudioDecoder &decoder, const char *mime=nullptr)
Definition VideoPlayer.h:203
void end()
Definition VideoPlayer.h:312
MultiVideoDemuxer & demuxer()
Definition VideoPlayer.h:375
void setUseAudioClock(bool active)
Definition VideoPlayer.h:239
void setActive(bool isActive)
Definition VideoPlayer.h:370
void setBufferSize(int size)
Definition VideoPlayer.h:244
MultiDecoder default_audio_decoder
Definition VideoPlayer.h:410
VideoPlayer(Demuxer &demuxer, VideoOutput &videoOutput, Print &audioOutput)
Definition VideoPlayer.h:154
MultiDecoder & audioDecoder()
Definition VideoPlayer.h:383
VideoPlayer & operator=(VideoPlayer const &)=delete
void setAudioOutput(AudioOutput &out)
Definition VideoPlayer.h:225
size_t copy()
Definition VideoPlayer.h:331
int buffer_size
Definition VideoPlayer.h:417
VideoPlayer(VideoPlayer const &)=delete
bool begin(Stream &source)
Definition VideoPlayer.h:252
Print * p_audio_output
Definition VideoPlayer.h:394
VideoOutput * p_video_output
Definition VideoPlayer.h:393
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
float fps
Definition Video.h:128