Skip to content

Commit 674abf4

Browse files
committed
MultiStreamingDecoder: support for AudioDecoder
1 parent 2b099d0 commit 674abf4

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

src/AudioTools/AudioCodecs/StreamingDecoder.h

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ class StreamingDecoderAdapter : public StreamingDecoder {
238238
* @return true if data was processed successfully, false otherwise
239239
*/
240240
virtual bool copy() override {
241-
int read = readBytes(&buffer[0], buffer.size());
241+
int read = readBytes(buffer.data(), buffer.size());
242242
int written = 0;
243243
if (read > 0) written = p_decoder->write(&buffer[0], read);
244244
return written > 0;
@@ -312,6 +312,19 @@ class MultiStreamingDecoder : public StreamingDecoder {
312312
*/
313313
MultiStreamingDecoder() = default;
314314

315+
/**
316+
* @brief Destructor
317+
*
318+
* Cleans up any internally created StreamingDecoderAdapter instances.
319+
*/
320+
~MultiStreamingDecoder() {
321+
// Clean up any adapters we created
322+
for (auto* adapter : adapters) {
323+
delete adapter;
324+
}
325+
adapters.clear();
326+
}
327+
315328
/**
316329
* @brief Starts the processing
317330
*
@@ -396,6 +409,7 @@ class MultiStreamingDecoder : public StreamingDecoder {
396409
}
397410
}
398411

412+
399413
/**
400414
* @brief Adds a decoder with explicit MIME type
401415
*
@@ -414,6 +428,33 @@ class MultiStreamingDecoder : public StreamingDecoder {
414428
}
415429
}
416430

431+
/**
432+
* @brief Adds an AudioDecoder with explicit MIME type
433+
*
434+
* Wraps an AudioDecoder in a StreamingDecoderAdapter and registers it with
435+
* the specified MIME type. This allows using traditional AudioDecoder instances
436+
* with the MultiStreamingDecoder's automatic format detection.
437+
*
438+
* @param decoder The AudioDecoder to wrap and register
439+
* @param mime The MIME type string to associate with this decoder
440+
* @param bufferSize Buffer size for the adapter (default: DEFAULT_BUFFER_SIZE)
441+
*
442+
* @note The created StreamingDecoderAdapter is stored internally and will be
443+
* automatically managed by the MultiStreamingDecoder.
444+
*/
445+
void addDecoder(AudioDecoder& decoder, const char* mime, int bufferSize = DEFAULT_BUFFER_SIZE) {
446+
if (mime != nullptr) {
447+
// Create a StreamingDecoderAdapter to wrap the AudioDecoder
448+
auto adapter = new StreamingDecoderAdapter(decoder, mime, bufferSize);
449+
adapters.push_back(adapter); // Store for cleanup
450+
451+
DecoderInfo info{mime, adapter};
452+
decoders.push_back(info);
453+
} else {
454+
LOGE("MIME type is nullptr - cannot add AudioDecoder");
455+
}
456+
}
457+
417458
/**
418459
* @brief Checks if the class is active
419460
*
@@ -691,6 +732,7 @@ class MultiStreamingDecoder : public StreamingDecoder {
691732
} actual_decoder; ///< Currently active decoder information
692733

693734
Vector<DecoderInfo> decoders{0}; ///< Collection of registered decoders
735+
Vector<StreamingDecoderAdapter*> adapters{0}; ///< Collection of internally created adapters
694736
MimeDetector mime_detector; ///< MIME type detection engine
695737
BufferedPrefixStream buffered_stream; ///< Buffered stream for data preservation
696738
Vector<uint8_t> detection_buffer{0}; ///< Buffer for format detection data

0 commit comments

Comments
 (0)