1/*
2 * Copyright (C) 2009 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//#define LOG_NDEBUG 0
18#define LOG_TAG "StagefrightMediaScanner"
19#include <utils/Log.h>
20
21#include <media/stagefright/StagefrightMediaScanner.h>
22
23#include <media/mediametadataretriever.h>
24#include <private/media/VideoFrame.h>
25
26// Sonivox includes
27#include <libsonivox/eas.h>
28
29namespace android {
30
31StagefrightMediaScanner::StagefrightMediaScanner() {}
32
33StagefrightMediaScanner::~StagefrightMediaScanner() {}
34
35static bool FileHasAcceptableExtension(const char *extension) {
36    static const char *kValidExtensions[] = {
37        ".mp3", ".mp4", ".m4a", ".3gp", ".3gpp", ".3g2", ".3gpp2",
38        ".mpeg", ".ogg", ".mid", ".smf", ".imy", ".wma", ".aac",
39        ".wav", ".amr", ".midi", ".xmf", ".rtttl", ".rtx", ".ota",
40        ".mkv", ".mka", ".webm", ".ts", ".fl", ".flac", ".mxmf",
41        ".avi", ".mpeg", ".mpg"
42    };
43    static const size_t kNumValidExtensions =
44        sizeof(kValidExtensions) / sizeof(kValidExtensions[0]);
45
46    for (size_t i = 0; i < kNumValidExtensions; ++i) {
47        if (!strcasecmp(extension, kValidExtensions[i])) {
48            return true;
49        }
50    }
51
52    return false;
53}
54
55static MediaScanResult HandleMIDI(
56        const char *filename, MediaScannerClient *client) {
57    // get the library configuration and do sanity check
58    const S_EAS_LIB_CONFIG* pLibConfig = EAS_Config();
59    if ((pLibConfig == NULL) || (LIB_VERSION != pLibConfig->libVersion)) {
60        LOGE("EAS library/header mismatch\n");
61        return MEDIA_SCAN_RESULT_ERROR;
62    }
63    EAS_I32 temp;
64
65    // spin up a new EAS engine
66    EAS_DATA_HANDLE easData = NULL;
67    EAS_HANDLE easHandle = NULL;
68    EAS_RESULT result = EAS_Init(&easData);
69    if (result == EAS_SUCCESS) {
70        EAS_FILE file;
71        file.path = filename;
72        file.fd = 0;
73        file.offset = 0;
74        file.length = 0;
75        result = EAS_OpenFile(easData, &file, &easHandle);
76    }
77    if (result == EAS_SUCCESS) {
78        result = EAS_Prepare(easData, easHandle);
79    }
80    if (result == EAS_SUCCESS) {
81        result = EAS_ParseMetaData(easData, easHandle, &temp);
82    }
83    if (easHandle) {
84        EAS_CloseFile(easData, easHandle);
85    }
86    if (easData) {
87        EAS_Shutdown(easData);
88    }
89
90    if (result != EAS_SUCCESS) {
91        return MEDIA_SCAN_RESULT_SKIPPED;
92    }
93
94    char buffer[20];
95    sprintf(buffer, "%ld", temp);
96    status_t status = client->addStringTag("duration", buffer);
97    if (status != OK) {
98        return MEDIA_SCAN_RESULT_ERROR;
99    }
100    return MEDIA_SCAN_RESULT_OK;
101}
102
103MediaScanResult StagefrightMediaScanner::processFile(
104        const char *path, const char *mimeType,
105        MediaScannerClient &client) {
106    LOGV("processFile '%s'.", path);
107
108    client.setLocale(locale());
109    client.beginFile();
110    MediaScanResult result = processFileInternal(path, mimeType, client);
111    client.endFile();
112    return result;
113}
114
115MediaScanResult StagefrightMediaScanner::processFileInternal(
116        const char *path, const char *mimeType,
117        MediaScannerClient &client) {
118    const char *extension = strrchr(path, '.');
119
120    if (!extension) {
121        return MEDIA_SCAN_RESULT_SKIPPED;
122    }
123
124    if (!FileHasAcceptableExtension(extension)) {
125        return MEDIA_SCAN_RESULT_SKIPPED;
126    }
127
128    if (!strcasecmp(extension, ".mid")
129            || !strcasecmp(extension, ".smf")
130            || !strcasecmp(extension, ".imy")
131            || !strcasecmp(extension, ".midi")
132            || !strcasecmp(extension, ".xmf")
133            || !strcasecmp(extension, ".rtttl")
134            || !strcasecmp(extension, ".rtx")
135            || !strcasecmp(extension, ".ota")
136            || !strcasecmp(extension, ".mxmf")) {
137        return HandleMIDI(path, &client);
138    }
139
140    sp<MediaMetadataRetriever> mRetriever(new MediaMetadataRetriever);
141
142    status_t status = mRetriever->setDataSource(path);
143    if (status) {
144        return MEDIA_SCAN_RESULT_ERROR;
145    }
146
147    const char *value;
148    if ((value = mRetriever->extractMetadata(
149                    METADATA_KEY_MIMETYPE)) != NULL) {
150        status = client.setMimeType(value);
151        if (status) {
152            return MEDIA_SCAN_RESULT_ERROR;
153        }
154    }
155
156    struct KeyMap {
157        const char *tag;
158        int key;
159    };
160    static const KeyMap kKeyMap[] = {
161        { "tracknumber", METADATA_KEY_CD_TRACK_NUMBER },
162        { "discnumber", METADATA_KEY_DISC_NUMBER },
163        { "album", METADATA_KEY_ALBUM },
164        { "artist", METADATA_KEY_ARTIST },
165        { "albumartist", METADATA_KEY_ALBUMARTIST },
166        { "composer", METADATA_KEY_COMPOSER },
167        { "genre", METADATA_KEY_GENRE },
168        { "title", METADATA_KEY_TITLE },
169        { "year", METADATA_KEY_YEAR },
170        { "duration", METADATA_KEY_DURATION },
171        { "writer", METADATA_KEY_WRITER },
172        { "compilation", METADATA_KEY_COMPILATION },
173        { "isdrm", METADATA_KEY_IS_DRM },
174    };
175    static const size_t kNumEntries = sizeof(kKeyMap) / sizeof(kKeyMap[0]);
176
177    for (size_t i = 0; i < kNumEntries; ++i) {
178        const char *value;
179        if ((value = mRetriever->extractMetadata(kKeyMap[i].key)) != NULL) {
180            status = client.addStringTag(kKeyMap[i].tag, value);
181            if (status != OK) {
182                return MEDIA_SCAN_RESULT_ERROR;
183            }
184        }
185    }
186
187    return MEDIA_SCAN_RESULT_OK;
188}
189
190char *StagefrightMediaScanner::extractAlbumArt(int fd) {
191    LOGV("extractAlbumArt %d", fd);
192
193    off64_t size = lseek64(fd, 0, SEEK_END);
194    if (size < 0) {
195        return NULL;
196    }
197    lseek64(fd, 0, SEEK_SET);
198
199    sp<MediaMetadataRetriever> mRetriever(new MediaMetadataRetriever);
200    if (mRetriever->setDataSource(fd, 0, size) == OK) {
201        sp<IMemory> mem = mRetriever->extractAlbumArt();
202
203        if (mem != NULL) {
204            MediaAlbumArt *art = static_cast<MediaAlbumArt *>(mem->pointer());
205
206            char *data = (char *)malloc(art->mSize + 4);
207            *(int32_t *)data = art->mSize;
208            memcpy(&data[4], &art[1], art->mSize);
209
210            return data;
211        }
212    }
213
214    return NULL;
215}
216
217}  // namespace android
218