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