1 /*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "../include/ID3.h"
18
19 #include <sys/stat.h>
20
21 #include <ctype.h>
22 #include <dirent.h>
23
24 #include <binder/ProcessState.h>
25 #include <media/stagefright/FileSource.h>
26 #include <media/stagefright/foundation/ADebug.h>
27
28 #define MAXPATHLEN 256
29
30 using namespace android;
31
hexdump(const void * _data,size_t size)32 static void hexdump(const void *_data, size_t size) {
33 const uint8_t *data = (const uint8_t *)_data;
34 size_t offset = 0;
35 while (offset < size) {
36 printf("0x%04x ", offset);
37
38 size_t n = size - offset;
39 if (n > 16) {
40 n = 16;
41 }
42
43 for (size_t i = 0; i < 16; ++i) {
44 if (i == 8) {
45 printf(" ");
46 }
47
48 if (offset + i < size) {
49 printf("%02x ", data[offset + i]);
50 } else {
51 printf(" ");
52 }
53 }
54
55 printf(" ");
56
57 for (size_t i = 0; i < n; ++i) {
58 if (isprint(data[offset + i])) {
59 printf("%c", data[offset + i]);
60 } else {
61 printf(".");
62 }
63 }
64
65 printf("\n");
66
67 offset += 16;
68 }
69 }
70
scanFile(const char * path)71 void scanFile(const char *path) {
72 sp<FileSource> file = new FileSource(path);
73 CHECK_EQ(file->initCheck(), (status_t)OK);
74
75 ID3 tag(file);
76 if (!tag.isValid()) {
77 printf("FAIL %s\n", path);
78 } else {
79 printf("SUCCESS %s\n", path);
80
81 ID3::Iterator it(tag, NULL);
82 while (!it.done()) {
83 String8 id;
84 it.getID(&id);
85
86 CHECK(id.length() > 0);
87 if (id[0] == 'T') {
88 String8 text;
89 it.getString(&text);
90
91 printf(" found text frame '%s': %s\n", id.string(), text.string());
92 } else {
93 printf(" found frame '%s'.\n", id.string());
94 }
95
96 it.next();
97 }
98
99 size_t dataSize;
100 String8 mime;
101 const void *data = tag.getAlbumArt(&dataSize, &mime);
102
103 if (data) {
104 printf("found album art: size=%d mime='%s'\n", dataSize,
105 mime.string());
106
107 hexdump(data, dataSize > 128 ? 128 : dataSize);
108 }
109 }
110 }
111
scan(const char * path)112 void scan(const char *path) {
113 struct stat st;
114 if (stat(path, &st) == 0 && S_ISREG(st.st_mode)) {
115 scanFile(path);
116 return;
117 }
118
119 DIR *dir = opendir(path);
120
121 if (dir == NULL) {
122 return;
123 }
124
125 rewinddir(dir);
126
127 struct dirent *ent;
128 while ((ent = readdir(dir)) != NULL) {
129 if (!strcmp(".", ent->d_name) || !strcmp("..", ent->d_name)) {
130 continue;
131 }
132
133 char newPath[MAXPATHLEN];
134 strcpy(newPath, path);
135 strcat(newPath, "/");
136 strcat(newPath, ent->d_name);
137
138 if (ent->d_type == DT_DIR) {
139 scan(newPath);
140 } else if (ent->d_type == DT_REG) {
141 size_t len = strlen(ent->d_name);
142
143 if (len >= 4
144 && !strcasecmp(ent->d_name + len - 4, ".mp3")) {
145 scanFile(newPath);
146 }
147 }
148 }
149
150 closedir(dir);
151 dir = NULL;
152 }
153
main(int argc,char ** argv)154 int main(int argc, char **argv) {
155 android::ProcessState::self()->startThreadPool();
156
157 DataSource::RegisterDefaultSniffers();
158
159 for (int i = 1; i < argc; ++i) {
160 scan(argv[i]);
161 }
162
163 return 0;
164 }
165