stagefright.cpp revision 80011fe130bc966aa357ed2b3dcc80cde2d0bb82
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/MediaPlayerImpl.h>
30#include <media/stagefright/MediaExtractor.h>
31#include <media/stagefright/MediaSource.h>
32#include <media/stagefright/MetaData.h>
33#include <media/stagefright/MmapSource.h>
34#include <media/stagefright/OMXClient.h>
35#include <media/stagefright/OMXCodec.h>
36#include <media/stagefright/OMXDecoder.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
53#define USE_OMX_CODEC   1
54
55static void playSource(OMXClient *client, const sp<MediaSource> &source) {
56    sp<MetaData> meta = source->getFormat();
57
58#if !USE_OMX_CODEC
59    sp<OMXDecoder> decoder = OMXDecoder::Create(
60            client, meta, false /* createEncoder */, source);
61#else
62    sp<OMXCodec> decoder = OMXCodec::Create(
63            client->interface(), meta, false /* createEncoder */, source);
64#endif
65
66    if (decoder == NULL) {
67        return;
68    }
69
70    decoder->start();
71
72    int n = 0;
73    int64_t startTime = getNowUs();
74
75    long numIterationsLeft = gNumRepetitions;
76    MediaSource::ReadOptions options;
77
78    while (numIterationsLeft-- > 0) {
79        long numFrames = 0;
80
81        MediaBuffer *buffer;
82
83        for (;;) {
84            status_t err = decoder->read(&buffer, &options);
85            options.clearSeekTo();
86
87            if (err != OK) {
88                CHECK_EQ(buffer, NULL);
89                break;
90            }
91
92            if ((n++ % 16) == 0) {
93                printf(".");
94                fflush(stdout);
95            }
96
97            buffer->release();
98            buffer = NULL;
99
100            ++numFrames;
101            if (gMaxNumFrames > 0 && numFrames == gMaxNumFrames) {
102                break;
103            }
104
105            if (gReproduceBug == 1 && numFrames == 40) {
106                printf("seeking past the end now.");
107                options.setSeekTo(LONG_MAX);
108            }
109        }
110
111        printf("$");
112        fflush(stdout);
113
114        options.setSeekTo(0);
115    }
116
117    decoder->stop();
118    printf("\n");
119
120    int64_t delay = getNowUs() - startTime;
121    printf("avg. %.2f fps\n", n * 1E6 / delay);
122
123    printf("decoded a total of %d frame(s).\n", n);
124}
125
126static void usage(const char *me) {
127    fprintf(stderr, "usage: %s\n", me);
128    fprintf(stderr, "       -h(elp)\n");
129    fprintf(stderr, "       -a(udio)\n");
130    fprintf(stderr, "       -n repetitions\n");
131    fprintf(stderr, "       -l(ist) components\n");
132    fprintf(stderr, "       -m max-number-of-frames-to-decode in each pass\n");
133    fprintf(stderr, "       -b bug to reproduce\n");
134}
135
136int main(int argc, char **argv) {
137    android::ProcessState::self()->startThreadPool();
138
139    bool audioOnly = false;
140    bool listComponents = false;
141    gNumRepetitions = 1;
142    gMaxNumFrames = 0;
143    gReproduceBug = -1;
144
145    int res;
146    while ((res = getopt(argc, argv, "han:lm:b:")) >= 0) {
147        switch (res) {
148            case 'a':
149            {
150                audioOnly = true;
151                break;
152            }
153
154            case 'l':
155            {
156                listComponents = true;
157                break;
158            }
159
160            case 'm':
161            case 'n':
162            case 'b':
163            {
164                char *end;
165                long x = strtol(optarg, &end, 10);
166
167                if (*end != '\0' || end == optarg || x <= 0) {
168                    x = 1;
169                }
170
171                if (res == 'n') {
172                    gNumRepetitions = x;
173                } else if (res == 'm') {
174                    gMaxNumFrames = x;
175                } else {
176                    CHECK_EQ(res, 'b');
177                    gReproduceBug = x;
178                }
179                break;
180            }
181
182            case '?':
183            case 'h':
184            default:
185            {
186                usage(argv[0]);
187                exit(1);
188                break;
189            }
190        }
191    }
192
193    argc -= optind;
194    argv += optind;
195
196    if (listComponents) {
197        sp<IServiceManager> sm = defaultServiceManager();
198        sp<IBinder> binder = sm->getService(String16("media.player"));
199        sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder);
200
201        CHECK(service.get() != NULL);
202
203        sp<IOMX> omx = service->createOMX();
204        CHECK(omx.get() != NULL);
205
206        List<String8> list;
207        omx->list_nodes(&list);
208
209        for (List<String8>::iterator it = list.begin();
210             it != list.end(); ++it) {
211            printf("%s\n", (*it).string());
212        }
213    }
214
215    DataSource::RegisterDefaultSniffers();
216
217    OMXClient client;
218    status_t err = client.connect();
219
220    for (int k = 0; k < argc; ++k) {
221        const char *filename = argv[k];
222
223        sp<DataSource> dataSource;
224        if (!strncasecmp("http://", filename, 7)) {
225            dataSource = new HTTPDataSource(filename);
226            dataSource = new CachingDataSource(dataSource, 64 * 1024, 10);
227        } else {
228            dataSource = new MmapSource(filename);
229        }
230
231        bool isJPEG = false;
232
233        size_t len = strlen(filename);
234        if (len >= 4 && !strcasecmp(filename + len - 4, ".jpg")) {
235            isJPEG = true;
236        }
237
238        sp<MediaSource> mediaSource;
239
240        if (isJPEG) {
241            mediaSource = new JPEGSource(dataSource);
242        } else {
243            sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource);
244
245            size_t numTracks = extractor->countTracks();
246
247            sp<MetaData> meta;
248            size_t i;
249            for (i = 0; i < numTracks; ++i) {
250                meta = extractor->getTrackMetaData(i);
251
252                const char *mime;
253                meta->findCString(kKeyMIMEType, &mime);
254
255                if (audioOnly && !strncasecmp(mime, "audio/", 6)) {
256                    break;
257                }
258
259                if (!audioOnly && !strncasecmp(mime, "video/", 6)) {
260                    break;
261                }
262            }
263
264            mediaSource = extractor->getTrack(i);
265        }
266
267        playSource(&client, mediaSource);
268    }
269
270    client.disconnect();
271
272    return 0;
273}
274