Skip to content

Commit

Permalink
[media] Limit max number of buffers to write with audio write ahead
Browse files Browse the repository at this point in the history
b/227837774
  • Loading branch information
borongc committed May 16, 2024
1 parent c291e10 commit d2226ce
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
41 changes: 41 additions & 0 deletions cobalt/media/base/sbplayer_pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,8 @@ void SbPlayerPipeline::OnDemuxerStreamRead(
for (const auto& buffer : buffers) {
playback_statistics_.OnAudioAU(buffer);
if (!buffer->end_of_stream()) {
last_audio_sample_interval_ =
buffer->timestamp() - timestamp_of_last_written_audio_;
timestamp_of_last_written_audio_ = buffer->timestamp();
}
}
Expand Down Expand Up @@ -1118,6 +1120,25 @@ void SbPlayerPipeline::OnNeedData(DemuxerStream::Type type,
audio_read_delayed_ = true;
return;
}
// LOG(ERROR) << "Brown test done " << time_ahead_of_playback;
max_buffers =
std::min(max_buffers, GetEstimatedMaxBuffers(adjusted_write_duration,
time_ahead_of_playback));
} else {
max_buffers =
std::min(max_buffers,
GetEstimatedMaxBuffers(
AdjustWriteDurationForPlaybackRate(
audio_write_duration_for_preroll_, playback_rate_),
timestamp_of_last_written_audio_ - seek_time_));
/*LOG(ERROR) << "Brown max_buffers " << max_buffers
<< " " <<
AdjustWriteDurationForPlaybackRate(audio_write_duration_for_preroll_,
playback_rate_)
<< " " << (timestamp_of_last_written_audio_ - seek_time_)
<< " " <<
(AdjustWriteDurationForPlaybackRate(audio_write_duration_for_preroll_,
playback_rate_) + kMediaTimeCheckInterval);*/
}

audio_read_delayed_ = false;
Expand All @@ -1135,6 +1156,26 @@ void SbPlayerPipeline::OnNeedData(DemuxerStream::Type type,
type, max_buffers));
}

int SbPlayerPipeline::GetEstimatedMaxBuffers(TimeDelta write_duration,
TimeDelta time_ahead_of_playback) {
int estimated_max_buffers = 1;
if (!last_audio_sample_interval_.is_zero()) {
TimeDelta time_allow_to_write = write_duration - time_ahead_of_playback;
estimated_max_buffers = time_allow_to_write.InMillisecondsRoundedUp() /
last_audio_sample_interval_.InMilliseconds() +
1;
/*LOG(ERROR) << "time_allow_to_write " << time_allow_to_write
<< " " << last_audio_sample_interval_
<< " " << time_ahead_of_playback
<< " " << write_duration
<< " " << estimated_max_buffers
<< " " << (time_allow_to_write.InMillisecondsF() /
last_audio_sample_interval_.InMillisecondsF());*/
}
return allow_batched_sample_write_ ? estimated_max_buffers : 1;
}


void SbPlayerPipeline::OnPlayerStatus(SbPlayerState state) {
DCHECK(task_runner_->RunsTasksInCurrentSequence());

Expand Down
4 changes: 4 additions & 0 deletions cobalt/media/base/sbplayer_pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ class MEDIA_EXPORT SbPlayerPipeline : public Pipeline,
void SetReadInProgress(::media::DemuxerStream::Type type, bool in_progress);
bool GetReadInProgress(::media::DemuxerStream::Type type) const;

int GetEstimatedMaxBuffers(TimeDelta write_duration,
TimeDelta time_ahead_of_playback);

// An identifier string for the pipeline, used in CVal to identify multiple
// pipelines.
const std::string pipeline_identifier_;
Expand Down Expand Up @@ -345,6 +348,7 @@ class MEDIA_EXPORT SbPlayerPipeline : public Pipeline,
// Indicates if video end of stream has been written into the underlying
// player.
bool is_video_eos_written_ = false;
TimeDelta last_audio_sample_interval_ = TimeDelta::FromMicroseconds(0);

// Last media time reported by GetMediaTime().
base::CVal<TimeDelta> last_media_time_;
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;
bool allow_batched_sample_write_ = true;
// When set to `false` (the default value), Cobalt calls
// `SbPlayerGetPreferredOutputMode()` with `kSbPlayerOutputModeInvalid` when
// there is no preference on output mode.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@

int SbPlayerGetMaximumNumberOfSamplesPerWrite(SbPlayer player,
SbMediaType sample_type) {
return 1;
return 64;
}

0 comments on commit d2226ce

Please sign in to comment.