Andreas Huber | 67e5a4f | 2010-01-08 10:57:34 -0800 | [diff] [blame] | 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 | |
Andreas Huber | fc9ba09 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "StagefrightMediaScanner" |
| 19 | #include <utils/Log.h> |
| 20 | |
Marco Nelissen | a28976b | 2012-04-13 13:28:12 -0700 | [diff] [blame] | 21 | #include <sys/types.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <fcntl.h> |
| 24 | |
Andreas Huber | 67e5a4f | 2010-01-08 10:57:34 -0800 | [diff] [blame] | 25 | #include <media/stagefright/StagefrightMediaScanner.h> |
| 26 | |
Andreas Huber | 2e39c1c | 2010-03-10 10:55:35 -0800 | [diff] [blame] | 27 | #include <media/mediametadataretriever.h> |
| 28 | #include <private/media/VideoFrame.h> |
Andreas Huber | 67e5a4f | 2010-01-08 10:57:34 -0800 | [diff] [blame] | 29 | |
Andreas Huber | fc9ba09 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 30 | // Sonivox includes |
| 31 | #include <libsonivox/eas.h> |
| 32 | |
Andreas Huber | 67e5a4f | 2010-01-08 10:57:34 -0800 | [diff] [blame] | 33 | namespace android { |
| 34 | |
Mike Lockwood | 0a095d0 | 2011-01-25 15:20:04 -0800 | [diff] [blame] | 35 | StagefrightMediaScanner::StagefrightMediaScanner() {} |
Andreas Huber | 67e5a4f | 2010-01-08 10:57:34 -0800 | [diff] [blame] | 36 | |
| 37 | StagefrightMediaScanner::~StagefrightMediaScanner() {} |
| 38 | |
Andreas Huber | fc9ba09 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 39 | static 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 Huber | 093437c | 2010-05-20 14:56:53 -0700 | [diff] [blame] | 43 | ".wav", ".amr", ".midi", ".xmf", ".rtttl", ".rtx", ".ota", |
Andreas Huber | 8d30cc8 | 2011-03-29 10:24:32 -0700 | [diff] [blame] | 44 | ".mkv", ".mka", ".webm", ".ts", ".fl", ".flac", ".mxmf", |
Jan Bjernler | 03163f4 | 2012-11-16 16:40:42 +0100 | [diff] [blame] | 45 | ".avi", ".mpeg", ".mpg", ".mpga" |
Andreas Huber | fc9ba09 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 46 | }; |
| 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 Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 59 | static MediaScanResult HandleMIDI( |
Andreas Huber | fc9ba09 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 60 | 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 Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 64 | ALOGE("EAS library/header mismatch\n"); |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 65 | return MEDIA_SCAN_RESULT_ERROR; |
Andreas Huber | fc9ba09 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 66 | } |
| 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 Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 95 | return MEDIA_SCAN_RESULT_SKIPPED; |
Andreas Huber | fc9ba09 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | char buffer[20]; |
| 99 | sprintf(buffer, "%ld", temp); |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 100 | status_t status = client->addStringTag("duration", buffer); |
Marco Nelissen | 153cefd | 2011-11-18 13:10:56 -0800 | [diff] [blame] | 101 | if (status != OK) { |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 102 | return MEDIA_SCAN_RESULT_ERROR; |
| 103 | } |
| 104 | return MEDIA_SCAN_RESULT_OK; |
Andreas Huber | fc9ba09 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 107 | MediaScanResult StagefrightMediaScanner::processFile( |
Andreas Huber | 67e5a4f | 2010-01-08 10:57:34 -0800 | [diff] [blame] | 108 | const char *path, const char *mimeType, |
| 109 | MediaScannerClient &client) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 110 | ALOGV("processFile '%s'.", path); |
Andreas Huber | 2e39c1c | 2010-03-10 10:55:35 -0800 | [diff] [blame] | 111 | |
Andreas Huber | 67e5a4f | 2010-01-08 10:57:34 -0800 | [diff] [blame] | 112 | client.setLocale(locale()); |
| 113 | client.beginFile(); |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 114 | MediaScanResult result = processFileInternal(path, mimeType, client); |
| 115 | client.endFile(); |
| 116 | return result; |
| 117 | } |
Andreas Huber | 67e5a4f | 2010-01-08 10:57:34 -0800 | [diff] [blame] | 118 | |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 119 | MediaScanResult StagefrightMediaScanner::processFileInternal( |
| 120 | const char *path, const char *mimeType, |
| 121 | MediaScannerClient &client) { |
Andreas Huber | fc9ba09 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 122 | const char *extension = strrchr(path, '.'); |
| 123 | |
| 124 | if (!extension) { |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 125 | return MEDIA_SCAN_RESULT_SKIPPED; |
Andreas Huber | fc9ba09 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | if (!FileHasAcceptableExtension(extension)) { |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 129 | return MEDIA_SCAN_RESULT_SKIPPED; |
Andreas Huber | fc9ba09 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 130 | } |
| 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 Huber | 8d30cc8 | 2011-03-29 10:24:32 -0700 | [diff] [blame] | 139 | || !strcasecmp(extension, ".ota") |
| 140 | || !strcasecmp(extension, ".mxmf")) { |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 141 | return HandleMIDI(path, &client); |
| 142 | } |
| 143 | |
| 144 | sp<MediaMetadataRetriever> mRetriever(new MediaMetadataRetriever); |
| 145 | |
Marco Nelissen | a28976b | 2012-04-13 13:28:12 -0700 | [diff] [blame] | 146 | 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 Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 156 | 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 Takekawa | 6ed70d2 | 2010-11-06 18:28:11 +0900 | [diff] [blame] | 166 | } |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 167 | } |
Andreas Huber | 7be6407 | 2010-01-13 11:25:10 -0800 | [diff] [blame] | 168 | |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 169 | 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 Nelissen | 4762a51 | 2012-04-04 14:08:01 -0700 | [diff] [blame] | 187 | { "width", METADATA_KEY_VIDEO_WIDTH }, |
| 188 | { "height", METADATA_KEY_VIDEO_HEIGHT }, |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 189 | }; |
| 190 | static const size_t kNumEntries = sizeof(kKeyMap) / sizeof(kKeyMap[0]); |
Mike Lockwood | 0a095d0 | 2011-01-25 15:20:04 -0800 | [diff] [blame] | 191 | |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 192 | 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 Nelissen | 153cefd | 2011-11-18 13:10:56 -0800 | [diff] [blame] | 196 | if (status != OK) { |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 197 | return MEDIA_SCAN_RESULT_ERROR; |
Andreas Huber | 67e5a4f | 2010-01-08 10:57:34 -0800 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 202 | return MEDIA_SCAN_RESULT_OK; |
Andreas Huber | 67e5a4f | 2010-01-08 10:57:34 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | char *StagefrightMediaScanner::extractAlbumArt(int fd) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 206 | ALOGV("extractAlbumArt %d", fd); |
Andreas Huber | 2e39c1c | 2010-03-10 10:55:35 -0800 | [diff] [blame] | 207 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 208 | off64_t size = lseek64(fd, 0, SEEK_END); |
Andreas Huber | fc9ba09 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 209 | if (size < 0) { |
| 210 | return NULL; |
| 211 | } |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 212 | lseek64(fd, 0, SEEK_SET); |
Andreas Huber | fc9ba09 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 213 | |
Mike Lockwood | 0a095d0 | 2011-01-25 15:20:04 -0800 | [diff] [blame] | 214 | sp<MediaMetadataRetriever> mRetriever(new MediaMetadataRetriever); |
James Dong | 7f7d52a | 2011-01-06 12:20:35 -0800 | [diff] [blame] | 215 | if (mRetriever->setDataSource(fd, 0, size) == OK) { |
Andreas Huber | 2e39c1c | 2010-03-10 10:55:35 -0800 | [diff] [blame] | 216 | sp<IMemory> mem = mRetriever->extractAlbumArt(); |
Andreas Huber | 67e5a4f | 2010-01-08 10:57:34 -0800 | [diff] [blame] | 217 | |
Andreas Huber | 2e39c1c | 2010-03-10 10:55:35 -0800 | [diff] [blame] | 218 | if (mem != NULL) { |
| 219 | MediaAlbumArt *art = static_cast<MediaAlbumArt *>(mem->pointer()); |
| 220 | |
Andreas Huber | fc9ba09 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 221 | char *data = (char *)malloc(art->mSize + 4); |
| 222 | *(int32_t *)data = art->mSize; |
Andreas Huber | 2e39c1c | 2010-03-10 10:55:35 -0800 | [diff] [blame] | 223 | memcpy(&data[4], &art[1], art->mSize); |
Andreas Huber | fc9ba09 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 224 | |
| 225 | return data; |
| 226 | } |
Andreas Huber | 67e5a4f | 2010-01-08 10:57:34 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | return NULL; |
| 230 | } |
| 231 | |
| 232 | } // namespace android |