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