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