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> |
Andreas Huber | 7782fdf | 2010-01-04 15:02:02 -0800 | [diff] [blame] | 30 | #include <cutils/properties.h> |
Mathias Agopian | 1875b7e | 2010-01-29 17:16:30 -0800 | [diff] [blame] | 31 | #include <binder/MemoryBase.h> |
| 32 | #include <binder/MemoryHeapBase.h> |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 33 | #include <android_runtime/ActivityManager.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 34 | #include <binder/IPCThreadState.h> |
| 35 | #include <binder/IServiceManager.h> |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 36 | #include <media/MediaMetadataRetrieverInterface.h> |
| 37 | #include <media/MediaPlayerInterface.h> |
| 38 | #include <media/PVMetadataRetriever.h> |
| 39 | #include <private/media/VideoFrame.h> |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 40 | #include "MidiMetadataRetriever.h" |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 41 | #include "MetadataRetrieverClient.h" |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 42 | #include "StagefrightMetadataRetriever.h" |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 43 | |
Dave Sparks | 7c0d20a | 2009-10-26 16:28:26 -0700 | [diff] [blame] | 44 | /* desktop Linux needs a little help with gettid() */ |
| 45 | #if defined(HAVE_GETTID) && !defined(HAVE_ANDROID_OS) |
| 46 | #define __KERNEL__ |
| 47 | # include <linux/unistd.h> |
| 48 | #ifdef _syscall0 |
| 49 | _syscall0(pid_t,gettid) |
| 50 | #else |
| 51 | pid_t gettid() { return syscall(__NR_gettid);} |
| 52 | #endif |
| 53 | #undef __KERNEL__ |
| 54 | #endif |
| 55 | |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 56 | namespace android { |
| 57 | |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 58 | extern player_type getPlayerType(const char* url); |
| 59 | extern player_type getPlayerType(int fd, int64_t offset, int64_t length); |
| 60 | |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 61 | MetadataRetrieverClient::MetadataRetrieverClient(pid_t pid) |
| 62 | { |
| 63 | LOGV("MetadataRetrieverClient constructor pid(%d)", pid); |
| 64 | mPid = pid; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 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(); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 95 | mThumbnail.clear(); |
| 96 | mAlbumArt.clear(); |
James Dong | 67fc234 | 2009-09-29 10:45:27 -0700 | [diff] [blame] | 97 | mMode = METADATA_MODE_FRAME_CAPTURE_AND_METADATA_RETRIEVAL; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 98 | IPCThreadState::self()->flushCommands(); |
| 99 | } |
| 100 | |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 101 | static sp<MediaMetadataRetrieverBase> createRetriever(player_type playerType) |
| 102 | { |
| 103 | sp<MediaMetadataRetrieverBase> p; |
| 104 | switch (playerType) { |
Andreas Huber | 6a3a018 | 2009-12-17 13:31:13 -0800 | [diff] [blame] | 105 | case STAGEFRIGHT_PLAYER: |
Andreas Huber | 7782fdf | 2010-01-04 15:02:02 -0800 | [diff] [blame] | 106 | { |
Andreas Huber | 8d65dd2 | 2010-06-23 16:40:57 -0700 | [diff] [blame] | 107 | p = new StagefrightMetadataRetriever; |
| 108 | break; |
Andreas Huber | 7782fdf | 2010-01-04 15:02:02 -0800 | [diff] [blame] | 109 | } |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 110 | #ifndef NO_OPENCORE |
| 111 | case PV_PLAYER: |
| 112 | LOGV("create pv metadata retriever"); |
| 113 | p = new PVMetadataRetriever(); |
| 114 | break; |
| 115 | #endif |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 116 | case SONIVOX_PLAYER: |
| 117 | LOGV("create midi metadata retriever"); |
| 118 | p = new MidiMetadataRetriever(); |
| 119 | break; |
| 120 | default: |
| 121 | // TODO: |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 122 | // support for TEST_PLAYER |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 123 | LOGE("player type %d is not supported", playerType); |
| 124 | break; |
| 125 | } |
| 126 | if (p == NULL) { |
| 127 | LOGE("failed to create a retriever object"); |
| 128 | } |
| 129 | return p; |
| 130 | } |
| 131 | |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 132 | status_t MetadataRetrieverClient::setDataSource(const char *url) |
| 133 | { |
| 134 | LOGV("setDataSource(%s)", url); |
| 135 | Mutex::Autolock lock(mLock); |
| 136 | if (url == NULL) { |
| 137 | return UNKNOWN_ERROR; |
| 138 | } |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 139 | player_type playerType = getPlayerType(url); |
| 140 | LOGV("player type = %d", playerType); |
| 141 | sp<MediaMetadataRetrieverBase> p = createRetriever(playerType); |
| 142 | if (p == NULL) return NO_INIT; |
James Dong | 67fc234 | 2009-09-29 10:45:27 -0700 | [diff] [blame] | 143 | status_t ret = p->setMode(mMode); |
| 144 | if (ret == NO_ERROR) { |
| 145 | ret = p->setDataSource(url); |
| 146 | } |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 147 | if (ret == NO_ERROR) mRetriever = p; |
| 148 | return ret; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | status_t MetadataRetrieverClient::setDataSource(int fd, int64_t offset, int64_t length) |
| 152 | { |
| 153 | LOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length); |
| 154 | Mutex::Autolock lock(mLock); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 155 | struct stat sb; |
| 156 | int ret = fstat(fd, &sb); |
| 157 | if (ret != 0) { |
| 158 | LOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno)); |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 159 | return BAD_VALUE; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 160 | } |
| 161 | LOGV("st_dev = %llu", sb.st_dev); |
| 162 | LOGV("st_mode = %u", sb.st_mode); |
| 163 | LOGV("st_uid = %lu", sb.st_uid); |
| 164 | LOGV("st_gid = %lu", sb.st_gid); |
| 165 | LOGV("st_size = %llu", sb.st_size); |
| 166 | |
| 167 | if (offset >= sb.st_size) { |
| 168 | LOGE("offset (%lld) bigger than file size (%llu)", offset, sb.st_size); |
| 169 | ::close(fd); |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 170 | return BAD_VALUE; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 171 | } |
| 172 | if (offset + length > sb.st_size) { |
| 173 | length = sb.st_size - offset; |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 174 | LOGV("calculated length = %lld", length); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 175 | } |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 176 | |
| 177 | player_type playerType = getPlayerType(fd, offset, length); |
| 178 | LOGV("player type = %d", playerType); |
| 179 | sp<MediaMetadataRetrieverBase> p = createRetriever(playerType); |
| 180 | if (p == NULL) { |
| 181 | ::close(fd); |
| 182 | return NO_INIT; |
| 183 | } |
James Dong | 67fc234 | 2009-09-29 10:45:27 -0700 | [diff] [blame] | 184 | status_t status = p->setMode(mMode); |
| 185 | if (status == NO_ERROR) { |
| 186 | p->setDataSource(fd, offset, length); |
| 187 | } |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 188 | if (status == NO_ERROR) mRetriever = p; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 189 | ::close(fd); |
| 190 | return status; |
| 191 | } |
| 192 | |
| 193 | status_t MetadataRetrieverClient::setMode(int mode) |
| 194 | { |
| 195 | LOGV("setMode"); |
| 196 | Mutex::Autolock lock(mLock); |
James Dong | 67fc234 | 2009-09-29 10:45:27 -0700 | [diff] [blame] | 197 | if (mode < METADATA_MODE_NOOP || |
| 198 | mode > METADATA_MODE_FRAME_CAPTURE_AND_METADATA_RETRIEVAL) { |
| 199 | LOGE("invalid mode %d", mode); |
| 200 | return BAD_VALUE; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 201 | } |
James Dong | 67fc234 | 2009-09-29 10:45:27 -0700 | [diff] [blame] | 202 | mMode = mode; |
| 203 | return NO_ERROR; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | status_t MetadataRetrieverClient::getMode(int* mode) const |
| 207 | { |
| 208 | LOGV("getMode"); |
| 209 | Mutex::Autolock lock(mLock); |
James Dong | 67fc234 | 2009-09-29 10:45:27 -0700 | [diff] [blame] | 210 | |
| 211 | // TODO: |
| 212 | // This may not be necessary. |
| 213 | // If setDataSource() has not been called, return the cached value |
| 214 | // otherwise, return the value retrieved from the retriever |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 215 | if (mRetriever == NULL) { |
James Dong | 67fc234 | 2009-09-29 10:45:27 -0700 | [diff] [blame] | 216 | *mode = mMode; |
| 217 | } else { |
| 218 | mRetriever->getMode(mode); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 219 | } |
James Dong | 67fc234 | 2009-09-29 10:45:27 -0700 | [diff] [blame] | 220 | return NO_ERROR; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | sp<IMemory> MetadataRetrieverClient::captureFrame() |
| 224 | { |
| 225 | LOGV("captureFrame"); |
| 226 | Mutex::Autolock lock(mLock); |
| 227 | mThumbnail.clear(); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 228 | if (mRetriever == NULL) { |
| 229 | LOGE("retriever is not initialized"); |
| 230 | return NULL; |
| 231 | } |
| 232 | VideoFrame *frame = mRetriever->captureFrame(); |
| 233 | if (frame == NULL) { |
| 234 | LOGE("failed to capture a video frame"); |
| 235 | return NULL; |
| 236 | } |
| 237 | size_t size = sizeof(VideoFrame) + frame->mSize; |
Mathias Agopian | 1875b7e | 2010-01-29 17:16:30 -0800 | [diff] [blame] | 238 | sp<MemoryHeapBase> heap = new MemoryHeapBase(size, 0, "MetadataRetrieverClient"); |
| 239 | if (heap == NULL) { |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 240 | LOGE("failed to create MemoryDealer"); |
| 241 | delete frame; |
| 242 | return NULL; |
| 243 | } |
Mathias Agopian | 1875b7e | 2010-01-29 17:16:30 -0800 | [diff] [blame] | 244 | mThumbnail = new MemoryBase(heap, 0, size); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 245 | if (mThumbnail == NULL) { |
| 246 | LOGE("not enough memory for VideoFrame size=%u", size); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 247 | delete frame; |
| 248 | return NULL; |
| 249 | } |
| 250 | VideoFrame *frameCopy = static_cast<VideoFrame *>(mThumbnail->pointer()); |
| 251 | frameCopy->mWidth = frame->mWidth; |
| 252 | frameCopy->mHeight = frame->mHeight; |
| 253 | frameCopy->mDisplayWidth = frame->mDisplayWidth; |
| 254 | frameCopy->mDisplayHeight = frame->mDisplayHeight; |
| 255 | frameCopy->mSize = frame->mSize; |
| 256 | frameCopy->mData = (uint8_t *)frameCopy + sizeof(VideoFrame); |
| 257 | memcpy(frameCopy->mData, frame->mData, frame->mSize); |
| 258 | delete frame; // Fix memory leakage |
| 259 | return mThumbnail; |
| 260 | } |
| 261 | |
| 262 | sp<IMemory> MetadataRetrieverClient::extractAlbumArt() |
| 263 | { |
| 264 | LOGV("extractAlbumArt"); |
| 265 | Mutex::Autolock lock(mLock); |
| 266 | mAlbumArt.clear(); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 267 | if (mRetriever == NULL) { |
| 268 | LOGE("retriever is not initialized"); |
| 269 | return NULL; |
| 270 | } |
| 271 | MediaAlbumArt *albumArt = mRetriever->extractAlbumArt(); |
| 272 | if (albumArt == NULL) { |
| 273 | LOGE("failed to extract an album art"); |
| 274 | return NULL; |
| 275 | } |
| 276 | size_t size = sizeof(MediaAlbumArt) + albumArt->mSize; |
Mathias Agopian | 1875b7e | 2010-01-29 17:16:30 -0800 | [diff] [blame] | 277 | sp<MemoryHeapBase> heap = new MemoryHeapBase(size, 0, "MetadataRetrieverClient"); |
| 278 | if (heap == NULL) { |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 279 | LOGE("failed to create MemoryDealer object"); |
| 280 | delete albumArt; |
| 281 | return NULL; |
| 282 | } |
Mathias Agopian | 1875b7e | 2010-01-29 17:16:30 -0800 | [diff] [blame] | 283 | mAlbumArt = new MemoryBase(heap, 0, size); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 284 | if (mAlbumArt == NULL) { |
| 285 | LOGE("not enough memory for MediaAlbumArt size=%u", size); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 286 | delete albumArt; |
| 287 | return NULL; |
| 288 | } |
| 289 | MediaAlbumArt *albumArtCopy = static_cast<MediaAlbumArt *>(mAlbumArt->pointer()); |
| 290 | albumArtCopy->mSize = albumArt->mSize; |
| 291 | albumArtCopy->mData = (uint8_t *)albumArtCopy + sizeof(MediaAlbumArt); |
| 292 | memcpy(albumArtCopy->mData, albumArt->mData, albumArt->mSize); |
| 293 | delete albumArt; // Fix memory leakage |
| 294 | return mAlbumArt; |
| 295 | } |
| 296 | |
| 297 | const char* MetadataRetrieverClient::extractMetadata(int keyCode) |
| 298 | { |
| 299 | LOGV("extractMetadata"); |
| 300 | Mutex::Autolock lock(mLock); |
| 301 | if (mRetriever == NULL) { |
| 302 | LOGE("retriever is not initialized"); |
| 303 | return NULL; |
| 304 | } |
| 305 | return mRetriever->extractMetadata(keyCode); |
| 306 | } |
| 307 | |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 308 | }; // namespace android |