| 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 #include "media/filters/fake_video_decoder.h" | 5 #include "media/filters/fake_video_decoder.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
| 11 #include "media/base/bind_to_current_loop.h" | 11 #include "media/base/bind_to_current_loop.h" |
| 12 #include "media/base/test_helpers.h" | 12 #include "media/base/test_helpers.h" |
| 13 | 13 |
| 14 namespace media { | 14 namespace media { |
| 15 | 15 |
| 16 FakeVideoDecoder::FakeVideoDecoder(int decoding_delay, | 16 FakeVideoDecoder::FakeVideoDecoder(int decoding_delay, |
| 17 bool supports_get_decode_output, |
| 17 int max_parallel_decoding_requests) | 18 int max_parallel_decoding_requests) |
| 18 : decoding_delay_(decoding_delay), | 19 : decoding_delay_(decoding_delay), |
| 20 supports_get_decode_output_(supports_get_decode_output), |
| 19 max_parallel_decoding_requests_(max_parallel_decoding_requests), | 21 max_parallel_decoding_requests_(max_parallel_decoding_requests), |
| 20 state_(STATE_UNINITIALIZED), | 22 state_(STATE_UNINITIALIZED), |
| 21 hold_decode_(false), | 23 hold_decode_(false), |
| 22 total_bytes_decoded_(0), | 24 total_bytes_decoded_(0), |
| 23 weak_factory_(this) { | 25 weak_factory_(this) { |
| 24 DCHECK_GE(decoding_delay, 0); | 26 DCHECK_GE(decoding_delay, 0); |
| 25 } | 27 } |
| 26 | 28 |
| 27 FakeVideoDecoder::~FakeVideoDecoder() { | 29 FakeVideoDecoder::~FakeVideoDecoder() { |
| 28 DCHECK_EQ(state_, STATE_UNINITIALIZED); | 30 DCHECK_EQ(state_, STATE_UNINITIALIZED); |
| 29 } | 31 } |
| 30 | 32 |
| 31 void FakeVideoDecoder::Initialize(const VideoDecoderConfig& config, | 33 void FakeVideoDecoder::Initialize(const VideoDecoderConfig& config, |
| 32 bool low_delay, | 34 bool low_delay, |
| 33 const PipelineStatusCB& status_cb, | 35 const PipelineStatusCB& status_cb) { |
| 34 const OutputCB& output_cb) { | |
| 35 DCHECK(thread_checker_.CalledOnValidThread()); | 36 DCHECK(thread_checker_.CalledOnValidThread()); |
| 36 DCHECK(config.IsValidConfig()); | 37 DCHECK(config.IsValidConfig()); |
| 37 DCHECK(held_decode_callbacks_.empty()) | 38 DCHECK(held_decode_callbacks_.empty()) |
| 38 << "No reinitialization during pending decode."; | 39 << "No reinitialization during pending decode."; |
| 39 DCHECK(reset_cb_.IsNull()) << "No reinitialization during pending reset."; | 40 DCHECK(reset_cb_.IsNull()) << "No reinitialization during pending reset."; |
| 40 | 41 |
| 41 current_config_ = config; | 42 current_config_ = config; |
| 42 init_cb_.SetCallback(BindToCurrentLoop(status_cb)); | 43 init_cb_.SetCallback(BindToCurrentLoop(status_cb)); |
| 43 | 44 |
| 44 // Don't need BindToCurrentLoop() because |output_cb_| is only called from | |
| 45 // RunDecodeCallback() which is posted from Decode(). | |
| 46 output_cb_ = output_cb; | |
| 47 | |
| 48 if (!decoded_frames_.empty()) { | 45 if (!decoded_frames_.empty()) { |
| 49 DVLOG(1) << "Decoded frames dropped during reinitialization."; | 46 DVLOG(1) << "Decoded frames dropped during reinitialization."; |
| 50 decoded_frames_.clear(); | 47 decoded_frames_.clear(); |
| 51 } | 48 } |
| 52 | 49 |
| 53 state_ = STATE_NORMAL; | 50 state_ = STATE_NORMAL; |
| 54 init_cb_.RunOrHold(PIPELINE_OK); | 51 init_cb_.RunOrHold(PIPELINE_OK); |
| 55 } | 52 } |
| 56 | 53 |
| 57 void FakeVideoDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer, | 54 void FakeVideoDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer, |
| 58 const DecodeCB& decode_cb) { | 55 const DecodeCB& decode_cb) { |
| 59 DCHECK(thread_checker_.CalledOnValidThread()); | 56 DCHECK(thread_checker_.CalledOnValidThread()); |
| 60 DCHECK(reset_cb_.IsNull()); | 57 DCHECK(reset_cb_.IsNull()); |
| 61 DCHECK_LE(decoded_frames_.size(), | 58 DCHECK_LE(decoded_frames_.size(), |
| 62 decoding_delay_ + held_decode_callbacks_.size()); | 59 decoding_delay_ + held_decode_callbacks_.size()); |
| 63 DCHECK_LT(static_cast<int>(held_decode_callbacks_.size()), | 60 DCHECK_LT(static_cast<int>(held_decode_callbacks_.size()), |
| 64 max_parallel_decoding_requests_); | 61 max_parallel_decoding_requests_); |
| 65 | 62 |
| 66 int buffer_size = buffer->end_of_stream() ? 0 : buffer->data_size(); | 63 int buffer_size = buffer->end_of_stream() ? 0 : buffer->data_size(); |
| 67 DecodeCB wrapped_decode_cb = | 64 DecodeCB wrapped_decode_cb = |
| 68 BindToCurrentLoop(base::Bind(&FakeVideoDecoder::OnFrameDecoded, | 65 BindToCurrentLoop(base::Bind(&FakeVideoDecoder::OnFrameDecoded, |
| 69 weak_factory_.GetWeakPtr(), | 66 weak_factory_.GetWeakPtr(), |
| 70 buffer_size, decode_cb)); | 67 buffer_size, |
| 68 decode_cb)); |
| 71 | 69 |
| 72 if (state_ == STATE_ERROR) { | 70 if (state_ == STATE_ERROR) { |
| 73 wrapped_decode_cb.Run(kDecodeError); | 71 wrapped_decode_cb.Run(kDecodeError, scoped_refptr<VideoFrame>()); |
| 74 return; | 72 return; |
| 75 } | 73 } |
| 76 | 74 |
| 77 if (buffer->end_of_stream()) { | 75 if (buffer->end_of_stream()) { |
| 78 state_ = STATE_END_OF_STREAM; | 76 state_ = STATE_END_OF_STREAM; |
| 79 } else { | 77 } else { |
| 80 DCHECK(VerifyFakeVideoBufferForTest(buffer, current_config_)); | 78 DCHECK(VerifyFakeVideoBufferForTest(buffer, current_config_)); |
| 81 scoped_refptr<VideoFrame> video_frame = VideoFrame::CreateColorFrame( | 79 scoped_refptr<VideoFrame> video_frame = VideoFrame::CreateColorFrame( |
| 82 current_config_.coded_size(), 0, 0, 0, buffer->timestamp()); | 80 current_config_.coded_size(), 0, 0, 0, buffer->timestamp()); |
| 83 decoded_frames_.push_back(video_frame); | 81 decoded_frames_.push_back(video_frame); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 107 SatisfyInit(); | 105 SatisfyInit(); |
| 108 if (!held_decode_callbacks_.empty()) | 106 if (!held_decode_callbacks_.empty()) |
| 109 SatisfyDecode(); | 107 SatisfyDecode(); |
| 110 if (!reset_cb_.IsNull()) | 108 if (!reset_cb_.IsNull()) |
| 111 SatisfyReset(); | 109 SatisfyReset(); |
| 112 | 110 |
| 113 decoded_frames_.clear(); | 111 decoded_frames_.clear(); |
| 114 state_ = STATE_UNINITIALIZED; | 112 state_ = STATE_UNINITIALIZED; |
| 115 } | 113 } |
| 116 | 114 |
| 115 scoped_refptr<VideoFrame> FakeVideoDecoder::GetDecodeOutput() { |
| 116 DCHECK(thread_checker_.CalledOnValidThread()); |
| 117 if (!supports_get_decode_output_ || decoded_frames_.empty()) |
| 118 return NULL; |
| 119 scoped_refptr<VideoFrame> out = decoded_frames_.front(); |
| 120 decoded_frames_.pop_front(); |
| 121 return out; |
| 122 } |
| 123 |
| 117 void FakeVideoDecoder::HoldNextInit() { | 124 void FakeVideoDecoder::HoldNextInit() { |
| 118 DCHECK(thread_checker_.CalledOnValidThread()); | 125 DCHECK(thread_checker_.CalledOnValidThread()); |
| 119 init_cb_.HoldCallback(); | 126 init_cb_.HoldCallback(); |
| 120 } | 127 } |
| 121 | 128 |
| 122 void FakeVideoDecoder::HoldDecode() { | 129 void FakeVideoDecoder::HoldDecode() { |
| 123 DCHECK(thread_checker_.CalledOnValidThread()); | 130 DCHECK(thread_checker_.CalledOnValidThread()); |
| 124 hold_decode_ = true; | 131 hold_decode_ = true; |
| 125 } | 132 } |
| 126 | 133 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 DCHECK(thread_checker_.CalledOnValidThread()); | 171 DCHECK(thread_checker_.CalledOnValidThread()); |
| 165 DCHECK(held_decode_callbacks_.empty()); | 172 DCHECK(held_decode_callbacks_.empty()); |
| 166 reset_cb_.RunHeldCallback(); | 173 reset_cb_.RunHeldCallback(); |
| 167 } | 174 } |
| 168 | 175 |
| 169 void FakeVideoDecoder::SimulateError() { | 176 void FakeVideoDecoder::SimulateError() { |
| 170 DCHECK(thread_checker_.CalledOnValidThread()); | 177 DCHECK(thread_checker_.CalledOnValidThread()); |
| 171 | 178 |
| 172 state_ = STATE_ERROR; | 179 state_ = STATE_ERROR; |
| 173 while (!held_decode_callbacks_.empty()) { | 180 while (!held_decode_callbacks_.empty()) { |
| 174 held_decode_callbacks_.front().Run(kDecodeError); | 181 held_decode_callbacks_.front().Run(kDecodeError, |
| 182 scoped_refptr<VideoFrame>()); |
| 175 held_decode_callbacks_.pop_front(); | 183 held_decode_callbacks_.pop_front(); |
| 176 } | 184 } |
| 177 decoded_frames_.clear(); | 185 decoded_frames_.clear(); |
| 178 } | 186 } |
| 179 | 187 |
| 180 int FakeVideoDecoder::GetMaxDecodeRequests() const { | 188 int FakeVideoDecoder::GetMaxDecodeRequests() const { |
| 181 return max_parallel_decoding_requests_; | 189 return max_parallel_decoding_requests_; |
| 182 } | 190 } |
| 183 | 191 |
| 184 void FakeVideoDecoder::OnFrameDecoded(int buffer_size, | 192 void FakeVideoDecoder::OnFrameDecoded( |
| 185 const DecodeCB& decode_cb, | 193 int buffer_size, |
| 186 Status status) { | 194 const DecodeCB& decode_cb, |
| 195 Status status, |
| 196 const scoped_refptr<VideoFrame>& video_frame) { |
| 187 DCHECK(thread_checker_.CalledOnValidThread()); | 197 DCHECK(thread_checker_.CalledOnValidThread()); |
| 188 | 198 |
| 189 if (status == kOk) | 199 if (status == kOk || status == kNotEnoughData) |
| 190 total_bytes_decoded_ += buffer_size; | 200 total_bytes_decoded_ += buffer_size; |
| 191 decode_cb.Run(status); | 201 decode_cb.Run(status, video_frame); |
| 192 } | 202 } |
| 193 | 203 |
| 194 void FakeVideoDecoder::RunOrHoldDecode(const DecodeCB& decode_cb) { | 204 void FakeVideoDecoder::RunOrHoldDecode(const DecodeCB& decode_cb) { |
| 195 DCHECK(thread_checker_.CalledOnValidThread()); | 205 DCHECK(thread_checker_.CalledOnValidThread()); |
| 196 | 206 |
| 197 if (hold_decode_) { | 207 if (hold_decode_) { |
| 198 held_decode_callbacks_.push_back(decode_cb); | 208 held_decode_callbacks_.push_back(decode_cb); |
| 199 } else { | 209 } else { |
| 200 DCHECK(held_decode_callbacks_.empty()); | 210 DCHECK(held_decode_callbacks_.empty()); |
| 201 RunDecodeCallback(decode_cb); | 211 RunDecodeCallback(decode_cb); |
| 202 } | 212 } |
| 203 } | 213 } |
| 204 | 214 |
| 205 void FakeVideoDecoder::RunDecodeCallback(const DecodeCB& decode_cb) { | 215 void FakeVideoDecoder::RunDecodeCallback(const DecodeCB& decode_cb) { |
| 206 DCHECK(thread_checker_.CalledOnValidThread()); | 216 DCHECK(thread_checker_.CalledOnValidThread()); |
| 207 | 217 |
| 208 if (!reset_cb_.IsNull()) { | 218 if (!reset_cb_.IsNull()) { |
| 209 DCHECK(decoded_frames_.empty()); | 219 DCHECK(decoded_frames_.empty()); |
| 210 decode_cb.Run(kAborted); | 220 decode_cb.Run(kAborted, scoped_refptr<VideoFrame>()); |
| 211 return; | 221 return; |
| 212 } | 222 } |
| 213 | 223 |
| 214 // Make sure we leave decoding_delay_ frames in the queue and also frames for | 224 // Make sure we leave decoding_delay_ frames in the queue and also frames for |
| 215 // all pending decode callbacks, except the current one. | 225 // all pending decode callbacks, except the current one. |
| 216 if (decoded_frames_.size() > | 226 if (decoded_frames_.size() <= |
| 217 decoding_delay_ + held_decode_callbacks_.size()) { | 227 decoding_delay_ + held_decode_callbacks_.size() && |
| 218 output_cb_.Run(decoded_frames_.front()); | 228 state_ != STATE_END_OF_STREAM) { |
| 219 decoded_frames_.pop_front(); | 229 decode_cb.Run(kNotEnoughData, scoped_refptr<VideoFrame>()); |
| 220 } else if (state_ == STATE_END_OF_STREAM) { | 230 return; |
| 221 // Drain the queue if this was the last request in the stream, otherwise | |
| 222 // just pop the last frame from the queue. | |
| 223 if (held_decode_callbacks_.empty()) { | |
| 224 while (!decoded_frames_.empty()) { | |
| 225 output_cb_.Run(decoded_frames_.front()); | |
| 226 decoded_frames_.pop_front(); | |
| 227 } | |
| 228 output_cb_.Run(VideoFrame::CreateEOSFrame()); | |
| 229 } else if (!decoded_frames_.empty()) { | |
| 230 output_cb_.Run(decoded_frames_.front()); | |
| 231 decoded_frames_.pop_front(); | |
| 232 } | |
| 233 } | 231 } |
| 234 | 232 |
| 235 decode_cb.Run(kOk); | 233 scoped_refptr<VideoFrame> frame; |
| 234 if (decoded_frames_.empty()) { |
| 235 DCHECK_EQ(state_, STATE_END_OF_STREAM); |
| 236 frame = VideoFrame::CreateEOSFrame(); |
| 237 } else { |
| 238 frame = decoded_frames_.front(); |
| 239 decoded_frames_.pop_front(); |
| 240 } |
| 241 decode_cb.Run(kOk, frame); |
| 236 } | 242 } |
| 237 | 243 |
| 238 void FakeVideoDecoder::DoReset() { | 244 void FakeVideoDecoder::DoReset() { |
| 239 DCHECK(thread_checker_.CalledOnValidThread()); | 245 DCHECK(thread_checker_.CalledOnValidThread()); |
| 240 DCHECK(held_decode_callbacks_.empty()); | 246 DCHECK(held_decode_callbacks_.empty()); |
| 241 DCHECK(!reset_cb_.IsNull()); | 247 DCHECK(!reset_cb_.IsNull()); |
| 242 | 248 |
| 243 reset_cb_.RunOrHold(); | 249 reset_cb_.RunOrHold(); |
| 244 } | 250 } |
| 245 | 251 |
| 246 } // namespace media | 252 } // namespace media |
| OLD | NEW |