Skip to content

Commit dafe4c4

Browse files
committed
StreamCopy support for custom mime determination
1 parent 4cb1308 commit dafe4c4

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

src/AudioTools/CoreAudio/StreamCopy.h

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class StreamCopyT {
153153
}
154154

155155
// determine mime
156-
notifyMime(buffer.data(), bytes_to_read);
156+
determineMime(buffer.data(), bytes_to_read);
157157

158158
// convert data
159159
if (p_converter!=nullptr) p_converter->convert((uint8_t*)buffer.data(), bytes_read );
@@ -352,6 +352,11 @@ class StreamCopyT {
352352
is_sync_audio_info = active;
353353
}
354354

355+
/// Defines the mime detector
356+
void setMimeDetector(const char* (*mimeDetectCallback)(uint8_t* data, size_t len)){
357+
this->mimeDetectCallback = mimeDetectCallback;
358+
}
359+
355360
protected:
356361
Stream *from = nullptr;
357362
AudioStream *from_audio = nullptr;
@@ -361,6 +366,7 @@ class StreamCopyT {
361366
void (*onWrite)(void*obj, void*buffer, size_t len) = nullptr;
362367
void (*notifyMimeCallback)(const char*mime) = nullptr;
363368
int (*availableCallback)(Stream*stream)=nullptr;
369+
const char* (*mimeDetectCallback)(uint8_t* data, size_t len) = defaultMimeDetector;
364370
void *onWriteObj = nullptr;
365371
bool is_first = false;
366372
bool check_available_for_write = false;
@@ -427,22 +433,30 @@ class StreamCopyT {
427433
}
428434

429435
/// Update the mime type
430-
void notifyMime(void* data, size_t len){
431-
if (is_first && len>4) {
436+
void determineMime(void* data, size_t len){
437+
if (is_first) {
438+
actual_mime = mimeDetectCallback((uint8_t*)data, len);
439+
if (notifyMimeCallback!=nullptr && actual_mime!=nullptr){
440+
notifyMimeCallback(actual_mime);
441+
}
442+
is_first = false;
443+
}
444+
}
445+
446+
static const char* defaultMimeDetector(uint8_t* data, size_t len){
447+
const char* mime = nullptr;
448+
if (len > 4) {
432449
const uint8_t *start = (const uint8_t *) data;
433-
actual_mime = "audio/basic";
450+
mime = "audio/basic";
434451
if (start[0]==0xFF && start[1]==0xF1){
435-
actual_mime = "audio/aac";
452+
mime = "audio/aac";
436453
} else if (memcmp(start,"ID3",3) || start[0]==0xFF || start[0]==0xFE ){
437-
actual_mime = "audio/mpeg";
454+
mime = "audio/mpeg";
438455
} else if (memcmp(start,"RIFF",4)){
439-
actual_mime = "audio/vnd.wave";
440-
}
441-
if (notifyMimeCallback!=nullptr){
442-
notifyMimeCallback(actual_mime);
456+
mime = "audio/vnd.wave";
443457
}
444458
}
445-
is_first = false;
459+
return mime;
446460
}
447461

448462
};

0 commit comments

Comments
 (0)