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>
29#include <cutils/properties.h>
30#include <binder/MemoryBase.h>
31#include <binder/MemoryHeapBase.h>
32#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
34#include <media/MediaMetadataRetrieverInterface.h>
35#include <media/MediaPlayerInterface.h>
36#include <private/media/VideoFrame.h>
37#include "MidiMetadataRetriever.h"
38#include "MetadataRetrieverClient.h"
39#include "StagefrightMetadataRetriever.h"
40#include "MediaPlayerFactory.h"
41
42namespace android {
43
44MetadataRetrieverClient::MetadataRetrieverClient(pid_t pid)
45{
46    ALOGV("MetadataRetrieverClient constructor pid(%d)", pid);
47    mPid = pid;
48    mThumbnail = NULL;
49    mAlbumArt = NULL;
50    mRetriever = NULL;
51}
52
53MetadataRetrieverClient::~MetadataRetrieverClient()
54{
55    ALOGV("MetadataRetrieverClient destructor");
56    disconnect();
57}
58
59status_t MetadataRetrieverClient::dump(int fd, const Vector<String16>& args) const
60{
61    const size_t SIZE = 256;
62    char buffer[SIZE];
63    String8 result;
64    result.append(" MetadataRetrieverClient\n");
65    snprintf(buffer, 255, "  pid(%d)\n", mPid);
66    result.append(buffer);
67    write(fd, result.string(), result.size());
68    write(fd, "\n", 1);
69    return NO_ERROR;
70}
71
72void MetadataRetrieverClient::disconnect()
73{
74    ALOGV("disconnect from pid %d", mPid);
75    Mutex::Autolock lock(mLock);
76    mRetriever.clear();
77    mThumbnail.clear();
78    mAlbumArt.clear();
79    IPCThreadState::self()->flushCommands();
80}
81
82static sp<MediaMetadataRetrieverBase> createRetriever(player_type playerType)
83{
84    sp<MediaMetadataRetrieverBase> p;
85    switch (playerType) {
86        case STAGEFRIGHT_PLAYER:
87        case NU_PLAYER:
88        {
89            p = new StagefrightMetadataRetriever;
90            break;
91        }
92        case SONIVOX_PLAYER:
93            ALOGV("create midi metadata retriever");
94            p = new MidiMetadataRetriever();
95            break;
96        default:
97            // TODO:
98            // support for TEST_PLAYER
99            ALOGE("player type %d is not supported",  playerType);
100            break;
101    }
102    if (p == NULL) {
103        ALOGE("failed to create a retriever object");
104    }
105    return p;
106}
107
108status_t MetadataRetrieverClient::setDataSource(
109        const char *url, const KeyedVector<String8, String8> *headers)
110{
111    ALOGV("setDataSource(%s)", url);
112    Mutex::Autolock lock(mLock);
113    if (url == NULL) {
114        return UNKNOWN_ERROR;
115    }
116
117    // When asking the MediaPlayerFactory subsystem to choose a media player for
118    // a given URL, a pointer to an outer IMediaPlayer can be passed to the
119    // factory system to be taken into consideration along with the URL.  In the
120    // case of choosing an instance of a MediaPlayerBase for a
121    // MetadataRetrieverClient, there is no outer IMediaPlayer which will
122    // eventually encapsulate the result of this selection.  In this case, just
123    // pass NULL to getPlayerType to indicate that there is no outer
124    // IMediaPlayer to consider during selection.
125    player_type playerType =
126        MediaPlayerFactory::getPlayerType(NULL /* client */, url);
127    ALOGV("player type = %d", playerType);
128    sp<MediaMetadataRetrieverBase> p = createRetriever(playerType);
129    if (p == NULL) return NO_INIT;
130    status_t ret = p->setDataSource(url, headers);
131    if (ret == NO_ERROR) mRetriever = p;
132    return ret;
133}
134
135status_t MetadataRetrieverClient::setDataSource(int fd, int64_t offset, int64_t length)
136{
137    ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
138    Mutex::Autolock lock(mLock);
139    struct stat sb;
140    int ret = fstat(fd, &sb);
141    if (ret != 0) {
142        ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
143        return BAD_VALUE;
144    }
145    ALOGV("st_dev  = %llu", sb.st_dev);
146    ALOGV("st_mode = %u", sb.st_mode);
147    ALOGV("st_uid  = %lu", sb.st_uid);
148    ALOGV("st_gid  = %lu", sb.st_gid);
149    ALOGV("st_size = %llu", sb.st_size);
150
151    if (offset >= sb.st_size) {
152        ALOGE("offset (%lld) bigger than file size (%llu)", offset, sb.st_size);
153        ::close(fd);
154        return BAD_VALUE;
155    }
156    if (offset + length > sb.st_size) {
157        length = sb.st_size - offset;
158        ALOGV("calculated length = %lld", length);
159    }
160
161    player_type playerType =
162        MediaPlayerFactory::getPlayerType(NULL /* client */,
163                                          fd,
164                                          offset,
165                                          length);
166    ALOGV("player type = %d", playerType);
167    sp<MediaMetadataRetrieverBase> p = createRetriever(playerType);
168    if (p == NULL) {
169        ::close(fd);
170        return NO_INIT;
171    }
172    status_t status = p->setDataSource(fd, offset, length);
173    if (status == NO_ERROR) mRetriever = p;
174    ::close(fd);
175    return status;
176}
177
178sp<IMemory> MetadataRetrieverClient::getFrameAtTime(int64_t timeUs, int option)
179{
180    ALOGV("getFrameAtTime: time(%lld us) option(%d)", timeUs, option);
181    Mutex::Autolock lock(mLock);
182    mThumbnail.clear();
183    if (mRetriever == NULL) {
184        ALOGE("retriever is not initialized");
185        return NULL;
186    }
187    VideoFrame *frame = mRetriever->getFrameAtTime(timeUs, option);
188    if (frame == NULL) {
189        ALOGE("failed to capture a video frame");
190        return NULL;
191    }
192    size_t size = sizeof(VideoFrame) + frame->mSize;
193    sp<MemoryHeapBase> heap = new MemoryHeapBase(size, 0, "MetadataRetrieverClient");
194    if (heap == NULL) {
195        ALOGE("failed to create MemoryDealer");
196        delete frame;
197        return NULL;
198    }
199    mThumbnail = new MemoryBase(heap, 0, size);
200    if (mThumbnail == NULL) {
201        ALOGE("not enough memory for VideoFrame size=%u", size);
202        delete frame;
203        return NULL;
204    }
205    VideoFrame *frameCopy = static_cast<VideoFrame *>(mThumbnail->pointer());
206    frameCopy->mWidth = frame->mWidth;
207    frameCopy->mHeight = frame->mHeight;
208    frameCopy->mDisplayWidth = frame->mDisplayWidth;
209    frameCopy->mDisplayHeight = frame->mDisplayHeight;
210    frameCopy->mSize = frame->mSize;
211    frameCopy->mRotationAngle = frame->mRotationAngle;
212    ALOGV("rotation: %d", frameCopy->mRotationAngle);
213    frameCopy->mData = (uint8_t *)frameCopy + sizeof(VideoFrame);
214    memcpy(frameCopy->mData, frame->mData, frame->mSize);
215    delete frame;  // Fix memory leakage
216    return mThumbnail;
217}
218
219sp<IMemory> MetadataRetrieverClient::extractAlbumArt()
220{
221    ALOGV("extractAlbumArt");
222    Mutex::Autolock lock(mLock);
223    mAlbumArt.clear();
224    if (mRetriever == NULL) {
225        ALOGE("retriever is not initialized");
226        return NULL;
227    }
228    MediaAlbumArt *albumArt = mRetriever->extractAlbumArt();
229    if (albumArt == NULL) {
230        ALOGE("failed to extract an album art");
231        return NULL;
232    }
233    size_t size = sizeof(MediaAlbumArt) + albumArt->mSize;
234    sp<MemoryHeapBase> heap = new MemoryHeapBase(size, 0, "MetadataRetrieverClient");
235    if (heap == NULL) {
236        ALOGE("failed to create MemoryDealer object");
237        delete albumArt;
238        return NULL;
239    }
240    mAlbumArt = new MemoryBase(heap, 0, size);
241    if (mAlbumArt == NULL) {
242        ALOGE("not enough memory for MediaAlbumArt size=%u", size);
243        delete albumArt;
244        return NULL;
245    }
246    MediaAlbumArt *albumArtCopy = static_cast<MediaAlbumArt *>(mAlbumArt->pointer());
247    albumArtCopy->mSize = albumArt->mSize;
248    albumArtCopy->mData = (uint8_t *)albumArtCopy + sizeof(MediaAlbumArt);
249    memcpy(albumArtCopy->mData, albumArt->mData, albumArt->mSize);
250    delete albumArt;  // Fix memory leakage
251    return mAlbumArt;
252}
253
254const char* MetadataRetrieverClient::extractMetadata(int keyCode)
255{
256    ALOGV("extractMetadata");
257    Mutex::Autolock lock(mLock);
258    if (mRetriever == NULL) {
259        ALOGE("retriever is not initialized");
260        return NULL;
261    }
262    return mRetriever->extractMetadata(keyCode);
263}
264
265}; // namespace android
266