blob: af8186c79a9e4928a36a41a712be46cba089c341 [file] [log] [blame]
Andreas Huber67e5a4f2010-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 Huberfc9ba092010-01-11 15:35:19 -080017//#define LOG_NDEBUG 0
18#define LOG_TAG "StagefrightMediaScanner"
19#include <utils/Log.h>
20
Marco Nelissena28976b2012-04-13 13:28:12 -070021#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24
Andreas Huber67e5a4f2010-01-08 10:57:34 -080025#include <media/stagefright/StagefrightMediaScanner.h>
26
Andreas Huber2e39c1c2010-03-10 10:55:35 -080027#include <media/mediametadataretriever.h>
28#include <private/media/VideoFrame.h>
Andreas Huber67e5a4f2010-01-08 10:57:34 -080029
Andreas Huberfc9ba092010-01-11 15:35:19 -080030// Sonivox includes
31#include <libsonivox/eas.h>
32
Andreas Huber67e5a4f2010-01-08 10:57:34 -080033namespace android {
34
Mike Lockwood0a095d02011-01-25 15:20:04 -080035StagefrightMediaScanner::StagefrightMediaScanner() {}
Andreas Huber67e5a4f2010-01-08 10:57:34 -080036
37StagefrightMediaScanner::~StagefrightMediaScanner() {}
38
Andreas Huberfc9ba092010-01-11 15:35:19 -080039static bool FileHasAcceptableExtension(const char *extension) {
40 static const char *kValidExtensions[] = {
41 ".mp3", ".mp4", ".m4a", ".3gp", ".3gpp", ".3g2", ".3gpp2",
42 ".mpeg", ".ogg", ".mid", ".smf", ".imy", ".wma", ".aac",
Andreas Huber093437c2010-05-20 14:56:53 -070043 ".wav", ".amr", ".midi", ".xmf", ".rtttl", ".rtx", ".ota",
Andreas Huber8d30cc82011-03-29 10:24:32 -070044 ".mkv", ".mka", ".webm", ".ts", ".fl", ".flac", ".mxmf",
Marco Nelissendb182fc2012-11-16 11:30:43 -080045 ".avi", ".mpeg", ".mpg", ".awb", ".mpga"
Andreas Huberfc9ba092010-01-11 15:35:19 -080046 };
47 static const size_t kNumValidExtensions =
48 sizeof(kValidExtensions) / sizeof(kValidExtensions[0]);
49
50 for (size_t i = 0; i < kNumValidExtensions; ++i) {
51 if (!strcasecmp(extension, kValidExtensions[i])) {
52 return true;
53 }
54 }
55
56 return false;
57}
58
Jeff Brown7188e552011-07-20 16:38:43 -070059static MediaScanResult HandleMIDI(
Andreas Huberfc9ba092010-01-11 15:35:19 -080060 const char *filename, MediaScannerClient *client) {
61 // get the library configuration and do sanity check
62 const S_EAS_LIB_CONFIG* pLibConfig = EAS_Config();
63 if ((pLibConfig == NULL) || (LIB_VERSION != pLibConfig->libVersion)) {
Steve Block29357bc2012-01-06 19:20:56 +000064 ALOGE("EAS library/header mismatch\n");
Jeff Brown7188e552011-07-20 16:38:43 -070065 return MEDIA_SCAN_RESULT_ERROR;
Andreas Huberfc9ba092010-01-11 15:35:19 -080066 }
67 EAS_I32 temp;
68
69 // spin up a new EAS engine
70 EAS_DATA_HANDLE easData = NULL;
71 EAS_HANDLE easHandle = NULL;
72 EAS_RESULT result = EAS_Init(&easData);
73 if (result == EAS_SUCCESS) {
74 EAS_FILE file;
75 file.path = filename;
76 file.fd = 0;
77 file.offset = 0;
78 file.length = 0;
79 result = EAS_OpenFile(easData, &file, &easHandle);
80 }
81 if (result == EAS_SUCCESS) {
82 result = EAS_Prepare(easData, easHandle);
83 }
84 if (result == EAS_SUCCESS) {
85 result = EAS_ParseMetaData(easData, easHandle, &temp);
86 }
87 if (easHandle) {
88 EAS_CloseFile(easData, easHandle);
89 }
90 if (easData) {
91 EAS_Shutdown(easData);
92 }
93
94 if (result != EAS_SUCCESS) {
Jeff Brown7188e552011-07-20 16:38:43 -070095 return MEDIA_SCAN_RESULT_SKIPPED;
Andreas Huberfc9ba092010-01-11 15:35:19 -080096 }
97
98 char buffer[20];
99 sprintf(buffer, "%ld", temp);
Jeff Brown7188e552011-07-20 16:38:43 -0700100 status_t status = client->addStringTag("duration", buffer);
Marco Nelissen153cefd2011-11-18 13:10:56 -0800101 if (status != OK) {
Jeff Brown7188e552011-07-20 16:38:43 -0700102 return MEDIA_SCAN_RESULT_ERROR;
103 }
104 return MEDIA_SCAN_RESULT_OK;
Andreas Huberfc9ba092010-01-11 15:35:19 -0800105}
106
Jeff Brown7188e552011-07-20 16:38:43 -0700107MediaScanResult StagefrightMediaScanner::processFile(
Andreas Huber67e5a4f2010-01-08 10:57:34 -0800108 const char *path, const char *mimeType,
109 MediaScannerClient &client) {
Steve Block3856b092011-10-20 11:56:00 +0100110 ALOGV("processFile '%s'.", path);
Andreas Huber2e39c1c2010-03-10 10:55:35 -0800111
Andreas Huber67e5a4f2010-01-08 10:57:34 -0800112 client.setLocale(locale());
113 client.beginFile();
Jeff Brown7188e552011-07-20 16:38:43 -0700114 MediaScanResult result = processFileInternal(path, mimeType, client);
115 client.endFile();
116 return result;
117}
Andreas Huber67e5a4f2010-01-08 10:57:34 -0800118
Jeff Brown7188e552011-07-20 16:38:43 -0700119MediaScanResult StagefrightMediaScanner::processFileInternal(
120 const char *path, const char *mimeType,
121 MediaScannerClient &client) {
Andreas Huberfc9ba092010-01-11 15:35:19 -0800122 const char *extension = strrchr(path, '.');
123
124 if (!extension) {
Jeff Brown7188e552011-07-20 16:38:43 -0700125 return MEDIA_SCAN_RESULT_SKIPPED;
Andreas Huberfc9ba092010-01-11 15:35:19 -0800126 }
127
128 if (!FileHasAcceptableExtension(extension)) {
Jeff Brown7188e552011-07-20 16:38:43 -0700129 return MEDIA_SCAN_RESULT_SKIPPED;
Andreas Huberfc9ba092010-01-11 15:35:19 -0800130 }
131
132 if (!strcasecmp(extension, ".mid")
133 || !strcasecmp(extension, ".smf")
134 || !strcasecmp(extension, ".imy")
135 || !strcasecmp(extension, ".midi")
136 || !strcasecmp(extension, ".xmf")
137 || !strcasecmp(extension, ".rtttl")
138 || !strcasecmp(extension, ".rtx")
Andreas Huber8d30cc82011-03-29 10:24:32 -0700139 || !strcasecmp(extension, ".ota")
140 || !strcasecmp(extension, ".mxmf")) {
Jeff Brown7188e552011-07-20 16:38:43 -0700141 return HandleMIDI(path, &client);
142 }
143
144 sp<MediaMetadataRetriever> mRetriever(new MediaMetadataRetriever);
145
Marco Nelissena28976b2012-04-13 13:28:12 -0700146 int fd = open(path, O_RDONLY | O_LARGEFILE);
147 status_t status;
148 if (fd < 0) {
149 // couldn't open it locally, maybe the media server can?
150 status = mRetriever->setDataSource(path);
151 } else {
152 status = mRetriever->setDataSource(fd, 0, 0x7ffffffffffffffL);
153 close(fd);
154 }
155
Jeff Brown7188e552011-07-20 16:38:43 -0700156 if (status) {
157 return MEDIA_SCAN_RESULT_ERROR;
158 }
159
160 const char *value;
161 if ((value = mRetriever->extractMetadata(
162 METADATA_KEY_MIMETYPE)) != NULL) {
163 status = client.setMimeType(value);
164 if (status) {
165 return MEDIA_SCAN_RESULT_ERROR;
Hiroshi Takekawa6ed70d22010-11-06 18:28:11 +0900166 }
Jeff Brown7188e552011-07-20 16:38:43 -0700167 }
Andreas Huber7be64072010-01-13 11:25:10 -0800168
Jeff Brown7188e552011-07-20 16:38:43 -0700169 struct KeyMap {
170 const char *tag;
171 int key;
172 };
173 static const KeyMap kKeyMap[] = {
174 { "tracknumber", METADATA_KEY_CD_TRACK_NUMBER },
175 { "discnumber", METADATA_KEY_DISC_NUMBER },
176 { "album", METADATA_KEY_ALBUM },
177 { "artist", METADATA_KEY_ARTIST },
178 { "albumartist", METADATA_KEY_ALBUMARTIST },
179 { "composer", METADATA_KEY_COMPOSER },
180 { "genre", METADATA_KEY_GENRE },
181 { "title", METADATA_KEY_TITLE },
182 { "year", METADATA_KEY_YEAR },
183 { "duration", METADATA_KEY_DURATION },
184 { "writer", METADATA_KEY_WRITER },
185 { "compilation", METADATA_KEY_COMPILATION },
186 { "isdrm", METADATA_KEY_IS_DRM },
Marco Nelissen4762a512012-04-04 14:08:01 -0700187 { "width", METADATA_KEY_VIDEO_WIDTH },
188 { "height", METADATA_KEY_VIDEO_HEIGHT },
Jeff Brown7188e552011-07-20 16:38:43 -0700189 };
190 static const size_t kNumEntries = sizeof(kKeyMap) / sizeof(kKeyMap[0]);
Mike Lockwood0a095d02011-01-25 15:20:04 -0800191
Jeff Brown7188e552011-07-20 16:38:43 -0700192 for (size_t i = 0; i < kNumEntries; ++i) {
193 const char *value;
194 if ((value = mRetriever->extractMetadata(kKeyMap[i].key)) != NULL) {
195 status = client.addStringTag(kKeyMap[i].tag, value);
Marco Nelissen153cefd2011-11-18 13:10:56 -0800196 if (status != OK) {
Jeff Brown7188e552011-07-20 16:38:43 -0700197 return MEDIA_SCAN_RESULT_ERROR;
Andreas Huber67e5a4f2010-01-08 10:57:34 -0800198 }
199 }
200 }
201
Jeff Brown7188e552011-07-20 16:38:43 -0700202 return MEDIA_SCAN_RESULT_OK;
Andreas Huber67e5a4f2010-01-08 10:57:34 -0800203}
204
205char *StagefrightMediaScanner::extractAlbumArt(int fd) {
Steve Block3856b092011-10-20 11:56:00 +0100206 ALOGV("extractAlbumArt %d", fd);
Andreas Huber2e39c1c2010-03-10 10:55:35 -0800207
James Dongc7fc37a2010-11-16 14:04:54 -0800208 off64_t size = lseek64(fd, 0, SEEK_END);
Andreas Huberfc9ba092010-01-11 15:35:19 -0800209 if (size < 0) {
210 return NULL;
211 }
James Dongc7fc37a2010-11-16 14:04:54 -0800212 lseek64(fd, 0, SEEK_SET);
Andreas Huberfc9ba092010-01-11 15:35:19 -0800213
Mike Lockwood0a095d02011-01-25 15:20:04 -0800214 sp<MediaMetadataRetriever> mRetriever(new MediaMetadataRetriever);
James Dong7f7d52a2011-01-06 12:20:35 -0800215 if (mRetriever->setDataSource(fd, 0, size) == OK) {
Andreas Huber2e39c1c2010-03-10 10:55:35 -0800216 sp<IMemory> mem = mRetriever->extractAlbumArt();
Andreas Huber67e5a4f2010-01-08 10:57:34 -0800217
Andreas Huber2e39c1c2010-03-10 10:55:35 -0800218 if (mem != NULL) {
219 MediaAlbumArt *art = static_cast<MediaAlbumArt *>(mem->pointer());
220
Andreas Huberfc9ba092010-01-11 15:35:19 -0800221 char *data = (char *)malloc(art->mSize + 4);
222 *(int32_t *)data = art->mSize;
Andreas Huber2e39c1c2010-03-10 10:55:35 -0800223 memcpy(&data[4], &art[1], art->mSize);
Andreas Huberfc9ba092010-01-11 15:35:19 -0800224
225 return data;
226 }
Andreas Huber67e5a4f2010-01-08 10:57:34 -0800227 }
228
229 return NULL;
230}
231
232} // namespace android