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