Skip to content

Commit

Permalink
(WIP) Allow Chrome Media m96 and m118 side by side
Browse files Browse the repository at this point in the history
b/358391765
  • Loading branch information
xiaomings committed Aug 8, 2024
1 parent 27db244 commit 3f1047f
Show file tree
Hide file tree
Showing 3,541 changed files with 642,366 additions and 810 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
2 changes: 1 addition & 1 deletion cobalt/dom/html_media_element.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1741,7 +1741,7 @@ void HTMLMediaElement::PlaybackStateChanged() {

float HTMLMediaElement::Volume() const { return muted_ ? 0 : volume(NULL); }

void HTMLMediaElement::SourceOpened(ChunkDemuxer* chunk_demuxer) {
void HTMLMediaElement::SourceOpened(media::ChunkDemuxerHolder* chunk_demuxer) {
TRACE_EVENT0("cobalt::dom", "HTMLMediaElement::SourceOpened()");
DCHECK(chunk_demuxer);
BeginProcessingMediaPlayerCallback();
Expand Down
3 changes: 1 addition & 2 deletions cobalt/dom/html_media_element.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class MediaSourceAttachment;
class HTMLMediaElement : public HTMLElement,
private cobalt::media::WebMediaPlayerClient {
public:
typedef ::media::ChunkDemuxer ChunkDemuxer;
typedef ::cobalt::media::WebMediaPlayer WebMediaPlayer;
typedef ::cobalt::media::WebMediaPlayerClient WebMediaPlayerClient;

Expand Down Expand Up @@ -242,7 +241,7 @@ class HTMLMediaElement : public HTMLElement,
void ContentSizeChanged() override;
void PlaybackStateChanged() override;
float Volume() const override;
void SourceOpened(ChunkDemuxer* chunk_demuxer) override;
void SourceOpened(media::ChunkDemuxerHolder* chunk_demuxer) override;
std::string SourceURL() const override;
std::string MaxVideoCapabilities() const override;
int MaxVideoInputSize() const override;
Expand Down
52 changes: 24 additions & 28 deletions cobalt/dom/media_source.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Modifications Copyright 2017 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 (C) 2013 Google Inc. All Rights Reserved.
*
Expand Down Expand Up @@ -28,20 +42,6 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

// Modifications Copyright 2017 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 "cobalt/dom/media_source.h"

#include <algorithm>
Expand Down Expand Up @@ -72,11 +72,6 @@ namespace dom {

namespace {

using ::media::CHUNK_DEMUXER_ERROR_EOS_STATUS_DECODE_ERROR;
using ::media::CHUNK_DEMUXER_ERROR_EOS_STATUS_NETWORK_ERROR;
using ::media::PIPELINE_OK;
using ::media::PipelineStatus;

const MediaSettings& GetMediaSettings(web::EnvironmentSettings* settings) {
DCHECK(settings);
DCHECK(settings->context());
Expand Down Expand Up @@ -201,7 +196,7 @@ double MediaSource::duration(script::ExceptionState* exception_state) const {
return std::numeric_limits<float>::quiet_NaN();
}

DCHECK(chunk_demuxer_);
DCHECK(chunk_demuxer_->valid());
return chunk_demuxer_->GetDuration();
}

Expand Down Expand Up @@ -290,17 +285,17 @@ scoped_refptr<SourceBuffer> MediaSource::AddSourceBuffer(

std::string guid = base::GenerateGUID();
scoped_refptr<SourceBuffer> source_buffer;
ChunkDemuxer::Status status = chunk_demuxer_->AddId(guid, type);
media::ChunkDemuxerHolder::Status status = chunk_demuxer_->AddId(guid, type);
switch (status) {
case ChunkDemuxer::kOk:
case media::ChunkDemuxerHolder::kOk:
source_buffer =
new SourceBuffer(settings, guid, this, chunk_demuxer_, &event_queue_);
break;
case ChunkDemuxer::kNotSupported:
case media::ChunkDemuxerHolder::kNotSupported:
web::DOMException::Raise(web::DOMException::kNotSupportedErr,
exception_state);
return NULL;
case ChunkDemuxer::kReachedIdLimit:
case media::ChunkDemuxerHolder::kReachedIdLimit:
web::DOMException::Raise(web::DOMException::kQuotaExceededErr,
exception_state);
return NULL;
Expand Down Expand Up @@ -355,12 +350,12 @@ void MediaSource::EndOfStreamAlgorithm(MediaSourceEndOfStreamError error) {
SetReadyState(kMediaSourceReadyStateEnded);
}

PipelineStatus pipeline_status = PIPELINE_OK;
::media::PipelineStatusCodes pipeline_status = ::media::PIPELINE_OK;

if (error == kMediaSourceEndOfStreamErrorNetwork) {
pipeline_status = CHUNK_DEMUXER_ERROR_EOS_STATUS_NETWORK_ERROR;
pipeline_status = ::media::CHUNK_DEMUXER_ERROR_EOS_STATUS_NETWORK_ERROR;
} else if (error == kMediaSourceEndOfStreamErrorDecode) {
pipeline_status = CHUNK_DEMUXER_ERROR_EOS_STATUS_DECODE_ERROR;
pipeline_status = ::media::CHUNK_DEMUXER_ERROR_EOS_STATUS_DECODE_ERROR;
}
chunk_demuxer_->MarkEndOfStream(pipeline_status);
}
Expand Down Expand Up @@ -504,7 +499,8 @@ bool MediaSource::StartAttachingToMediaElement(
return true;
}

void MediaSource::CompleteAttachingToMediaElement(ChunkDemuxer* chunk_demuxer) {
void MediaSource::CompleteAttachingToMediaElement(
media::ChunkDemuxerHolder* chunk_demuxer) {
DCHECK(chunk_demuxer);
DCHECK(!chunk_demuxer_);
if (is_using_media_source_attachment_methods_) {
Expand Down
37 changes: 18 additions & 19 deletions cobalt/dom/media_source.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Modifications Copyright 2017 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 (C) 2013 Google Inc. All Rights Reserved.
*
Expand Down Expand Up @@ -28,20 +42,6 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

// Modifications Copyright 2017 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_H_
#define COBALT_DOM_MEDIA_SOURCE_H_

Expand All @@ -65,10 +65,10 @@
#include "cobalt/dom/source_buffer_list.h"
#include "cobalt/dom/time_ranges.h"
#include "cobalt/dom/video_track.h"
#include "cobalt/media/base/chunk_demuxer_holder.h"
#include "cobalt/script/environment_settings.h"
#include "cobalt/script/exception_state.h"
#include "cobalt/web/event_target.h"
#include "media/filters/chunk_demuxer.h"

namespace cobalt {
namespace dom {
Expand All @@ -78,8 +78,6 @@ namespace dom {
// https://www.w3.org/TR/2016/CR-media-source-20160705/#idl-def-MediaSource
class MediaSource : public web::EventTarget {
public:
typedef ::media::ChunkDemuxer ChunkDemuxer;

// Custom, not in any spec.
//
explicit MediaSource(script::EnvironmentSettings* settings);
Expand Down Expand Up @@ -116,7 +114,8 @@ class MediaSource : public web::EventTarget {
bool StartAttachingToMediaElement(HTMLMediaElement* media_element);
bool StartAttachingToMediaElement(
MediaSourceAttachmentSupplement* media_source_attachment);
void CompleteAttachingToMediaElement(ChunkDemuxer* chunk_demuxer);
void CompleteAttachingToMediaElement(
media::ChunkDemuxerHolder* chunk_demuxer);
void Close();
bool IsClosed() const;
scoped_refptr<TimeRanges> GetBufferedRange() const;
Expand Down Expand Up @@ -166,7 +165,7 @@ class MediaSource : public web::EventTarget {
offload_algorithm_runner_;
std::unique_ptr<base::Thread> algorithm_process_thread_;

ChunkDemuxer* chunk_demuxer_;
media::ChunkDemuxerHolder* chunk_demuxer_;
MediaSourceReadyState ready_state_;
EventQueue event_queue_;
// TODO(b/338425449): Remove direct references to HTMLMediaElement.
Expand Down
3 changes: 2 additions & 1 deletion cobalt/dom/media_source_attachment.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "cobalt/dom/html_media_element.h"
#include "cobalt/dom/media_source_ready_state.h"
#include "cobalt/dom/time_ranges.h"
#include "cobalt/media/base/chunk_demuxer_holder.h"
#include "cobalt/script/tracer.h"
#include "cobalt/web/url_registry.h"
#include "media/filters/chunk_demuxer.h"
Expand Down Expand Up @@ -59,7 +60,7 @@ class MediaSourceAttachment
virtual bool StartAttachingToMediaElement(
HTMLMediaElement* media_element) = 0;
virtual void CompleteAttachingToMediaElement(
::media::ChunkDemuxer* chunk_demuxer) = 0;
media::ChunkDemuxerHolder* chunk_demuxer) = 0;

virtual void Close() = 0;

Expand Down
2 changes: 1 addition & 1 deletion cobalt/dom/same_thread_media_source_attachment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ bool SameThreadMediaSourceAttachment::StartAttachingToMediaElement(
}

void SameThreadMediaSourceAttachment::CompleteAttachingToMediaElement(
::media::ChunkDemuxer* chunk_demuxer) {
media::ChunkDemuxerHolder* chunk_demuxer) {
DCHECK_EQ(task_runner_, base::SequencedTaskRunner::GetCurrentDefault());

media_source_->CompleteAttachingToMediaElement(chunk_demuxer);
Expand Down
4 changes: 2 additions & 2 deletions cobalt/dom/same_thread_media_source_attachment.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
#include "cobalt/dom/media_source_ready_state.h"
#include "cobalt/dom/time_ranges.h"
#include "cobalt/dom/video_track_list.h"
#include "cobalt/media/base/chunk_demuxer_holder.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 {
Expand All @@ -46,7 +46,7 @@ class SameThreadMediaSourceAttachment : public MediaSourceAttachmentSupplement {
// MediaSourceAttachment
bool StartAttachingToMediaElement(HTMLMediaElement* media_element) override;
void CompleteAttachingToMediaElement(
::media::ChunkDemuxer* chunk_demuxer) override;
media::ChunkDemuxerHolder* chunk_demuxer) override;
void Close() override;
scoped_refptr<TimeRanges> GetBufferedRange() const override;
MediaSourceReadyState GetReadyState() const override;
Expand Down
64 changes: 41 additions & 23 deletions cobalt/dom/source_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,24 +153,44 @@ void SourceBuffer::OnInitSegmentReceivedHelper::Detach() {
source_buffer_ = nullptr;
}

void SourceBuffer::OnInitSegmentReceivedHelper::TryToRunOnInitSegmentReceived(
std::unique_ptr<MediaTracks> tracks) {
void SourceBuffer::OnInitSegmentReceivedHelper::
TryToRunOnInitSegmentReceivedM96(
std::unique_ptr<::media_m96::MediaTracks> tracks) {
if (!task_runner_->RunsTasksInCurrentSequence()) {
task_runner_->PostTask(
FROM_HERE,
base::Bind(&OnInitSegmentReceivedHelper::TryToRunOnInitSegmentReceived,
this, base::Passed(&tracks)));
base::Bind(
&OnInitSegmentReceivedHelper::TryToRunOnInitSegmentReceivedM96,
this, base::Passed(&tracks)));
return;
}

if (source_buffer_) {
source_buffer_->OnInitSegmentReceived(std::move(tracks));
source_buffer_->OnInitSegmentReceived();
}
}

void SourceBuffer::OnInitSegmentReceivedHelper::
TryToRunOnInitSegmentReceivedM114(
std::unique_ptr<::media_m114::MediaTracks> tracks) {
if (!task_runner_->RunsTasksInCurrentSequence()) {
task_runner_->PostTask(
FROM_HERE,
base::Bind(
&OnInitSegmentReceivedHelper::TryToRunOnInitSegmentReceivedM114,
this, base::Passed(&tracks)));
return;
}

if (source_buffer_) {
source_buffer_->OnInitSegmentReceived();
}
}

SourceBuffer::SourceBuffer(script::EnvironmentSettings* settings,
const std::string& id, MediaSource* media_source,
ChunkDemuxer* chunk_demuxer, EventQueue* event_queue)
media::ChunkDemuxerHolder* chunk_demuxer,
EventQueue* event_queue)
: web::EventTarget(settings),
on_init_segment_received_helper_(new OnInitSegmentReceivedHelper(this)),
id_(id),
Expand Down Expand Up @@ -204,15 +224,19 @@ SourceBuffer::SourceBuffer(script::EnvironmentSettings* settings,
LOG(INFO) << "Evict extra in bytes is set to " << evict_extra_in_bytes_;
LOG(INFO) << "Max append size in bytes is set to " << max_append_buffer_size_;

chunk_demuxer_->SetTracksWatcher(
id_,
base::Bind(&OnInitSegmentReceivedHelper::TryToRunOnInitSegmentReceived,
if (chunk_demuxer_->is_m114()) {
chunk_demuxer_->As<::media_m114::ChunkDemuxer*>()->SetTracksWatcher(
id_,
base::Bind(
&OnInitSegmentReceivedHelper::TryToRunOnInitSegmentReceivedM114,
on_init_segment_received_helper_));
} else {
chunk_demuxer_->As<::media_m96::ChunkDemuxer*>()->SetTracksWatcher(
id_, base::Bind(
&OnInitSegmentReceivedHelper::TryToRunOnInitSegmentReceivedM96,
on_init_segment_received_helper_));
chunk_demuxer_->SetParseWarningCallback(
id, base::BindRepeating([](::media::SourceBufferParseWarning warning) {
LOG(WARNING) << "Encountered SourceBufferParseWarning "
<< static_cast<int>(warning);
}));
}
chunk_demuxer_->SetParseWarningCallback(id);
}

void SourceBuffer::set_mode(SourceBufferAppendMode mode,
Expand Down Expand Up @@ -249,13 +273,7 @@ scoped_refptr<TimeRanges> SourceBuffer::buffered(
return NULL;
}

scoped_refptr<TimeRanges> time_ranges = new TimeRanges;
::media::Ranges<base::TimeDelta> ranges =
chunk_demuxer_->GetBufferedRanges(id_);
for (size_t i = 0; i < ranges.size(); i++) {
time_ranges->Add(ranges.start(i).InSecondsF(), ranges.end(i).InSecondsF());
}
return time_ranges;
return chunk_demuxer_->GetBufferedRanges<TimeRanges>(id_);
}

double SourceBuffer::timestamp_offset(
Expand Down Expand Up @@ -514,7 +532,7 @@ void SourceBuffer::OnRemovedFromMediaSource() {
metrics_.PrintCurrentMetricsAndUpdateAccumulatedMetrics();

chunk_demuxer_->RemoveId(id_);
if (chunk_demuxer_->GetAllStreams().empty()) {
if (chunk_demuxer_->HasAnyStreams()) {
metrics_.PrintAccumulatedMetrics();
}

Expand Down Expand Up @@ -577,7 +595,7 @@ void SourceBuffer::set_memory_limit(size_t memory_limit,
chunk_demuxer_->SetSourceBufferStreamMemoryLimit(id_, memory_limit);
}

void SourceBuffer::OnInitSegmentReceived(std::unique_ptr<MediaTracks> tracks) {
void SourceBuffer::OnInitSegmentReceived() {
if (!first_initialization_segment_received_) {
media_source_->SetSourceBufferActive(this, true);
first_initialization_segment_received_ = true;
Expand Down
Loading

0 comments on commit 3f1047f

Please sign in to comment.