[ServiceWorker] Implement WebServiceWorkerContextClient::openWindow().
[chromium-blink-merge.git] / content / renderer / media / android / media_source_delegate.h
blobc8eaade945e3809719d30ace3a0cbb41e2dea492
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_
8 #include <string>
9 #include <vector>
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"
24 namespace base {
25 class SingleThreadTaskRunner;
28 namespace media {
29 class ChunkDemuxer;
30 class DecoderBuffer;
31 class DecryptingDemuxerStream;
32 class DemuxerStream;
33 class MediaLog;
34 struct DemuxerConfigs;
35 struct DemuxerData;
38 namespace content {
40 class RendererDemuxerAndroid;
42 class MediaSourceDelegate : public media::DemuxerHost {
43 public:
44 typedef base::Callback<void(blink::WebMediaSource*)>
45 MediaSourceOpenedCB;
46 typedef base::Callback<void(blink::WebMediaPlayer::NetworkState)>
47 UpdateNetworkStateCB;
48 typedef base::Callback<void(const base::TimeDelta&)> DurationChangeCB;
50 MediaSourceDelegate(
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);
67 blink::WebTimeRanges Buffered() const;
68 size_t DecodedFrameCount() const;
69 size_t DroppedFrameCount() const;
70 size_t AudioDecodedByteCount() const;
71 size_t VideoDecodedByteCount() const;
73 // In MSE case, calls ChunkDemuxer::CancelPendingSeek(). Also sets the
74 // expectation that a regular seek will be arriving and to trivially finish
75 // any browser seeks that may be requested prior to the regular seek.
76 void CancelPendingSeek(const base::TimeDelta& seek_time);
78 // In MSE case, calls ChunkDemuxer::StartWaitingForSeek(), first calling
79 // ChunkDemuxer::CancelPendingSeek() if a browser seek is in progress.
80 // Also sets the expectation that a regular seek will be arriving and to
81 // trivially finish any browser seeks that may be requested prior to the
82 // regular seek.
83 void StartWaitingForSeek(const base::TimeDelta& seek_time);
85 // Seeks the demuxer and later calls OnDemuxerSeekDone() after the seek has
86 // been completed. There must be no other seek of the demuxer currently in
87 // process when this method is called.
88 // If |is_browser_seek| is true, then this is a short-term hack browser
89 // seek.
90 // TODO(wolenetz): Instead of doing browser seek, browser player should replay
91 // cached data since last keyframe. See http://crbug.com/304234.
92 void Seek(const base::TimeDelta& seek_time, bool is_browser_seek);
94 // Called when DemuxerStreamPlayer needs to read data from ChunkDemuxer.
95 void OnReadFromDemuxer(media::DemuxerStream::Type type);
97 // Must be called explicitly before |this| can be destroyed.
98 void Stop(const base::Closure& stop_cb);
100 // Called on the main thread to check whether the video stream is encrypted.
101 bool IsVideoEncrypted();
103 // Gets the ChunkDemuxer timeline offset.
104 base::Time GetTimelineOffset() const;
106 private:
107 // Methods inherited from DemuxerHost.
108 void AddBufferedTimeRange(base::TimeDelta start,
109 base::TimeDelta end) override;
110 void SetDuration(base::TimeDelta duration) override;
111 void OnDemuxerError(media::PipelineStatus status) override;
112 void AddTextStream(media::DemuxerStream* text_stream,
113 const media::TextTrackConfig& config) override;
114 void RemoveTextStream(media::DemuxerStream* text_stream) override;
116 // Notifies |demuxer_client_| and fires |duration_changed_cb_|.
117 void OnDurationChanged(const base::TimeDelta& duration);
119 // Callback for ChunkDemuxer initialization.
120 void OnDemuxerInitDone(media::PipelineStatus status);
122 // Initializes DecryptingDemuxerStreams if audio/video stream is encrypted.
123 void InitAudioDecryptingDemuxerStream();
124 void InitVideoDecryptingDemuxerStream();
126 // Callbacks for DecryptingDemuxerStream::Initialize().
127 void OnAudioDecryptingDemuxerStreamInitDone(media::PipelineStatus status);
128 void OnVideoDecryptingDemuxerStreamInitDone(media::PipelineStatus status);
130 // Callback for ChunkDemuxer::Seek() and callback chain for resetting
131 // decrypted audio/video streams if present.
133 // Runs on the media thread.
134 void OnDemuxerSeekDone(media::PipelineStatus status);
135 void ResetAudioDecryptingDemuxerStream();
136 void ResetVideoDecryptingDemuxerStream();
137 void FinishResettingDecryptingDemuxerStreams();
139 void OnDemuxerOpened();
140 void OnEncryptedMediaInitData(const std::string& init_data_type,
141 const std::vector<uint8>& init_data);
142 void NotifyDemuxerReady();
144 // Stops and clears objects on the media thread.
145 void StopDemuxer(const base::Closure& stop_cb);
147 void InitializeDemuxer();
148 void SeekInternal(const base::TimeDelta& seek_time);
149 // Reads an access unit from the demuxer stream |stream| and stores it in
150 // the |index|th access unit in |params|.
151 void ReadFromDemuxerStream(media::DemuxerStream::Type type,
152 scoped_ptr<media::DemuxerData> data,
153 size_t index);
154 void OnBufferReady(media::DemuxerStream::Type type,
155 scoped_ptr<media::DemuxerData> data,
156 size_t index,
157 media::DemuxerStream::Status status,
158 const scoped_refptr<media::DecoderBuffer>& buffer);
160 // Helper function for calculating duration.
161 base::TimeDelta GetDuration() const;
163 bool IsSeeking() const;
165 // Returns |seek_time| if it is still buffered or if there is no currently
166 // buffered range including or soon after |seek_time|. If |seek_time| is not
167 // buffered, but there is a later range buffered near to |seek_time|, returns
168 // next buffered range's start time instead. Only call this for browser seeks.
169 // |seeking_lock_| must be held by caller.
170 base::TimeDelta FindBufferedBrowserSeekTime_Locked(
171 const base::TimeDelta& seek_time) const;
173 // Get the demuxer configs for a particular stream identified by |is_audio|.
174 // Returns true on success, of false otherwise.
175 bool GetDemuxerConfigFromStream(media::DemuxerConfigs* configs,
176 bool is_audio);
178 RendererDemuxerAndroid* demuxer_client_;
179 int demuxer_client_id_;
181 scoped_refptr<media::MediaLog> media_log_;
182 UpdateNetworkStateCB update_network_state_cb_;
183 DurationChangeCB duration_change_cb_;
185 scoped_ptr<media::ChunkDemuxer> chunk_demuxer_;
186 bool is_demuxer_ready_;
188 media::SetDecryptorReadyCB set_decryptor_ready_cb_;
190 scoped_ptr<media::DecryptingDemuxerStream> audio_decrypting_demuxer_stream_;
191 scoped_ptr<media::DecryptingDemuxerStream> video_decrypting_demuxer_stream_;
193 media::DemuxerStream* audio_stream_;
194 media::DemuxerStream* video_stream_;
196 media::PipelineStatistics statistics_;
197 media::Ranges<base::TimeDelta> buffered_time_ranges_;
199 MediaSourceOpenedCB media_source_opened_cb_;
200 media::Demuxer::EncryptedMediaInitDataCB encrypted_media_init_data_cb_;
202 // Temporary for EME v0.1. In the future the init data type should be passed
203 // through GenerateKeyRequest() directly from WebKit.
204 std::string init_data_type_;
206 // Lock used to serialize access for |seeking_|.
207 mutable base::Lock seeking_lock_;
208 bool seeking_;
210 // Lock used to serialize access for |is_video_encrypted_|.
211 mutable base::Lock is_video_encrypted_lock_;
212 bool is_video_encrypted_;
214 // Track if we are currently performing a browser seek, and track whether or
215 // not a regular seek is expected soon. If a regular seek is expected soon,
216 // then any in-progress browser seek will be canceled pending the
217 // regular seek, if using |chunk_demuxer_|, and any requested browser seek
218 // will be trivially finished. Access is serialized by |seeking_lock_|.
219 bool doing_browser_seek_;
220 base::TimeDelta browser_seek_time_;
221 bool expecting_regular_seek_;
223 size_t access_unit_size_;
225 // Task runner for main renderer and media threads.
226 const scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
227 const scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_;
229 // NOTE: Weak pointers must be invalidated before all other member variables.
230 base::WeakPtr<MediaSourceDelegate> main_weak_this_;
231 base::WeakPtrFactory<MediaSourceDelegate> main_weak_factory_;
232 base::WeakPtrFactory<MediaSourceDelegate> media_weak_factory_;
234 DISALLOW_COPY_AND_ASSIGN(MediaSourceDelegate);
237 } // namespace content
239 #endif // CONTENT_RENDERER_MEDIA_ANDROID_MEDIA_SOURCE_DELEGATE_H_