1 // Copyright 2020 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIASOURCE_SAME_THREAD_MEDIA_SOURCE_ATTACHMENT_H_ 6 #define THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIASOURCE_SAME_THREAD_MEDIA_SOURCE_ATTACHMENT_H_ 7 8 #include <memory> 9 10 #include "base/util/type_safety/pass_key.h" 11 #include "third_party/blink/public/platform/web_time_range.h" 12 #include "third_party/blink/renderer/core/html/track/audio_track.h" 13 #include "third_party/blink/renderer/core/html/track/audio_track_list.h" 14 #include "third_party/blink/renderer/core/html/track/video_track.h" 15 #include "third_party/blink/renderer/core/html/track/video_track_list.h" 16 #include "third_party/blink/renderer/modules/mediasource/media_source.h" 17 #include "third_party/blink/renderer/modules/mediasource/media_source_attachment_supplement.h" 18 #include "third_party/blink/renderer/modules/mediasource/url_media_source.h" 19 #include "third_party/blink/renderer/platform/heap/persistent.h" 20 21 namespace blink { 22 23 // Concrete attachment that supports operation only on the main thread. 24 class SameThreadMediaSourceAttachment final 25 : public MediaSourceAttachmentSupplement { 26 public: 27 // The only intended caller of this constructor is 28 // URLMediaSource::createObjectUrl, made more clear by using the PassKey. The 29 // raw pointer is then adopted into a scoped_refptr in 30 // MediaSourceRegistryImpl::RegisterURL. 31 SameThreadMediaSourceAttachment(MediaSource* media_source, 32 util::PassKey<URLMediaSource>); 33 34 // MediaSourceAttachmentSupplement 35 void NotifyDurationChanged(MediaSourceTracer* tracer, double duration) final; 36 double GetRecentMediaTime(MediaSourceTracer* tracer) final; 37 bool GetElementError(MediaSourceTracer* tracer) final; 38 AudioTrackList* CreateAudioTrackList(MediaSourceTracer* tracer) final; 39 VideoTrackList* CreateVideoTrackList(MediaSourceTracer* tracer) final; 40 void AddAudioTrackToMediaElement(MediaSourceTracer* tracer, 41 AudioTrack* track) final; 42 void AddVideoTrackToMediaElement(MediaSourceTracer* tracer, 43 VideoTrack* track) final; 44 void RemoveAudioTracksFromMediaElement(MediaSourceTracer* tracer, 45 Vector<String> audio_ids, 46 bool enqueue_change_event) final; 47 void RemoveVideoTracksFromMediaElement(MediaSourceTracer* tracer, 48 Vector<String> video_ids, 49 bool enqueue_change_event) final; 50 void OnMediaSourceContextDestroyed() final; 51 52 // MediaSourceAttachment 53 void Unregister() final; 54 MediaSourceTracer* StartAttachingToMediaElement(HTMLMediaElement*, 55 bool* success) final; 56 void CompleteAttachingToMediaElement(MediaSourceTracer* tracer, 57 std::unique_ptr<WebMediaSource>) final; 58 59 void Close(MediaSourceTracer* tracer) final; 60 WebTimeRanges BufferedInternal(MediaSourceTracer* tracer) const final; 61 WebTimeRanges SeekableInternal(MediaSourceTracer* tracer) const final; 62 void OnTrackChanged(MediaSourceTracer* tracer, TrackBase*) final; 63 64 void OnElementTimeUpdate(double time) final; 65 void OnElementError() final; 66 void OnElementContextDestroyed() final; 67 68 private: 69 ~SameThreadMediaSourceAttachment() override; 70 71 // In this same thread implementation, if the media element context is 72 // destroyed, then so should be the Media Source's context. This method 73 // this precondition in debug mode. 74 void VerifyCalledWhileContextsAliveForDebugging() const; 75 76 // Cache of the registered MediaSource. Retains strong reference from 77 // construction of this object until Unregister() is called. 78 Persistent<MediaSource> registered_media_source_; 79 80 // These are mostly used to verify correct behavior of the media element and 81 // media source state pumping in debug builds. In a cross-thread attachment 82 // implementation, state like this will be relied upon for servicing the 83 // MediaSource API. 84 double recent_element_time_; // See OnElementTimeUpdate(). 85 bool element_has_error_; // See OnElementError(). 86 bool element_context_destroyed_; // See OnElementContextDestroyed(). 87 bool media_source_context_destroyed_; // See OnMediaSourceContextDestroyed(). 88 89 DISALLOW_COPY_AND_ASSIGN(SameThreadMediaSourceAttachment); 90 }; 91 92 } // namespace blink 93 94 #endif // THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIASOURCE_SAME_THREAD_MEDIA_SOURCE_ATTACHMENT_H_ 95