stagefright.cpp revision cfc7a7feb81b946341bc01ade68291bf8b6e1037
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//#define LOG_NDEBUG 0
18#define LOG_TAG "stagefright"
19#include <media/stagefright/foundation/ADebug.h>
20
21#include <sys/time.h>
22
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26
27#include "SineSource.h"
28
29#include <binder/IServiceManager.h>
30#include <binder/ProcessState.h>
31#include <media/IMediaPlayerService.h>
32#include <media/stagefright/foundation/ALooper.h>
33#include "include/ARTSPController.h"
34#include "include/LiveSession.h"
35#include "include/NuCachedSource2.h"
36#include <media/stagefright/AudioPlayer.h>
37#include <media/stagefright/DataSource.h>
38#include <media/stagefright/JPEGSource.h>
39#include <media/stagefright/MediaDefs.h>
40#include <media/stagefright/MediaErrors.h>
41#include <media/stagefright/MediaExtractor.h>
42#include <media/stagefright/MediaSource.h>
43#include <media/stagefright/MetaData.h>
44#include <media/stagefright/OMXClient.h>
45#include <media/stagefright/OMXCodec.h>
46#include <media/mediametadataretriever.h>
47
48#include <media/stagefright/foundation/hexdump.h>
49#include <media/stagefright/MPEG2TSWriter.h>
50#include <media/stagefright/MPEG4Writer.h>
51
52#include <private/media/VideoFrame.h>
53#include <SkBitmap.h>
54#include <SkImageEncoder.h>
55
56#include <fcntl.h>
57
58#include <gui/SurfaceTextureClient.h>
59
60#include <surfaceflinger/ISurfaceComposer.h>
61#include <surfaceflinger/SurfaceComposerClient.h>
62
63using namespace android;
64
65static long gNumRepetitions;
66static long gMaxNumFrames;  // 0 means decode all available.
67static long gReproduceBug;  // if not -1.
68static bool gPreferSoftwareCodec;
69static bool gForceToUseHardwareCodec;
70static bool gPlaybackAudio;
71static bool gWriteMP4;
72static bool gDisplayHistogram;
73static String8 gWriteMP4Filename;
74
75static sp<ANativeWindow> gSurface;
76
77#define USE_SURFACE_COMPOSER 0
78
79static int64_t getNowUs() {
80    struct timeval tv;
81    gettimeofday(&tv, NULL);
82
83    return (int64_t)tv.tv_usec + tv.tv_sec * 1000000ll;
84}
85
86static int CompareIncreasing(const int64_t *a, const int64_t *b) {
87    return (*a) < (*b) ? -1 : (*a) > (*b) ? 1 : 0;
88}
89
90static void displayDecodeHistogram(Vector<int64_t> *decodeTimesUs) {
91    printf("decode times:\n");
92
93    decodeTimesUs->sort(CompareIncreasing);
94
95    size_t n = decodeTimesUs->size();
96    int64_t minUs = decodeTimesUs->itemAt(0);
97    int64_t maxUs = decodeTimesUs->itemAt(n - 1);
98
99    printf("min decode time %lld us (%.2f secs)\n", minUs, minUs / 1E6);
100    printf("max decode time %lld us (%.2f secs)\n", maxUs, maxUs / 1E6);
101
102    size_t counts[100];
103    for (size_t i = 0; i < 100; ++i) {
104        counts[i] = 0;
105    }
106
107    for (size_t i = 0; i < n; ++i) {
108        int64_t x = decodeTimesUs->itemAt(i);
109
110        size_t slot = ((x - minUs) * 100) / (maxUs - minUs);
111        if (slot == 100) { slot = 99; }
112
113        ++counts[slot];
114    }
115
116    for (size_t i = 0; i < 100; ++i) {
117        int64_t slotUs = minUs + (i * (maxUs - minUs) / 100);
118
119        double fps = 1E6 / slotUs;
120        printf("[%.2f fps]: %d\n", fps, counts[i]);
121    }
122}
123
124static void displayAVCProfileLevelIfPossible(const sp<MetaData>& meta) {
125    uint32_t type;
126    const void *data;
127    size_t size;
128    if (meta->findData(kKeyAVCC, &type, &data, &size)) {
129        const uint8_t *ptr = (const uint8_t *)data;
130        CHECK(size >= 7);
131        CHECK(ptr[0] == 1);  // configurationVersion == 1
132        uint8_t profile = ptr[1];
133        uint8_t level = ptr[3];
134        fprintf(stderr, "AVC video profile %d and level %d\n", profile, level);
135    }
136}
137
138static void playSource(OMXClient *client, sp<MediaSource> &source) {
139    sp<MetaData> meta = source->getFormat();
140
141    const char *mime;
142    CHECK(meta->findCString(kKeyMIMEType, &mime));
143
144    sp<MediaSource> rawSource;
145    if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_RAW, mime)) {
146        rawSource = source;
147    } else {
148        int flags = 0;
149        if (gPreferSoftwareCodec) {
150            flags |= OMXCodec::kPreferSoftwareCodecs;
151        }
152        if (gForceToUseHardwareCodec) {
153            CHECK(!gPreferSoftwareCodec);
154            flags |= OMXCodec::kHardwareCodecsOnly;
155        }
156        rawSource = OMXCodec::Create(
157            client->interface(), meta, false /* createEncoder */, source,
158            NULL /* matchComponentName */,
159            flags,
160            gSurface);
161
162        if (rawSource == NULL) {
163            fprintf(stderr, "Failed to instantiate decoder for '%s'.\n", mime);
164            return;
165        }
166        displayAVCProfileLevelIfPossible(meta);
167    }
168
169    source.clear();
170
171    status_t err = rawSource->start();
172
173    if (err != OK) {
174        fprintf(stderr, "rawSource returned error %d (0x%08x)\n", err, err);
175        return;
176    }
177
178    if (gPlaybackAudio) {
179        AudioPlayer *player = new AudioPlayer(NULL);
180        player->setSource(rawSource);
181        rawSource.clear();
182
183        player->start(true /* sourceAlreadyStarted */);
184
185        status_t finalStatus;
186        while (!player->reachedEOS(&finalStatus)) {
187            usleep(100000ll);
188        }
189
190        delete player;
191        player = NULL;
192
193        return;
194    } else if (gReproduceBug >= 3 && gReproduceBug <= 5) {
195        int64_t durationUs;
196        CHECK(meta->findInt64(kKeyDuration, &durationUs));
197
198        status_t err;
199        MediaBuffer *buffer;
200        MediaSource::ReadOptions options;
201        int64_t seekTimeUs = -1;
202        for (;;) {
203            err = rawSource->read(&buffer, &options);
204            options.clearSeekTo();
205
206            bool shouldSeek = false;
207            if (err == INFO_FORMAT_CHANGED) {
208                CHECK(buffer == NULL);
209
210                printf("format changed.\n");
211                continue;
212            } else if (err != OK) {
213                printf("reached EOF.\n");
214
215                shouldSeek = true;
216            } else {
217                int64_t timestampUs;
218                CHECK(buffer->meta_data()->findInt64(kKeyTime, &timestampUs));
219
220                bool failed = false;
221
222                if (seekTimeUs >= 0) {
223                    int64_t diff = timestampUs - seekTimeUs;
224
225                    if (diff < 0) {
226                        diff = -diff;
227                    }
228
229                    if ((gReproduceBug == 4 && diff > 500000)
230                        || (gReproduceBug == 5 && timestampUs < 0)) {
231                        printf("wanted: %.2f secs, got: %.2f secs\n",
232                               seekTimeUs / 1E6, timestampUs / 1E6);
233
234                        printf("ERROR: ");
235                        failed = true;
236                    }
237                }
238
239                printf("buffer has timestamp %lld us (%.2f secs)\n",
240                       timestampUs, timestampUs / 1E6);
241
242                buffer->release();
243                buffer = NULL;
244
245                if (failed) {
246                    break;
247                }
248
249                shouldSeek = ((double)rand() / RAND_MAX) < 0.1;
250
251                if (gReproduceBug == 3) {
252                    shouldSeek = false;
253                }
254            }
255
256            seekTimeUs = -1;
257
258            if (shouldSeek) {
259                seekTimeUs = (rand() * (float)durationUs) / RAND_MAX;
260                options.setSeekTo(seekTimeUs);
261
262                printf("seeking to %lld us (%.2f secs)\n",
263                       seekTimeUs, seekTimeUs / 1E6);
264            }
265        }
266
267        rawSource->stop();
268
269        return;
270    }
271
272    int n = 0;
273    int64_t startTime = getNowUs();
274
275    long numIterationsLeft = gNumRepetitions;
276    MediaSource::ReadOptions options;
277
278    int64_t sumDecodeUs = 0;
279    int64_t totalBytes = 0;
280
281    Vector<int64_t> decodeTimesUs;
282
283    while (numIterationsLeft-- > 0) {
284        long numFrames = 0;
285
286        MediaBuffer *buffer;
287
288        for (;;) {
289            int64_t startDecodeUs = getNowUs();
290            status_t err = rawSource->read(&buffer, &options);
291            int64_t delayDecodeUs = getNowUs() - startDecodeUs;
292
293            options.clearSeekTo();
294
295            if (err != OK) {
296                CHECK(buffer == NULL);
297
298                if (err == INFO_FORMAT_CHANGED) {
299                    printf("format changed.\n");
300                    continue;
301                }
302
303                break;
304            }
305
306            if (buffer->range_length() > 0) {
307                if (gDisplayHistogram && n > 0) {
308                    // Ignore the first time since it includes some setup
309                    // cost.
310                    decodeTimesUs.push(delayDecodeUs);
311                }
312
313                if ((n++ % 16) == 0) {
314                    printf(".");
315                    fflush(stdout);
316                }
317            }
318
319            sumDecodeUs += delayDecodeUs;
320            totalBytes += buffer->range_length();
321
322            buffer->release();
323            buffer = NULL;
324
325            ++numFrames;
326            if (gMaxNumFrames > 0 && numFrames == gMaxNumFrames) {
327                break;
328            }
329
330            if (gReproduceBug == 1 && numFrames == 40) {
331                printf("seeking past the end now.");
332                options.setSeekTo(0x7fffffffL);
333            } else if (gReproduceBug == 2 && numFrames == 40) {
334                printf("seeking to 5 secs.");
335                options.setSeekTo(5000000);
336            }
337        }
338
339        printf("$");
340        fflush(stdout);
341
342        options.setSeekTo(0);
343    }
344
345    rawSource->stop();
346    printf("\n");
347
348    int64_t delay = getNowUs() - startTime;
349    if (!strncasecmp("video/", mime, 6)) {
350        printf("avg. %.2f fps\n", n * 1E6 / delay);
351
352        printf("avg. time to decode one buffer %.2f usecs\n",
353               (double)sumDecodeUs / n);
354
355        printf("decoded a total of %d frame(s).\n", n);
356
357        if (gDisplayHistogram) {
358            displayDecodeHistogram(&decodeTimesUs);
359        }
360    } else if (!strncasecmp("audio/", mime, 6)) {
361        // Frame count makes less sense for audio, as the output buffer
362        // sizes may be different across decoders.
363        printf("avg. %.2f KB/sec\n", totalBytes / 1024 * 1E6 / delay);
364
365        printf("decoded a total of %lld bytes\n", totalBytes);
366    }
367}
368
369////////////////////////////////////////////////////////////////////////////////
370
371struct DetectSyncSource : public MediaSource {
372    DetectSyncSource(const sp<MediaSource> &source);
373
374    virtual status_t start(MetaData *params = NULL);
375    virtual status_t stop();
376    virtual sp<MetaData> getFormat();
377
378    virtual status_t read(
379            MediaBuffer **buffer, const ReadOptions *options);
380
381private:
382    enum StreamType {
383        AVC,
384        MPEG4,
385        H263,
386        OTHER,
387    };
388
389    sp<MediaSource> mSource;
390    StreamType mStreamType;
391
392    DISALLOW_EVIL_CONSTRUCTORS(DetectSyncSource);
393};
394
395DetectSyncSource::DetectSyncSource(const sp<MediaSource> &source)
396    : mSource(source),
397      mStreamType(OTHER) {
398    const char *mime;
399    CHECK(mSource->getFormat()->findCString(kKeyMIMEType, &mime));
400
401    if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_AVC)) {
402        mStreamType = AVC;
403    } else if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_MPEG4)) {
404        mStreamType = MPEG4;
405        CHECK(!"sync frame detection not implemented yet for MPEG4");
406    } else if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_H263)) {
407        mStreamType = H263;
408        CHECK(!"sync frame detection not implemented yet for H.263");
409    }
410}
411
412status_t DetectSyncSource::start(MetaData *params) {
413    return mSource->start(params);
414}
415
416status_t DetectSyncSource::stop() {
417    return mSource->stop();
418}
419
420sp<MetaData> DetectSyncSource::getFormat() {
421    return mSource->getFormat();
422}
423
424static bool isIDRFrame(MediaBuffer *buffer) {
425    const uint8_t *data =
426        (const uint8_t *)buffer->data() + buffer->range_offset();
427    size_t size = buffer->range_length();
428    for (size_t i = 0; i + 3 < size; ++i) {
429        if (!memcmp("\x00\x00\x01", &data[i], 3)) {
430            uint8_t nalType = data[i + 3] & 0x1f;
431            if (nalType == 5) {
432                return true;
433            }
434        }
435    }
436
437    return false;
438}
439
440status_t DetectSyncSource::read(
441        MediaBuffer **buffer, const ReadOptions *options) {
442    status_t err = mSource->read(buffer, options);
443
444    if (err != OK) {
445        return err;
446    }
447
448    if (mStreamType == AVC && isIDRFrame(*buffer)) {
449        (*buffer)->meta_data()->setInt32(kKeyIsSyncFrame, true);
450    } else {
451        (*buffer)->meta_data()->setInt32(kKeyIsSyncFrame, true);
452    }
453
454    return OK;
455}
456
457////////////////////////////////////////////////////////////////////////////////
458
459static void writeSourcesToMP4(
460        Vector<sp<MediaSource> > &sources, bool syncInfoPresent) {
461#if 0
462    sp<MPEG4Writer> writer =
463        new MPEG4Writer(gWriteMP4Filename.string());
464#else
465    sp<MPEG2TSWriter> writer =
466        new MPEG2TSWriter(gWriteMP4Filename.string());
467#endif
468
469    // at most one minute.
470    writer->setMaxFileDuration(60000000ll);
471
472    for (size_t i = 0; i < sources.size(); ++i) {
473        sp<MediaSource> source = sources.editItemAt(i);
474
475        CHECK_EQ(writer->addSource(
476                    syncInfoPresent ? source : new DetectSyncSource(source)),
477                (status_t)OK);
478    }
479
480    sp<MetaData> params = new MetaData;
481    params->setInt32(kKeyNotRealTime, true);
482    CHECK_EQ(writer->start(params.get()), (status_t)OK);
483
484    while (!writer->reachedEOS()) {
485        usleep(100000);
486    }
487    writer->stop();
488}
489
490static void performSeekTest(const sp<MediaSource> &source) {
491    CHECK_EQ((status_t)OK, source->start());
492
493    int64_t durationUs;
494    CHECK(source->getFormat()->findInt64(kKeyDuration, &durationUs));
495
496    for (int64_t seekTimeUs = 0; seekTimeUs <= durationUs;
497            seekTimeUs += 60000ll) {
498        MediaSource::ReadOptions options;
499        options.setSeekTo(
500                seekTimeUs, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC);
501
502        MediaBuffer *buffer;
503        status_t err;
504        for (;;) {
505            err = source->read(&buffer, &options);
506
507            options.clearSeekTo();
508
509            if (err == INFO_FORMAT_CHANGED) {
510                CHECK(buffer == NULL);
511                continue;
512            }
513
514            if (err != OK) {
515                CHECK(buffer == NULL);
516                break;
517            }
518
519            if (buffer->range_length() > 0) {
520                break;
521            }
522
523            CHECK(buffer != NULL);
524
525            buffer->release();
526            buffer = NULL;
527        }
528
529        if (err == OK) {
530            int64_t timeUs;
531            CHECK(buffer->meta_data()->findInt64(kKeyTime, &timeUs));
532
533            printf("%lld\t%lld\t%lld\n", seekTimeUs, timeUs, seekTimeUs - timeUs);
534
535            buffer->release();
536            buffer = NULL;
537        } else {
538            printf("ERROR\n");
539            break;
540        }
541    }
542
543    CHECK_EQ((status_t)OK, source->stop());
544}
545
546static void usage(const char *me) {
547    fprintf(stderr, "usage: %s\n", me);
548    fprintf(stderr, "       -h(elp)\n");
549    fprintf(stderr, "       -a(udio)\n");
550    fprintf(stderr, "       -n repetitions\n");
551    fprintf(stderr, "       -l(ist) components\n");
552    fprintf(stderr, "       -m max-number-of-frames-to-decode in each pass\n");
553    fprintf(stderr, "       -b bug to reproduce\n");
554    fprintf(stderr, "       -p(rofiles) dump decoder profiles supported\n");
555    fprintf(stderr, "       -t(humbnail) extract video thumbnail or album art\n");
556    fprintf(stderr, "       -s(oftware) prefer software codec\n");
557    fprintf(stderr, "       -r(hardware) force to use hardware codec\n");
558    fprintf(stderr, "       -o playback audio\n");
559    fprintf(stderr, "       -w(rite) filename (write to .mp4 file)\n");
560    fprintf(stderr, "       -k seek test\n");
561    fprintf(stderr, "       -x display a histogram of decoding times/fps "
562                    "(video only)\n");
563    fprintf(stderr, "       -S allocate buffers from a surface\n");
564}
565
566int main(int argc, char **argv) {
567    android::ProcessState::self()->startThreadPool();
568
569    bool audioOnly = false;
570    bool listComponents = false;
571    bool dumpProfiles = false;
572    bool extractThumbnail = false;
573    bool seekTest = false;
574    bool useSurfaceAlloc = false;
575    gNumRepetitions = 1;
576    gMaxNumFrames = 0;
577    gReproduceBug = -1;
578    gPreferSoftwareCodec = false;
579    gForceToUseHardwareCodec = false;
580    gPlaybackAudio = false;
581    gWriteMP4 = false;
582    gDisplayHistogram = false;
583
584    sp<ALooper> looper;
585    sp<ARTSPController> rtspController;
586    sp<LiveSession> liveSession;
587
588    int res;
589    while ((res = getopt(argc, argv, "han:lm:b:ptsrow:kxS")) >= 0) {
590        switch (res) {
591            case 'a':
592            {
593                audioOnly = true;
594                break;
595            }
596
597            case 'l':
598            {
599                listComponents = true;
600                break;
601            }
602
603            case 'm':
604            case 'n':
605            case 'b':
606            {
607                char *end;
608                long x = strtol(optarg, &end, 10);
609
610                if (*end != '\0' || end == optarg || x <= 0) {
611                    x = 1;
612                }
613
614                if (res == 'n') {
615                    gNumRepetitions = x;
616                } else if (res == 'm') {
617                    gMaxNumFrames = x;
618                } else {
619                    CHECK_EQ(res, 'b');
620                    gReproduceBug = x;
621                }
622                break;
623            }
624
625            case 'w':
626            {
627                gWriteMP4 = true;
628                gWriteMP4Filename.setTo(optarg);
629                break;
630            }
631
632            case 'p':
633            {
634                dumpProfiles = true;
635                break;
636            }
637
638            case 't':
639            {
640                extractThumbnail = true;
641                break;
642            }
643
644            case 's':
645            {
646                gPreferSoftwareCodec = true;
647                break;
648            }
649
650            case 'r':
651            {
652                gForceToUseHardwareCodec = true;
653                break;
654            }
655
656            case 'o':
657            {
658                gPlaybackAudio = true;
659                break;
660            }
661
662            case 'k':
663            {
664                seekTest = true;
665                break;
666            }
667
668            case 'x':
669            {
670                gDisplayHistogram = true;
671                break;
672            }
673
674            case 'S':
675            {
676                useSurfaceAlloc = true;
677                break;
678            }
679
680            case '?':
681            case 'h':
682            default:
683            {
684                usage(argv[0]);
685                exit(1);
686                break;
687            }
688        }
689    }
690
691    if (gPlaybackAudio && !audioOnly) {
692        // This doesn't make any sense if we're decoding the video track.
693        gPlaybackAudio = false;
694    }
695
696    argc -= optind;
697    argv += optind;
698
699    if (extractThumbnail) {
700        sp<IServiceManager> sm = defaultServiceManager();
701        sp<IBinder> binder = sm->getService(String16("media.player"));
702        sp<IMediaPlayerService> service =
703            interface_cast<IMediaPlayerService>(binder);
704
705        CHECK(service.get() != NULL);
706
707        sp<IMediaMetadataRetriever> retriever =
708            service->createMetadataRetriever(getpid());
709
710        CHECK(retriever != NULL);
711
712        for (int k = 0; k < argc; ++k) {
713            const char *filename = argv[k];
714
715            bool failed = true;
716            CHECK_EQ(retriever->setDataSource(filename), (status_t)OK);
717            sp<IMemory> mem =
718                    retriever->getFrameAtTime(-1,
719                                    MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC);
720
721            if (mem != NULL) {
722                failed = false;
723                printf("getFrameAtTime(%s) => OK\n", filename);
724
725                VideoFrame *frame = (VideoFrame *)mem->pointer();
726
727                SkBitmap bitmap;
728                bitmap.setConfig(
729                        SkBitmap::kRGB_565_Config, frame->mWidth, frame->mHeight);
730
731                bitmap.setPixels((uint8_t *)frame + sizeof(VideoFrame));
732
733                CHECK(SkImageEncoder::EncodeFile(
734                            "/sdcard/out.jpg", bitmap,
735                            SkImageEncoder::kJPEG_Type,
736                            SkImageEncoder::kDefaultQuality));
737            }
738
739            {
740                mem = retriever->extractAlbumArt();
741
742                if (mem != NULL) {
743                    failed = false;
744                    printf("extractAlbumArt(%s) => OK\n", filename);
745                }
746            }
747
748            if (failed) {
749                printf("both getFrameAtTime and extractAlbumArt "
750                    "failed on file '%s'.\n", filename);
751            }
752        }
753
754        return 0;
755    }
756
757    if (dumpProfiles) {
758        sp<IServiceManager> sm = defaultServiceManager();
759        sp<IBinder> binder = sm->getService(String16("media.player"));
760        sp<IMediaPlayerService> service =
761            interface_cast<IMediaPlayerService>(binder);
762
763        CHECK(service.get() != NULL);
764
765        sp<IOMX> omx = service->getOMX();
766        CHECK(omx.get() != NULL);
767
768        const char *kMimeTypes[] = {
769            MEDIA_MIMETYPE_VIDEO_AVC, MEDIA_MIMETYPE_VIDEO_MPEG4,
770            MEDIA_MIMETYPE_VIDEO_H263, MEDIA_MIMETYPE_AUDIO_AAC,
771            MEDIA_MIMETYPE_AUDIO_AMR_NB, MEDIA_MIMETYPE_AUDIO_AMR_WB,
772            MEDIA_MIMETYPE_AUDIO_MPEG
773        };
774
775        for (size_t k = 0; k < sizeof(kMimeTypes) / sizeof(kMimeTypes[0]);
776             ++k) {
777            printf("type '%s':\n", kMimeTypes[k]);
778
779            Vector<CodecCapabilities> results;
780            CHECK_EQ(QueryCodecs(omx, kMimeTypes[k],
781                                 true, // queryDecoders
782                                 &results), (status_t)OK);
783
784            for (size_t i = 0; i < results.size(); ++i) {
785                printf("  decoder '%s' supports ",
786                       results[i].mComponentName.string());
787
788                if (results[i].mProfileLevels.size() == 0) {
789                    printf("NOTHING.\n");
790                    continue;
791                }
792
793                for (size_t j = 0; j < results[i].mProfileLevels.size(); ++j) {
794                    const CodecProfileLevel &profileLevel =
795                        results[i].mProfileLevels[j];
796
797                    printf("%s%ld/%ld", j > 0 ? ", " : "",
798                           profileLevel.mProfile, profileLevel.mLevel);
799                }
800
801                printf("\n");
802            }
803        }
804    }
805
806    if (listComponents) {
807        sp<IServiceManager> sm = defaultServiceManager();
808        sp<IBinder> binder = sm->getService(String16("media.player"));
809        sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder);
810
811        CHECK(service.get() != NULL);
812
813        sp<IOMX> omx = service->getOMX();
814        CHECK(omx.get() != NULL);
815
816        List<IOMX::ComponentInfo> list;
817        omx->listNodes(&list);
818
819        for (List<IOMX::ComponentInfo>::iterator it = list.begin();
820             it != list.end(); ++it) {
821            printf("%s\n", (*it).mName.string());
822        }
823    }
824
825    sp<SurfaceComposerClient> composerClient;
826    sp<SurfaceControl> control;
827
828    if (useSurfaceAlloc && !audioOnly) {
829#if USE_SURFACE_COMPOSER
830        composerClient = new SurfaceComposerClient;
831        CHECK_EQ(composerClient->initCheck(), (status_t)OK);
832
833        control = composerClient->createSurface(
834                getpid(),
835                String8("A Surface"),
836                0,
837                1280,
838                800,
839                PIXEL_FORMAT_RGB_565,
840                0);
841
842        CHECK(control != NULL);
843        CHECK(control->isValid());
844
845        CHECK_EQ(composerClient->openTransaction(), (status_t)OK);
846        CHECK_EQ(control->setLayer(30000), (status_t)OK);
847        CHECK_EQ(control->show(), (status_t)OK);
848        CHECK_EQ(composerClient->closeTransaction(), (status_t)OK);
849
850        gSurface = control->getSurface();
851        CHECK(gSurface != NULL);
852#else
853        sp<SurfaceTexture> texture = new SurfaceTexture(0 /* tex */);
854        gSurface = new SurfaceTextureClient(texture);
855#endif
856    }
857
858    DataSource::RegisterDefaultSniffers();
859
860    OMXClient client;
861    status_t err = client.connect();
862
863    for (int k = 0; k < argc; ++k) {
864        bool syncInfoPresent = true;
865
866        const char *filename = argv[k];
867
868        sp<DataSource> dataSource = DataSource::CreateFromURI(filename);
869
870        if (strncasecmp(filename, "sine:", 5)
871                && strncasecmp(filename, "rtsp://", 7)
872                && strncasecmp(filename, "httplive://", 11)
873                && dataSource == NULL) {
874            fprintf(stderr, "Unable to create data source.\n");
875            return 1;
876        }
877
878        bool isJPEG = false;
879
880        size_t len = strlen(filename);
881        if (len >= 4 && !strcasecmp(filename + len - 4, ".jpg")) {
882            isJPEG = true;
883        }
884
885        Vector<sp<MediaSource> > mediaSources;
886        sp<MediaSource> mediaSource;
887
888        if (isJPEG) {
889            mediaSource = new JPEGSource(dataSource);
890            if (gWriteMP4) {
891                mediaSources.push(mediaSource);
892            }
893        } else if (!strncasecmp("sine:", filename, 5)) {
894            char *end;
895            long sampleRate = strtol(filename + 5, &end, 10);
896
897            if (end == filename + 5) {
898                sampleRate = 44100;
899            }
900            mediaSource = new SineSource(sampleRate, 1);
901            if (gWriteMP4) {
902                mediaSources.push(mediaSource);
903            }
904        } else {
905            sp<MediaExtractor> extractor;
906
907            if (!strncasecmp("rtsp://", filename, 7)) {
908                if (looper == NULL) {
909                    looper = new ALooper;
910                    looper->start();
911                }
912
913                rtspController = new ARTSPController(looper);
914                status_t err = rtspController->connect(filename);
915                if (err != OK) {
916                    fprintf(stderr, "could not connect to rtsp server.\n");
917                    return -1;
918                }
919
920                extractor = rtspController.get();
921
922                syncInfoPresent = false;
923            } else if (!strncasecmp("httplive://", filename, 11)) {
924                String8 uri("http://");
925                uri.append(filename + 11);
926
927                if (looper == NULL) {
928                    looper = new ALooper;
929                    looper->start();
930                }
931                liveSession = new LiveSession;
932                looper->registerHandler(liveSession);
933
934                liveSession->connect(uri.string());
935                dataSource = liveSession->getDataSource();
936
937                extractor =
938                    MediaExtractor::Create(
939                            dataSource, MEDIA_MIMETYPE_CONTAINER_MPEG2TS);
940
941                syncInfoPresent = false;
942            } else {
943                extractor = MediaExtractor::Create(dataSource);
944                if (extractor == NULL) {
945                    fprintf(stderr, "could not create extractor.\n");
946                    return -1;
947                }
948            }
949
950            size_t numTracks = extractor->countTracks();
951
952            if (gWriteMP4) {
953                bool haveAudio = false;
954                bool haveVideo = false;
955                for (size_t i = 0; i < numTracks; ++i) {
956                    sp<MediaSource> source = extractor->getTrack(i);
957
958                    const char *mime;
959                    CHECK(source->getFormat()->findCString(
960                                kKeyMIMEType, &mime));
961
962                    bool useTrack = false;
963                    if (!haveAudio && !strncasecmp("audio/", mime, 6)) {
964                        haveAudio = true;
965                        useTrack = true;
966                    } else if (!haveVideo && !strncasecmp("video/", mime, 6)) {
967                        haveVideo = true;
968                        useTrack = true;
969                    }
970
971                    if (useTrack) {
972                        mediaSources.push(source);
973
974                        if (haveAudio && haveVideo) {
975                            break;
976                        }
977                    }
978                }
979            } else {
980                sp<MetaData> meta;
981                size_t i;
982                for (i = 0; i < numTracks; ++i) {
983                    meta = extractor->getTrackMetaData(
984                            i, MediaExtractor::kIncludeExtensiveMetaData);
985
986                    const char *mime;
987                    meta->findCString(kKeyMIMEType, &mime);
988
989                    if (audioOnly && !strncasecmp(mime, "audio/", 6)) {
990                        break;
991                    }
992
993                    if (!audioOnly && !strncasecmp(mime, "video/", 6)) {
994                        break;
995                    }
996
997                    meta = NULL;
998                }
999
1000                if (meta == NULL) {
1001                    fprintf(stderr,
1002                            "No suitable %s track found. The '-a' option will "
1003                            "target audio tracks only, the default is to target "
1004                            "video tracks only.\n",
1005                            audioOnly ? "audio" : "video");
1006                    return -1;
1007                }
1008
1009                int64_t thumbTimeUs;
1010                if (meta->findInt64(kKeyThumbnailTime, &thumbTimeUs)) {
1011                    printf("thumbnailTime: %lld us (%.2f secs)\n",
1012                           thumbTimeUs, thumbTimeUs / 1E6);
1013                }
1014
1015                mediaSource = extractor->getTrack(i);
1016            }
1017        }
1018
1019        if (gWriteMP4) {
1020            writeSourcesToMP4(mediaSources, syncInfoPresent);
1021        } else if (seekTest) {
1022            performSeekTest(mediaSource);
1023        } else {
1024            playSource(&client, mediaSource);
1025        }
1026
1027        if (rtspController != NULL) {
1028            rtspController->disconnect();
1029            rtspController.clear();
1030
1031            sleep(3);
1032        }
1033    }
1034
1035    if (useSurfaceAlloc && !audioOnly) {
1036        gSurface.clear();
1037
1038#if USE_SURFACE_COMPOSER
1039        composerClient->dispose();
1040#endif
1041    }
1042
1043    client.disconnect();
1044
1045    return 0;
1046}
1047