OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_BASE_ANDROID_MEDIA_SOURCE_PLAYER_H_ | 5 #ifndef MEDIA_BASE_ANDROID_MEDIA_SOURCE_PLAYER_H_ |
6 #define MEDIA_BASE_ANDROID_MEDIA_SOURCE_PLAYER_H_ | 6 #define MEDIA_BASE_ANDROID_MEDIA_SOURCE_PLAYER_H_ |
7 | 7 |
8 #include <jni.h> | 8 #include <jni.h> |
9 #include <map> | 9 #include <map> |
10 #include <string> | 10 #include <string> |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 | 92 |
93 // Gets MediaCrypto object from |drm_bridge_|. | 93 // Gets MediaCrypto object from |drm_bridge_|. |
94 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); | 94 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); |
95 | 95 |
96 // Callback to notify that MediaCrypto is ready in |drm_bridge_|. | 96 // Callback to notify that MediaCrypto is ready in |drm_bridge_|. |
97 void OnMediaCryptoReady(); | 97 void OnMediaCryptoReady(); |
98 | 98 |
99 // Handle pending events if all the decoder jobs are not currently decoding. | 99 // Handle pending events if all the decoder jobs are not currently decoding. |
100 void ProcessPendingEvents(); | 100 void ProcessPendingEvents(); |
101 | 101 |
102 // Helper method to clear any pending |SURFACE_CHANGE_EVENT_PENDING| | |
103 // and reset |video_decoder_job_| to null. | |
104 void ResetVideoDecoderJob(); | |
105 void ResetAudioDecoderJob(); | |
106 | |
107 // Helper methods to configure the decoder jobs. | |
108 void ConfigureVideoDecoderJob(); | |
109 void ConfigureAudioDecoderJob(); | |
110 | |
111 // Flush the decoders and clean up all the data needs to be decoded. | 102 // Flush the decoders and clean up all the data needs to be decoded. |
112 void ClearDecodingData(); | 103 void ClearDecodingData(); |
113 | 104 |
114 // Called to decode more data. | 105 // Called to decode more data. |
115 void DecodeMoreAudio(); | 106 void DecodeMoreAudio(); |
116 void DecodeMoreVideo(); | 107 void DecodeMoreVideo(); |
117 | 108 |
118 // Functions check whether audio/video is present. | 109 // Functions check whether audio/video is present. |
119 bool HasVideo(); | 110 bool HasVideo(); |
120 bool HasAudio(); | 111 bool HasAudio(); |
(...skipping 26 matching lines...) Expand all Loading... |
147 void ScheduleSeekEventAndStopDecoding(base::TimeDelta seek_time); | 138 void ScheduleSeekEventAndStopDecoding(base::TimeDelta seek_time); |
148 | 139 |
149 // Schedules a browser seek event. We must not currently be processing any | 140 // Schedules a browser seek event. We must not currently be processing any |
150 // seek. Note that there is possibility that browser seek of renderer demuxer | 141 // seek. Note that there is possibility that browser seek of renderer demuxer |
151 // may unexpectedly stall due to lack of buffered data at or after the browser | 142 // may unexpectedly stall due to lack of buffered data at or after the browser |
152 // seek time. | 143 // seek time. |
153 // TODO(wolenetz): Instead of doing hack browser seek, replay cached data | 144 // TODO(wolenetz): Instead of doing hack browser seek, replay cached data |
154 // since last keyframe. See http://crbug.com/304234. | 145 // since last keyframe. See http://crbug.com/304234. |
155 void BrowserSeekToCurrentTime(); | 146 void BrowserSeekToCurrentTime(); |
156 | 147 |
157 // Helper function to set the volume. | |
158 void SetVolumeInternal(); | |
159 | |
160 // Helper function to determine whether a protected surface is needed for | 148 // Helper function to determine whether a protected surface is needed for |
161 // video playback. | 149 // video playback. |
162 bool IsProtectedSurfaceRequired(); | 150 bool IsProtectedSurfaceRequired(); |
163 | 151 |
164 // Called when a MediaDecoderJob finishes prefetching data. Once all | 152 // Called when a MediaDecoderJob finishes prefetching data. Once all |
165 // MediaDecoderJobs have prefetched data, then this method updates | 153 // MediaDecoderJobs have prefetched data, then this method updates |
166 // |start_time_ticks_| and |start_presentation_timestamp_| so that video can | 154 // |start_time_ticks_| and |start_presentation_timestamp_| so that video can |
167 // resync with audio and starts decoding. | 155 // resync with audio and starts decoding. |
168 void OnPrefetchDone(); | 156 void OnPrefetchDone(); |
169 | 157 |
170 // Sets the demuxer configs for audio or video stream. | 158 // Called when the demuxer config changes. |
171 void SetDemuxerConfigs(const DemuxerConfigs& configs, bool is_audio); | 159 void OnDemuxerConfigsChanged(); |
172 | 160 |
173 // Called when new decryption key becomes available. | 161 // Called when new decryption key becomes available. |
174 void OnKeyAdded(); | 162 void OnKeyAdded(); |
175 | 163 |
176 // Called when the CDM is detached. | 164 // Called when the CDM is detached. |
177 void OnCdmUnset(); | 165 void OnCdmUnset(); |
178 | 166 |
179 // Test-only method to setup hook for the completion of the next decode cycle. | 167 // Test-only method to setup hook for the completion of the next decode cycle. |
180 // This callback state is cleared when it is next run. | 168 // This callback state is cleared when it is next run. |
181 // Prevent usage creep by only calling this from the | 169 // Prevent usage creep by only calling this from the |
182 // ReleaseWithOnPrefetchDoneAlreadyPosted MediaSourcePlayerTest. | 170 // ReleaseWithOnPrefetchDoneAlreadyPosted MediaSourcePlayerTest. |
183 void set_decode_callback_for_testing(const base::Closure& test_decode_cb) { | 171 void set_decode_callback_for_testing(const base::Closure& test_decode_cb) { |
184 decode_callback_for_testing_ = test_decode_cb; | 172 decode_callback_for_testing_ = test_decode_cb; |
185 } | 173 } |
186 | 174 |
187 // TODO(qinmin/wolenetz): Reorder these based on their priority from | 175 // Please keep this in sync with |kPendingEventNames| in GetEventName(). |
188 // ProcessPendingEvents(). Release() and other routines are dependent upon | |
189 // priority consistency. | |
190 enum PendingEventFlags { | 176 enum PendingEventFlags { |
191 NO_EVENT_PENDING = 0, | 177 NO_EVENT_PENDING = 0, |
192 SEEK_EVENT_PENDING = 1 << 0, | 178 PREFETCH_DONE_EVENT_PENDING = 1 << 0, |
193 SURFACE_CHANGE_EVENT_PENDING = 1 << 1, | 179 SEEK_EVENT_PENDING = 1 << 1, |
194 CONFIG_CHANGE_EVENT_PENDING = 1 << 2, | 180 DECODER_CREATION_EVENT_PENDING = 1 << 2, |
195 PREFETCH_REQUEST_EVENT_PENDING = 1 << 3, | 181 PREFETCH_REQUEST_EVENT_PENDING = 1 << 3, |
196 PREFETCH_DONE_EVENT_PENDING = 1 << 4, | |
197 }; | 182 }; |
198 | 183 |
199 static const char* GetEventName(PendingEventFlags event); | 184 static const char* GetEventName(PendingEventFlags event); |
200 bool IsEventPending(PendingEventFlags event) const; | 185 bool IsEventPending(PendingEventFlags event) const; |
201 void SetPendingEvent(PendingEventFlags event); | 186 void SetPendingEvent(PendingEventFlags event); |
202 void ClearPendingEvent(PendingEventFlags event); | 187 void ClearPendingEvent(PendingEventFlags event); |
203 | 188 |
| 189 // If the player is previously waiting for audio or video decoder job, retry |
| 190 // creating the decoders identified by |audio| and |video|. |
| 191 void RetryDecoderCreation(bool audio, bool video); |
| 192 |
204 scoped_ptr<DemuxerAndroid> demuxer_; | 193 scoped_ptr<DemuxerAndroid> demuxer_; |
205 | 194 |
206 // Pending event that the player needs to do. | 195 // Pending event that the player needs to do. |
207 unsigned pending_event_; | 196 unsigned pending_event_; |
208 | 197 |
209 // Stats about the media. | 198 // Stats about the media. |
210 base::TimeDelta duration_; | 199 base::TimeDelta duration_; |
211 int width_; | |
212 int height_; | |
213 AudioCodec audio_codec_; | |
214 VideoCodec video_codec_; | |
215 int num_channels_; | |
216 int sampling_rate_; | |
217 // TODO(xhwang/qinmin): Add |video_extra_data_|. | |
218 std::vector<uint8> audio_extra_data_; | |
219 bool reached_audio_eos_; | |
220 bool reached_video_eos_; | |
221 bool playing_; | 200 bool playing_; |
222 bool is_audio_encrypted_; | |
223 bool is_video_encrypted_; | |
224 double volume_; | |
225 | 201 |
226 // base::TickClock used by |clock_|. | 202 // base::TickClock used by |clock_|. |
227 base::DefaultTickClock default_tick_clock_; | 203 base::DefaultTickClock default_tick_clock_; |
228 | 204 |
229 // Reference clock. Keeps track of current playback time. | 205 // Reference clock. Keeps track of current playback time. |
230 Clock clock_; | 206 Clock clock_; |
231 | 207 |
232 // Timestamps for providing simple A/V sync. When start decoding an audio | 208 // Timestamps for providing simple A/V sync. When start decoding an audio |
233 // chunk, we record its presentation timestamp and the current system time. | 209 // chunk, we record its presentation timestamp and the current system time. |
234 // Then we use this information to estimate when the next audio/video frame | 210 // Then we use this information to estimate when the next audio/video frame |
235 // should be rendered. | 211 // should be rendered. |
236 // TODO(qinmin): Need to fix the problem if audio/video lagged too far behind | 212 // TODO(qinmin): Need to fix the problem if audio/video lagged too far behind |
237 // due to network or decoding problem. | 213 // due to network or decoding problem. |
238 base::TimeTicks start_time_ticks_; | 214 base::TimeTicks start_time_ticks_; |
239 base::TimeDelta start_presentation_timestamp_; | 215 base::TimeDelta start_presentation_timestamp_; |
240 | 216 |
241 // The surface object currently owned by the player. | |
242 gfx::ScopedJavaSurface surface_; | |
243 | |
244 // Track whether or not the player has received any video data since the most | |
245 // recent of player construction, end of last seek, or receiving and | |
246 // detecting a |kConfigChanged| access unit from the demuxer. | |
247 // If no such video data has been received, the next video data begins with | |
248 // an I-frame. Otherwise, we have no such guarantee. | |
249 bool next_video_data_is_iframe_; | |
250 | |
251 // Flag that is true if doing a hack browser seek or false if doing a | 217 // Flag that is true if doing a hack browser seek or false if doing a |
252 // regular seek. Only valid when |SEEK_EVENT_PENDING| is pending. | 218 // regular seek. Only valid when |SEEK_EVENT_PENDING| is pending. |
253 // TODO(wolenetz): Instead of doing hack browser seek, replay cached data | 219 // TODO(wolenetz): Instead of doing hack browser seek, replay cached data |
254 // since last keyframe. See http://crbug.com/304234. | 220 // since last keyframe. See http://crbug.com/304234. |
255 bool doing_browser_seek_; | 221 bool doing_browser_seek_; |
256 | 222 |
257 // If already doing a browser seek when a regular seek request arrives, | 223 // If already doing a browser seek when a regular seek request arrives, |
258 // these fields remember the regular seek so OnDemuxerSeekDone() can trigger | 224 // these fields remember the regular seek so OnDemuxerSeekDone() can trigger |
259 // it when the browser seek is done. These are only valid when | 225 // it when the browser seek is done. These are only valid when |
260 // |SEEK_EVENT_PENDING| is pending. | 226 // |SEEK_EVENT_PENDING| is pending. |
261 bool pending_seek_; | 227 bool pending_seek_; |
262 base::TimeDelta pending_seek_time_; | 228 base::TimeDelta pending_seek_time_; |
263 | 229 |
264 // Decoder jobs. | 230 // Decoder jobs. |
265 scoped_ptr<AudioDecoderJob, MediaDecoderJob::Deleter> audio_decoder_job_; | 231 scoped_ptr<AudioDecoderJob, MediaDecoderJob::Deleter> audio_decoder_job_; |
266 scoped_ptr<VideoDecoderJob, MediaDecoderJob::Deleter> video_decoder_job_; | 232 scoped_ptr<VideoDecoderJob, MediaDecoderJob::Deleter> video_decoder_job_; |
267 | 233 |
268 bool reconfig_audio_decoder_; | |
269 bool reconfig_video_decoder_; | |
270 | |
271 // Track the most recent preroll target. Decoder re-creation needs this to | 234 // Track the most recent preroll target. Decoder re-creation needs this to |
272 // resume any in-progress preroll. | 235 // resume any in-progress preroll. |
273 base::TimeDelta preroll_timestamp_; | 236 base::TimeDelta preroll_timestamp_; |
274 | 237 |
275 // A cancelable task that is posted when the audio decoder starts requesting | 238 // A cancelable task that is posted when the audio decoder starts requesting |
276 // new data. This callback runs if no data arrives before the timeout period | 239 // new data. This callback runs if no data arrives before the timeout period |
277 // elapses. | 240 // elapses. |
278 base::CancelableClosure decoder_starvation_callback_; | 241 base::CancelableClosure decoder_starvation_callback_; |
279 | 242 |
280 MediaDrmBridge* drm_bridge_; | 243 MediaDrmBridge* drm_bridge_; |
281 int cdm_registration_id_; | 244 int cdm_registration_id_; |
282 | 245 |
283 // No decryption key available to decrypt the encrypted buffer. In this case, | 246 // No decryption key available to decrypt the encrypted buffer. In this case, |
284 // the player should pause. When a new key is added (OnKeyAdded()), we should | 247 // the player should pause. When a new key is added (OnKeyAdded()), we should |
285 // try to start playback again. | 248 // try to start playback again. |
286 bool is_waiting_for_key_; | 249 bool is_waiting_for_key_; |
287 | 250 |
| 251 // Indicates whether the player is waiting for audio or video decoder to be |
| 252 // created. This could happen if video surface is not available or key is |
| 253 // not added. |
| 254 bool is_waiting_for_audio_decoder_; |
| 255 bool is_waiting_for_video_decoder_; |
| 256 |
288 // Test-only callback for hooking the completion of the next decode cycle. | 257 // Test-only callback for hooking the completion of the next decode cycle. |
289 base::Closure decode_callback_for_testing_; | 258 base::Closure decode_callback_for_testing_; |
290 | 259 |
291 // Whether |surface_| is currently used by the player. | 260 // Whether |surface_| is currently used by the player. |
292 bool is_surface_in_use_; | 261 bool is_surface_in_use_; |
293 | 262 |
294 // Whether there are pending data requests by the decoder. | |
295 bool has_pending_audio_data_request_; | |
296 bool has_pending_video_data_request_; | |
297 | |
298 // Weak pointer passed to media decoder jobs for callbacks. | 263 // Weak pointer passed to media decoder jobs for callbacks. |
299 // NOTE: Weak pointers must be invalidated before all other member variables. | 264 // NOTE: Weak pointers must be invalidated before all other member variables. |
300 base::WeakPtrFactory<MediaSourcePlayer> weak_factory_; | 265 base::WeakPtrFactory<MediaSourcePlayer> weak_factory_; |
301 base::WeakPtr<MediaSourcePlayer> weak_this_; | 266 base::WeakPtr<MediaSourcePlayer> weak_this_; |
302 | 267 |
303 DISALLOW_COPY_AND_ASSIGN(MediaSourcePlayer); | 268 DISALLOW_COPY_AND_ASSIGN(MediaSourcePlayer); |
304 }; | 269 }; |
305 | 270 |
306 } // namespace media | 271 } // namespace media |
307 | 272 |
308 #endif // MEDIA_BASE_ANDROID_MEDIA_SOURCE_PLAYER_H_ | 273 #endif // MEDIA_BASE_ANDROID_MEDIA_SOURCE_PLAYER_H_ |
OLD | NEW |