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