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