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 bool supports_get_decode_output, |
18 : task_runner_(base::MessageLoopProxy::current()), | 18 int max_parallel_decoding_requests) |
19 decoding_delay_(decoding_delay), | 19 : decoding_delay_(decoding_delay), |
20 supports_get_decode_output_(supports_get_decode_output), | 20 supports_get_decode_output_(supports_get_decode_output), |
21 state_(UNINITIALIZED), | 21 max_parallel_decoding_requests_(max_parallel_decoding_requests), |
| 22 state_(STATE_UNINITIALIZED), |
| 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_, 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 DCHECK(task_runner_->BelongsToCurrentThread()); | 36 DCHECK(thread_checker_.CalledOnValidThread()); |
35 DCHECK(config.IsValidConfig()); | 37 DCHECK(config.IsValidConfig()); |
36 DCHECK(decode_cb_.IsNull()) << "No reinitialization during pending decode."; | 38 DCHECK(held_decode_callbacks_.empty()) |
| 39 << "No reinitialization during pending decode."; |
37 DCHECK(reset_cb_.IsNull()) << "No reinitialization during pending reset."; | 40 DCHECK(reset_cb_.IsNull()) << "No reinitialization during pending reset."; |
38 | 41 |
39 current_config_ = config; | 42 current_config_ = config; |
40 init_cb_.SetCallback(BindToCurrentLoop(status_cb)); | 43 init_cb_.SetCallback(BindToCurrentLoop(status_cb)); |
41 | 44 |
42 if (!decoded_frames_.empty()) { | 45 if (!decoded_frames_.empty()) { |
43 DVLOG(1) << "Decoded frames dropped during reinitialization."; | 46 DVLOG(1) << "Decoded frames dropped during reinitialization."; |
44 decoded_frames_.clear(); | 47 decoded_frames_.clear(); |
45 } | 48 } |
46 | 49 |
47 state_ = NORMAL; | 50 state_ = STATE_NORMAL; |
48 init_cb_.RunOrHold(PIPELINE_OK); | 51 init_cb_.RunOrHold(PIPELINE_OK); |
49 } | 52 } |
50 | 53 |
51 void FakeVideoDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer, | 54 void FakeVideoDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer, |
52 const DecodeCB& decode_cb) { | 55 const DecodeCB& decode_cb) { |
53 DCHECK(task_runner_->BelongsToCurrentThread()); | 56 DCHECK(thread_checker_.CalledOnValidThread()); |
54 DCHECK(decode_cb_.IsNull()) << "Overlapping decodes are not supported."; | |
55 DCHECK(reset_cb_.IsNull()); | 57 DCHECK(reset_cb_.IsNull()); |
56 DCHECK_LE(decoded_frames_.size(), static_cast<size_t>(decoding_delay_)); | 58 DCHECK_LE(decoded_frames_.size(), |
| 59 decoding_delay_ + held_decode_callbacks_.size()); |
| 60 DCHECK_LT(static_cast<int>(held_decode_callbacks_.size()), |
| 61 max_parallel_decoding_requests_); |
57 | 62 |
58 int buffer_size = buffer->end_of_stream() ? 0 : buffer->data_size(); | 63 int buffer_size = buffer->end_of_stream() ? 0 : buffer->data_size(); |
59 decode_cb_.SetCallback( | 64 DecodeCB wrapped_decode_cb = |
60 BindToCurrentLoop(base::Bind(&FakeVideoDecoder::OnFrameDecoded, | 65 BindToCurrentLoop(base::Bind(&FakeVideoDecoder::OnFrameDecoded, |
61 weak_factory_.GetWeakPtr(), | 66 weak_factory_.GetWeakPtr(), |
62 buffer_size, | 67 buffer_size, |
63 decode_cb))); | 68 decode_cb)); |
64 | 69 |
65 if (buffer->end_of_stream() && decoded_frames_.empty()) { | 70 if (state_ == STATE_ERROR) { |
66 decode_cb_.RunOrHold(kOk, VideoFrame::CreateEOSFrame()); | 71 wrapped_decode_cb.Run(kDecodeError, scoped_refptr<VideoFrame>()); |
67 return; | 72 return; |
68 } | 73 } |
69 | 74 |
70 if (!buffer->end_of_stream()) { | 75 if (buffer->end_of_stream()) { |
| 76 state_ = STATE_END_OF_STREAM; |
| 77 } else { |
71 DCHECK(VerifyFakeVideoBufferForTest(buffer, current_config_)); | 78 DCHECK(VerifyFakeVideoBufferForTest(buffer, current_config_)); |
72 scoped_refptr<VideoFrame> video_frame = VideoFrame::CreateColorFrame( | 79 scoped_refptr<VideoFrame> video_frame = VideoFrame::CreateColorFrame( |
73 current_config_.coded_size(), 0, 0, 0, buffer->timestamp()); | 80 current_config_.coded_size(), 0, 0, 0, buffer->timestamp()); |
74 decoded_frames_.push_back(video_frame); | 81 decoded_frames_.push_back(video_frame); |
75 | |
76 if (decoded_frames_.size() <= static_cast<size_t>(decoding_delay_)) { | |
77 decode_cb_.RunOrHold(kNotEnoughData, scoped_refptr<VideoFrame>()); | |
78 return; | |
79 } | |
80 } | 82 } |
81 | 83 |
82 scoped_refptr<VideoFrame> frame = decoded_frames_.front(); | 84 RunOrHoldDecode(wrapped_decode_cb); |
83 decoded_frames_.pop_front(); | |
84 decode_cb_.RunOrHold(kOk, frame); | |
85 } | 85 } |
86 | 86 |
87 void FakeVideoDecoder::Reset(const base::Closure& closure) { | 87 void FakeVideoDecoder::Reset(const base::Closure& closure) { |
88 DCHECK(task_runner_->BelongsToCurrentThread()); | 88 DCHECK(thread_checker_.CalledOnValidThread()); |
89 DCHECK(reset_cb_.IsNull()); | 89 DCHECK(reset_cb_.IsNull()); |
| 90 |
90 reset_cb_.SetCallback(BindToCurrentLoop(closure)); | 91 reset_cb_.SetCallback(BindToCurrentLoop(closure)); |
| 92 decoded_frames_.clear(); |
91 | 93 |
92 // Defer the reset if a decode is pending. | 94 // Defer the reset if a decode is pending. |
93 if (!decode_cb_.IsNull()) | 95 if (!held_decode_callbacks_.empty()) |
94 return; | 96 return; |
95 | 97 |
96 DoReset(); | 98 DoReset(); |
97 } | 99 } |
98 | 100 |
99 void FakeVideoDecoder::Stop() { | 101 void FakeVideoDecoder::Stop() { |
100 DCHECK(task_runner_->BelongsToCurrentThread()); | 102 DCHECK(thread_checker_.CalledOnValidThread()); |
101 | 103 |
102 if (!init_cb_.IsNull()) | 104 if (!init_cb_.IsNull()) |
103 SatisfyInit(); | 105 SatisfyInit(); |
104 if (!decode_cb_.IsNull()) | 106 if (!held_decode_callbacks_.empty()) |
105 SatisfyDecode(); | 107 SatisfyDecode(); |
106 if (!reset_cb_.IsNull()) | 108 if (!reset_cb_.IsNull()) |
107 SatisfyReset(); | 109 SatisfyReset(); |
108 | 110 |
109 decoded_frames_.clear(); | 111 decoded_frames_.clear(); |
110 state_ = UNINITIALIZED; | 112 state_ = STATE_UNINITIALIZED; |
111 } | 113 } |
112 | 114 |
113 scoped_refptr<VideoFrame> FakeVideoDecoder::GetDecodeOutput() { | 115 scoped_refptr<VideoFrame> FakeVideoDecoder::GetDecodeOutput() { |
114 DCHECK(task_runner_->BelongsToCurrentThread()); | 116 DCHECK(thread_checker_.CalledOnValidThread()); |
115 if (!supports_get_decode_output_ || decoded_frames_.empty()) | 117 if (!supports_get_decode_output_ || decoded_frames_.empty()) |
116 return NULL; | 118 return NULL; |
117 scoped_refptr<VideoFrame> out = decoded_frames_.front(); | 119 scoped_refptr<VideoFrame> out = decoded_frames_.front(); |
118 decoded_frames_.pop_front(); | 120 decoded_frames_.pop_front(); |
119 return out; | 121 return out; |
120 } | 122 } |
121 | 123 |
122 void FakeVideoDecoder::HoldNextInit() { | 124 void FakeVideoDecoder::HoldNextInit() { |
123 DCHECK(task_runner_->BelongsToCurrentThread()); | 125 DCHECK(thread_checker_.CalledOnValidThread()); |
124 init_cb_.HoldCallback(); | 126 init_cb_.HoldCallback(); |
125 } | 127 } |
126 | 128 |
127 void FakeVideoDecoder::HoldNextDecode() { | 129 void FakeVideoDecoder::HoldDecode() { |
128 DCHECK(task_runner_->BelongsToCurrentThread()); | 130 DCHECK(thread_checker_.CalledOnValidThread()); |
129 decode_cb_.HoldCallback(); | 131 hold_decode_ = true; |
130 } | 132 } |
131 | 133 |
132 void FakeVideoDecoder::HoldNextReset() { | 134 void FakeVideoDecoder::HoldNextReset() { |
133 DCHECK(task_runner_->BelongsToCurrentThread()); | 135 DCHECK(thread_checker_.CalledOnValidThread()); |
134 reset_cb_.HoldCallback(); | 136 reset_cb_.HoldCallback(); |
135 } | 137 } |
136 | 138 |
137 void FakeVideoDecoder::SatisfyInit() { | 139 void FakeVideoDecoder::SatisfyInit() { |
138 DCHECK(task_runner_->BelongsToCurrentThread()); | 140 DCHECK(thread_checker_.CalledOnValidThread()); |
139 DCHECK(decode_cb_.IsNull()); | 141 DCHECK(held_decode_callbacks_.empty()); |
140 DCHECK(reset_cb_.IsNull()); | 142 DCHECK(reset_cb_.IsNull()); |
141 | 143 |
142 init_cb_.RunHeldCallback(); | 144 init_cb_.RunHeldCallback(); |
143 } | 145 } |
144 | 146 |
145 void FakeVideoDecoder::SatisfyDecode() { | 147 void FakeVideoDecoder::SatisfyDecode() { |
146 DCHECK(task_runner_->BelongsToCurrentThread()); | 148 DCHECK(thread_checker_.CalledOnValidThread()); |
147 decode_cb_.RunHeldCallback(); | 149 DCHECK(hold_decode_); |
148 | 150 |
149 if (!reset_cb_.IsNull()) | 151 hold_decode_ = false; |
| 152 |
| 153 while (!held_decode_callbacks_.empty()) { |
| 154 SatisfySingleDecode(); |
| 155 } |
| 156 } |
| 157 |
| 158 void FakeVideoDecoder::SatisfySingleDecode() { |
| 159 DCHECK(thread_checker_.CalledOnValidThread()); |
| 160 DCHECK(!held_decode_callbacks_.empty()); |
| 161 |
| 162 DecodeCB decode_cb = held_decode_callbacks_.front(); |
| 163 held_decode_callbacks_.pop_front(); |
| 164 RunDecodeCallback(decode_cb); |
| 165 |
| 166 if (!reset_cb_.IsNull() && held_decode_callbacks_.empty()) |
150 DoReset(); | 167 DoReset(); |
151 } | 168 } |
152 | 169 |
153 void FakeVideoDecoder::SatisfyReset() { | 170 void FakeVideoDecoder::SatisfyReset() { |
154 DCHECK(task_runner_->BelongsToCurrentThread()); | 171 DCHECK(thread_checker_.CalledOnValidThread()); |
155 DCHECK(decode_cb_.IsNull()); | 172 DCHECK(held_decode_callbacks_.empty()); |
156 reset_cb_.RunHeldCallback(); | 173 reset_cb_.RunHeldCallback(); |
157 } | 174 } |
158 | 175 |
159 void FakeVideoDecoder::DoReset() { | 176 void FakeVideoDecoder::SimulateError() { |
160 DCHECK(task_runner_->BelongsToCurrentThread()); | 177 DCHECK(thread_checker_.CalledOnValidThread()); |
161 DCHECK(decode_cb_.IsNull()); | |
162 DCHECK(!reset_cb_.IsNull()); | |
163 | 178 |
| 179 state_ = STATE_ERROR; |
| 180 while (!held_decode_callbacks_.empty()) { |
| 181 held_decode_callbacks_.front().Run(kDecodeError, |
| 182 scoped_refptr<VideoFrame>()); |
| 183 held_decode_callbacks_.pop_front(); |
| 184 } |
164 decoded_frames_.clear(); | 185 decoded_frames_.clear(); |
165 reset_cb_.RunOrHold(); | 186 } |
| 187 |
| 188 int FakeVideoDecoder::GetMaxDecodeRequests() const { |
| 189 return max_parallel_decoding_requests_; |
166 } | 190 } |
167 | 191 |
168 void FakeVideoDecoder::OnFrameDecoded( | 192 void FakeVideoDecoder::OnFrameDecoded( |
169 int buffer_size, | 193 int buffer_size, |
170 const DecodeCB& decode_cb, | 194 const DecodeCB& decode_cb, |
171 Status status, | 195 Status status, |
172 const scoped_refptr<VideoFrame>& video_frame) { | 196 const scoped_refptr<VideoFrame>& video_frame) { |
| 197 DCHECK(thread_checker_.CalledOnValidThread()); |
| 198 |
173 if (status == kOk || status == kNotEnoughData) | 199 if (status == kOk || status == kNotEnoughData) |
174 total_bytes_decoded_ += buffer_size; | 200 total_bytes_decoded_ += buffer_size; |
175 decode_cb.Run(status, video_frame); | 201 decode_cb.Run(status, video_frame); |
176 } | 202 } |
177 | 203 |
| 204 void FakeVideoDecoder::RunOrHoldDecode(const DecodeCB& decode_cb) { |
| 205 DCHECK(thread_checker_.CalledOnValidThread()); |
| 206 |
| 207 if (hold_decode_) { |
| 208 held_decode_callbacks_.push_back(decode_cb); |
| 209 } else { |
| 210 DCHECK(held_decode_callbacks_.empty()); |
| 211 RunDecodeCallback(decode_cb); |
| 212 } |
| 213 } |
| 214 |
| 215 void FakeVideoDecoder::RunDecodeCallback(const DecodeCB& decode_cb) { |
| 216 DCHECK(thread_checker_.CalledOnValidThread()); |
| 217 |
| 218 if (!reset_cb_.IsNull()) { |
| 219 DCHECK(decoded_frames_.empty()); |
| 220 decode_cb.Run(kAborted, scoped_refptr<VideoFrame>()); |
| 221 return; |
| 222 } |
| 223 |
| 224 // Make sure we leave decoding_delay_ frames in the queue and also frames for |
| 225 // all pending decode callbacks, except the current one. |
| 226 if (decoded_frames_.size() <= |
| 227 decoding_delay_ + held_decode_callbacks_.size() && |
| 228 state_ != STATE_END_OF_STREAM) { |
| 229 decode_cb.Run(kNotEnoughData, scoped_refptr<VideoFrame>()); |
| 230 return; |
| 231 } |
| 232 |
| 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); |
| 242 } |
| 243 |
| 244 void FakeVideoDecoder::DoReset() { |
| 245 DCHECK(thread_checker_.CalledOnValidThread()); |
| 246 DCHECK(held_decode_callbacks_.empty()); |
| 247 DCHECK(!reset_cb_.IsNull()); |
| 248 |
| 249 reset_cb_.RunOrHold(); |
| 250 } |
| 251 |
178 } // namespace media | 252 } // namespace media |
OLD | NEW |