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