DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "OmxDataDecoder.h"
#include "OMX_Types.h"
#include "OMX_Component.h"
#include "OMX_Audio.h"

extern mozilla::LogModule* GetPDMLog();

#ifdef LOG
#undef LOG
#endif

#define LOG(arg, ...) MOZ_LOG(GetPDMLog(), mozilla::LogLevel::Debug, ("OmxDataDecoder::%s: " arg, __func__, ##__VA_ARGS__))

#define CHECK_OMX_ERR(err)     \
  if (err != OMX_ErrorNone) {  \
    NotifyError(err, __func__);\
    return;                    \
  }                            \


namespace mozilla {

static const char*
StateTypeToStr(OMX_STATETYPE aType)
{
  MOZ_ASSERT(aType == OMX_StateLoaded ||
             aType == OMX_StateIdle ||
             aType == OMX_StateExecuting ||
             aType == OMX_StatePause ||
             aType == OMX_StateWaitForResources ||
             aType == OMX_StateInvalid);

  switch (aType) {
    case OMX_StateLoaded:
      return "OMX_StateLoaded";
    case OMX_StateIdle:
      return "OMX_StateIdle";
    case OMX_StateExecuting:
      return "OMX_StateExecuting";
    case OMX_StatePause:
      return "OMX_StatePause";
    case OMX_StateWaitForResources:
      return "OMX_StateWaitForResources";
    case OMX_StateInvalid:
      return "OMX_StateInvalid";
    default:
      return "Unknown";
  }
}

// There should be 2 ports and port number start from 0.
void GetPortIndex(nsTArray<uint32_t>& aPortIndex) {
  aPortIndex.AppendElement(0);
  aPortIndex.AppendElement(1);
}

OmxDataDecoder::OmxDataDecoder(const TrackInfo& aTrackInfo,
                               MediaDataDecoderCallback* aCallback)
  : mMonitor("OmxDataDecoder")
  , mOmxTaskQueue(CreateMediaDecodeTaskQueue())
  , mWatchManager(this, mOmxTaskQueue)
  , mOmxState(OMX_STATETYPE::OMX_StateInvalid, "OmxDataDecoder::mOmxState")
  , mTrackInfo(aTrackInfo.Clone())
  , mFlushing(false)
  , mShuttingDown(false)
  , mCheckingInputExhausted(false)
  , mPortSettingsChanged(-1, "OmxDataDecoder::mPortSettingsChanged")
  , mAudioCompactor(mAudioQueue)
  , mCallback(aCallback)
{
  LOG("(%p)", this);
  mOmxLayer = new OmxPromiseLayer(mOmxTaskQueue, this);

  nsCOMPtr<nsIRunnable> r =
    NS_NewRunnableMethod(this, &OmxDataDecoder::InitializationTask);
  mOmxTaskQueue->Dispatch(r.forget());
}

OmxDataDecoder::~OmxDataDecoder()
{
  LOG("(%p)", this);
  mWatchManager.Shutdown();
}

void
OmxDataDecoder::InitializationTask()
{
  mWatchManager.Watch(mOmxState, &OmxDataDecoder::OmxStateRunner);
  mWatchManager.Watch(mPortSettingsChanged, &OmxDataDecoder::PortSettingsChanged);
}

void
OmxDataDecoder::EndOfStream()
{
  LOG("(%p)", this);
  MOZ_ASSERT(mOmxTaskQueue->IsCurrentThreadIn());

  RefPtr<OmxDataDecoder> self = this;
  nsCOMPtr<nsIRunnable> r =
    NS_NewRunnableFunction([self] () {
      self->mCallback->DrainComplete();
    });
  mReaderTaskQueue->Dispatch(r.forget());
}

RefPtr<MediaDataDecoder::InitPromise>
OmxDataDecoder::Init()
{
  LOG("(%p)", this);
  mReaderTaskQueue = AbstractThread::GetCurrent()->AsTaskQueue();
  MOZ_ASSERT(mReaderTaskQueue);

  RefPtr<InitPromise> p = mInitPromise.Ensure(__func__);
  RefPtr<OmxDataDecoder> self = this;

  // TODO: it needs to get permission from resource manager before allocating
  //       Omx component.
  InvokeAsync(mOmxTaskQueue, mOmxLayer.get(), __func__, &OmxPromiseLayer::Init,
              mOmxTaskQueue, mTrackInfo.get())
    ->Then(mReaderTaskQueue, __func__,
      [self] () {
        // Omx state should be OMX_StateIdle.
        nsCOMPtr<nsIRunnable> r =
          NS_NewRunnableFunction([self] () {
            self->mOmxState = self->mOmxLayer->GetState();
            MOZ_ASSERT(self->mOmxState != OMX_StateIdle);
          });
        self->mOmxTaskQueue->Dispatch(r.forget());
      },
      [self] () {
        self->RejectInitPromise(DecoderFailureReason::INIT_ERROR, __func__);
      });

  return p;
}

nsresult
OmxDataDecoder::Input(MediaRawData* aSample)
{
  LOG("(%p) sample %p", this, aSample);
  MOZ_ASSERT(mInitPromise.IsEmpty());

  RefPtr<OmxDataDecoder> self = this;
  RefPtr<MediaRawData> sample = aSample;

  nsCOMPtr<nsIRunnable> r =
    NS_NewRunnableFunction([self, sample] () {
      self->mMediaRawDatas.AppendElement(sample);

      // Start to fill/empty buffers.
      if (self->mOmxState == OMX_StateIdle ||
          self->mOmxState == OMX_StateExecuting) {
        self->FillAndEmptyBuffers();
      }
    });
  mOmxTaskQueue->Dispatch(r.forget());

  return NS_OK;
}

nsresult
OmxDataDecoder::Flush()
{
  LOG("(%p)", this);

  mFlushing = true;

  nsCOMPtr<nsIRunnable> r =
    NS_NewRunnableMethod(this, &OmxDataDecoder::DoFlush);
  mOmxTaskQueue->Dispatch(r.forget());

  // According to the definition of Flush() in PDM:
  // "the decoder must be ready to accept new input for decoding".
  // So it needs to wait for the Omx to complete the flush command.
  MonitorAutoLock lock(mMonitor);
  while (mFlushing) {
    lock.Wait();
  }

  return NS_OK;
}

nsresult
OmxDataDecoder::Drain()
{
  LOG("(%p)", this);

  // TODO: For video decoding, it needs to copy the latest video frame to yuv
  //       and output to layer again, because all video buffers will be released
  //       later.
  nsCOMPtr<nsIRunnable> r =
    NS_NewRunnableMethod(this, &OmxDataDecoder::SendEosBuffer);
  mOmxTaskQueue->Dispatch(r.forget());

  return NS_OK;
}

nsresult
OmxDataDecoder::Shutdown()
{
  LOG("(%p)", this);

  mShuttingDown = true;

  nsCOMPtr<nsIRunnable> r =
    NS_NewRunnableMethod(this, &OmxDataDecoder::DoAsyncShutdown);
  mOmxTaskQueue->Dispatch(r.forget());

  {
    // DoAsyncShutdown() will be running for a while, it could be still running
    // when reader releasing the decoder and then it causes problem. To avoid it,
    // Shutdown() must block until DoAsyncShutdown() is completed.
    MonitorAutoLock lock(mMonitor);
    while (mShuttingDown) {
      lock.Wait();
    }
  }

  mOmxTaskQueue->BeginShutdown();
  mOmxTaskQueue->AwaitShutdownAndIdle();

  return NS_OK;
}

void
OmxDataDecoder::DoAsyncShutdown()
{
  LOG("(%p)", this);
  MOZ_ASSERT(mOmxTaskQueue->IsCurrentThreadIn());
  MOZ_ASSERT(mFlushing);

  mWatchManager.Unwatch(mOmxState, &OmxDataDecoder::OmxStateRunner);
  mWatchManager.Unwatch(mPortSettingsChanged, &OmxDataDecoder::PortSettingsChanged);

  // Flush to all ports, so all buffers can be returned from component.
  RefPtr<OmxDataDecoder> self = this;
  mOmxLayer->SendCommand(OMX_CommandFlush, OMX_ALL, nullptr)
    ->Then(mOmxTaskQueue, __func__,
           [self] () -> RefPtr<OmxCommandPromise> {
             LOG("DoAsyncShutdown: flush complete");
             return self->mOmxLayer->SendCommand(OMX_CommandStateSet, OMX_StateIdle, nullptr);
           },
           [self] () {
             self->mOmxLayer->Shutdown();
           })
    ->CompletionPromise()
    ->Then(mOmxTaskQueue, __func__,
           [self] () -> RefPtr<OmxCommandPromise> {
             RefPtr<OmxCommandPromise> p =
               self->mOmxLayer->SendCommand(OMX_CommandStateSet, OMX_StateLoaded, nullptr);

             LOG("DoAsyncShutdown: collecting buffers...");
             self->CollectBufferPromises(OMX_DirMax)
               ->Then(self->mOmxTaskQueue, __func__,
                   [self] () {
                   // According to spec 3.1.1.2.2.1:
                   // OMX_StateLoaded needs to be sent before releasing buffers.
                   // And state transition from OMX_StateIdle to OMX_StateLoaded
                   // is completed when all of the buffers have been removed
                   // from the component.
                   // Here the buffer promises are not resolved due to displaying
                   // in layer, it needs to wait before the layer returns the
                   // buffers.
                   LOG("DoAsyncShutdown: all buffers collected, releasing buffers...");
                   self->ReleaseBuffers(OMX_DirInput);
                   self->ReleaseBuffers(OMX_DirOutput);
                   },
                   [self] () {
                     self->mOmxLayer->Shutdown();
                   });

             return p;
           },
           [self] () {
             self->mOmxLayer->Shutdown();
           })
    ->CompletionPromise()
    ->Then(mOmxTaskQueue, __func__,
           [self] () {
             LOG("DoAsyncShutdown: OMX_StateLoaded, it is safe to shutdown omx");
             self->mOmxLayer->Shutdown();

             MonitorAutoLock lock(self->mMonitor);
             self->mShuttingDown = false;
             self->mMonitor.Notify();
           },
           [self] () {
             self->mOmxLayer->Shutdown();

             MonitorAutoLock lock(self->mMonitor);
             self->mShuttingDown = false;
             self->mMonitor.Notify();
           });
}

void
OmxDataDecoder::CheckIfInputExhausted()
{
  MOZ_ASSERT(mOmxTaskQueue->IsCurrentThreadIn());
  MOZ_ASSERT(!mCheckingInputExhausted);

  mCheckingInputExhausted = false;

  if (mMediaRawDatas.Length()) {
    return;
  }

  // When all input buffers are not in omx component, it means all samples have
  // been fed into OMX component.
  for (auto buf : mInPortBuffers) {
    if (buf->mStatus == BufferData::BufferStatus::OMX_COMPONENT) {
      return;
    }
  }

  // When all output buffers are held by component, it means client is waiting for output.
  for (auto buf : mOutPortBuffers) {
    if (buf->mStatus != BufferData::BufferStatus::OMX_COMPONENT) {
      return;
    }
  }

  LOG("Call InputExhausted()");
  mCallback->InputExhausted();
}

void
OmxDataDecoder::OutputAudio(BufferData* aBufferData)
{
  MOZ_ASSERT(mOmxTaskQueue->IsCurrentThreadIn());
  OMX_BUFFERHEADERTYPE* buf = aBufferData->mBuffer;
  AudioInfo* info = mTrackInfo->GetAsAudioInfo();
  if (buf->nFilledLen) {
    uint64_t offset = 0;
    uint32_t frames = buf->nFilledLen / (2 * info->mChannels);
    if (aBufferData->mRawData) {
      offset = aBufferData->mRawData->mOffset;
    }
    typedef AudioCompactor::NativeCopy OmxCopy;
    mAudioCompactor.Push(offset,
                         buf->nTimeStamp,
                         info->mRate,
                         frames,
                         info->mChannels,
                         OmxCopy(buf->pBuffer + buf->nOffset,
                                 buf->nFilledLen,
                                 info->mChannels));
    RefPtr<AudioData> audio = mAudioQueue.PopFront();
    mCallback->Output(audio);
  }
  aBufferData->mStatus = BufferData::BufferStatus::FREE;
}

void
OmxDataDecoder::FillBufferDone(BufferData* aData)
{
  MOZ_ASSERT(!aData || aData->mStatus == BufferData::BufferStatus::OMX_CLIENT);

  if (mTrackInfo->IsAudio()) {
    OutputAudio(aData);
  } else {
    MOZ_ASSERT(0);
  }

  if (aData->mBuffer->nFlags & OMX_BUFFERFLAG_EOS) {
    EndOfStream();
  } else {
    FillAndEmptyBuffers();

    // If the latest decoded sample's MediaRawData is also the latest input
    // sample, it means there is no input data in queue and component, calling
    // CheckIfInputExhausted().
    if (aData->mRawData == mLatestInputRawData && !mCheckingInputExhausted) {
      mCheckingInputExhausted = true;
      nsCOMPtr<nsIRunnable> r =
        NS_NewRunnableMethod(this, &OmxDataDecoder::CheckIfInputExhausted);
      mOmxTaskQueue->Dispatch(r.forget());
    }
  }
}

void
OmxDataDecoder::FillBufferFailure(OmxBufferFailureHolder aFailureHolder)
{
  NotifyError(aFailureHolder.mError, __func__);
}

void
OmxDataDecoder::EmptyBufferDone(BufferData* aData)
{
  MOZ_ASSERT(!aData || aData->mStatus == BufferData::BufferStatus::OMX_CLIENT);

  // Nothing to do when status of input buffer is OMX_CLIENT.
  aData->mStatus = BufferData::BufferStatus::FREE;
  FillAndEmptyBuffers();
}

void
OmxDataDecoder::EmptyBufferFailure(OmxBufferFailureHolder aFailureHolder)
{
  NotifyError(aFailureHolder.mError, __func__);
}

void
OmxDataDecoder::NotifyError(OMX_ERRORTYPE aError, const char* aLine)
{
  LOG("NotifyError %d at %s", aError, aLine);
  mCallback->Error();
}

void
OmxDataDecoder::FillAndEmptyBuffers()
{
  MOZ_ASSERT(mOmxTaskQueue->IsCurrentThreadIn());
  MOZ_ASSERT(mOmxState == OMX_StateExecuting);

  // During the port setting changed, it is forbidden to do any buffer operation.
  if (mPortSettingsChanged != -1 || mShuttingDown || mFlushing) {
    return;
  }

  // Trigger input port.
  while (!!mMediaRawDatas.Length()) {
    // input buffer must be usedi by component if there is data available.
    RefPtr<BufferData> inbuf = FindAvailableBuffer(OMX_DirInput);
    if (!inbuf) {
      LOG("no input buffer!");
      break;
    }

    RefPtr<MediaRawData> data = mMediaRawDatas[0];
    memcpy(inbuf->mBuffer->pBuffer, data->Data(), data->Size());
    inbuf->mBuffer->nFilledLen = data->Size();
    inbuf->mBuffer->nOffset = 0;
    // TODO: the frame size could larger than buffer size in video case.
    inbuf->mBuffer->nFlags = inbuf->mBuffer->nAllocLen > data->Size() ?
                             OMX_BUFFERFLAG_ENDOFFRAME : 0;
    inbuf->mBuffer->nTimeStamp = data->mTime;
    if (data->Size()) {
      inbuf->mRawData = mMediaRawDatas[0];
    } else {
       LOG("send EOS buffer");
      inbuf->mBuffer->nFlags |= OMX_BUFFERFLAG_EOS;
    }

    LOG("feed sample %p to omx component, len %d, flag %X", data.get(),
        inbuf->mBuffer->nFilledLen, inbuf->mBuffer->nFlags);
    mOmxLayer->EmptyBuffer(inbuf)->Then(mOmxTaskQueue, __func__, this,
                                        &OmxDataDecoder::EmptyBufferDone,
                                        &OmxDataDecoder::EmptyBufferFailure);
    mLatestInputRawData.swap(mMediaRawDatas[0]);
    mMediaRawDatas.RemoveElementAt(0);
  }

  // Trigger output port.
  while (true) {
    RefPtr<BufferData> outbuf = FindAvailableBuffer(OMX_DirOutput);
    if (!outbuf) {
      break;
    }

    mOmxLayer->FillBuffer(outbuf)->Then(mOmxTaskQueue, __func__, this,
                                        &OmxDataDecoder::FillBufferDone,
                                        &OmxDataDecoder::FillBufferFailure);
  }
}

OmxPromiseLayer::BufferData*
OmxDataDecoder::FindAvailableBuffer(OMX_DIRTYPE aType)
{
  BUFFERLIST* buffers = GetBuffers(aType);

  for (uint32_t i = 0; i < buffers->Length(); i++) {
    BufferData* buf = buffers->ElementAt(i);
    if (buf->mStatus == BufferData::BufferStatus::FREE) {
      return buf;
    }
  }

  return nullptr;
}

nsresult
OmxDataDecoder::AllocateBuffers(OMX_DIRTYPE aType)
{
  MOZ_ASSERT(mOmxTaskQueue->IsCurrentThreadIn());

  return mOmxLayer->AllocateOmxBuffer(aType, GetBuffers(aType));
}

nsresult
OmxDataDecoder::ReleaseBuffers(OMX_DIRTYPE aType)
{
  MOZ_ASSERT(mOmxTaskQueue->IsCurrentThreadIn());

  return mOmxLayer->ReleaseOmxBuffer(aType, GetBuffers(aType));
}

nsTArray<RefPtr<OmxPromiseLayer::BufferData>>*
OmxDataDecoder::GetBuffers(OMX_DIRTYPE aType)
{
  MOZ_ASSERT(aType == OMX_DIRTYPE::OMX_DirInput ||
             aType == OMX_DIRTYPE::OMX_DirOutput);

  if (aType == OMX_DIRTYPE::OMX_DirInput) {
    return &mInPortBuffers;
  }
  return &mOutPortBuffers;
}

void
OmxDataDecoder::ResolveInitPromise(const char* aMethodName)
{
  LOG("Resolved InitPromise");
  RefPtr<OmxDataDecoder> self = this;
  nsCOMPtr<nsIRunnable> r =
    NS_NewRunnableFunction([self, aMethodName] () {
      MOZ_ASSERT(self->mReaderTaskQueue->IsCurrentThreadIn());
      self->mInitPromise.ResolveIfExists(self->mTrackInfo->GetType(), aMethodName);
    });
  mReaderTaskQueue->Dispatch(r.forget());
}

void
OmxDataDecoder::RejectInitPromise(DecoderFailureReason aReason, const char* aMethodName)
{
  RefPtr<OmxDataDecoder> self = this;
  nsCOMPtr<nsIRunnable> r =
    NS_NewRunnableFunction([self, aReason, aMethodName] () {
      MOZ_ASSERT(self->mReaderTaskQueue->IsCurrentThreadIn());
      self->mInitPromise.RejectIfExists(aReason, aMethodName);
    });
  mReaderTaskQueue->Dispatch(r.forget());
}

void
OmxDataDecoder::OmxStateRunner()
{
  MOZ_ASSERT(mOmxTaskQueue->IsCurrentThreadIn());
  LOG("OMX state: %s", StateTypeToStr(mOmxState));

  // TODO: maybe it'd be better to use promise CompletionPromise() to replace
  //       this state machine.
  if (mOmxState == OMX_StateLoaded) {
    // Config codec parameters by minetype.
    if (mTrackInfo->IsAudio()) {
      ConfigAudioCodec();
    }

    // Send OpenMax state commane to OMX_StateIdle.
    RefPtr<OmxDataDecoder> self = this;
    mOmxLayer->SendCommand(OMX_CommandStateSet, OMX_StateIdle, nullptr)
      ->Then(mOmxTaskQueue, __func__,
             [self] () {
               // Current state should be OMX_StateIdle.
               self->mOmxState = self->mOmxLayer->GetState();
               MOZ_ASSERT(self->mOmxState == OMX_StateIdle);
             },
             [self] () {
               self->RejectInitPromise(DecoderFailureReason::INIT_ERROR, __func__);
             });

    // Allocate input and output buffers.
    OMX_DIRTYPE types[] = {OMX_DIRTYPE::OMX_DirInput, OMX_DIRTYPE::OMX_DirOutput};
    for(const auto id : types) {
      if (NS_FAILED(AllocateBuffers(id))) {
        LOG("Failed to allocate buffer on port %d", id);
        RejectInitPromise(DecoderFailureReason::INIT_ERROR, __func__);
        break;
      }
    }
  } else if (mOmxState == OMX_StateIdle) {
    RefPtr<OmxDataDecoder> self = this;
    mOmxLayer->SendCommand(OMX_CommandStateSet, OMX_StateExecuting, nullptr)
      ->Then(mOmxTaskQueue, __func__,
             [self] () {
               self->mOmxState = self->mOmxLayer->GetState();
               MOZ_ASSERT(self->mOmxState == OMX_StateExecuting);

               self->ResolveInitPromise(__func__);
             },
             [self] () {
               self->RejectInitPromise(DecoderFailureReason::INIT_ERROR, __func__);
             });
  } else if (mOmxState == OMX_StateExecuting) {
    // Config codec once it gets OMX_StateExecuting state.
    FillCodecConfigDataToOmx();
  } else {
    MOZ_ASSERT(0);
  }
}

void
OmxDataDecoder::ConfigAudioCodec()
{
  const AudioInfo* audioInfo = mTrackInfo->GetAsAudioInfo();
  OMX_ERRORTYPE err;

  // TODO: it needs to handle other formats like mp3, amr-nb...etc.
  if (audioInfo->mMimeType.EqualsLiteral("audio/mp4a-latm")) {
    OMX_AUDIO_PARAM_AACPROFILETYPE aac_profile;
    InitOmxParameter(&aac_profile);
    err = mOmxLayer->GetParameter(OMX_IndexParamAudioAac, &aac_profile, sizeof(aac_profile));
    CHECK_OMX_ERR(err);
    aac_profile.nSampleRate = audioInfo->mRate;
    aac_profile.nChannels = audioInfo->mChannels;
    aac_profile.eAACProfile = (OMX_AUDIO_AACPROFILETYPE)audioInfo->mProfile;
    err = mOmxLayer->SetParameter(OMX_IndexParamAudioAac, &aac_profile, sizeof(aac_profile));
    CHECK_OMX_ERR(err);
    LOG("Config OMX_IndexParamAudioAac, channel %d, sample rate %d, profile %d",
        audioInfo->mChannels, audioInfo->mRate, audioInfo->mProfile);
  }
}

void
OmxDataDecoder::FillCodecConfigDataToOmx()
{
  // Codec config data should be the first sample running on Omx TaskQueue.
  MOZ_ASSERT(mOmxTaskQueue->IsCurrentThreadIn());
  MOZ_ASSERT(!mMediaRawDatas.Length());
  MOZ_ASSERT(mOmxState == OMX_StateIdle || mOmxState == OMX_StateExecuting);


  RefPtr<BufferData> inbuf = FindAvailableBuffer(OMX_DirInput);
  if (mTrackInfo->IsAudio()) {
    AudioInfo* audio_info = mTrackInfo->GetAsAudioInfo();
    memcpy(inbuf->mBuffer->pBuffer,
           audio_info->mCodecSpecificConfig->Elements(),
           audio_info->mCodecSpecificConfig->Length());
    inbuf->mBuffer->nFilledLen = audio_info->mCodecSpecificConfig->Length();
    inbuf->mBuffer->nOffset = 0;
    inbuf->mBuffer->nFlags = (OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG);
  } else {
    MOZ_ASSERT(0);
  }

  LOG("Feed codec configure data to OMX component");
  mOmxLayer->EmptyBuffer(inbuf)->Then(mOmxTaskQueue, __func__, this,
                                      &OmxDataDecoder::EmptyBufferDone,
                                      &OmxDataDecoder::EmptyBufferFailure);
}

bool
OmxDataDecoder::Event(OMX_EVENTTYPE aEvent, OMX_U32 aData1, OMX_U32 aData2)
{
  MOZ_ASSERT(mOmxTaskQueue->IsCurrentThreadIn());

  if (mOmxLayer->Event(aEvent, aData1, aData2)) {
    return true;
  }

  switch (aEvent) {
    case OMX_EventPortSettingsChanged:
    {
      // According to spec: "To prevent the loss of any input data, the
      // component issuing the OMX_EventPortSettingsChanged event on its input
      // port should buffer all input port data that arrives between the
      // emission of the OMX_EventPortSettingsChanged event and the arrival of
      // the command to disable the input port."
      //
      // So client needs to disable port and reallocate buffers.
      MOZ_ASSERT(mPortSettingsChanged == -1);
      mPortSettingsChanged = aData1;
      LOG("Got OMX_EventPortSettingsChanged event");
      break;
    }
    default:
    {
      LOG("WARNING: got none handle event: %d, aData1: %d, aData2: %d",
          aEvent, aData1, aData2);
      return false;
    }
  }

  return true;
}

template<class T> void
OmxDataDecoder::InitOmxParameter(T* aParam)
{
  PodZero(aParam);
  aParam->nSize = sizeof(T);
  aParam->nVersion.s.nVersionMajor = 1;
}

bool
OmxDataDecoder::BuffersCanBeReleased(OMX_DIRTYPE aType)
{
  BUFFERLIST* buffers = GetBuffers(aType);
  uint32_t len = buffers->Length();
  for (uint32_t i = 0; i < len; i++) {
    BufferData::BufferStatus buf_status = buffers->ElementAt(i)->mStatus;
    if (buf_status == BufferData::BufferStatus::OMX_COMPONENT ||
        buf_status == BufferData::BufferStatus::OMX_CLIENT_OUTPUT) {
      return false;
    }
  }
  return true;
}

OMX_DIRTYPE
OmxDataDecoder::GetPortDirection(uint32_t aPortIndex)
{
  OMX_PARAM_PORTDEFINITIONTYPE def;
  InitOmxParameter(&def);
  def.nPortIndex = mPortSettingsChanged;

  OMX_ERRORTYPE err = mOmxLayer->GetParameter(OMX_IndexParamPortDefinition,
                                              &def,
                                              sizeof(def));
  if (err != OMX_ErrorNone) {
    return OMX_DirMax;
  }
  return def.eDir;
}

RefPtr<OmxPromiseLayer::OmxBufferPromise::AllPromiseType>
OmxDataDecoder::CollectBufferPromises(OMX_DIRTYPE aType)
{
  MOZ_ASSERT(mOmxTaskQueue->IsCurrentThreadIn());

  nsTArray<RefPtr<OmxBufferPromise>> promises;
  OMX_DIRTYPE types[] = {OMX_DIRTYPE::OMX_DirInput, OMX_DIRTYPE::OMX_DirOutput};
  for (const auto type : types) {
    if ((aType == type) || (aType == OMX_DirMax)) {
      // find the buffer which has promise.
      BUFFERLIST* buffers = GetBuffers(type);

      for (uint32_t i = 0; i < buffers->Length(); i++) {
        BufferData* buf = buffers->ElementAt(i);
        if (!buf->mPromise.IsEmpty()) {
          // OmxBufferPromise is not exclusive, it can be multiple "Then"s, so it
          // is safe to call "Ensure" here.
          promises.AppendElement(buf->mPromise.Ensure(__func__));
        }
      }
    }
  }

  LOG("CollectBufferPromises: type %d, total %d promiese", aType, promises.Length());
  if (promises.Length()) {
    return OmxBufferPromise::All(mOmxTaskQueue, promises);
  }

  nsTArray<BufferData*> headers;
  return OmxBufferPromise::AllPromiseType::CreateAndResolve(headers, __func__);
}

void
OmxDataDecoder::PortSettingsChanged()
{
  MOZ_ASSERT(mOmxTaskQueue->IsCurrentThreadIn());

  if (mPortSettingsChanged == -1 || mOmxState == OMX_STATETYPE::OMX_StateInvalid) {
    return;
  }

  // The PortSettingsChanged algorithm:
  //
  //   1. disable port.
  //   2. wait for port buffers return to client and then release these buffers.
  //   3. enable port.
  //   4. allocate port buffers.
  //

  // Disable port. Get port definition if the target port is enable.
  OMX_PARAM_PORTDEFINITIONTYPE def;
  InitOmxParameter(&def);
  def.nPortIndex = mPortSettingsChanged;

  OMX_ERRORTYPE err = mOmxLayer->GetParameter(OMX_IndexParamPortDefinition,
                                              &def,
                                              sizeof(def));
  CHECK_OMX_ERR(err);

  RefPtr<OmxDataDecoder> self = this;
  if (def.bEnabled) {
    // 1. disable port.
    LOG("PortSettingsChanged: disable port %d", def.nPortIndex);
    mOmxLayer->SendCommand(OMX_CommandPortDisable, mPortSettingsChanged, nullptr)
      ->Then(mOmxTaskQueue, __func__,
             [self, def] () -> RefPtr<OmxCommandPromise> {
               // 3. enable port.
               // Send enable port command.
               RefPtr<OmxCommandPromise> p =
                 self->mOmxLayer->SendCommand(OMX_CommandPortEnable,
                                              self->mPortSettingsChanged,
                                              nullptr);

               // 4. allocate port buffers.
               // Allocate new port buffers.
               nsresult rv = self->AllocateBuffers(def.eDir);
               if (NS_FAILED(rv)) {
                 self->NotifyError(OMX_ErrorUndefined, __func__);
               }

               return p;
             },
             [self] () {
               self->NotifyError(OMX_ErrorUndefined, __func__);
             })
      ->CompletionPromise()
      ->Then(mOmxTaskQueue, __func__,
             [self] () {
               LOG("PortSettingsChanged: port settings changed complete");
               // finish port setting changed.
               self->mPortSettingsChanged = -1;
               self->FillAndEmptyBuffers();
             },
             [self] () {
               self->NotifyError(OMX_ErrorUndefined, __func__);
             });

    // 2. wait for port buffers return to client and then release these buffers.
    //
    // Port buffers will be returned to client soon once OMX_CommandPortDisable
    // command is sent. Then releasing these buffers.
    CollectBufferPromises(def.eDir)
      ->Then(mOmxTaskQueue, __func__,
          [self, def] () {
            MOZ_ASSERT(self->BuffersCanBeReleased(def.eDir));
            nsresult rv = self->ReleaseBuffers(def.eDir);
            if (NS_FAILED(rv)) {
              MOZ_RELEASE_ASSERT(0);
              self->NotifyError(OMX_ErrorUndefined, __func__);
            }
          },
          [self] () {
            self->NotifyError(OMX_ErrorUndefined, __func__);
          });
  }
}

void
OmxDataDecoder::SendEosBuffer()
{
  MOZ_ASSERT(mOmxTaskQueue->IsCurrentThreadIn());

  // There is no 'Drain' API in OpenMax, so it needs to wait for output sample
  // with EOS flag. However, MediaRawData doesn't provide EOS information,
  // so here it generates an empty BufferData with eos OMX_BUFFERFLAG_EOS in queue.
  // This behaviour should be compliant with spec, I think...
  RefPtr<MediaRawData> eos_data = new MediaRawData();
  mMediaRawDatas.AppendElement(eos_data);
  FillAndEmptyBuffers();
}

void
OmxDataDecoder::DoFlush()
{
  MOZ_ASSERT(mOmxTaskQueue->IsCurrentThreadIn());

  // 1. Call OMX command OMX_CommandFlush in Omx TaskQueue.
  // 2. Remove all elements in mMediaRawDatas when flush is completed.
  mOmxLayer->SendCommand(OMX_CommandFlush, OMX_ALL, nullptr)
    ->Then(mOmxTaskQueue, __func__, this,
           &OmxDataDecoder::FlushComplete,
           &OmxDataDecoder::FlushFailure);
}

void
OmxDataDecoder::FlushComplete(OMX_COMMANDTYPE aCommandType)
{
  mMediaRawDatas.Clear();
  mFlushing = false;

  MonitorAutoLock lock(mMonitor);
  mMonitor.Notify();
  LOG("Flush complete");
}

void OmxDataDecoder::FlushFailure(OmxCommandFailureHolder aFailureHolder)
{
  NotifyError(OMX_ErrorUndefined, __func__);
  mFlushing = false;

  MonitorAutoLock lock(mMonitor);
  mMonitor.Notify();
}

}