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