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); |
| 240 | mThumbnail.clear(); |
| 241 | mThumbnailDealer.clear(); |
| 242 | if (mRetriever == NULL) { |
| 243 | LOGE("retriever is not initialized"); |
| 244 | return NULL; |
| 245 | } |
| 246 | VideoFrame *frame = mRetriever->captureFrame(); |
| 247 | if (frame == NULL) { |
| 248 | LOGE("failed to capture a video frame"); |
| 249 | return NULL; |
| 250 | } |
| 251 | size_t size = sizeof(VideoFrame) + frame->mSize; |
| 252 | mThumbnailDealer = new MemoryDealer(size); |
| 253 | if (mThumbnailDealer == NULL) { |
| 254 | LOGE("failed to create MemoryDealer"); |
| 255 | delete frame; |
| 256 | return NULL; |
| 257 | } |
| 258 | mThumbnail = mThumbnailDealer->allocate(size); |
| 259 | if (mThumbnail == NULL) { |
| 260 | LOGE("not enough memory for VideoFrame size=%u", size); |
| 261 | mThumbnailDealer.clear(); |
| 262 | delete frame; |
| 263 | return NULL; |
| 264 | } |
| 265 | VideoFrame *frameCopy = static_cast<VideoFrame *>(mThumbnail->pointer()); |
| 266 | frameCopy->mWidth = frame->mWidth; |
| 267 | frameCopy->mHeight = frame->mHeight; |
| 268 | frameCopy->mDisplayWidth = frame->mDisplayWidth; |
| 269 | frameCopy->mDisplayHeight = frame->mDisplayHeight; |
| 270 | frameCopy->mSize = frame->mSize; |
| 271 | frameCopy->mData = (uint8_t *)frameCopy + sizeof(VideoFrame); |
| 272 | memcpy(frameCopy->mData, frame->mData, frame->mSize); |
| 273 | delete frame; // Fix memory leakage |
| 274 | return mThumbnail; |
| 275 | } |
| 276 | |
| 277 | sp<IMemory> MetadataRetrieverClient::extractAlbumArt() |
| 278 | { |
| 279 | LOGV("extractAlbumArt"); |
| 280 | Mutex::Autolock lock(mLock); |
| 281 | mAlbumArt.clear(); |
| 282 | mAlbumArtDealer.clear(); |
| 283 | if (mRetriever == NULL) { |
| 284 | LOGE("retriever is not initialized"); |
| 285 | return NULL; |
| 286 | } |
| 287 | MediaAlbumArt *albumArt = mRetriever->extractAlbumArt(); |
| 288 | if (albumArt == NULL) { |
| 289 | LOGE("failed to extract an album art"); |
| 290 | return NULL; |
| 291 | } |
| 292 | size_t size = sizeof(MediaAlbumArt) + albumArt->mSize; |
| 293 | mAlbumArtDealer = new MemoryDealer(size); |
| 294 | if (mAlbumArtDealer == NULL) { |
| 295 | LOGE("failed to create MemoryDealer object"); |
| 296 | delete albumArt; |
| 297 | return NULL; |
| 298 | } |
| 299 | mAlbumArt = mAlbumArtDealer->allocate(size); |
| 300 | if (mAlbumArt == NULL) { |
| 301 | LOGE("not enough memory for MediaAlbumArt size=%u", size); |
| 302 | mAlbumArtDealer.clear(); |
| 303 | delete albumArt; |
| 304 | return NULL; |
| 305 | } |
| 306 | MediaAlbumArt *albumArtCopy = static_cast<MediaAlbumArt *>(mAlbumArt->pointer()); |
| 307 | albumArtCopy->mSize = albumArt->mSize; |
| 308 | albumArtCopy->mData = (uint8_t *)albumArtCopy + sizeof(MediaAlbumArt); |
| 309 | memcpy(albumArtCopy->mData, albumArt->mData, albumArt->mSize); |
| 310 | delete albumArt; // Fix memory leakage |
| 311 | return mAlbumArt; |
| 312 | } |
| 313 | |
| 314 | const char* MetadataRetrieverClient::extractMetadata(int keyCode) |
| 315 | { |
| 316 | LOGV("extractMetadata"); |
| 317 | Mutex::Autolock lock(mLock); |
| 318 | if (mRetriever == NULL) { |
| 319 | LOGE("retriever is not initialized"); |
| 320 | return NULL; |
| 321 | } |
| 322 | return mRetriever->extractMetadata(keyCode); |
| 323 | } |
| 324 | |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 325 | }; // namespace android |