The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright (C) 2008 The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | #define LOG_TAG "MetadataRetrieverClient" |
| 20 | #include <utils/Log.h> |
| 21 | |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/stat.h> |
Dave Sparks | 7c0d20a | 2009-10-26 16:28:26 -0700 | [diff] [blame] | 24 | #include <sys/resource.h> |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 25 | #include <dirent.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #include <string.h> |
| 29 | #include <cutils/atomic.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 30 | #include <binder/MemoryDealer.h> |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 31 | #include <android_runtime/ActivityManager.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 32 | #include <binder/IPCThreadState.h> |
| 33 | #include <binder/IServiceManager.h> |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 34 | #include <media/MediaMetadataRetrieverInterface.h> |
| 35 | #include <media/MediaPlayerInterface.h> |
| 36 | #include <media/PVMetadataRetriever.h> |
| 37 | #include <private/media/VideoFrame.h> |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 38 | #include "VorbisMetadataRetriever.h" |
| 39 | #include "MidiMetadataRetriever.h" |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 40 | #include "MetadataRetrieverClient.h" |
| 41 | |
Dave Sparks | 7c0d20a | 2009-10-26 16:28:26 -0700 | [diff] [blame] | 42 | /* desktop Linux needs a little help with gettid() */ |
| 43 | #if defined(HAVE_GETTID) && !defined(HAVE_ANDROID_OS) |
| 44 | #define __KERNEL__ |
| 45 | # include <linux/unistd.h> |
| 46 | #ifdef _syscall0 |
| 47 | _syscall0(pid_t,gettid) |
| 48 | #else |
| 49 | pid_t gettid() { return syscall(__NR_gettid);} |
| 50 | #endif |
| 51 | #undef __KERNEL__ |
| 52 | #endif |
| 53 | |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 54 | namespace android { |
| 55 | |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 56 | extern player_type getPlayerType(const char* url); |
| 57 | extern player_type getPlayerType(int fd, int64_t offset, int64_t length); |
| 58 | |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 59 | MetadataRetrieverClient::MetadataRetrieverClient(pid_t pid) |
| 60 | { |
| 61 | LOGV("MetadataRetrieverClient constructor pid(%d)", pid); |
| 62 | mPid = pid; |
| 63 | mThumbnailDealer = NULL; |
| 64 | mAlbumArtDealer = NULL; |
| 65 | mThumbnail = NULL; |
| 66 | mAlbumArt = NULL; |
Jean-Baptiste Queru | 680f8c7 | 2009-03-21 11:40:18 -0700 | [diff] [blame] | 67 | mRetriever = NULL; |
James Dong | 67fc234 | 2009-09-29 10:45:27 -0700 | [diff] [blame] | 68 | mMode = METADATA_MODE_FRAME_CAPTURE_AND_METADATA_RETRIEVAL; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | MetadataRetrieverClient::~MetadataRetrieverClient() |
| 72 | { |
| 73 | LOGV("MetadataRetrieverClient destructor"); |
| 74 | disconnect(); |
| 75 | } |
| 76 | |
| 77 | status_t MetadataRetrieverClient::dump(int fd, const Vector<String16>& args) const |
| 78 | { |
| 79 | const size_t SIZE = 256; |
| 80 | char buffer[SIZE]; |
| 81 | String8 result; |
| 82 | result.append(" MetadataRetrieverClient\n"); |
James Dong | 67fc234 | 2009-09-29 10:45:27 -0700 | [diff] [blame] | 83 | snprintf(buffer, 255, " pid(%d) mode(%d)\n", mPid, mMode); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 84 | result.append(buffer); |
| 85 | write(fd, result.string(), result.size()); |
| 86 | write(fd, "\n", 1); |
| 87 | return NO_ERROR; |
| 88 | } |
| 89 | |
| 90 | void MetadataRetrieverClient::disconnect() |
| 91 | { |
| 92 | LOGV("disconnect from pid %d", mPid); |
| 93 | Mutex::Autolock lock(mLock); |
| 94 | mRetriever.clear(); |
| 95 | mThumbnailDealer.clear(); |
| 96 | mAlbumArtDealer.clear(); |
| 97 | mThumbnail.clear(); |
| 98 | mAlbumArt.clear(); |
James Dong | 67fc234 | 2009-09-29 10:45:27 -0700 | [diff] [blame] | 99 | mMode = METADATA_MODE_FRAME_CAPTURE_AND_METADATA_RETRIEVAL; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 100 | IPCThreadState::self()->flushCommands(); |
| 101 | } |
| 102 | |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 103 | static sp<MediaMetadataRetrieverBase> createRetriever(player_type playerType) |
| 104 | { |
| 105 | sp<MediaMetadataRetrieverBase> p; |
| 106 | switch (playerType) { |
| 107 | #ifndef NO_OPENCORE |
| 108 | case PV_PLAYER: |
| 109 | LOGV("create pv metadata retriever"); |
| 110 | p = new PVMetadataRetriever(); |
| 111 | break; |
| 112 | #endif |
| 113 | case VORBIS_PLAYER: |
| 114 | LOGV("create vorbis metadata retriever"); |
| 115 | p = new VorbisMetadataRetriever(); |
| 116 | break; |
| 117 | case SONIVOX_PLAYER: |
| 118 | LOGV("create midi metadata retriever"); |
| 119 | p = new MidiMetadataRetriever(); |
| 120 | break; |
| 121 | default: |
| 122 | // TODO: |
| 123 | // support for STAGEFRIGHT_PLAYER and TEST_PLAYER |
| 124 | LOGE("player type %d is not supported", playerType); |
| 125 | break; |
| 126 | } |
| 127 | if (p == NULL) { |
| 128 | LOGE("failed to create a retriever object"); |
| 129 | } |
| 130 | return p; |
| 131 | } |
| 132 | |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 133 | status_t MetadataRetrieverClient::setDataSource(const char *url) |
| 134 | { |
| 135 | LOGV("setDataSource(%s)", url); |
| 136 | Mutex::Autolock lock(mLock); |
| 137 | if (url == NULL) { |
| 138 | return UNKNOWN_ERROR; |
| 139 | } |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 140 | player_type playerType = getPlayerType(url); |
Andreas Huber | dcaa220 | 2009-11-05 12:04:22 -0800 | [diff] [blame] | 141 | #if !defined(NO_OPENCORE) && defined(BUILD_WITH_FULL_STAGEFRIGHT) |
| 142 | if (playerType == STAGEFRIGHT_PLAYER) { |
| 143 | // Stagefright doesn't support metadata in this branch yet. |
| 144 | playerType = PV_PLAYER; |
| 145 | } |
| 146 | #endif |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 147 | LOGV("player type = %d", playerType); |
| 148 | sp<MediaMetadataRetrieverBase> p = createRetriever(playerType); |
| 149 | if (p == NULL) return NO_INIT; |
James Dong | 67fc234 | 2009-09-29 10:45:27 -0700 | [diff] [blame] | 150 | status_t ret = p->setMode(mMode); |
| 151 | if (ret == NO_ERROR) { |
| 152 | ret = p->setDataSource(url); |
| 153 | } |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 154 | if (ret == NO_ERROR) mRetriever = p; |
| 155 | return ret; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | status_t MetadataRetrieverClient::setDataSource(int fd, int64_t offset, int64_t length) |
| 159 | { |
| 160 | LOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length); |
| 161 | Mutex::Autolock lock(mLock); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 162 | struct stat sb; |
| 163 | int ret = fstat(fd, &sb); |
| 164 | if (ret != 0) { |
| 165 | LOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno)); |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 166 | return BAD_VALUE; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 167 | } |
| 168 | LOGV("st_dev = %llu", sb.st_dev); |
| 169 | LOGV("st_mode = %u", sb.st_mode); |
| 170 | LOGV("st_uid = %lu", sb.st_uid); |
| 171 | LOGV("st_gid = %lu", sb.st_gid); |
| 172 | LOGV("st_size = %llu", sb.st_size); |
| 173 | |
| 174 | if (offset >= sb.st_size) { |
| 175 | LOGE("offset (%lld) bigger than file size (%llu)", offset, sb.st_size); |
| 176 | ::close(fd); |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 177 | return BAD_VALUE; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 178 | } |
| 179 | if (offset + length > sb.st_size) { |
| 180 | length = sb.st_size - offset; |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 181 | LOGV("calculated length = %lld", length); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 182 | } |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 183 | |
| 184 | player_type playerType = getPlayerType(fd, offset, length); |
Andreas Huber | dcaa220 | 2009-11-05 12:04:22 -0800 | [diff] [blame] | 185 | #if !defined(NO_OPENCORE) && defined(BUILD_WITH_FULL_STAGEFRIGHT) |
| 186 | if (playerType == STAGEFRIGHT_PLAYER) { |
| 187 | // Stagefright doesn't support metadata in this branch yet. |
| 188 | playerType = PV_PLAYER; |
| 189 | } |
| 190 | #endif |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 191 | LOGV("player type = %d", playerType); |
| 192 | sp<MediaMetadataRetrieverBase> p = createRetriever(playerType); |
| 193 | if (p == NULL) { |
| 194 | ::close(fd); |
| 195 | return NO_INIT; |
| 196 | } |
James Dong | 67fc234 | 2009-09-29 10:45:27 -0700 | [diff] [blame] | 197 | status_t status = p->setMode(mMode); |
| 198 | if (status == NO_ERROR) { |
| 199 | p->setDataSource(fd, offset, length); |
| 200 | } |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 201 | if (status == NO_ERROR) mRetriever = p; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 202 | ::close(fd); |
| 203 | return status; |
| 204 | } |
| 205 | |
| 206 | status_t MetadataRetrieverClient::setMode(int mode) |
| 207 | { |
| 208 | LOGV("setMode"); |
| 209 | Mutex::Autolock lock(mLock); |
James Dong | 67fc234 | 2009-09-29 10:45:27 -0700 | [diff] [blame] | 210 | if (mode < METADATA_MODE_NOOP || |
| 211 | mode > METADATA_MODE_FRAME_CAPTURE_AND_METADATA_RETRIEVAL) { |
| 212 | LOGE("invalid mode %d", mode); |
| 213 | return BAD_VALUE; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 214 | } |
James Dong | 67fc234 | 2009-09-29 10:45:27 -0700 | [diff] [blame] | 215 | mMode = mode; |
| 216 | return NO_ERROR; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | status_t MetadataRetrieverClient::getMode(int* mode) const |
| 220 | { |
| 221 | LOGV("getMode"); |
| 222 | Mutex::Autolock lock(mLock); |
James Dong | 67fc234 | 2009-09-29 10:45:27 -0700 | [diff] [blame] | 223 | |
| 224 | // TODO: |
| 225 | // This may not be necessary. |
| 226 | // If setDataSource() has not been called, return the cached value |
| 227 | // otherwise, return the value retrieved from the retriever |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 228 | if (mRetriever == NULL) { |
James Dong | 67fc234 | 2009-09-29 10:45:27 -0700 | [diff] [blame] | 229 | *mode = mMode; |
| 230 | } else { |
| 231 | mRetriever->getMode(mode); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 232 | } |
James Dong | 67fc234 | 2009-09-29 10:45:27 -0700 | [diff] [blame] | 233 | return NO_ERROR; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | sp<IMemory> MetadataRetrieverClient::captureFrame() |
| 237 | { |
| 238 | LOGV("captureFrame"); |
| 239 | Mutex::Autolock lock(mLock); |
Dave Sparks | 7c0d20a | 2009-10-26 16:28:26 -0700 | [diff] [blame] | 240 | Priority priority(ANDROID_PRIORITY_BACKGROUND); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 241 | mThumbnail.clear(); |
| 242 | mThumbnailDealer.clear(); |
| 243 | if (mRetriever == NULL) { |
| 244 | LOGE("retriever is not initialized"); |
| 245 | return NULL; |
| 246 | } |
| 247 | VideoFrame *frame = mRetriever->captureFrame(); |
| 248 | if (frame == NULL) { |
| 249 | LOGE("failed to capture a video frame"); |
| 250 | return NULL; |
| 251 | } |
| 252 | size_t size = sizeof(VideoFrame) + frame->mSize; |
| 253 | mThumbnailDealer = new MemoryDealer(size); |
| 254 | if (mThumbnailDealer == NULL) { |
| 255 | LOGE("failed to create MemoryDealer"); |
| 256 | delete frame; |
| 257 | return NULL; |
| 258 | } |
| 259 | mThumbnail = mThumbnailDealer->allocate(size); |
| 260 | if (mThumbnail == NULL) { |
| 261 | LOGE("not enough memory for VideoFrame size=%u", size); |
| 262 | mThumbnailDealer.clear(); |
| 263 | delete frame; |
| 264 | return NULL; |
| 265 | } |
| 266 | VideoFrame *frameCopy = static_cast<VideoFrame *>(mThumbnail->pointer()); |
| 267 | frameCopy->mWidth = frame->mWidth; |
| 268 | frameCopy->mHeight = frame->mHeight; |
| 269 | frameCopy->mDisplayWidth = frame->mDisplayWidth; |
| 270 | frameCopy->mDisplayHeight = frame->mDisplayHeight; |
| 271 | frameCopy->mSize = frame->mSize; |
| 272 | frameCopy->mData = (uint8_t *)frameCopy + sizeof(VideoFrame); |
| 273 | memcpy(frameCopy->mData, frame->mData, frame->mSize); |
| 274 | delete frame; // Fix memory leakage |
| 275 | return mThumbnail; |
| 276 | } |
| 277 | |
| 278 | sp<IMemory> MetadataRetrieverClient::extractAlbumArt() |
| 279 | { |
| 280 | LOGV("extractAlbumArt"); |
| 281 | Mutex::Autolock lock(mLock); |
Dave Sparks | 7c0d20a | 2009-10-26 16:28:26 -0700 | [diff] [blame] | 282 | Priority priority(ANDROID_PRIORITY_BACKGROUND); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 283 | mAlbumArt.clear(); |
| 284 | mAlbumArtDealer.clear(); |
| 285 | if (mRetriever == NULL) { |
| 286 | LOGE("retriever is not initialized"); |
| 287 | return NULL; |
| 288 | } |
| 289 | MediaAlbumArt *albumArt = mRetriever->extractAlbumArt(); |
| 290 | if (albumArt == NULL) { |
| 291 | LOGE("failed to extract an album art"); |
| 292 | return NULL; |
| 293 | } |
| 294 | size_t size = sizeof(MediaAlbumArt) + albumArt->mSize; |
| 295 | mAlbumArtDealer = new MemoryDealer(size); |
| 296 | if (mAlbumArtDealer == NULL) { |
| 297 | LOGE("failed to create MemoryDealer object"); |
| 298 | delete albumArt; |
| 299 | return NULL; |
| 300 | } |
| 301 | mAlbumArt = mAlbumArtDealer->allocate(size); |
| 302 | if (mAlbumArt == NULL) { |
| 303 | LOGE("not enough memory for MediaAlbumArt size=%u", size); |
| 304 | mAlbumArtDealer.clear(); |
| 305 | delete albumArt; |
| 306 | return NULL; |
| 307 | } |
| 308 | MediaAlbumArt *albumArtCopy = static_cast<MediaAlbumArt *>(mAlbumArt->pointer()); |
| 309 | albumArtCopy->mSize = albumArt->mSize; |
| 310 | albumArtCopy->mData = (uint8_t *)albumArtCopy + sizeof(MediaAlbumArt); |
| 311 | memcpy(albumArtCopy->mData, albumArt->mData, albumArt->mSize); |
| 312 | delete albumArt; // Fix memory leakage |
| 313 | return mAlbumArt; |
| 314 | } |
| 315 | |
| 316 | const char* MetadataRetrieverClient::extractMetadata(int keyCode) |
| 317 | { |
| 318 | LOGV("extractMetadata"); |
| 319 | Mutex::Autolock lock(mLock); |
| 320 | if (mRetriever == NULL) { |
| 321 | LOGE("retriever is not initialized"); |
| 322 | return NULL; |
| 323 | } |
Dave Sparks | 7c0d20a | 2009-10-26 16:28:26 -0700 | [diff] [blame] | 324 | Priority priority(ANDROID_PRIORITY_BACKGROUND); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 325 | return mRetriever->extractMetadata(keyCode); |
| 326 | } |
| 327 | |
Dave Sparks | 7c0d20a | 2009-10-26 16:28:26 -0700 | [diff] [blame] | 328 | MetadataRetrieverClient::Priority::Priority(int newPriority) |
| 329 | { |
| 330 | mOldPriority = getpriority(PRIO_PROCESS, 0); |
| 331 | setpriority(PRIO_PROCESS, 0, newPriority); |
| 332 | } |
| 333 | |
| 334 | MetadataRetrieverClient::Priority::~Priority() |
| 335 | { |
| 336 | setpriority(PRIO_PROCESS, 0, mOldPriority); |
| 337 | } |
| 338 | |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 339 | }; // namespace android |