Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / media / filters / fake_video_decoder.cc
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
3 // found in the LICENSE file.
4
5 #include "media/filters/fake_video_decoder.h"
6
7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
9 #include "base/location.h"
10 #include "base/message_loop/message_loop_proxy.h"
11 #include "media/base/bind_to_current_loop.h"
12 #include "media/base/test_helpers.h"
13
14 namespace media {
15
16 FakeVideoDecoder::FakeVideoDecoder(int decoding_delay,
17                                    int max_parallel_decoding_requests)
18     : decoding_delay_(decoding_delay),
19       max_parallel_decoding_requests_(max_parallel_decoding_requests),
20       state_(STATE_UNINITIALIZED),
21       hold_decode_(false),
22       total_bytes_decoded_(0),
23       weak_factory_(this) {
24   DCHECK_GE(decoding_delay, 0);
25 }
26
27 FakeVideoDecoder::~FakeVideoDecoder() {
28   DCHECK(thread_checker_.CalledOnValidThread());
29
30   if (state_ == STATE_UNINITIALIZED)
31     return;
32
33   if (!init_cb_.IsNull())
34     SatisfyInit();
35   if (!held_decode_callbacks_.empty())
36     SatisfyDecode();
37   if (!reset_cb_.IsNull())
38     SatisfyReset();
39
40   decoded_frames_.clear();
41 }
42
43 std::string FakeVideoDecoder::GetDisplayName() const {
44   return "FakeVideoDecoder";
45 }
46
47 void FakeVideoDecoder::Initialize(const VideoDecoderConfig& config,
48                                   bool low_delay,
49                                   const PipelineStatusCB& status_cb,
50                                   const OutputCB& output_cb) {
51   DCHECK(thread_checker_.CalledOnValidThread());
52   DCHECK(config.IsValidConfig());
53   DCHECK(held_decode_callbacks_.empty())
54       << "No reinitialization during pending decode.";
55   DCHECK(reset_cb_.IsNull()) << "No reinitialization during pending reset.";
56
57   current_config_ = config;
58   init_cb_.SetCallback(BindToCurrentLoop(status_cb));
59
60   // Don't need BindToCurrentLoop() because |output_cb_| is only called from
61   // RunDecodeCallback() which is posted from Decode().
62   output_cb_ = output_cb;
63
64   if (!decoded_frames_.empty()) {
65     DVLOG(1) << "Decoded frames dropped during reinitialization.";
66     decoded_frames_.clear();
67   }
68
69   state_ = STATE_NORMAL;
70   init_cb_.RunOrHold(PIPELINE_OK);
71 }
72
73 void FakeVideoDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer,
74                               const DecodeCB& decode_cb) {
75   DCHECK(thread_checker_.CalledOnValidThread());
76   DCHECK(reset_cb_.IsNull());
77   DCHECK_LE(decoded_frames_.size(),
78             decoding_delay_ + held_decode_callbacks_.size());
79   DCHECK_LT(static_cast<int>(held_decode_callbacks_.size()),
80             max_parallel_decoding_requests_);
81   DCHECK_NE(state_, STATE_END_OF_STREAM);
82
83   int buffer_size = buffer->end_of_stream() ? 0 : buffer->data_size();
84   DecodeCB wrapped_decode_cb = base::Bind(&FakeVideoDecoder::OnFrameDecoded,
85                                           weak_factory_.GetWeakPtr(),
86                                           buffer_size,
87                                           BindToCurrentLoop(decode_cb));
88
89   if (state_ == STATE_ERROR) {
90     wrapped_decode_cb.Run(kDecodeError);
91     return;
92   }
93
94   if (buffer->end_of_stream()) {
95     state_ = STATE_END_OF_STREAM;
96   } else {
97     DCHECK(VerifyFakeVideoBufferForTest(buffer, current_config_));
98     scoped_refptr<VideoFrame> video_frame = VideoFrame::CreateColorFrame(
99         current_config_.coded_size(), 0, 0, 0, buffer->timestamp());
100     decoded_frames_.push_back(video_frame);
101   }
102
103   RunOrHoldDecode(wrapped_decode_cb);
104 }
105
106 void FakeVideoDecoder::Reset(const base::Closure& closure) {
107   DCHECK(thread_checker_.CalledOnValidThread());
108   DCHECK(reset_cb_.IsNull());
109
110   reset_cb_.SetCallback(BindToCurrentLoop(closure));
111   decoded_frames_.clear();
112
113   // Defer the reset if a decode is pending.
114   if (!held_decode_callbacks_.empty())
115     return;
116
117   DoReset();
118 }
119
120 void FakeVideoDecoder::HoldNextInit() {
121   DCHECK(thread_checker_.CalledOnValidThread());
122   init_cb_.HoldCallback();
123 }
124
125 void FakeVideoDecoder::HoldDecode() {
126   DCHECK(thread_checker_.CalledOnValidThread());
127   hold_decode_ = true;
128 }
129
130 void FakeVideoDecoder::HoldNextReset() {
131   DCHECK(thread_checker_.CalledOnValidThread());
132   reset_cb_.HoldCallback();
133 }
134
135 void FakeVideoDecoder::SatisfyInit() {
136   DCHECK(thread_checker_.CalledOnValidThread());
137   DCHECK(held_decode_callbacks_.empty());
138   DCHECK(reset_cb_.IsNull());
139
140   init_cb_.RunHeldCallback();
141 }
142
143 void FakeVideoDecoder::SatisfyDecode() {
144   DCHECK(thread_checker_.CalledOnValidThread());
145   DCHECK(hold_decode_);
146
147   hold_decode_ = false;
148
149   while (!held_decode_callbacks_.empty()) {
150     SatisfySingleDecode();
151   }
152 }
153
154 void FakeVideoDecoder::SatisfySingleDecode() {
155   DCHECK(thread_checker_.CalledOnValidThread());
156   DCHECK(!held_decode_callbacks_.empty());
157
158   DecodeCB decode_cb = held_decode_callbacks_.front();
159   held_decode_callbacks_.pop_front();
160   RunDecodeCallback(decode_cb);
161
162   if (!reset_cb_.IsNull() && held_decode_callbacks_.empty())
163     DoReset();
164 }
165
166 void FakeVideoDecoder::SatisfyReset() {
167   DCHECK(thread_checker_.CalledOnValidThread());
168   DCHECK(held_decode_callbacks_.empty());
169   reset_cb_.RunHeldCallback();
170 }
171
172 void FakeVideoDecoder::SimulateError() {
173   DCHECK(thread_checker_.CalledOnValidThread());
174
175   state_ = STATE_ERROR;
176   while (!held_decode_callbacks_.empty()) {
177     held_decode_callbacks_.front().Run(kDecodeError);
178     held_decode_callbacks_.pop_front();
179   }
180   decoded_frames_.clear();
181 }
182
183 int FakeVideoDecoder::GetMaxDecodeRequests() const {
184   return max_parallel_decoding_requests_;
185 }
186
187 void FakeVideoDecoder::OnFrameDecoded(int buffer_size,
188                                       const DecodeCB& decode_cb,
189                                       Status status) {
190   DCHECK(thread_checker_.CalledOnValidThread());
191
192   if (status == kOk)
193     total_bytes_decoded_ += buffer_size;
194   decode_cb.Run(status);
195 }
196
197 void FakeVideoDecoder::RunOrHoldDecode(const DecodeCB& decode_cb) {
198   DCHECK(thread_checker_.CalledOnValidThread());
199
200   if (hold_decode_) {
201     held_decode_callbacks_.push_back(decode_cb);
202   } else {
203     DCHECK(held_decode_callbacks_.empty());
204     RunDecodeCallback(decode_cb);
205   }
206 }
207
208 void FakeVideoDecoder::RunDecodeCallback(const DecodeCB& decode_cb) {
209   DCHECK(thread_checker_.CalledOnValidThread());
210
211   if (!reset_cb_.IsNull()) {
212     DCHECK(decoded_frames_.empty());
213     decode_cb.Run(kAborted);
214     return;
215   }
216
217   // Make sure we leave decoding_delay_ frames in the queue and also frames for
218   // all pending decode callbacks, except the current one.
219   if (decoded_frames_.size() >
220       decoding_delay_ + held_decode_callbacks_.size()) {
221     output_cb_.Run(decoded_frames_.front());
222     decoded_frames_.pop_front();
223   } else if (state_ == STATE_END_OF_STREAM) {
224     // Drain the queue if this was the last request in the stream, otherwise
225     // just pop the last frame from the queue.
226     if (held_decode_callbacks_.empty()) {
227       while (!decoded_frames_.empty()) {
228         output_cb_.Run(decoded_frames_.front());
229         decoded_frames_.pop_front();
230       }
231       state_ = STATE_NORMAL;
232     } else if (!decoded_frames_.empty()) {
233       output_cb_.Run(decoded_frames_.front());
234       decoded_frames_.pop_front();
235     }
236   }
237
238   decode_cb.Run(kOk);
239 }
240
241 void FakeVideoDecoder::DoReset() {
242   DCHECK(thread_checker_.CalledOnValidThread());
243   DCHECK(held_decode_callbacks_.empty());
244   DCHECK(!reset_cb_.IsNull());
245
246   reset_cb_.RunOrHold();
247 }
248
249 }  // namespace media