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.

Header

Mercurial (b6d82b1a6b02)

VCS Links

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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "RemoteAudioDecoder.h"

#include "RemoteDecoderManagerChild.h"
#include "OpusDecoder.h"
#include "VorbisDecoder.h"
#include "WAVDecoder.h"

#include "mozilla/PodOperations.h"

namespace mozilla {

RemoteAudioDecoderChild::RemoteAudioDecoderChild() : RemoteDecoderChild() {}

mozilla::ipc::IPCResult RemoteAudioDecoderChild::RecvOutput(
    const DecodedOutputIPDL& aDecodedData) {
  AssertOnManagerThread();
  MOZ_ASSERT(aDecodedData.type() == DecodedOutputIPDL::TRemoteAudioDataIPDL);
  const RemoteAudioDataIPDL& aData = aDecodedData.get_RemoteAudioDataIPDL();

  AlignedAudioBuffer alignedAudioBuffer;
  // Use std::min to make sure we can't overrun our buffer in case someone is
  // fibbing about buffer sizes.
  alignedAudioBuffer.SetLength(
      std::min((unsigned long)aData.audioDataBufferSize(),
               (unsigned long)aData.buffer().Size<AudioDataValue>()));
  PodCopy(alignedAudioBuffer.Data(), aData.buffer().get<AudioDataValue>(),
          alignedAudioBuffer.Length());

  Unused << SendDoneWithOutput(std::move(aData.buffer()));

  RefPtr<AudioData> audio = new AudioData(
      aData.base().offset(), aData.base().time(), std::move(alignedAudioBuffer),
      aData.channels(), aData.rate(), aData.channelMap());

  mDecodedData.AppendElement(std::move(audio));
  return IPC_OK();
}

MediaResult RemoteAudioDecoderChild::InitIPDL(
    const AudioInfo& aAudioInfo,
    const CreateDecoderParams::OptionSet& aOptions) {
  RefPtr<RemoteDecoderManagerChild> manager =
      RemoteDecoderManagerChild::GetRDDProcessSingleton();

  // The manager isn't available because RemoteDecoderManagerChild has been
  // initialized with null end points and we don't want to decode video on RDD
  // process anymore. Return false here so that we can fallback to other PDMs.
  if (!manager) {
    return MediaResult(NS_ERROR_DOM_MEDIA_FATAL_ERR,
                       RESULT_DETAIL("RemoteDecoderManager is not available."));
  }

  if (!manager->CanSend()) {
    return MediaResult(NS_ERROR_DOM_MEDIA_FATAL_ERR,
                       RESULT_DETAIL("RemoteDecoderManager unable to send."));
  }

  mIPDLSelfRef = this;
  bool success = false;
  nsCString errorDescription;
  if (manager->SendPRemoteDecoderConstructor(
          this, aAudioInfo, aOptions, Nothing(), &success, &errorDescription)) {
    mCanSend = true;
  }

  return success ? MediaResult(NS_OK)
                 : MediaResult(NS_ERROR_DOM_MEDIA_FATAL_ERR, errorDescription);
}

RemoteAudioDecoderParent::RemoteAudioDecoderParent(
    RemoteDecoderManagerParent* aParent, const AudioInfo& aAudioInfo,
    const CreateDecoderParams::OptionSet& aOptions,
    TaskQueue* aManagerTaskQueue, TaskQueue* aDecodeTaskQueue, bool* aSuccess,
    nsCString* aErrorDescription)
    : RemoteDecoderParent(aParent, aManagerTaskQueue, aDecodeTaskQueue),
      mAudioInfo(aAudioInfo) {
  CreateDecoderParams params(mAudioInfo);
  params.mTaskQueue = mDecodeTaskQueue;
  params.mOptions = aOptions;
  MediaResult error(NS_OK);
  params.mError = &error;

  if (VorbisDataDecoder::IsVorbis(params.mConfig.mMimeType)) {
    mDecoder = new VorbisDataDecoder(params);
  } else if (OpusDataDecoder::IsOpus(params.mConfig.mMimeType)) {
    mDecoder = new OpusDataDecoder(params);
  } else if (WaveDataDecoder::IsWave(params.mConfig.mMimeType)) {
    mDecoder = new WaveDataDecoder(params);
  }

  if (NS_FAILED(error)) {
    MOZ_ASSERT(aErrorDescription);
    *aErrorDescription = error.Description();
  }

  *aSuccess = !!mDecoder;
}

MediaResult RemoteAudioDecoderParent::ProcessDecodedData(
    const MediaDataDecoder::DecodedData& aData) {
  MOZ_ASSERT(OnManagerThread());

  for (const auto& data : aData) {
    MOZ_ASSERT(data->mType == MediaData::Type::AUDIO_DATA,
               "Can only decode audio using RemoteAudioDecoderParent!");
    AudioData* audio = static_cast<AudioData*>(data.get());

    MOZ_ASSERT(audio->Data().Elements(),
               "Decoded audio must output an AlignedAudioBuffer "
               "to be used with RemoteAudioDecoderParent");

    ShmemBuffer buffer = mDecodedFramePool.Get(
        this, audio->Data().Length() * sizeof(AudioDataValue));
    if (!buffer.Valid()) {
      return MediaResult(NS_ERROR_OUT_OF_MEMORY,
                         "ShmemBuffer::Get failed in "
                         "RemoteAudioDecoderParent::ProcessDecodedData");
    }
    if (audio->Data().Length() > buffer.Get().Size<AudioDataValue>()) {
      return MediaResult(NS_ERROR_OUT_OF_MEMORY,
                         "ShmemBuffer::Get returned less than requested in "
                         "RemoteAudioDecoderParent::ProcessDecodedData");
    }

    PodCopy(buffer.Get().get<AudioDataValue>(), audio->Data().Elements(),
            audio->Data().Length());

    RemoteAudioDataIPDL output(
        MediaDataIPDL(data->mOffset, data->mTime, data->mTimecode,
                      data->mDuration, data->mKeyframe),
        audio->mChannels, audio->mRate, audio->mChannelMap,
        audio->Data().Length(), std::move(buffer.Get()));

    Unused << SendOutput(output);
  }

  return NS_OK;
}

}  // namespace mozilla