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 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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(const 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_; | |
| 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 const base::TimeTicks& start_time_ticks, |
| 113 const base::TimeDelta& start_presentation_timestamp); | 120 const base::TimeDelta& start_presentation_timestamp); |
|
wolenetz
2014/03/17 19:51:00
nit: const & for TimeDelta is not current style. U
qinmin
2014/03/18 18:58:58
Done.
| |
| 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 const base::TimeTicks& start_time_ticks, |
| 123 const base::TimeDelta& start_presentation_timestamp, | 130 const 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 const 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. | |
|
wolenetz
2014/03/17 19:51:00
nit: Describe meaning of |current_chunk|.
qinmin
2014/03/18 18:58:58
Done.
| |
| 145 bool IsDemuxerDataEmpty(bool current_chunk) const; | |
| 146 | |
| 147 // Clearn all the received data. | |
| 148 void ClearData(); | |
| 149 | |
| 150 // Request new data for the current chunk if it runs out of data. | |
| 151 void RequestCurrentChunkIfEmpty(); | |
| 152 | |
| 134 // The UI message loop where callbacks should be dispatched. | 153 // The UI message loop where callbacks should be dispatched. |
| 135 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; | 154 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
| 136 | 155 |
| 137 // The task runner that decoder job runs on. | 156 // The task runner that decoder job runs on. |
| 138 scoped_refptr<base::SingleThreadTaskRunner> decoder_task_runner_; | 157 scoped_refptr<base::SingleThreadTaskRunner> decoder_task_runner_; |
| 139 | 158 |
| 140 // The media codec bridge used for decoding. Owned by derived class. | 159 // The media codec bridge used for decoding. Owned by derived class. |
| 141 // NOTE: This MUST NOT be accessed in the destructor. | 160 // NOTE: This MUST NOT be accessed in the destructor. |
| 142 MediaCodecBridge* media_codec_bridge_; | 161 MediaCodecBridge* media_codec_bridge_; |
| 143 | 162 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 176 | 195 |
| 177 // Callback used to request more data. | 196 // Callback used to request more data. |
| 178 base::Closure request_data_cb_; | 197 base::Closure request_data_cb_; |
| 179 | 198 |
| 180 // Callback to run when new data has been received. | 199 // Callback to run when new data has been received. |
| 181 base::Closure on_data_received_cb_; | 200 base::Closure on_data_received_cb_; |
| 182 | 201 |
| 183 // Callback to run when the current Decode() operation completes. | 202 // Callback to run when the current Decode() operation completes. |
| 184 DecoderCallback decode_cb_; | 203 DecoderCallback decode_cb_; |
| 185 | 204 |
| 186 // The current access unit being processed. | 205 // Data received over IPC from last RequestData() operation. |
| 187 size_t access_unit_index_; | 206 // We keep 2 chunks at the same time to reduce the IPC latency between chunks. |
| 207 // If data inside the current chunk are all decoded, we will request a new | |
| 208 // chunk from the demuxer and swap the current chunk with the other one. | |
| 209 // New data will always be stored in the other chunk since the current | |
| 210 // one may be still in use. | |
| 211 DemuxerData received_data_[2]; | |
| 188 | 212 |
| 189 // Data received over IPC from last RequestData() operation. | 213 // Index to the current data chunk that is being decoded. |
| 190 DemuxerData received_data_; | 214 size_t current_demuxer_data_index_; |
| 215 | |
| 216 // Index to the access unit inside each data chunk that is being decoded. | |
| 217 size_t access_unit_index_[2]; | |
| 191 | 218 |
| 192 // The index of input buffer that can be used by QueueInputBuffer(). | 219 // The index of input buffer that can be used by QueueInputBuffer(). |
| 193 // If the index is uninitialized or invalid, it must be -1. | 220 // If the index is uninitialized or invalid, it must be -1. |
| 194 int input_buf_index_; | 221 int input_buf_index_; |
| 195 | 222 |
| 196 bool stop_decode_pending_; | 223 bool stop_decode_pending_; |
| 197 | 224 |
| 198 // Indicates that this object should be destroyed once the current | 225 // Indicates that this object should be destroyed once the current |
| 199 // Decode() has completed. This gets set when Release() gets called | 226 // Decode() has completed. This gets set when Release() gets called |
| 200 // while there is a decode in progress. | 227 // while there is a decode in progress. |
| 201 bool destroy_pending_; | 228 bool destroy_pending_; |
| 202 | 229 |
| 230 // Indicates whether the decoder is in the middle of requesting new data. | |
| 231 bool is_requesting_demuxer_data_; | |
| 232 | |
| 233 // Indicates whether the incoming data should be ignored. | |
| 234 bool is_incoming_data_invalid_; | |
| 235 | |
| 236 friend class MediaSourcePlayerTest; | |
| 203 DISALLOW_IMPLICIT_CONSTRUCTORS(MediaDecoderJob); | 237 DISALLOW_IMPLICIT_CONSTRUCTORS(MediaDecoderJob); |
| 204 }; | 238 }; |
| 205 | 239 |
| 206 } // namespace media | 240 } // namespace media |
| 207 | 241 |
| 208 #endif // MEDIA_BASE_ANDROID_MEDIA_DECODER_JOB_H_ | 242 #endif // MEDIA_BASE_ANDROID_MEDIA_DECODER_JOB_H_ |
| OLD | NEW |