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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 // Flush the decoder. | 52 // Flush the decoder. |
53 void Flush(); | 53 void Flush(); |
54 | 54 |
55 struct Deleter { | 55 struct Deleter { |
56 inline void operator()(MediaDecoderJob* ptr) const { ptr->Release(); } | 56 inline void operator()(MediaDecoderJob* ptr) const { ptr->Release(); } |
57 }; | 57 }; |
58 | 58 |
59 // Causes this instance to be deleted on the thread it is bound to. | 59 // Causes this instance to be deleted on the thread it is bound to. |
60 void Release(); | 60 void Release(); |
61 | 61 |
| 62 // Called on the UI thread to indicate that one decode cycle has completed. |
| 63 void OnDecodeCompleted(); |
| 64 |
62 protected: | 65 protected: |
63 MediaDecoderJob(base::Thread* thread, bool is_audio); | 66 MediaDecoderJob(base::Thread* thread, bool is_audio); |
64 | 67 |
65 // Release the output buffer and render it. | 68 // Release the output buffer and render it. |
66 void ReleaseOutputBuffer( | 69 void ReleaseOutputBuffer( |
67 int outputBufferIndex, size_t size, | 70 int outputBufferIndex, size_t size, |
68 const base::TimeDelta& presentation_timestamp, | 71 const base::TimeDelta& presentation_timestamp, |
69 bool end_of_stream, const MediaDecoderJob::DecoderCallback& callback); | 72 bool end_of_stream, const MediaDecoderJob::DecoderCallback& callback); |
70 | 73 |
71 // Helper function to decoder data on |thread_|. |unit| contains all the data | 74 // Helper function to decoder data on |thread_|. |unit| contains all the data |
(...skipping 17 matching lines...) Expand all Loading... |
89 | 92 |
90 // Thread the decode task runs on. | 93 // Thread the decode task runs on. |
91 base::Thread* thread_; | 94 base::Thread* thread_; |
92 | 95 |
93 // Whether the decoder needs to be flushed. | 96 // Whether the decoder needs to be flushed. |
94 bool needs_flush_; | 97 bool needs_flush_; |
95 | 98 |
96 // Whether this is an audio decoder. | 99 // Whether this is an audio decoder. |
97 bool is_audio_; | 100 bool is_audio_; |
98 | 101 |
99 // Weak pointer passed to media decoder jobs for callbacks. | 102 // Weak pointer passed to media decoder jobs for callbacks. It is bounded to |
| 103 // the decoder thread. |
100 base::WeakPtrFactory<MediaDecoderJob> weak_this_; | 104 base::WeakPtrFactory<MediaDecoderJob> weak_this_; |
| 105 |
| 106 // Whether the decoder is actively decoding data. |
| 107 bool decoding_; |
101 }; | 108 }; |
102 | 109 |
103 typedef scoped_ptr<MediaDecoderJob, MediaDecoderJob::Deleter> | 110 typedef scoped_ptr<MediaDecoderJob, MediaDecoderJob::Deleter> |
104 ScopedMediaDecoderJob; | 111 ScopedMediaDecoderJob; |
105 | 112 |
106 // This class handles media source extensions on Android. It uses Android | 113 // This class handles media source extensions on Android. It uses Android |
107 // MediaCodec to decode audio and video streams in two separate threads. | 114 // MediaCodec to decode audio and video streams in two separate threads. |
108 // IPC is being used to send data from the render process to this object. | 115 // IPC is being used to send data from the render process to this object. |
109 // TODO(qinmin): use shared memory to send data between processes. | 116 // TODO(qinmin): use shared memory to send data between processes. |
110 class MEDIA_EXPORT MediaSourcePlayer : public MediaPlayerAndroid { | 117 class MEDIA_EXPORT MediaSourcePlayer : public MediaPlayerAndroid { |
(...skipping 13 matching lines...) Expand all Loading... |
124 virtual void SetVolume(float leftVolume, float rightVolume) OVERRIDE; | 131 virtual void SetVolume(float leftVolume, float rightVolume) OVERRIDE; |
125 virtual int GetVideoWidth() OVERRIDE; | 132 virtual int GetVideoWidth() OVERRIDE; |
126 virtual int GetVideoHeight() OVERRIDE; | 133 virtual int GetVideoHeight() OVERRIDE; |
127 virtual base::TimeDelta GetCurrentTime() OVERRIDE; | 134 virtual base::TimeDelta GetCurrentTime() OVERRIDE; |
128 virtual base::TimeDelta GetDuration() OVERRIDE; | 135 virtual base::TimeDelta GetDuration() OVERRIDE; |
129 virtual bool IsPlaying() OVERRIDE; | 136 virtual bool IsPlaying() OVERRIDE; |
130 virtual bool CanPause() OVERRIDE; | 137 virtual bool CanPause() OVERRIDE; |
131 virtual bool CanSeekForward() OVERRIDE; | 138 virtual bool CanSeekForward() OVERRIDE; |
132 virtual bool CanSeekBackward() OVERRIDE; | 139 virtual bool CanSeekBackward() OVERRIDE; |
133 virtual bool IsPlayerReady() OVERRIDE; | 140 virtual bool IsPlayerReady() OVERRIDE; |
134 virtual void OnSeekRequestAck() OVERRIDE; | 141 virtual void OnSeekRequestAck(unsigned seek_request_id) OVERRIDE; |
135 | 142 |
136 // Called when the demuxer is ready. | 143 // Called when the demuxer is ready. |
137 virtual void DemuxerReady( | 144 virtual void DemuxerReady( |
138 const MediaPlayerHostMsg_DemuxerReady_Params& params) OVERRIDE; | 145 const MediaPlayerHostMsg_DemuxerReady_Params& params) OVERRIDE; |
139 | 146 |
140 // Called when the requested data is received from the demuxer. | 147 // Called when the requested data is received from the demuxer. |
141 virtual void ReadFromDemuxerAck( | 148 virtual void ReadFromDemuxerAck( |
142 const MediaPlayerHostMsg_ReadFromDemuxerAck_Params& params) OVERRIDE; | 149 const MediaPlayerHostMsg_ReadFromDemuxerAck_Params& params) OVERRIDE; |
143 | 150 |
144 private: | 151 private: |
(...skipping 10 matching lines...) Expand all Loading... |
155 | 162 |
156 // Called when the decoder finishes its task. | 163 // Called when the decoder finishes its task. |
157 void MediaDecoderCallback( | 164 void MediaDecoderCallback( |
158 bool is_audio, bool decode_succeeded, | 165 bool is_audio, bool decode_succeeded, |
159 const base::TimeDelta& presentation_timestamp, | 166 const base::TimeDelta& presentation_timestamp, |
160 const base::Time& wallclock_time, bool end_of_stream); | 167 const base::Time& wallclock_time, bool end_of_stream); |
161 | 168 |
162 // Handle pending events when all the decoder jobs finished. | 169 // Handle pending events when all the decoder jobs finished. |
163 void ProcessPendingEvents(); | 170 void ProcessPendingEvents(); |
164 | 171 |
| 172 // Helper method to create the decoder jobs. |
| 173 void CreateVideoDecoderJob(); |
| 174 void CreateAudioDecoderJob(); |
| 175 |
165 // Flush the decoders and clean up all the data needs to be decoded. | 176 // Flush the decoders and clean up all the data needs to be decoded. |
166 void ClearDecodingData(); | 177 void ClearDecodingData(); |
167 | 178 |
168 // Called to decoder more data. | 179 // Called to decoder more data. |
169 void DecodeMoreAudio(); | 180 void DecodeMoreAudio(); |
170 void DecodeMoreVideo(); | 181 void DecodeMoreVideo(); |
171 | 182 |
172 // Functions check whether audio/video is present. | 183 // Functions check whether audio/video is present. |
173 bool HasVideo(); | 184 bool HasVideo(); |
174 bool HasAudio(); | 185 bool HasAudio(); |
175 | 186 |
176 enum PendingEventFlags { | 187 enum PendingEventFlags { |
177 NO_EVENT_PENDING = 0, | 188 NO_EVENT_PENDING = 0, |
178 SEEK_EVENT_PENDING = 1 << 0, | 189 SEEK_EVENT_PENDING = 1 << 0, |
179 SURFACE_CHANGE_EVENT_PENDING = 1 << 1, | 190 SURFACE_CHANGE_EVENT_PENDING = 1 << 1, |
| 191 CONFIG_CHANGE_EVENT_PENDING = 1 << 2, |
180 }; | 192 }; |
181 // Pending event that the player needs to do. | 193 // Pending event that the player needs to do. |
182 unsigned pending_event_; | 194 unsigned pending_event_; |
183 | 195 |
184 // Number of active decoding tasks. | 196 // Number of active decoding tasks. |
185 int active_decoding_tasks_; | 197 int active_decoding_tasks_; |
186 | 198 |
| 199 // ID to keep track of whether all the seek requests are acked. |
| 200 unsigned seek_request_id_; |
| 201 |
187 // Stats about the media. | 202 // Stats about the media. |
188 base::TimeDelta duration_; | 203 base::TimeDelta duration_; |
189 int width_; | 204 int width_; |
190 int height_; | 205 int height_; |
191 AudioCodec audio_codec_; | 206 AudioCodec audio_codec_; |
192 VideoCodec video_codec_; | 207 VideoCodec video_codec_; |
193 int num_channels_; | 208 int num_channels_; |
194 int sampling_rate_; | 209 int sampling_rate_; |
195 bool seekable_; | 210 bool seekable_; |
196 base::TimeDelta last_presentation_timestamp_; | 211 base::TimeDelta last_presentation_timestamp_; |
197 std::vector<uint8> audio_extra_data_; | 212 std::vector<uint8> audio_extra_data_; |
198 bool audio_finished_; | 213 bool audio_finished_; |
199 bool video_finished_; | 214 bool video_finished_; |
200 bool playing_; | 215 bool playing_; |
201 | 216 |
202 // Timestamps for providing simple A/V sync. When start decoding an audio | 217 // Timestamps for providing simple A/V sync. When start decoding an audio |
203 // chunk, we record its presentation timestamp and the current system time. | 218 // chunk, we record its presentation timestamp and the current system time. |
204 // Then we use this information to estimate when the next audio/video frame | 219 // Then we use this information to estimate when the next audio/video frame |
205 // should be rendered. | 220 // should be rendered. |
206 // TODO(qinmin): Need to fix the problem if audio/video lagged too far behind | 221 // TODO(qinmin): Need to fix the problem if audio/video lagged too far behind |
207 // due to network or decoding problem. | 222 // due to network or decoding problem. |
208 base::Time start_wallclock_time_; | 223 base::Time start_wallclock_time_; |
209 base::TimeDelta start_presentation_timestamp_; | 224 base::TimeDelta start_presentation_timestamp_; |
210 | 225 |
211 // Decoder jobs. | 226 // The surface object currently owned by the player. |
| 227 gfx::ScopedJavaSurface surface_; |
| 228 |
| 229 // Decoder jobs |
212 ScopedMediaDecoderJob audio_decoder_job_; | 230 ScopedMediaDecoderJob audio_decoder_job_; |
213 ScopedMediaDecoderJob video_decoder_job_; | 231 ScopedMediaDecoderJob video_decoder_job_; |
214 | 232 |
| 233 bool reconfig_audio_decoder_; |
| 234 bool reconfig_video_decoder_; |
| 235 |
215 // These variables keep track of the current decoding data. | 236 // These variables keep track of the current decoding data. |
216 // TODO(qinmin): remove these variables when we no longer relies on IPC for | 237 // TODO(qinmin): remove these variables when we no longer relies on IPC for |
217 // data passing. | 238 // data passing. |
218 size_t audio_access_unit_index_; | 239 size_t audio_access_unit_index_; |
219 size_t video_access_unit_index_; | 240 size_t video_access_unit_index_; |
220 bool waiting_for_audio_data_; | 241 bool waiting_for_audio_data_; |
221 bool waiting_for_video_data_; | 242 bool waiting_for_video_data_; |
222 MediaPlayerHostMsg_ReadFromDemuxerAck_Params received_audio_; | 243 MediaPlayerHostMsg_ReadFromDemuxerAck_Params received_audio_; |
223 MediaPlayerHostMsg_ReadFromDemuxerAck_Params received_video_; | 244 MediaPlayerHostMsg_ReadFromDemuxerAck_Params received_video_; |
224 | 245 |
225 // Whether the video decoder need to use anempty surface. | |
226 bool use_empty_surface_; | |
227 | |
228 // Weak pointer passed to media decoder jobs for callbacks. | 246 // Weak pointer passed to media decoder jobs for callbacks. |
229 base::WeakPtrFactory<MediaSourcePlayer> weak_this_; | 247 base::WeakPtrFactory<MediaSourcePlayer> weak_this_; |
230 | 248 |
231 DISALLOW_COPY_AND_ASSIGN(MediaSourcePlayer); | 249 DISALLOW_COPY_AND_ASSIGN(MediaSourcePlayer); |
232 }; | 250 }; |
233 | 251 |
234 } // namespace media | 252 } // namespace media |
235 | 253 |
236 #endif // MEDIA_BASE_ANDROID_MEDIA_SOURCE_PLAYER_H_ | 254 #endif // MEDIA_BASE_ANDROID_MEDIA_SOURCE_PLAYER_H_ |
OLD | NEW |