stagefright.cpp revision 48c948b1137e7bbdb161b51908657ab72ac5e2da
1ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang/*
2ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang * Copyright (C) 2009 The Android Open Source Project
31b362b15af34006e6a11974088a46d42b903418eJohann *
41b362b15af34006e6a11974088a46d42b903418eJohann * Licensed under the Apache License, Version 2.0 (the "License");
51b362b15af34006e6a11974088a46d42b903418eJohann * you may not use this file except in compliance with the License.
61b362b15af34006e6a11974088a46d42b903418eJohann * You may obtain a copy of the License at
71b362b15af34006e6a11974088a46d42b903418eJohann *
81b362b15af34006e6a11974088a46d42b903418eJohann *      http://www.apache.org/licenses/LICENSE-2.0
91b362b15af34006e6a11974088a46d42b903418eJohann *
10ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang * Unless required by applicable law or agreed to in writing, software
11ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang * distributed under the License is distributed on an "AS IS" BASIS,
12ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131b362b15af34006e6a11974088a46d42b903418eJohann * See the License for the specific language governing permissions and
141b362b15af34006e6a11974088a46d42b903418eJohann * limitations under the License.
151b362b15af34006e6a11974088a46d42b903418eJohann */
161b362b15af34006e6a11974088a46d42b903418eJohann
171b362b15af34006e6a11974088a46d42b903418eJohann#include <sys/time.h>
181b362b15af34006e6a11974088a46d42b903418eJohann
191b362b15af34006e6a11974088a46d42b903418eJohann#include <stdlib.h>
201b362b15af34006e6a11974088a46d42b903418eJohann#include <string.h>
211b362b15af34006e6a11974088a46d42b903418eJohann#include <unistd.h>
221b362b15af34006e6a11974088a46d42b903418eJohann
231b362b15af34006e6a11974088a46d42b903418eJohann#include <binder/IServiceManager.h>
241b362b15af34006e6a11974088a46d42b903418eJohann#include <binder/ProcessState.h>
25ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include <media/IMediaPlayerService.h>
26ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include <media/stagefright/CachingDataSource.h>
27ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include <media/stagefright/HTTPDataSource.h>
281b362b15af34006e6a11974088a46d42b903418eJohann#include <media/stagefright/JPEGSource.h>
291b362b15af34006e6a11974088a46d42b903418eJohann#include <media/stagefright/MediaDebug.h>
301b362b15af34006e6a11974088a46d42b903418eJohann#include <media/stagefright/MediaDefs.h>
311b362b15af34006e6a11974088a46d42b903418eJohann#include <media/stagefright/MediaPlayerImpl.h>
321b362b15af34006e6a11974088a46d42b903418eJohann#include <media/stagefright/MediaExtractor.h>
331b362b15af34006e6a11974088a46d42b903418eJohann#include <media/stagefright/MediaSource.h>
341b362b15af34006e6a11974088a46d42b903418eJohann#include <media/stagefright/MetaData.h>
351b362b15af34006e6a11974088a46d42b903418eJohann#include <media/stagefright/MmapSource.h>
361b362b15af34006e6a11974088a46d42b903418eJohann#include <media/stagefright/OMXClient.h>
371b362b15af34006e6a11974088a46d42b903418eJohann#include <media/stagefright/OMXCodec.h>
381b362b15af34006e6a11974088a46d42b903418eJohann
391b362b15af34006e6a11974088a46d42b903418eJohannusing namespace android;
401b362b15af34006e6a11974088a46d42b903418eJohann
411b362b15af34006e6a11974088a46d42b903418eJohannstatic long gNumRepetitions;
421b362b15af34006e6a11974088a46d42b903418eJohannstatic long gMaxNumFrames;  // 0 means decode all available.
431b362b15af34006e6a11974088a46d42b903418eJohannstatic long gReproduceBug;  // if not -1.
441b362b15af34006e6a11974088a46d42b903418eJohann
451b362b15af34006e6a11974088a46d42b903418eJohannstatic int64_t getNowUs() {
461b362b15af34006e6a11974088a46d42b903418eJohann    struct timeval tv;
471b362b15af34006e6a11974088a46d42b903418eJohann    gettimeofday(&tv, NULL);
481b362b15af34006e6a11974088a46d42b903418eJohann
491b362b15af34006e6a11974088a46d42b903418eJohann    return (int64_t)tv.tv_usec + tv.tv_sec * 1000000;
501b362b15af34006e6a11974088a46d42b903418eJohann}
511b362b15af34006e6a11974088a46d42b903418eJohann
521b362b15af34006e6a11974088a46d42b903418eJohannstatic void playSource(OMXClient *client, const sp<MediaSource> &source) {
531b362b15af34006e6a11974088a46d42b903418eJohann    sp<MetaData> meta = source->getFormat();
541b362b15af34006e6a11974088a46d42b903418eJohann
551b362b15af34006e6a11974088a46d42b903418eJohann    sp<OMXCodec> decoder = OMXCodec::Create(
561b362b15af34006e6a11974088a46d42b903418eJohann            client->interface(), meta, false /* createEncoder */, source);
571b362b15af34006e6a11974088a46d42b903418eJohann
581b362b15af34006e6a11974088a46d42b903418eJohann    if (decoder == NULL) {
591b362b15af34006e6a11974088a46d42b903418eJohann        return;
601b362b15af34006e6a11974088a46d42b903418eJohann    }
611b362b15af34006e6a11974088a46d42b903418eJohann
621b362b15af34006e6a11974088a46d42b903418eJohann    decoder->start();
631b362b15af34006e6a11974088a46d42b903418eJohann
641b362b15af34006e6a11974088a46d42b903418eJohann    if (gReproduceBug == 3) {
651b362b15af34006e6a11974088a46d42b903418eJohann        status_t err;
661b362b15af34006e6a11974088a46d42b903418eJohann        MediaBuffer *buffer;
671b362b15af34006e6a11974088a46d42b903418eJohann        MediaSource::ReadOptions options;
681b362b15af34006e6a11974088a46d42b903418eJohann        int64_t seekTimeUs = -1;
691b362b15af34006e6a11974088a46d42b903418eJohann        for (;;) {
701b362b15af34006e6a11974088a46d42b903418eJohann            err = decoder->read(&buffer, &options);
711b362b15af34006e6a11974088a46d42b903418eJohann            options.clearSeekTo();
721b362b15af34006e6a11974088a46d42b903418eJohann
731b362b15af34006e6a11974088a46d42b903418eJohann            bool shouldSeek = false;
741b362b15af34006e6a11974088a46d42b903418eJohann            if (err != OK) {
751b362b15af34006e6a11974088a46d42b903418eJohann                printf("reached EOF.\n");
761b362b15af34006e6a11974088a46d42b903418eJohann
771b362b15af34006e6a11974088a46d42b903418eJohann                shouldSeek = true;
781b362b15af34006e6a11974088a46d42b903418eJohann            } else {
791b362b15af34006e6a11974088a46d42b903418eJohann                int64_t timestampUs;
801b362b15af34006e6a11974088a46d42b903418eJohann                CHECK(buffer->meta_data()->findInt64(kKeyTime, &timestampUs));
811b362b15af34006e6a11974088a46d42b903418eJohann
821b362b15af34006e6a11974088a46d42b903418eJohann                bool failed = false;
831b362b15af34006e6a11974088a46d42b903418eJohann                if (seekTimeUs >= 0) {
841b362b15af34006e6a11974088a46d42b903418eJohann                    int64_t diff = timestampUs - seekTimeUs;
851b362b15af34006e6a11974088a46d42b903418eJohann
861b362b15af34006e6a11974088a46d42b903418eJohann                    if (diff > 500000) {
871b362b15af34006e6a11974088a46d42b903418eJohann                        printf("ERROR: ");
881b362b15af34006e6a11974088a46d42b903418eJohann                        failed = true;
891b362b15af34006e6a11974088a46d42b903418eJohann                    }
901b362b15af34006e6a11974088a46d42b903418eJohann                }
91ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
921b362b15af34006e6a11974088a46d42b903418eJohann                printf("buffer has timestamp %lld us (%.2f secs)\n",
931b362b15af34006e6a11974088a46d42b903418eJohann                       timestampUs, timestampUs / 1E6);
941b362b15af34006e6a11974088a46d42b903418eJohann
951b362b15af34006e6a11974088a46d42b903418eJohann                buffer->release();
961b362b15af34006e6a11974088a46d42b903418eJohann                buffer = NULL;
971b362b15af34006e6a11974088a46d42b903418eJohann
981b362b15af34006e6a11974088a46d42b903418eJohann                if (failed) {
991b362b15af34006e6a11974088a46d42b903418eJohann                    break;
1001b362b15af34006e6a11974088a46d42b903418eJohann                }
1011b362b15af34006e6a11974088a46d42b903418eJohann
1021b362b15af34006e6a11974088a46d42b903418eJohann                shouldSeek = ((double)rand() / RAND_MAX) < 0.1;
1031b362b15af34006e6a11974088a46d42b903418eJohann                shouldSeek = false;
1041b362b15af34006e6a11974088a46d42b903418eJohann            }
1051b362b15af34006e6a11974088a46d42b903418eJohann
1061b362b15af34006e6a11974088a46d42b903418eJohann            seekTimeUs = -1;
1071b362b15af34006e6a11974088a46d42b903418eJohann
1081b362b15af34006e6a11974088a46d42b903418eJohann            if (shouldSeek) {
1091b362b15af34006e6a11974088a46d42b903418eJohann                seekTimeUs = (rand() * 30E6) / RAND_MAX;
1101b362b15af34006e6a11974088a46d42b903418eJohann                options.setSeekTo(seekTimeUs);
1111b362b15af34006e6a11974088a46d42b903418eJohann
1121b362b15af34006e6a11974088a46d42b903418eJohann                printf("seeking to %lld us (%.2f secs)\n",
1131b362b15af34006e6a11974088a46d42b903418eJohann                       seekTimeUs, seekTimeUs / 1E6);
1141b362b15af34006e6a11974088a46d42b903418eJohann            }
1151b362b15af34006e6a11974088a46d42b903418eJohann        }
1161b362b15af34006e6a11974088a46d42b903418eJohann
1171b362b15af34006e6a11974088a46d42b903418eJohann        decoder->stop();
1181b362b15af34006e6a11974088a46d42b903418eJohann
1191b362b15af34006e6a11974088a46d42b903418eJohann        return;
1201b362b15af34006e6a11974088a46d42b903418eJohann    }
1211b362b15af34006e6a11974088a46d42b903418eJohann
1221b362b15af34006e6a11974088a46d42b903418eJohann    int n = 0;
1231b362b15af34006e6a11974088a46d42b903418eJohann    int64_t startTime = getNowUs();
1241b362b15af34006e6a11974088a46d42b903418eJohann
1251b362b15af34006e6a11974088a46d42b903418eJohann    long numIterationsLeft = gNumRepetitions;
1261b362b15af34006e6a11974088a46d42b903418eJohann    MediaSource::ReadOptions options;
1271b362b15af34006e6a11974088a46d42b903418eJohann
1281b362b15af34006e6a11974088a46d42b903418eJohann    while (numIterationsLeft-- > 0) {
1291b362b15af34006e6a11974088a46d42b903418eJohann        long numFrames = 0;
1301b362b15af34006e6a11974088a46d42b903418eJohann
1311b362b15af34006e6a11974088a46d42b903418eJohann        MediaBuffer *buffer;
1321b362b15af34006e6a11974088a46d42b903418eJohann
1331b362b15af34006e6a11974088a46d42b903418eJohann        for (;;) {
1341b362b15af34006e6a11974088a46d42b903418eJohann            status_t err = decoder->read(&buffer, &options);
1351b362b15af34006e6a11974088a46d42b903418eJohann            options.clearSeekTo();
1361b362b15af34006e6a11974088a46d42b903418eJohann
1371b362b15af34006e6a11974088a46d42b903418eJohann            if (err != OK) {
1381b362b15af34006e6a11974088a46d42b903418eJohann                CHECK_EQ(buffer, NULL);
1391b362b15af34006e6a11974088a46d42b903418eJohann                break;
1401b362b15af34006e6a11974088a46d42b903418eJohann            }
1411b362b15af34006e6a11974088a46d42b903418eJohann
1421b362b15af34006e6a11974088a46d42b903418eJohann            if ((n++ % 16) == 0) {
1431b362b15af34006e6a11974088a46d42b903418eJohann                printf(".");
1441b362b15af34006e6a11974088a46d42b903418eJohann                fflush(stdout);
1451b362b15af34006e6a11974088a46d42b903418eJohann            }
1461b362b15af34006e6a11974088a46d42b903418eJohann
1471b362b15af34006e6a11974088a46d42b903418eJohann            buffer->release();
1481b362b15af34006e6a11974088a46d42b903418eJohann            buffer = NULL;
1491b362b15af34006e6a11974088a46d42b903418eJohann
1501b362b15af34006e6a11974088a46d42b903418eJohann            ++numFrames;
1511b362b15af34006e6a11974088a46d42b903418eJohann            if (gMaxNumFrames > 0 && numFrames == gMaxNumFrames) {
1521b362b15af34006e6a11974088a46d42b903418eJohann                break;
1531b362b15af34006e6a11974088a46d42b903418eJohann            }
1541b362b15af34006e6a11974088a46d42b903418eJohann
1551b362b15af34006e6a11974088a46d42b903418eJohann            if (gReproduceBug == 1 && numFrames == 40) {
1561b362b15af34006e6a11974088a46d42b903418eJohann                printf("seeking past the end now.");
1571b362b15af34006e6a11974088a46d42b903418eJohann                options.setSeekTo(0x7fffffffL);
1581b362b15af34006e6a11974088a46d42b903418eJohann            } else if (gReproduceBug == 2 && numFrames == 40) {
1591b362b15af34006e6a11974088a46d42b903418eJohann                printf("seeking to 5 secs.");
1601b362b15af34006e6a11974088a46d42b903418eJohann                options.setSeekTo(5000000);
1611b362b15af34006e6a11974088a46d42b903418eJohann            }
1621b362b15af34006e6a11974088a46d42b903418eJohann        }
1631b362b15af34006e6a11974088a46d42b903418eJohann
1641b362b15af34006e6a11974088a46d42b903418eJohann        printf("$");
1651b362b15af34006e6a11974088a46d42b903418eJohann        fflush(stdout);
1661b362b15af34006e6a11974088a46d42b903418eJohann
1671b362b15af34006e6a11974088a46d42b903418eJohann        options.setSeekTo(0);
1681b362b15af34006e6a11974088a46d42b903418eJohann    }
1691b362b15af34006e6a11974088a46d42b903418eJohann
1701b362b15af34006e6a11974088a46d42b903418eJohann    decoder->stop();
1711b362b15af34006e6a11974088a46d42b903418eJohann    printf("\n");
1721b362b15af34006e6a11974088a46d42b903418eJohann
1731b362b15af34006e6a11974088a46d42b903418eJohann    int64_t delay = getNowUs() - startTime;
1741b362b15af34006e6a11974088a46d42b903418eJohann    printf("avg. %.2f fps\n", n * 1E6 / delay);
1751b362b15af34006e6a11974088a46d42b903418eJohann
1761b362b15af34006e6a11974088a46d42b903418eJohann    printf("decoded a total of %d frame(s).\n", n);
1771b362b15af34006e6a11974088a46d42b903418eJohann}
1781b362b15af34006e6a11974088a46d42b903418eJohann
1791b362b15af34006e6a11974088a46d42b903418eJohannstatic void usage(const char *me) {
1801b362b15af34006e6a11974088a46d42b903418eJohann    fprintf(stderr, "usage: %s\n", me);
1811b362b15af34006e6a11974088a46d42b903418eJohann    fprintf(stderr, "       -h(elp)\n");
1821b362b15af34006e6a11974088a46d42b903418eJohann    fprintf(stderr, "       -a(udio)\n");
1831b362b15af34006e6a11974088a46d42b903418eJohann    fprintf(stderr, "       -n repetitions\n");
1841b362b15af34006e6a11974088a46d42b903418eJohann    fprintf(stderr, "       -l(ist) components\n");
1851b362b15af34006e6a11974088a46d42b903418eJohann    fprintf(stderr, "       -m max-number-of-frames-to-decode in each pass\n");
1861b362b15af34006e6a11974088a46d42b903418eJohann    fprintf(stderr, "       -b bug to reproduce\n");
1871b362b15af34006e6a11974088a46d42b903418eJohann    fprintf(stderr, "       -p(rofiles) dump decoder profiles supported\n");
1881b362b15af34006e6a11974088a46d42b903418eJohann}
1891b362b15af34006e6a11974088a46d42b903418eJohann
1901b362b15af34006e6a11974088a46d42b903418eJohannint main(int argc, char **argv) {
1911b362b15af34006e6a11974088a46d42b903418eJohann    android::ProcessState::self()->startThreadPool();
1921b362b15af34006e6a11974088a46d42b903418eJohann
1931b362b15af34006e6a11974088a46d42b903418eJohann    bool audioOnly = false;
1941b362b15af34006e6a11974088a46d42b903418eJohann    bool listComponents = false;
1951b362b15af34006e6a11974088a46d42b903418eJohann    bool dumpProfiles = false;
1961b362b15af34006e6a11974088a46d42b903418eJohann    gNumRepetitions = 1;
1971b362b15af34006e6a11974088a46d42b903418eJohann    gMaxNumFrames = 0;
1981b362b15af34006e6a11974088a46d42b903418eJohann    gReproduceBug = -1;
1991b362b15af34006e6a11974088a46d42b903418eJohann
2001b362b15af34006e6a11974088a46d42b903418eJohann    int res;
2011b362b15af34006e6a11974088a46d42b903418eJohann    while ((res = getopt(argc, argv, "han:lm:b:p")) >= 0) {
2021b362b15af34006e6a11974088a46d42b903418eJohann        switch (res) {
2031b362b15af34006e6a11974088a46d42b903418eJohann            case 'a':
2041b362b15af34006e6a11974088a46d42b903418eJohann            {
2051b362b15af34006e6a11974088a46d42b903418eJohann                audioOnly = true;
2061b362b15af34006e6a11974088a46d42b903418eJohann                break;
2071b362b15af34006e6a11974088a46d42b903418eJohann            }
2081b362b15af34006e6a11974088a46d42b903418eJohann
2091b362b15af34006e6a11974088a46d42b903418eJohann            case 'l':
2101b362b15af34006e6a11974088a46d42b903418eJohann            {
2111b362b15af34006e6a11974088a46d42b903418eJohann                listComponents = true;
2121b362b15af34006e6a11974088a46d42b903418eJohann                break;
2131b362b15af34006e6a11974088a46d42b903418eJohann            }
2141b362b15af34006e6a11974088a46d42b903418eJohann
2151b362b15af34006e6a11974088a46d42b903418eJohann            case 'm':
2161b362b15af34006e6a11974088a46d42b903418eJohann            case 'n':
217ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin            case 'b':
218ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin            {
219ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                char *end;
220ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                long x = strtol(optarg, &end, 10);
221ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin
222ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                if (*end != '\0' || end == optarg || x <= 0) {
223ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                    x = 1;
224ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                }
225ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin
226ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                if (res == 'n') {
227ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                    gNumRepetitions = x;
228ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                } else if (res == 'm') {
229ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                    gMaxNumFrames = x;
230ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                } else {
231ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                    CHECK_EQ(res, 'b');
232ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                    gReproduceBug = x;
233ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                }
234ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                break;
235ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin            }
236ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin
237ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin            case 'p':
238ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin            {
239ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                dumpProfiles = true;
240ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                break;
241ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin            }
242ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin
243ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin            case '?':
244ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin            case 'h':
245ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin            default:
246ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin            {
247ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                usage(argv[0]);
248ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                exit(1);
249ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                break;
250ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin            }
251ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin        }
252ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin    }
253ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin
254ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin    argc -= optind;
255ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin    argv += optind;
256ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin
257ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin    if (dumpProfiles) {
258ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin        sp<IServiceManager> sm = defaultServiceManager();
259ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin        sp<IBinder> binder = sm->getService(String16("media.player"));
260ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin        sp<IMediaPlayerService> service =
261ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin            interface_cast<IMediaPlayerService>(binder);
262ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin
263ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin        CHECK(service.get() != NULL);
264ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin
265ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin        sp<IOMX> omx = service->createOMX();
266ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin        CHECK(omx.get() != NULL);
267ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin
268ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin        const char *kMimeTypes[] = {
269ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin            MEDIA_MIMETYPE_VIDEO_AVC, MEDIA_MIMETYPE_VIDEO_MPEG4,
270ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin            MEDIA_MIMETYPE_VIDEO_H263
271ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin        };
272ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin
273ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin        for (size_t k = 0; k < sizeof(kMimeTypes) / sizeof(kMimeTypes[0]);
274ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin             ++k) {
275ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin            printf("type '%s':\n", kMimeTypes[k]);
276ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin
277ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin            Vector<CodecCapabilities> results;
278ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin            CHECK_EQ(QueryCodecs(omx, kMimeTypes[k],
279ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                                 true, // queryDecoders
280ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                                 &results), OK);
281ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin
282ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin            for (size_t i = 0; i < results.size(); ++i) {
283ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                printf("  decoder '%s' supports ",
284ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                       results[i].mComponentName.string());
285ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin
286ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                if (results[i].mProfileLevels.size() == 0) {
287ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                    printf("NOTHING.\n");
288ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                    continue;
289ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                }
290ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin
291ca15b5fc158a9df465aaf1acfe38d8cb5042c81bKunter Gultekin                for (size_t j = 0; j < results[i].mProfileLevels.size(); ++j) {
292ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang                    const CodecProfileLevel &profileLevel =
2931b362b15af34006e6a11974088a46d42b903418eJohann                        results[i].mProfileLevels[j];
2941b362b15af34006e6a11974088a46d42b903418eJohann
2951b362b15af34006e6a11974088a46d42b903418eJohann                    printf("%s%ld/%ld", j > 0 ? ", " : "",
2961b362b15af34006e6a11974088a46d42b903418eJohann                           profileLevel.mProfile, profileLevel.mLevel);
2971b362b15af34006e6a11974088a46d42b903418eJohann                }
2981b362b15af34006e6a11974088a46d42b903418eJohann
2991b362b15af34006e6a11974088a46d42b903418eJohann                printf("\n");
3001b362b15af34006e6a11974088a46d42b903418eJohann            }
3011b362b15af34006e6a11974088a46d42b903418eJohann        }
302    }
303
304    if (listComponents) {
305        sp<IServiceManager> sm = defaultServiceManager();
306        sp<IBinder> binder = sm->getService(String16("media.player"));
307        sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder);
308
309        CHECK(service.get() != NULL);
310
311        sp<IOMX> omx = service->createOMX();
312        CHECK(omx.get() != NULL);
313
314        List<String8> list;
315        omx->list_nodes(&list);
316
317        for (List<String8>::iterator it = list.begin();
318             it != list.end(); ++it) {
319            printf("%s\n", (*it).string());
320        }
321    }
322
323    DataSource::RegisterDefaultSniffers();
324
325    OMXClient client;
326    status_t err = client.connect();
327
328    for (int k = 0; k < argc; ++k) {
329        const char *filename = argv[k];
330
331        sp<DataSource> dataSource;
332        if (!strncasecmp("http://", filename, 7)) {
333            dataSource = new HTTPDataSource(filename);
334            dataSource = new CachingDataSource(dataSource, 64 * 1024, 10);
335        } else {
336            dataSource = new MmapSource(filename);
337        }
338
339        bool isJPEG = false;
340
341        size_t len = strlen(filename);
342        if (len >= 4 && !strcasecmp(filename + len - 4, ".jpg")) {
343            isJPEG = true;
344        }
345
346        sp<MediaSource> mediaSource;
347
348        if (isJPEG) {
349            mediaSource = new JPEGSource(dataSource);
350        } else {
351            sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource);
352
353            size_t numTracks = extractor->countTracks();
354
355            sp<MetaData> meta;
356            size_t i;
357            for (i = 0; i < numTracks; ++i) {
358                meta = extractor->getTrackMetaData(i);
359
360                const char *mime;
361                meta->findCString(kKeyMIMEType, &mime);
362
363                if (audioOnly && !strncasecmp(mime, "audio/", 6)) {
364                    break;
365                }
366
367                if (!audioOnly && !strncasecmp(mime, "video/", 6)) {
368                    break;
369                }
370            }
371
372            mediaSource = extractor->getTrack(i);
373        }
374
375        playSource(&client, mediaSource);
376    }
377
378    client.disconnect();
379
380    return 0;
381}
382