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