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