[M120 Migration][MM][CAPI] Fix the logic for media using capi player.
[platform/framework/web/chromium-efl.git] / media / mojo / services / interface_factory_impl.h
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.
4
5 #ifndef MEDIA_MOJO_SERVICES_INTERFACE_FACTORY_IMPL_H_
6 #define MEDIA_MOJO_SERVICES_INTERFACE_FACTORY_IMPL_H_
7
8 #include <memory>
9
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"
33
34 namespace media {
35
36 class CdmFactory;
37 class MojoMediaClient;
38 class Renderer;
39
40 class InterfaceFactoryImpl final
41     : public DeferredDestroy<mojom::InterfaceFactory> {
42  public:
43   InterfaceFactoryImpl(
44       mojo::PendingRemote<mojom::FrameInterfaceFactory> frame_interfaces,
45       MojoMediaClient* mojo_media_client);
46
47   InterfaceFactoryImpl(const InterfaceFactoryImpl&) = delete;
48   InterfaceFactoryImpl& operator=(const InterfaceFactoryImpl&) = delete;
49
50   ~InterfaceFactoryImpl() final;
51
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;
59
60   void CreateAudioEncoder(
61       mojo::PendingReceiver<mojom::AudioEncoder> receiver) final;
62
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;
70 #endif
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>
83           client_extension,
84       mojo::PendingReceiver<mojom::Renderer> receiver) final;
85 #endif  // BUILDFLAG(IS_ANDROID)
86 #if BUILDFLAG(IS_WIN)
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)
95
96   void CreateCdm(const CdmConfig& cdm_config, CreateCdmCallback callback) final;
97
98   // DeferredDestroy<mojom::InterfaceFactory> implemenation.
99   void OnDestroyPending(base::OnceClosure destroy_cb) final;
100
101  private:
102   // Returns true when there is no media component (audio/video decoder,
103   // renderer, cdm and cdm proxy) receivers exist.
104   bool IsEmpty();
105
106   void SetReceiverDisconnectHandler();
107   void OnReceiverDisconnect();
108
109 #if BUILDFLAG(ENABLE_MOJO_RENDERER) || BUILDFLAG(ENABLE_CAST_RENDERER) || \
110     BUILDFLAG(IS_WIN)
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);
115 #endif
116
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)
124
125 #if BUILDFLAG(ALLOW_OOP_VIDEO_DECODER)
126   void FinishCreatingVideoDecoder(
127       mojo::PendingReceiver<mojom::VideoDecoder> receiver,
128       mojo::PendingRemote<media::stable::mojom::StableVideoDecoder>
129           dst_video_decoder);
130 #endif  // BUILDFLAG(ALLOW_OOP_VIDEO_DECODER)
131
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
134   // available.
135   MojoCdmServiceContext cdm_service_context_;
136
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)
141
142 #if BUILDFLAG(ENABLE_MOJO_VIDEO_DECODER)
143   mojo::UniqueReceiverSet<mojom::VideoDecoder> video_decoder_receivers_;
144 #endif  // BUILDFLAG(ENABLE_MOJO_VIDEO_DECODER)
145
146 #if BUILDFLAG(ENABLE_MOJO_AUDIO_ENCODER)
147   mojo::UniqueReceiverSet<mojom::AudioEncoder> audio_encoder_receivers_;
148 #endif  // BUILDFLAG(ENABLE_MOJO_VIDEO_ENCODER)
149
150 #if BUILDFLAG(ENABLE_MOJO_RENDERER) || BUILDFLAG(ENABLE_CAST_RENDERER) || \
151     BUILDFLAG(IS_WIN)
152   // TODO(xhwang): Use MojoMediaLog for Renderer.
153   NullMediaLog media_log_;
154   mojo::UniqueReceiverSet<mojom::Renderer> renderer_receivers_;
155 #endif
156
157 #if BUILDFLAG(ENABLE_MOJO_CDM)
158   std::unique_ptr<CdmFactory> cdm_factory_;
159   mojo::UniqueReceiverSet<mojom::ContentDecryptionModule> cdm_receivers_;
160
161   // MojoCdmServices pending initialization.
162   std::map<MojoCdmService*, std::unique_ptr<MojoCdmService>>
163       pending_mojo_cdm_services_;
164 #endif  // BUILDFLAG(ENABLE_MOJO_CDM)
165
166   mojo::Remote<mojom::FrameInterfaceFactory> frame_interfaces_;
167
168   mojo::UniqueReceiverSet<mojom::Decryptor> decryptor_receivers_;
169
170   raw_ptr<MojoMediaClient> mojo_media_client_;
171   base::OnceClosure destroy_cb_;
172
173   // NOTE: Weak pointers must be invalidated before all other member variables.
174   base::WeakPtrFactory<InterfaceFactoryImpl> weak_ptr_factory_{this};
175 };
176
177 }  // namespace media
178
179 #endif  // MEDIA_MOJO_SERVICES_INTERFACE_FACTORY_IMPL_H_