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 (b9b3e10bc8fc)

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 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
/* -*- 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 "RemoteDecoderModule.h"

#include "base/thread.h"
#include "mozilla/dom/ContentChild.h"  // for launching RDD w/ ContentChild
#include "mozilla/layers/SynchronousTask.h"
#include "mozilla/StaticPrefs_media.h"
#include "mozilla/SyncRunnable.h"

#ifdef MOZ_AV1
#  include "AOMDecoder.h"
#endif
#include "RemoteAudioDecoder.h"
#include "RemoteDecoderManagerChild.h"
#include "RemoteMediaDataDecoder.h"
#include "RemoteVideoDecoder.h"
#include "OpusDecoder.h"
#include "VideoUtils.h"
#include "VorbisDecoder.h"
#include "WAVDecoder.h"

namespace mozilla {

using base::Thread;
using dom::ContentChild;
using namespace ipc;
using namespace layers;

StaticMutex RemoteDecoderModule::sLaunchMonitor;

RemoteDecoderModule::RemoteDecoderModule()
    : mManagerThread(RemoteDecoderManagerChild::GetManagerThread()) {}

bool RemoteDecoderModule::SupportsMimeType(
    const nsACString& aMimeType, DecoderDoctorDiagnostics* aDiagnostics) const {
  bool supports = false;

#ifdef MOZ_AV1
  if (StaticPrefs::media_av1_enabled()) {
    supports |= AOMDecoder::IsAV1(aMimeType);
  }
#endif
  if (StaticPrefs::media_rdd_vorbis_enabled()) {
    supports |= VorbisDataDecoder::IsVorbis(aMimeType);
  }
  if (StaticPrefs::media_rdd_wav_enabled()) {
    supports |= WaveDataDecoder::IsWave(aMimeType);
  }
  if (StaticPrefs::media_rdd_opus_enabled()) {
    supports |= OpusDataDecoder::IsOpus(aMimeType);
  }

  MOZ_LOG(
      sPDMLog, LogLevel::Debug,
      ("Sandbox decoder %s requested type", supports ? "supports" : "rejects"));
  return supports;
}

void RemoteDecoderModule::LaunchRDDProcessIfNeeded() {
  if (!XRE_IsContentProcess()) {
    return;
  }

  StaticMutexAutoLock mon(sLaunchMonitor);

  // We have a couple possible states here.  We are in a content process
  // and:
  // 1) the RDD process has never been launched.  RDD should be launched
  //    and the IPC connections setup.
  // 2) the RDD process has been launched, but this particular content
  //    process has not setup (or has lost) its IPC connection.
  // In the code below, we assume we need to launch the RDD process and
  // setup the IPC connections.  However, if the manager thread for
  // RemoteDecoderManagerChild is available we do a quick check to see
  // if we can send (meaning the IPC channel is open).  If we can send,
  // then no work is necessary.  If we can't send, then we call
  // LaunchRDDProcess which will launch RDD if necessary, and setup the
  // IPC connections between *this* content process and the RDD process.
  bool needsLaunch = true;
  if (mManagerThread) {
    RefPtr<Runnable> task = NS_NewRunnableFunction(
        "RemoteDecoderModule::LaunchRDDProcessIfNeeded-CheckSend", [&]() {
          if (RemoteDecoderManagerChild::GetRDDProcessSingleton()) {
            needsLaunch =
                !RemoteDecoderManagerChild::GetRDDProcessSingleton()->CanSend();
          }
        });
    SyncRunnable::DispatchToThread(mManagerThread, task);
  }

  if (needsLaunch) {
    ContentChild::GetSingleton()->LaunchRDDProcess();
    mManagerThread = RemoteDecoderManagerChild::GetManagerThread();
  }
}

already_AddRefed<MediaDataDecoder> RemoteDecoderModule::CreateAudioDecoder(
    const CreateDecoderParams& aParams) {
  LaunchRDDProcessIfNeeded();

  if (!mManagerThread) {
    return nullptr;
  }

  // OpusDataDecoder will check this option to provide the same info
  // that IsDefaultPlaybackDeviceMono provides.  We want to avoid calls
  // to IsDefaultPlaybackDeviceMono on RDD because initializing audio
  // backends on RDD will be blocked by the sandbox.
  CreateDecoderParams::OptionSet options(aParams.mOptions);
  if (OpusDataDecoder::IsOpus(aParams.mConfig.mMimeType) &&
      IsDefaultPlaybackDeviceMono()) {
    options += CreateDecoderParams::Option::DefaultPlaybackDeviceMono;
  }

  RefPtr<RemoteAudioDecoderChild> child = new RemoteAudioDecoderChild();
  MediaResult result(NS_OK);
  // We can use child as a ref here because this is a sync dispatch. In
  // the error case for InitIPDL, we can't just let the RefPtr go out of
  // scope at the end of the method because it will release the
  // RemoteAudioDecoderChild on the wrong thread.  This will assert in
  // RemoteDecoderChild's destructor.  Passing the RefPtr by reference
  // allows us to release the RemoteAudioDecoderChild on the manager
  // thread during this single dispatch.
  RefPtr<Runnable> task =
      NS_NewRunnableFunction("RemoteDecoderModule::CreateAudioDecoder", [&]() {
        result = child->InitIPDL(aParams.AudioConfig(), options);
        if (NS_FAILED(result)) {
          // Release RemoteAudioDecoderChild here, while we're on
          // manager thread.  Don't just let the RefPtr go out of scope.
          child = nullptr;
        }
      });
  SyncRunnable::DispatchToThread(mManagerThread, task);

  if (NS_FAILED(result)) {
    if (aParams.mError) {
      *aParams.mError = result;
    }
    return nullptr;
  }

  RefPtr<RemoteMediaDataDecoder> object = new RemoteMediaDataDecoder(
      child, mManagerThread,
      RemoteDecoderManagerChild::GetManagerAbstractThread());

  return object.forget();
}

already_AddRefed<MediaDataDecoder> RemoteDecoderModule::CreateVideoDecoder(
    const CreateDecoderParams& aParams) {
  LaunchRDDProcessIfNeeded();

  if (!mManagerThread) {
    return nullptr;
  }

  RefPtr<RemoteVideoDecoderChild> child = new RemoteVideoDecoderChild();
  MediaResult result(NS_OK);
  // We can use child as a ref here because this is a sync dispatch. In
  // the error case for InitIPDL, we can't just let the RefPtr go out of
  // scope at the end of the method because it will release the
  // RemoteVideoDecoderChild on the wrong thread.  This will assert in
  // RemoteDecoderChild's destructor.  Passing the RefPtr by reference
  // allows us to release the RemoteVideoDecoderChild on the manager
  // thread during this single dispatch.
  RefPtr<Runnable> task =
      NS_NewRunnableFunction("RemoteDecoderModule::CreateVideoDecoder", [&]() {
        result = child->InitIPDL(
            aParams.VideoConfig(), aParams.mRate.mValue, aParams.mOptions,
            aParams.mKnowsCompositor
                ? &aParams.mKnowsCompositor->GetTextureFactoryIdentifier()
                : nullptr);
        if (NS_FAILED(result)) {
          // Release RemoteVideoDecoderChild here, while we're on
          // manager thread.  Don't just let the RefPtr go out of scope.
          child = nullptr;
        }
      });
  SyncRunnable::DispatchToThread(mManagerThread, task);

  if (NS_FAILED(result)) {
    if (aParams.mError) {
      *aParams.mError = result;
    }
    return nullptr;
  }

  RefPtr<RemoteMediaDataDecoder> object = new RemoteMediaDataDecoder(
      child, mManagerThread,
      RemoteDecoderManagerChild::GetManagerAbstractThread());

  return object.forget();
}

}  // namespace mozilla