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