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