stagefright.cpp revision 18291bc20e55e8f3fd5feb786771a8ed32c19c59
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <sys/time.h>
18
19#include <stdlib.h>
20#include <string.h>
21#include <unistd.h>
22
23#include <binder/IServiceManager.h>
24#include <binder/ProcessState.h>
25#include <media/IMediaPlayerService.h>
26#include <media/stagefright/CachingDataSource.h>
27#include <media/stagefright/HTTPDataSource.h>
28#include <media/stagefright/MediaDebug.h>
29#include <media/stagefright/MediaDefs.h>
30#include <media/stagefright/MediaPlayerImpl.h>
31#include <media/stagefright/MediaExtractor.h>
32#include <media/stagefright/MediaSource.h>
33#include <media/stagefright/MetaData.h>
34#include <media/stagefright/MmapSource.h>
35#include <media/stagefright/OMXClient.h>
36#include <media/stagefright/OMXCodec.h>
37
38#include "JPEGSource.h"
39
40using namespace android;
41
42static long gNumRepetitions;
43static long gMaxNumFrames;  // 0 means decode all available.
44static long gReproduceBug;  // if not -1.
45
46static int64_t getNowUs() {
47    struct timeval tv;
48    gettimeofday(&tv, NULL);
49
50    return (int64_t)tv.tv_usec + tv.tv_sec * 1000000;
51}
52
53static void playSource(OMXClient *client, const sp<MediaSource> &source) {
54    sp<MetaData> meta = source->getFormat();
55
56    sp<OMXCodec> decoder = OMXCodec::Create(
57            client->interface(), meta, false /* createEncoder */, source);
58
59    if (decoder == NULL) {
60        return;
61    }
62
63    decoder->start();
64
65    int n = 0;
66    int64_t startTime = getNowUs();
67
68    long numIterationsLeft = gNumRepetitions;
69    MediaSource::ReadOptions options;
70
71    while (numIterationsLeft-- > 0) {
72        long numFrames = 0;
73
74        MediaBuffer *buffer;
75
76        for (;;) {
77            status_t err = decoder->read(&buffer, &options);
78            options.clearSeekTo();
79
80            if (err != OK) {
81                CHECK_EQ(buffer, NULL);
82                break;
83            }
84
85            if ((n++ % 16) == 0) {
86                printf(".");
87                fflush(stdout);
88            }
89
90            buffer->release();
91            buffer = NULL;
92
93            ++numFrames;
94            if (gMaxNumFrames > 0 && numFrames == gMaxNumFrames) {
95                break;
96            }
97
98            if (gReproduceBug == 1 && numFrames == 40) {
99                printf("seeking past the end now.");
100                options.setSeekTo(0x7fffffffL);
101            } else if (gReproduceBug == 2 && numFrames == 40) {
102                printf("seeking to 5 secs.");
103                options.setSeekTo(5000000);
104            }
105        }
106
107        printf("$");
108        fflush(stdout);
109
110        options.setSeekTo(0);
111    }
112
113    decoder->stop();
114    printf("\n");
115
116    int64_t delay = getNowUs() - startTime;
117    printf("avg. %.2f fps\n", n * 1E6 / delay);
118
119    printf("decoded a total of %d frame(s).\n", n);
120}
121
122static void usage(const char *me) {
123    fprintf(stderr, "usage: %s\n", me);
124    fprintf(stderr, "       -h(elp)\n");
125    fprintf(stderr, "       -a(udio)\n");
126    fprintf(stderr, "       -n repetitions\n");
127    fprintf(stderr, "       -l(ist) components\n");
128    fprintf(stderr, "       -m max-number-of-frames-to-decode in each pass\n");
129    fprintf(stderr, "       -b bug to reproduce\n");
130    fprintf(stderr, "       -p(rofiles) dump decoder profiles supported\n");
131}
132
133int main(int argc, char **argv) {
134    android::ProcessState::self()->startThreadPool();
135
136    bool audioOnly = false;
137    bool listComponents = false;
138    bool dumpProfiles = false;
139    gNumRepetitions = 1;
140    gMaxNumFrames = 0;
141    gReproduceBug = -1;
142
143    int res;
144    while ((res = getopt(argc, argv, "han:lm:b:p")) >= 0) {
145        switch (res) {
146            case 'a':
147            {
148                audioOnly = true;
149                break;
150            }
151
152            case 'l':
153            {
154                listComponents = true;
155                break;
156            }
157
158            case 'm':
159            case 'n':
160            case 'b':
161            {
162                char *end;
163                long x = strtol(optarg, &end, 10);
164
165                if (*end != '\0' || end == optarg || x <= 0) {
166                    x = 1;
167                }
168
169                if (res == 'n') {
170                    gNumRepetitions = x;
171                } else if (res == 'm') {
172                    gMaxNumFrames = x;
173                } else {
174                    CHECK_EQ(res, 'b');
175                    gReproduceBug = x;
176                }
177                break;
178            }
179
180            case 'p':
181            {
182                dumpProfiles = true;
183                break;
184            }
185
186            case '?':
187            case 'h':
188            default:
189            {
190                usage(argv[0]);
191                exit(1);
192                break;
193            }
194        }
195    }
196
197    argc -= optind;
198    argv += optind;
199
200    if (dumpProfiles) {
201        sp<IServiceManager> sm = defaultServiceManager();
202        sp<IBinder> binder = sm->getService(String16("media.player"));
203        sp<IMediaPlayerService> service =
204            interface_cast<IMediaPlayerService>(binder);
205
206        CHECK(service.get() != NULL);
207
208        sp<IOMX> omx = service->createOMX();
209        CHECK(omx.get() != NULL);
210
211        const char *kMimeTypes[] = {
212            MEDIA_MIMETYPE_VIDEO_AVC, MEDIA_MIMETYPE_VIDEO_MPEG4,
213            MEDIA_MIMETYPE_VIDEO_H263
214        };
215
216        for (size_t k = 0; k < sizeof(kMimeTypes) / sizeof(kMimeTypes[0]);
217             ++k) {
218            printf("type '%s':\n", kMimeTypes[k]);
219
220            Vector<CodecCapabilities> results;
221            CHECK_EQ(QueryCodecs(omx, kMimeTypes[k],
222                                 true, // queryDecoders
223                                 &results), OK);
224
225            for (size_t i = 0; i < results.size(); ++i) {
226                printf("  decoder '%s' supports ",
227                       results[i].mComponentName.string());
228
229                if (results[i].mProfileLevels.size() == 0) {
230                    printf("NOTHING.\n");
231                    continue;
232                }
233
234                for (size_t j = 0; j < results[i].mProfileLevels.size(); ++j) {
235                    const CodecProfileLevel &profileLevel =
236                        results[i].mProfileLevels[j];
237
238                    printf("%s%ld/%ld", j > 0 ? ", " : "",
239                           profileLevel.mProfile, profileLevel.mLevel);
240                }
241
242                printf("\n");
243            }
244        }
245    }
246
247    if (listComponents) {
248        sp<IServiceManager> sm = defaultServiceManager();
249        sp<IBinder> binder = sm->getService(String16("media.player"));
250        sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder);
251
252        CHECK(service.get() != NULL);
253
254        sp<IOMX> omx = service->createOMX();
255        CHECK(omx.get() != NULL);
256
257        List<String8> list;
258        omx->list_nodes(&list);
259
260        for (List<String8>::iterator it = list.begin();
261             it != list.end(); ++it) {
262            printf("%s\n", (*it).string());
263        }
264    }
265
266    DataSource::RegisterDefaultSniffers();
267
268    OMXClient client;
269    status_t err = client.connect();
270
271    for (int k = 0; k < argc; ++k) {
272        const char *filename = argv[k];
273
274        sp<DataSource> dataSource;
275        if (!strncasecmp("http://", filename, 7)) {
276            dataSource = new HTTPDataSource(filename);
277            dataSource = new CachingDataSource(dataSource, 64 * 1024, 10);
278        } else {
279            dataSource = new MmapSource(filename);
280        }
281
282        bool isJPEG = false;
283
284        size_t len = strlen(filename);
285        if (len >= 4 && !strcasecmp(filename + len - 4, ".jpg")) {
286            isJPEG = true;
287        }
288
289        sp<MediaSource> mediaSource;
290
291        if (isJPEG) {
292            mediaSource = new JPEGSource(dataSource);
293        } else {
294            sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource);
295
296            size_t numTracks = extractor->countTracks();
297
298            sp<MetaData> meta;
299            size_t i;
300            for (i = 0; i < numTracks; ++i) {
301                meta = extractor->getTrackMetaData(i);
302
303                const char *mime;
304                meta->findCString(kKeyMIMEType, &mime);
305
306                if (audioOnly && !strncasecmp(mime, "audio/", 6)) {
307                    break;
308                }
309
310                if (!audioOnly && !strncasecmp(mime, "video/", 6)) {
311                    break;
312                }
313            }
314
315            mediaSource = extractor->getTrack(i);
316        }
317
318        playSource(&client, mediaSource);
319    }
320
321    client.disconnect();
322
323    return 0;
324}
325