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