arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
MimeDetector.h
1#pragma once
2
3#include "AudioTools/AudioCodecs/MP3HeaderParser.h"
4#include "AudioTools/CoreAudio/AudioBasic/StrView.h"
5
6namespace audio_tools {
7
23 public:
24 MimeDetector() {
25 setCheck("audio/mpeg", checkMP3Ext);
26 setCheck("audio/aac", checkAACExt);
27 setCheck("audio/vnd.wave", checkWAV);
28 setCheck("audio/ogg", checkOGG);
29 setCheck("video/MP2T", checkMP2T);
30 }
31
32 bool begin() {
33 is_first = true;
34 return true;
35 }
36
38 size_t write(uint8_t* data, size_t len) {
39 determineMime(data, len);
40 return len;
41 }
42
44 void setCheck(const char* mime, bool (*check)(uint8_t* start, size_t len)) {
46 for (int j = 0; j < checks.size(); j++) {
47 Check l_check = checks[j];
48 if (mime_str.equals(l_check.mime)) {
49 l_check.check = check;
50 return;
51 }
52 }
53 Check check_to_add{mime, check};
54 checks.push_back(check_to_add);
55 }
56
57 // /// Define the callback that will notify about mime changes
58 void setMimeCallback(void (*callback)(const char*)) {
59 TRACED();
60 this->notifyMimeCallback = callback;
61 }
62
65 const char* mime() { return actual_mime; }
66
67 static bool checkAAC(uint8_t* start, size_t len) {
68 return start[0] == 0xFF &&
69 (start[1] == 0xF0 || start[1] == 0xF1 || start[1] == 0xF9);
70 }
71
72 static bool checkAACExt(uint8_t* start, size_t len) {
73 // quick check
74 if (!(start[0] == 0xFF &&
75 (start[1] == 0xF0 || start[1] == 0xF1 || start[1] == 0xF9)))
76 return false;
77 MP3HeaderParser mp3;
78 // it should start with a synch word
79 if (mp3.findSyncWord((const uint8_t*)start, len) != 0) {
80 return false;
81 }
82 // make sure that it is not an mp3
83 if (mp3.isValid(start, len)) {
84 return false;
85 }
86 return true;
87 }
88
89 static bool checkMP3(uint8_t* start, size_t len) {
90 return memcmp(start, "ID3", 3) == 0 ||
91 (start[0] == 0xFF && ((start[1] & 0xE0) == 0xE0));
92 }
93
94 static bool checkMP3Ext(uint8_t* start, size_t len) {
95 MP3HeaderParser mp3;
96 return mp3.isValid(start, len);
97 }
98
99 static bool checkWAV(uint8_t* start, size_t len) {
100 return memcmp(start, "OggS", 4) == 0;
101 }
102
103 static bool checkOGG(uint8_t* start, size_t len) {
104 return memcmp(start, "OggS", 4) == 0;
105 }
106
108 static bool checkMP2T(uint8_t* start, size_t len) {
109 if (len < 189)
110 return start[0] == 0x47;
111
112 return start[0] == 0x47 && start[188] == 0x47;
113 }
114
115 protected:
116 struct Check {
117 const char* mime = nullptr;
118 bool (*check)(uint8_t* data, size_t len) = nullptr;
119 Check(const char* mime, bool (*check)(uint8_t* data, size_t len)) {
120 this->mime = mime;
121 this->check = check;
122 }
123 Check() = default;
124 };
125 Vector<Check> checks{0};
126 bool is_first = false;
127 const char* actual_mime = nullptr;
128 void (*notifyMimeCallback)(const char* mime) = nullptr;
129
131 void determineMime(void* data, size_t len) {
132 if (is_first) {
133 actual_mime = lookupMime((uint8_t*)data, len);
134 if (notifyMimeCallback != nullptr && actual_mime != nullptr) {
135 notifyMimeCallback(actual_mime);
136 }
137 is_first = false;
138 }
139 }
140
142 const char* lookupMime(uint8_t* data, size_t len) {
143 for (int j = 0; j < checks.size(); j++) {
144 Check l_check = checks[j];
145 if (l_check.check(data, len)) {
146 return l_check.mime;
147 }
148 }
149 return nullptr;
150 }
151};
152
153} // namespace audio_tools
Logic to detemine the mime type from the content. By default the following mime types are supported (...
Definition MimeDetector.h:22
void determineMime(void *data, size_t len)
Update the mime type.
Definition MimeDetector.h:131
void setCheck(const char *mime, bool(*check)(uint8_t *start, size_t len))
adds/updates the checking logic for the indicated mime
Definition MimeDetector.h:44
const char * lookupMime(uint8_t *data, size_t len)
Default logic which supports aac, mp3, wav and ogg.
Definition MimeDetector.h:142
size_t write(uint8_t *data, size_t len)
write the header to determine the mime
Definition MimeDetector.h:38
const char * mime()
Definition MimeDetector.h:65
static bool checkMP2T(uint8_t *start, size_t len)
MPEG-2 TS Byte Stream Format.
Definition MimeDetector.h:108
A simple wrapper to provide string functions on existing allocated char*. If the underlying char* is ...
Definition StrView.h:28
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioConfig.h:885
Definition MimeDetector.h:116