Chromium Code Reviews| 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 | 13 |
| 14 namespace base { | 14 namespace base { |
| 15 class SingleThreadTaskRunner; | 15 class SingleThreadTaskRunner; |
| 16 } | 16 } |
| 17 | 17 |
| 18 namespace media { | 18 namespace media { |
| 19 | 19 |
| 20 // Class for managing all the decoding tasks. Each decoding task will be posted | 20 // 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. | 21 // 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 | |
| 23 // an inactive chunk. And when the current active chunk becomes empty, a new | |
| 24 // data request will be sent to the renderer. | |
| 22 class MediaDecoderJob { | 25 class MediaDecoderJob { |
| 23 public: | 26 public: |
| 24 struct Deleter { | 27 struct Deleter { |
| 25 inline void operator()(MediaDecoderJob* ptr) const { ptr->Release(); } | 28 inline void operator()(MediaDecoderJob* ptr) const { ptr->Release(); } |
| 26 }; | 29 }; |
| 27 | 30 |
| 28 // Callback when a decoder job finishes its work. Args: whether decode | 31 // Callback when a decoder job finishes its work. Args: whether decode |
| 29 // finished successfully, presentation time, audio output bytes. | 32 // finished successfully, presentation time, audio output bytes. |
| 30 // If the presentation time is equal to kNoTimestamp(), the decoder job | 33 // If the presentation time is equal to kNoTimestamp(), the decoder job |
| 31 // skipped rendering of the decoded output and the callback target should | 34 // skipped rendering of the decoded output and the callback target should |
| 32 // update its clock to avoid introducing extra delays to the next frame. | 35 // update its clock to avoid introducing extra delays to the next frame. |
| 33 typedef base::Callback<void(MediaCodecStatus, const base::TimeDelta&, | 36 typedef base::Callback<void(MediaCodecStatus, base::TimeDelta, |
| 34 size_t)> DecoderCallback; | 37 size_t)> DecoderCallback; |
| 35 // Callback when a decoder job finishes releasing the output buffer. | 38 // Callback when a decoder job finishes releasing the output buffer. |
| 36 // Args: audio output bytes, must be 0 for video. | 39 // Args: audio output bytes, must be 0 for video. |
| 37 typedef base::Callback<void(size_t)> ReleaseOutputCompletionCallback; | 40 typedef base::Callback<void(size_t)> ReleaseOutputCompletionCallback; |
| 38 | 41 |
| 39 virtual ~MediaDecoderJob(); | 42 virtual ~MediaDecoderJob(); |
| 40 | 43 |
| 41 // Called by MediaSourcePlayer when more data for this object has arrived. | 44 // Called by MediaSourcePlayer when more data for this object has arrived. |
| 42 void OnDataReceived(const DemuxerData& data); | 45 void OnDataReceived(const DemuxerData& data); |
| 43 | 46 |
| 44 // Prefetch so we know the decoder job has data when we call Decode(). | 47 // Prefetch so we know the decoder job has data when we call Decode(). |
| 45 // |prefetch_cb| - Run when prefetching has completed. | 48 // |prefetch_cb| - Run when prefetching has completed. |
| 46 void Prefetch(const base::Closure& prefetch_cb); | 49 void Prefetch(const base::Closure& prefetch_cb); |
| 47 | 50 |
| 48 // Called by MediaSourcePlayer to decode some data. | 51 // Called by MediaSourcePlayer to decode some data. |
| 49 // |callback| - Run when decode operation has completed. | 52 // |callback| - Run when decode operation has completed. |
| 50 // | 53 // |
| 51 // Returns true if the next decode was started and |callback| will be | 54 // Returns true if the next decode was started and |callback| will be |
| 52 // called when the decode operation is complete. | 55 // called when the decode operation is complete. |
| 53 // Returns false if a config change is needed. |callback| is ignored | 56 // Returns false if a config change is needed. |callback| is ignored |
| 54 // and will not be called. | 57 // and will not be called. |
| 55 bool Decode(const base::TimeTicks& start_time_ticks, | 58 bool Decode(base::TimeTicks start_time_ticks, |
| 56 const base::TimeDelta& start_presentation_timestamp, | 59 base::TimeDelta start_presentation_timestamp, |
| 57 const DecoderCallback& callback); | 60 const DecoderCallback& callback); |
| 58 | 61 |
| 59 // Called to stop the last Decode() early. | 62 // Called to stop the last Decode() early. |
| 60 // If the decoder is in the process of decoding the next frame, then | 63 // If the decoder is in the process of decoding the next frame, then |
| 61 // this method will just allow the decode to complete as normal. If | 64 // this method will just allow the decode to complete as normal. If |
| 62 // this object is waiting for a data request to complete, then this method | 65 // this object is waiting for a data request to complete, then this method |
| 63 // will wait for the data to arrive and then call the |callback| | 66 // will wait for the data to arrive and then call the |callback| |
| 64 // passed to Decode() with a status of MEDIA_CODEC_STOPPED. This ensures that | 67 // passed to Decode() with a status of MEDIA_CODEC_STOPPED. This ensures that |
| 65 // the |callback| passed to Decode() is always called and the status | 68 // the |callback| passed to Decode() is always called and the status |
| 66 // reflects whether data was actually decoded or the decode terminated early. | 69 // reflects whether data was actually decoded or the decode terminated early. |
| 67 void StopDecode(); | 70 void StopDecode(); |
| 68 | 71 |
| 69 // Flush the decoder. | 72 // Flush the decoder. |
| 70 void Flush(); | 73 void Flush(); |
| 71 | 74 |
| 72 // Enter prerolling state. The job must not currently be decoding. | 75 // Enter prerolling state. The job must not currently be decoding. |
| 73 void BeginPrerolling(const base::TimeDelta& preroll_timestamp); | 76 void BeginPrerolling(base::TimeDelta preroll_timestamp); |
| 74 | 77 |
| 75 bool prerolling() const { return prerolling_; } | 78 bool prerolling() const { return prerolling_; } |
| 76 | 79 |
| 77 bool is_decoding() const { return !decode_cb_.is_null(); } | 80 bool is_decoding() const { return !decode_cb_.is_null(); } |
| 78 | 81 |
| 82 bool is_requesting_demuxer_data() const { | |
| 83 return is_requesting_demuxer_data_; | |
|
wolenetz
2014/03/18 21:14:47
nit: Does all of this fit on one line?
qinmin
2014/03/19 02:45:22
just 1 space short :(
On 2014/03/18 21:14:47, wol
wolenetz
2014/03/19 17:26:05
Sorry, my miscalculation (I missed the indentation
| |
| 84 } | |
| 85 | |
| 79 protected: | 86 protected: |
| 80 MediaDecoderJob( | 87 MediaDecoderJob( |
| 81 const scoped_refptr<base::SingleThreadTaskRunner>& decoder_task_runner, | 88 const scoped_refptr<base::SingleThreadTaskRunner>& decoder_task_runner, |
| 82 MediaCodecBridge* media_codec_bridge, | 89 MediaCodecBridge* media_codec_bridge, |
| 83 const base::Closure& request_data_cb); | 90 const base::Closure& request_data_cb); |
| 84 | 91 |
| 85 // Release the output buffer at index |output_buffer_index| and render it if | 92 // Release the output buffer at index |output_buffer_index| and render it if |
| 86 // |render_output| is true. Upon completion, |callback| will be called. | 93 // |render_output| is true. Upon completion, |callback| will be called. |
| 87 virtual void ReleaseOutputBuffer( | 94 virtual void ReleaseOutputBuffer( |
| 88 int output_buffer_index, | 95 int output_buffer_index, |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 100 | 107 |
| 101 MediaCodecStatus QueueInputBuffer(const AccessUnit& unit); | 108 MediaCodecStatus QueueInputBuffer(const AccessUnit& unit); |
| 102 | 109 |
| 103 // Returns true if this object has data to decode. | 110 // Returns true if this object has data to decode. |
| 104 bool HasData() const; | 111 bool HasData() const; |
| 105 | 112 |
| 106 // Initiates a request for more data. | 113 // Initiates a request for more data. |
| 107 // |done_cb| is called when more data is available in |received_data_|. | 114 // |done_cb| is called when more data is available in |received_data_|. |
| 108 void RequestData(const base::Closure& done_cb); | 115 void RequestData(const base::Closure& done_cb); |
| 109 | 116 |
| 110 // Posts a task to start decoding the next access unit in |received_data_|. | 117 // Posts a task to start decoding the current access unit in |received_data_|. |
| 111 void DecodeNextAccessUnit( | 118 void DecodeCurrentAccessUnit( |
| 112 const base::TimeTicks& start_time_ticks, | 119 base::TimeTicks start_time_ticks, |
| 113 const base::TimeDelta& start_presentation_timestamp); | 120 base::TimeDelta start_presentation_timestamp); |
| 114 | 121 |
| 115 // Helper function to decoder data on |thread_|. |unit| contains all the data | 122 // Helper function to decoder data on |thread_|. |unit| contains all the data |
| 116 // to be decoded. |start_time_ticks| and |start_presentation_timestamp| | 123 // to be decoded. |start_time_ticks| and |start_presentation_timestamp| |
| 117 // represent the system time and the presentation timestamp when the first | 124 // represent the system time and the presentation timestamp when the first |
| 118 // frame is rendered. We use these information to estimate when the current | 125 // frame is rendered. We use these information to estimate when the current |
| 119 // frame should be rendered. If |needs_flush| is true, codec needs to be | 126 // frame should be rendered. If |needs_flush| is true, codec needs to be |
| 120 // flushed at the beginning of this call. | 127 // flushed at the beginning of this call. |
| 121 void DecodeInternal(const AccessUnit& unit, | 128 void DecodeInternal(const AccessUnit& unit, |
| 122 const base::TimeTicks& start_time_ticks, | 129 base::TimeTicks start_time_ticks, |
| 123 const base::TimeDelta& start_presentation_timestamp, | 130 base::TimeDelta start_presentation_timestamp, |
| 124 bool needs_flush, | 131 bool needs_flush, |
| 125 const DecoderCallback& callback); | 132 const DecoderCallback& callback); |
| 126 | 133 |
| 127 // Called on the UI thread to indicate that one decode cycle has completed. | 134 // Called on the UI thread to indicate that one decode cycle has completed. |
| 128 // Completes any pending job destruction or any pending decode stop. If | 135 // Completes any pending job destruction or any pending decode stop. If |
| 129 // destruction was not pending, passes its arguments to |decode_cb_|. | 136 // destruction was not pending, passes its arguments to |decode_cb_|. |
| 130 void OnDecodeCompleted(MediaCodecStatus status, | 137 void OnDecodeCompleted(MediaCodecStatus status, |
| 131 const base::TimeDelta& presentation_timestamp, | 138 base::TimeDelta presentation_timestamp, |
| 132 size_t audio_output_bytes); | 139 size_t audio_output_bytes); |
| 133 | 140 |
| 141 // Helper function to get the current access unit that is being decoded. | |
| 142 const AccessUnit& CurrentAccessUnit() const; | |
| 143 | |
| 144 // Check whether a chunk is empty. If |is_active_demuxer_data| is true, this | |
| 145 // function returns whether |received_data_[current_demuxer_data_index_]| | |
| 146 // is empty. Otherwise, it will return whether | |
| 147 // |received_data_[1 - current_demuxer_data_index_]| is empty. | |
| 148 bool IsDemuxerDataEmpty(bool is_active_demuxer_data) const; | |
| 149 | |
| 150 // Clearn all the received data. | |
| 151 void ClearData(); | |
| 152 | |
| 153 // Request new data for the current chunk if it runs out of data. | |
| 154 void RequestCurrentChunkIfEmpty(); | |
| 155 | |
| 156 // Initialize |received_data_| and |access_unit_index_|. | |
| 157 void InitializeReceivedData(); | |
| 158 | |
| 159 // Return the index to |received_data_| that is not currently being decoded. | |
| 160 size_t inactive_demuxer_data_index() const { | |
| 161 return 1 - current_demuxer_data_index_; | |
| 162 } | |
| 163 | |
| 134 // The UI message loop where callbacks should be dispatched. | 164 // The UI message loop where callbacks should be dispatched. |
| 135 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; | 165 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
| 136 | 166 |
| 137 // The task runner that decoder job runs on. | 167 // The task runner that decoder job runs on. |
| 138 scoped_refptr<base::SingleThreadTaskRunner> decoder_task_runner_; | 168 scoped_refptr<base::SingleThreadTaskRunner> decoder_task_runner_; |
| 139 | 169 |
| 140 // The media codec bridge used for decoding. Owned by derived class. | 170 // The media codec bridge used for decoding. Owned by derived class. |
| 141 // NOTE: This MUST NOT be accessed in the destructor. | 171 // NOTE: This MUST NOT be accessed in the destructor. |
| 142 MediaCodecBridge* media_codec_bridge_; | 172 MediaCodecBridge* media_codec_bridge_; |
| 143 | 173 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 176 | 206 |
| 177 // Callback used to request more data. | 207 // Callback used to request more data. |
| 178 base::Closure request_data_cb_; | 208 base::Closure request_data_cb_; |
| 179 | 209 |
| 180 // Callback to run when new data has been received. | 210 // Callback to run when new data has been received. |
| 181 base::Closure on_data_received_cb_; | 211 base::Closure on_data_received_cb_; |
| 182 | 212 |
| 183 // Callback to run when the current Decode() operation completes. | 213 // Callback to run when the current Decode() operation completes. |
| 184 DecoderCallback decode_cb_; | 214 DecoderCallback decode_cb_; |
| 185 | 215 |
| 186 // The current access unit being processed. | 216 // Data received over IPC from last RequestData() operation. |
| 187 size_t access_unit_index_; | 217 // We keep 2 chunks at the same time to reduce the IPC latency between chunks. |
| 218 // If data inside the current chunk are all decoded, we will request a new | |
| 219 // chunk from the demuxer and swap the current chunk with the other one. | |
| 220 // New data will always be stored in the other chunk since the current | |
| 221 // one may be still in use. | |
| 222 DemuxerData received_data_[2]; | |
| 188 | 223 |
| 189 // Data received over IPC from last RequestData() operation. | 224 // Index to the current data chunk that is being decoded. |
| 190 DemuxerData received_data_; | 225 size_t current_demuxer_data_index_; |
| 226 | |
| 227 // Index to the access unit inside each data chunk that is being decoded. | |
| 228 size_t access_unit_index_[2]; | |
| 191 | 229 |
| 192 // The index of input buffer that can be used by QueueInputBuffer(). | 230 // The index of input buffer that can be used by QueueInputBuffer(). |
| 193 // If the index is uninitialized or invalid, it must be -1. | 231 // If the index is uninitialized or invalid, it must be -1. |
| 194 int input_buf_index_; | 232 int input_buf_index_; |
| 195 | 233 |
| 196 bool stop_decode_pending_; | 234 bool stop_decode_pending_; |
| 197 | 235 |
| 198 // Indicates that this object should be destroyed once the current | 236 // Indicates that this object should be destroyed once the current |
| 199 // Decode() has completed. This gets set when Release() gets called | 237 // Decode() has completed. This gets set when Release() gets called |
| 200 // while there is a decode in progress. | 238 // while there is a decode in progress. |
| 201 bool destroy_pending_; | 239 bool destroy_pending_; |
| 202 | 240 |
| 241 // Indicates whether the decoder is in the middle of requesting new data. | |
| 242 bool is_requesting_demuxer_data_; | |
| 243 | |
| 244 // Indicates whether the incoming data should be ignored. | |
| 245 bool is_incoming_data_invalid_; | |
| 246 | |
| 247 friend class MediaSourcePlayerTest; | |
| 203 DISALLOW_IMPLICIT_CONSTRUCTORS(MediaDecoderJob); | 248 DISALLOW_IMPLICIT_CONSTRUCTORS(MediaDecoderJob); |
| 204 }; | 249 }; |
| 205 | 250 |
| 206 } // namespace media | 251 } // namespace media |
| 207 | 252 |
| 208 #endif // MEDIA_BASE_ANDROID_MEDIA_DECODER_JOB_H_ | 253 #endif // MEDIA_BASE_ANDROID_MEDIA_DECODER_JOB_H_ |
| OLD | NEW |