stagefright.cpp revision 66d6f1fcd9cb80a603b833e93779eb0dfb5e67ee
1952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen/*
2952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen * Copyright (C) 2009 The Android Open Source Project
3952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen *
4952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen * Licensed under the Apache License, Version 2.0 (the "License");
5952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen * you may not use this file except in compliance with the License.
6952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen * You may obtain a copy of the License at
7952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen *
8952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen *      http://www.apache.org/licenses/LICENSE-2.0
964b4f192a8e40b2a1e3573227522ee1de508c5b9Johnny Chen *
1064b4f192a8e40b2a1e3573227522ee1de508c5b9Johnny Chen * Unless required by applicable law or agreed to in writing, software
1164b4f192a8e40b2a1e3573227522ee1de508c5b9Johnny Chen * distributed under the License is distributed on an "AS IS" BASIS,
1264b4f192a8e40b2a1e3573227522ee1de508c5b9Johnny Chen * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen * See the License for the specific language governing permissions and
14952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen * limitations under the License.
153a9c0967d3987d50e5f0a30fbbcdfce2e9c7dccaJohnny Chen */
16952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
17952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen#include <sys/time.h>
1864b4f192a8e40b2a1e3573227522ee1de508c5b9Johnny Chen
1964b4f192a8e40b2a1e3573227522ee1de508c5b9Johnny Chen#include <stdlib.h>
2064b4f192a8e40b2a1e3573227522ee1de508c5b9Johnny Chen#include <string.h>
2164b4f192a8e40b2a1e3573227522ee1de508c5b9Johnny Chen#include <unistd.h>
2264b4f192a8e40b2a1e3573227522ee1de508c5b9Johnny Chen
2364b4f192a8e40b2a1e3573227522ee1de508c5b9Johnny Chen#include "SineSource.h"
24952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
2564b4f192a8e40b2a1e3573227522ee1de508c5b9Johnny Chen#include <binder/IServiceManager.h>
2664b4f192a8e40b2a1e3573227522ee1de508c5b9Johnny Chen#include <binder/ProcessState.h>
2764b4f192a8e40b2a1e3573227522ee1de508c5b9Johnny Chen#include <media/IMediaPlayerService.h>
2864b4f192a8e40b2a1e3573227522ee1de508c5b9Johnny Chen#include <media/stagefright/AudioPlayer.h>
2964b4f192a8e40b2a1e3573227522ee1de508c5b9Johnny Chen#include <media/stagefright/DataSource.h>
3064b4f192a8e40b2a1e3573227522ee1de508c5b9Johnny Chen#include <media/stagefright/JPEGSource.h>
3164b4f192a8e40b2a1e3573227522ee1de508c5b9Johnny Chen#include <media/stagefright/MediaDebug.h>
326033bedc163bc57b5e5c3c1aee8ba81f64b4f11eJohnny Chen#include <media/stagefright/MediaDefs.h>
33952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen#include <media/stagefright/MediaErrors.h>
34952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen#include <media/stagefright/MediaExtractor.h>
35952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen#include <media/stagefright/MediaSource.h>
365f2ed17114c1711a23011c160832b318d77db888Johnny Chen#include <media/stagefright/MetaData.h>
375f2ed17114c1711a23011c160832b318d77db888Johnny Chen#include <media/stagefright/OMXClient.h>
385f2ed17114c1711a23011c160832b318d77db888Johnny Chen#include <media/stagefright/OMXCodec.h>
39952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen#include <media/mediametadataretriever.h>
40952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
41952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen#include <media/stagefright/foundation/hexdump.h>
42952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen#include <media/stagefright/MPEG4Writer.h>
43952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
44c6e328a322123a276524857242f99e019e56a5deJohnny Chenusing namespace android;
45c6e328a322123a276524857242f99e019e56a5deJohnny Chen
46c6e328a322123a276524857242f99e019e56a5deJohnny Chenstatic long gNumRepetitions;
47c6e328a322123a276524857242f99e019e56a5deJohnny Chenstatic long gMaxNumFrames;  // 0 means decode all available.
485f2ed17114c1711a23011c160832b318d77db888Johnny Chenstatic long gReproduceBug;  // if not -1.
49952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chenstatic bool gPreferSoftwareCodec;
505f2ed17114c1711a23011c160832b318d77db888Johnny Chenstatic bool gPlaybackAudio;
51952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chenstatic bool gWriteMP4;
52952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chenstatic String8 gWriteMP4Filename;
53952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
54952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chenstatic int64_t getNowUs() {
55952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen    struct timeval tv;
56952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen    gettimeofday(&tv, NULL);
57952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
58c6e328a322123a276524857242f99e019e56a5deJohnny Chen    return (int64_t)tv.tv_usec + tv.tv_sec * 1000000ll;
59c6e328a322123a276524857242f99e019e56a5deJohnny Chen}
60c6e328a322123a276524857242f99e019e56a5deJohnny Chen
61c6e328a322123a276524857242f99e019e56a5deJohnny Chenstatic void playSource(OMXClient *client, const sp<MediaSource> &source) {
625f2ed17114c1711a23011c160832b318d77db888Johnny Chen    sp<MetaData> meta = source->getFormat();
63952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
645f2ed17114c1711a23011c160832b318d77db888Johnny Chen    const char *mime;
65952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen    CHECK(meta->findCString(kKeyMIMEType, &mime));
66952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
67952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen    sp<MediaSource> rawSource;
68952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen    if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_RAW, mime)) {
69952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen        rawSource = source;
70952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen    } else {
71952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen        rawSource = OMXCodec::Create(
72952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen            client->interface(), meta, false /* createEncoder */, source,
73952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen            NULL /* matchComponentName */,
747d7f447af7fba3eae442ab99c18f5ae532886099Johnny Chen            gPreferSoftwareCodec ? OMXCodec::kPreferSoftwareCodecs : 0);
75952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
76952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen        if (rawSource == NULL) {
77952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen            fprintf(stderr, "Failed to instantiate decoder for '%s'.\n", mime);
78952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen            return;
79952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen        }
80952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen    }
81952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
82952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen    status_t err = rawSource->start();
83952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
84952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen    if (err != OK) {
85952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen        fprintf(stderr, "rawSource returned error %d (0x%08x)\n", err, err);
86952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen        return;
87952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen    }
88952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
89952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen    if (gPlaybackAudio) {
90952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen        AudioPlayer *player = new AudioPlayer(NULL);
91952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen        player->setSource(rawSource);
92952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen        rawSource.clear();
93952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
94952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen        player->start(true /* sourceAlreadyStarted */);
95952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
96952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen        status_t finalStatus;
97952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen        while (!player->reachedEOS(&finalStatus)) {
98952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen            usleep(100000ll);
99952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen        }
100952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
101952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen        delete player;
102952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen        player = NULL;
103952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
104952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen        return;
105952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen    } else if (gReproduceBug >= 3 && gReproduceBug <= 5) {
106952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen        int64_t durationUs;
107952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen        CHECK(meta->findInt64(kKeyDuration, &durationUs));
108952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
109952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen        status_t err;
110952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen        MediaBuffer *buffer;
111952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen        MediaSource::ReadOptions options;
112952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen        int64_t seekTimeUs = -1;
113952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen        for (;;) {
11464b4f192a8e40b2a1e3573227522ee1de508c5b9Johnny Chen            err = rawSource->read(&buffer, &options);
115952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen            options.clearSeekTo();
116952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
117952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen            bool shouldSeek = false;
118952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen            if (err == INFO_FORMAT_CHANGED) {
119952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen                CHECK_EQ(buffer, NULL);
120952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
121952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen                printf("format changed.\n");
122952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen                continue;
123952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen            } else if (err != OK) {
124952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen                printf("reached EOF.\n");
125952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
126952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen                shouldSeek = true;
127952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen            } else {
128952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen                int64_t timestampUs;
129952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen                CHECK(buffer->meta_data()->findInt64(kKeyTime, &timestampUs));
130952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
131952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen                bool failed = false;
132952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
133952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen                if (seekTimeUs >= 0) {
134952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen                    int64_t diff = timestampUs - seekTimeUs;
135952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
136952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen                    if (diff < 0) {
137952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen                        diff = -diff;
138952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen                    }
139952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
140952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen                    if ((gReproduceBug == 4 && diff > 500000)
141952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen                        || (gReproduceBug == 5 && timestampUs < 0)) {
142952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen                        printf("wanted: %.2f secs, got: %.2f secs\n",
143952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen                               seekTimeUs / 1E6, timestampUs / 1E6);
144952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
145952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen                        printf("ERROR: ");
146952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen                        failed = true;
147952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen                    }
148952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen                }
149952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
150952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen                printf("buffer has timestamp %lld us (%.2f secs)\n",
151952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen                       timestampUs, timestampUs / 1E6);
152952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
153952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen                buffer->release();
154952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen                buffer = NULL;
155952cc2b9f1fdbf4919e06d559d8d0ae27b4bc538Johnny Chen
156                if (failed) {
157                    break;
158                }
159
160                shouldSeek = ((double)rand() / RAND_MAX) < 0.1;
161
162                if (gReproduceBug == 3) {
163                    shouldSeek = false;
164                }
165            }
166
167            seekTimeUs = -1;
168
169            if (shouldSeek) {
170                seekTimeUs = (rand() * (float)durationUs) / RAND_MAX;
171                options.setSeekTo(seekTimeUs);
172
173                printf("seeking to %lld us (%.2f secs)\n",
174                       seekTimeUs, seekTimeUs / 1E6);
175            }
176        }
177
178        rawSource->stop();
179
180        return;
181    }
182
183    int n = 0;
184    int64_t startTime = getNowUs();
185
186    long numIterationsLeft = gNumRepetitions;
187    MediaSource::ReadOptions options;
188
189    int64_t sumDecodeUs = 0;
190    int64_t totalBytes = 0;
191
192    while (numIterationsLeft-- > 0) {
193        long numFrames = 0;
194
195        MediaBuffer *buffer;
196
197        for (;;) {
198            int64_t startDecodeUs = getNowUs();
199            status_t err = rawSource->read(&buffer, &options);
200            int64_t delayDecodeUs = getNowUs() - startDecodeUs;
201
202            options.clearSeekTo();
203
204            if (err != OK) {
205                CHECK_EQ(buffer, NULL);
206
207                if (err == INFO_FORMAT_CHANGED) {
208                    printf("format changed.\n");
209                    continue;
210                }
211
212                break;
213            }
214
215            if (buffer->range_length() > 0 && (n++ % 16) == 0) {
216                printf(".");
217                fflush(stdout);
218            }
219
220            sumDecodeUs += delayDecodeUs;
221            totalBytes += buffer->range_length();
222
223            buffer->release();
224            buffer = NULL;
225
226            ++numFrames;
227            if (gMaxNumFrames > 0 && numFrames == gMaxNumFrames) {
228                break;
229            }
230
231            if (gReproduceBug == 1 && numFrames == 40) {
232                printf("seeking past the end now.");
233                options.setSeekTo(0x7fffffffL);
234            } else if (gReproduceBug == 2 && numFrames == 40) {
235                printf("seeking to 5 secs.");
236                options.setSeekTo(5000000);
237            }
238        }
239
240        printf("$");
241        fflush(stdout);
242
243        options.setSeekTo(0);
244    }
245
246    rawSource->stop();
247    printf("\n");
248
249    int64_t delay = getNowUs() - startTime;
250    if (!strncasecmp("video/", mime, 6)) {
251        printf("avg. %.2f fps\n", n * 1E6 / delay);
252
253        printf("avg. time to decode one buffer %.2f usecs\n",
254               (double)sumDecodeUs / n);
255
256        printf("decoded a total of %d frame(s).\n", n);
257    } else if (!strncasecmp("audio/", mime, 6)) {
258        // Frame count makes less sense for audio, as the output buffer
259        // sizes may be different across decoders.
260        printf("avg. %.2f KB/sec\n", totalBytes / 1024 * 1E6 / delay);
261
262        printf("decoded a total of %lld bytes\n", totalBytes);
263    }
264}
265
266static void writeSourceToMP4(const sp<MediaSource> &source) {
267    sp<MPEG4Writer> writer =
268        new MPEG4Writer(gWriteMP4Filename.string());
269
270    CHECK_EQ(writer->addSource(source), OK);
271
272    sp<MetaData> params = new MetaData;
273    CHECK_EQ(writer->start(), OK);
274
275    while (!writer->reachedEOS()) {
276        usleep(100000);
277    }
278    writer->stop();
279}
280
281static void performSeekTest(const sp<MediaSource> &source) {
282    CHECK_EQ(OK, source->start());
283
284    int64_t durationUs;
285    CHECK(source->getFormat()->findInt64(kKeyDuration, &durationUs));
286
287    for (int64_t seekTimeUs = 0; seekTimeUs <= durationUs;
288            seekTimeUs += 60000ll) {
289        MediaSource::ReadOptions options;
290        options.setSeekTo(
291                seekTimeUs, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC);
292
293        MediaBuffer *buffer;
294        status_t err;
295        for (;;) {
296            err = source->read(&buffer, &options);
297
298            options.clearSeekTo();
299
300            if (err == INFO_FORMAT_CHANGED) {
301                CHECK(buffer == NULL);
302                continue;
303            }
304
305            if (err != OK) {
306                CHECK(buffer == NULL);
307                break;
308            }
309
310            if (buffer->range_length() > 0) {
311                break;
312            }
313
314            CHECK(buffer != NULL);
315
316            buffer->release();
317            buffer = NULL;
318        }
319
320        if (err == OK) {
321            int64_t timeUs;
322            CHECK(buffer->meta_data()->findInt64(kKeyTime, &timeUs));
323
324            printf("%lld\t%lld\t%lld\n", seekTimeUs, timeUs, seekTimeUs - timeUs);
325
326            buffer->release();
327            buffer = NULL;
328        } else {
329            printf("ERROR\n");
330            break;
331        }
332    }
333
334    CHECK_EQ(OK, source->stop());
335}
336
337static void usage(const char *me) {
338    fprintf(stderr, "usage: %s\n", me);
339    fprintf(stderr, "       -h(elp)\n");
340    fprintf(stderr, "       -a(udio)\n");
341    fprintf(stderr, "       -n repetitions\n");
342    fprintf(stderr, "       -l(ist) components\n");
343    fprintf(stderr, "       -m max-number-of-frames-to-decode in each pass\n");
344    fprintf(stderr, "       -b bug to reproduce\n");
345    fprintf(stderr, "       -p(rofiles) dump decoder profiles supported\n");
346    fprintf(stderr, "       -t(humbnail) extract video thumbnail or album art\n");
347    fprintf(stderr, "       -s(oftware) prefer software codec\n");
348    fprintf(stderr, "       -o playback audio\n");
349    fprintf(stderr, "       -w(rite) filename (write to .mp4 file)\n");
350    fprintf(stderr, "       -k seek test\n");
351}
352
353int main(int argc, char **argv) {
354    android::ProcessState::self()->startThreadPool();
355
356    bool audioOnly = false;
357    bool listComponents = false;
358    bool dumpProfiles = false;
359    bool extractThumbnail = false;
360    bool seekTest = false;
361    gNumRepetitions = 1;
362    gMaxNumFrames = 0;
363    gReproduceBug = -1;
364    gPreferSoftwareCodec = false;
365    gPlaybackAudio = false;
366    gWriteMP4 = false;
367
368    int res;
369    while ((res = getopt(argc, argv, "han:lm:b:ptsow:k")) >= 0) {
370        switch (res) {
371            case 'a':
372            {
373                audioOnly = true;
374                break;
375            }
376
377            case 'l':
378            {
379                listComponents = true;
380                break;
381            }
382
383            case 'm':
384            case 'n':
385            case 'b':
386            {
387                char *end;
388                long x = strtol(optarg, &end, 10);
389
390                if (*end != '\0' || end == optarg || x <= 0) {
391                    x = 1;
392                }
393
394                if (res == 'n') {
395                    gNumRepetitions = x;
396                } else if (res == 'm') {
397                    gMaxNumFrames = x;
398                } else {
399                    CHECK_EQ(res, 'b');
400                    gReproduceBug = x;
401                }
402                break;
403            }
404
405            case 'w':
406            {
407                gWriteMP4 = true;
408                gWriteMP4Filename.setTo(optarg);
409                break;
410            }
411
412            case 'p':
413            {
414                dumpProfiles = true;
415                break;
416            }
417
418            case 't':
419            {
420                extractThumbnail = true;
421                break;
422            }
423
424            case 's':
425            {
426                gPreferSoftwareCodec = true;
427                break;
428            }
429
430            case 'o':
431            {
432                gPlaybackAudio = true;
433                break;
434            }
435
436            case 'k':
437            {
438                seekTest = true;
439                break;
440            }
441
442            case '?':
443            case 'h':
444            default:
445            {
446                usage(argv[0]);
447                exit(1);
448                break;
449            }
450        }
451    }
452
453    if (gPlaybackAudio && !audioOnly) {
454        // This doesn't make any sense if we're decoding the video track.
455        gPlaybackAudio = false;
456    }
457
458    argc -= optind;
459    argv += optind;
460
461    if (extractThumbnail) {
462        sp<IServiceManager> sm = defaultServiceManager();
463        sp<IBinder> binder = sm->getService(String16("media.player"));
464        sp<IMediaPlayerService> service =
465            interface_cast<IMediaPlayerService>(binder);
466
467        CHECK(service.get() != NULL);
468
469        sp<IMediaMetadataRetriever> retriever =
470            service->createMetadataRetriever(getpid());
471
472        CHECK(retriever != NULL);
473
474        for (int k = 0; k < argc; ++k) {
475            const char *filename = argv[k];
476
477            CHECK_EQ(retriever->setDataSource(filename), OK);
478            CHECK_EQ(retriever->setMode(
479                        METADATA_MODE_FRAME_CAPTURE_AND_METADATA_RETRIEVAL),
480                     OK);
481
482            sp<IMemory> mem = retriever->captureFrame();
483
484            if (mem != NULL) {
485                printf("captureFrame(%s) => OK\n", filename);
486            } else {
487                mem = retriever->extractAlbumArt();
488
489                if (mem != NULL) {
490                    printf("extractAlbumArt(%s) => OK\n", filename);
491                } else {
492                    printf("both captureFrame and extractAlbumArt "
493                           "failed on file '%s'.\n", filename);
494                }
495            }
496        }
497
498        return 0;
499    }
500
501    if (dumpProfiles) {
502        sp<IServiceManager> sm = defaultServiceManager();
503        sp<IBinder> binder = sm->getService(String16("media.player"));
504        sp<IMediaPlayerService> service =
505            interface_cast<IMediaPlayerService>(binder);
506
507        CHECK(service.get() != NULL);
508
509        sp<IOMX> omx = service->getOMX();
510        CHECK(omx.get() != NULL);
511
512        const char *kMimeTypes[] = {
513            MEDIA_MIMETYPE_VIDEO_AVC, MEDIA_MIMETYPE_VIDEO_MPEG4,
514            MEDIA_MIMETYPE_VIDEO_H263, MEDIA_MIMETYPE_AUDIO_AAC,
515            MEDIA_MIMETYPE_AUDIO_AMR_NB, MEDIA_MIMETYPE_AUDIO_AMR_WB,
516            MEDIA_MIMETYPE_AUDIO_MPEG
517        };
518
519        for (size_t k = 0; k < sizeof(kMimeTypes) / sizeof(kMimeTypes[0]);
520             ++k) {
521            printf("type '%s':\n", kMimeTypes[k]);
522
523            Vector<CodecCapabilities> results;
524            CHECK_EQ(QueryCodecs(omx, kMimeTypes[k],
525                                 true, // queryDecoders
526                                 &results), OK);
527
528            for (size_t i = 0; i < results.size(); ++i) {
529                printf("  decoder '%s' supports ",
530                       results[i].mComponentName.string());
531
532                if (results[i].mProfileLevels.size() == 0) {
533                    printf("NOTHING.\n");
534                    continue;
535                }
536
537                for (size_t j = 0; j < results[i].mProfileLevels.size(); ++j) {
538                    const CodecProfileLevel &profileLevel =
539                        results[i].mProfileLevels[j];
540
541                    printf("%s%ld/%ld", j > 0 ? ", " : "",
542                           profileLevel.mProfile, profileLevel.mLevel);
543                }
544
545                printf("\n");
546            }
547        }
548    }
549
550    if (listComponents) {
551        sp<IServiceManager> sm = defaultServiceManager();
552        sp<IBinder> binder = sm->getService(String16("media.player"));
553        sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder);
554
555        CHECK(service.get() != NULL);
556
557        sp<IOMX> omx = service->getOMX();
558        CHECK(omx.get() != NULL);
559
560        List<IOMX::ComponentInfo> list;
561        omx->listNodes(&list);
562
563        for (List<IOMX::ComponentInfo>::iterator it = list.begin();
564             it != list.end(); ++it) {
565            printf("%s\n", (*it).mName.string());
566        }
567    }
568
569    DataSource::RegisterDefaultSniffers();
570
571    OMXClient client;
572    status_t err = client.connect();
573
574    for (int k = 0; k < argc; ++k) {
575        const char *filename = argv[k];
576
577        sp<DataSource> dataSource = DataSource::CreateFromURI(filename);
578
579        if (strncasecmp(filename, "sine:", 5) && dataSource == NULL) {
580            fprintf(stderr, "Unable to create data source.\n");
581            return 1;
582        }
583
584        bool isJPEG = false;
585
586        size_t len = strlen(filename);
587        if (len >= 4 && !strcasecmp(filename + len - 4, ".jpg")) {
588            isJPEG = true;
589        }
590
591        sp<MediaSource> mediaSource;
592
593        if (isJPEG) {
594            mediaSource = new JPEGSource(dataSource);
595        } else if (!strncasecmp("sine:", filename, 5)) {
596            char *end;
597            long sampleRate = strtol(filename + 5, &end, 10);
598
599            if (end == filename + 5) {
600                sampleRate = 44100;
601            }
602            mediaSource = new SineSource(sampleRate, 1);
603        } else {
604            sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource);
605            if (extractor == NULL) {
606                fprintf(stderr, "could not create extractor.\n");
607                return -1;
608            }
609
610            size_t numTracks = extractor->countTracks();
611
612            sp<MetaData> meta;
613            size_t i;
614            for (i = 0; i < numTracks; ++i) {
615                meta = extractor->getTrackMetaData(
616                        i, MediaExtractor::kIncludeExtensiveMetaData);
617
618                const char *mime;
619                meta->findCString(kKeyMIMEType, &mime);
620
621                if (audioOnly && !strncasecmp(mime, "audio/", 6)) {
622                    break;
623                }
624
625                if (!audioOnly && !strncasecmp(mime, "video/", 6)) {
626                    break;
627                }
628
629                meta = NULL;
630            }
631
632            if (meta == NULL) {
633                fprintf(stderr,
634                        "No suitable %s track found. The '-a' option will "
635                        "target audio tracks only, the default is to target "
636                        "video tracks only.\n",
637                        audioOnly ? "audio" : "video");
638                return -1;
639            }
640
641            int64_t thumbTimeUs;
642            if (meta->findInt64(kKeyThumbnailTime, &thumbTimeUs)) {
643                printf("thumbnailTime: %lld us (%.2f secs)\n",
644                       thumbTimeUs, thumbTimeUs / 1E6);
645            }
646
647            mediaSource = extractor->getTrack(i);
648        }
649
650        if (gWriteMP4) {
651            writeSourceToMP4(mediaSource);
652        } else if (seekTest) {
653            performSeekTest(mediaSource);
654        } else {
655            playSource(&client, mediaSource);
656        }
657    }
658
659    client.disconnect();
660
661    return 0;
662}
663