File: UtilityMediaServiceParent.cpp

package info (click to toggle)
firefox 147.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,324 kB
  • sloc: cpp: 7,607,156; javascript: 6,532,492; ansic: 3,775,158; python: 1,415,368; xml: 634,556; asm: 438,949; java: 186,241; sh: 62,751; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (222 lines) | stat: -rw-r--r-- 7,085 bytes parent folder | download
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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 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 "UtilityMediaServiceParent.h"

#include "GeckoProfiler.h"
#include "nsDebugImpl.h"

#include "MediaCodecsSupport.h"
#include "mozilla/RemoteMediaManagerParent.h"

#include "mozilla/gfx/gfxVars.h"

#if defined(XP_WIN) && defined(MOZ_SANDBOX)
#  include "WMF.h"
#  include "WMFDecoderModule.h"
#  include "WMFUtils.h"

#  include "mozilla/sandboxTarget.h"
#  include "mozilla/ipc/UtilityProcessImpl.h"
#endif  // defined(XP_WIN) && defined(MOZ_SANDBOX)

#ifdef MOZ_WIDGET_ANDROID
#  include "mozilla/StaticPrefs_media.h"
#  include "AndroidDecoderModule.h"
#endif

#include "mozilla/ipc/UtilityProcessChild.h"
#include "mozilla/RemoteDecodeUtils.h"

#ifdef MOZ_WMF_MEDIA_ENGINE
#  include "gfxConfig.h"
#  include "mozilla/gfx/DeviceManagerDx.h"
#endif

#ifdef MOZ_WMF_CDM
#  include "mozilla/MFCDMParent.h"
#  include "mozilla/PMFCDM.h"
#endif

namespace mozilla::ipc {

UtilityMediaServiceParent::UtilityMediaServiceParent(
    nsTArray<gfx::GfxVarUpdate>&& aUpdates)
    : mKind(GetCurrentSandboxingKind()),
      mUtilityMediaServiceParentStart(TimeStamp::Now()) {
  switch (mKind) {
#ifdef MOZ_WMF_MEDIA_ENGINE
    case SandboxingKind::MF_MEDIA_ENGINE_CDM:
      nsDebugImpl::SetMultiprocessMode("MF Media Engine CDM");
      profiler_set_process_name(nsCString("MF Media Engine CDM"));
      break;
#endif
    case SandboxingKind::GENERIC_UTILITY:
      break;
    default:
      nsDebugImpl::SetMultiprocessMode("Utility AudioDecoder");
      profiler_set_process_name(nsCString("Utility AudioDecoder"));
      break;
  }
  gfx::gfxVars::Initialize();
  gfx::gfxVars::ApplyUpdate(aUpdates);
#ifdef MOZ_WMF_MEDIA_ENGINE
  if (mKind == SandboxingKind::MF_MEDIA_ENGINE_CDM) {
    gfx::gfxConfig::Init();
    gfx::DeviceManagerDx::Init();
  }
#endif
}

UtilityMediaServiceParent::~UtilityMediaServiceParent() {
  gfx::gfxVars::Shutdown();
#ifdef MOZ_WMF_MEDIA_ENGINE
  if (mKind == SandboxingKind::MF_MEDIA_ENGINE_CDM) {
    gfx::gfxConfig::Shutdown();
    gfx::DeviceManagerDx::Shutdown();
  }
#endif
#ifdef MOZ_WMF_CDM
  if (mKind == SandboxingKind::MF_MEDIA_ENGINE_CDM) {
    MFCDMParent::Shutdown();
  }
#endif
}

/* static */
void UtilityMediaServiceParent::GenericPreloadForSandbox() {
#if defined(MOZ_SANDBOX) && defined(XP_WIN)
  // Preload AV dlls so we can enable Binary Signature Policy
  // to restrict further dll loads.
  UtilityProcessImpl::LoadLibraryOrCrash(L"mozavcodec.dll");
  UtilityProcessImpl::LoadLibraryOrCrash(L"mozavutil.dll");
#endif  // defined(MOZ_SANDBOX) && defined(XP_WIN)
}

/* static */
void UtilityMediaServiceParent::WMFPreloadForSandbox() {
#if defined(MOZ_SANDBOX) && defined(XP_WIN)
  // mfplat.dll and mf.dll will be preloaded by
  // wmf::MediaFoundationInitializer::HasInitialized()

#  if defined(NS_FREE_PERMANENT_DATA)
  // WMF Shutdown requires this or it will badly crash
  UtilityProcessImpl::LoadLibraryOrCrash(L"ole32.dll");
#  endif  // defined(NS_FREE_PERMANENT_DATA)

  auto rv = wmf::MediaFoundationInitializer::HasInitialized();
  if (!rv) {
    NS_WARNING("Failed to init Media Foundation in the Utility process");
    return;
  }
#endif  // defined(MOZ_SANDBOX) && defined(XP_WIN)
}

void UtilityMediaServiceParent::Start(
    Endpoint<PUtilityMediaServiceParent>&& aEndpoint) {
  MOZ_ASSERT(NS_IsMainThread());

  DebugOnly<bool> ok = std::move(aEndpoint).Bind(this);
  MOZ_ASSERT(ok);

#ifdef MOZ_WIDGET_ANDROID
  if (StaticPrefs::media_utility_android_media_codec_enabled()) {
    AndroidDecoderModule::SetSupportedMimeTypes();
  }
#endif

  auto supported = media::MCSInfo::GetSupportFromFactory();
  (void)SendUpdateMediaCodecsSupported(GetRemoteMediaInFromKind(mKind),
                                       supported);
  PROFILER_MARKER_UNTYPED("UtilityMediaServiceParent::Start", IPC,
                          MarkerOptions(MarkerTiming::IntervalUntilNowFrom(
                              mUtilityMediaServiceParentStart)));
}

mozilla::ipc::IPCResult
UtilityMediaServiceParent::RecvNewContentRemoteMediaManager(
    Endpoint<PRemoteMediaManagerParent>&& aEndpoint,
    const ContentParentId& aParentId) {
  MOZ_ASSERT(NS_IsMainThread());
  if (!RemoteMediaManagerParent::CreateForContent(std::move(aEndpoint),
                                                  aParentId)) {
    return IPC_FAIL_NO_REASON(this);
  }
  return IPC_OK();
}

#ifdef MOZ_WMF_MEDIA_ENGINE
mozilla::ipc::IPCResult UtilityMediaServiceParent::RecvInitVideoBridge(
    Endpoint<PVideoBridgeChild>&& aEndpoint,
    const ContentDeviceData& aContentDeviceData) {
  MOZ_ASSERT(mKind == SandboxingKind::MF_MEDIA_ENGINE_CDM);
  if (!RemoteMediaManagerParent::CreateVideoBridgeToOtherProcess(
          std::move(aEndpoint))) {
    return IPC_FAIL_NO_REASON(this);
  }

  gfx::gfxConfig::Inherit(
      {
          gfx::Feature::HW_COMPOSITING,
          gfx::Feature::D3D11_COMPOSITING,
          gfx::Feature::OPENGL_COMPOSITING,
      },
      aContentDeviceData.prefs());

  if (gfx::gfxConfig::IsEnabled(gfx::Feature::D3D11_COMPOSITING)) {
    if (auto* devmgr = gfx::DeviceManagerDx::Get()) {
      devmgr->ImportDeviceInfo(aContentDeviceData.d3d11());
    }
  }

  (void)SendCompleteCreatedVideoBridge();
  return IPC_OK();
}
#endif

IPCResult UtilityMediaServiceParent::RecvUpdateVar(
    const nsTArray<GfxVarUpdate>& aUpdate) {
  gfx::gfxVars::ApplyUpdate(aUpdate);

  MOZ_ALWAYS_SUCCEEDS(NS_DispatchBackgroundTask(
      NS_NewRunnableFunction(
          "UtilityMediaServiceParent::RecvUpdateVar",
          [self = RefPtr{this}]() {
            NS_DispatchToMainThread(NS_NewRunnableFunction(
                "UtilityMediaServiceParent::UpdateMediaCodecsSupported",
                [self, supported = media::MCSInfo::GetSupportFromFactory(
                           true /* force refresh */)]() {
                  (void)self->SendUpdateMediaCodecsSupported(
                      GetRemoteMediaInFromKind(self->mKind), supported);
                }));
          }),
      nsIEventTarget::DISPATCH_NORMAL));
  return IPC_OK();
}

#ifdef MOZ_WMF_CDM
IPCResult UtilityMediaServiceParent::RecvGetKeySystemCapabilities(
    GetKeySystemCapabilitiesResolver&& aResolver) {
  MOZ_ASSERT(mKind == SandboxingKind::MF_MEDIA_ENGINE_CDM);
  MFCDMParent::GetAllKeySystemsCapabilities()->Then(
      GetCurrentSerialEventTarget(), __func__,
      [aResolver](CopyableTArray<MFCDMCapabilitiesIPDL>&& aCapabilities) {
        aResolver(std::move(aCapabilities));
      },
      [aResolver](nsresult) {
        aResolver(CopyableTArray<MFCDMCapabilitiesIPDL>());
      });
  return IPC_OK();
}

IPCResult UtilityMediaServiceParent::RecvUpdateWidevineL1Path(
    const nsString& aPath) {
  MFCDMParent::SetWidevineL1Path(NS_ConvertUTF16toUTF8(aPath).get());
  return IPC_OK();
}
#endif

}  // namespace mozilla::ipc