Upstream version 6.35.121.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                                    bool supports_get_decode_output)
18     : task_runner_(base::MessageLoopProxy::current()),
19       decoding_delay_(decoding_delay),
20       supports_get_decode_output_(supports_get_decode_output),
21       state_(UNINITIALIZED),
22       total_bytes_decoded_(0),
23       weak_factory_(this) {
24   DCHECK_GE(decoding_delay, 0);
25 }
26
27 FakeVideoDecoder::~FakeVideoDecoder() {
28   DCHECK_EQ(state_, UNINITIALIZED);
29 }
30
31 void FakeVideoDecoder::Initialize(const VideoDecoderConfig& config,
32                                   const PipelineStatusCB& status_cb) {
33   DCHECK(task_runner_->BelongsToCurrentThread());
34   DCHECK(config.IsValidConfig());
35   DCHECK(decode_cb_.IsNull()) << "No reinitialization during pending decode.";
36   DCHECK(reset_cb_.IsNull()) << "No reinitialization during pending reset.";
37
38   current_config_ = config;
39   init_cb_.SetCallback(BindToCurrentLoop(status_cb));
40
41   if (!decoded_frames_.empty()) {
42     DVLOG(1) << "Decoded frames dropped during reinitialization.";
43     decoded_frames_.clear();
44   }
45
46   state_ = NORMAL;
47   init_cb_.RunOrHold(PIPELINE_OK);
48 }
49
50 void FakeVideoDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer,
51                               const DecodeCB& decode_cb) {
52   DCHECK(task_runner_->BelongsToCurrentThread());
53   DCHECK(decode_cb_.IsNull()) << "Overlapping decodes are not supported.";
54   DCHECK(reset_cb_.IsNull());
55   DCHECK_LE(decoded_frames_.size(), static_cast<size_t>(decoding_delay_));
56
57   int buffer_size = buffer->end_of_stream() ? 0 : buffer->data_size();
58   decode_cb_.SetCallback(
59       BindToCurrentLoop(base::Bind(&FakeVideoDecoder::OnFrameDecoded,
60                                    weak_factory_.GetWeakPtr(),
61                                    buffer_size,
62                                    decode_cb)));
63
64   if (buffer->end_of_stream() && decoded_frames_.empty()) {
65     decode_cb_.RunOrHold(kOk, VideoFrame::CreateEOSFrame());
66     return;
67   }
68
69   if (!buffer->end_of_stream()) {
70     DCHECK(VerifyFakeVideoBufferForTest(buffer, current_config_));
71     scoped_refptr<VideoFrame> video_frame = VideoFrame::CreateColorFrame(
72         current_config_.coded_size(), 0, 0, 0, buffer->timestamp());
73     decoded_frames_.push_back(video_frame);
74
75     if (decoded_frames_.size() <= static_cast<size_t>(decoding_delay_)) {
76       decode_cb_.RunOrHold(kNotEnoughData, scoped_refptr<VideoFrame>());
77       return;
78     }
79   }
80
81   scoped_refptr<VideoFrame> frame = decoded_frames_.front();
82   decoded_frames_.pop_front();
83   decode_cb_.RunOrHold(kOk, frame);
84 }
85
86 void FakeVideoDecoder::Reset(const base::Closure& closure) {
87   DCHECK(task_runner_->BelongsToCurrentThread());
88   DCHECK(reset_cb_.IsNull());
89   reset_cb_.SetCallback(BindToCurrentLoop(closure));
90
91   // Defer the reset if a decode is pending.
92   if (!decode_cb_.IsNull())
93     return;
94
95   DoReset();
96 }
97
98 void FakeVideoDecoder::Stop(const base::Closure& closure) {
99   DCHECK(task_runner_->BelongsToCurrentThread());
100   stop_cb_.SetCallback(BindToCurrentLoop(closure));
101
102   // Defer the stop if an init, a decode or a reset is pending.
103   if (!init_cb_.IsNull() || !decode_cb_.IsNull() || !reset_cb_.IsNull())
104     return;
105
106   DoStop();
107 }
108
109 scoped_refptr<VideoFrame> FakeVideoDecoder::GetDecodeOutput() {
110   DCHECK(task_runner_->BelongsToCurrentThread());
111   if (!supports_get_decode_output_ || decoded_frames_.empty())
112     return NULL;
113   scoped_refptr<VideoFrame> out = decoded_frames_.front();
114   decoded_frames_.pop_front();
115   return out;
116 }
117
118 void FakeVideoDecoder::HoldNextInit() {
119   DCHECK(task_runner_->BelongsToCurrentThread());
120   init_cb_.HoldCallback();
121 }
122
123 void FakeVideoDecoder::HoldNextRead() {
124   DCHECK(task_runner_->BelongsToCurrentThread());
125   decode_cb_.HoldCallback();
126 }
127
128 void FakeVideoDecoder::HoldNextReset() {
129   DCHECK(task_runner_->BelongsToCurrentThread());
130   reset_cb_.HoldCallback();
131 }
132
133 void FakeVideoDecoder::HoldNextStop() {
134   DCHECK(task_runner_->BelongsToCurrentThread());
135   stop_cb_.HoldCallback();
136 }
137
138 void FakeVideoDecoder::SatisfyInit() {
139   DCHECK(task_runner_->BelongsToCurrentThread());
140   DCHECK(decode_cb_.IsNull());
141   DCHECK(reset_cb_.IsNull());
142
143   init_cb_.RunHeldCallback();
144
145   if (!stop_cb_.IsNull())
146     DoStop();
147 }
148
149 void FakeVideoDecoder::SatisfyRead() {
150   DCHECK(task_runner_->BelongsToCurrentThread());
151   decode_cb_.RunHeldCallback();
152
153   if (!reset_cb_.IsNull())
154     DoReset();
155
156   if (reset_cb_.IsNull() && !stop_cb_.IsNull())
157     DoStop();
158 }
159
160 void FakeVideoDecoder::SatisfyReset() {
161   DCHECK(task_runner_->BelongsToCurrentThread());
162   DCHECK(decode_cb_.IsNull());
163   reset_cb_.RunHeldCallback();
164
165   if (!stop_cb_.IsNull())
166     DoStop();
167 }
168
169 void FakeVideoDecoder::SatisfyStop() {
170   DCHECK(task_runner_->BelongsToCurrentThread());
171   DCHECK(decode_cb_.IsNull());
172   DCHECK(reset_cb_.IsNull());
173   stop_cb_.RunHeldCallback();
174 }
175
176 void FakeVideoDecoder::DoReset() {
177   DCHECK(task_runner_->BelongsToCurrentThread());
178   DCHECK(decode_cb_.IsNull());
179   DCHECK(!reset_cb_.IsNull());
180
181   decoded_frames_.clear();
182   reset_cb_.RunOrHold();
183 }
184
185 void FakeVideoDecoder::DoStop() {
186   DCHECK(task_runner_->BelongsToCurrentThread());
187   DCHECK(decode_cb_.IsNull());
188   DCHECK(reset_cb_.IsNull());
189   DCHECK(!stop_cb_.IsNull());
190
191   state_ = UNINITIALIZED;
192   decoded_frames_.clear();
193   stop_cb_.RunOrHold();
194 }
195
196 void FakeVideoDecoder::OnFrameDecoded(
197     int buffer_size,
198     const DecodeCB& decode_cb,
199     Status status,
200     const scoped_refptr<VideoFrame>& video_frame) {
201   if (status == kOk || status == kNotEnoughData)
202     total_bytes_decoded_ += buffer_size;
203   decode_cb.Run(status, video_frame);
204 }
205
206 }  // namespace media