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> |
| 24 | #include <dirent.h> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | #include <string.h> |
| 28 | #include <cutils/atomic.h> |
Andreas Huber | 7782fdf | 2010-01-04 15:02:02 -0800 | [diff] [blame] | 29 | #include <cutils/properties.h> |
Mathias Agopian | 1875b7e | 2010-01-29 17:16:30 -0800 | [diff] [blame] | 30 | #include <binder/MemoryBase.h> |
| 31 | #include <binder/MemoryHeapBase.h> |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 32 | #include <android_runtime/ActivityManager.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 33 | #include <binder/IPCThreadState.h> |
| 34 | #include <binder/IServiceManager.h> |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 35 | #include <media/MediaMetadataRetrieverInterface.h> |
| 36 | #include <media/MediaPlayerInterface.h> |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 37 | #include <private/media/VideoFrame.h> |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 38 | #include "MidiMetadataRetriever.h" |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 39 | #include "MetadataRetrieverClient.h" |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 40 | #include "StagefrightMetadataRetriever.h" |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 41 | |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 42 | namespace android { |
| 43 | |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 44 | extern player_type getPlayerType(const char* url); |
| 45 | extern player_type getPlayerType(int fd, int64_t offset, int64_t length); |
| 46 | |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 47 | MetadataRetrieverClient::MetadataRetrieverClient(pid_t pid) |
| 48 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 49 | ALOGV("MetadataRetrieverClient constructor pid(%d)", pid); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 50 | mPid = pid; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 51 | mThumbnail = NULL; |
| 52 | mAlbumArt = NULL; |
Jean-Baptiste Queru | 680f8c7 | 2009-03-21 11:40:18 -0700 | [diff] [blame] | 53 | mRetriever = NULL; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | MetadataRetrieverClient::~MetadataRetrieverClient() |
| 57 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 58 | ALOGV("MetadataRetrieverClient destructor"); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 59 | disconnect(); |
| 60 | } |
| 61 | |
| 62 | status_t MetadataRetrieverClient::dump(int fd, const Vector<String16>& args) const |
| 63 | { |
| 64 | const size_t SIZE = 256; |
| 65 | char buffer[SIZE]; |
| 66 | String8 result; |
| 67 | result.append(" MetadataRetrieverClient\n"); |
James Dong | 11eab05 | 2011-01-06 12:20:35 -0800 | [diff] [blame] | 68 | snprintf(buffer, 255, " pid(%d)\n", mPid); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 69 | result.append(buffer); |
| 70 | write(fd, result.string(), result.size()); |
| 71 | write(fd, "\n", 1); |
| 72 | return NO_ERROR; |
| 73 | } |
| 74 | |
| 75 | void MetadataRetrieverClient::disconnect() |
| 76 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 77 | ALOGV("disconnect from pid %d", mPid); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 78 | Mutex::Autolock lock(mLock); |
| 79 | mRetriever.clear(); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 80 | mThumbnail.clear(); |
| 81 | mAlbumArt.clear(); |
| 82 | IPCThreadState::self()->flushCommands(); |
| 83 | } |
| 84 | |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 85 | static sp<MediaMetadataRetrieverBase> createRetriever(player_type playerType) |
| 86 | { |
| 87 | sp<MediaMetadataRetrieverBase> p; |
| 88 | switch (playerType) { |
Andreas Huber | 6a3a018 | 2009-12-17 13:31:13 -0800 | [diff] [blame] | 89 | case STAGEFRIGHT_PLAYER: |
Andreas Huber | 7782fdf | 2010-01-04 15:02:02 -0800 | [diff] [blame] | 90 | { |
Andreas Huber | 8d65dd2 | 2010-06-23 16:40:57 -0700 | [diff] [blame] | 91 | p = new StagefrightMetadataRetriever; |
| 92 | break; |
Andreas Huber | 7782fdf | 2010-01-04 15:02:02 -0800 | [diff] [blame] | 93 | } |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 94 | case SONIVOX_PLAYER: |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 95 | ALOGV("create midi metadata retriever"); |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 96 | p = new MidiMetadataRetriever(); |
| 97 | break; |
| 98 | default: |
| 99 | // TODO: |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 100 | // support for TEST_PLAYER |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 101 | ALOGE("player type %d is not supported", playerType); |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 102 | break; |
| 103 | } |
| 104 | if (p == NULL) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 105 | ALOGE("failed to create a retriever object"); |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 106 | } |
| 107 | return p; |
| 108 | } |
| 109 | |
Andreas Huber | 5b7ced6 | 2011-03-21 10:25:44 -0700 | [diff] [blame] | 110 | status_t MetadataRetrieverClient::setDataSource( |
| 111 | const char *url, const KeyedVector<String8, String8> *headers) |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 112 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 113 | ALOGV("setDataSource(%s)", url); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 114 | Mutex::Autolock lock(mLock); |
| 115 | if (url == NULL) { |
| 116 | return UNKNOWN_ERROR; |
| 117 | } |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 118 | player_type playerType = getPlayerType(url); |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 119 | ALOGV("player type = %d", playerType); |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 120 | sp<MediaMetadataRetrieverBase> p = createRetriever(playerType); |
| 121 | if (p == NULL) return NO_INIT; |
Andreas Huber | 5b7ced6 | 2011-03-21 10:25:44 -0700 | [diff] [blame] | 122 | status_t ret = p->setDataSource(url, headers); |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 123 | if (ret == NO_ERROR) mRetriever = p; |
| 124 | return ret; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | status_t MetadataRetrieverClient::setDataSource(int fd, int64_t offset, int64_t length) |
| 128 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 129 | ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 130 | Mutex::Autolock lock(mLock); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 131 | struct stat sb; |
| 132 | int ret = fstat(fd, &sb); |
| 133 | if (ret != 0) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 134 | ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno)); |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 135 | return BAD_VALUE; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 136 | } |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 137 | ALOGV("st_dev = %llu", sb.st_dev); |
| 138 | ALOGV("st_mode = %u", sb.st_mode); |
| 139 | ALOGV("st_uid = %lu", sb.st_uid); |
| 140 | ALOGV("st_gid = %lu", sb.st_gid); |
| 141 | ALOGV("st_size = %llu", sb.st_size); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 142 | |
| 143 | if (offset >= sb.st_size) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 144 | ALOGE("offset (%lld) bigger than file size (%llu)", offset, sb.st_size); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 145 | ::close(fd); |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 146 | return BAD_VALUE; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 147 | } |
| 148 | if (offset + length > sb.st_size) { |
| 149 | length = sb.st_size - offset; |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 150 | ALOGV("calculated length = %lld", length); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 151 | } |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 152 | |
| 153 | player_type playerType = getPlayerType(fd, offset, length); |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 154 | ALOGV("player type = %d", playerType); |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 155 | sp<MediaMetadataRetrieverBase> p = createRetriever(playerType); |
| 156 | if (p == NULL) { |
| 157 | ::close(fd); |
| 158 | return NO_INIT; |
| 159 | } |
James Dong | 11eab05 | 2011-01-06 12:20:35 -0800 | [diff] [blame] | 160 | status_t status = p->setDataSource(fd, offset, length); |
James Dong | 392ff3b | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 161 | if (status == NO_ERROR) mRetriever = p; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 162 | ::close(fd); |
| 163 | return status; |
| 164 | } |
| 165 | |
James Dong | faf09ba | 2010-12-02 17:42:08 -0800 | [diff] [blame] | 166 | sp<IMemory> MetadataRetrieverClient::getFrameAtTime(int64_t timeUs, int option) |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 167 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 168 | ALOGV("getFrameAtTime: time(%lld us) option(%d)", timeUs, option); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 169 | Mutex::Autolock lock(mLock); |
| 170 | mThumbnail.clear(); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 171 | if (mRetriever == NULL) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 172 | ALOGE("retriever is not initialized"); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 173 | return NULL; |
| 174 | } |
James Dong | faf09ba | 2010-12-02 17:42:08 -0800 | [diff] [blame] | 175 | VideoFrame *frame = mRetriever->getFrameAtTime(timeUs, option); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 176 | if (frame == NULL) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 177 | ALOGE("failed to capture a video frame"); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 178 | return NULL; |
| 179 | } |
| 180 | size_t size = sizeof(VideoFrame) + frame->mSize; |
Mathias Agopian | 1875b7e | 2010-01-29 17:16:30 -0800 | [diff] [blame] | 181 | sp<MemoryHeapBase> heap = new MemoryHeapBase(size, 0, "MetadataRetrieverClient"); |
| 182 | if (heap == NULL) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 183 | ALOGE("failed to create MemoryDealer"); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 184 | delete frame; |
| 185 | return NULL; |
| 186 | } |
Mathias Agopian | 1875b7e | 2010-01-29 17:16:30 -0800 | [diff] [blame] | 187 | mThumbnail = new MemoryBase(heap, 0, size); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 188 | if (mThumbnail == NULL) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 189 | ALOGE("not enough memory for VideoFrame size=%u", size); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 190 | delete frame; |
| 191 | return NULL; |
| 192 | } |
| 193 | VideoFrame *frameCopy = static_cast<VideoFrame *>(mThumbnail->pointer()); |
| 194 | frameCopy->mWidth = frame->mWidth; |
| 195 | frameCopy->mHeight = frame->mHeight; |
| 196 | frameCopy->mDisplayWidth = frame->mDisplayWidth; |
| 197 | frameCopy->mDisplayHeight = frame->mDisplayHeight; |
| 198 | frameCopy->mSize = frame->mSize; |
James Dong | 53ebc72 | 2010-11-08 16:04:27 -0800 | [diff] [blame] | 199 | frameCopy->mRotationAngle = frame->mRotationAngle; |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 200 | ALOGV("rotation: %d", frameCopy->mRotationAngle); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 201 | frameCopy->mData = (uint8_t *)frameCopy + sizeof(VideoFrame); |
| 202 | memcpy(frameCopy->mData, frame->mData, frame->mSize); |
| 203 | delete frame; // Fix memory leakage |
| 204 | return mThumbnail; |
| 205 | } |
| 206 | |
| 207 | sp<IMemory> MetadataRetrieverClient::extractAlbumArt() |
| 208 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 209 | ALOGV("extractAlbumArt"); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 210 | Mutex::Autolock lock(mLock); |
| 211 | mAlbumArt.clear(); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 212 | if (mRetriever == NULL) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 213 | ALOGE("retriever is not initialized"); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 214 | return NULL; |
| 215 | } |
| 216 | MediaAlbumArt *albumArt = mRetriever->extractAlbumArt(); |
| 217 | if (albumArt == NULL) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 218 | ALOGE("failed to extract an album art"); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 219 | return NULL; |
| 220 | } |
| 221 | size_t size = sizeof(MediaAlbumArt) + albumArt->mSize; |
Mathias Agopian | 1875b7e | 2010-01-29 17:16:30 -0800 | [diff] [blame] | 222 | sp<MemoryHeapBase> heap = new MemoryHeapBase(size, 0, "MetadataRetrieverClient"); |
| 223 | if (heap == NULL) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 224 | ALOGE("failed to create MemoryDealer object"); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 225 | delete albumArt; |
| 226 | return NULL; |
| 227 | } |
Mathias Agopian | 1875b7e | 2010-01-29 17:16:30 -0800 | [diff] [blame] | 228 | mAlbumArt = new MemoryBase(heap, 0, size); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 229 | if (mAlbumArt == NULL) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 230 | ALOGE("not enough memory for MediaAlbumArt size=%u", size); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 231 | delete albumArt; |
| 232 | return NULL; |
| 233 | } |
| 234 | MediaAlbumArt *albumArtCopy = static_cast<MediaAlbumArt *>(mAlbumArt->pointer()); |
| 235 | albumArtCopy->mSize = albumArt->mSize; |
| 236 | albumArtCopy->mData = (uint8_t *)albumArtCopy + sizeof(MediaAlbumArt); |
| 237 | memcpy(albumArtCopy->mData, albumArt->mData, albumArt->mSize); |
| 238 | delete albumArt; // Fix memory leakage |
| 239 | return mAlbumArt; |
| 240 | } |
| 241 | |
| 242 | const char* MetadataRetrieverClient::extractMetadata(int keyCode) |
| 243 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 244 | ALOGV("extractMetadata"); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 245 | Mutex::Autolock lock(mLock); |
| 246 | if (mRetriever == NULL) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 247 | ALOGE("retriever is not initialized"); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 248 | return NULL; |
| 249 | } |
| 250 | return mRetriever->extractMetadata(keyCode); |
| 251 | } |
| 252 | |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 253 | }; // namespace android |