1 // Copyright 2017 The Chromium Authors. All rights reserved. 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 CONTENT_RENDERER_MEDIA_MEDIA_FACTORY_H_ 6 #define CONTENT_RENDERER_MEDIA_MEDIA_FACTORY_H_ 7 8 #include <memory> 9 10 #include "base/memory/weak_ptr.h" 11 #include "build/buildflag.h" 12 #include "build/chromecast_buildflags.h" 13 #include "components/viz/common/surfaces/surface_id.h" 14 #include "media/base/renderer_factory_selector.h" 15 #include "media/base/routing_token_callback.h" 16 #include "media/blink/url_index.h" 17 #include "media/blink/webmediaplayer_params.h" 18 #include "media/media_buildflags.h" 19 #include "media/mojo/buildflags.h" 20 #include "media/mojo/clients/mojo_renderer_factory.h" 21 #include "media/mojo/mojom/interface_factory.mojom.h" 22 #include "mojo/public/cpp/bindings/remote.h" 23 #include "third_party/blink/public/platform/web_media_player_source.h" 24 #include "third_party/blink/public/platform/web_set_sink_id_callbacks.h" 25 #include "third_party/blink/public/platform/web_string.h" 26 #include "third_party/blink/public/web/web_media_inspector.h" 27 28 #if BUILDFLAG(ENABLE_MEDIA_REMOTING) 29 // Needed by remoting sender. 30 #include "media/mojo/mojom/remoting.mojom.h" 31 #endif // BUILDFLAG(ENABLE_MEDIA_REMOTING) 32 33 namespace blink { 34 class BrowserInterfaceBrokerProxy; 35 class WebContentDecryptionModule; 36 class WebEncryptedMediaClient; 37 class WebLocalFrame; 38 class WebMediaPlayer; 39 class WebMediaPlayerClient; 40 class WebMediaPlayerEncryptedMediaClient; 41 } 42 43 namespace cc { 44 class LayerTreeSettings; 45 } 46 47 namespace media { 48 class CdmFactory; 49 class DecoderFactory; 50 class DefaultDecoderFactory; 51 class MediaLog; 52 class MediaObserver; 53 class RemotePlaybackClientWrapper; 54 class RendererWebMediaPlayerDelegate; 55 class WebEncryptedMediaClientImpl; 56 } 57 58 namespace content { 59 60 class RenderFrameImpl; 61 class MediaInterfaceFactory; 62 struct RenderFrameMediaPlaybackOptions; 63 64 // Assist to RenderFrameImpl in creating various media clients. 65 class MediaFactory { 66 public: 67 // Helper function returning whether VideoSurfaceLayer should be enabled. 68 static blink::WebMediaPlayer::SurfaceLayerMode GetVideoSurfaceLayerMode(); 69 70 // Helper function returning whether VideoSurfaceLayer should be enabled for 71 // MediaStreams. 72 static bool VideoSurfaceLayerEnabledForMS(); 73 74 // Create a MediaFactory to assist the |render_frame| with media tasks. 75 // |request_routing_token_cb| bound to |render_frame| IPC functions for 76 // obtaining overlay tokens. 77 MediaFactory(RenderFrameImpl* render_frame, 78 media::RequestRoutingTokenCallback request_routing_token_cb); 79 ~MediaFactory(); 80 81 // Instruct MediaFactory to establish Mojo channels as needed to perform its 82 // factory duties. This should be called by RenderFrameImpl as soon as its own 83 // interface provider is bound. 84 void SetupMojo(); 85 86 // Creates the VideoFrameSubmitter and its task_runner based on the current 87 // SurfaceLayerMode; 88 std::unique_ptr<blink::WebVideoFrameSubmitter> CreateSubmitter( 89 scoped_refptr<base::SingleThreadTaskRunner>* 90 video_frame_compositor_task_runner, 91 const cc::LayerTreeSettings& settings, 92 media::MediaLog* media_log); 93 94 // Creates a new WebMediaPlayer for the given |source| (either a stream or 95 // URL). All pointers other than |initial_cdm| are required to be non-null. 96 // The created player serves and is directed by the |client| (e.g. 97 // HTMLMediaElement). The |encrypted_client| will be used to establish 98 // means of decryption for encrypted content. |initial_cdm| should point 99 // to a ContentDecryptionModule if MediaKeys have been provided to the 100 // |encrypted_client| (otherwise null). |sink_id|, when not empty, identifies 101 // the audio sink to use for this player (see HTMLMediaElement.sinkId). 102 // |parent_frame_sink_id| identifies the local root widget's FrameSinkId. 103 blink::WebMediaPlayer* CreateMediaPlayer( 104 const blink::WebMediaPlayerSource& source, 105 blink::WebMediaPlayerClient* client, 106 blink::MediaInspectorContext* inspector_context, 107 blink::WebMediaPlayerEncryptedMediaClient* encrypted_client, 108 blink::WebContentDecryptionModule* initial_cdm, 109 const blink::WebString& sink_id, 110 viz::FrameSinkId parent_frame_sink_id, 111 const cc::LayerTreeSettings& settings); 112 113 // Provides an EncryptedMediaClient to connect blink's EME layer to media's 114 // implementation of requestMediaKeySystemAccess. Will always return the same 115 // client whose lifetime is tied to this Factory (same as the RenderFrame). 116 blink::WebEncryptedMediaClient* EncryptedMediaClient(); 117 118 private: 119 std::unique_ptr<media::RendererFactorySelector> CreateRendererFactorySelector( 120 media::MediaLog* media_log, 121 blink::WebURL url, 122 const RenderFrameMediaPlaybackOptions& renderer_media_playback_options, 123 media::DecoderFactory* decoder_factory, 124 std::unique_ptr<media::RemotePlaybackClientWrapper> client_wrapper, 125 base::WeakPtr<media::MediaObserver>* out_media_observer); 126 127 blink::WebMediaPlayer* CreateWebMediaPlayerForMediaStream( 128 blink::WebMediaPlayerClient* client, 129 blink::MediaInspectorContext* inspector_context, 130 const blink::WebString& sink_id, 131 blink::WebLocalFrame* frame, 132 viz::FrameSinkId parent_frame_sink_id, 133 const cc::LayerTreeSettings& settings); 134 135 // Returns the media delegate for WebMediaPlayer usage. If 136 // |media_player_delegate_| is NULL, one is created. 137 media::RendererWebMediaPlayerDelegate* GetWebMediaPlayerDelegate(); 138 139 media::DecoderFactory* GetDecoderFactory(); 140 141 #if BUILDFLAG(ENABLE_MEDIA_REMOTING) 142 media::mojom::RemoterFactory* GetRemoterFactory(); 143 #endif 144 145 media::CdmFactory* GetCdmFactory(); 146 147 media::mojom::InterfaceFactory* GetMediaInterfaceFactory(); 148 149 std::unique_ptr<media::MojoRendererFactory> CreateMojoRendererFactory(); 150 151 // The render frame we're helping. RenderFrameImpl owns this factory, so the 152 // pointer will always be valid. 153 RenderFrameImpl* render_frame_; 154 155 // The media interface provider attached to this frame, lazily initialized. 156 std::unique_ptr<MediaInterfaceFactory> media_interface_factory_; 157 158 // Injected callback for requesting overlay routing tokens. 159 media::RequestRoutingTokenCallback request_routing_token_cb_; 160 161 // Handy pointer to RenderFrame's browser interface broker. Null until 162 // SetupMojo(). Lifetime matches that of the owning |render_frame_|. Will 163 // always be valid once assigned. 164 blink::BrowserInterfaceBrokerProxy* interface_broker_ = nullptr; 165 166 // Manages play, pause notifications for WebMediaPlayer implementations; its 167 // lifetime is tied to the RenderFrame via the RenderFrameObserver interface. 168 media::RendererWebMediaPlayerDelegate* media_player_delegate_ = nullptr; 169 170 // The CDM and decoder factory attached to this frame, lazily initialized. 171 std::unique_ptr<media::DefaultDecoderFactory> decoder_factory_; 172 std::unique_ptr<media::CdmFactory> cdm_factory_; 173 174 // Media resource cache, lazily initialized. 175 std::unique_ptr<media::ResourceFetchContext> fetch_context_; 176 std::unique_ptr<media::UrlIndex> url_index_; 177 178 // EncryptedMediaClient attached to this frame; lazily initialized. 179 std::unique_ptr<media::WebEncryptedMediaClientImpl> 180 web_encrypted_media_client_; 181 182 #if BUILDFLAG(ENABLE_MEDIA_REMOTING) 183 // Lazy-bound remote for the RemoterFactory service in the browser 184 // process. Always use the GetRemoterFactory() accessor instead of this. 185 mojo::Remote<media::mojom::RemoterFactory> remoter_factory_; 186 #endif 187 }; 188 189 } // namespace content 190 191 #endif // CONTENT_RENDERER_MEDIA_MEDIA_FACTORY_H_ 192