summaryrefslogtreecommitdiff
path: root/media/filters/fake_video_decoder.cc
blob: d5bb03bf6884f2044cbad0464e3ebde5c32aa149 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/filters/fake_video_decoder.h"

#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/location.h"
#include "base/message_loop/message_loop_proxy.h"
#include "media/base/bind_to_current_loop.h"
#include "media/base/test_helpers.h"

namespace media {

FakeVideoDecoder::FakeVideoDecoder(int decoding_delay,
                                   bool supports_get_decode_output)
    : task_runner_(base::MessageLoopProxy::current()),
      weak_factory_(this),
      decoding_delay_(decoding_delay),
      supports_get_decode_output_(supports_get_decode_output),
      state_(UNINITIALIZED),
      total_bytes_decoded_(0) {
  DCHECK_GE(decoding_delay, 0);
}

FakeVideoDecoder::~FakeVideoDecoder() {
  DCHECK_EQ(state_, UNINITIALIZED);
}

void FakeVideoDecoder::Initialize(const VideoDecoderConfig& config,
                                  const PipelineStatusCB& status_cb) {
  DCHECK(task_runner_->BelongsToCurrentThread());
  DCHECK(config.IsValidConfig());
  DCHECK(decode_cb_.IsNull()) << "No reinitialization during pending decode.";
  DCHECK(reset_cb_.IsNull()) << "No reinitialization during pending reset.";

  weak_this_ = weak_factory_.GetWeakPtr();

  current_config_ = config;
  init_cb_.SetCallback(BindToCurrentLoop(status_cb));

  if (!decoded_frames_.empty()) {
    DVLOG(1) << "Decoded frames dropped during reinitialization.";
    decoded_frames_.clear();
  }

  state_ = NORMAL;
  init_cb_.RunOrHold(PIPELINE_OK);
}

void FakeVideoDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer,
                              const DecodeCB& decode_cb) {
  DCHECK(task_runner_->BelongsToCurrentThread());
  DCHECK(decode_cb_.IsNull()) << "Overlapping decodes are not supported.";
  DCHECK(reset_cb_.IsNull());
  DCHECK_LE(decoded_frames_.size(), static_cast<size_t>(decoding_delay_));

  int buffer_size = buffer->end_of_stream() ? 0 : buffer->data_size();
  decode_cb_.SetCallback(BindToCurrentLoop(base::Bind(
      &FakeVideoDecoder::OnFrameDecoded, weak_this_, buffer_size, decode_cb)));

  if (buffer->end_of_stream() && decoded_frames_.empty()) {
    decode_cb_.RunOrHold(kOk, VideoFrame::CreateEOSFrame());
    return;
  }

  if (!buffer->end_of_stream()) {
    DCHECK(VerifyFakeVideoBufferForTest(buffer, current_config_));
    scoped_refptr<VideoFrame> video_frame = VideoFrame::CreateColorFrame(
        current_config_.coded_size(), 0, 0, 0, buffer->timestamp());
    decoded_frames_.push_back(video_frame);

    if (decoded_frames_.size() <= static_cast<size_t>(decoding_delay_)) {
      decode_cb_.RunOrHold(kNotEnoughData, scoped_refptr<VideoFrame>());
      return;
    }
  }

  scoped_refptr<VideoFrame> frame = decoded_frames_.front();
  decoded_frames_.pop_front();
  decode_cb_.RunOrHold(kOk, frame);
}

void FakeVideoDecoder::Reset(const base::Closure& closure) {
  DCHECK(task_runner_->BelongsToCurrentThread());
  DCHECK(reset_cb_.IsNull());
  reset_cb_.SetCallback(BindToCurrentLoop(closure));

  // Defer the reset if a decode is pending.
  if (!decode_cb_.IsNull())
    return;

  DoReset();
}

void FakeVideoDecoder::Stop(const base::Closure& closure) {
  DCHECK(task_runner_->BelongsToCurrentThread());
  stop_cb_.SetCallback(BindToCurrentLoop(closure));

  // Defer the stop if an init, a decode or a reset is pending.
  if (!init_cb_.IsNull() || !decode_cb_.IsNull() || !reset_cb_.IsNull())
    return;

  DoStop();
}

scoped_refptr<VideoFrame> FakeVideoDecoder::GetDecodeOutput() {
  DCHECK(task_runner_->BelongsToCurrentThread());
  if (!supports_get_decode_output_ || decoded_frames_.empty())
    return NULL;
  scoped_refptr<VideoFrame> out = decoded_frames_.front();
  decoded_frames_.pop_front();
  return out;
}

void FakeVideoDecoder::HoldNextInit() {
  DCHECK(task_runner_->BelongsToCurrentThread());
  init_cb_.HoldCallback();
}

void FakeVideoDecoder::HoldNextRead() {
  DCHECK(task_runner_->BelongsToCurrentThread());
  decode_cb_.HoldCallback();
}

void FakeVideoDecoder::HoldNextReset() {
  DCHECK(task_runner_->BelongsToCurrentThread());
  reset_cb_.HoldCallback();
}

void FakeVideoDecoder::HoldNextStop() {
  DCHECK(task_runner_->BelongsToCurrentThread());
  stop_cb_.HoldCallback();
}

void FakeVideoDecoder::SatisfyInit() {
  DCHECK(task_runner_->BelongsToCurrentThread());
  DCHECK(decode_cb_.IsNull());
  DCHECK(reset_cb_.IsNull());

  init_cb_.RunHeldCallback();

  if (!stop_cb_.IsNull())
    DoStop();
}

void FakeVideoDecoder::SatisfyRead() {
  DCHECK(task_runner_->BelongsToCurrentThread());
  decode_cb_.RunHeldCallback();

  if (!reset_cb_.IsNull())
    DoReset();

  if (reset_cb_.IsNull() && !stop_cb_.IsNull())
    DoStop();
}

void FakeVideoDecoder::SatisfyReset() {
  DCHECK(task_runner_->BelongsToCurrentThread());
  DCHECK(decode_cb_.IsNull());
  reset_cb_.RunHeldCallback();

  if (!stop_cb_.IsNull())
    DoStop();
}

void FakeVideoDecoder::SatisfyStop() {
  DCHECK(task_runner_->BelongsToCurrentThread());
  DCHECK(decode_cb_.IsNull());
  DCHECK(reset_cb_.IsNull());
  stop_cb_.RunHeldCallback();
}

void FakeVideoDecoder::DoReset() {
  DCHECK(task_runner_->BelongsToCurrentThread());
  DCHECK(decode_cb_.IsNull());
  DCHECK(!reset_cb_.IsNull());

  decoded_frames_.clear();
  reset_cb_.RunOrHold();
}

void FakeVideoDecoder::DoStop() {
  DCHECK(task_runner_->BelongsToCurrentThread());
  DCHECK(decode_cb_.IsNull());
  DCHECK(reset_cb_.IsNull());
  DCHECK(!stop_cb_.IsNull());

  state_ = UNINITIALIZED;
  decoded_frames_.clear();
  stop_cb_.RunOrHold();
}

void FakeVideoDecoder::OnFrameDecoded(
    int buffer_size,
    const DecodeCB& decode_cb,
    Status status,
    const scoped_refptr<VideoFrame>& video_frame) {
  if (status == kOk || status == kNotEnoughData)
    total_bytes_decoded_ += buffer_size;
  decode_cb.Run(status, video_frame);
}

}  // namespace media