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 //#define LOG_NDEBUG 0 18 #define LOG_TAG "OMXHarness" 19 #include <utils/Log.h> 20 21 #include "OMXHarness.h" 22 23 #include <sys/time.h> 24 25 #include <binder/ProcessState.h> 26 #include <binder/IServiceManager.h> 27 #include <binder/MemoryDealer.h> 28 #include <media/IMediaPlayerService.h> 29 #include <media/stagefright/DataSource.h> 30 #include <media/stagefright/MediaBuffer.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/OMXCodec.h> 38 39 #define DEFAULT_TIMEOUT 500000 40 41 namespace android { 42 43 static int64_t getNowUs() { 44 struct timeval tv; 45 gettimeofday(&tv, NULL); 46 47 return (int64_t)tv.tv_usec + tv.tv_sec * 1000000; 48 } 49 50 Harness::Harness() 51 : mInitCheck(NO_INIT) { 52 mInitCheck = initOMX(); 53 } 54 55 Harness::~Harness() { 56 } 57 58 status_t Harness::initCheck() const { 59 return mInitCheck; 60 } 61 62 status_t Harness::initOMX() { 63 sp<IServiceManager> sm = defaultServiceManager(); 64 sp<IBinder> binder = sm->getService(String16("media.player")); 65 sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder); 66 mOMX = service->getOMX(); 67 68 return mOMX != 0 ? OK : NO_INIT; 69 } 70 71 void Harness::onMessage(const omx_message &msg) { 72 Mutex::Autolock autoLock(mLock); 73 mMessageQueue.push_back(msg); 74 mMessageAddedCondition.signal(); 75 } 76 77 status_t Harness::dequeueMessageForNode( 78 IOMX::node_id node, omx_message *msg, int64_t timeoutUs) { 79 return dequeueMessageForNodeIgnoringBuffers( 80 node, NULL, NULL, msg, timeoutUs); 81 } 82 83 // static 84 bool Harness::handleBufferMessage( 85 const omx_message &msg, 86 Vector<Buffer> *inputBuffers, 87 Vector<Buffer> *outputBuffers) { 88 switch (msg.type) { 89 case omx_message::EMPTY_BUFFER_DONE: 90 { 91 if (inputBuffers) { 92 for (size_t i = 0; i < inputBuffers->size(); ++i) { 93 if ((*inputBuffers)[i].mID == msg.u.buffer_data.buffer) { 94 inputBuffers->editItemAt(i).mFlags &= ~kBufferBusy; 95 return true; 96 } 97 } 98 CHECK(!"should not be here"); 99 } 100 break; 101 } 102 103 case omx_message::FILL_BUFFER_DONE: 104 { 105 if (outputBuffers) { 106 for (size_t i = 0; i < outputBuffers->size(); ++i) { 107 if ((*outputBuffers)[i].mID == msg.u.buffer_data.buffer) { 108 outputBuffers->editItemAt(i).mFlags &= ~kBufferBusy; 109 return true; 110 } 111 } 112 CHECK(!"should not be here"); 113 } 114 break; 115 } 116 117 default: 118 break; 119 } 120 121 return false; 122 } 123 124 status_t Harness::dequeueMessageForNodeIgnoringBuffers( 125 IOMX::node_id node, 126 Vector<Buffer> *inputBuffers, 127 Vector<Buffer> *outputBuffers, 128 omx_message *msg, int64_t timeoutUs) { 129 int64_t finishBy = getNowUs() + timeoutUs; 130 131 for (;;) { 132 Mutex::Autolock autoLock(mLock); 133 List<omx_message>::iterator it = mMessageQueue.begin(); 134 while (it != mMessageQueue.end()) { 135 if ((*it).node == node) { 136 if (handleBufferMessage(*it, inputBuffers, outputBuffers)) { 137 it = mMessageQueue.erase(it); 138 continue; 139 } 140 141 *msg = *it; 142 mMessageQueue.erase(it); 143 144 return OK; 145 } 146 147 ++it; 148 } 149 150 status_t err = (timeoutUs < 0) 151 ? mMessageAddedCondition.wait(mLock) 152 : mMessageAddedCondition.waitRelative( 153 mLock, (finishBy - getNowUs()) * 1000); 154 155 if (err == TIMED_OUT) { 156 return err; 157 } 158 CHECK_EQ(err, OK); 159 } 160 } 161 162 status_t Harness::getPortDefinition( 163 IOMX::node_id node, OMX_U32 portIndex, 164 OMX_PARAM_PORTDEFINITIONTYPE *def) { 165 def->nSize = sizeof(*def); 166 def->nVersion.s.nVersionMajor = 1; 167 def->nVersion.s.nVersionMinor = 0; 168 def->nVersion.s.nRevision = 0; 169 def->nVersion.s.nStep = 0; 170 def->nPortIndex = portIndex; 171 return mOMX->getParameter( 172 node, OMX_IndexParamPortDefinition, def, sizeof(*def)); 173 } 174 175 #define EXPECT(condition, info) \ 176 if (!(condition)) { \ 177 LOGE(info); printf("\n * " info "\n"); return UNKNOWN_ERROR; \ 178 } 179 180 #define EXPECT_SUCCESS(err, info) \ 181 EXPECT((err) == OK, info " failed") 182 183 status_t Harness::allocatePortBuffers( 184 const sp<MemoryDealer> &dealer, 185 IOMX::node_id node, OMX_U32 portIndex, 186 Vector<Buffer> *buffers) { 187 buffers->clear(); 188 189 OMX_PARAM_PORTDEFINITIONTYPE def; 190 status_t err = getPortDefinition(node, portIndex, &def); 191 EXPECT_SUCCESS(err, "getPortDefinition"); 192 193 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) { 194 Buffer buffer; 195 buffer.mMemory = dealer->allocate(def.nBufferSize); 196 buffer.mFlags = 0; 197 CHECK(buffer.mMemory != NULL); 198 199 err = mOMX->allocateBufferWithBackup( 200 node, portIndex, buffer.mMemory, &buffer.mID); 201 EXPECT_SUCCESS(err, "allocateBuffer"); 202 203 buffers->push(buffer); 204 } 205 206 return OK; 207 } 208 209 status_t Harness::setRole(IOMX::node_id node, const char *role) { 210 OMX_PARAM_COMPONENTROLETYPE params; 211 params.nSize = sizeof(params); 212 params.nVersion.s.nVersionMajor = 1; 213 params.nVersion.s.nVersionMinor = 0; 214 params.nVersion.s.nRevision = 0; 215 params.nVersion.s.nStep = 0; 216 strncpy((char *)params.cRole, role, OMX_MAX_STRINGNAME_SIZE - 1); 217 params.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0'; 218 219 return mOMX->setParameter( 220 node, OMX_IndexParamStandardComponentRole, 221 ¶ms, sizeof(params)); 222 } 223 224 struct NodeReaper { 225 NodeReaper(const sp<Harness> &harness, IOMX::node_id node) 226 : mHarness(harness), 227 mNode(node) { 228 } 229 230 ~NodeReaper() { 231 if (mNode != 0) { 232 mHarness->mOMX->freeNode(mNode); 233 mNode = 0; 234 } 235 } 236 237 void disarm() { 238 mNode = 0; 239 } 240 241 private: 242 sp<Harness> mHarness; 243 IOMX::node_id mNode; 244 245 NodeReaper(const NodeReaper &); 246 NodeReaper &operator=(const NodeReaper &); 247 }; 248 249 static sp<MediaExtractor> CreateExtractorFromURI(const char *uri) { 250 sp<DataSource> source = DataSource::CreateFromURI(uri); 251 252 if (source == NULL) { 253 return NULL; 254 } 255 256 return MediaExtractor::Create(source); 257 } 258 259 static sp<MediaSource> MakeSource( 260 const char *uri, 261 const char *mimeType) { 262 sp<MediaExtractor> extractor = CreateExtractorFromURI(uri); 263 264 if (extractor == NULL) { 265 return NULL; 266 } 267 268 for (size_t i = 0; i < extractor->countTracks(); ++i) { 269 sp<MetaData> meta = extractor->getTrackMetaData(i); 270 271 const char *trackMIME; 272 CHECK(meta->findCString(kKeyMIMEType, &trackMIME)); 273 274 if (!strcasecmp(trackMIME, mimeType)) { 275 return extractor->getTrack(i); 276 } 277 } 278 279 return NULL; 280 } 281 282 status_t Harness::testStateTransitions( 283 const char *componentName, const char *componentRole) { 284 if (strncmp(componentName, "OMX.", 4)) { 285 // Non-OMX components, i.e. software decoders won't execute this 286 // test. 287 return OK; 288 } 289 290 sp<MemoryDealer> dealer = new MemoryDealer(16 * 1024 * 1024, "OMXHarness"); 291 IOMX::node_id node; 292 293 status_t err = 294 mOMX->allocateNode(componentName, this, &node); 295 EXPECT_SUCCESS(err, "allocateNode"); 296 297 NodeReaper reaper(this, node); 298 299 err = setRole(node, componentRole); 300 EXPECT_SUCCESS(err, "setRole"); 301 302 // Initiate transition Loaded->Idle 303 err = mOMX->sendCommand(node, OMX_CommandStateSet, OMX_StateIdle); 304 EXPECT_SUCCESS(err, "sendCommand(go-to-Idle)"); 305 306 omx_message msg; 307 err = dequeueMessageForNode(node, &msg, DEFAULT_TIMEOUT); 308 // Make sure node doesn't just transition to idle before we are done 309 // allocating all input and output buffers. 310 EXPECT(err == TIMED_OUT, 311 "Component must not transition from loaded to idle before " 312 "all input and output buffers are allocated."); 313 314 // Now allocate buffers. 315 Vector<Buffer> inputBuffers; 316 err = allocatePortBuffers(dealer, node, 0, &inputBuffers); 317 EXPECT_SUCCESS(err, "allocatePortBuffers(input)"); 318 319 err = dequeueMessageForNode(node, &msg, DEFAULT_TIMEOUT); 320 CHECK_EQ(err, TIMED_OUT); 321 322 Vector<Buffer> outputBuffers; 323 err = allocatePortBuffers(dealer, node, 1, &outputBuffers); 324 EXPECT_SUCCESS(err, "allocatePortBuffers(output)"); 325 326 err = dequeueMessageForNode(node, &msg, DEFAULT_TIMEOUT); 327 EXPECT(err == OK 328 && msg.type == omx_message::EVENT 329 && msg.u.event_data.event == OMX_EventCmdComplete 330 && msg.u.event_data.data1 == OMX_CommandStateSet 331 && msg.u.event_data.data2 == OMX_StateIdle, 332 "Component did not properly transition to idle state " 333 "after all input and output buffers were allocated."); 334 335 // Initiate transition Idle->Executing 336 err = mOMX->sendCommand(node, OMX_CommandStateSet, OMX_StateExecuting); 337 EXPECT_SUCCESS(err, "sendCommand(go-to-Executing)"); 338 339 err = dequeueMessageForNode(node, &msg, DEFAULT_TIMEOUT); 340 EXPECT(err == OK 341 && msg.type == omx_message::EVENT 342 && msg.u.event_data.event == OMX_EventCmdComplete 343 && msg.u.event_data.data1 == OMX_CommandStateSet 344 && msg.u.event_data.data2 == OMX_StateExecuting, 345 "Component did not properly transition from idle to " 346 "executing state."); 347 348 for (size_t i = 0; i < outputBuffers.size(); ++i) { 349 err = mOMX->fillBuffer(node, outputBuffers[i].mID); 350 EXPECT_SUCCESS(err, "fillBuffer"); 351 352 outputBuffers.editItemAt(i).mFlags |= kBufferBusy; 353 } 354 355 err = mOMX->sendCommand(node, OMX_CommandFlush, 1); 356 EXPECT_SUCCESS(err, "sendCommand(flush-output-port)"); 357 358 err = dequeueMessageForNodeIgnoringBuffers( 359 node, &inputBuffers, &outputBuffers, &msg, DEFAULT_TIMEOUT); 360 EXPECT(err == OK 361 && msg.type == omx_message::EVENT 362 && msg.u.event_data.event == OMX_EventCmdComplete 363 && msg.u.event_data.data1 == OMX_CommandFlush 364 && msg.u.event_data.data2 == 1, 365 "Component did not properly acknowledge flushing the output port."); 366 367 for (size_t i = 0; i < outputBuffers.size(); ++i) { 368 EXPECT((outputBuffers[i].mFlags & kBufferBusy) == 0, 369 "Not all output buffers have been returned to us by the time " 370 "we received the flush-complete notification."); 371 } 372 373 for (size_t i = 0; i < outputBuffers.size(); ++i) { 374 err = mOMX->fillBuffer(node, outputBuffers[i].mID); 375 EXPECT_SUCCESS(err, "fillBuffer"); 376 377 outputBuffers.editItemAt(i).mFlags |= kBufferBusy; 378 } 379 380 // Initiate transition Executing->Idle 381 err = mOMX->sendCommand(node, OMX_CommandStateSet, OMX_StateIdle); 382 EXPECT_SUCCESS(err, "sendCommand(go-to-Idle)"); 383 384 err = dequeueMessageForNodeIgnoringBuffers( 385 node, &inputBuffers, &outputBuffers, &msg, DEFAULT_TIMEOUT); 386 EXPECT(err == OK 387 && msg.type == omx_message::EVENT 388 && msg.u.event_data.event == OMX_EventCmdComplete 389 && msg.u.event_data.data1 == OMX_CommandStateSet 390 && msg.u.event_data.data2 == OMX_StateIdle, 391 "Component did not properly transition to from executing to " 392 "idle state."); 393 394 for (size_t i = 0; i < inputBuffers.size(); ++i) { 395 EXPECT((inputBuffers[i].mFlags & kBufferBusy) == 0, 396 "Not all input buffers have been returned to us by the " 397 "time we received the transition-to-idle complete " 398 "notification."); 399 } 400 401 for (size_t i = 0; i < outputBuffers.size(); ++i) { 402 EXPECT((outputBuffers[i].mFlags & kBufferBusy) == 0, 403 "Not all output buffers have been returned to us by the " 404 "time we received the transition-to-idle complete " 405 "notification."); 406 } 407 408 // Initiate transition Idle->Loaded 409 err = mOMX->sendCommand(node, OMX_CommandStateSet, OMX_StateLoaded); 410 EXPECT_SUCCESS(err, "sendCommand(go-to-Loaded)"); 411 412 // Make sure node doesn't just transition to loaded before we are done 413 // freeing all input and output buffers. 414 err = dequeueMessageForNode(node, &msg, DEFAULT_TIMEOUT); 415 CHECK_EQ(err, TIMED_OUT); 416 417 for (size_t i = 0; i < inputBuffers.size(); ++i) { 418 err = mOMX->freeBuffer(node, 0, inputBuffers[i].mID); 419 EXPECT_SUCCESS(err, "freeBuffer"); 420 } 421 422 err = dequeueMessageForNode(node, &msg, DEFAULT_TIMEOUT); 423 CHECK_EQ(err, TIMED_OUT); 424 425 for (size_t i = 0; i < outputBuffers.size(); ++i) { 426 err = mOMX->freeBuffer(node, 1, outputBuffers[i].mID); 427 EXPECT_SUCCESS(err, "freeBuffer"); 428 } 429 430 err = dequeueMessageForNode(node, &msg, DEFAULT_TIMEOUT); 431 EXPECT(err == OK 432 && msg.type == omx_message::EVENT 433 && msg.u.event_data.event == OMX_EventCmdComplete 434 && msg.u.event_data.data1 == OMX_CommandStateSet 435 && msg.u.event_data.data2 == OMX_StateLoaded, 436 "Component did not properly transition to from idle to " 437 "loaded state after freeing all input and output buffers."); 438 439 err = mOMX->freeNode(node); 440 EXPECT_SUCCESS(err, "freeNode"); 441 442 reaper.disarm(); 443 444 node = 0; 445 446 return OK; 447 } 448 449 static const char *GetMimeFromComponentRole(const char *componentRole) { 450 struct RoleToMime { 451 const char *mRole; 452 const char *mMime; 453 }; 454 const RoleToMime kRoleToMime[] = { 455 { "video_decoder.avc", "video/avc" }, 456 { "video_decoder.mpeg4", "video/mp4v-es" }, 457 { "video_decoder.h263", "video/3gpp" }, 458 { "video_decoder.vpx", "video/x-vnd.on2.vp8" }, 459 460 // we appear to use this as a synonym to amrnb. 461 { "audio_decoder.amr", "audio/3gpp" }, 462 463 { "audio_decoder.amrnb", "audio/3gpp" }, 464 { "audio_decoder.amrwb", "audio/amr-wb" }, 465 { "audio_decoder.aac", "audio/mp4a-latm" }, 466 { "audio_decoder.mp3", "audio/mpeg" }, 467 { "audio_decoder.vorbis", "audio/vorbis" }, 468 { "audio_decoder.g711alaw", MEDIA_MIMETYPE_AUDIO_G711_ALAW }, 469 { "audio_decoder.g711mlaw", MEDIA_MIMETYPE_AUDIO_G711_MLAW }, 470 }; 471 472 for (size_t i = 0; i < sizeof(kRoleToMime) / sizeof(kRoleToMime[0]); ++i) { 473 if (!strcmp(componentRole, kRoleToMime[i].mRole)) { 474 return kRoleToMime[i].mMime; 475 } 476 } 477 478 return NULL; 479 } 480 481 static const char *GetURLForMime(const char *mime) { 482 struct MimeToURL { 483 const char *mMime; 484 const char *mURL; 485 }; 486 static const MimeToURL kMimeToURL[] = { 487 { "video/avc", 488 "file:///sdcard/media_api/video/H264_500_AAC_128.3gp" }, 489 { "video/mp4v-es", "file:///sdcard/media_api/video/MPEG4_320_AAC_64.mp4" }, 490 { "video/3gpp", 491 "file:///sdcard/media_api/video/H263_500_AMRNB_12.3gp" }, 492 { "audio/3gpp", 493 "file:///sdcard/media_api/video/H263_500_AMRNB_12.3gp" }, 494 { "audio/amr-wb", NULL }, 495 { "audio/mp4a-latm", 496 "file:///sdcard/media_api/video/H263_56_AAC_24.3gp" }, 497 { "audio/mpeg", 498 "file:///sdcard/media_api/music/MP3_48KHz_128kbps_s_1_17_CBR.mp3" }, 499 { "audio/vorbis", NULL }, 500 { "video/x-vnd.on2.vp8", 501 "file:///sdcard/media_api/video/big-buck-bunny_trailer.webm" }, 502 { MEDIA_MIMETYPE_AUDIO_G711_ALAW, "file:///sdcard/M1F1-Alaw-AFsp.wav" }, 503 { MEDIA_MIMETYPE_AUDIO_G711_MLAW, 504 "file:///sdcard/M1F1-mulaw-AFsp.wav" }, 505 }; 506 507 for (size_t i = 0; i < sizeof(kMimeToURL) / sizeof(kMimeToURL[0]); ++i) { 508 if (!strcasecmp(kMimeToURL[i].mMime, mime)) { 509 return kMimeToURL[i].mURL; 510 } 511 } 512 513 return NULL; 514 } 515 516 static sp<MediaSource> CreateSourceForMime(const char *mime) { 517 const char *url = GetURLForMime(mime); 518 CHECK(url != NULL); 519 520 sp<MediaExtractor> extractor = CreateExtractorFromURI(url); 521 522 if (extractor == NULL) { 523 return NULL; 524 } 525 526 for (size_t i = 0; i < extractor->countTracks(); ++i) { 527 sp<MetaData> meta = extractor->getTrackMetaData(i); 528 CHECK(meta != NULL); 529 530 const char *trackMime; 531 CHECK(meta->findCString(kKeyMIMEType, &trackMime)); 532 533 if (!strcasecmp(mime, trackMime)) { 534 return extractor->getTrack(i); 535 } 536 } 537 538 return NULL; 539 } 540 541 static double uniform_rand() { 542 return (double)rand() / RAND_MAX; 543 } 544 545 static bool CloseEnough(int64_t time1Us, int64_t time2Us) { 546 #if 0 547 int64_t diff = time1Us - time2Us; 548 if (diff < 0) { 549 diff = -diff; 550 } 551 552 return diff <= 50000; 553 #else 554 return time1Us == time2Us; 555 #endif 556 } 557 558 status_t Harness::testSeek( 559 const char *componentName, const char *componentRole) { 560 bool isEncoder = 561 !strncmp(componentRole, "audio_encoder.", 14) 562 || !strncmp(componentRole, "video_encoder.", 14); 563 564 if (isEncoder) { 565 // Not testing seek behaviour for encoders. 566 567 printf(" * Not testing seek functionality for encoders.\n"); 568 return OK; 569 } 570 571 const char *mime = GetMimeFromComponentRole(componentRole); 572 573 if (!mime) { 574 LOGI("Cannot perform seek test with this componentRole (%s)", 575 componentRole); 576 577 return OK; 578 } 579 580 sp<MediaSource> source = CreateSourceForMime(mime); 581 582 sp<MediaSource> seekSource = CreateSourceForMime(mime); 583 if (source == NULL || seekSource == NULL) { 584 return UNKNOWN_ERROR; 585 } 586 587 CHECK_EQ(seekSource->start(), OK); 588 589 sp<MediaSource> codec = OMXCodec::Create( 590 mOMX, source->getFormat(), false /* createEncoder */, 591 source, componentName); 592 593 CHECK(codec != NULL); 594 595 CHECK_EQ(codec->start(), OK); 596 597 int64_t durationUs; 598 CHECK(source->getFormat()->findInt64(kKeyDuration, &durationUs)); 599 600 LOGI("stream duration is %lld us (%.2f secs)", 601 durationUs, durationUs / 1E6); 602 603 static const int32_t kNumIterations = 5000; 604 605 // We are always going to seek beyond EOS in the first iteration (i == 0) 606 // followed by a linear read for the second iteration (i == 1). 607 // After that it's all random. 608 for (int32_t i = 0; i < kNumIterations; ++i) { 609 int64_t requestedSeekTimeUs; 610 int64_t actualSeekTimeUs; 611 MediaSource::ReadOptions options; 612 613 double r = uniform_rand(); 614 615 if ((i == 1) || (i > 0 && r < 0.5)) { 616 // 50% chance of just continuing to decode from last position. 617 618 requestedSeekTimeUs = -1; 619 620 LOGI("requesting linear read"); 621 } else { 622 if (i == 0 || r < 0.55) { 623 // 5% chance of seeking beyond end of stream. 624 625 requestedSeekTimeUs = durationUs; 626 627 LOGI("requesting seek beyond EOF"); 628 } else { 629 requestedSeekTimeUs = 630 (int64_t)(uniform_rand() * durationUs); 631 632 LOGI("requesting seek to %lld us (%.2f secs)", 633 requestedSeekTimeUs, requestedSeekTimeUs / 1E6); 634 } 635 636 MediaBuffer *buffer = NULL; 637 options.setSeekTo( 638 requestedSeekTimeUs, MediaSource::ReadOptions::SEEK_NEXT_SYNC); 639 640 if (seekSource->read(&buffer, &options) != OK) { 641 CHECK_EQ(buffer, NULL); 642 actualSeekTimeUs = -1; 643 } else { 644 CHECK(buffer != NULL); 645 CHECK(buffer->meta_data()->findInt64(kKeyTime, &actualSeekTimeUs)); 646 CHECK(actualSeekTimeUs >= 0); 647 648 buffer->release(); 649 buffer = NULL; 650 } 651 652 LOGI("nearest keyframe is at %lld us (%.2f secs)", 653 actualSeekTimeUs, actualSeekTimeUs / 1E6); 654 } 655 656 status_t err; 657 MediaBuffer *buffer; 658 for (;;) { 659 err = codec->read(&buffer, &options); 660 options.clearSeekTo(); 661 if (err == INFO_FORMAT_CHANGED) { 662 CHECK_EQ(buffer, NULL); 663 continue; 664 } 665 if (err == OK) { 666 CHECK(buffer != NULL); 667 if (buffer->range_length() == 0) { 668 buffer->release(); 669 buffer = NULL; 670 continue; 671 } 672 } else { 673 CHECK_EQ(buffer, NULL); 674 } 675 676 break; 677 } 678 679 if (requestedSeekTimeUs < 0) { 680 // Linear read. 681 if (err != OK) { 682 CHECK_EQ(buffer, NULL); 683 } else { 684 CHECK(buffer != NULL); 685 buffer->release(); 686 buffer = NULL; 687 } 688 } else if (actualSeekTimeUs < 0) { 689 EXPECT(err != OK, 690 "We attempted to seek beyond EOS and expected " 691 "ERROR_END_OF_STREAM to be returned, but instead " 692 "we got a valid buffer."); 693 EXPECT(err == ERROR_END_OF_STREAM, 694 "We attempted to seek beyond EOS and expected " 695 "ERROR_END_OF_STREAM to be returned, but instead " 696 "we found some other error."); 697 CHECK_EQ(err, ERROR_END_OF_STREAM); 698 CHECK_EQ(buffer, NULL); 699 } else { 700 EXPECT(err == OK, 701 "Expected a valid buffer to be returned from " 702 "OMXCodec::read."); 703 CHECK(buffer != NULL); 704 705 int64_t bufferTimeUs; 706 CHECK(buffer->meta_data()->findInt64(kKeyTime, &bufferTimeUs)); 707 if (!CloseEnough(bufferTimeUs, actualSeekTimeUs)) { 708 printf("\n * Attempted seeking to %lld us (%.2f secs)", 709 requestedSeekTimeUs, requestedSeekTimeUs / 1E6); 710 printf("\n * Nearest keyframe is at %lld us (%.2f secs)", 711 actualSeekTimeUs, actualSeekTimeUs / 1E6); 712 printf("\n * Returned buffer was at %lld us (%.2f secs)\n\n", 713 bufferTimeUs, bufferTimeUs / 1E6); 714 715 buffer->release(); 716 buffer = NULL; 717 718 CHECK_EQ(codec->stop(), OK); 719 720 return UNKNOWN_ERROR; 721 } 722 723 buffer->release(); 724 buffer = NULL; 725 } 726 } 727 728 CHECK_EQ(codec->stop(), OK); 729 730 return OK; 731 } 732 733 status_t Harness::test( 734 const char *componentName, const char *componentRole) { 735 printf("testing %s [%s] ... ", componentName, componentRole); 736 LOGI("testing %s [%s].", componentName, componentRole); 737 738 status_t err1 = testStateTransitions(componentName, componentRole); 739 status_t err2 = testSeek(componentName, componentRole); 740 741 if (err1 != OK) { 742 return err1; 743 } 744 745 return err2; 746 } 747 748 status_t Harness::testAll() { 749 List<IOMX::ComponentInfo> componentInfos; 750 status_t err = mOMX->listNodes(&componentInfos); 751 EXPECT_SUCCESS(err, "listNodes"); 752 753 for (List<IOMX::ComponentInfo>::iterator it = componentInfos.begin(); 754 it != componentInfos.end(); ++it) { 755 const IOMX::ComponentInfo &info = *it; 756 const char *componentName = info.mName.string(); 757 758 if (strncmp(componentName, "OMX.google.", 11)) { 759 continue; 760 } 761 762 for (List<String8>::const_iterator role_it = info.mRoles.begin(); 763 role_it != info.mRoles.end(); ++role_it) { 764 const char *componentRole = (*role_it).string(); 765 766 err = test(componentName, componentRole); 767 768 if (err == OK) { 769 printf("OK\n"); 770 } 771 } 772 } 773 774 return OK; 775 } 776 777 } // namespace android 778 779 static void usage(const char *me) { 780 fprintf(stderr, "usage: %s\n" 781 " -h(elp) Show this information\n" 782 " -s(eed) Set the random seed\n" 783 " [ component role ]\n\n" 784 "When launched without specifying a specific component " 785 "and role, tool will test all available OMX components " 786 "in all their supported roles. To determine available " 787 "component names, use \"stagefright -l\"\n" 788 "It's also a good idea to run a separate \"adb logcat\"" 789 " for additional debug and progress information.", me); 790 791 exit(0); 792 } 793 794 int main(int argc, char **argv) { 795 using namespace android; 796 797 android::ProcessState::self()->startThreadPool(); 798 DataSource::RegisterDefaultSniffers(); 799 800 const char *me = argv[0]; 801 802 unsigned long seed = 0xdeadbeef; 803 804 int res; 805 while ((res = getopt(argc, argv, "hs:")) >= 0) { 806 switch (res) { 807 case 's': 808 { 809 char *end; 810 unsigned long x = strtoul(optarg, &end, 10); 811 812 if (*end != '\0' || end == optarg) { 813 fprintf(stderr, "Malformed seed.\n"); 814 return 1; 815 } 816 817 seed = x; 818 break; 819 } 820 821 case '?': 822 fprintf(stderr, "\n"); 823 // fall through 824 825 case 'h': 826 default: 827 { 828 usage(me); 829 exit(1); 830 break; 831 } 832 } 833 } 834 835 argc -= optind; 836 argv += optind; 837 838 printf("To reproduce the conditions for this test, launch " 839 "with \"%s -s %lu\"\n", me, seed); 840 841 srand(seed); 842 843 sp<Harness> h = new Harness; 844 CHECK_EQ(h->initCheck(), OK); 845 846 if (argc == 0) { 847 h->testAll(); 848 } else if (argc == 2) { 849 if (h->test(argv[0], argv[1]) == OK) { 850 printf("OK\n"); 851 } 852 } 853 854 return 0; 855 } 856