Skip to content

Commit

Permalink
Launch HTMLMediaElement.buffered optimization
Browse files Browse the repository at this point in the history
This is needed for MSE-in-Workers support. See context in the linked
bug.

b/338452286
  • Loading branch information
at-ninja committed Sep 27, 2024
1 parent 6f2089c commit 13e37ed
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 77 deletions.
36 changes: 9 additions & 27 deletions cobalt/dom/html_media_element.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,6 @@ const MediaSettings& GetMediaSettings(
return web_settings->media_settings();
}

// If this function returns true, HTMLMediaElement::buffered() will attempt to
// call MediaSource::GetBufferedRange() if available, and fallback to
// WebMediaPlayer::UpdateBufferedTimeRanges().
// The default value is false.
bool IsMediaElementUsingMediaSourceBufferedRangeEnabled(
const web::EnvironmentSettings* settings) {
return GetMediaSettings(settings)
.IsMediaElementUsingMediaSourceBufferedRangeEnabled()
.value_or(false);
}

// If this function returns true, HTMLMediaElement will proxy calls to the
// attached MediaSource object through the MediaSourceAttachment interface
// instead of directly calling against the MediaSource object.
Expand Down Expand Up @@ -257,25 +246,18 @@ uint16_t HTMLMediaElement::network_state() const {
}

scoped_refptr<TimeRanges> HTMLMediaElement::buffered() const {
scoped_refptr<TimeRanges> buffered = new TimeRanges;

DCHECK(node_document());
DCHECK(node_document()->html_element_context());
DCHECK(node_document()->html_element_context()->environment_settings());
const auto* settings =
node_document()->html_element_context()->environment_settings();
if (IsMediaElementUsingMediaSourceBufferedRangeEnabled(settings)) {
if (is_using_media_source_attachment_methods_) {
if (media_source_attachment_) {
return media_source_attachment_->GetBufferedRange();
}
} else {
if (media_source_) {
return media_source_->GetBufferedRange();
}
if (is_using_media_source_attachment_methods_) {
if (media_source_attachment_) {
return media_source_attachment_->GetBufferedRange();
}
} else {
if (media_source_) {
return media_source_->GetBufferedRange();
}
}

scoped_refptr<TimeRanges> buffered = new TimeRanges;

if (!player_) {
LOG(INFO) << "(empty)";
return buffered;
Expand Down
6 changes: 0 additions & 6 deletions cobalt/dom/media_settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,6 @@ bool MediaSettingsImpl::Set(const std::string& name, int value) {
LOG(INFO) << name << ": set to " << value;
return true;
}
} else if (name == "MediaElement.EnableUsingMediaSourceBufferedRange") {
if (value == 0 || value == 1) {
is_media_element_using_media_source_buffered_range_enabled_ = value != 0;
LOG(INFO) << name << ": set to " << value;
return true;
}
} else if (name == "MediaElement.EnableUsingMediaSourceAttachmentMethods") {
if (value == 0 || value == 1) {
is_media_element_using_media_source_attachment_methods_enabled_ =
Expand Down
9 changes: 0 additions & 9 deletions cobalt/dom/media_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ class MediaSettings {
GetMediaElementTimeupdateEventIntervalInMilliseconds() const = 0;
virtual base::Optional<bool> IsPaintingVideoBackgroundToBlack() const = 0;
virtual base::Optional<bool>
IsMediaElementUsingMediaSourceBufferedRangeEnabled() const = 0;
virtual base::Optional<bool>
IsMediaElementUsingMediaSourceAttachmentMethodsEnabled() const = 0;

protected:
Expand Down Expand Up @@ -97,11 +95,6 @@ class MediaSettingsImpl : public MediaSettings {
base::Optional<bool> IsPaintingVideoBackgroundToBlack() const override {
return is_painting_video_background_to_black_;
}
base::Optional<bool> IsMediaElementUsingMediaSourceBufferedRangeEnabled()
const override {
base::AutoLock auto_lock(lock_);
return is_media_element_using_media_source_buffered_range_enabled_;
}
base::Optional<bool> IsMediaElementUsingMediaSourceAttachmentMethodsEnabled()
const override {
base::AutoLock auto_lock(lock_);
Expand All @@ -124,8 +117,6 @@ class MediaSettingsImpl : public MediaSettings {
base::Optional<int> max_source_buffer_append_size_in_bytes_;

base::Optional<int> media_element_timeupdate_event_interval_in_milliseconds_;
base::Optional<bool>
is_media_element_using_media_source_buffered_range_enabled_;
base::Optional<bool>
is_media_element_using_media_source_attachment_methods_enabled_;

Expand Down
13 changes: 0 additions & 13 deletions cobalt/dom/media_settings_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ TEST(MediaSettingsImplTest, SunnyDay) {
ASSERT_TRUE(
impl.Set("MediaElement.TimeupdateEventIntervalInMilliseconds", 100001));
ASSERT_TRUE(impl.Set("MediaElement.PaintingVideoBackgroundToBlack", 1));
ASSERT_TRUE(impl.Set("MediaElement.EnableUsingMediaSourceBufferedRange", 1));
ASSERT_TRUE(
impl.Set("MediaElement.EnableUsingMediaSourceAttachmentMethods", 1));

Expand All @@ -62,8 +61,6 @@ TEST(MediaSettingsImplTest, SunnyDay) {
EXPECT_EQ(impl.GetMediaElementTimeupdateEventIntervalInMilliseconds().value(),
100001);
EXPECT_TRUE(impl.IsPaintingVideoBackgroundToBlack().value());
EXPECT_TRUE(
impl.IsMediaElementUsingMediaSourceBufferedRangeEnabled().value());
EXPECT_TRUE(
impl.IsMediaElementUsingMediaSourceAttachmentMethodsEnabled().value());
}
Expand All @@ -82,8 +79,6 @@ TEST(MediaSettingsImplTest, RainyDay) {
ASSERT_FALSE(
impl.Set("MediaElement.TimeupdateEventIntervalInMilliseconds", 0));
ASSERT_FALSE(impl.Set("MediaElement.PaintingVideoBackgroundToBlack", 2));
ASSERT_FALSE(
impl.Set("MediaElement.EnableUsingMediaSourceBufferedRange", -101));
ASSERT_FALSE(
impl.Set("MediaElement.EnableUsingMediaSourceAttachmentMethods", -101));

Expand All @@ -96,7 +91,6 @@ TEST(MediaSettingsImplTest, RainyDay) {
EXPECT_FALSE(impl.GetMaxSourceBufferAppendSizeInBytes());
EXPECT_FALSE(impl.GetMediaElementTimeupdateEventIntervalInMilliseconds());
EXPECT_FALSE(impl.IsPaintingVideoBackgroundToBlack());
EXPECT_FALSE(impl.IsMediaElementUsingMediaSourceBufferedRangeEnabled());
EXPECT_FALSE(impl.IsMediaElementUsingMediaSourceAttachmentMethodsEnabled());
}

Expand All @@ -114,7 +108,6 @@ TEST(MediaSettingsImplTest, ZeroValuesWork) {
// O is an invalid value for
// "MediaElement.TimeupdateEventIntervalInMilliseconds".
ASSERT_TRUE(impl.Set("MediaElement.PaintingVideoBackgroundToBlack", 0));
ASSERT_TRUE(impl.Set("MediaElement.EnableUsingMediaSourceBufferedRange", 0));
ASSERT_TRUE(
impl.Set("MediaElement.EnableUsingMediaSourceAttachmentMethods", 0));

Expand All @@ -125,8 +118,6 @@ TEST(MediaSettingsImplTest, ZeroValuesWork) {
EXPECT_FALSE(impl.IsCallingEndedWhenClosedEnabled().value());
EXPECT_EQ(impl.GetMaxSizeForImmediateJob().value(), 0);
EXPECT_FALSE(impl.IsPaintingVideoBackgroundToBlack().value());
EXPECT_FALSE(
impl.IsMediaElementUsingMediaSourceBufferedRangeEnabled().value());
EXPECT_FALSE(
impl.IsMediaElementUsingMediaSourceAttachmentMethodsEnabled().value());
}
Expand All @@ -145,7 +136,6 @@ TEST(MediaSettingsImplTest, Updatable) {
ASSERT_TRUE(
impl.Set("MediaElement.TimeupdateEventIntervalInMilliseconds", 1));
ASSERT_TRUE(impl.Set("MediaElement.PaintingVideoBackgroundToBlack", 0));
ASSERT_TRUE(impl.Set("MediaElement.EnableUsingMediaSourceBufferedRange", 0));
ASSERT_TRUE(
impl.Set("MediaElement.EnableUsingMediaSourceAttachmentMethods", 0));

Expand All @@ -160,7 +150,6 @@ TEST(MediaSettingsImplTest, Updatable) {
ASSERT_TRUE(
impl.Set("MediaElement.TimeupdateEventIntervalInMilliseconds", 2));
ASSERT_TRUE(impl.Set("MediaElement.PaintingVideoBackgroundToBlack", 1));
ASSERT_TRUE(impl.Set("MediaElement.EnableUsingMediaSourceBufferedRange", 1));
ASSERT_TRUE(
impl.Set("MediaElement.EnableUsingMediaSourceAttachmentMethods", 1));

Expand All @@ -174,8 +163,6 @@ TEST(MediaSettingsImplTest, Updatable) {
EXPECT_EQ(impl.GetMediaElementTimeupdateEventIntervalInMilliseconds().value(),
2);
EXPECT_TRUE(impl.IsPaintingVideoBackgroundToBlack().value());
EXPECT_TRUE(
impl.IsMediaElementUsingMediaSourceBufferedRangeEnabled().value());
EXPECT_TRUE(
impl.IsMediaElementUsingMediaSourceAttachmentMethodsEnabled().value());
}
Expand Down
23 changes: 1 addition & 22 deletions cobalt/dom/media_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,6 @@ int GetMaxSizeForImmediateJob(web::EnvironmentSettings* settings) {
return max_size;
}

// If this function returns true, MediaSource::GetSeekable() will short-circuit
// getting the buffered range from HTMLMediaElement by directly calling to
// MediaSource::GetBufferedRange(). This reduces potential cross-object,
// cross-thread calls between MediaSource and HTMLMediaElement.
// The default value is false.
bool IsMediaElementUsingMediaSourceBufferedRangeEnabled(
web::EnvironmentSettings* settings) {
return GetMediaSettings(settings)
.IsMediaElementUsingMediaSourceBufferedRangeEnabled()
.value_or(false);
}

} // namespace

MediaSource::MediaSource(script::EnvironmentSettings* settings)
Expand Down Expand Up @@ -566,16 +554,7 @@ scoped_refptr<TimeRanges> MediaSource::GetSeekable() const {

if (source_duration == std::numeric_limits<double>::infinity()) {
scoped_refptr<TimeRanges> buffered = nullptr;
if (IsMediaElementUsingMediaSourceBufferedRangeEnabled(
environment_settings())) {
buffered = GetBufferedRange();
} else {
if (is_using_media_source_attachment_methods_) {
buffered = media_source_attachment_->media_element()->buffered();
} else {
buffered = attached_element_->buffered();
}
}
buffered = GetBufferedRange();

if (live_seekable_range_->length() != 0) {
if (buffered->length() == 0) {
Expand Down

0 comments on commit 13e37ed

Please sign in to comment.