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