arduino-audio-tools
|
Manage multiple StreamingDecoders with automatic format detection. More...
#include <StreamingDecoder.h>
Classes | |
struct | DecoderInfo |
Information about a registered decoder. More... | |
Public Member Functions | |
MultiStreamingDecoder ()=default | |
Default constructor. | |
~MultiStreamingDecoder () | |
Destructor. | |
void | addDecoder (AudioDecoder &decoder, const char *mime, int bufferSize=DEFAULT_BUFFER_SIZE) |
Adds an AudioDecoder with explicit MIME type. | |
void | addDecoder (StreamingDecoder &decoder) |
Adds a decoder that will be selected by its MIME type. | |
void | addDecoder (StreamingDecoder &decoder, const char *mime) |
Adds a decoder with explicit MIME type. | |
virtual void | addNotifyAudioChange (AudioInfoSupport &bi) |
Adds target to be notified about audio changes. | |
AudioInfo | audioInfo () override |
Provides the audio information from the selected decoder. | |
virtual AudioInfo | audioInfoOut () |
bool | begin () override |
Starts the processing. | |
virtual void | clearNotifyAudioChange () |
Deletes all change notify subscriptions. | |
virtual bool | copy () override |
Process a single read operation - to be called in the loop. | |
bool | copyAll () |
Process all available data. | |
void | end () override |
Releases the reserved memory. | |
bool | isNotifyActive () |
Checks if the automatic AudioInfo update is active. | |
const char * | mime () override |
Provides the MIME type of the selected decoder. | |
MimeDetector & | mimeDetector () |
Provides access to the internal MIME detector. | |
virtual | operator bool () override |
Checks if the class is active. | |
virtual bool | removeNotifyAudioChange (AudioInfoSupport &bi) |
Removes a target in order not to be notified about audio changes. | |
bool | selectDecoder (const char *mime) |
Selects the actual decoder by MIME type. | |
const char * | selectedMime () |
Returns the MIME type that was detected and selected. | |
void | setInput (Stream &inStream) |
Stream Interface: Decode directly by taking data from the stream. | |
void | setMimeSource (MimeSource &mimeSource) |
Sets an external MIME source for format detection. | |
void | setNotifyActive (bool flag) |
Deactivate/Reactivate automatic AudioInfo updates: (default is active) | |
void | setOutput (AudioOutput &out_stream) override |
Defines the output streams and register to be notified. | |
void | setOutput (AudioStream &out_stream) override |
Defines the output streams and register to be notified. | |
void | setOutput (Print &out_stream) override |
Defines the output Stream. | |
Protected Member Functions | |
void | notifyAudioChange (AudioInfo info) |
size_t | readBytes (uint8_t *data, size_t len) override |
Reads bytes from the input stream. | |
bool | selectDecoder () |
Automatically detects MIME type and selects appropriate decoder. | |
void | setAudioInfo (AudioInfo newInfo) override |
Defines the input AudioInfo. | |
const char * | toStr (const char *str) |
Protected Attributes | |
struct audio_tools::MultiStreamingDecoder::DecoderInfo | actual_decoder |
Currently active decoder information. | |
Vector< StreamingDecoderAdapter * > | adapters |
Collection of internally created adapters. | |
BufferedStream | buffered_stream {0} |
Buffered stream for data preservation. | |
Vector< DecoderInfo > | decoders {0} |
Collection of registered decoders. | |
Vector< uint8_t > | detection_buffer {0} |
Buffer for format detection data. | |
AudioInfo | info |
bool | is_first = true |
Flag for first copy() call. | |
bool | is_notify_active = true |
MimeDetector | mime_detector |
MIME type detection engine. | |
Vector< AudioInfoSupport * > | notify_vector |
Stream * | p_data_source = nullptr |
effective data source for decoder | |
Stream * | p_input = nullptr |
Input stream for encoded audio data. | |
MimeSource * | p_mime_source |
Optional MIME source for custom logic. | |
Print * | p_print = nullptr |
Output stream for decoded PCM data. | |
const char * | selected_mime = nullptr |
MIME type that was selected. | |
Manage multiple StreamingDecoders with automatic format detection.
This class automatically detects the audio format from incoming streaming data and selects the appropriate decoder from a collection of registered decoders. The format detection is performed using the MimeDetector on the first chunk of data, and the detected data is preserved for the selected decoder using a buffered stream approach.
Key features:
|
inline |
Destructor.
Cleans up any internally created StreamingDecoderAdapter instances.
|
inline |
Adds an AudioDecoder with explicit MIME type.
Wraps an AudioDecoder in a StreamingDecoderAdapter and registers it with the specified MIME type. This allows using traditional AudioDecoder instances with the MultiStreamingDecoder's automatic format detection.
decoder | The AudioDecoder to wrap and register |
mime | The MIME type string to associate with this decoder |
bufferSize | Buffer size for the adapter (default: DEFAULT_BUFFER_SIZE) |
|
inline |
Adds a decoder that will be selected by its MIME type.
Registers a StreamingDecoder that will be automatically selected when the corresponding MIME type is detected in the input stream.
decoder | The StreamingDecoder to register |
|
inline |
Adds a decoder with explicit MIME type.
Registers a StreamingDecoder with a specific MIME type, which may be different from what the decoder's mime() method returns.
decoder | The StreamingDecoder to register |
mime | The MIME type string to associate with this decoder |
|
inlinevirtualinherited |
Adds target to be notified about audio changes.
Reimplemented in CodecNOP, EncodedAudioOutput, EncodedAudioStream, AACDecoderFDK, DecoderBasic, CodecChain, MP3DecoderHelix, MP3DecoderMAD, OggContainerDecoder, RTSPClient< TcpClient, UdpSocket >, Pipeline, and Pipeline::ModifyingStreamAdapter.
|
inlineoverridevirtual |
Provides the audio information from the selected decoder.
Implements StreamingDecoder.
|
inlinevirtualinherited |
provides the actual output AudioInfo: this is usually the same as audioInfo() unless we use a transforming stream
Reimplemented in PureDataStream, PWMAudioOutput, ChannelFormatConverterStreamT< T >, ChannelFormatConverterStream, NumberFormatConverterStreamT< TFrom, TTo >, NumberFormatConverterStream, FormatConverterStream, Pipeline, ResampleStream, and ResampleStreamT< TInterpolator >.
|
inlineoverridevirtual |
Starts the processing.
Initializes the MIME detector and prepares for format detection.
Implements StreamingDecoder.
|
inlinevirtualinherited |
Deletes all change notify subscriptions.
Reimplemented in RTSPClient< TcpClient, UdpSocket >.
|
inlineoverridevirtual |
Process a single read operation - to be called in the loop.
On the first call, this method reads data for format detection, selects the appropriate decoder, and sets up a buffered stream. Subsequent calls delegate to the selected decoder's copy() method.
Implements StreamingDecoder.
|
inlineinherited |
Process all available data.
Convenience method that calls copy() repeatedly until all available data has been processed.
|
inlineoverridevirtual |
Releases the reserved memory.
Stops the currently active decoder and resets the state for next use.
Implements StreamingDecoder.
|
inlineoverridevirtual |
Provides the MIME type of the selected decoder.
Implements StreamingDecoder.
|
inline |
Provides access to the internal MIME detector.
Returns a reference to the MimeDetector instance used for automatic format detection. This allows access to advanced features such as:
|
inlineoverridevirtual |
Checks if the class is active.
Implements StreamingDecoder.
|
inlineoverrideprotectedvirtual |
Reads bytes from the input stream.
data | Buffer to store read data |
len | Maximum number of bytes to read |
Implements StreamingDecoder.
|
inlinevirtualinherited |
Removes a target in order not to be notified about audio changes.
Reimplemented in RTSPClient< TcpClient, UdpSocket >.
|
inlineprotected |
Automatically detects MIME type and selects appropriate decoder.
This method performs automatic format detection and decoder selection when no decoder is currently active. It supports two modes of operation:
The method reads a small sample of data (80 bytes) from the input stream for format detection, then preserves this data in a buffered stream so it remains available to the selected decoder. This ensures no audio data is lost during the detection process.
|
inline |
Selects the actual decoder by MIME type.
Searches through registered decoders to find one that matches the detected MIME type, then initializes it for use.
mime | The MIME type string to match |
|
inline |
Returns the MIME type that was detected and selected.
|
inlineoverrideprotectedvirtualinherited |
Defines the input AudioInfo.
Implements AudioInfoSupport.
|
inline |
Stream Interface: Decode directly by taking data from the stream.
inStream | The input stream containing encoded audio data |
|
inline |
Sets an external MIME source for format detection.
Provides an alternative to automatic MIME detection by allowing an external source to provide the MIME type information. This is particularly useful when the MIME type is already known from other sources such as:
When a MIME source is set, the automatic detection process (which requires reading and analyzing stream data) is bypassed, making the decoder initialization more efficient and faster.
mimeSource | Reference to a MimeSource object that provides the MIME type through its mime() method |
|
inlineoverridevirtual |
Defines the output streams and register to be notified.
out_stream | The AudioOutput for decoded audio data |
Reimplemented from StreamingDecoder.
|
inlineoverridevirtual |
Defines the output streams and register to be notified.
out_stream | The AudioStream for decoded audio data |
Reimplemented from StreamingDecoder.
|
inlineoverridevirtual |
Defines the output Stream.
out_stream | The output stream for decoded audio data |
Reimplemented from StreamingDecoder.
|
protected |
Collection of internally created adapters.
|
protected |
Optional MIME source for custom logic.