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