1 // Copyright 2021 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/mojo/services/gpu_mojo_media_client.h"
7 #include "base/task/sequenced_task_runner.h"
8 #include "gpu/command_buffer/service/ref_counted_lock.h"
9 #include "gpu/config/gpu_finch_features.h"
10 #include "media/base/android/android_cdm_factory.h"
11 #include "media/base/media_log.h"
12 #include "media/base/media_switches.h"
13 #include "media/filters/android/media_codec_audio_decoder.h"
14 #include "media/gpu/android/android_video_surface_chooser_impl.h"
15 #include "media/gpu/android/codec_allocator.h"
16 #include "media/gpu/android/direct_shared_image_video_provider.h"
17 #include "media/gpu/android/maybe_render_early_manager.h"
18 #include "media/gpu/android/media_codec_video_decoder.h"
19 #include "media/gpu/android/pooled_shared_image_video_provider.h"
20 #include "media/gpu/android/video_frame_factory_impl.h"
21 #include "media/media_buildflags.h"
22 #include "media/mojo/services/android_mojo_util.h"
24 #if BUILDFLAG(USE_PROPRIETARY_CODECS)
25 #include "media/gpu/android/ndk_audio_encoder.h"
28 using media::android_mojo_util::CreateMediaDrmStorage;
29 using media::android_mojo_util::CreateProvisionFetcher;
33 std::unique_ptr<VideoDecoder> CreatePlatformVideoDecoder(
34 VideoDecoderTraits& traits) {
35 scoped_refptr<gpu::RefCountedLock> ref_counted_lock;
37 // When this feature is enabled, CodecImage, CodecBufferWaitCorrdinator and
38 // other media classes used in MCVD path will be accessed by multiple gpu
39 // threads. To implement thread safetyness, we are using a global ref
40 // counted lock here. CodecImage, CodecOutputBufferRenderer,
41 // CodecBufferWaitCoordinator expects this ref counted lock to be held by the
42 // classes which are accessing them (AndroidVideoImageBacking, MRE,
43 // FrameInfoHelper etc.)
44 if (features::NeedThreadSafeAndroidMedia()) {
45 ref_counted_lock = base::MakeRefCounted<gpu::RefCountedLock>();
48 std::unique_ptr<SharedImageVideoProvider> image_provider =
49 std::make_unique<DirectSharedImageVideoProvider>(
50 traits.gpu_task_runner, traits.get_command_buffer_stub_cb,
53 if (base::FeatureList::IsEnabled(kUsePooledSharedImageVideoProvider)) {
54 // Wrap |image_provider| in a pool.
55 image_provider = PooledSharedImageVideoProvider::Create(
56 traits.gpu_task_runner, traits.get_command_buffer_stub_cb,
57 std::move(image_provider), ref_counted_lock);
59 // TODO(liberato): Create this only if we're using Vulkan, else it's
60 // ignored. If we can tell that here, then VideoFrameFactory can use it
61 // as a signal about whether it's supposed to get YCbCrInfo rather than
62 // requiring the provider to set |is_vulkan| in the ImageRecord.
63 auto frame_info_helper = FrameInfoHelper::Create(
64 traits.gpu_task_runner, traits.get_command_buffer_stub_cb,
67 return MediaCodecVideoDecoder::Create(
68 traits.gpu_preferences, traits.gpu_feature_info,
69 traits.media_log->Clone(), DeviceInfo::GetInstance(),
70 CodecAllocator::GetInstance(traits.gpu_task_runner),
71 std::make_unique<AndroidVideoSurfaceChooserImpl>(
72 DeviceInfo::GetInstance()->IsSetOutputSurfaceSupported()),
73 traits.android_overlay_factory_cb,
74 std::move(traits.request_overlay_info_cb),
75 std::make_unique<VideoFrameFactoryImpl>(
76 traits.gpu_task_runner, traits.gpu_preferences,
77 std::move(image_provider),
78 MaybeRenderEarlyManager::Create(traits.gpu_task_runner,
80 std::move(frame_info_helper), ref_counted_lock),
84 absl::optional<SupportedVideoDecoderConfigs>
85 GetPlatformSupportedVideoDecoderConfigs(
86 base::WeakPtr<MediaGpuChannelManager> manager,
87 gpu::GpuDriverBugWorkarounds gpu_workarounds,
88 gpu::GpuPreferences gpu_preferences,
89 const gpu::GPUInfo& gpu_info,
90 base::OnceCallback<SupportedVideoDecoderConfigs()> get_vda_configs) {
91 return MediaCodecVideoDecoder::GetSupportedConfigs();
94 std::unique_ptr<AudioDecoder> CreatePlatformAudioDecoder(
95 scoped_refptr<base::SequencedTaskRunner> task_runner,
96 std::unique_ptr<MediaLog> media_log) {
97 return std::make_unique<MediaCodecAudioDecoder>(std::move(task_runner));
100 std::unique_ptr<AudioEncoder> CreatePlatformAudioEncoder(
101 scoped_refptr<base::SequencedTaskRunner> task_runner) {
102 #if BUILDFLAG(USE_PROPRIETARY_CODECS)
103 if (!NdkAudioEncoder::IsSupported()) {
107 return std::make_unique<NdkAudioEncoder>(std::move(task_runner));
113 std::unique_ptr<CdmFactory> CreatePlatformCdmFactory(
114 mojom::FrameInterfaceFactory* frame_interfaces) {
115 return std::make_unique<AndroidCdmFactory>(
116 base::BindRepeating(&CreateProvisionFetcher, frame_interfaces),
117 base::BindRepeating(&CreateMediaDrmStorage, frame_interfaces));
120 VideoDecoderType GetPlatformDecoderImplementationType(
121 gpu::GpuDriverBugWorkarounds gpu_workarounds,
122 gpu::GpuPreferences gpu_preferences,
123 const gpu::GPUInfo& gpu_info) {
124 return VideoDecoderType::kMediaCodec;