RemoteMediaManagerChild.h (7467B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 #ifndef include_dom_media_ipc_RemoteMediaManagerChild_h 7 #define include_dom_media_ipc_RemoteMediaManagerChild_h 8 9 #include <functional> 10 11 #include "GPUVideoImage.h" 12 #include "PDMFactory.h" 13 #include "PlatformEncoderModule.h" 14 #include "ipc/EnumSerializer.h" 15 #include "mozilla/PRemoteMediaManagerChild.h" 16 #include "mozilla/ipc/UtilityProcessSandboxing.h" 17 #include "mozilla/layers/VideoBridgeUtils.h" 18 19 namespace mozilla { 20 21 namespace dom { 22 class MediaKeys; 23 } 24 25 class PMFCDMChild; 26 class PMFMediaEngineChild; 27 class RemoteCDMChild; 28 class RemoteDecoderChild; 29 class RemoteMediaDataEncoderChild; 30 31 enum class RemoteMediaIn { 32 Unspecified, 33 RddProcess, 34 GpuProcess, 35 UtilityProcess_Generic, 36 UtilityProcess_AppleMedia, 37 UtilityProcess_WMF, 38 UtilityProcess_MFMediaEngineCDM, 39 SENTINEL, 40 }; 41 42 enum class TrackSupport { 43 None, 44 DecodeAudio, 45 DecodeVideo, 46 EncodeAudio, 47 EncodeVideo, 48 }; 49 using TrackSupportSet = EnumSet<TrackSupport, uint8_t>; 50 51 class RemoteMediaManagerChild final 52 : public PRemoteMediaManagerChild, 53 public mozilla::ipc::IShmemAllocator, 54 public mozilla::layers::IGPUVideoSurfaceManager { 55 friend class PRemoteMediaManagerChild; 56 57 public: 58 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RemoteMediaManagerChild, override) 59 60 // Can only be called from the manager thread 61 static RemoteMediaManagerChild* GetSingleton(RemoteMediaIn aLocation); 62 63 static void Init(); 64 static void SetSupported(RemoteMediaIn aLocation, 65 const media::MediaCodecsSupported& aSupported); 66 67 // Can be called from any thread. 68 static bool Supports(RemoteMediaIn aLocation, 69 const SupportDecoderParams& aParams, 70 DecoderDoctorDiagnostics* aDiagnostics); 71 static RefPtr<PlatformDecoderModule::CreateDecoderPromise> CreateAudioDecoder( 72 const CreateDecoderParams& aParams, RemoteMediaIn aLocation); 73 static RefPtr<PlatformDecoderModule::CreateDecoderPromise> CreateVideoDecoder( 74 const CreateDecoderParams& aParams, RemoteMediaIn aLocation); 75 static RefPtr<RemoteCDMChild> CreateCDM(RemoteMediaIn aLocation, 76 dom::MediaKeys* aKeys, 77 const nsAString& aKeySystem, 78 bool aDistinctiveIdentifierRequired, 79 bool aPersistentStateRequired); 80 81 static media::EncodeSupportSet Supports(RemoteMediaIn aLocation, 82 CodecType aCodec); 83 static RefPtr<PlatformEncoderModule::CreateEncoderPromise> InitializeEncoder( 84 RefPtr<RemoteMediaDataEncoderChild>&& aEncoder, 85 const EncoderConfig& aConfig); 86 87 // Can be called from any thread. 88 static nsCOMPtr<nsISerialEventTarget> GetManagerThread(); 89 90 // Return the track support information based on the location of the remote 91 // process. Thread-safe. 92 static TrackSupportSet GetTrackSupport(RemoteMediaIn aLocation); 93 94 // Can be called from any thread, dispatches the request to the IPDL thread 95 // internally and will be ignored if the IPDL actor has been destroyed. 96 already_AddRefed<gfx::SourceSurface> Readback( 97 const SurfaceDescriptorGPUVideo& aSD) override; 98 already_AddRefed<layers::Image> TransferToImage( 99 const SurfaceDescriptorGPUVideo& aSD, const gfx::IntSize& aSize, 100 const gfx::ColorDepth& aColorDepth, gfx::YUVColorSpace aYUVColorSpace, 101 gfx::ColorSpace2 aColorPrimaries, gfx::TransferFunction aTransferFunction, 102 gfx::ColorRange aColorRange) override; 103 void DeallocateSurfaceDescriptor( 104 const SurfaceDescriptorGPUVideo& aSD) override; 105 106 void OnSetCurrent(const SurfaceDescriptorGPUVideo& aSD) override; 107 108 bool AllocShmem(size_t aSize, mozilla::ipc::Shmem* aShmem) override { 109 return PRemoteMediaManagerChild::AllocShmem(aSize, aShmem); 110 } 111 bool AllocUnsafeShmem(size_t aSize, mozilla::ipc::Shmem* aShmem) override { 112 return PRemoteMediaManagerChild::AllocUnsafeShmem(aSize, aShmem); 113 } 114 115 // Can be called from any thread, dispatches the request to the IPDL thread 116 // internally and will be ignored if the IPDL actor has been destroyed. 117 bool DeallocShmem(mozilla::ipc::Shmem& aShmem) override; 118 119 // Main thread only 120 static void InitForGPUProcess( 121 Endpoint<PRemoteMediaManagerChild>&& aVideoManager); 122 static void Shutdown(); 123 124 // Helper method to handle IPDL promise rejections. This will allow the 125 // caller in the layers above to recover gracefully by recreating the encoder 126 // or decoder. 127 static void HandleRejectionError( 128 const RemoteMediaManagerChild* aDyingManager, RemoteMediaIn aLocation, 129 const mozilla::ipc::ResponseRejectReason& aReason, 130 std::function<void(const MediaResult&)>&& aCallback); 131 132 // Run aTask (on the manager thread) when we next attempt to create a new 133 // manager (even if creation fails). Intended to be called from ActorDestroy 134 // when we get notified that the old manager is being destroyed. Can only be 135 // called from the manager thread. 136 static void RunWhenGPUProcessRecreated( 137 const RemoteMediaManagerChild* aDyingManager, 138 already_AddRefed<Runnable> aTask); 139 140 RemoteMediaIn Location() const { return mLocation; } 141 142 // A thread-safe method to launch the utility process if it hasn't launched 143 // yet. 144 static RefPtr<GenericNonExclusivePromise> LaunchUtilityProcessIfNeeded( 145 RemoteMediaIn aLocation); 146 147 protected: 148 void HandleFatalError(const char* aMsg) override; 149 150 PRemoteDecoderChild* AllocPRemoteDecoderChild( 151 const RemoteDecoderInfoIPDL& aRemoteDecoderInfo, 152 const CreateDecoderParams::OptionSet& aOptions, 153 const Maybe<layers::TextureFactoryIdentifier>& aIdentifier, 154 const Maybe<uint64_t>& aMediaEngineId, 155 const Maybe<TrackingId>& aTrackingId, PRemoteCDMChild* aCDM); 156 bool DeallocPRemoteDecoderChild(PRemoteDecoderChild* actor); 157 158 PMFMediaEngineChild* AllocPMFMediaEngineChild(); 159 bool DeallocPMFMediaEngineChild(PMFMediaEngineChild* actor); 160 161 PMFCDMChild* AllocPMFCDMChild(const nsAString& aKeySystem); 162 bool DeallocPMFCDMChild(PMFCDMChild* actor); 163 164 private: 165 explicit RemoteMediaManagerChild(RemoteMediaIn aLocation); 166 ~RemoteMediaManagerChild() = default; 167 static RefPtr<PlatformDecoderModule::CreateDecoderPromise> Construct( 168 RefPtr<RemoteDecoderChild>&& aChild, 169 CreateDecoderParamsForAsync&& aParams, RemoteMediaIn aLocation); 170 171 static void OpenRemoteMediaManagerChildForProcess( 172 Endpoint<PRemoteMediaManagerChild>&& aEndpoint, RemoteMediaIn aLocation); 173 174 // A thread-safe method to launch the RDD process if it hasn't launched yet. 175 static RefPtr<GenericNonExclusivePromise> LaunchRDDProcessIfNeeded(); 176 177 // The location for decoding, Rdd or Gpu process. 178 const RemoteMediaIn mLocation; 179 }; 180 181 } // namespace mozilla 182 183 namespace IPC { 184 template <> 185 struct ParamTraits<mozilla::RemoteMediaIn> 186 : public ContiguousEnumSerializer<mozilla::RemoteMediaIn, 187 mozilla::RemoteMediaIn::Unspecified, 188 mozilla::RemoteMediaIn::SENTINEL> {}; 189 } // namespace IPC 190 191 #endif // include_dom_media_ipc_RemoteMediaManagerChild_h