From 7d6816703899c61fcd7051005ddfa5a811c9c588 Mon Sep 17 00:00:00 2001 From: Bo-Rong Chen Date: Fri, 8 Nov 2024 17:56:07 -0800 Subject: [PATCH] [media] Add StarboardRendererFactory 1. Add StarboardRendererFactory for MediaFactory to select StarboardRenderer when `use_starboard_media=true`. 2. Remove hard code StarboardRenderer in WebMediaPlayerImpl. Note RendererType::kStarboard cannot be gated due to mojom files. b/375278384 --- .../java/dev/cobalt/coat/CobaltActivity.java | 16 +++++++++++++--- .../service/display/overlay_processor_android.cc | 9 ++++++++- content/renderer/media/media_factory.cc | 15 +++++++++++---- .../media/media_permission_dispatcher.cc | 7 ++++++- media/base/media_switches.cc | 15 +++++++++++++++ media/base/media_switches.h | 8 ++++++++ 6 files changed, 61 insertions(+), 9 deletions(-) diff --git a/cobalt/android/apk/app/src/main/java/dev/cobalt/coat/CobaltActivity.java b/cobalt/android/apk/app/src/main/java/dev/cobalt/coat/CobaltActivity.java index 5b2b75e133b8..02a5a9546b17 100644 --- a/cobalt/android/apk/app/src/main/java/dev/cobalt/coat/CobaltActivity.java +++ b/cobalt/android/apk/app/src/main/java/dev/cobalt/coat/CobaltActivity.java @@ -81,6 +81,10 @@ public abstract class CobaltActivity extends Activity { private static final Pattern URL_PARAM_PATTERN = Pattern.compile("^[a-zA-Z0-9_=]*$"); + // Media switch - media_switches::kDisableStarboardRenderer + private static final String DISABLE_STARBOARD_RENDERER_SWITCH = "disable-starboard-renderer"; + + public static final int JAVA_BRIDGE_INITIALIZATION_DELAY_MILLI_SECONDS = 100; // Maintain the list of JavaScript-exposed objects as a member variable // to prevent them from being garbage collected prematurely. private List javaScriptAndroidObjectList = new ArrayList<>(); @@ -391,9 +395,15 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); createContent(savedInstanceState); - videoSurfaceView = new VideoSurfaceView(this); - addContentView( - videoSurfaceView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); + if (CommandLine.getInstance().hasSwitch(DISABLE_STARBOARD_RENDERER_SWITCH)) { + // TODO(b/326827007): Revisit renderer to support secondary videos. + Log.d(TAG, "Disable starboard renderer."); + } else { + Log.d(TAG, "Enable starboard renderer."); + videoSurfaceView = new VideoSurfaceView(this); + addContentView( + videoSurfaceView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); + } } /** diff --git a/components/viz/service/display/overlay_processor_android.cc b/components/viz/service/display/overlay_processor_android.cc index 874dd2e3bad6..122a43ca545a 100644 --- a/components/viz/service/display/overlay_processor_android.cc +++ b/components/viz/service/display/overlay_processor_android.cc @@ -18,6 +18,7 @@ #include "ui/gfx/geometry/rect_conversions.h" #if BUILDFLAG(USE_STARBOARD_MEDIA) +#include "media/base/media_switches.h" #include "components/viz/service/display/starboard/overlay_strategy_underlay_starboard.h" #endif // BUILDFLAG(USE_STARBOARD_MEDIA) @@ -60,7 +61,13 @@ OverlayProcessorAndroid::OverlayProcessorAndroid( // a dummy resource that has no relation to what the overlay contains. // https://crbug.com/842931 . #if BUILDFLAG(USE_STARBOARD_MEDIA) - strategies_.push_back(std::make_unique(this)); + if (media::IsStarboardRendererEnabled()) { + strategies_.push_back( + std::make_unique(this)); + } else { + strategies_.push_back(std::make_unique( + this, OverlayStrategyUnderlay::OpaqueMode::AllowTransparentCandidates)); + } #else // BUILDFLAG(USE_STARBOARD_MEDIA) strategies_.push_back(std::make_unique( this, OverlayStrategyUnderlay::OpaqueMode::AllowTransparentCandidates)); diff --git a/content/renderer/media/media_factory.cc b/content/renderer/media/media_factory.cc index 4a4d8d97ddc0..14528e6cc7c8 100644 --- a/content/renderer/media/media_factory.cc +++ b/content/renderer/media/media_factory.cc @@ -761,13 +761,20 @@ MediaFactory::CreateRendererFactorySelector( is_base_renderer_factory_set = true; #if BUILDFLAG(USE_STARBOARD_MEDIA) // TODO(b/326827007): Revisit renderer to support secondary videos. - factory_selector->AddFactory( - RendererType::kStarboard, - std::make_unique( + if (media::IsStarboardRendererEnabled()) { + factory_selector->AddFactory( + RendererType::kStarboard, + std::make_unique( media_log, // TODO: b/383327725 - Cobalt: Inject these values from the web app. kWriteDurationLocal, kWriteDurationRemote)); - factory_selector->SetBaseRendererType(RendererType::kStarboard); + factory_selector->SetBaseRendererType(RendererType::kStarboard); + } else { + auto renderer_impl_factory = CreateRendererImplFactory( + player_id, media_log, decoder_factory, render_thread, render_frame_); + factory_selector->AddBaseFactory(RendererType::kRendererImpl, + std::move(renderer_impl_factory)); + } #else // BUILDFLAG(USE_STARBOARD_MEDIA) auto renderer_impl_factory = CreateRendererImplFactory( player_id, media_log, decoder_factory, render_thread, render_frame_); diff --git a/content/renderer/media/media_permission_dispatcher.cc b/content/renderer/media/media_permission_dispatcher.cc index e79a4f13b1f3..6bae480fb2f6 100644 --- a/content/renderer/media/media_permission_dispatcher.cc +++ b/content/renderer/media/media_permission_dispatcher.cc @@ -11,6 +11,10 @@ #include "third_party/blink/public/web/web_local_frame.h" #include "url/gurl.h" +#if BUILDFLAG(USE_STARBOARD_MEDIA) +#include "media/base/media_switches.h" +#endif // BUILDFLAG(USE_STARBOARD_MEDIA) + namespace { using Type = media::MediaPermission::Type; @@ -150,7 +154,8 @@ void MediaPermissionDispatcher::OnPermissionStatus( requests_.erase(iter); #if BUILDFLAG(USE_STARBOARD_MEDIA) - std::move(permission_status_cb).Run(true); + // DRM is only supported using StarboardRenderer. + std::move(permission_status_cb).Run(media::IsStarboardRendererEnabled()); #else // BUILDFLAG(USE_STARBOARD_MEDIA) std::move(permission_status_cb) .Run(status == blink::mojom::PermissionStatus::GRANTED); diff --git a/media/base/media_switches.cc b/media/base/media_switches.cc index 16a035ca7921..952d6b80e402 100644 --- a/media/base/media_switches.cc +++ b/media/base/media_switches.cc @@ -240,6 +240,11 @@ MEDIA_EXPORT extern const char kLacrosUseChromeosProtectedAv1[] = "lacros-use-chromeos-protected-av1"; #endif // BUILDFLAG(IS_CHROMEOS) +#if BUILDFLAG(USE_STARBOARD_MEDIA) +// Force media player using default Renderer instead of StarboardRenderer. +const char kDisableStarboardRenderer[] = "disable-starboard-renderer"; +#endif // BUILDFLAG(USE_STARBOARD_MEDIA) + namespace autoplay { // Autoplay policy that requires a document user activation. @@ -1557,4 +1562,14 @@ uint32_t GetPassthroughAudioFormats() { #endif // BUILDFLAG(ENABLE_PASSTHROUGH_AUDIO_CODECS) } +#if BUILDFLAG(USE_STARBOARD_MEDIA) +bool IsStarboardRendererEnabled() { + if (base::CommandLine::ForCurrentProcess()->HasSwitch( + switches::kDisableStarboardRenderer)) { + return false; + } + return true; +} +#endif // BUILDFLAG(USE_STARBOARD_MEDIA) + } // namespace media diff --git a/media/base/media_switches.h b/media/base/media_switches.h index 376f1fd3e46c..386650316c72 100644 --- a/media/base/media_switches.h +++ b/media/base/media_switches.h @@ -99,6 +99,10 @@ MEDIA_EXPORT extern const char kLacrosUseChromeosProtectedMedia[]; MEDIA_EXPORT extern const char kLacrosUseChromeosProtectedAv1[]; #endif // BUILDFLAG(IS_CHROMEOS) +#if BUILDFLAG(USE_STARBOARD_MEDIA) +MEDIA_EXPORT extern const char kDisableStarboardRenderer[]; +#endif // BUILDFLAG(USE_STARBOARD_MEDIA) + namespace autoplay { MEDIA_EXPORT extern const char kDocumentUserActivationRequiredPolicy[]; @@ -449,6 +453,10 @@ MEDIA_EXPORT bool IsMediaFoundationD3D11VideoCaptureEnabled(); MEDIA_EXPORT bool IsOutOfProcessVideoDecodingEnabled(); #endif // BUILDFLAG(ALLOW_OOP_VIDEO_DECODER) +#if BUILDFLAG(USE_STARBOARD_MEDIA) +MEDIA_EXPORT bool IsStarboardRendererEnabled(); +#endif // BUILDFLAG(USE_STARBOARD_MEDIA) + enum class kCrosGlobalMediaControlsPinOptions { kPin, kNotPin,