1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef MEDIA_MOJO_SERVICES_INTERFACE_FACTORY_IMPL_H_
6 #define MEDIA_MOJO_SERVICES_INTERFACE_FACTORY_IMPL_H_
10 #include "base/memory/raw_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/unguessable_token.h"
13 #include "build/build_config.h"
14 #include "media/base/media_util.h"
15 #include "media/media_buildflags.h"
16 #include "media/mojo/buildflags.h"
17 #include "media/mojo/mojom/audio_decoder.mojom.h"
18 #include "media/mojo/mojom/audio_encoder.mojom.h"
19 #include "media/mojo/mojom/content_decryption_module.mojom.h"
20 #include "media/mojo/mojom/decryptor.mojom.h"
21 #include "media/mojo/mojom/frame_interface_factory.mojom.h"
22 #include "media/mojo/mojom/interface_factory.mojom.h"
23 #include "media/mojo/mojom/media_log.mojom.h"
24 #include "media/mojo/mojom/renderer.mojom.h"
25 #include "media/mojo/mojom/video_decoder.mojom.h"
26 #include "media/mojo/services/deferred_destroy_unique_receiver_set.h"
27 #include "media/mojo/services/mojo_cdm_service_context.h"
28 #include "mojo/public/cpp/bindings/pending_receiver.h"
29 #include "mojo/public/cpp/bindings/pending_remote.h"
30 #include "mojo/public/cpp/bindings/receiver.h"
31 #include "mojo/public/cpp/bindings/remote.h"
32 #include "mojo/public/cpp/bindings/unique_receiver_set.h"
37 class MojoMediaClient;
40 class InterfaceFactoryImpl final
41 : public DeferredDestroy<mojom::InterfaceFactory> {
44 mojo::PendingRemote<mojom::FrameInterfaceFactory> frame_interfaces,
45 MojoMediaClient* mojo_media_client);
47 InterfaceFactoryImpl(const InterfaceFactoryImpl&) = delete;
48 InterfaceFactoryImpl& operator=(const InterfaceFactoryImpl&) = delete;
50 ~InterfaceFactoryImpl() final;
52 // mojom::InterfaceFactory implementation.
53 void CreateAudioDecoder(
54 mojo::PendingReceiver<mojom::AudioDecoder> receiver) final;
55 void CreateVideoDecoder(
56 mojo::PendingReceiver<mojom::VideoDecoder> receiver,
57 mojo::PendingRemote<media::stable::mojom::StableVideoDecoder>
58 dst_video_decoder) final;
60 void CreateAudioEncoder(
61 mojo::PendingReceiver<mojom::AudioEncoder> receiver) final;
63 void CreateDefaultRenderer(
64 const std::string& audio_device_id,
65 mojo::PendingReceiver<mojom::Renderer> receiver) final;
66 #if BUILDFLAG(ENABLE_CAST_RENDERER)
67 void CreateCastRenderer(
68 const base::UnguessableToken& overlay_plane_id,
69 mojo::PendingReceiver<mojom::Renderer> receiver) final;
71 #if BUILDFLAG(IS_ANDROID) || defined(TIZEN_MULTIMEDIA)
72 void CreateMediaPlayerRenderer(
73 mojo::PendingRemote<mojom::MediaPlayerRendererClientExtension>
74 client_extension_remote,
75 mojo::PendingReceiver<mojom::Renderer> receiver,
76 mojo::PendingReceiver<mojom::MediaPlayerRendererExtension>
77 renderer_extension_receiver) final;
78 #endif // BUILDFLAG(IS_ANDROID) || defined(TIZEN_MULTIMEDIA)
79 #if defined(IS_ANDROID)
80 void CreateFlingingRenderer(
81 const std::string& presentation_id,
82 mojo::PendingRemote<mojom::FlingingRendererClientExtension>
84 mojo::PendingReceiver<mojom::Renderer> receiver) final;
85 #endif // BUILDFLAG(IS_ANDROID)
87 void CreateMediaFoundationRenderer(
88 mojo::PendingRemote<mojom::MediaLog> media_log_remote,
89 mojo::PendingReceiver<mojom::Renderer> receiver,
90 mojo::PendingReceiver<mojom::MediaFoundationRendererExtension>
91 renderer_extension_receiver,
92 mojo::PendingRemote<media::mojom::MediaFoundationRendererClientExtension>
93 client_extension_remote) final;
94 #endif // BUILDFLAG(IS_WIN)
96 void CreateCdm(const CdmConfig& cdm_config, CreateCdmCallback callback) final;
98 // DeferredDestroy<mojom::InterfaceFactory> implemenation.
99 void OnDestroyPending(base::OnceClosure destroy_cb) final;
102 // Returns true when there is no media component (audio/video decoder,
103 // renderer, cdm and cdm proxy) receivers exist.
106 void SetReceiverDisconnectHandler();
107 void OnReceiverDisconnect();
109 #if BUILDFLAG(ENABLE_MOJO_RENDERER) || BUILDFLAG(ENABLE_CAST_RENDERER) || \
111 // Creates MojoRendererService for `renderer`, bind it to `receiver` and add
112 // them to `renderer_receivers_`.
113 void AddRenderer(std::unique_ptr<media::Renderer> renderer,
114 mojo::PendingReceiver<mojom::Renderer> receiver);
117 #if BUILDFLAG(ENABLE_MOJO_CDM)
118 CdmFactory* GetCdmFactory();
119 void OnCdmServiceInitialized(MojoCdmService* raw_mojo_cdm_service,
120 CreateCdmCallback callback,
121 mojom::CdmContextPtr cdm_context,
122 const std::string& error_message);
123 #endif // BUILDFLAG(ENABLE_MOJO_CDM)
125 #if BUILDFLAG(ALLOW_OOP_VIDEO_DECODER)
126 void FinishCreatingVideoDecoder(
127 mojo::PendingReceiver<mojom::VideoDecoder> receiver,
128 mojo::PendingRemote<media::stable::mojom::StableVideoDecoder>
130 #endif // BUILDFLAG(ALLOW_OOP_VIDEO_DECODER)
132 // Must be declared before the receivers below because the bound objects might
133 // take a raw pointer of |cdm_service_context_| and assume it's always
135 MojoCdmServiceContext cdm_service_context_;
137 #if BUILDFLAG(ENABLE_MOJO_AUDIO_DECODER)
138 class AudioDecoderReceivers;
139 std::unique_ptr<AudioDecoderReceivers> audio_decoder_receivers_;
140 #endif // BUILDFLAG(ENABLE_MOJO_AUDIO_DECODER)
142 #if BUILDFLAG(ENABLE_MOJO_VIDEO_DECODER)
143 mojo::UniqueReceiverSet<mojom::VideoDecoder> video_decoder_receivers_;
144 #endif // BUILDFLAG(ENABLE_MOJO_VIDEO_DECODER)
146 #if BUILDFLAG(ENABLE_MOJO_AUDIO_ENCODER)
147 mojo::UniqueReceiverSet<mojom::AudioEncoder> audio_encoder_receivers_;
148 #endif // BUILDFLAG(ENABLE_MOJO_VIDEO_ENCODER)
150 #if BUILDFLAG(ENABLE_MOJO_RENDERER) || BUILDFLAG(ENABLE_CAST_RENDERER) || \
152 // TODO(xhwang): Use MojoMediaLog for Renderer.
153 NullMediaLog media_log_;
154 mojo::UniqueReceiverSet<mojom::Renderer> renderer_receivers_;
157 #if BUILDFLAG(ENABLE_MOJO_CDM)
158 std::unique_ptr<CdmFactory> cdm_factory_;
159 mojo::UniqueReceiverSet<mojom::ContentDecryptionModule> cdm_receivers_;
161 // MojoCdmServices pending initialization.
162 std::map<MojoCdmService*, std::unique_ptr<MojoCdmService>>
163 pending_mojo_cdm_services_;
164 #endif // BUILDFLAG(ENABLE_MOJO_CDM)
166 mojo::Remote<mojom::FrameInterfaceFactory> frame_interfaces_;
168 mojo::UniqueReceiverSet<mojom::Decryptor> decryptor_receivers_;
170 raw_ptr<MojoMediaClient> mojo_media_client_;
171 base::OnceClosure destroy_cb_;
173 // NOTE: Weak pointers must be invalidated before all other member variables.
174 base::WeakPtrFactory<InterfaceFactoryImpl> weak_ptr_factory_{this};
179 #endif // MEDIA_MOJO_SERVICES_INTERFACE_FACTORY_IMPL_H_