Skip to content

Commit

Permalink
[media] Add StarboardRendererFactory
Browse files Browse the repository at this point in the history
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
  • Loading branch information
borongc committed Nov 9, 2024
1 parent 819f56c commit bdb50e6
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ 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.
Expand Down Expand Up @@ -344,9 +347,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));
}

initializeJavaBridge();
}
Expand Down
9 changes: 8 additions & 1 deletion components/viz/service/display/overlay_processor_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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<OverlayStrategyUnderlayStarboard>(this));
if (media::IsStarboardRendererEnabled()) {
strategies_.push_back(
std::make_unique<OverlayStrategyUnderlayStarboard>(this));
} else {
strategies_.push_back(std::make_unique<OverlayStrategyUnderlay>(
this, OverlayStrategyUnderlay::OpaqueMode::AllowTransparentCandidates));
}
#else // BUILDFLAG(USE_STARBOARD_MEDIA)
strategies_.push_back(std::make_unique<OverlayStrategyUnderlay>(
this, OverlayStrategyUnderlay::OpaqueMode::AllowTransparentCandidates));
Expand Down
16 changes: 16 additions & 0 deletions content/renderer/media/media_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@
#include "media/mojo/mojom/speech_recognition_service.mojom.h"
#endif // BUILDFLAG(IS_WIN)

#if BUILDFLAG(USE_STARBOARD_MEDIA)
#include "media/starboard/starboard_renderer_factory.h"
#endif // BUILDFLAG(USE_STARBOARD_MEDIA)

namespace {

// This limit is much higher than it needs to be right now, because the logic
Expand Down Expand Up @@ -740,6 +744,18 @@ MediaFactory::CreateRendererFactorySelector(
}
#endif // BUILDFLAG(IS_CASTOS) || BUILDFLAG(IS_CAST_ANDROID)

#if BUILDFLAG(USE_STARBOARD_MEDIA)
if (!is_base_renderer_factory_set && media::IsStarboardRendererEnabled()) {
// TODO(b/326827007): Revisit renderer to support secondary videos
// TODO(b/328305808): DRM is only supported when using StarboardRenderer
is_base_renderer_factory_set = true;
factory_selector->AddFactory(
RendererType::kStarboard,
std::make_unique<media::StarboardRendererFactory>());
factory_selector->SetBaseRendererType(RendererType::kStarboard);
}
#endif // BUILDFLAG(USE_STARBOARD_MEDIA)

if (!is_base_renderer_factory_set) {
// TODO(crbug.com/1265448): These sorts of checks shouldn't be necessary if
// this method were significantly refactored to split things up by
Expand Down
15 changes: 15 additions & 0 deletions media/base/media_switches.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -1550,4 +1555,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
8 changes: 8 additions & 0 deletions media/base/media_switches.h
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -446,6 +450,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,
Expand Down
4 changes: 4 additions & 0 deletions media/base/renderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ std::string GetRendererName(RendererType renderer_type) {
return "CastStreamingRenderer";
case RendererType::kContentEmbedderDefined:
return "EmbedderDefined";
// TODO(b/375278384): RendererType::kStarboard cannot
// be gated due to media_types.mojom
case RendererType::kStarboard:
return "StarboardRenderer";
case RendererType::kTest:
return "Media Renderer Implementation For Testing";
}
Expand Down
5 changes: 4 additions & 1 deletion media/base/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ enum class RendererType {
kRemoting = 8, // RemotingRendererFactory for remoting::Receiver
kCastStreaming = 9, // PlaybackCommandForwardingRendererFactory
kContentEmbedderDefined = 10, // Defined by the content embedder
kTest = 11, // Renderer implementations used in tests
// TODO(b/375278384): RendererType::kStarboard cannot
// be gated due to media_types.mojom
kStarboard = 11, // StarboardRendererFactory
kTest = 12, // Renderer implementations used in tests
kMaxValue = kTest,
};

Expand Down
3 changes: 2 additions & 1 deletion media/mojo/mojom/media_types.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -534,5 +534,6 @@ enum RendererType {
kRemoting = 8, // RemotingRendererFactory for remoting::Receiver
kCastStreaming = 9, // CastStreamingRendererFactory
kContentEmbedderDefined = 10, // Defined by the content embedder
kTest= 11, // Renderer implementations used in tests
kStarboard = 11, // StarboardRendererFactory
kTest= 12, // Renderer implementations used in tests
};
5 changes: 5 additions & 0 deletions media/mojo/mojom/media_types_enum_mojom_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ struct EnumTraits<media::mojom::RendererType, ::media::RendererType> {
return media::mojom::RendererType::kCastStreaming;
case ::media::RendererType::kContentEmbedderDefined:
return media::mojom::RendererType::kContentEmbedderDefined;
case ::media::RendererType::kStarboard:
return media::mojom::RendererType::kStarboard;
case ::media::RendererType::kTest:
return media::mojom::RendererType::kTest;
}
Expand Down Expand Up @@ -356,6 +358,9 @@ struct EnumTraits<media::mojom::RendererType, ::media::RendererType> {
case media::mojom::RendererType::kContentEmbedderDefined:
*output = ::media::RendererType::kContentEmbedderDefined;
return true;
case media::mojom::RendererType::kStarboard:
*output = ::media::RendererType::kStarboard;
return true;
case media::mojom::RendererType::kTest:
*output = ::media::RendererType::kTest;
return true;
Expand Down
2 changes: 2 additions & 0 deletions media/starboard/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ source_set("starboard") {
"starboard_memory_allocator.h",
"starboard_renderer.cc",
"starboard_renderer.h",
"starboard_renderer_factory.cc",
"starboard_renderer_factory.h",
"starboard_utils.cc",
"starboard_utils.h",
]
Expand Down
42 changes: 42 additions & 0 deletions media/starboard/starboard_renderer_factory.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "media/starboard/starboard_renderer_factory.h"

#include <memory>

#include "base/check.h"
#include "base/logging.h"
#include "base/task/sequenced_task_runner.h"
#include "media/starboard/starboard_renderer.h"

namespace media {

StarboardRendererFactory::StarboardRendererFactory() {}

StarboardRendererFactory::~StarboardRendererFactory() = default;

std::unique_ptr<Renderer> StarboardRendererFactory::CreateRenderer(
const scoped_refptr<base::SequencedTaskRunner>& media_task_runner,
const scoped_refptr<base::TaskRunner>& worker_task_runner,
AudioRendererSink* audio_renderer_sink,
VideoRendererSink* video_renderer_sink,
RequestOverlayInfoCB request_overlay_info_cb,
const gfx::ColorSpace& target_color_space) {
DCHECK(video_renderer_sink);
return std::make_unique<media::StarboardRenderer>(media_task_runner,
video_renderer_sink);
}

} // namespace media
44 changes: 44 additions & 0 deletions media/starboard/starboard_renderer_factory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef MEDIA_STARBOARD_STARBOARD_RENDERER_FACTORY_H_
#define MEDIA_STARBOARD_STARBOARD_RENDERER_FACTORY_H_

#include "base/task/sequenced_task_runner.h"
#include "media/base/renderer_factory.h"

namespace media {

// Creates Renderers using Starboard.
class MEDIA_EXPORT StarboardRendererFactory final : public RendererFactory {
public:
StarboardRendererFactory();

StarboardRendererFactory(const StarboardRendererFactory&) = delete;
StarboardRendererFactory& operator=(const StarboardRendererFactory&) = delete;

~StarboardRendererFactory() final;

std::unique_ptr<Renderer> CreateRenderer(
const scoped_refptr<base::SequencedTaskRunner>& media_task_runner,
const scoped_refptr<base::TaskRunner>& worker_task_runner,
AudioRendererSink* audio_renderer_sink,
VideoRendererSink* video_renderer_sink,
RequestOverlayInfoCB request_overlay_info_cb,
const gfx::ColorSpace& target_color_space) final;
};

} // namespace media

#endif // MEDIA_STARBOARD_STARBOARD_RENDERER_FACTORY_H_
23 changes: 8 additions & 15 deletions third_party/blink/renderer/platform/media/web_media_player_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2800,21 +2800,6 @@ std::unique_ptr<media::Renderer> WebMediaPlayerImpl::CreateRenderer(
&WebMediaPlayerImpl::OnOverlayInfoRequested, weak_this_));
#endif

#if BUILDFLAG(USE_STARBOARD_MEDIA)
// TODO(b/375278384): Select the StarboardRenderer properly instead of
// hard coding.

// StarboardRenderer always uses full screen with overlay video mode.
overlay_info_.is_fullscreen = true;

// `media_task_runner_` is always true, use an if statement to avoid
// potential build warning on unreachable code.
if (media_task_runner_) {
return std::make_unique<media::StarboardRenderer>(media_task_runner_,
compositor_.get());
}
#endif // BUILDFLAG(USE_STARBOARD_MEDIA)

if (renderer_type) {
DVLOG(1) << __func__
<< ": renderer_type=" << static_cast<int>(renderer_type.value());
Expand All @@ -2831,6 +2816,14 @@ std::unique_ptr<media::Renderer> WebMediaPlayerImpl::CreateRenderer(
media_metrics_provider_->SetRendererType(renderer_type_);
media_log_->SetProperty<MediaLogProperty::kRendererName>(renderer_type_);

#if BUILDFLAG(USE_STARBOARD_MEDIA)
LOG(INFO) << "Renderer Type is " << GetRendererName(renderer_type_) << ".";
if (renderer_type_ == media::RendererType::kStarboard) {
// StarboardRenderer always uses full screen with overlay video mode.
overlay_info_.is_fullscreen = true;
}
#endif // BUILDFLAG(USE_STARBOARD_MEDIA)

return renderer_factory_selector_->GetCurrentFactory()->CreateRenderer(
media_task_runner_, worker_task_runner_, audio_source_provider_.get(),
compositor_.get(), std::move(request_overlay_info_cb),
Expand Down

0 comments on commit bdb50e6

Please sign in to comment.