StagefrightMediaScanner.cpp revision 2e39c1ca101e01ff13b03129a87939e7982b25bf
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 29// Ogg Vorbis includes 30#include "ivorbiscodec.h" 31#include "ivorbisfile.h" 32 33namespace android { 34 35StagefrightMediaScanner::StagefrightMediaScanner() 36 : mRetriever(new MediaMetadataRetriever) { 37} 38 39StagefrightMediaScanner::~StagefrightMediaScanner() {} 40 41static bool FileHasAcceptableExtension(const char *extension) { 42 static const char *kValidExtensions[] = { 43 ".mp3", ".mp4", ".m4a", ".3gp", ".3gpp", ".3g2", ".3gpp2", 44 ".mpeg", ".ogg", ".mid", ".smf", ".imy", ".wma", ".aac", 45 ".wav", ".amr", ".midi", ".xmf", ".rtttl", ".rtx", ".ota" 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 59static status_t HandleMIDI( 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)) { 64 LOGE("EAS library/header mismatch\n"); 65 return UNKNOWN_ERROR; 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) { 95 return UNKNOWN_ERROR; 96 } 97 98 char buffer[20]; 99 sprintf(buffer, "%ld", temp); 100 if (!client->addStringTag("duration", buffer)) return UNKNOWN_ERROR; 101 102 return OK; 103} 104 105static status_t HandleOGG( 106 const char *filename, MediaScannerClient *client) { 107 int duration; 108 109 FILE *file = fopen(filename,"r"); 110 if (!file) 111 return UNKNOWN_ERROR; 112 113 OggVorbis_File vf; 114 if (ov_open(file, &vf, NULL, 0) < 0) { 115 return UNKNOWN_ERROR; 116 } 117 118 char **ptr=ov_comment(&vf,-1)->user_comments; 119 while(*ptr){ 120 char *val = strstr(*ptr, "="); 121 if (val) { 122 int keylen = val++ - *ptr; 123 char key[keylen + 1]; 124 strncpy(key, *ptr, keylen); 125 key[keylen] = 0; 126 if (!client->addStringTag(key, val)) goto failure; 127 } 128 ++ptr; 129 } 130 131 // Duration 132 duration = ov_time_total(&vf, -1); 133 if (duration > 0) { 134 char buffer[20]; 135 sprintf(buffer, "%d", duration); 136 if (!client->addStringTag("duration", buffer)) goto failure; 137 } 138 139 ov_clear(&vf); // this also closes the FILE 140 return OK; 141 142failure: 143 ov_clear(&vf); // this also closes the FILE 144 return UNKNOWN_ERROR; 145} 146 147status_t StagefrightMediaScanner::processFile( 148 const char *path, const char *mimeType, 149 MediaScannerClient &client) { 150 LOGV("processFile '%s'.", path); 151 152 client.setLocale(locale()); 153 client.beginFile(); 154 155 const char *extension = strrchr(path, '.'); 156 157 if (!extension) { 158 return UNKNOWN_ERROR; 159 } 160 161 if (!FileHasAcceptableExtension(extension)) { 162 client.endFile(); 163 164 return UNKNOWN_ERROR; 165 } 166 167 if (!strcasecmp(extension, ".mid") 168 || !strcasecmp(extension, ".smf") 169 || !strcasecmp(extension, ".imy") 170 || !strcasecmp(extension, ".midi") 171 || !strcasecmp(extension, ".xmf") 172 || !strcasecmp(extension, ".rtttl") 173 || !strcasecmp(extension, ".rtx") 174 || !strcasecmp(extension, ".ota")) { 175 return HandleMIDI(path, &client); 176 } 177 178 if (!strcasecmp(extension, ".ogg")) { 179 return HandleOGG(path, &client); 180 } 181 182 if (mRetriever->setDataSource(path) == OK 183 && mRetriever->setMode( 184 METADATA_MODE_METADATA_RETRIEVAL_ONLY) == OK) { 185 const char *value; 186 if ((value = mRetriever->extractMetadata( 187 METADATA_KEY_MIMETYPE)) != NULL) { 188 client.setMimeType(value); 189 } 190 191 struct KeyMap { 192 const char *tag; 193 int key; 194 }; 195 static const KeyMap kKeyMap[] = { 196 { "tracknumber", METADATA_KEY_CD_TRACK_NUMBER }, 197 { "discnumber", METADATA_KEY_DISC_NUMBER }, 198 { "album", METADATA_KEY_ALBUM }, 199 { "artist", METADATA_KEY_ARTIST }, 200 { "albumartist", METADATA_KEY_ALBUMARTIST }, 201 { "composer", METADATA_KEY_COMPOSER }, 202 { "genre", METADATA_KEY_GENRE }, 203 { "title", METADATA_KEY_TITLE }, 204 { "year", METADATA_KEY_YEAR }, 205 { "duration", METADATA_KEY_DURATION }, 206 { "writer", METADATA_KEY_WRITER }, 207 }; 208 static const size_t kNumEntries = sizeof(kKeyMap) / sizeof(kKeyMap[0]); 209 210 for (size_t i = 0; i < kNumEntries; ++i) { 211 const char *value; 212 if ((value = mRetriever->extractMetadata(kKeyMap[i].key)) != NULL) { 213 client.addStringTag(kKeyMap[i].tag, value); 214 } 215 } 216 } 217 218 client.endFile(); 219 220 return OK; 221} 222 223char *StagefrightMediaScanner::extractAlbumArt(int fd) { 224 LOGV("extractAlbumArt %d", fd); 225 226 off_t size = lseek(fd, 0, SEEK_END); 227 if (size < 0) { 228 return NULL; 229 } 230 lseek(fd, 0, SEEK_SET); 231 232 if (mRetriever->setDataSource(fd, 0, size) == OK 233 && mRetriever->setMode( 234 METADATA_MODE_FRAME_CAPTURE_ONLY) == OK) { 235 sp<IMemory> mem = mRetriever->extractAlbumArt(); 236 237 if (mem != NULL) { 238 MediaAlbumArt *art = static_cast<MediaAlbumArt *>(mem->pointer()); 239 240 char *data = (char *)malloc(art->mSize + 4); 241 *(int32_t *)data = art->mSize; 242 memcpy(&data[4], &art[1], art->mSize); 243 244 return data; 245 } 246 } 247 248 return NULL; 249} 250 251} // namespace android 252