1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
// 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 "third_party/blink/renderer/modules/mediasource/same_thread_media_source_attachment.h"
#include "base/memory/scoped_refptr.h"
#include "base/types/pass_key.h"
#include "third_party/blink/renderer/core/html/media/html_media_element.h"
#include "third_party/blink/renderer/modules/mediasource/attachment_creation_pass_key_provider.h"
#include "third_party/blink/renderer/modules/mediasource/media_source.h"
#include "third_party/blink/renderer/modules/mediasource/same_thread_media_source_tracer.h"
namespace {
// Downcasts |tracer| to the expected same-thread attachment's tracer type.
// Includes a debug-mode check that the tracer matches the expected attachment
// semantic.
blink::SameThreadMediaSourceTracer* GetTracerImpl(
blink::MediaSourceTracer* tracer) {
DCHECK(!tracer || !tracer->IsCrossThreadForDebugging());
return static_cast<blink::SameThreadMediaSourceTracer*>(tracer);
}
blink::MediaSource* GetMediaSource(blink::MediaSourceTracer* tracer) {
return GetTracerImpl(tracer)->GetMediaSource();
}
blink::HTMLMediaElement* GetMediaElement(blink::MediaSourceTracer* tracer) {
return GetTracerImpl(tracer)->GetMediaElement();
}
} // namespace
namespace blink {
SameThreadMediaSourceAttachment::SameThreadMediaSourceAttachment(
MediaSource* media_source,
AttachmentCreationPassKeyProvider::PassKey /* passkey */)
: registered_media_source_(media_source),
recent_element_time_(0.0),
element_has_error_(false),
element_context_destroyed_(false),
media_source_context_destroyed_(false) {
// This kind of attachment only operates on the main thread.
DCHECK(IsMainThread());
DVLOG(1) << __func__ << " this=" << this << " media_source=" << media_source;
// Verify that at construction time, refcounting of this object begins at
// precisely 1.
DCHECK(HasOneRef());
}
SameThreadMediaSourceAttachment::~SameThreadMediaSourceAttachment() {
DVLOG(1) << __func__ << " this=" << this;
}
void SameThreadMediaSourceAttachment::NotifyDurationChanged(
MediaSourceTracer* tracer,
double duration) {
DVLOG(1) << __func__ << " this=" << this;
VerifyCalledWhileContextsAliveForDebugging();
HTMLMediaElement* element = GetMediaElement(tracer);
bool request_seek = element->currentTime() > duration;
element->DurationChanged(duration, request_seek);
}
base::TimeDelta SameThreadMediaSourceAttachment::GetRecentMediaTime(
MediaSourceTracer* tracer) {
DVLOG(1) << __func__ << " this=" << this;
VerifyCalledWhileContextsAliveForDebugging();
HTMLMediaElement* element = GetMediaElement(tracer);
base::TimeDelta result = base::Seconds(element->currentTime());
DVLOG(2) << __func__ << " this=" << this
<< " -> recent time=" << recent_element_time_
<< ", actual currentTime=" << result;
return result;
}
bool SameThreadMediaSourceAttachment::GetElementError(
MediaSourceTracer* tracer) {
DVLOG(1) << __func__ << " this=" << this;
VerifyCalledWhileContextsAliveForDebugging();
HTMLMediaElement* element = GetMediaElement(tracer);
bool current_element_error_state = !!element->error();
DCHECK_EQ(current_element_error_state, element_has_error_);
return current_element_error_state;
}
AudioTrackList* SameThreadMediaSourceAttachment::CreateAudioTrackList(
MediaSourceTracer* tracer) {
DVLOG(1) << __func__ << " this=" << this;
VerifyCalledWhileContextsAliveForDebugging();
HTMLMediaElement* element = GetMediaElement(tracer);
return MakeGarbageCollected<AudioTrackList>(*element);
}
VideoTrackList* SameThreadMediaSourceAttachment::CreateVideoTrackList(
MediaSourceTracer* tracer) {
DVLOG(1) << __func__ << " this=" << this;
VerifyCalledWhileContextsAliveForDebugging();
HTMLMediaElement* element = GetMediaElement(tracer);
return MakeGarbageCollected<VideoTrackList>(*element);
}
void SameThreadMediaSourceAttachment::AddAudioTrackToMediaElement(
MediaSourceTracer* tracer,
AudioTrack* track) {
DVLOG(3) << __func__ << " this=" << this;
VerifyCalledWhileContextsAliveForDebugging();
HTMLMediaElement* element = GetMediaElement(tracer);
element->audioTracks().Add(track);
}
void SameThreadMediaSourceAttachment::AddVideoTrackToMediaElement(
MediaSourceTracer* tracer,
VideoTrack* track) {
DVLOG(3) << __func__ << " this=" << this;
VerifyCalledWhileContextsAliveForDebugging();
HTMLMediaElement* element = GetMediaElement(tracer);
element->videoTracks().Add(track);
}
void SameThreadMediaSourceAttachment::RemoveAudioTracksFromMediaElement(
MediaSourceTracer* tracer,
Vector<String> audio_ids,
bool enqueue_change_event) {
DVLOG(3) << __func__ << " this=" << this << ", ids size=" << audio_ids.size()
<< ", enqueue_change_event=" << enqueue_change_event;
if (element_context_destroyed_ || media_source_context_destroyed_) {
DVLOG(3) << __func__ << " this=" << this
<< " -> skipping due to context(s) destroyed";
return;
}
HTMLMediaElement* element = GetMediaElement(tracer);
for (auto& audio_id : audio_ids) {
if (element->audioTracks().getTrackById(audio_id)) {
element->audioTracks().Remove(audio_id);
} else {
// This case can happen on element noneSupported() after MSE had added
// some track(s). See https://crbug.com/1204656.
DVLOG(3) << __func__ << " this=" << this
<< ", skipping removal of missing audio track id " << audio_id;
}
}
if (enqueue_change_event) {
Event* event = Event::Create(event_type_names::kChange);
event->SetTarget(&element->audioTracks());
element->ScheduleEvent(event);
}
}
void SameThreadMediaSourceAttachment::RemoveVideoTracksFromMediaElement(
MediaSourceTracer* tracer,
Vector<String> video_ids,
bool enqueue_change_event) {
DVLOG(3) << __func__ << " this=" << this << ", ids size=" << video_ids.size()
<< ", enqueue_change_event=" << enqueue_change_event;
if (element_context_destroyed_ || media_source_context_destroyed_) {
DVLOG(3) << __func__ << " this=" << this
<< " -> skipping due to context(s) destroyed";
return;
}
HTMLMediaElement* element = GetMediaElement(tracer);
for (auto& video_id : video_ids) {
if (element->videoTracks().getTrackById(video_id)) {
element->videoTracks().Remove(video_id);
} else {
// This case can happen on element noneSupported() after MSE had added
// some track(s). See https://crbug.com/1204656.
DVLOG(3) << __func__ << " this=" << this
<< ", skipping removal of missing video track id " << video_id;
}
}
if (enqueue_change_event) {
Event* event = Event::Create(event_type_names::kChange);
event->SetTarget(&element->videoTracks());
element->ScheduleEvent(event);
}
}
void SameThreadMediaSourceAttachment::OnMediaSourceContextDestroyed() {
DVLOG(3) << __func__ << " this=" << this;
// We should only be notified once.
DCHECK(!media_source_context_destroyed_);
media_source_context_destroyed_ = true;
}
void SameThreadMediaSourceAttachment::Unregister() {
DVLOG(1) << __func__ << " this=" << this;
// The only expected caller is a MediaSourceRegistryImpl on the main thread.
DCHECK(IsMainThread());
// Release our strong reference to the MediaSource. Note that revokeObjectURL
// of the url associated with this attachment could commonly follow this path
// while the MediaSource (and any attachment to an HTMLMediaElement) may still
// be alive/active.
DCHECK(registered_media_source_);
registered_media_source_ = nullptr;
}
MediaSourceTracer*
SameThreadMediaSourceAttachment::StartAttachingToMediaElement(
HTMLMediaElement* element,
bool* success) {
VerifyCalledWhileContextsAliveForDebugging();
DCHECK(success);
if (!registered_media_source_) {
*success = false;
return nullptr;
}
MediaSourceTracer* tracer =
registered_media_source_->StartAttachingToMediaElement(
WrapRefCounted(this), element);
// For this same-thread attachment start, a non-nullptr tracer indicates
// success here.
*success = !!tracer;
return tracer;
}
void SameThreadMediaSourceAttachment::CompleteAttachingToMediaElement(
MediaSourceTracer* tracer,
std::unique_ptr<WebMediaSource> web_media_source) {
VerifyCalledWhileContextsAliveForDebugging();
GetMediaSource(tracer)->CompleteAttachingToMediaElement(
std::move(web_media_source));
}
void SameThreadMediaSourceAttachment::Close(MediaSourceTracer* tracer) {
// The media element may have already notified us that its context is
// destroyed, so VerifyCalledWhileContextIsAliveForDebugging() is unusable in
// this scope. Note that we might be called during main thread context
// destruction, and the ordering of MediaSource versus HTMLMediaElement
// context destruction notification is nondeterminate. MediaSource closes
// itself on its context destruction notification, so elide calling Close()
// again on it in that case. This affords stronger guarantees on when
// MediaSource::Close is callable by an attachment.
if (media_source_context_destroyed_)
return;
GetMediaSource(tracer)->Close();
}
WebTimeRanges SameThreadMediaSourceAttachment::BufferedInternal(
MediaSourceTracer* tracer) const {
// The ordering of MediaSource versus HTMLMediaElement context destruction
// notification is nondeterminate.
if (media_source_context_destroyed_) {
return {};
}
// Since the attached MediaSource, HTMLMediaElement and the element's player's
// underlying demuxer are all owned by the main thread in this SameThread
// attachment, it is safe to get an ExclusiveKey here to pass to the attached
// MediaSource to let it know it is safe to access the underlying demuxer to
// tell us what is currently buffered.
return GetMediaSource(tracer)->BufferedInternal(GetExclusiveKey());
}
WebTimeRanges SameThreadMediaSourceAttachment::SeekableInternal(
MediaSourceTracer* tracer) const {
VerifyCalledWhileContextsAliveForDebugging();
// Since the attached MediaSource, HTMLMediaElement and the element's player's
// underlying demuxer are all owned by the main thread in this SameThread
// attachment, it is safe to get an ExclusiveKey here to pass to the attached
// MediaSource to let it know it is safe to access the underlying demuxer to
// tell us what is currently seekable.
return GetMediaSource(tracer)->SeekableInternal(GetExclusiveKey());
}
void SameThreadMediaSourceAttachment::OnTrackChanged(MediaSourceTracer* tracer,
TrackBase* track) {
// In this same thread implementation, the MSE side of the attachment can loop
// back into this from SourceBuffer's initialization segment received
// algorithm notifying the element, which then calls this. Regardless, we are
// not called as part of execution context teardown, so verification should be
// stable here.
VerifyCalledWhileContextsAliveForDebugging();
GetMediaSource(tracer)->OnTrackChanged(track);
}
void SameThreadMediaSourceAttachment::OnElementTimeUpdate(double time) {
DVLOG(3) << __func__ << " this=" << this << ", time=" << time;
// The ordering of MediaSource versus HTMLMediaElement context destruction
// notification is nondeterminate.
if (media_source_context_destroyed_) {
return;
}
recent_element_time_ = time;
}
void SameThreadMediaSourceAttachment::OnElementError() {
DVLOG(3) << __func__ << " this=" << this;
VerifyCalledWhileContextsAliveForDebugging();
DCHECK(!element_has_error_)
<< "At most one transition to element error per attachment is expected";
element_has_error_ = true;
}
void SameThreadMediaSourceAttachment::OnElementContextDestroyed() {
DVLOG(3) << __func__ << " this=" << this;
// We should only be notified once.
DCHECK(!element_context_destroyed_);
element_context_destroyed_ = true;
}
void SameThreadMediaSourceAttachment::
VerifyCalledWhileContextsAliveForDebugging() const {
DCHECK(!element_context_destroyed_);
DCHECK(!media_source_context_destroyed_);
}
} // namespace blink
|