Skip to content

Commit

Permalink
Add simple MediaSourceAttachment class (#3232)
Browse files Browse the repository at this point in the history
Registers `MediaSource` objects to the `MediaSourceRegistry` through a
`MediaSourceAttachment` proxy. The new `MediaSourceAttachment` class is
cross-thread reference counting safe, unlike `MediaSource`, which will
allow for utilizing `MediaSource` objects created on Dedicated Workers.

This change does not add any additional functionality, and is intended
as an incremental update towards enabling the creation of `MediaSource`
objects on Dedicated Worker threads.

See [Chromium's `MediaSourceAttachment` interface][1], and the commit
that [initially introduced it][2].

[1]:
https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/core/html/media/media_source_attachment.h
[2]: https://chromium-review.googlesource.com/c/chromium/src/+/2300804

b/338425449

Change-Id: I016c010c9ef4a0e576bd67ce584d75dbe75986ec
(cherry picked from commit 29e0d65)
  • Loading branch information
at-ninja authored and anonymous1-me committed May 17, 2024
1 parent 886e3d5 commit 29fb0fe
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 14 deletions.
5 changes: 3 additions & 2 deletions cobalt/browser/web_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include "cobalt/dom/keyboard_event.h"
#include "cobalt/dom/keyboard_event_init.h"
#include "cobalt/dom/local_storage_database.h"
#include "cobalt/dom/media_source_attachment.h"
#include "cobalt/dom/mutation_observer_task_manager.h"
#include "cobalt/dom/navigation_type.h"
#include "cobalt/dom/navigator.h"
Expand Down Expand Up @@ -417,7 +418,7 @@ class WebModule::Impl {
dom::MutationObserverTaskManager mutation_observer_task_manager_;

// Object to register and retrieve MediaSource object with a string key.
std::unique_ptr<dom::MediaSource::Registry> media_source_registry_;
std::unique_ptr<dom::MediaSourceAttachment::Registry> media_source_registry_;

// The Window object wraps all DOM-related components.
scoped_refptr<dom::Window> window_;
Expand Down Expand Up @@ -584,7 +585,7 @@ WebModule::Impl::Impl(web::Context* web_context, const ConstructionData& data)
web_context_->name(), data.options.track_event_stats));
DCHECK(web_module_stat_tracker_);

media_source_registry_.reset(new dom::MediaSource::Registry);
media_source_registry_.reset(new dom::MediaSourceAttachment::Registry);

const media::DecoderBufferMemoryInfo* memory_info = nullptr;

Expand Down
1 change: 1 addition & 0 deletions cobalt/dom/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ static_library("dom") {
"media_settings.cc",
"media_source.cc",
"media_source.h",
"media_source_attachment.h",
"memory_info.cc",
"memory_info.h",
"mime_type_array.cc",
Expand Down
4 changes: 2 additions & 2 deletions cobalt/dom/dom_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ class GlobalEnvironment;
class JavaScriptEngine;
} // namespace script
namespace dom {
class MediaSource;
class MediaSourceAttachment;
class Window;

// A package of global state to be passed around to script objects
// that ask for it in their IDL custom attributes.
class DOMSettings : public web::EnvironmentSettings {
public:
typedef web::UrlRegistry<MediaSource> MediaSourceRegistry;
typedef web::UrlRegistry<MediaSourceAttachment> MediaSourceRegistry;
// Hold optional settings for DOMSettings.
struct Options {
// Microphone options.
Expand Down
4 changes: 2 additions & 2 deletions cobalt/dom/html_element_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ namespace cobalt {
namespace dom {

class HTMLElementFactory;
class MediaSource;
class MediaSourceAttachment;

// This class contains references to several objects that are required by HTML
// elements, including HTML element factory, which is used to create new
// HTML elements.
class HTMLElementContext {
public:
typedef web::UrlRegistry<MediaSource> MediaSourceRegistry;
typedef web::UrlRegistry<MediaSourceAttachment> MediaSourceRegistry;

#if !defined(COBALT_BUILD_TYPE_GOLD)
// No-args constructor for tests.
Expand Down
11 changes: 10 additions & 1 deletion cobalt/dom/html_media_element.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "cobalt/dom/html_video_element.h"
#include "cobalt/dom/media_settings.h"
#include "cobalt/dom/media_source.h"
#include "cobalt/dom/media_source_attachment.h"
#include "cobalt/dom/media_source_ready_state.h"
#include "cobalt/loader/fetcher_factory.h"
#include "cobalt/media/url_fetcher_data_source.h"
Expand Down Expand Up @@ -892,8 +893,16 @@ void HTMLMediaElement::LoadResource(const GURL& initial_url,
return;
}

media_source_ =
scoped_refptr<MediaSourceAttachment> attachment =
html_element_context()->media_source_registry()->Retrieve(url.spec());

if (!attachment) {
NoneSupported("Media source is NULL.");
return;
}

media_source_ = attachment->media_source();

if (!media_source_) {
NoneSupported("Media source is NULL.");
return;
Expand Down
2 changes: 0 additions & 2 deletions cobalt/dom/media_source.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
#include "cobalt/script/environment_settings.h"
#include "cobalt/script/exception_state.h"
#include "cobalt/web/event_target.h"
#include "cobalt/web/url_registry.h"
#include "media/filters/chunk_demuxer.h"

namespace cobalt {
Expand All @@ -79,7 +78,6 @@ namespace dom {
class MediaSource : public web::EventTarget {
public:
typedef ::media::ChunkDemuxer ChunkDemuxer;
typedef web::UrlRegistry<MediaSource> Registry;

// Custom, not in any spec.
//
Expand Down
56 changes: 56 additions & 0 deletions cobalt/dom/media_source_attachment.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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 COBALT_DOM_MEDIA_SOURCE_ATTACHMENT_H_
#define COBALT_DOM_MEDIA_SOURCE_ATTACHMENT_H_

#include "base/memory/ref_counted.h"
#include "cobalt/dom/media_source.h"
#include "cobalt/script/tracer.h"
#include "cobalt/web/url_registry.h"

namespace cobalt {
namespace dom {

// Interface for potential cross-context management of MediaSource objects.
// Used with the MediaSourceRegistry for attaching MediaSources to media
// elements.
class MediaSourceAttachment
: public base::RefCountedThreadSafe<MediaSourceAttachment>,
public script::Traceable {
public:
typedef web::UrlRegistry<MediaSourceAttachment> Registry;

explicit MediaSourceAttachment(scoped_refptr<MediaSource> media_source)
: media_source_(media_source) {}

scoped_refptr<MediaSource> media_source() const { return media_source_; }

void TraceMembers(script::Tracer* tracer) override {
tracer->Trace(media_source_);
}

private:
friend class base::RefCountedThreadSafe<MediaSourceAttachment>;
~MediaSourceAttachment() = default;

scoped_refptr<MediaSource> media_source_;

DISALLOW_COPY_AND_ASSIGN(MediaSourceAttachment);
};

} // namespace dom
} // namespace cobalt

#endif // COBALT_DOM_MEDIA_SOURCE_ATTACHMENT_H_
7 changes: 6 additions & 1 deletion cobalt/dom/url_media_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "cobalt/base/polymorphic_downcast.h"
#include "cobalt/dom/dom_settings.h"
#include "cobalt/dom/media_source.h"
#include "cobalt/dom/media_source_attachment.h"
#include "cobalt/web/context.h"
#include "cobalt/web/environment_settings.h"
#include "cobalt/web/url.h"
Expand All @@ -34,7 +35,11 @@ void RegisterMediaSourceObjectURL(
base::polymorphic_downcast<dom::DOMSettings*>(environment_settings);
DCHECK(dom_settings);
DCHECK(dom_settings->media_source_registry());
dom_settings->media_source_registry()->Register(blob_url, media_source);

scoped_refptr<MediaSourceAttachment> attachment =
base::MakeRefCounted<MediaSourceAttachment>(media_source);

dom_settings->media_source_registry()->Register(blob_url, attachment);
}

// extern
Expand Down
4 changes: 2 additions & 2 deletions cobalt/dom/window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include "cobalt/dom/input_event.h"
#include "cobalt/dom/keyboard_event.h"
#include "cobalt/dom/location.h"
#include "cobalt/dom/media_source.h"
#include "cobalt/dom/media_source_attachment.h"
#include "cobalt/dom/mouse_event.h"
#include "cobalt/dom/mutation_observer_task_manager.h"
#include "cobalt/dom/navigator.h"
Expand Down Expand Up @@ -102,7 +102,7 @@ Window::Window(
script::ExecutionState* execution_state,
script::ScriptRunner* script_runner,
script::ScriptValueFactory* script_value_factory,
MediaSource::Registry* media_source_registry,
MediaSourceAttachment::Registry* media_source_registry,
DomStatTracker* dom_stat_tracker, const std::string& font_language_script,
const base::Callback<void(const GURL&)> navigation_callback,
const loader::Decoder::OnCompleteFunction& load_complete_callback,
Expand Down
4 changes: 2 additions & 2 deletions cobalt/dom/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class Element;
class History;
class LocalStorageDatabase;
class Location;
class MediaSource;
class MediaSourceAttachment;
class Navigator;
class OnScreenKeyboard;
class Performance;
Expand All @@ -119,7 +119,7 @@ class Window : public web::WindowOrWorkerGlobalScope,
// base::TimeDelta parameter will contain the document's timeline time when
// close() was called.
typedef base::Callback<void(base::TimeDelta)> CloseCallback;
typedef web::UrlRegistry<MediaSource> MediaSourceRegistry;
typedef web::UrlRegistry<MediaSourceAttachment> MediaSourceRegistry;
typedef base::Callback<void(const std::string&,
const base::Optional<std::string>&)>
CacheCallback;
Expand Down

0 comments on commit 29fb0fe

Please sign in to comment.