• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/IMediaHTTPService.h>
35 #include <media/MediaMetadataRetrieverInterface.h>
36 #include <media/MediaPlayerInterface.h>
37 #include <media/stagefright/DataSource.h>
38 #include <media/stagefright/Utils.h>
39 #include <private/media/VideoFrame.h>
40 #include "MetadataRetrieverClient.h"
41 #include "StagefrightMetadataRetriever.h"
42 #include "MediaPlayerFactory.h"
43 
44 namespace android {
45 
MetadataRetrieverClient(pid_t pid)46 MetadataRetrieverClient::MetadataRetrieverClient(pid_t pid)
47 {
48     ALOGV("MetadataRetrieverClient constructor pid(%d)", pid);
49     mPid = pid;
50     mThumbnail = NULL;
51     mAlbumArt = NULL;
52     mRetriever = NULL;
53 }
54 
~MetadataRetrieverClient()55 MetadataRetrieverClient::~MetadataRetrieverClient()
56 {
57     ALOGV("MetadataRetrieverClient destructor");
58     disconnect();
59 }
60 
dump(int fd,const Vector<String16> &)61 status_t MetadataRetrieverClient::dump(int fd, const Vector<String16>& /*args*/)
62 {
63     const size_t SIZE = 256;
64     char buffer[SIZE];
65     String8 result;
66     result.append(" MetadataRetrieverClient\n");
67     snprintf(buffer, 255, "  pid(%d)\n", mPid);
68     result.append(buffer);
69     write(fd, result.string(), result.size());
70     write(fd, "\n", 1);
71     return NO_ERROR;
72 }
73 
disconnect()74 void MetadataRetrieverClient::disconnect()
75 {
76     ALOGV("disconnect from pid %d", mPid);
77     Mutex::Autolock lock(mLock);
78     mRetriever.clear();
79     mThumbnail.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 = DataSource::CreateFromIDataSource(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     mThumbnail.clear();
204     if (mRetriever == NULL) {
205         ALOGE("retriever is not initialized");
206         return NULL;
207     }
208     VideoFrame *frame = mRetriever->getFrameAtTime(
209             timeUs, option, colorFormat, metaOnly);
210     if (frame == NULL) {
211         ALOGE("failed to capture a video frame");
212         return NULL;
213     }
214     size_t size = frame->getFlattenedSize();
215     sp<MemoryHeapBase> heap = new MemoryHeapBase(size, 0, "MetadataRetrieverClient");
216     if (heap == NULL) {
217         ALOGE("failed to create MemoryDealer");
218         delete frame;
219         return NULL;
220     }
221     mThumbnail = new MemoryBase(heap, 0, size);
222     if (mThumbnail == NULL) {
223         ALOGE("not enough memory for VideoFrame size=%zu", size);
224         delete frame;
225         return NULL;
226     }
227     VideoFrame *frameCopy = static_cast<VideoFrame *>(mThumbnail->pointer());
228     frameCopy->copyFlattened(*frame);
229     delete frame;  // Fix memory leakage
230     return mThumbnail;
231 }
232 
extractAlbumArt()233 sp<IMemory> MetadataRetrieverClient::extractAlbumArt()
234 {
235     ALOGV("extractAlbumArt");
236     Mutex::Autolock lock(mLock);
237     mAlbumArt.clear();
238     if (mRetriever == NULL) {
239         ALOGE("retriever is not initialized");
240         return NULL;
241     }
242     MediaAlbumArt *albumArt = mRetriever->extractAlbumArt();
243     if (albumArt == NULL) {
244         ALOGE("failed to extract an album art");
245         return NULL;
246     }
247     size_t size = sizeof(MediaAlbumArt) + albumArt->size();
248     sp<MemoryHeapBase> heap = new MemoryHeapBase(size, 0, "MetadataRetrieverClient");
249     if (heap == NULL) {
250         ALOGE("failed to create MemoryDealer object");
251         delete albumArt;
252         return NULL;
253     }
254     mAlbumArt = new MemoryBase(heap, 0, size);
255     if (mAlbumArt == NULL) {
256         ALOGE("not enough memory for MediaAlbumArt size=%zu", size);
257         delete albumArt;
258         return NULL;
259     }
260     MediaAlbumArt::init((MediaAlbumArt *) mAlbumArt->pointer(),
261                         albumArt->size(), albumArt->data());
262     delete albumArt;  // We've taken our copy.
263     return mAlbumArt;
264 }
265 
extractMetadata(int keyCode)266 const char* MetadataRetrieverClient::extractMetadata(int keyCode)
267 {
268     ALOGV("extractMetadata");
269     Mutex::Autolock lock(mLock);
270     if (mRetriever == NULL) {
271         ALOGE("retriever is not initialized");
272         return NULL;
273     }
274     return mRetriever->extractMetadata(keyCode);
275 }
276 
277 }; // namespace android
278