|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "AudioTools/CoreAudio/AudioBasic/StrView.h" |
| 4 | +#include "AudioTools/AudioCodecs/MP3HeaderParser.h" |
| 5 | + |
| 6 | +namespace audio_tools { |
| 7 | + |
| 8 | +/** |
| 9 | + * @brief Logic to detemine the mime type from the content. |
| 10 | + * By default the following mime types are supported (audio/aac, audio/mpeg, |
| 11 | + * audio/vnd.wave, audio/ogg). You can register your own custom detection logic |
| 12 | + * to cover additional file types. |
| 13 | + * @ingroup codecs |
| 14 | + * @ingroup decoder |
| 15 | + * @author Phil Schatzmann |
| 16 | + * @copyright GPLv3 |
| 17 | + */ |
| 18 | + |
| 19 | +class MimeDetector { |
| 20 | + public: |
| 21 | + MimeDetector() { |
| 22 | + setCheck("audio/aac", checkAAC); |
| 23 | + setCheck("audio/mpeg", checkMP3); |
| 24 | + setCheck("audio/vnd.wave", checkWAV); |
| 25 | + setCheck("audio/ogg", checkOGG); |
| 26 | + } |
| 27 | + |
| 28 | + bool begin() { |
| 29 | + is_first = true; |
| 30 | + return true; |
| 31 | + } |
| 32 | + |
| 33 | + /// write the header to determine the mime |
| 34 | + size_t write(uint8_t* data, size_t len) { |
| 35 | + determineMime(data, len); |
| 36 | + return len; |
| 37 | + } |
| 38 | + |
| 39 | + /// adds/updates the checking logic for the indicated mime |
| 40 | + void setCheck(const char* mime, bool (*check)(uint8_t* start, size_t len)) { |
| 41 | + StrView mime_str{mime}; |
| 42 | + for (int j = 0; j < checks.size(); j++) { |
| 43 | + Check l_check = checks[j]; |
| 44 | + if (mime_str.equals(l_check.mime)) { |
| 45 | + l_check.check = check; |
| 46 | + return; |
| 47 | + } |
| 48 | + } |
| 49 | + Check check_to_add{mime, check}; |
| 50 | + checks.push_back(check_to_add); |
| 51 | + } |
| 52 | + |
| 53 | + // /// Define the callback that will notify about mime changes |
| 54 | + void setMimeCallback(void (*callback)(const char*)) { |
| 55 | + TRACED(); |
| 56 | + this->notifyMimeCallback = callback; |
| 57 | + } |
| 58 | + |
| 59 | + /// Provides the actual mime type, that was determined from the first |
| 60 | + /// available data |
| 61 | + const char* mime() { return actual_mime; } |
| 62 | + |
| 63 | + static bool checkAAC(uint8_t* start, size_t len) { |
| 64 | + return start[0] == 0xFF && |
| 65 | + (start[1] == 0xF0 || start[1] == 0xF1 || start[1] == 0xF9); |
| 66 | + } |
| 67 | + |
| 68 | + static bool checkAACExt(uint8_t* start, size_t len) { |
| 69 | + // quick check |
| 70 | + if (!start[0] == 0xFF && |
| 71 | + (start[1] == 0xF0 || start[1] == 0xF1 || start[1] == 0xF9)) |
| 72 | + return false; |
| 73 | + MP3HeaderParser mp3; |
| 74 | + // it should start with a synch word |
| 75 | + if (mp3.findSyncWord(start, len)!=0){ |
| 76 | + return false; |
| 77 | + } |
| 78 | + // make sure that it is not an mp3 |
| 79 | + if (mp3.isValid(start, len)){ |
| 80 | + return false; |
| 81 | + } |
| 82 | + return true; |
| 83 | + } |
| 84 | + |
| 85 | + static bool checkMP3(uint8_t* start, size_t len) { |
| 86 | + return memcmp(start, "ID3", 3) == 0 || |
| 87 | + (start[0] == 0xFF && ((start[1] & 0xE0) == 0xE0)); |
| 88 | + } |
| 89 | + |
| 90 | + static bool checkMP3Ext(uint8_t* start, size_t len) { |
| 91 | + MP3HeaderParser mp3; |
| 92 | + return mp3.isValid(start, len); |
| 93 | + } |
| 94 | + |
| 95 | + static bool checkWAV(uint8_t* start, size_t len) { |
| 96 | + return memcmp(start, "OggS", 4) == 0; |
| 97 | + } |
| 98 | + |
| 99 | + static bool checkOGG(uint8_t* start, size_t len) { |
| 100 | + return memcmp(start, "OggS", 4) == 0; |
| 101 | + } |
| 102 | + |
| 103 | + protected: |
| 104 | + struct Check { |
| 105 | + const char* mime = nullptr; |
| 106 | + bool (*check)(uint8_t* data, size_t len) = nullptr; |
| 107 | + Check(const char* mime, bool (*check)(uint8_t* data, size_t len)) { |
| 108 | + this->mime = mime; |
| 109 | + this->check = check; |
| 110 | + } |
| 111 | + Check() = default; |
| 112 | + }; |
| 113 | + Vector<Check> checks{0}; |
| 114 | + bool is_first = false; |
| 115 | + const char* actual_mime = nullptr; |
| 116 | + void (*notifyMimeCallback)(const char* mime) = nullptr; |
| 117 | + |
| 118 | + /// Update the mime type |
| 119 | + void determineMime(void* data, size_t len) { |
| 120 | + if (is_first) { |
| 121 | + actual_mime = lookupMime((uint8_t*)data, len); |
| 122 | + if (notifyMimeCallback != nullptr && actual_mime != nullptr) { |
| 123 | + notifyMimeCallback(actual_mime); |
| 124 | + } |
| 125 | + is_first = false; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /// Default logic which supports aac, mp3, wav and ogg |
| 130 | + const char* lookupMime(uint8_t* data, size_t len) { |
| 131 | + for (int j = 0; j < checks.size(); j++) { |
| 132 | + Check l_check = checks[j]; |
| 133 | + if (l_check.check(data, len)) { |
| 134 | + return l_check.mime; |
| 135 | + } |
| 136 | + } |
| 137 | + return nullptr; |
| 138 | + } |
| 139 | +}; |
| 140 | + |
| 141 | +} // namespace audio_tools |
0 commit comments