1 // Copyright 2013 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.
5 #ifndef CONTENT_RENDERER_MEDIA_ANDROID_MEDIA_SOURCE_DELEGATE_H_
6 #define CONTENT_RENDERER_MEDIA_ANDROID_MEDIA_SOURCE_DELEGATE_H_
11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/time/time.h"
16 #include "media/base/decryptor.h"
17 #include "media/base/demuxer.h"
18 #include "media/base/media_keys.h"
19 #include "media/base/pipeline_status.h"
20 #include "media/base/ranges.h"
21 #include "media/base/text_track.h"
22 #include "third_party/WebKit/public/platform/WebMediaPlayer.h"
25 class SingleThreadTaskRunner
;
31 class DecryptingDemuxerStream
;
34 struct DemuxerConfigs
;
40 class RendererDemuxerAndroid
;
42 class MediaSourceDelegate
: public media::DemuxerHost
{
44 typedef base::Callback
<void(blink::WebMediaSource
*)>
46 typedef base::Callback
<void(blink::WebMediaPlayer::NetworkState
)>
48 typedef base::Callback
<void(const base::TimeDelta
&)> DurationChangeCB
;
51 RendererDemuxerAndroid
* demuxer_client
,
52 int demuxer_client_id
,
53 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
54 const scoped_refptr
<media::MediaLog
> media_log
);
55 ~MediaSourceDelegate() override
;
57 // Initialize the MediaSourceDelegate. |media_source| will be owned by
58 // this object after this call.
59 void InitializeMediaSource(
60 const MediaSourceOpenedCB
& media_source_opened_cb
,
61 const media::Demuxer::EncryptedMediaInitDataCB
&
62 encrypted_media_init_data_cb
,
63 const media::SetDecryptorReadyCB
& set_decryptor_ready_cb
,
64 const UpdateNetworkStateCB
& update_network_state_cb
,
65 const DurationChangeCB
& duration_change_cb
,
66 const base::Closure
& waiting_for_decryption_key_cb
);
68 blink::WebTimeRanges
Buffered() const;
69 size_t DecodedFrameCount() const;
70 size_t DroppedFrameCount() const;
71 size_t AudioDecodedByteCount() const;
72 size_t VideoDecodedByteCount() const;
74 // In MSE case, calls ChunkDemuxer::CancelPendingSeek(). Also sets the
75 // expectation that a regular seek will be arriving and to trivially finish
76 // any browser seeks that may be requested prior to the regular seek.
77 void CancelPendingSeek(const base::TimeDelta
& seek_time
);
79 // In MSE case, calls ChunkDemuxer::StartWaitingForSeek(), first calling
80 // ChunkDemuxer::CancelPendingSeek() if a browser seek is in progress.
81 // Also sets the expectation that a regular seek will be arriving and to
82 // trivially finish any browser seeks that may be requested prior to the
84 void StartWaitingForSeek(const base::TimeDelta
& seek_time
);
86 // Seeks the demuxer and later calls OnDemuxerSeekDone() after the seek has
87 // been completed. There must be no other seek of the demuxer currently in
88 // process when this method is called.
89 // If |is_browser_seek| is true, then this is a short-term hack browser
91 // TODO(wolenetz): Instead of doing browser seek, browser player should replay
92 // cached data since last keyframe. See http://crbug.com/304234.
93 void Seek(const base::TimeDelta
& seek_time
, bool is_browser_seek
);
95 // Called when DemuxerStreamPlayer needs to read data from ChunkDemuxer.
96 void OnReadFromDemuxer(media::DemuxerStream::Type type
);
98 // Must be called explicitly before |this| can be destroyed.
99 void Stop(const base::Closure
& stop_cb
);
101 // Called on the main thread to check whether the video stream is encrypted.
102 bool IsVideoEncrypted();
104 // Gets the ChunkDemuxer timeline offset.
105 base::Time
GetTimelineOffset() const;
108 // Methods inherited from DemuxerHost.
109 void AddBufferedTimeRange(base::TimeDelta start
,
110 base::TimeDelta end
) override
;
111 void SetDuration(base::TimeDelta duration
) override
;
112 void OnDemuxerError(media::PipelineStatus status
) override
;
113 void AddTextStream(media::DemuxerStream
* text_stream
,
114 const media::TextTrackConfig
& config
) override
;
115 void RemoveTextStream(media::DemuxerStream
* text_stream
) override
;
117 // Notifies |demuxer_client_| and fires |duration_changed_cb_|.
118 void OnDurationChanged(const base::TimeDelta
& duration
);
120 // Callback for ChunkDemuxer initialization.
121 void OnDemuxerInitDone(media::PipelineStatus status
);
123 // Initializes DecryptingDemuxerStreams if audio/video stream is encrypted.
124 void InitAudioDecryptingDemuxerStream();
125 void InitVideoDecryptingDemuxerStream();
127 // Callbacks for DecryptingDemuxerStream::Initialize().
128 void OnAudioDecryptingDemuxerStreamInitDone(media::PipelineStatus status
);
129 void OnVideoDecryptingDemuxerStreamInitDone(media::PipelineStatus status
);
131 // Callback for ChunkDemuxer::Seek() and callback chain for resetting
132 // decrypted audio/video streams if present.
134 // Runs on the media thread.
135 void OnDemuxerSeekDone(media::PipelineStatus status
);
136 void ResetAudioDecryptingDemuxerStream();
137 void ResetVideoDecryptingDemuxerStream();
138 void FinishResettingDecryptingDemuxerStreams();
140 void OnDemuxerOpened();
141 void OnEncryptedMediaInitData(media::EmeInitDataType init_data_type
,
142 const std::vector
<uint8
>& init_data
);
143 void NotifyDemuxerReady();
145 // Stops and clears objects on the media thread.
146 void StopDemuxer(const base::Closure
& stop_cb
);
148 void InitializeDemuxer();
149 void SeekInternal(const base::TimeDelta
& seek_time
);
150 // Reads an access unit from the demuxer stream |stream| and stores it in
151 // the |index|th access unit in |params|.
152 void ReadFromDemuxerStream(media::DemuxerStream::Type type
,
153 scoped_ptr
<media::DemuxerData
> data
,
155 void OnBufferReady(media::DemuxerStream::Type type
,
156 scoped_ptr
<media::DemuxerData
> data
,
158 media::DemuxerStream::Status status
,
159 const scoped_refptr
<media::DecoderBuffer
>& buffer
);
161 // Helper function for calculating duration.
162 base::TimeDelta
GetDuration() const;
164 bool IsSeeking() const;
166 // Returns |seek_time| if it is still buffered or if there is no currently
167 // buffered range including or soon after |seek_time|. If |seek_time| is not
168 // buffered, but there is a later range buffered near to |seek_time|, returns
169 // next buffered range's start time instead. Only call this for browser seeks.
170 // |seeking_lock_| must be held by caller.
171 base::TimeDelta
FindBufferedBrowserSeekTime_Locked(
172 const base::TimeDelta
& seek_time
) const;
174 // Get the demuxer configs for a particular stream identified by |is_audio|.
175 // Returns true on success, of false otherwise.
176 bool GetDemuxerConfigFromStream(media::DemuxerConfigs
* configs
,
179 RendererDemuxerAndroid
* demuxer_client_
;
180 int demuxer_client_id_
;
182 scoped_refptr
<media::MediaLog
> media_log_
;
183 UpdateNetworkStateCB update_network_state_cb_
;
184 DurationChangeCB duration_change_cb_
;
186 scoped_ptr
<media::ChunkDemuxer
> chunk_demuxer_
;
187 bool is_demuxer_ready_
;
189 media::SetDecryptorReadyCB set_decryptor_ready_cb_
;
191 scoped_ptr
<media::DecryptingDemuxerStream
> audio_decrypting_demuxer_stream_
;
192 scoped_ptr
<media::DecryptingDemuxerStream
> video_decrypting_demuxer_stream_
;
194 media::DemuxerStream
* audio_stream_
;
195 media::DemuxerStream
* video_stream_
;
197 media::PipelineStatistics statistics_
;
198 media::Ranges
<base::TimeDelta
> buffered_time_ranges_
;
200 MediaSourceOpenedCB media_source_opened_cb_
;
201 media::Demuxer::EncryptedMediaInitDataCB encrypted_media_init_data_cb_
;
202 base::Closure waiting_for_decryption_key_cb_
;
204 // Lock used to serialize access for |seeking_|.
205 mutable base::Lock seeking_lock_
;
208 // Lock used to serialize access for |is_video_encrypted_|.
209 mutable base::Lock is_video_encrypted_lock_
;
210 bool is_video_encrypted_
;
212 // Track if we are currently performing a browser seek, and track whether or
213 // not a regular seek is expected soon. If a regular seek is expected soon,
214 // then any in-progress browser seek will be canceled pending the
215 // regular seek, if using |chunk_demuxer_|, and any requested browser seek
216 // will be trivially finished. Access is serialized by |seeking_lock_|.
217 bool doing_browser_seek_
;
218 base::TimeDelta browser_seek_time_
;
219 bool expecting_regular_seek_
;
221 size_t access_unit_size_
;
223 // Task runner for main renderer and media threads.
224 const scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner_
;
225 const scoped_refptr
<base::SingleThreadTaskRunner
> media_task_runner_
;
227 // NOTE: Weak pointers must be invalidated before all other member variables.
228 base::WeakPtr
<MediaSourceDelegate
> main_weak_this_
;
229 base::WeakPtrFactory
<MediaSourceDelegate
> main_weak_factory_
;
230 base::WeakPtrFactory
<MediaSourceDelegate
> media_weak_factory_
;
232 DISALLOW_COPY_AND_ASSIGN(MediaSourceDelegate
);
235 } // namespace content
237 #endif // CONTENT_RENDERER_MEDIA_ANDROID_MEDIA_SOURCE_DELEGATE_H_