Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[android] Support configuring maximum number of samples per write to SbPlayer from webapp #3325

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions cobalt/media/base/sbplayer_pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ SbPlayerPipeline::SbPlayerPipeline(
const scoped_refptr<base::SequencedTaskRunner>& task_runner,
const GetDecodeTargetGraphicsContextProviderFunc&
get_decode_target_graphics_context_provider_func,
bool allow_resume_after_suspend, bool allow_batched_sample_write,
bool allow_resume_after_suspend, int audio_batched_sample_write,
bool force_punch_out_by_default,
#if SB_API_VERSION >= 15
TimeDelta audio_write_duration_local, TimeDelta audio_write_duration_remote,
Expand All @@ -146,7 +146,7 @@ SbPlayerPipeline::SbPlayerPipeline(
sbplayer_interface_(interface),
task_runner_(task_runner),
allow_resume_after_suspend_(allow_resume_after_suspend),
allow_batched_sample_write_(allow_batched_sample_write),
audio_batched_sample_write_(audio_batched_sample_write),
window_(window),
get_decode_target_graphics_context_provider_func_(
get_decode_target_graphics_context_provider_func),
Expand Down Expand Up @@ -1111,8 +1111,10 @@ void SbPlayerPipeline::OnNeedData(DemuxerStream::Type type,
return;
}

int max_buffers =
allow_batched_sample_write_ ? max_number_of_buffers_to_write : 1;
int max_buffers = audio_batched_sample_write_ > 1
? std::min(max_number_of_buffers_to_write,
audio_batched_sample_write_)
: 1;

if (GetReadInProgress(type)) return;

Expand Down Expand Up @@ -1164,13 +1166,13 @@ void SbPlayerPipeline::OnNeedData(DemuxerStream::Type type,
audio_read_delayed_ = true;
return;
}
if (allow_batched_sample_write_ &&
if (audio_batched_sample_write_ > 1 &&
!time_ahead_of_playback.is_negative()) {
estimated_max_buffers = GetEstimatedMaxBuffers(adjusted_write_duration,
time_ahead_of_playback,
false /* is_preroll */);
}
} else if (allow_batched_sample_write_) {
} else if (audio_batched_sample_write_ > 1) {
if (!time_ahead_of_playback_for_preroll.is_negative()) {
estimated_max_buffers = GetEstimatedMaxBuffers(
adjusted_write_duration_for_preroll,
Expand Down Expand Up @@ -1226,7 +1228,7 @@ int SbPlayerPipeline::GetEstimatedMaxBuffers(TimeDelta write_duration,
DCHECK_GE(time_ahead_of_playback.InMicroseconds(), 0);

int estimated_max_buffers = 1;
if (!allow_batched_sample_write_ ||
if (!(audio_batched_sample_write_ > 1) ||
write_duration <= time_ahead_of_playback) {
return estimated_max_buffers;
}
Expand Down
9 changes: 6 additions & 3 deletions cobalt/media/base/sbplayer_pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class MEDIA_EXPORT SbPlayerPipeline : public Pipeline,
const GetDecodeTargetGraphicsContextProviderFunc&
get_decode_target_graphics_context_provider_func,
bool allow_resume_after_suspend,
bool allow_batched_sample_write,
int audio_batched_sample_write,
bool force_punch_out_by_default,
#if SB_API_VERSION >= 15
TimeDelta audio_write_duration_local,
Expand Down Expand Up @@ -222,8 +222,11 @@ class MEDIA_EXPORT SbPlayerPipeline : public Pipeline,
// Whether we should save DecoderBuffers for resume after suspend.
const bool allow_resume_after_suspend_;

// Whether we enable batched sample write functionality.
const bool allow_batched_sample_write_;
// The number of samples per write that is allowed at once.
// The minimum number of |audio_batched_sample_write_| and
// SbPlayerGetMaximumNumberOfSamplesPerWrite() specifies
// the maximum number of samples SbPlayerWriteSamples() can accept.
const int audio_batched_sample_write_;

// The default output mode passed to `SbPlayerGetPreferredOutputMode()`.
SbPlayerOutputMode default_output_mode_ = kSbPlayerOutputModeInvalid;
Expand Down
9 changes: 4 additions & 5 deletions cobalt/media/media_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,9 @@ class CanPlayTypeHandlerStarboard : public CanPlayTypeHandler {
} // namespace

bool MediaModule::SetConfiguration(const std::string& name, int32 value) {
if (name == "EnableBatchedSampleWrite") {
allow_batched_sample_write_ = value;
LOG(INFO) << (allow_batched_sample_write_ ? "Enabling" : "Disabling")
<< " batched sample write.";
if (name == "AudioBatchedSampleWrite" && value > 0) {
audio_batched_sample_write_ = value;
LOG(INFO) << "Set MaximumNumberOfSamplesPerWrite to " << value;
borongc marked this conversation as resolved.
Show resolved Hide resolved
return true;
} else if (name == "ForcePunchOutByDefault") {
force_punch_out_by_default_ = value;
Expand Down Expand Up @@ -249,7 +248,7 @@ std::unique_ptr<WebMediaPlayer> MediaModule::CreateWebMediaPlayer(
base::Bind(&MediaModule::GetSbDecodeTargetGraphicsContextProvider,
base::Unretained(this)),
client, this, options_.allow_resume_after_suspend,
allow_batched_sample_write_, force_punch_out_by_default_,
audio_batched_sample_write_, force_punch_out_by_default_,
#if SB_API_VERSION >= 15
audio_write_duration_local_, audio_write_duration_remote_,
#endif // SB_API_VERSION >= 15
Expand Down
2 changes: 1 addition & 1 deletion cobalt/media/media_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class MediaModule : public WebMediaPlayerFactory,
Players players_;
bool suspended_ = false;

bool allow_batched_sample_write_ = false;
int audio_batched_sample_write_ = 1;
borongc marked this conversation as resolved.
Show resolved Hide resolved
// When set to `false` (the default value), Cobalt calls
// `SbPlayerGetPreferredOutputMode()` with `kSbPlayerOutputModeInvalid` when
// there is no preference on output mode.
Expand Down
6 changes: 3 additions & 3 deletions cobalt/media/player/web_media_player_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ WebMediaPlayerImpl::WebMediaPlayerImpl(
const Pipeline::GetDecodeTargetGraphicsContextProviderFunc&
get_decode_target_graphics_context_provider_func,
WebMediaPlayerClient* client, WebMediaPlayerDelegate* delegate,
bool allow_resume_after_suspend, bool allow_batched_sample_write,
bool allow_resume_after_suspend, int audio_batched_sample_write,
bool force_punch_out_by_default,
#if SB_API_VERSION >= 15
base::TimeDelta audio_write_duration_local,
Expand All @@ -123,7 +123,7 @@ WebMediaPlayerImpl::WebMediaPlayerImpl(
client_(client),
delegate_(delegate),
allow_resume_after_suspend_(allow_resume_after_suspend),
allow_batched_sample_write_(allow_batched_sample_write),
audio_batched_sample_write_(audio_batched_sample_write),
force_punch_out_by_default_(force_punch_out_by_default),
proxy_(new WebMediaPlayerProxy(task_runner_, this)),
media_log_(media_log),
Expand All @@ -143,7 +143,7 @@ WebMediaPlayerImpl::WebMediaPlayerImpl(
pipeline_ = new SbPlayerPipeline(
interface, window, pipeline_thread_.task_runner(),
get_decode_target_graphics_context_provider_func,
allow_resume_after_suspend_, allow_batched_sample_write_,
allow_resume_after_suspend_, audio_batched_sample_write_,
force_punch_out_by_default_,
#if SB_API_VERSION >= 15
audio_write_duration_local, audio_write_duration_remote,
Expand Down
4 changes: 2 additions & 2 deletions cobalt/media/player/web_media_player_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class WebMediaPlayerImpl : public WebMediaPlayer,
WebMediaPlayerClient* client,
WebMediaPlayerDelegate* delegate,
bool allow_resume_after_suspend,
bool allow_batched_sample_write,
int audio_batched_sample_write,
bool force_punch_out_by_default,
#if SB_API_VERSION >= 15
base::TimeDelta audio_write_duration_local,
Expand Down Expand Up @@ -304,7 +304,7 @@ class WebMediaPlayerImpl : public WebMediaPlayer,
WebMediaPlayerClient* const client_;
WebMediaPlayerDelegate* const delegate_;
const bool allow_resume_after_suspend_;
const bool allow_batched_sample_write_;
const int audio_batched_sample_write_;
const bool force_punch_out_by_default_;
scoped_refptr<DecodeTargetProvider> decode_target_provider_;

Expand Down
2 changes: 2 additions & 0 deletions starboard/android/shared/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ static_library("starboard_platform") {
"player_components_factory.h",
"player_create.cc",
"player_destroy.cc",
"player_get_maximum_number_of_samples_per_write.cc",
"player_get_preferred_output_mode.cc",
"player_set_bounds.cc",
"player_set_max_video_input_size.cc",
Expand Down Expand Up @@ -436,6 +437,7 @@ static_library("starboard_platform") {
sources -= [
"//starboard/shared/starboard/player/player_create.cc",
"//starboard/shared/starboard/player/player_destroy.cc",
"//starboard/shared/starboard/player/player_get_maximum_number_of_samples_per_write.cc",
"//starboard/shared/starboard/player/player_get_preferred_output_mode_prefer_punchout.cc",
"//starboard/shared/starboard/player/player_set_bounds.cc",
"//starboard/shared/starboard/player/player_set_playback_rate.cc",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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 "starboard/player.h"

int SbPlayerGetMaximumNumberOfSamplesPerWrite(SbPlayer player,
SbMediaType sample_type) {
return 256;
}
Loading