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 <datasource/FileSource.h>
26 #include <media/stagefright/foundation/ADebug.h>
27 #include <media/MediaExtractorPluginHelper.h>
28
29 #define MAXPATHLEN 256
30
31 using namespace android;
32
hexdump(const void * _data,size_t size)33 static void hexdump(const void *_data, size_t size) {
34 const uint8_t *data = (const uint8_t *)_data;
35 size_t offset = 0;
36 while (offset < size) {
37 printf("0x%04zx ", offset);
38
39 size_t n = size - offset;
40 if (n > 16) {
41 n = 16;
42 }
43
44 for (size_t i = 0; i < 16; ++i) {
45 if (i == 8) {
46 printf(" ");
47 }
48
49 if (offset + i < size) {
50 printf("%02x ", data[offset + i]);
51 } else {
52 printf(" ");
53 }
54 }
55
56 printf(" ");
57
58 for (size_t i = 0; i < n; ++i) {
59 if (isprint(data[offset + i])) {
60 printf("%c", data[offset + i]);
61 } else {
62 printf(".");
63 }
64 }
65
66 printf("\n");
67
68 offset += 16;
69 }
70 }
71
scanFile(const char * path)72 void scanFile(const char *path) {
73 sp<FileSource> file = new FileSource(path);
74 CHECK_EQ(file->initCheck(), (status_t)OK);
75
76 DataSourceHelper helper(file->wrap());
77 ID3 tag(&helper);
78 if (!tag.isValid()) {
79 printf("FAIL %s\n", path);
80 } else {
81 printf("SUCCESS %s\n", path);
82
83 ID3::Iterator it(tag, NULL);
84 while (!it.done()) {
85 String8 id;
86 it.getID(&id);
87
88 CHECK(id.length() > 0);
89 if (id[0] == 'T') {
90 String8 text;
91 it.getString(&text);
92
93 printf(" found text frame '%s': %s\n", id.c_str(), text.c_str());
94 } else {
95 printf(" found frame '%s'.\n", id.c_str());
96 }
97
98 it.next();
99 }
100
101 size_t dataSize;
102 String8 mime;
103 const void *data = tag.getAlbumArt(&dataSize, &mime);
104
105 if (data) {
106 printf("found album art: size=%zu mime='%s'\n", dataSize,
107 mime.c_str());
108
109 hexdump(data, dataSize > 128 ? 128 : dataSize);
110 }
111 }
112 }
113
scan(const char * path)114 void scan(const char *path) {
115 struct stat st;
116 if (stat(path, &st) == 0 && S_ISREG(st.st_mode)) {
117 scanFile(path);
118 return;
119 }
120
121 DIR *dir = opendir(path);
122
123 if (dir == NULL) {
124 return;
125 }
126
127 rewinddir(dir);
128
129 struct dirent *ent;
130 while ((ent = readdir(dir)) != NULL) {
131 if (!strcmp(".", ent->d_name) || !strcmp("..", ent->d_name)) {
132 continue;
133 }
134
135 char newPath[MAXPATHLEN];
136 strcpy(newPath, path);
137 strcat(newPath, "/");
138 strcat(newPath, ent->d_name);
139
140 if (ent->d_type == DT_DIR) {
141 scan(newPath);
142 } else if (ent->d_type == DT_REG) {
143 size_t len = strlen(ent->d_name);
144
145 if (len >= 4
146 && !strcasecmp(ent->d_name + len - 4, ".mp3")) {
147 scanFile(newPath);
148 }
149 }
150 }
151
152 closedir(dir);
153 dir = NULL;
154 }
155
main(int argc,char ** argv)156 int main(int argc, char **argv) {
157 android::ProcessState::self()->startThreadPool();
158
159 for (int i = 1; i < argc; ++i) {
160 scan(argv[i]);
161 }
162
163 return 0;
164 }
165