stagefright.cpp revision 348a8eab84f4bba76c04ca83b2f5418467aa1a48
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <sys/time.h>
18
19#include <stdlib.h>
20#include <string.h>
21#include <unistd.h>
22
23#include "SineSource.h"
24
25#include <binder/IServiceManager.h>
26#include <binder/ProcessState.h>
27#include <media/IMediaPlayerService.h>
28#include <media/stagefright/foundation/ALooper.h>
29#include "include/ARTSPController.h"
30#include <media/stagefright/AudioPlayer.h>
31#include <media/stagefright/DataSource.h>
32#include <media/stagefright/JPEGSource.h>
33#include <media/stagefright/MediaDebug.h>
34#include <media/stagefright/MediaDefs.h>
35#include <media/stagefright/MediaErrors.h>
36#include <media/stagefright/MediaExtractor.h>
37#include <media/stagefright/MediaSource.h>
38#include <media/stagefright/MetaData.h>
39#include <media/stagefright/OMXClient.h>
40#include <media/stagefright/OMXCodec.h>
41#include <media/mediametadataretriever.h>
42
43#include <media/stagefright/foundation/hexdump.h>
44#include <media/stagefright/MPEG4Writer.h>
45
46using namespace android;
47
48static long gNumRepetitions;
49static long gMaxNumFrames;  // 0 means decode all available.
50static long gReproduceBug;  // if not -1.
51static bool gPreferSoftwareCodec;
52static bool gPlaybackAudio;
53static bool gWriteMP4;
54static String8 gWriteMP4Filename;
55
56static int64_t getNowUs() {
57    struct timeval tv;
58    gettimeofday(&tv, NULL);
59
60    return (int64_t)tv.tv_usec + tv.tv_sec * 1000000ll;
61}
62
63static void playSource(OMXClient *client, const sp<MediaSource> &source) {
64    sp<MetaData> meta = source->getFormat();
65
66    const char *mime;
67    CHECK(meta->findCString(kKeyMIMEType, &mime));
68
69    sp<MediaSource> rawSource;
70    if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_RAW, mime)) {
71        rawSource = source;
72    } else {
73        rawSource = OMXCodec::Create(
74            client->interface(), meta, false /* createEncoder */, source,
75            NULL /* matchComponentName */,
76            gPreferSoftwareCodec ? OMXCodec::kPreferSoftwareCodecs : 0);
77
78        if (rawSource == NULL) {
79            fprintf(stderr, "Failed to instantiate decoder for '%s'.\n", mime);
80            return;
81        }
82    }
83
84    status_t err = rawSource->start();
85
86    if (err != OK) {
87        fprintf(stderr, "rawSource returned error %d (0x%08x)\n", err, err);
88        return;
89    }
90
91    if (gPlaybackAudio) {
92        AudioPlayer *player = new AudioPlayer(NULL);
93        player->setSource(rawSource);
94        rawSource.clear();
95
96        player->start(true /* sourceAlreadyStarted */);
97
98        status_t finalStatus;
99        while (!player->reachedEOS(&finalStatus)) {
100            usleep(100000ll);
101        }
102
103        delete player;
104        player = NULL;
105
106        return;
107    } else if (gReproduceBug >= 3 && gReproduceBug <= 5) {
108        int64_t durationUs;
109        CHECK(meta->findInt64(kKeyDuration, &durationUs));
110
111        status_t err;
112        MediaBuffer *buffer;
113        MediaSource::ReadOptions options;
114        int64_t seekTimeUs = -1;
115        for (;;) {
116            err = rawSource->read(&buffer, &options);
117            options.clearSeekTo();
118
119            bool shouldSeek = false;
120            if (err == INFO_FORMAT_CHANGED) {
121                CHECK_EQ(buffer, NULL);
122
123                printf("format changed.\n");
124                continue;
125            } else if (err != OK) {
126                printf("reached EOF.\n");
127
128                shouldSeek = true;
129            } else {
130                int64_t timestampUs;
131                CHECK(buffer->meta_data()->findInt64(kKeyTime, &timestampUs));
132
133                bool failed = false;
134
135                if (seekTimeUs >= 0) {
136                    int64_t diff = timestampUs - seekTimeUs;
137
138                    if (diff < 0) {
139                        diff = -diff;
140                    }
141
142                    if ((gReproduceBug == 4 && diff > 500000)
143                        || (gReproduceBug == 5 && timestampUs < 0)) {
144                        printf("wanted: %.2f secs, got: %.2f secs\n",
145                               seekTimeUs / 1E6, timestampUs / 1E6);
146
147                        printf("ERROR: ");
148                        failed = true;
149                    }
150                }
151
152                printf("buffer has timestamp %lld us (%.2f secs)\n",
153                       timestampUs, timestampUs / 1E6);
154
155                buffer->release();
156                buffer = NULL;
157
158                if (failed) {
159                    break;
160                }
161
162                shouldSeek = ((double)rand() / RAND_MAX) < 0.1;
163
164                if (gReproduceBug == 3) {
165                    shouldSeek = false;
166                }
167            }
168
169            seekTimeUs = -1;
170
171            if (shouldSeek) {
172                seekTimeUs = (rand() * (float)durationUs) / RAND_MAX;
173                options.setSeekTo(seekTimeUs);
174
175                printf("seeking to %lld us (%.2f secs)\n",
176                       seekTimeUs, seekTimeUs / 1E6);
177            }
178        }
179
180        rawSource->stop();
181
182        return;
183    }
184
185    int n = 0;
186    int64_t startTime = getNowUs();
187
188    long numIterationsLeft = gNumRepetitions;
189    MediaSource::ReadOptions options;
190
191    int64_t sumDecodeUs = 0;
192    int64_t totalBytes = 0;
193
194    while (numIterationsLeft-- > 0) {
195        long numFrames = 0;
196
197        MediaBuffer *buffer;
198
199        for (;;) {
200            int64_t startDecodeUs = getNowUs();
201            status_t err = rawSource->read(&buffer, &options);
202            int64_t delayDecodeUs = getNowUs() - startDecodeUs;
203
204            options.clearSeekTo();
205
206            if (err != OK) {
207                CHECK_EQ(buffer, NULL);
208
209                if (err == INFO_FORMAT_CHANGED) {
210                    printf("format changed.\n");
211                    continue;
212                }
213
214                break;
215            }
216
217            if (buffer->range_length() > 0 && (n++ % 16) == 0) {
218                printf(".");
219                fflush(stdout);
220            }
221
222            sumDecodeUs += delayDecodeUs;
223            totalBytes += buffer->range_length();
224
225            buffer->release();
226            buffer = NULL;
227
228            ++numFrames;
229            if (gMaxNumFrames > 0 && numFrames == gMaxNumFrames) {
230                break;
231            }
232
233            if (gReproduceBug == 1 && numFrames == 40) {
234                printf("seeking past the end now.");
235                options.setSeekTo(0x7fffffffL);
236            } else if (gReproduceBug == 2 && numFrames == 40) {
237                printf("seeking to 5 secs.");
238                options.setSeekTo(5000000);
239            }
240        }
241
242        printf("$");
243        fflush(stdout);
244
245        options.setSeekTo(0);
246    }
247
248    rawSource->stop();
249    printf("\n");
250
251    int64_t delay = getNowUs() - startTime;
252    if (!strncasecmp("video/", mime, 6)) {
253        printf("avg. %.2f fps\n", n * 1E6 / delay);
254
255        printf("avg. time to decode one buffer %.2f usecs\n",
256               (double)sumDecodeUs / n);
257
258        printf("decoded a total of %d frame(s).\n", n);
259    } else if (!strncasecmp("audio/", mime, 6)) {
260        // Frame count makes less sense for audio, as the output buffer
261        // sizes may be different across decoders.
262        printf("avg. %.2f KB/sec\n", totalBytes / 1024 * 1E6 / delay);
263
264        printf("decoded a total of %lld bytes\n", totalBytes);
265    }
266}
267
268static void writeSourceToMP4(const sp<MediaSource> &source) {
269    sp<MPEG4Writer> writer =
270        new MPEG4Writer(gWriteMP4Filename.string());
271
272    CHECK_EQ(writer->addSource(source), OK);
273
274    sp<MetaData> params = new MetaData;
275    CHECK_EQ(writer->start(), OK);
276
277    while (!writer->reachedEOS()) {
278        usleep(100000);
279    }
280    writer->stop();
281}
282
283static void performSeekTest(const sp<MediaSource> &source) {
284    CHECK_EQ(OK, source->start());
285
286    int64_t durationUs;
287    CHECK(source->getFormat()->findInt64(kKeyDuration, &durationUs));
288
289    for (int64_t seekTimeUs = 0; seekTimeUs <= durationUs;
290            seekTimeUs += 60000ll) {
291        MediaSource::ReadOptions options;
292        options.setSeekTo(
293                seekTimeUs, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC);
294
295        MediaBuffer *buffer;
296        status_t err;
297        for (;;) {
298            err = source->read(&buffer, &options);
299
300            options.clearSeekTo();
301
302            if (err == INFO_FORMAT_CHANGED) {
303                CHECK(buffer == NULL);
304                continue;
305            }
306
307            if (err != OK) {
308                CHECK(buffer == NULL);
309                break;
310            }
311
312            if (buffer->range_length() > 0) {
313                break;
314            }
315
316            CHECK(buffer != NULL);
317
318            buffer->release();
319            buffer = NULL;
320        }
321
322        if (err == OK) {
323            int64_t timeUs;
324            CHECK(buffer->meta_data()->findInt64(kKeyTime, &timeUs));
325
326            printf("%lld\t%lld\t%lld\n", seekTimeUs, timeUs, seekTimeUs - timeUs);
327
328            buffer->release();
329            buffer = NULL;
330        } else {
331            printf("ERROR\n");
332            break;
333        }
334    }
335
336    CHECK_EQ(OK, source->stop());
337}
338
339static void usage(const char *me) {
340    fprintf(stderr, "usage: %s\n", me);
341    fprintf(stderr, "       -h(elp)\n");
342    fprintf(stderr, "       -a(udio)\n");
343    fprintf(stderr, "       -n repetitions\n");
344    fprintf(stderr, "       -l(ist) components\n");
345    fprintf(stderr, "       -m max-number-of-frames-to-decode in each pass\n");
346    fprintf(stderr, "       -b bug to reproduce\n");
347    fprintf(stderr, "       -p(rofiles) dump decoder profiles supported\n");
348    fprintf(stderr, "       -t(humbnail) extract video thumbnail or album art\n");
349    fprintf(stderr, "       -s(oftware) prefer software codec\n");
350    fprintf(stderr, "       -o playback audio\n");
351    fprintf(stderr, "       -w(rite) filename (write to .mp4 file)\n");
352    fprintf(stderr, "       -k seek test\n");
353}
354
355int main(int argc, char **argv) {
356    android::ProcessState::self()->startThreadPool();
357
358    bool audioOnly = false;
359    bool listComponents = false;
360    bool dumpProfiles = false;
361    bool extractThumbnail = false;
362    bool seekTest = false;
363    gNumRepetitions = 1;
364    gMaxNumFrames = 0;
365    gReproduceBug = -1;
366    gPreferSoftwareCodec = false;
367    gPlaybackAudio = false;
368    gWriteMP4 = false;
369
370    sp<ALooper> looper;
371    sp<ARTSPController> rtspController;
372
373    int res;
374    while ((res = getopt(argc, argv, "han:lm:b:ptsow:k")) >= 0) {
375        switch (res) {
376            case 'a':
377            {
378                audioOnly = true;
379                break;
380            }
381
382            case 'l':
383            {
384                listComponents = true;
385                break;
386            }
387
388            case 'm':
389            case 'n':
390            case 'b':
391            {
392                char *end;
393                long x = strtol(optarg, &end, 10);
394
395                if (*end != '\0' || end == optarg || x <= 0) {
396                    x = 1;
397                }
398
399                if (res == 'n') {
400                    gNumRepetitions = x;
401                } else if (res == 'm') {
402                    gMaxNumFrames = x;
403                } else {
404                    CHECK_EQ(res, 'b');
405                    gReproduceBug = x;
406                }
407                break;
408            }
409
410            case 'w':
411            {
412                gWriteMP4 = true;
413                gWriteMP4Filename.setTo(optarg);
414                break;
415            }
416
417            case 'p':
418            {
419                dumpProfiles = true;
420                break;
421            }
422
423            case 't':
424            {
425                extractThumbnail = true;
426                break;
427            }
428
429            case 's':
430            {
431                gPreferSoftwareCodec = true;
432                break;
433            }
434
435            case 'o':
436            {
437                gPlaybackAudio = true;
438                break;
439            }
440
441            case 'k':
442            {
443                seekTest = true;
444                break;
445            }
446
447            case '?':
448            case 'h':
449            default:
450            {
451                usage(argv[0]);
452                exit(1);
453                break;
454            }
455        }
456    }
457
458    if (gPlaybackAudio && !audioOnly) {
459        // This doesn't make any sense if we're decoding the video track.
460        gPlaybackAudio = false;
461    }
462
463    argc -= optind;
464    argv += optind;
465
466    if (extractThumbnail) {
467        sp<IServiceManager> sm = defaultServiceManager();
468        sp<IBinder> binder = sm->getService(String16("media.player"));
469        sp<IMediaPlayerService> service =
470            interface_cast<IMediaPlayerService>(binder);
471
472        CHECK(service.get() != NULL);
473
474        sp<IMediaMetadataRetriever> retriever =
475            service->createMetadataRetriever(getpid());
476
477        CHECK(retriever != NULL);
478
479        for (int k = 0; k < argc; ++k) {
480            const char *filename = argv[k];
481
482            CHECK_EQ(retriever->setDataSource(filename), OK);
483            CHECK_EQ(retriever->setMode(
484                        METADATA_MODE_FRAME_CAPTURE_AND_METADATA_RETRIEVAL),
485                     OK);
486
487            sp<IMemory> mem = retriever->captureFrame();
488
489            if (mem != NULL) {
490                printf("captureFrame(%s) => OK\n", filename);
491            } else {
492                mem = retriever->extractAlbumArt();
493
494                if (mem != NULL) {
495                    printf("extractAlbumArt(%s) => OK\n", filename);
496                } else {
497                    printf("both captureFrame and extractAlbumArt "
498                           "failed on file '%s'.\n", filename);
499                }
500            }
501        }
502
503        return 0;
504    }
505
506    if (dumpProfiles) {
507        sp<IServiceManager> sm = defaultServiceManager();
508        sp<IBinder> binder = sm->getService(String16("media.player"));
509        sp<IMediaPlayerService> service =
510            interface_cast<IMediaPlayerService>(binder);
511
512        CHECK(service.get() != NULL);
513
514        sp<IOMX> omx = service->getOMX();
515        CHECK(omx.get() != NULL);
516
517        const char *kMimeTypes[] = {
518            MEDIA_MIMETYPE_VIDEO_AVC, MEDIA_MIMETYPE_VIDEO_MPEG4,
519            MEDIA_MIMETYPE_VIDEO_H263, MEDIA_MIMETYPE_AUDIO_AAC,
520            MEDIA_MIMETYPE_AUDIO_AMR_NB, MEDIA_MIMETYPE_AUDIO_AMR_WB,
521            MEDIA_MIMETYPE_AUDIO_MPEG
522        };
523
524        for (size_t k = 0; k < sizeof(kMimeTypes) / sizeof(kMimeTypes[0]);
525             ++k) {
526            printf("type '%s':\n", kMimeTypes[k]);
527
528            Vector<CodecCapabilities> results;
529            CHECK_EQ(QueryCodecs(omx, kMimeTypes[k],
530                                 true, // queryDecoders
531                                 &results), OK);
532
533            for (size_t i = 0; i < results.size(); ++i) {
534                printf("  decoder '%s' supports ",
535                       results[i].mComponentName.string());
536
537                if (results[i].mProfileLevels.size() == 0) {
538                    printf("NOTHING.\n");
539                    continue;
540                }
541
542                for (size_t j = 0; j < results[i].mProfileLevels.size(); ++j) {
543                    const CodecProfileLevel &profileLevel =
544                        results[i].mProfileLevels[j];
545
546                    printf("%s%ld/%ld", j > 0 ? ", " : "",
547                           profileLevel.mProfile, profileLevel.mLevel);
548                }
549
550                printf("\n");
551            }
552        }
553    }
554
555    if (listComponents) {
556        sp<IServiceManager> sm = defaultServiceManager();
557        sp<IBinder> binder = sm->getService(String16("media.player"));
558        sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder);
559
560        CHECK(service.get() != NULL);
561
562        sp<IOMX> omx = service->getOMX();
563        CHECK(omx.get() != NULL);
564
565        List<IOMX::ComponentInfo> list;
566        omx->listNodes(&list);
567
568        for (List<IOMX::ComponentInfo>::iterator it = list.begin();
569             it != list.end(); ++it) {
570            printf("%s\n", (*it).mName.string());
571        }
572    }
573
574    DataSource::RegisterDefaultSniffers();
575
576    OMXClient client;
577    status_t err = client.connect();
578
579    for (int k = 0; k < argc; ++k) {
580        const char *filename = argv[k];
581
582        sp<DataSource> dataSource = DataSource::CreateFromURI(filename);
583
584        if ((strncasecmp(filename, "sine:", 5)
585                && strncasecmp(filename, "rtsp://", 7)) && dataSource == NULL) {
586            fprintf(stderr, "Unable to create data source.\n");
587            return 1;
588        }
589
590        bool isJPEG = false;
591
592        size_t len = strlen(filename);
593        if (len >= 4 && !strcasecmp(filename + len - 4, ".jpg")) {
594            isJPEG = true;
595        }
596
597        sp<MediaSource> mediaSource;
598
599        if (isJPEG) {
600            mediaSource = new JPEGSource(dataSource);
601        } else if (!strncasecmp("sine:", filename, 5)) {
602            char *end;
603            long sampleRate = strtol(filename + 5, &end, 10);
604
605            if (end == filename + 5) {
606                sampleRate = 44100;
607            }
608            mediaSource = new SineSource(sampleRate, 1);
609        } else {
610            sp<MediaExtractor> extractor;
611
612            if (!strncasecmp("rtsp://", filename, 7)) {
613                if (looper == NULL) {
614                    looper = new ALooper;
615                    looper->start();
616                }
617
618                rtspController = new ARTSPController(looper);
619                status_t err = rtspController->connect(filename);
620                if (err != OK) {
621                    fprintf(stderr, "could not connect to rtsp server.\n");
622                    return -1;
623                }
624
625                extractor = rtspController.get();
626            } else {
627                extractor = MediaExtractor::Create(dataSource);
628                if (extractor == NULL) {
629                    fprintf(stderr, "could not create extractor.\n");
630                    return -1;
631                }
632            }
633
634            size_t numTracks = extractor->countTracks();
635
636            sp<MetaData> meta;
637            size_t i;
638            for (i = 0; i < numTracks; ++i) {
639                meta = extractor->getTrackMetaData(
640                        i, MediaExtractor::kIncludeExtensiveMetaData);
641
642                const char *mime;
643                meta->findCString(kKeyMIMEType, &mime);
644
645                if (audioOnly && !strncasecmp(mime, "audio/", 6)) {
646                    break;
647                }
648
649                if (!audioOnly && !strncasecmp(mime, "video/", 6)) {
650                    break;
651                }
652
653                meta = NULL;
654            }
655
656            if (meta == NULL) {
657                fprintf(stderr,
658                        "No suitable %s track found. The '-a' option will "
659                        "target audio tracks only, the default is to target "
660                        "video tracks only.\n",
661                        audioOnly ? "audio" : "video");
662                return -1;
663            }
664
665            int64_t thumbTimeUs;
666            if (meta->findInt64(kKeyThumbnailTime, &thumbTimeUs)) {
667                printf("thumbnailTime: %lld us (%.2f secs)\n",
668                       thumbTimeUs, thumbTimeUs / 1E6);
669            }
670
671            mediaSource = extractor->getTrack(i);
672        }
673
674        if (gWriteMP4) {
675            writeSourceToMP4(mediaSource);
676        } else if (seekTest) {
677            performSeekTest(mediaSource);
678        } else {
679            playSource(&client, mediaSource);
680        }
681
682        if (rtspController != NULL) {
683            rtspController->disconnect();
684            rtspController.clear();
685
686            sleep(3);
687        }
688    }
689
690    client.disconnect();
691
692    return 0;
693}
694