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