blob: 84f65ff11fdba0a9b8a719eb1d93daae45aff3be [file] [log] [blame]
Andreas Hubere4a83802010-01-08 10:57:34 -08001/*
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
Andreas Huberaee3c632010-01-11 15:35:19 -080017//#define LOG_NDEBUG 0
18#define LOG_TAG "StagefrightMediaScanner"
19#include <utils/Log.h>
20
Andreas Hubere4a83802010-01-08 10:57:34 -080021#include <media/stagefright/StagefrightMediaScanner.h>
22
Andreas Huber1ce98672010-03-10 10:55:35 -080023#include <media/mediametadataretriever.h>
24#include <private/media/VideoFrame.h>
Andreas Hubere4a83802010-01-08 10:57:34 -080025
Andreas Huberaee3c632010-01-11 15:35:19 -080026// Sonivox includes
27#include <libsonivox/eas.h>
28
Andreas Hubere4a83802010-01-08 10:57:34 -080029namespace android {
30
Mike Lockwood8151dc322011-01-25 15:20:04 -080031StagefrightMediaScanner::StagefrightMediaScanner() {}
Andreas Hubere4a83802010-01-08 10:57:34 -080032
33StagefrightMediaScanner::~StagefrightMediaScanner() {}
34
Andreas Huberaee3c632010-01-11 15:35:19 -080035static 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",
Andreas Huber072f5242010-05-20 14:56:53 -070039 ".wav", ".amr", ".midi", ".xmf", ".rtttl", ".rtx", ".ota",
Glenn Kastenf9f223e2011-01-13 11:17:00 -080040 ".mkv", ".mka", ".webm", ".ts", ".fl", ".flac"
Andreas Huberaee3c632010-01-11 15:35:19 -080041 };
42 static const size_t kNumValidExtensions =
43 sizeof(kValidExtensions) / sizeof(kValidExtensions[0]);
44
45 for (size_t i = 0; i < kNumValidExtensions; ++i) {
46 if (!strcasecmp(extension, kValidExtensions[i])) {
47 return true;
48 }
49 }
50
51 return false;
52}
53
54static status_t HandleMIDI(
55 const char *filename, MediaScannerClient *client) {
56 // get the library configuration and do sanity check
57 const S_EAS_LIB_CONFIG* pLibConfig = EAS_Config();
58 if ((pLibConfig == NULL) || (LIB_VERSION != pLibConfig->libVersion)) {
59 LOGE("EAS library/header mismatch\n");
60 return UNKNOWN_ERROR;
61 }
62 EAS_I32 temp;
63
64 // spin up a new EAS engine
65 EAS_DATA_HANDLE easData = NULL;
66 EAS_HANDLE easHandle = NULL;
67 EAS_RESULT result = EAS_Init(&easData);
68 if (result == EAS_SUCCESS) {
69 EAS_FILE file;
70 file.path = filename;
71 file.fd = 0;
72 file.offset = 0;
73 file.length = 0;
74 result = EAS_OpenFile(easData, &file, &easHandle);
75 }
76 if (result == EAS_SUCCESS) {
77 result = EAS_Prepare(easData, easHandle);
78 }
79 if (result == EAS_SUCCESS) {
80 result = EAS_ParseMetaData(easData, easHandle, &temp);
81 }
82 if (easHandle) {
83 EAS_CloseFile(easData, easHandle);
84 }
85 if (easData) {
86 EAS_Shutdown(easData);
87 }
88
89 if (result != EAS_SUCCESS) {
90 return UNKNOWN_ERROR;
91 }
92
93 char buffer[20];
94 sprintf(buffer, "%ld", temp);
95 if (!client->addStringTag("duration", buffer)) return UNKNOWN_ERROR;
96
97 return OK;
98}
99
Andreas Hubere4a83802010-01-08 10:57:34 -0800100status_t StagefrightMediaScanner::processFile(
101 const char *path, const char *mimeType,
102 MediaScannerClient &client) {
Andreas Huber1ce98672010-03-10 10:55:35 -0800103 LOGV("processFile '%s'.", path);
104
Andreas Hubere4a83802010-01-08 10:57:34 -0800105 client.setLocale(locale());
106 client.beginFile();
107
Andreas Huberaee3c632010-01-11 15:35:19 -0800108 const char *extension = strrchr(path, '.');
109
110 if (!extension) {
111 return UNKNOWN_ERROR;
112 }
113
114 if (!FileHasAcceptableExtension(extension)) {
115 client.endFile();
116
117 return UNKNOWN_ERROR;
118 }
119
120 if (!strcasecmp(extension, ".mid")
121 || !strcasecmp(extension, ".smf")
122 || !strcasecmp(extension, ".imy")
123 || !strcasecmp(extension, ".midi")
124 || !strcasecmp(extension, ".xmf")
125 || !strcasecmp(extension, ".rtttl")
126 || !strcasecmp(extension, ".rtx")
127 || !strcasecmp(extension, ".ota")) {
Hiroshi Takekawadc1a26e2010-11-06 18:28:11 +0900128 status_t status = HandleMIDI(path, &client);
129 if (status != OK) {
130 return status;
131 }
Mike Lockwood8151dc322011-01-25 15:20:04 -0800132 } else {
133 sp<MediaMetadataRetriever> mRetriever(new MediaMetadataRetriever);
Andreas Huber1cb02bf2010-01-13 11:25:10 -0800134
Mike Lockwood8151dc322011-01-25 15:20:04 -0800135 if (mRetriever->setDataSource(path) == OK) {
Andreas Hubere4a83802010-01-08 10:57:34 -0800136 const char *value;
Mike Lockwood8151dc322011-01-25 15:20:04 -0800137 if ((value = mRetriever->extractMetadata(
138 METADATA_KEY_MIMETYPE)) != NULL) {
139 client.setMimeType(value);
140 }
141
142 struct KeyMap {
143 const char *tag;
144 int key;
145 };
146 static const KeyMap kKeyMap[] = {
147 { "tracknumber", METADATA_KEY_CD_TRACK_NUMBER },
148 { "discnumber", METADATA_KEY_DISC_NUMBER },
149 { "album", METADATA_KEY_ALBUM },
150 { "artist", METADATA_KEY_ARTIST },
151 { "albumartist", METADATA_KEY_ALBUMARTIST },
152 { "composer", METADATA_KEY_COMPOSER },
153 { "genre", METADATA_KEY_GENRE },
154 { "title", METADATA_KEY_TITLE },
155 { "year", METADATA_KEY_YEAR },
156 { "duration", METADATA_KEY_DURATION },
157 { "writer", METADATA_KEY_WRITER },
158 { "compilation", METADATA_KEY_COMPILATION },
159 };
160 static const size_t kNumEntries = sizeof(kKeyMap) / sizeof(kKeyMap[0]);
161
162 for (size_t i = 0; i < kNumEntries; ++i) {
163 const char *value;
164 if ((value = mRetriever->extractMetadata(kKeyMap[i].key)) != NULL) {
165 client.addStringTag(kKeyMap[i].tag, value);
166 }
Andreas Hubere4a83802010-01-08 10:57:34 -0800167 }
168 }
169 }
170
171 client.endFile();
172
173 return OK;
174}
175
176char *StagefrightMediaScanner::extractAlbumArt(int fd) {
Andreas Huber1ce98672010-03-10 10:55:35 -0800177 LOGV("extractAlbumArt %d", fd);
178
James Dongb1262a82010-11-16 14:04:54 -0800179 off64_t size = lseek64(fd, 0, SEEK_END);
Andreas Huberaee3c632010-01-11 15:35:19 -0800180 if (size < 0) {
181 return NULL;
182 }
James Dongb1262a82010-11-16 14:04:54 -0800183 lseek64(fd, 0, SEEK_SET);
Andreas Huberaee3c632010-01-11 15:35:19 -0800184
Mike Lockwood8151dc322011-01-25 15:20:04 -0800185 sp<MediaMetadataRetriever> mRetriever(new MediaMetadataRetriever);
James Dong11eab052011-01-06 12:20:35 -0800186 if (mRetriever->setDataSource(fd, 0, size) == OK) {
Andreas Huber1ce98672010-03-10 10:55:35 -0800187 sp<IMemory> mem = mRetriever->extractAlbumArt();
Andreas Hubere4a83802010-01-08 10:57:34 -0800188
Andreas Huber1ce98672010-03-10 10:55:35 -0800189 if (mem != NULL) {
190 MediaAlbumArt *art = static_cast<MediaAlbumArt *>(mem->pointer());
191
Andreas Huberaee3c632010-01-11 15:35:19 -0800192 char *data = (char *)malloc(art->mSize + 4);
193 *(int32_t *)data = art->mSize;
Andreas Huber1ce98672010-03-10 10:55:35 -0800194 memcpy(&data[4], &art[1], art->mSize);
Andreas Huberaee3c632010-01-11 15:35:19 -0800195
196 return data;
197 }
Andreas Hubere4a83802010-01-08 10:57:34 -0800198 }
199
200 return NULL;
201}
202
203} // namespace android