1 // Copyright 2014 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 #include "media/renderers/renderer_impl_factory.h"
10 #include "base/functional/bind.h"
11 #include "base/task/sequenced_task_runner.h"
12 #include "build/build_config.h"
13 #include "media/base/audio_buffer.h"
14 #include "media/base/decoder_factory.h"
15 #include "media/renderers/audio_renderer_impl.h"
16 #include "media/renderers/renderer_impl.h"
17 #include "media/renderers/video_renderer_impl.h"
18 #include "media/video/gpu_memory_buffer_video_frame_pool.h"
19 #include "media/video/gpu_video_accelerator_factories.h"
23 #if BUILDFLAG(IS_ANDROID)
24 RendererImplFactory::RendererImplFactory(
26 DecoderFactory* decoder_factory,
27 const GetGpuFactoriesCB& get_gpu_factories_cb,
28 MediaPlayerLoggingID media_player_id)
29 : media_log_(media_log),
30 decoder_factory_(decoder_factory),
31 get_gpu_factories_cb_(get_gpu_factories_cb),
32 media_player_id_(media_player_id) {
33 DCHECK(decoder_factory_);
36 RendererImplFactory::RendererImplFactory(
38 DecoderFactory* decoder_factory,
39 const GetGpuFactoriesCB& get_gpu_factories_cb,
40 MediaPlayerLoggingID media_player_id,
41 std::unique_ptr<SpeechRecognitionClient> speech_recognition_client)
42 : media_log_(media_log),
43 decoder_factory_(decoder_factory),
44 get_gpu_factories_cb_(get_gpu_factories_cb),
45 media_player_id_(media_player_id),
46 speech_recognition_client_(std::move(speech_recognition_client)) {
47 DCHECK(decoder_factory_);
51 RendererImplFactory::~RendererImplFactory() = default;
53 std::vector<std::unique_ptr<AudioDecoder>>
54 RendererImplFactory::CreateAudioDecoders(
55 const scoped_refptr<base::SequencedTaskRunner>& media_task_runner) {
56 // Create our audio decoders and renderer.
57 std::vector<std::unique_ptr<AudioDecoder>> audio_decoders;
59 decoder_factory_->CreateAudioDecoders(media_task_runner, media_log_,
61 return audio_decoders;
64 std::vector<std::unique_ptr<VideoDecoder>>
65 RendererImplFactory::CreateVideoDecoders(
66 const scoped_refptr<base::SequencedTaskRunner>& media_task_runner,
67 RequestOverlayInfoCB request_overlay_info_cb,
68 const gfx::ColorSpace& target_color_space,
69 GpuVideoAcceleratorFactories* gpu_factories) {
70 // Create our video decoders and renderer.
71 std::vector<std::unique_ptr<VideoDecoder>> video_decoders;
73 decoder_factory_->CreateVideoDecoders(
74 media_task_runner, gpu_factories, media_log_,
75 std::move(request_overlay_info_cb), target_color_space, &video_decoders);
77 return video_decoders;
80 std::unique_ptr<Renderer> RendererImplFactory::CreateRenderer(
81 const scoped_refptr<base::SequencedTaskRunner>& media_task_runner,
82 const scoped_refptr<base::TaskRunner>& worker_task_runner,
83 AudioRendererSink* audio_renderer_sink,
84 VideoRendererSink* video_renderer_sink,
85 RequestOverlayInfoCB request_overlay_info_cb,
86 const gfx::ColorSpace& target_color_space) {
87 DCHECK(audio_renderer_sink);
89 std::unique_ptr<AudioRenderer> audio_renderer(new AudioRendererImpl(
90 media_task_runner, audio_renderer_sink,
91 // Unretained is safe here, because the RendererFactory is guaranteed to
92 // outlive the RendererImpl. The RendererImpl is destroyed when WMPI
93 // destructor calls pipeline_controller_.Stop() -> PipelineImpl::Stop() ->
94 // RendererWrapper::Stop -> RendererWrapper::DestroyRenderer(). And the
95 // RendererFactory is owned by WMPI and gets called after WMPI destructor
97 base::BindRepeating(&RendererImplFactory::CreateAudioDecoders,
98 base::Unretained(this), media_task_runner),
99 media_log_, media_player_id_
100 #if BUILDFLAG(IS_ANDROID)
104 speech_recognition_client_.get()));
107 GpuVideoAcceleratorFactories* gpu_factories = nullptr;
108 if (get_gpu_factories_cb_)
109 gpu_factories = get_gpu_factories_cb_.Run();
111 std::unique_ptr<GpuMemoryBufferVideoFramePool> gmb_pool;
112 if (gpu_factories && gpu_factories->ShouldUseGpuMemoryBuffersForVideoFrames(
113 false /* for_media_stream */)) {
114 gmb_pool = std::make_unique<GpuMemoryBufferVideoFramePool>(
115 media_task_runner, std::move(worker_task_runner), gpu_factories);
118 std::unique_ptr<VideoRenderer> video_renderer(new VideoRendererImpl(
119 media_task_runner, video_renderer_sink,
120 // Unretained is safe here, because the RendererFactory is guaranteed to
121 // outlive the RendererImpl. The RendererImpl is destroyed when WMPI
122 // destructor calls pipeline_controller_.Stop() -> PipelineImpl::Stop() ->
123 // RendererWrapper::Stop -> RendererWrapper::DestroyRenderer(). And the
124 // RendererFactory is owned by WMPI and gets called after WMPI destructor
126 base::BindRepeating(&RendererImplFactory::CreateVideoDecoders,
127 base::Unretained(this), media_task_runner,
128 std::move(request_overlay_info_cb),
129 target_color_space, gpu_factories),
130 true, media_log_, std::move(gmb_pool), media_player_id_));
132 return std::make_unique<RendererImpl>(
133 media_task_runner, std::move(audio_renderer), std::move(video_renderer));