OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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_DECODER_JOB_H_ | 5 #ifndef MEDIA_BASE_ANDROID_MEDIA_DECODER_JOB_H_ |
6 #define MEDIA_BASE_ANDROID_MEDIA_DECODER_JOB_H_ | 6 #define MEDIA_BASE_ANDROID_MEDIA_DECODER_JOB_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
11 #include "media/base/android/demuxer_stream_player_params.h" | 11 #include "media/base/android/demuxer_stream_player_params.h" |
12 #include "media/base/android/media_codec_bridge.h" | 12 #include "media/base/android/media_codec_bridge.h" |
| 13 #include "ui/gl/android/scoped_java_surface.h" |
13 | 14 |
14 namespace base { | 15 namespace base { |
15 class SingleThreadTaskRunner; | 16 class SingleThreadTaskRunner; |
16 } | 17 } |
17 | 18 |
18 namespace media { | 19 namespace media { |
19 | 20 |
| 21 class MediaDrmBridge; |
| 22 |
20 // Class for managing all the decoding tasks. Each decoding task will be posted | 23 // Class for managing all the decoding tasks. Each decoding task will be posted |
21 // onto the same thread. The thread will be stopped once Stop() is called. | 24 // onto the same thread. The thread will be stopped once Stop() is called. |
22 // Data is stored in 2 chunks. When new data arrives, it is always stored in | 25 // Data is stored in 2 chunks. When new data arrives, it is always stored in |
23 // an inactive chunk. And when the current active chunk becomes empty, a new | 26 // an inactive chunk. And when the current active chunk becomes empty, a new |
24 // data request will be sent to the renderer. | 27 // data request will be sent to the renderer. |
25 class MediaDecoderJob { | 28 class MediaDecoderJob { |
26 public: | 29 public: |
27 struct Deleter { | 30 struct Deleter { |
28 inline void operator()(MediaDecoderJob* ptr) const { ptr->Release(); } | 31 inline void operator()(MediaDecoderJob* ptr) const { ptr->Release(); } |
29 }; | 32 }; |
(...skipping 19 matching lines...) Expand all Loading... |
49 | 52 |
50 // Prefetch so we know the decoder job has data when we call Decode(). | 53 // Prefetch so we know the decoder job has data when we call Decode(). |
51 // |prefetch_cb| - Run when prefetching has completed. | 54 // |prefetch_cb| - Run when prefetching has completed. |
52 void Prefetch(const base::Closure& prefetch_cb); | 55 void Prefetch(const base::Closure& prefetch_cb); |
53 | 56 |
54 // Called by MediaSourcePlayer to decode some data. | 57 // Called by MediaSourcePlayer to decode some data. |
55 // |callback| - Run when decode operation has completed. | 58 // |callback| - Run when decode operation has completed. |
56 // | 59 // |
57 // Returns true if the next decode was started and |callback| will be | 60 // Returns true if the next decode was started and |callback| will be |
58 // called when the decode operation is complete. | 61 // called when the decode operation is complete. |
59 // Returns false if a config change is needed. |callback| is ignored | 62 // Returns false if |media_codec_bridge_| cannot be created. |callback| is |
60 // and will not be called. | 63 // ignored and will not be called. |
61 bool Decode(base::TimeTicks start_time_ticks, | 64 bool Decode(base::TimeTicks start_time_ticks, |
62 base::TimeDelta start_presentation_timestamp, | 65 base::TimeDelta start_presentation_timestamp, |
63 const DecoderCallback& callback); | 66 const DecoderCallback& callback); |
64 | 67 |
65 // Called to stop the last Decode() early. | 68 // Called to stop the last Decode() early. |
66 // If the decoder is in the process of decoding the next frame, then | 69 // If the decoder is in the process of decoding the next frame, then |
67 // this method will just allow the decode to complete as normal. If | 70 // this method will just allow the decode to complete as normal. If |
68 // this object is waiting for a data request to complete, then this method | 71 // this object is waiting for a data request to complete, then this method |
69 // will wait for the data to arrive and then call the |callback| | 72 // will wait for the data to arrive and then call the |callback| |
70 // passed to Decode() with a status of MEDIA_CODEC_STOPPED. This ensures that | 73 // passed to Decode() with a status of MEDIA_CODEC_STOPPED. This ensures that |
71 // the |callback| passed to Decode() is always called and the status | 74 // the |callback| passed to Decode() is always called and the status |
72 // reflects whether data was actually decoded or the decode terminated early. | 75 // reflects whether data was actually decoded or the decode terminated early. |
73 void StopDecode(); | 76 void StopDecode(); |
74 | 77 |
75 // Flush the decoder. | 78 // Flushes the decoder and abandons all the data that is being decoded. |
76 void Flush(); | 79 void Flush(); |
77 | 80 |
78 // Enter prerolling state. The job must not currently be decoding. | 81 // Enter prerolling state. The job must not currently be decoding. |
79 void BeginPrerolling(base::TimeDelta preroll_timestamp); | 82 void BeginPrerolling(base::TimeDelta preroll_timestamp); |
80 | 83 |
81 bool prerolling() const { return prerolling_; } | 84 // Releases all the decoder resources as the current tab is going background. |
| 85 virtual void ReleaseDecoderResources(); |
| 86 |
| 87 // Sets the demuxer configs. Returns true if configs has changed, or false |
| 88 // otherwise. |
| 89 virtual bool SetDemuxerConfigs(const DemuxerConfigs& configs); |
| 90 |
| 91 // Returns whether the decoder has finished decoding all the data. |
| 92 bool OutputEOSReached() const; |
| 93 |
| 94 // Returns true if the audio/video stream is available, implemented by child |
| 95 // classes. |
| 96 virtual bool HasStream() const = 0; |
82 | 97 |
83 bool is_decoding() const { return !decode_cb_.is_null(); } | 98 bool is_decoding() const { return !decode_cb_.is_null(); } |
84 | 99 |
85 bool is_requesting_demuxer_data() const { | 100 void set_drm_bridge(MediaDrmBridge* drm_bridge) { drm_bridge_ = drm_bridge; } |
86 return is_requesting_demuxer_data_; | 101 |
87 } | 102 bool is_content_encrypted() const { return is_content_encrypted_; } |
88 | 103 |
89 protected: | 104 protected: |
| 105 // Creates a new MediaDecoderJob instance. |
| 106 // |decoder_task_runner| - Thread on which the decoder task will run. |
| 107 // |request_data_cb| - Callback to request more data for the decoder. |
| 108 // |on_demuxer_config_changed_cb| - Callback to inform the caller that |
| 109 // demuxer config has changed. |
90 MediaDecoderJob( | 110 MediaDecoderJob( |
91 const scoped_refptr<base::SingleThreadTaskRunner>& decoder_task_runner, | 111 const scoped_refptr<base::SingleThreadTaskRunner>& decoder_task_runner, |
92 MediaCodecBridge* media_codec_bridge, | 112 const base::Closure& request_data_cb, |
93 const base::Closure& request_data_cb); | 113 const base::Closure& on_demuxer_config_changed_cb); |
94 | 114 |
95 // Release the output buffer at index |output_buffer_index| and render it if | 115 // Release the output buffer at index |output_buffer_index| and render it if |
96 // |render_output| is true. Upon completion, |callback| will be called. | 116 // |render_output| is true. Upon completion, |callback| will be called. |
97 virtual void ReleaseOutputBuffer( | 117 virtual void ReleaseOutputBuffer( |
98 int output_buffer_index, | 118 int output_buffer_index, |
99 size_t size, | 119 size_t size, |
100 bool render_output, | 120 bool render_output, |
101 base::TimeDelta current_presentation_timestamp, | 121 base::TimeDelta current_presentation_timestamp, |
102 const ReleaseOutputCompletionCallback& callback) = 0; | 122 const ReleaseOutputCompletionCallback& callback) = 0; |
103 | 123 |
104 // Returns true if the "time to render" needs to be computed for frames in | 124 // Returns true if the "time to render" needs to be computed for frames in |
105 // this decoder job. | 125 // this decoder job. |
106 virtual bool ComputeTimeToRender() const = 0; | 126 virtual bool ComputeTimeToRender() const = 0; |
107 | 127 |
| 128 // Gets MediaCrypto object from |drm_bridge_|. |
| 129 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); |
| 130 |
| 131 // Releases the |media_codec_bridge_|. |
| 132 void ReleaseMediaCodecBridge(); |
| 133 |
| 134 MediaDrmBridge* drm_bridge() { return drm_bridge_; } |
| 135 |
| 136 void set_is_content_encrypted(bool is_content_encrypted) { |
| 137 is_content_encrypted_ = is_content_encrypted; |
| 138 } |
| 139 |
| 140 bool need_to_reconfig_decoder_job_; |
| 141 |
| 142 scoped_ptr<MediaCodecBridge> media_codec_bridge_; |
| 143 |
108 private: | 144 private: |
109 friend class MediaSourcePlayerTest; | 145 friend class MediaSourcePlayerTest; |
110 | 146 |
111 // Causes this instance to be deleted on the thread it is bound to. | 147 // Causes this instance to be deleted on the thread it is bound to. |
112 void Release(); | 148 void Release(); |
113 | 149 |
114 MediaCodecStatus QueueInputBuffer(const AccessUnit& unit); | 150 // Queues an access unit into |media_codec_bridge_|'s input buffer. If |
| 151 // |drain_decoder| is true, the access unit is replaced with an EOS unit so |
| 152 // that we can drain the current buffer in |media_codec_bridge_|. |
| 153 MediaCodecStatus QueueInputBuffer(const AccessUnit& unit, bool drain_decoder); |
115 | 154 |
116 // Returns true if this object has data to decode. | 155 // Returns true if this object has data to decode. |
117 bool HasData() const; | 156 bool HasData() const; |
118 | 157 |
119 // Initiates a request for more data. | 158 // Initiates a request for more data. |
120 // |done_cb| is called when more data is available in |received_data_|. | 159 // |done_cb| is called when more data is available in |received_data_|. |
121 void RequestData(const base::Closure& done_cb); | 160 void RequestData(const base::Closure& done_cb); |
122 | 161 |
123 // Posts a task to start decoding the current access unit in |received_data_|. | 162 // Posts a task to start decoding the current access unit in |received_data_|. |
124 void DecodeCurrentAccessUnit( | 163 void DecodeCurrentAccessUnit( |
125 base::TimeTicks start_time_ticks, | 164 base::TimeTicks start_time_ticks, |
126 base::TimeDelta start_presentation_timestamp); | 165 base::TimeDelta start_presentation_timestamp); |
127 | 166 |
128 // Helper function to decoder data on |thread_|. |unit| contains all the data | 167 // Helper function to decoder data on |thread_|. |unit| contains all the data |
129 // to be decoded. |start_time_ticks| and |start_presentation_timestamp| | 168 // to be decoded. |start_time_ticks| and |start_presentation_timestamp| |
130 // represent the system time and the presentation timestamp when the first | 169 // represent the system time and the presentation timestamp when the first |
131 // frame is rendered. We use these information to estimate when the current | 170 // frame is rendered. We use these information to estimate when the current |
132 // frame should be rendered. If |needs_flush| is true, codec needs to be | 171 // frame should be rendered. If |needs_flush| is true, codec needs to be |
133 // flushed at the beginning of this call. | 172 // flushed at the beginning of this call. |
134 void DecodeInternal(const AccessUnit& unit, | 173 void DecodeInternal(const AccessUnit& unit, |
135 base::TimeTicks start_time_ticks, | 174 base::TimeTicks start_time_ticks, |
136 base::TimeDelta start_presentation_timestamp, | 175 base::TimeDelta start_presentation_timestamp, |
137 bool needs_flush, | 176 bool needs_flush, |
| 177 bool drain_decoder, |
138 const DecoderCallback& callback); | 178 const DecoderCallback& callback); |
139 | 179 |
140 // Called on the UI thread to indicate that one decode cycle has completed. | 180 // Called on the UI thread to indicate that one decode cycle has completed. |
141 // Completes any pending job destruction or any pending decode stop. If | 181 // Completes any pending job destruction or any pending decode stop. If |
142 // destruction was not pending, passes its arguments to |decode_cb_|. | 182 // destruction was not pending, passes its arguments to |decode_cb_|. |
143 void OnDecodeCompleted(MediaCodecStatus status, | 183 void OnDecodeCompleted(MediaCodecStatus status, |
144 base::TimeDelta current_presentation_timestamp, | 184 base::TimeDelta current_presentation_timestamp, |
145 base::TimeDelta max_presentation_timestamp); | 185 base::TimeDelta max_presentation_timestamp); |
146 | 186 |
147 // Helper function to get the current access unit that is being decoded. | 187 // Helper function to get the current access unit that is being decoded. |
148 const AccessUnit& CurrentAccessUnit() const; | 188 const AccessUnit& CurrentAccessUnit() const; |
149 | 189 |
150 // Check whether a chunk has no remaining access units to decode. If | 190 // Checks whether a chunk has no remaining access units to decode. If |
151 // |is_active_chunk| is true, this function returns whether decoder has | 191 // |is_active_chunk| is true, this function returns whether decoder has |
152 // consumed all data in |received_data_[current_demuxer_data_index_]|. | 192 // consumed all data in |received_data_[current_demuxer_data_index_]|. |
153 // Otherwise, it returns whether decoder has consumed all data in the inactive | 193 // Otherwise, it returns whether decoder has consumed all data in the inactive |
154 // chunk. | 194 // chunk. |
155 bool NoAccessUnitsRemainingInChunk(bool is_active_chunk) const; | 195 bool NoAccessUnitsRemainingInChunk(bool is_active_chunk) const; |
156 | 196 |
157 // Clearn all the received data. | 197 // Requests new data for the current chunk if it runs out of data. |
158 void ClearData(); | |
159 | |
160 // Request new data for the current chunk if it runs out of data. | |
161 void RequestCurrentChunkIfEmpty(); | 198 void RequestCurrentChunkIfEmpty(); |
162 | 199 |
163 // Initialize |received_data_| and |access_unit_index_|. | 200 // Initializes |received_data_| and |access_unit_index_|. |
164 void InitializeReceivedData(); | 201 void InitializeReceivedData(); |
165 | 202 |
| 203 void OnDecoderDrained(); |
| 204 |
| 205 // Creates |media_codec_bridge_| for decoding purpose. |
| 206 bool CreateMediaCodecBridge(); |
| 207 |
| 208 // Called when an access unit is consumed by the decoder. |is_config_change| |
| 209 // indicates whether the current access unit is a config change. If it is |
| 210 // true, the next access unit is guarateed to be an I-frame. |
| 211 virtual void CurrentDataConsumed(bool is_config_change) {} |
| 212 |
| 213 // Called when |media_codec_bridge_| is released |
| 214 virtual void OnMediaCodecBridgeReleased() {} |
| 215 |
| 216 // Implemented by the child class to create |media_codec_bridge_| for a |
| 217 // particular stream. |
| 218 virtual bool CreateMediaCodecBridgeInternal() = 0; |
| 219 |
| 220 // Returns true if the |configs| doesn't match the current demuxer configs |
| 221 // the decoder job has. |
| 222 virtual bool IsDemuxerConfigChanged(const DemuxerConfigs& configs) const = 0; |
| 223 |
| 224 // Update the demuxer configs. |
| 225 virtual void UpdateDemuxerConfigs(const DemuxerConfigs& configs) = 0; |
| 226 |
166 // Return the index to |received_data_| that is not currently being decoded. | 227 // Return the index to |received_data_| that is not currently being decoded. |
167 size_t inactive_demuxer_data_index() const { | 228 size_t inactive_demuxer_data_index() const { |
168 return 1 - current_demuxer_data_index_; | 229 return 1 - current_demuxer_data_index_; |
169 } | 230 } |
170 | 231 |
171 // The UI message loop where callbacks should be dispatched. | 232 // The UI message loop where callbacks should be dispatched. |
172 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; | 233 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
173 | 234 |
174 // The task runner that decoder job runs on. | 235 // The task runner that decoder job runs on. |
175 scoped_refptr<base::SingleThreadTaskRunner> decoder_task_runner_; | 236 scoped_refptr<base::SingleThreadTaskRunner> decoder_task_runner_; |
176 | 237 |
177 // The media codec bridge used for decoding. Owned by derived class. | |
178 // NOTE: This MUST NOT be accessed in the destructor. | |
179 MediaCodecBridge* media_codec_bridge_; | |
180 | |
181 // Whether the decoder needs to be flushed. | 238 // Whether the decoder needs to be flushed. |
182 bool needs_flush_; | 239 bool needs_flush_; |
183 | 240 |
184 // Whether input EOS is encountered. | 241 // Whether input EOS is encountered. |
185 // TODO(wolenetz/qinmin): Protect with a lock. See http://crbug.com/320043. | 242 // TODO(wolenetz/qinmin): Protect with a lock. See http://crbug.com/320043. |
186 bool input_eos_encountered_; | 243 bool input_eos_encountered_; |
187 | 244 |
188 // Whether output EOS is encountered. | 245 // Whether output EOS is encountered. |
189 bool output_eos_encountered_; | 246 bool output_eos_encountered_; |
190 | 247 |
(...skipping 12 matching lines...) Expand all Loading... |
203 // Indicates prerolling state. If true, this job has not yet decoded output | 260 // Indicates prerolling state. If true, this job has not yet decoded output |
204 // that it will render, since the most recent of job construction or | 261 // that it will render, since the most recent of job construction or |
205 // BeginPrerolling(). If false, |preroll_timestamp_| has been reached. | 262 // BeginPrerolling(). If false, |preroll_timestamp_| has been reached. |
206 // TODO(qinmin): Comparing access unit's timestamp with |preroll_timestamp_| | 263 // TODO(qinmin): Comparing access unit's timestamp with |preroll_timestamp_| |
207 // is not very accurate. | 264 // is not very accurate. |
208 bool prerolling_; | 265 bool prerolling_; |
209 | 266 |
210 // Callback used to request more data. | 267 // Callback used to request more data. |
211 base::Closure request_data_cb_; | 268 base::Closure request_data_cb_; |
212 | 269 |
| 270 // Callback to notify the caller config has changed. |
| 271 base::Closure on_demuxer_config_changed_cb_; |
| 272 |
213 // Callback to run when new data has been received. | 273 // Callback to run when new data has been received. |
214 base::Closure on_data_received_cb_; | 274 base::Closure on_data_received_cb_; |
215 | 275 |
216 // Callback to run when the current Decode() operation completes. | 276 // Callback to run when the current Decode() operation completes. |
217 DecoderCallback decode_cb_; | 277 DecoderCallback decode_cb_; |
218 | 278 |
219 // Data received over IPC from last RequestData() operation. | 279 // Data received over IPC from last RequestData() operation. |
220 // We keep 2 chunks at the same time to reduce the IPC latency between chunks. | 280 // We keep 2 chunks at the same time to reduce the IPC latency between chunks. |
221 // If data inside the current chunk are all decoded, we will request a new | 281 // If data inside the current chunk are all decoded, we will request a new |
222 // chunk from the demuxer and swap the current chunk with the other one. | 282 // chunk from the demuxer and swap the current chunk with the other one. |
223 // New data will always be stored in the other chunk since the current | 283 // New data will always be stored in the other chunk since the current |
224 // one may be still in use. | 284 // one may be still in use. |
225 DemuxerData received_data_[2]; | 285 DemuxerData received_data_[2]; |
226 | 286 |
227 // Index to the current data chunk that is being decoded. | 287 // Index to the current data chunk that is being decoded. |
228 size_t current_demuxer_data_index_; | 288 size_t current_demuxer_data_index_; |
229 | 289 |
230 // Index to the access unit inside each data chunk that is being decoded. | 290 // Index to the access unit inside each data chunk that is being decoded. |
231 size_t access_unit_index_[2]; | 291 size_t access_unit_index_[2]; |
232 | 292 |
233 // The index of input buffer that can be used by QueueInputBuffer(). | 293 // The index of input buffer that can be used by QueueInputBuffer(). |
234 // If the index is uninitialized or invalid, it must be -1. | 294 // If the index is uninitialized or invalid, it must be -1. |
235 int input_buf_index_; | 295 int input_buf_index_; |
236 | 296 |
| 297 // Indicates whether content is encrypted. |
| 298 bool is_content_encrypted_; |
| 299 |
| 300 // Indicates the decoder job should stop after decoding the current access |
| 301 // unit. |
237 bool stop_decode_pending_; | 302 bool stop_decode_pending_; |
238 | 303 |
239 // Indicates that this object should be destroyed once the current | 304 // Indicates that this object should be destroyed once the current |
240 // Decode() has completed. This gets set when Release() gets called | 305 // Decode() has completed. This gets set when Release() gets called |
241 // while there is a decode in progress. | 306 // while there is a decode in progress. |
242 bool destroy_pending_; | 307 bool destroy_pending_; |
243 | 308 |
244 // Indicates whether the decoder is in the middle of requesting new data. | 309 // Indicates whether the decoder is in the middle of requesting new data. |
245 bool is_requesting_demuxer_data_; | 310 bool is_requesting_demuxer_data_; |
246 | 311 |
247 // Indicates whether the incoming data should be ignored. | 312 // Indicates whether the incoming data should be ignored. |
248 bool is_incoming_data_invalid_; | 313 bool is_incoming_data_invalid_; |
249 | 314 |
250 // Weak pointer passed to media decoder jobs for callbacks. It is bounded to | 315 // Indicates that |media_codec_bridge_| should be released once the current |
251 // the decoder thread. | 316 // Decode() has completed. This gets set when ReleaseDecoderResources() gets |
252 // NOTE: Weak pointers must be invalidated before all other member variables. | 317 // called while there is a decode in progress. |
253 base::WeakPtrFactory<MediaDecoderJob> weak_factory_; | 318 bool release_resources_pending_; |
| 319 |
| 320 // Pointer to a DRM object that will be used for encrypted streams. |
| 321 MediaDrmBridge* drm_bridge_; |
| 322 |
| 323 // Indicates whether |media_codec_bridge_| is in the middle of being drained |
| 324 // due to a config change. |
| 325 bool drain_decoder_; |
254 | 326 |
255 DISALLOW_IMPLICIT_CONSTRUCTORS(MediaDecoderJob); | 327 DISALLOW_IMPLICIT_CONSTRUCTORS(MediaDecoderJob); |
256 }; | 328 }; |
257 | 329 |
258 } // namespace media | 330 } // namespace media |
259 | 331 |
260 #endif // MEDIA_BASE_ANDROID_MEDIA_DECODER_JOB_H_ | 332 #endif // MEDIA_BASE_ANDROID_MEDIA_DECODER_JOB_H_ |
OLD | NEW |