stagefright.cpp revision 19c9a1e2bb6ead5e1e895aea8be573fe0f8cc7bb
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 usage(const char *me) {
282    fprintf(stderr, "usage: %s\n", me);
283    fprintf(stderr, "       -h(elp)\n");
284    fprintf(stderr, "       -a(udio)\n");
285    fprintf(stderr, "       -n repetitions\n");
286    fprintf(stderr, "       -l(ist) components\n");
287    fprintf(stderr, "       -m max-number-of-frames-to-decode in each pass\n");
288    fprintf(stderr, "       -b bug to reproduce\n");
289    fprintf(stderr, "       -p(rofiles) dump decoder profiles supported\n");
290    fprintf(stderr, "       -t(humbnail) extract video thumbnail or album art\n");
291    fprintf(stderr, "       -s(oftware) prefer software codec\n");
292    fprintf(stderr, "       -o playback audio\n");
293    fprintf(stderr, "       -w(rite) filename (write to .mp4 file)\n");
294}
295
296int main(int argc, char **argv) {
297    android::ProcessState::self()->startThreadPool();
298
299    bool audioOnly = false;
300    bool listComponents = false;
301    bool dumpProfiles = false;
302    bool extractThumbnail = false;
303    gNumRepetitions = 1;
304    gMaxNumFrames = 0;
305    gReproduceBug = -1;
306    gPreferSoftwareCodec = false;
307    gPlaybackAudio = false;
308    gWriteMP4 = false;
309
310    int res;
311    while ((res = getopt(argc, argv, "han:lm:b:ptsow:")) >= 0) {
312        switch (res) {
313            case 'a':
314            {
315                audioOnly = true;
316                break;
317            }
318
319            case 'l':
320            {
321                listComponents = true;
322                break;
323            }
324
325            case 'm':
326            case 'n':
327            case 'b':
328            {
329                char *end;
330                long x = strtol(optarg, &end, 10);
331
332                if (*end != '\0' || end == optarg || x <= 0) {
333                    x = 1;
334                }
335
336                if (res == 'n') {
337                    gNumRepetitions = x;
338                } else if (res == 'm') {
339                    gMaxNumFrames = x;
340                } else {
341                    CHECK_EQ(res, 'b');
342                    gReproduceBug = x;
343                }
344                break;
345            }
346
347            case 'w':
348            {
349                gWriteMP4 = true;
350                gWriteMP4Filename.setTo(optarg);
351                break;
352            }
353
354            case 'p':
355            {
356                dumpProfiles = true;
357                break;
358            }
359
360            case 't':
361            {
362                extractThumbnail = true;
363                break;
364            }
365
366            case 's':
367            {
368                gPreferSoftwareCodec = true;
369                break;
370            }
371
372            case 'o':
373            {
374                gPlaybackAudio = true;
375                break;
376            }
377
378            case '?':
379            case 'h':
380            default:
381            {
382                usage(argv[0]);
383                exit(1);
384                break;
385            }
386        }
387    }
388
389    if (gPlaybackAudio && !audioOnly) {
390        // This doesn't make any sense if we're decoding the video track.
391        gPlaybackAudio = false;
392    }
393
394    argc -= optind;
395    argv += optind;
396
397    if (extractThumbnail) {
398        sp<IServiceManager> sm = defaultServiceManager();
399        sp<IBinder> binder = sm->getService(String16("media.player"));
400        sp<IMediaPlayerService> service =
401            interface_cast<IMediaPlayerService>(binder);
402
403        CHECK(service.get() != NULL);
404
405        sp<IMediaMetadataRetriever> retriever =
406            service->createMetadataRetriever(getpid());
407
408        CHECK(retriever != NULL);
409
410        for (int k = 0; k < argc; ++k) {
411            const char *filename = argv[k];
412
413            CHECK_EQ(retriever->setDataSource(filename), OK);
414            CHECK_EQ(retriever->setMode(
415                        METADATA_MODE_FRAME_CAPTURE_AND_METADATA_RETRIEVAL),
416                     OK);
417
418            sp<IMemory> mem = retriever->captureFrame();
419
420            if (mem != NULL) {
421                printf("captureFrame(%s) => OK\n", filename);
422            } else {
423                mem = retriever->extractAlbumArt();
424
425                if (mem != NULL) {
426                    printf("extractAlbumArt(%s) => OK\n", filename);
427                } else {
428                    printf("both captureFrame and extractAlbumArt "
429                           "failed on file '%s'.\n", filename);
430                }
431            }
432        }
433
434        return 0;
435    }
436
437    if (dumpProfiles) {
438        sp<IServiceManager> sm = defaultServiceManager();
439        sp<IBinder> binder = sm->getService(String16("media.player"));
440        sp<IMediaPlayerService> service =
441            interface_cast<IMediaPlayerService>(binder);
442
443        CHECK(service.get() != NULL);
444
445        sp<IOMX> omx = service->getOMX();
446        CHECK(omx.get() != NULL);
447
448        const char *kMimeTypes[] = {
449            MEDIA_MIMETYPE_VIDEO_AVC, MEDIA_MIMETYPE_VIDEO_MPEG4,
450            MEDIA_MIMETYPE_VIDEO_H263, MEDIA_MIMETYPE_AUDIO_AAC,
451            MEDIA_MIMETYPE_AUDIO_AMR_NB, MEDIA_MIMETYPE_AUDIO_AMR_WB,
452            MEDIA_MIMETYPE_AUDIO_MPEG
453        };
454
455        for (size_t k = 0; k < sizeof(kMimeTypes) / sizeof(kMimeTypes[0]);
456             ++k) {
457            printf("type '%s':\n", kMimeTypes[k]);
458
459            Vector<CodecCapabilities> results;
460            CHECK_EQ(QueryCodecs(omx, kMimeTypes[k],
461                                 true, // queryDecoders
462                                 &results), OK);
463
464            for (size_t i = 0; i < results.size(); ++i) {
465                printf("  decoder '%s' supports ",
466                       results[i].mComponentName.string());
467
468                if (results[i].mProfileLevels.size() == 0) {
469                    printf("NOTHING.\n");
470                    continue;
471                }
472
473                for (size_t j = 0; j < results[i].mProfileLevels.size(); ++j) {
474                    const CodecProfileLevel &profileLevel =
475                        results[i].mProfileLevels[j];
476
477                    printf("%s%ld/%ld", j > 0 ? ", " : "",
478                           profileLevel.mProfile, profileLevel.mLevel);
479                }
480
481                printf("\n");
482            }
483        }
484    }
485
486    if (listComponents) {
487        sp<IServiceManager> sm = defaultServiceManager();
488        sp<IBinder> binder = sm->getService(String16("media.player"));
489        sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder);
490
491        CHECK(service.get() != NULL);
492
493        sp<IOMX> omx = service->getOMX();
494        CHECK(omx.get() != NULL);
495
496        List<IOMX::ComponentInfo> list;
497        omx->listNodes(&list);
498
499        for (List<IOMX::ComponentInfo>::iterator it = list.begin();
500             it != list.end(); ++it) {
501            printf("%s\n", (*it).mName.string());
502        }
503    }
504
505    DataSource::RegisterDefaultSniffers();
506
507    OMXClient client;
508    status_t err = client.connect();
509
510    for (int k = 0; k < argc; ++k) {
511        const char *filename = argv[k];
512
513        sp<DataSource> dataSource = DataSource::CreateFromURI(filename);
514
515        if (strncasecmp(filename, "sine:", 5) && dataSource == NULL) {
516            fprintf(stderr, "Unable to create data source.\n");
517            return 1;
518        }
519
520        bool isJPEG = false;
521
522        size_t len = strlen(filename);
523        if (len >= 4 && !strcasecmp(filename + len - 4, ".jpg")) {
524            isJPEG = true;
525        }
526
527        sp<MediaSource> mediaSource;
528
529        if (isJPEG) {
530            mediaSource = new JPEGSource(dataSource);
531        } else if (!strncasecmp("sine:", filename, 5)) {
532            char *end;
533            long sampleRate = strtol(filename + 5, &end, 10);
534
535            if (end == filename + 5) {
536                sampleRate = 44100;
537            }
538            mediaSource = new SineSource(sampleRate, 1);
539        } else {
540            sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource);
541            if (extractor == NULL) {
542                fprintf(stderr, "could not create extractor.\n");
543                return -1;
544            }
545
546            size_t numTracks = extractor->countTracks();
547
548            sp<MetaData> meta;
549            size_t i;
550            for (i = 0; i < numTracks; ++i) {
551                meta = extractor->getTrackMetaData(
552                        i, MediaExtractor::kIncludeExtensiveMetaData);
553
554                const char *mime;
555                meta->findCString(kKeyMIMEType, &mime);
556
557                if (audioOnly && !strncasecmp(mime, "audio/", 6)) {
558                    break;
559                }
560
561                if (!audioOnly && !strncasecmp(mime, "video/", 6)) {
562                    break;
563                }
564
565                meta = NULL;
566            }
567
568            if (meta == NULL) {
569                fprintf(stderr,
570                        "No suitable %s track found. The '-a' option will "
571                        "target audio tracks only, the default is to target "
572                        "video tracks only.\n",
573                        audioOnly ? "audio" : "video");
574                return -1;
575            }
576
577            int64_t thumbTimeUs;
578            if (meta->findInt64(kKeyThumbnailTime, &thumbTimeUs)) {
579                printf("thumbnailTime: %lld us (%.2f secs)\n",
580                       thumbTimeUs, thumbTimeUs / 1E6);
581            }
582
583            mediaSource = extractor->getTrack(i);
584        }
585
586        if (gWriteMP4) {
587            writeSourceToMP4(mediaSource);
588        } else {
589            playSource(&client, mediaSource);
590        }
591    }
592
593    client.disconnect();
594
595    return 0;
596}
597