|
1 | 1 | #pragma once
|
| 2 | +#include "AudioTools/CoreAudio/AudioBasic/StrView.h" |
2 | 3 |
|
3 | 4 | namespace audio_tools {
|
4 | 5 |
|
| 6 | + |
5 | 7 | /**
|
6 | 8 | * @brief Logic to detemine the mime type from the content.
|
7 | 9 | * By default the following mime types are supported (audio/aac, audio/mpeg,
|
@@ -59,24 +61,33 @@ class MimeDetector {
|
59 | 61 | }
|
60 | 62 | }
|
61 | 63 |
|
| 64 | + static bool findSyncWord(uint8_t* buf, int nBytes, uint8_t SYNCWORDH, |
| 65 | + uint8_t SYNCWORDL) { |
| 66 | + for (int i = 0; i < nBytes - 1; i++) { |
| 67 | + if ((buf[i + 0] & SYNCWORDH) == SYNCWORDH && |
| 68 | + (buf[i + 1] & SYNCWORDL) == SYNCWORDL) |
| 69 | + return true; |
| 70 | + } |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
62 | 74 | /// Default logic which supports aac, mp3, wav and ogg
|
63 | 75 | static const char* defaultMimeDetector(uint8_t* data, size_t len) {
|
64 | 76 | const char* mime = nullptr;
|
65 |
| - if (len > 4) { |
66 |
| - const uint8_t* start = (const uint8_t*)data; |
67 |
| - if (start[0] == 0xFF && (start[1] == 0xF0 || start[1] == 0xF1 || start[1] == 0xF9)) { |
68 |
| - mime = "audio/aac"; |
69 |
| - } else if (memcmp(start, "ID3", 3) == 0 || start[0] == 0xFF || |
70 |
| - start[1] == 0xFE) { |
71 |
| - mime = "audio/mpeg"; |
72 |
| - } else if (memcmp(start, "RIFF", 4) == 0) { |
73 |
| - mime = "audio/vnd.wave"; |
74 |
| - } else if (memcmp(start, "OggS", 4) == 0) { |
75 |
| - mime = "audio/ogg"; |
76 |
| - } |
| 77 | + if (memcmp(data, "ID3", 3) || (data[0] == (char)0xff && (data[1] == (char)0xfb || data[1] == (char)0xe3 || data[1] == (char)0xe2))) { |
| 78 | + mime = "audio/mpeg"; |
| 79 | + } else if (data[0] == (char)0xff && (data[1] == (char)0xf1 || data[1] == (char)0xf9)) { |
| 80 | + mime = "audio/aac"; |
| 81 | + } else if (memcmp(data, "RIFF", 4) == 0) { |
| 82 | + mime = "audio/vnd.wave"; |
| 83 | + } else if (memcmp(data, "OggS", 4) == 0) { |
| 84 | + mime = "audio/ogg"; |
77 | 85 | }
|
| 86 | + |
78 | 87 | if (mime != nullptr) {
|
79 | 88 | LOGI("Determined mime: %s", mime);
|
| 89 | + } else { |
| 90 | + LOGW("Could not determine mime"); |
80 | 91 | }
|
81 | 92 | return mime;
|
82 | 93 | }
|
|
0 commit comments