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/DataSource.h>
35 #include <media/IMediaHTTPService.h>
36 #include <media/MediaMetadataRetrieverInterface.h>
37 #include <media/MediaPlayerInterface.h>
38 #include <media/stagefright/InterfaceUtils.h>
39 #include <media/stagefright/Utils.h>
40 #include <media/stagefright/FoundationUtils.h>
41 #include <private/media/VideoFrame.h>
42 #include "MetadataRetrieverClient.h"
43 #include "StagefrightMetadataRetriever.h"
44 #include "MediaPlayerFactory.h"
45 
46 namespace android {
47 
MetadataRetrieverClient(pid_t pid)48 MetadataRetrieverClient::MetadataRetrieverClient(pid_t pid)
49 {
50     ALOGV("MetadataRetrieverClient constructor pid(%d)", pid);
51     mPid = pid;
52     mAlbumArt = NULL;
53     mRetriever = NULL;
54 }
55 
~MetadataRetrieverClient()56 MetadataRetrieverClient::~MetadataRetrieverClient()
57 {
58     ALOGV("MetadataRetrieverClient destructor");
59     disconnect();
60 }
61 
dump(int fd,const Vector<String16> &)62 status_t MetadataRetrieverClient::dump(int fd, const Vector<String16>& /*args*/)
63 {
64     const size_t SIZE = 256;
65     char buffer[SIZE];
66     String8 result;
67     result.append(" MetadataRetrieverClient\n");
68     snprintf(buffer, 255, "  pid(%d)\n", mPid);
69     result.append(buffer);
70     write(fd, result.string(), result.size());
71     write(fd, "\n", 1);
72     return NO_ERROR;
73 }
74 
disconnect()75 void MetadataRetrieverClient::disconnect()
76 {
77     ALOGV("disconnect from pid %d", mPid);
78     Mutex::Autolock lock(mLock);
79     mRetriever.clear();
80     mAlbumArt.clear();
81     IPCThreadState::self()->flushCommands();
82 }
83 
createRetriever(player_type playerType)84 static sp<MediaMetadataRetrieverBase> createRetriever(player_type playerType)
85 {
86     sp<MediaMetadataRetrieverBase> p;
87     switch (playerType) {
88         case STAGEFRIGHT_PLAYER:
89         case NU_PLAYER:
90         {
91             p = new StagefrightMetadataRetriever;
92             break;
93         }
94         default:
95             // TODO:
96             // support for TEST_PLAYER
97             ALOGE("player type %d is not supported",  playerType);
98             break;
99     }
100     if (p == NULL) {
101         ALOGE("failed to create a retriever object");
102     }
103     return p;
104 }
105 
setDataSource(const sp<IMediaHTTPService> & httpService,const char * url,const KeyedVector<String8,String8> * headers)106 status_t MetadataRetrieverClient::setDataSource(
107         const sp<IMediaHTTPService> &httpService,
108         const char *url,
109         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(httpService, url, headers);
131     if (ret == NO_ERROR) mRetriever = p;
132     return ret;
133 }
134 
setDataSource(int fd,int64_t offset,int64_t length)135 status_t MetadataRetrieverClient::setDataSource(int fd, int64_t offset, int64_t length)
136 {
137     ALOGV("setDataSource fd=%d (%s), offset=%lld, length=%lld",
138             fd, nameForFd(fd).c_str(), (long long) offset, (long long) length);
139     Mutex::Autolock lock(mLock);
140     struct stat sb;
141     int ret = fstat(fd, &sb);
142     if (ret != 0) {
143         ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
144         return BAD_VALUE;
145     }
146     ALOGV("st_dev  = %llu", static_cast<unsigned long long>(sb.st_dev));
147     ALOGV("st_mode = %u", sb.st_mode);
148     ALOGV("st_uid  = %lu", static_cast<unsigned long>(sb.st_uid));
149     ALOGV("st_gid  = %lu", static_cast<unsigned long>(sb.st_gid));
150     ALOGV("st_size = %llu", static_cast<unsigned long long>(sb.st_size));
151 
152     if (offset >= sb.st_size) {
153         ALOGE("offset (%lld) bigger than file size (%llu)",
154                 (long long)offset, (unsigned long long)sb.st_size);
155         return BAD_VALUE;
156     }
157     if (offset + length > sb.st_size) {
158         length = sb.st_size - offset;
159         ALOGV("calculated length = %lld", (long long)length);
160     }
161 
162     player_type playerType =
163         MediaPlayerFactory::getPlayerType(NULL /* client */,
164                                           fd,
165                                           offset,
166                                           length);
167     ALOGV("player type = %d", playerType);
168     sp<MediaMetadataRetrieverBase> p = createRetriever(playerType);
169     if (p == NULL) {
170         return NO_INIT;
171     }
172     status_t status = p->setDataSource(fd, offset, length);
173     if (status == NO_ERROR) mRetriever = p;
174     return status;
175 }
176 
setDataSource(const sp<IDataSource> & source,const char * mime)177 status_t MetadataRetrieverClient::setDataSource(
178         const sp<IDataSource>& source, const char *mime)
179 {
180     ALOGV("setDataSource(IDataSource)");
181     Mutex::Autolock lock(mLock);
182 
183     sp<DataSource> dataSource = CreateDataSourceFromIDataSource(source);
184     player_type playerType =
185         MediaPlayerFactory::getPlayerType(NULL /* client */, dataSource);
186     ALOGV("player type = %d", playerType);
187     sp<MediaMetadataRetrieverBase> p = createRetriever(playerType);
188     if (p == NULL) return NO_INIT;
189     status_t ret = p->setDataSource(dataSource, mime);
190     if (ret == NO_ERROR) mRetriever = p;
191     return ret;
192 }
193 
194 Mutex MetadataRetrieverClient::sLock;
195 
getFrameAtTime(int64_t timeUs,int option,int colorFormat,bool metaOnly)196 sp<IMemory> MetadataRetrieverClient::getFrameAtTime(
197         int64_t timeUs, int option, int colorFormat, bool metaOnly)
198 {
199     ALOGV("getFrameAtTime: time(%lld us) option(%d) colorFormat(%d), metaOnly(%d)",
200             (long long)timeUs, option, colorFormat, metaOnly);
201     Mutex::Autolock lock(mLock);
202     Mutex::Autolock glock(sLock);
203     if (mRetriever == NULL) {
204         ALOGE("retriever is not initialized");
205         return NULL;
206     }
207     sp<IMemory> frame = mRetriever->getFrameAtTime(timeUs, option, colorFormat, metaOnly);
208     if (frame == NULL) {
209         ALOGE("failed to capture a video frame");
210         return NULL;
211     }
212     return frame;
213 }
214 
getImageAtIndex(int index,int colorFormat,bool metaOnly,bool thumbnail)215 sp<IMemory> MetadataRetrieverClient::getImageAtIndex(
216         int index, int colorFormat, bool metaOnly, bool thumbnail) {
217     ALOGV("getImageAtIndex: index(%d) colorFormat(%d), metaOnly(%d) thumbnail(%d)",
218             index, colorFormat, metaOnly, thumbnail);
219     Mutex::Autolock lock(mLock);
220     Mutex::Autolock glock(sLock);
221     if (mRetriever == NULL) {
222         ALOGE("retriever is not initialized");
223         return NULL;
224     }
225     sp<IMemory> frame = mRetriever->getImageAtIndex(index, colorFormat, metaOnly, thumbnail);
226     if (frame == NULL) {
227         ALOGE("failed to extract image");
228         return NULL;
229     }
230     return frame;
231 }
232 
getImageRectAtIndex(int index,int colorFormat,int left,int top,int right,int bottom)233 sp<IMemory> MetadataRetrieverClient::getImageRectAtIndex(
234         int index, int colorFormat, int left, int top, int right, int bottom) {
235     ALOGV("getImageRectAtIndex: index(%d) colorFormat(%d), rect {%d, %d, %d, %d}",
236             index, colorFormat, left, top, right, bottom);
237     Mutex::Autolock lock(mLock);
238     Mutex::Autolock glock(sLock);
239     if (mRetriever == NULL) {
240         ALOGE("retriever is not initialized");
241         return NULL;
242     }
243     sp<IMemory> frame = mRetriever->getImageRectAtIndex(
244             index, colorFormat, left, top, right, bottom);
245     if (frame == NULL) {
246         ALOGE("failed to extract image");
247         return NULL;
248     }
249     return frame;
250 }
251 
getFrameAtIndex(std::vector<sp<IMemory>> * frames,int frameIndex,int numFrames,int colorFormat,bool metaOnly)252 status_t MetadataRetrieverClient::getFrameAtIndex(
253             std::vector<sp<IMemory> > *frames,
254             int frameIndex, int numFrames, int colorFormat, bool metaOnly) {
255     ALOGV("getFrameAtIndex: frameIndex(%d), numFrames(%d), colorFormat(%d), metaOnly(%d)",
256             frameIndex, numFrames, colorFormat, metaOnly);
257     Mutex::Autolock lock(mLock);
258     Mutex::Autolock glock(sLock);
259     if (mRetriever == NULL) {
260         ALOGE("retriever is not initialized");
261         return INVALID_OPERATION;
262     }
263 
264     status_t err = mRetriever->getFrameAtIndex(
265             frames, frameIndex, numFrames, colorFormat, metaOnly);
266     if (err != OK) {
267         frames->clear();
268         return err;
269     }
270     return OK;
271 }
272 
extractAlbumArt()273 sp<IMemory> MetadataRetrieverClient::extractAlbumArt()
274 {
275     ALOGV("extractAlbumArt");
276     Mutex::Autolock lock(mLock);
277     mAlbumArt.clear();
278     if (mRetriever == NULL) {
279         ALOGE("retriever is not initialized");
280         return NULL;
281     }
282     MediaAlbumArt *albumArt = mRetriever->extractAlbumArt();
283     if (albumArt == NULL) {
284         ALOGE("failed to extract an album art");
285         return NULL;
286     }
287     size_t size = sizeof(MediaAlbumArt) + albumArt->size();
288     sp<MemoryHeapBase> heap = new MemoryHeapBase(size, 0, "MetadataRetrieverClient");
289     if (heap == NULL) {
290         ALOGE("failed to create MemoryDealer object");
291         delete albumArt;
292         return NULL;
293     }
294     mAlbumArt = new MemoryBase(heap, 0, size);
295     if (mAlbumArt == NULL) {
296         ALOGE("not enough memory for MediaAlbumArt size=%zu", size);
297         delete albumArt;
298         return NULL;
299     }
300     MediaAlbumArt::init((MediaAlbumArt *) mAlbumArt->pointer(),
301                         albumArt->size(), albumArt->data());
302     delete albumArt;  // We've taken our copy.
303     return mAlbumArt;
304 }
305 
extractMetadata(int keyCode)306 const char* MetadataRetrieverClient::extractMetadata(int keyCode)
307 {
308     ALOGV("extractMetadata");
309     Mutex::Autolock lock(mLock);
310     if (mRetriever == NULL) {
311         ALOGE("retriever is not initialized");
312         return NULL;
313     }
314     return mRetriever->extractMetadata(keyCode);
315 }
316 
317 }; // namespace android
318