forked from youtube/cobalt
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create stub for cross-thread MediaSource attachments (youtube#4169)
Enables MediaSource, SourceBuffer, and SourceBufferList objects to be created on Dedicated Workers. Creation of these objects is not gated behind any H5VCC flags, but they will be non-transferable without usage of H5VCC flags. The initial implementation of CrossThreadMediaSourceAttachment is very bare bones, and does not allow for MediaElements to interact with the attached MediaSources yet. However, this lays the groundwork for supporting creating the relevant MediaSource objects on the worker thread, and the transfer process to get the reference back to the main thread. Usage of creating MediaSource URLs is gated through the `MediaSource.EnableInWorkers` H5VCC flag, in addition to existing flags `MediaElement.EnableUsingMediaSourceBufferedRange` and `MediaElement.EnableUsingMediaSourceAttachmentMethods`. If and only if all three flags are enabled, MediaSources created on Dedicated Workers and passed to `URL.createObjectURL` will return a non-empty string that can be sent to the main thread, and used to 'access' the MediaSourceAttachment. In addition, if and only if all three H5VCC flags are enabled, [`MediaSource.canConstructInDedicatedWorker`](https://developer.mozilla.org/en-US/docs/Web/API/MediaSource/canConstructInDedicatedWorker_static) will return true. This is based on the following Chromium commit: • https://chromium-review.googlesource.com/c/chromium/src/+/2407075 b/338425449
- Loading branch information
Showing
20 changed files
with
571 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// 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. | ||
// | ||
// Copyright 2020 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "cobalt/dom/cross_thread_media_source_attachment.h" | ||
|
||
#include "base/task/sequenced_task_runner.h" | ||
#include "cobalt/dom/media_source.h" | ||
#include "cobalt/dom/media_source_attachment.h" | ||
#include "cobalt/dom/media_source_ready_state.h" | ||
#include "cobalt/dom/time_ranges.h" | ||
#include "cobalt/script/tracer.h" | ||
#include "cobalt/web/url_registry.h" | ||
#include "media/filters/chunk_demuxer.h" | ||
|
||
|
||
namespace cobalt { | ||
namespace dom { | ||
|
||
CrossThreadMediaSourceAttachment::CrossThreadMediaSourceAttachment( | ||
scoped_refptr<MediaSource> media_source) | ||
: media_source_(media_source), | ||
task_runner_(base::SequencedTaskRunner::GetCurrentDefault()), | ||
recent_element_time_(0.0), | ||
element_has_error_(false) {} | ||
|
||
void CrossThreadMediaSourceAttachment::TraceMembers(script::Tracer* tracer) { | ||
DCHECK_NE(task_runner_, base::SequencedTaskRunner::GetCurrentDefault()); | ||
|
||
tracer->Trace(attached_element_); | ||
tracer->Trace(media_source_); | ||
} | ||
|
||
bool CrossThreadMediaSourceAttachment::StartAttachingToMediaElement( | ||
HTMLMediaElement* media_element) { | ||
DCHECK_NE(task_runner_, base::SequencedTaskRunner::GetCurrentDefault()); | ||
NOTIMPLEMENTED(); | ||
return false; | ||
} | ||
|
||
void CrossThreadMediaSourceAttachment::CompleteAttachingToMediaElement( | ||
::media::ChunkDemuxer* chunk_demuxer) { | ||
DCHECK_NE(task_runner_, base::SequencedTaskRunner::GetCurrentDefault()); | ||
NOTIMPLEMENTED(); | ||
} | ||
|
||
void CrossThreadMediaSourceAttachment::Close() { | ||
DCHECK_NE(task_runner_, base::SequencedTaskRunner::GetCurrentDefault()); | ||
NOTIMPLEMENTED(); | ||
} | ||
|
||
scoped_refptr<TimeRanges> CrossThreadMediaSourceAttachment::GetBufferedRange() | ||
const { | ||
DCHECK_NE(task_runner_, base::SequencedTaskRunner::GetCurrentDefault()); | ||
NOTIMPLEMENTED(); | ||
return nullptr; | ||
} | ||
|
||
MediaSourceReadyState CrossThreadMediaSourceAttachment::GetReadyState() const { | ||
DCHECK_NE(task_runner_, base::SequencedTaskRunner::GetCurrentDefault()); | ||
NOTIMPLEMENTED(); | ||
return kMediaSourceReadyStateClosed; | ||
} | ||
|
||
void CrossThreadMediaSourceAttachment::NotifyDurationChanged(double duration) { | ||
DCHECK_NE(task_runner_, base::SequencedTaskRunner::GetCurrentDefault()); | ||
NOTIMPLEMENTED(); | ||
} | ||
|
||
bool CrossThreadMediaSourceAttachment::HasMaxVideoCapabilities() const { | ||
DCHECK_NE(task_runner_, base::SequencedTaskRunner::GetCurrentDefault()); | ||
NOTIMPLEMENTED(); | ||
return false; | ||
} | ||
|
||
double CrossThreadMediaSourceAttachment::GetRecentMediaTime() { | ||
DCHECK_NE(task_runner_, base::SequencedTaskRunner::GetCurrentDefault()); | ||
NOTIMPLEMENTED(); | ||
return 0; | ||
} | ||
|
||
bool CrossThreadMediaSourceAttachment::GetElementError() { | ||
DCHECK_NE(task_runner_, base::SequencedTaskRunner::GetCurrentDefault()); | ||
NOTIMPLEMENTED(); | ||
return false; | ||
} | ||
|
||
scoped_refptr<AudioTrackList> | ||
CrossThreadMediaSourceAttachment::CreateAudioTrackList( | ||
script::EnvironmentSettings* settings) { | ||
DCHECK_NE(task_runner_, base::SequencedTaskRunner::GetCurrentDefault()); | ||
NOTIMPLEMENTED(); | ||
return nullptr; | ||
} | ||
|
||
scoped_refptr<VideoTrackList> | ||
CrossThreadMediaSourceAttachment::CreateVideoTrackList( | ||
script::EnvironmentSettings* settings) { | ||
DCHECK_NE(task_runner_, base::SequencedTaskRunner::GetCurrentDefault()); | ||
NOTIMPLEMENTED(); | ||
return nullptr; | ||
} | ||
|
||
void CrossThreadMediaSourceAttachment::OnElementTimeUpdate(double time) { | ||
NOTIMPLEMENTED(); | ||
recent_element_time_ = time; | ||
} | ||
|
||
void CrossThreadMediaSourceAttachment::OnElementError() { | ||
DCHECK(!element_has_error_) | ||
<< "At most one transition to element error per attachment is expected"; | ||
NOTIMPLEMENTED(); | ||
element_has_error_ = true; | ||
} | ||
|
||
} // namespace dom | ||
} // namespace cobalt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// 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. | ||
// | ||
// Copyright 2020 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef COBALT_DOM_CROSS_THREAD_MEDIA_SOURCE_ATTACHMENT_H_ | ||
#define COBALT_DOM_CROSS_THREAD_MEDIA_SOURCE_ATTACHMENT_H_ | ||
|
||
#include "base/memory/weak_ptr.h" | ||
#include "base/task/sequenced_task_runner.h" | ||
#include "cobalt/dom/audio_track_list.h" | ||
#include "cobalt/dom/media_source.h" | ||
#include "cobalt/dom/media_source_attachment_supplement.h" | ||
#include "cobalt/dom/media_source_ready_state.h" | ||
#include "cobalt/dom/time_ranges.h" | ||
#include "cobalt/dom/video_track_list.h" | ||
#include "cobalt/script/environment_settings.h" | ||
#include "cobalt/script/tracer.h" | ||
#include "cobalt/web/url_registry.h" | ||
#include "media/filters/chunk_demuxer.h" | ||
|
||
namespace cobalt { | ||
namespace dom { | ||
|
||
// MediaSourceAttachment that supports operations between a HTMLMediaElement on | ||
// the main browser thread and a MediaSource on a DedicatedWorker. | ||
class CrossThreadMediaSourceAttachment | ||
: public MediaSourceAttachmentSupplement { | ||
public: | ||
explicit CrossThreadMediaSourceAttachment( | ||
scoped_refptr<MediaSource> media_source); | ||
|
||
// Traceable | ||
void TraceMembers(script::Tracer* tracer) override; | ||
|
||
// MediaSourceAttachment | ||
bool StartAttachingToMediaElement(HTMLMediaElement* media_element) override; | ||
void CompleteAttachingToMediaElement( | ||
::media::ChunkDemuxer* chunk_demuxer) override; | ||
void Close() override; | ||
scoped_refptr<TimeRanges> GetBufferedRange() const override; | ||
MediaSourceReadyState GetReadyState() const override; | ||
|
||
void OnElementTimeUpdate(double time) override; | ||
void OnElementError() override; | ||
|
||
// MediaSourceAttachmentSupplement | ||
void NotifyDurationChanged(double duration) override; | ||
bool HasMaxVideoCapabilities() const override; | ||
double GetRecentMediaTime() override; | ||
bool GetElementError() override; | ||
scoped_refptr<AudioTrackList> CreateAudioTrackList( | ||
script::EnvironmentSettings* settings) override; | ||
scoped_refptr<VideoTrackList> CreateVideoTrackList( | ||
script::EnvironmentSettings* settings) override; | ||
|
||
// TODO(338425449): Remove methods after feature rollout. | ||
scoped_refptr<MediaSource> media_source() const override { return nullptr; } | ||
base::WeakPtr<HTMLMediaElement> media_element() const override { | ||
return nullptr; | ||
} | ||
|
||
private: | ||
~CrossThreadMediaSourceAttachment() = default; | ||
|
||
// Reference to the registered MediaSource. | ||
scoped_refptr<MediaSource> media_source_; | ||
|
||
// Reference to the HTMLMediaElement the associated MediaSource is attached | ||
// to. Only set after StartAttachingToMediaElement is called. | ||
base::WeakPtr<HTMLMediaElement> attached_element_ = nullptr; | ||
|
||
// Used to ensure all calls are made on the thread that created this object. | ||
base::SequencedTaskRunner* task_runner_; | ||
|
||
double recent_element_time_; // See OnElementTimeUpdate(). | ||
bool element_has_error_; // See OnElementError(). | ||
|
||
DISALLOW_COPY_AND_ASSIGN(CrossThreadMediaSourceAttachment); | ||
}; | ||
|
||
} // namespace dom | ||
} // namespace cobalt | ||
|
||
#endif // COBALT_DOM_CROSS_THREAD_MEDIA_SOURCE_ATTACHMENT_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.