#include "content/renderer/media/media_factory.h"
#include <memory>
#include <string>
#include <tuple>
#include <utility>
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/metrics/field_trial_params.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/bind_post_task.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "build/build_config.h"
#include "build/buildflag.h"
#include "build/chromecast_buildflags.h"
#include "cc/trees/layer_tree_settings.h"
#include "components/viz/common/features.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_switches.h"
#include "content/public/renderer/content_renderer_client.h"
#include "content/public/renderer/key_system_support.h"
#include "content/public/renderer/render_frame_media_playback_options.h"
#include "content/renderer/media/batching_media_log.h"
#include "content/renderer/media/inspector_media_event_handler.h"
#include "content/renderer/media/media_interface_factory.h"
#include "content/renderer/media/render_media_event_handler.h"
#include "content/renderer/media/renderer_web_media_player_delegate.h"
#include "content/renderer/render_frame_impl.h"
#include "content/renderer/render_thread_impl.h"
#include "gpu/ipc/client/client_shared_image_interface.h"
#include "media/base/cdm_factory.h"
#include "media/base/decoder_factory.h"
#include "media/base/demuxer.h"
#include "media/base/key_systems_impl.h"
#include "media/base/media_switches.h"
#include "media/base/renderer_factory_selector.h"
#include "media/media_buildflags.h"
#include "media/mojo/buildflags.h"
#include "media/mojo/mojom/key_system_support.mojom.h"
#include "media/renderers/decrypting_renderer_factory.h"
#include "media/renderers/default_decoder_factory.h"
#include "media/renderers/renderer_impl_factory.h"
#include "media/video/gpu_video_accelerator_factories.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "services/service_manager/public/cpp/connect.h"
#include "services/viz/public/cpp/gpu/context_provider_command_buffer.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_registry.h"
#include "third_party/blink/public/common/thread_safe_browser_interface_broker_proxy.h"
#include "third_party/blink/public/platform/browser_interface_broker_proxy.h"
#include "third_party/blink/public/platform/media/key_system_config_selector.h"
#include "third_party/blink/public/platform/media/remote_playback_client_wrapper_impl.h"
#include "third_party/blink/public/platform/media/video_frame_compositor.h"
#include "third_party/blink/public/platform/media/web_encrypted_media_client_impl.h"
#include "third_party/blink/public/platform/media/web_media_player_builder.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/web_media_player_client.h"
#include "third_party/blink/public/platform/web_surface_layer_bridge.h"
#include "third_party/blink/public/platform/web_video_frame_submitter.h"
#include "third_party/blink/public/web/blink.h"
#include "third_party/blink/public/web/modules/media/audio/audio_device_factory.h"
#include "third_party/blink/public/web/modules/mediastream/media_stream_video_source.h"
#include "third_party/blink/public/web/modules/mediastream/web_media_player_ms.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/public/web/web_view.h"
#include "url/origin.h"
#if BUILDFLAG(IS_ANDROID)
#include "content/renderer/media/android/flinging_renderer_client_factory.h"
#include "content/renderer/media/android/media_player_renderer_client_factory.h"
#include "content/renderer/media/android/stream_texture_wrapper_impl.h"
#include "media/base/android/media_codec_util.h"
#include "media/base/media.h"
#include "url/gurl.h"
#endif
#if BUILDFLAG(ENABLE_CAST_RECEIVER)
#include "components/cast_streaming/common/public/cast_streaming_url.h"
#include "components/cast_streaming/common/public/features.h"
#include "components/cast_streaming/renderer/public/resource_provider.h"
#include "components/cast_streaming/renderer/public/wrapping_renderer_factory_selector.h"
#endif
#if BUILDFLAG(ENABLE_CAST_RENDERER)
#include "content/renderer/media/cast_renderer_client_factory.h"
#endif
#if BUILDFLAG(IS_FUCHSIA)
#include "media/cdm/fuchsia/fuchsia_cdm_factory.h"
#include "media/fuchsia/video/fuchsia_decoder_factory.h"
#include "media/mojo/clients/mojo_fuchsia_cdm_provider.h"
#elif BUILDFLAG(ENABLE_MOJO_CDM)
#include "media/mojo/clients/mojo_cdm_factory.h"
#else
#include "media/cdm/default_cdm_factory.h"
#endif
#if BUILDFLAG(IS_FUCHSIA) && BUILDFLAG(ENABLE_MOJO_CDM)
#error "MojoCdm should be disabled for Fuchsia."
#endif
#if BUILDFLAG(ENABLE_MOJO_AUDIO_DECODER) || BUILDFLAG(ENABLE_MOJO_VIDEO_DECODER)
#include "media/mojo/clients/mojo_decoder_factory.h"
#endif
#if BUILDFLAG(ENABLE_MEDIA_REMOTING)
#include "media/remoting/courier_renderer_factory.h"
#include "media/remoting/renderer_controller.h"
#endif
#if BUILDFLAG(ENABLE_CAST_AUDIO_RENDERER)
#include "content/renderer/media/cast_renderer_factory.h"
#endif
#if BUILDFLAG(IS_CASTOS) || BUILDFLAG(IS_CAST_ANDROID)
#include "media/base/remoting_constants.h"
#include "media/remoting/receiver_controller.h"
#include "media/remoting/remoting_renderer_factory.h"
#endif
#if BUILDFLAG(IS_WIN)
#include "content/renderer/media/win/dcomp_texture_wrapper_impl.h"
#include "content/renderer/media/win/overlay_state_observer_impl.h"
#include "content/renderer/media/win/overlay_state_service_provider.h"
#include "gpu/config/gpu_driver_bug_workarounds.h"
#include "media/base/win/mf_feature_checks.h"
#include "media/cdm/win/media_foundation_cdm.h"
#include "media/mojo/clients/win/media_foundation_renderer_client_factory.h"
#include "media/mojo/mojom/speech_recognition_service.mojom.h"
#endif
namespace {
constexpr size_t kDefaultMaxWebMediaPlayers = …;
size_t GetMaxWebMediaPlayers() { … }
void PostContextProviderToCallback(
scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
scoped_refptr<viz::RasterContextProvider> unwanted_context_provider,
blink::WebSubmitterConfigurationCallback set_context_provider_callback) { … }
void LogRoughness(
media::MediaLog* media_log,
const cc::VideoPlaybackRoughnessReporter::Measurement& measurement) { … }
std::unique_ptr<media::RendererImplFactory> CreateRendererImplFactory(
media::MediaPlayerLoggingID player_id,
media::MediaLog* media_log,
media::DecoderFactory* decoder_factory,
content::RenderThreadImpl* render_thread,
content::RenderFrameImpl* render_frame) { … }
enum class MediaPlayerType { … };
std::unique_ptr<blink::WebVideoFrameSubmitter> CreateSubmitter(
scoped_refptr<base::SingleThreadTaskRunner>
main_thread_compositor_task_runner,
const cc::LayerTreeSettings& settings,
media::MediaLog* media_log,
content::RenderFrame* render_frame) { … }
}
namespace content {
MediaFactory::MediaFactory(
RenderFrameImpl* render_frame,
media::RequestRoutingTokenCallback request_routing_token_cb)
: … { … }
MediaFactory::~MediaFactory() { … }
void MediaFactory::SetupMojo() { … }
std::unique_ptr<blink::WebMediaPlayer> MediaFactory::CreateMediaPlayer(
const blink::WebMediaPlayerSource& source,
blink::WebMediaPlayerClient* client,
blink::MediaInspectorContext* inspector_context,
blink::WebMediaPlayerEncryptedMediaClient* encrypted_client,
blink::WebContentDecryptionModule* initial_cdm,
const blink::WebString& sink_id,
viz::FrameSinkId parent_frame_sink_id,
const cc::LayerTreeSettings& settings,
scoped_refptr<base::SingleThreadTaskRunner>
main_thread_compositor_task_runner,
scoped_refptr<base::TaskRunner> compositor_worker_task_runner) { … }
blink::WebEncryptedMediaClient* MediaFactory::EncryptedMediaClient() { … }
std::unique_ptr<media::RendererFactorySelector>
MediaFactory::CreateRendererFactorySelector(
media::MediaPlayerLoggingID player_id,
media::MediaLog* media_log,
blink::WebURL url,
const RenderFrameMediaPlaybackOptions& renderer_media_playback_options,
media::DecoderFactory* decoder_factory,
std::unique_ptr<media::RemotePlaybackClientWrapper> client_wrapper,
base::WeakPtr<media::MediaObserver>* out_media_observer,
int element_id) { … }
std::unique_ptr<blink::WebMediaPlayer>
MediaFactory::CreateWebMediaPlayerForMediaStream(
blink::WebMediaPlayerClient* client,
blink::MediaInspectorContext* inspector_context,
const blink::WebString& sink_id,
blink::WebLocalFrame* frame,
viz::FrameSinkId parent_frame_sink_id,
const cc::LayerTreeSettings& settings,
scoped_refptr<base::SingleThreadTaskRunner>
main_thread_compositor_task_runner,
scoped_refptr<base::TaskRunner> compositor_worker_task_runner) { … }
media::RendererWebMediaPlayerDelegate*
MediaFactory::GetWebMediaPlayerDelegate() { … }
base::WeakPtr<media::DecoderFactory> MediaFactory::GetDecoderFactory() { … }
void MediaFactory::EnsureDecoderFactory() { … }
#if BUILDFLAG(ENABLE_MEDIA_REMOTING)
media::mojom::RemoterFactory* MediaFactory::GetRemoterFactory() { … }
#endif
std::unique_ptr<media::KeySystemSupportRegistration>
MediaFactory::GetSupportedKeySystems(media::GetSupportedKeySystemsCB cb) { … }
media::KeySystems* MediaFactory::GetKeySystems() { … }
media::CdmFactory* MediaFactory::GetCdmFactory() { … }
media::mojom::InterfaceFactory* MediaFactory::GetMediaInterfaceFactory() { … }
std::unique_ptr<media::MojoRendererFactory>
MediaFactory::CreateMojoRendererFactory() { … }
const blink::BrowserInterfaceBrokerProxy& MediaFactory::GetInterfaceBroker()
const { … }
}