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
|
/* -*- 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 "RemoteMediaManagerParent.h"
#if XP_WIN
# include <objbase.h>
#endif
#include "ImageContainer.h"
#include "PDMFactory.h"
#include "RemoteAudioDecoder.h"
#include "RemoteCDMParent.h"
#include "RemoteMediaDataEncoderParent.h"
#include "RemoteVideoDecoder.h"
#include "VideoUtils.h" // for MediaThreadType
#include "mozilla/RDDParent.h"
#include "mozilla/RemoteDecodeUtils.h"
#include "mozilla/SyncRunnable.h"
#include "mozilla/gfx/GPUParent.h"
#include "mozilla/ipc/Endpoint.h"
#include "mozilla/ipc/UtilityProcessChild.h"
#include "mozilla/layers/ImageDataSerializer.h"
#include "mozilla/layers/VideoBridgeChild.h"
#include "mozilla/layers/VideoBridgeParent.h"
#include "nsIObserverService.h"
#ifdef MOZ_WMF_MEDIA_ENGINE
# include "MFMediaEngineParent.h"
#endif
#ifdef MOZ_WMF_CDM
# include "MFCDMParent.h"
#endif
#ifdef MOZ_WIDGET_ANDROID
# include "mozilla/MediaDrmRemoteCDMParent.h"
#endif
namespace mozilla {
#define LOG(msg, ...) \
MOZ_LOG(gRemoteDecodeLog, LogLevel::Debug, (msg, ##__VA_ARGS__))
using namespace ipc;
using namespace layers;
using namespace gfx;
StaticRefPtr<TaskQueue> sRemoteMediaManagerParentThread;
void RemoteMediaManagerParent::StoreImage(const SurfaceDescriptorGPUVideo& aSD,
Image* aImage,
TextureClient* aTexture) {
MOZ_ASSERT(OnManagerThread());
mImageMap[static_cast<SurfaceDescriptorRemoteDecoder>(aSD).handle()] = aImage;
mTextureMap[static_cast<SurfaceDescriptorRemoteDecoder>(aSD).handle()] =
aTexture;
}
class RemoteDecoderManagerThreadShutdownObserver : public nsIObserver {
virtual ~RemoteDecoderManagerThreadShutdownObserver() = default;
public:
RemoteDecoderManagerThreadShutdownObserver() = default;
NS_DECL_ISUPPORTS
NS_IMETHOD Observe(nsISupports* aSubject, const char* aTopic,
const char16_t* aData) override {
MOZ_ASSERT(strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0);
RemoteMediaManagerParent::ShutdownVideoBridge();
RemoteMediaManagerParent::ShutdownThreads();
return NS_OK;
}
};
NS_IMPL_ISUPPORTS(RemoteDecoderManagerThreadShutdownObserver, nsIObserver);
bool RemoteMediaManagerParent::StartupThreads() {
MOZ_ASSERT(NS_IsMainThread());
if (sRemoteMediaManagerParentThread) {
return true;
}
nsCOMPtr<nsIObserverService> observerService = services::GetObserverService();
if (!observerService) {
return false;
}
sRemoteMediaManagerParentThread = TaskQueue::Create(
GetMediaThreadPool(MediaThreadType::SUPERVISOR), "RemVidParent");
if (XRE_IsGPUProcess()) {
MOZ_ALWAYS_SUCCEEDS(
sRemoteMediaManagerParentThread->Dispatch(NS_NewRunnableFunction(
"RemoteMediaManagerParent::StartupThreads",
[]() { layers::VideoBridgeChild::StartupForGPUProcess(); })));
}
auto* obs = new RemoteDecoderManagerThreadShutdownObserver();
observerService->AddObserver(obs, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
return true;
}
void RemoteMediaManagerParent::ShutdownThreads() {
sRemoteMediaManagerParentThread->BeginShutdown();
sRemoteMediaManagerParentThread->AwaitShutdownAndIdle();
sRemoteMediaManagerParentThread = nullptr;
}
/* static */
void RemoteMediaManagerParent::ShutdownVideoBridge() {
if (sRemoteMediaManagerParentThread) {
RefPtr<Runnable> task =
NS_NewRunnableFunction("RemoteMediaManagerParent::ShutdownVideoBridge",
[]() { VideoBridgeChild::Shutdown(); });
SyncRunnable::DispatchToThread(sRemoteMediaManagerParentThread, task);
}
}
bool RemoteMediaManagerParent::OnManagerThread() {
return sRemoteMediaManagerParentThread->IsOnCurrentThread();
}
/* static */
void RemoteMediaManagerParent::Dispatch(
already_AddRefed<nsIRunnable> aRunnable) {
if (!sRemoteMediaManagerParentThread) {
MOZ_DIAGNOSTIC_CRASH(
"Dispatching after RemoteMediaManagerParent thread shutdown!");
return;
}
MOZ_ALWAYS_SUCCEEDS(
sRemoteMediaManagerParentThread->Dispatch(std::move(aRunnable)));
}
PDMFactory& RemoteMediaManagerParent::EnsurePDMFactory() {
MOZ_ASSERT(OnManagerThread());
if (!mPDMFactory) {
mPDMFactory = MakeRefPtr<PDMFactory>();
}
return *mPDMFactory;
}
bool RemoteMediaManagerParent::CreateForContent(
Endpoint<PRemoteMediaManagerParent>&& aEndpoint,
dom::ContentParentId aChildId) {
MOZ_ASSERT(XRE_GetProcessType() == GeckoProcessType_RDD ||
XRE_GetProcessType() == GeckoProcessType_Utility ||
XRE_GetProcessType() == GeckoProcessType_GPU);
MOZ_ASSERT(NS_IsMainThread());
if (!StartupThreads()) {
return false;
}
RefPtr<RemoteMediaManagerParent> parent =
new RemoteMediaManagerParent(sRemoteMediaManagerParentThread, aChildId);
RefPtr<Runnable> task =
NewRunnableMethod<Endpoint<PRemoteMediaManagerParent>&&>(
"dom::RemoteMediaManagerParent::Open", parent,
&RemoteMediaManagerParent::Open, std::move(aEndpoint));
MOZ_ALWAYS_SUCCEEDS(sRemoteMediaManagerParentThread->Dispatch(task.forget()));
return true;
}
bool RemoteMediaManagerParent::CreateVideoBridgeToOtherProcess(
Endpoint<PVideoBridgeChild>&& aEndpoint) {
LOG("Create video bridge");
// We never want to decode in the GPU process, but output
// frames to the parent process.
MOZ_ASSERT(XRE_GetProcessType() == GeckoProcessType_RDD ||
XRE_GetProcessType() == GeckoProcessType_Utility);
#ifdef MOZ_WMF_MEDIA_ENGINE
MOZ_ASSERT_IF(
XRE_GetProcessType() == GeckoProcessType_Utility,
GetCurrentSandboxingKind() == SandboxingKind::MF_MEDIA_ENGINE_CDM);
#endif
MOZ_ASSERT(NS_IsMainThread());
if (!StartupThreads()) {
return false;
}
RefPtr<Runnable> task =
NewRunnableFunction("gfx::VideoBridgeChild::Open",
&VideoBridgeChild::Open, std::move(aEndpoint));
MOZ_ALWAYS_SUCCEEDS(sRemoteMediaManagerParentThread->Dispatch(task.forget()));
return true;
}
RemoteMediaManagerParent::RemoteMediaManagerParent(
nsISerialEventTarget* aThread, dom::ContentParentId aContentId)
: mThread(aThread), mContentId(aContentId) {
MOZ_COUNT_CTOR(RemoteMediaManagerParent);
auto& registrar =
XRE_IsGPUProcess() ? GPUParent::GetSingleton()->AsyncShutdownService()
: XRE_IsUtilityProcess()
? UtilityProcessChild::GetSingleton()->AsyncShutdownService()
: RDDParent::GetSingleton()->AsyncShutdownService();
registrar.Register(this);
}
RemoteMediaManagerParent::~RemoteMediaManagerParent() {
MOZ_COUNT_DTOR(RemoteMediaManagerParent);
auto& registrar =
XRE_IsGPUProcess() ? GPUParent::GetSingleton()->AsyncShutdownService()
: XRE_IsUtilityProcess()
? UtilityProcessChild::GetSingleton()->AsyncShutdownService()
: RDDParent::GetSingleton()->AsyncShutdownService();
registrar.Deregister(this);
}
void RemoteMediaManagerParent::ActorDestroy(
mozilla::ipc::IProtocol::ActorDestroyReason) {
mThread = nullptr;
}
PRemoteDecoderParent* RemoteMediaManagerParent::AllocPRemoteDecoderParent(
const RemoteDecoderInfoIPDL& aRemoteDecoderInfo,
const CreateDecoderParams::OptionSet& aOptions,
const Maybe<layers::TextureFactoryIdentifier>& aIdentifier,
const Maybe<uint64_t>& aMediaEngineId, const Maybe<TrackingId>& aTrackingId,
PRemoteCDMParent* aCDM) {
RefPtr<TaskQueue> decodeTaskQueue =
TaskQueue::Create(GetMediaThreadPool(MediaThreadType::PLATFORM_DECODER),
"RemoteVideoDecoderParent::mDecodeTaskQueue");
auto* cdm = static_cast<RemoteCDMParent*>(aCDM);
if (aRemoteDecoderInfo.type() ==
RemoteDecoderInfoIPDL::TVideoDecoderInfoIPDL) {
const VideoDecoderInfoIPDL& decoderInfo =
aRemoteDecoderInfo.get_VideoDecoderInfoIPDL();
return new RemoteVideoDecoderParent(
this, decoderInfo.videoInfo(), decoderInfo.framerate(), aOptions,
aIdentifier, sRemoteMediaManagerParentThread, decodeTaskQueue,
aMediaEngineId, aTrackingId, cdm);
}
if (aRemoteDecoderInfo.type() == RemoteDecoderInfoIPDL::TAudioInfo) {
return new RemoteAudioDecoderParent(
this, aRemoteDecoderInfo.get_AudioInfo(), aOptions,
sRemoteMediaManagerParentThread, decodeTaskQueue, aMediaEngineId, cdm);
}
MOZ_CRASH("unrecognized type of RemoteDecoderInfoIPDL union");
return nullptr;
}
bool RemoteMediaManagerParent::DeallocPRemoteDecoderParent(
PRemoteDecoderParent* actor) {
RemoteDecoderParent* parent = static_cast<RemoteDecoderParent*>(actor);
parent->Destroy();
return true;
}
already_AddRefed<PRemoteEncoderParent>
RemoteMediaManagerParent::AllocPRemoteEncoderParent(
const EncoderConfig& aConfig) {
return MakeAndAddRef<RemoteMediaDataEncoderParent>(aConfig);
}
PMFMediaEngineParent* RemoteMediaManagerParent::AllocPMFMediaEngineParent() {
#ifdef MOZ_WMF_MEDIA_ENGINE
return new MFMediaEngineParent(this, sRemoteMediaManagerParentThread);
#else
return nullptr;
#endif
}
bool RemoteMediaManagerParent::DeallocPMFMediaEngineParent(
PMFMediaEngineParent* actor) {
#ifdef MOZ_WMF_MEDIA_ENGINE
MFMediaEngineParent* parent = static_cast<MFMediaEngineParent*>(actor);
parent->Destroy();
#endif
return true;
}
PMFCDMParent* RemoteMediaManagerParent::AllocPMFCDMParent(
const nsAString& aKeySystem) {
#ifdef MOZ_WMF_CDM
return new MFCDMParent(aKeySystem, this, sRemoteMediaManagerParentThread);
#else
return nullptr;
#endif
}
bool RemoteMediaManagerParent::DeallocPMFCDMParent(PMFCDMParent* actor) {
#ifdef MOZ_WMF_CDM
static_cast<MFCDMParent*>(actor)->Destroy();
#endif
return true;
}
PRemoteCDMParent* RemoteMediaManagerParent::AllocPRemoteCDMParent(
const nsAString& aKeySystem) {
#ifdef MOZ_WIDGET_ANDROID
return new MediaDrmRemoteCDMParent(aKeySystem);
#else
return nullptr;
#endif
}
void RemoteMediaManagerParent::Open(
Endpoint<PRemoteMediaManagerParent>&& aEndpoint) {
if (!aEndpoint.Bind(this)) {
// We can't recover from this.
MOZ_CRASH("Failed to bind RemoteMediaManagerParent to endpoint");
}
}
mozilla::ipc::IPCResult RemoteMediaManagerParent::RecvReadback(
const SurfaceDescriptorGPUVideo& aSD, SurfaceDescriptor* aResult) {
const SurfaceDescriptorRemoteDecoder& sd = aSD;
RefPtr<Image> image = mImageMap[sd.handle()];
if (!image) {
*aResult = null_t();
return IPC_OK();
}
// Read directly into the shmem to avoid extra copies, if possible.
SurfaceDescriptorBuffer sdb;
nsresult rv = image->BuildSurfaceDescriptorBuffer(
sdb, Image::BuildSdbFlags::RgbOnly, [&](uint32_t aBufferSize) {
Shmem buffer;
if (!AllocShmem(aBufferSize, &buffer)) {
return MemoryOrShmem();
}
return MemoryOrShmem(std::move(buffer));
});
if (NS_SUCCEEDED(rv)) {
*aResult = std::move(sdb);
return IPC_OK();
}
if (sdb.data().type() == MemoryOrShmem::TShmem) {
DeallocShmem(sdb.data().get_Shmem());
}
*aResult = null_t();
return IPC_OK();
}
mozilla::ipc::IPCResult
RemoteMediaManagerParent::RecvDeallocateSurfaceDescriptorGPUVideo(
const SurfaceDescriptorGPUVideo& aSD) {
MOZ_ASSERT(OnManagerThread());
const SurfaceDescriptorRemoteDecoder& sd = aSD;
mImageMap.erase(sd.handle());
mTextureMap.erase(sd.handle());
return IPC_OK();
}
mozilla::ipc::IPCResult RemoteMediaManagerParent::RecvOnSetCurrent(
const SurfaceDescriptorGPUVideo& aSD) {
MOZ_ASSERT(OnManagerThread());
const SurfaceDescriptorRemoteDecoder& sd = aSD;
RefPtr<Image> image = mImageMap[sd.handle()];
if (!image) {
return IPC_OK();
}
image->OnSetCurrent();
return IPC_OK();
}
already_AddRefed<Image> RemoteMediaManagerParent::TransferToImage(
const SurfaceDescriptorGPUVideo& aSD, const IntSize& aSize,
const ColorDepth& aColorDepth, YUVColorSpace aYUVColorSpace,
ColorSpace2 aColorPrimaries, TransferFunction aTransferFunction,
ColorRange aColorRange) {
MOZ_ASSERT(OnManagerThread());
const SurfaceDescriptorRemoteDecoder& sd = aSD;
const auto i = mImageMap.find(sd.handle());
if (NS_WARN_IF(i == mImageMap.end())) {
return nullptr;
}
return do_AddRef(i->second);
}
void RemoteMediaManagerParent::DeallocateSurfaceDescriptor(
const SurfaceDescriptorGPUVideo& aSD) {
if (!OnManagerThread()) {
MOZ_ALWAYS_SUCCEEDS(
sRemoteMediaManagerParentThread->Dispatch(NS_NewRunnableFunction(
"RemoteMediaManagerParent::DeallocateSurfaceDescriptor",
[ref = RefPtr{this}, sd = aSD]() {
ref->RecvDeallocateSurfaceDescriptorGPUVideo(sd);
})));
} else {
RecvDeallocateSurfaceDescriptorGPUVideo(aSD);
}
}
#undef LOG
} // namespace mozilla
|