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
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319

media / filters / fake_video_decoder.cc [blame]

// Copyright 2013 The Chromium Authors
// 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/location.h"
#include "base/task/bind_post_task.h"
#include "media/base/test_helpers.h"

namespace media {

FakeVideoDecoder::FakeVideoDecoder(int decoder_id,
                                   int decoding_delay,
                                   int max_parallel_decoding_requests,
                                   const BytesDecodedCB& bytes_decoded_cb)
    : decoder_id_(decoder_id),
      decoding_delay_(decoding_delay),
      max_parallel_decoding_requests_(max_parallel_decoding_requests),
      bytes_decoded_cb_(bytes_decoded_cb),
      state_(STATE_UNINITIALIZED),
      hold_decode_(false),
      total_bytes_decoded_(0),
      fail_to_initialize_(false) {
  DETACH_FROM_SEQUENCE(sequence_checker_);
  DVLOG(1) << decoder_id_ << ": " << __func__;
  DCHECK_GE(decoding_delay, 0);
}

FakeVideoDecoder::~FakeVideoDecoder() {
  DVLOG(1) << decoder_id_ << ": " << __func__;
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (state_ == STATE_UNINITIALIZED)
    return;

  if (!init_cb_.IsNull())
    SatisfyInit();
  if (!held_decode_callbacks_.empty())
    SatisfyDecode();
  if (!reset_cb_.IsNull())
    SatisfyReset();

  decoded_frames_.clear();
  total_decoded_frames_ = 0;
}

void FakeVideoDecoder::EnableEncryptedConfigSupport() {
  supports_encrypted_config_ = true;
}

void FakeVideoDecoder::SetIsPlatformDecoder(bool value) {
  is_platform_decoder_ = value;
}

base::WeakPtr<FakeVideoDecoder> FakeVideoDecoder::GetWeakPtr() {
  return weak_factory_.GetWeakPtr();
}

bool FakeVideoDecoder::SupportsDecryption() const {
  return supports_encrypted_config_;
}

bool FakeVideoDecoder::IsPlatformDecoder() const {
  return is_platform_decoder_;
}

VideoDecoderType FakeVideoDecoder::GetDecoderType() const {
  return VideoDecoderType::kTesting;
}

void FakeVideoDecoder::Initialize(const VideoDecoderConfig& config,
                                  bool low_delay,
                                  CdmContext* cdm_context,
                                  InitCB init_cb,
                                  const OutputCB& output_cb,
                                  const WaitingCB& waiting_cb) {
  DVLOG(1) << decoder_id_ << ": " << __func__;
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(config.IsValidConfig());
  DCHECK(held_decode_callbacks_.empty())
      << "No reinitialization during pending decode.";
  DCHECK(reset_cb_.IsNull()) << "No reinitialization during pending reset.";

  current_config_ = config;
  init_cb_.SetCallback(base::BindPostTaskToCurrentDefault(std::move(init_cb)));

  // Don't need base::BindPostTaskToCurrentDefault() because |output_cb_| is
  // only called from RunDecodeCallback() which is posted from Decode().
  output_cb_ = output_cb;

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

  if (config.is_encrypted() && (!supports_encrypted_config_ || !cdm_context)) {
    DVLOG(1) << "Encrypted config not supported.";
    state_ = STATE_NORMAL;
    init_cb_.RunOrHold(DecoderStatus::Codes::kUnsupportedEncryptionMode);
    return;
  }

  if (fail_to_initialize_) {
    DVLOG(1) << decoder_id_ << ": Initialization failed.";
    state_ = STATE_ERROR;
    init_cb_.RunOrHold(DecoderStatus::Codes::kFailed);
  } else {
    DVLOG(1) << decoder_id_ << ": Initialization succeeded.";
    state_ = STATE_NORMAL;
    init_cb_.RunOrHold(DecoderStatus::Codes::kOk);
  }
}

void FakeVideoDecoder::Decode(scoped_refptr<DecoderBuffer> buffer,
                              DecodeCB decode_cb) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(reset_cb_.IsNull());
  DCHECK_LE(decoded_frames_.size(),
            decoding_delay_ + held_decode_callbacks_.size());
  DCHECK_LT(static_cast<int>(held_decode_callbacks_.size()),
            max_parallel_decoding_requests_);
  DCHECK_NE(state_, STATE_END_OF_STREAM);

  int buffer_size = buffer->end_of_stream() ? 0 : buffer->size();
  DecodeCB wrapped_decode_cb = base::BindOnce(
      &FakeVideoDecoder::OnFrameDecoded, weak_factory_.GetWeakPtr(),
      buffer_size, base::BindPostTaskToCurrentDefault(std::move(decode_cb)));

  if (state_ == STATE_ERROR) {
    std::move(wrapped_decode_cb).Run(DecoderStatus::Codes::kFailed);
    return;
  }

  if (buffer->end_of_stream()) {
    if (buffer->next_config()) {
      eos_next_configs_.emplace_back(
          absl::get<VideoDecoderConfig>(*buffer->next_config()));

      if (enable_eliding_eos_) {
        DCHECK(held_decode_callbacks_.empty());
        current_config_ = eos_next_configs_.back();
        std::move(wrapped_decode_cb)
            .Run(DecoderStatus::Codes::kElidedEndOfStreamForConfigChange);
        return;
      }
    }

    state_ = STATE_END_OF_STREAM;
  } else {
    DCHECK(VerifyFakeVideoBufferForTest(*buffer, current_config_));
    decoded_frames_.push_back(MakeVideoFrame(*buffer));
  }

  RunOrHoldDecode(std::move(wrapped_decode_cb));
}

scoped_refptr<VideoFrame> FakeVideoDecoder::MakeVideoFrame(
    const DecoderBuffer& buffer) {
  auto frame = VideoFrame::CreateVideoHoleFrame(base::UnguessableToken(),
                                                current_config_.coded_size(),
                                                buffer.timestamp());
  DCHECK(frame);
  return frame;
}

void FakeVideoDecoder::Reset(base::OnceClosure closure) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(reset_cb_.IsNull());

  reset_cb_.SetCallback(base::BindPostTaskToCurrentDefault(std::move(closure)));
  decoded_frames_.clear();

  // Defer the reset if a decode is pending.
  if (!held_decode_callbacks_.empty())
    return;

  DoReset();
}

void FakeVideoDecoder::HoldNextInit() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  init_cb_.HoldCallback();
}

void FakeVideoDecoder::HoldDecode() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  hold_decode_ = true;
}

void FakeVideoDecoder::HoldNextReset() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  reset_cb_.HoldCallback();
}

void FakeVideoDecoder::SatisfyInit() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(held_decode_callbacks_.empty());
  DCHECK(reset_cb_.IsNull());

  init_cb_.RunHeldCallback();
}

void FakeVideoDecoder::SatisfyDecode() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(hold_decode_);

  hold_decode_ = false;

  while (!held_decode_callbacks_.empty()) {
    SatisfySingleDecode();
  }
}

void FakeVideoDecoder::SatisfySingleDecode() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(!held_decode_callbacks_.empty());

  DecodeCB decode_cb = std::move(held_decode_callbacks_.front());
  held_decode_callbacks_.pop_front();
  total_decoded_frames_++;
  RunDecodeCallback(std::move(decode_cb));

  if (!reset_cb_.IsNull() && held_decode_callbacks_.empty())
    DoReset();
}

void FakeVideoDecoder::SatisfyReset() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(held_decode_callbacks_.empty());
  reset_cb_.RunHeldCallback();
}

void FakeVideoDecoder::SimulateError() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  state_ = STATE_ERROR;
  while (!held_decode_callbacks_.empty()) {
    std::move(held_decode_callbacks_.front())
        .Run(DecoderStatus::Codes::kFailed);
    held_decode_callbacks_.pop_front();
  }
  decoded_frames_.clear();
}

void FakeVideoDecoder::SimulateFailureToInit() {
  fail_to_initialize_ = true;
}

int FakeVideoDecoder::GetMaxDecodeRequests() const {
  return max_parallel_decoding_requests_;
}

void FakeVideoDecoder::OnFrameDecoded(int buffer_size,
                                      DecodeCB decode_cb,
                                      DecoderStatus status) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (status.is_ok()) {
    total_bytes_decoded_ += buffer_size;
    if (bytes_decoded_cb_)
      bytes_decoded_cb_.Run(buffer_size);
  }
  std::move(decode_cb).Run(std::move(status));
}

void FakeVideoDecoder::RunOrHoldDecode(DecodeCB decode_cb) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (hold_decode_) {
    held_decode_callbacks_.push_back(std::move(decode_cb));
  } else {
    DCHECK(held_decode_callbacks_.empty());
    RunDecodeCallback(std::move(decode_cb));
  }
}

void FakeVideoDecoder::RunDecodeCallback(DecodeCB decode_cb) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (!reset_cb_.IsNull()) {
    DCHECK(decoded_frames_.empty());
    std::move(decode_cb).Run(DecoderStatus::Codes::kAborted);
    return;
  }

  // Make sure we leave decoding_delay_ frames in the queue and also frames for
  // all pending decode callbacks, except the current one.
  if (decoded_frames_.size() >
      decoding_delay_ + held_decode_callbacks_.size()) {
    output_cb_.Run(decoded_frames_.front());
    decoded_frames_.pop_front();
  } else if (state_ == STATE_END_OF_STREAM) {
    // Drain the queue if this was the last request in the stream, otherwise
    // just pop the last frame from the queue.
    if (held_decode_callbacks_.empty()) {
      while (!decoded_frames_.empty()) {
        output_cb_.Run(decoded_frames_.front());
        decoded_frames_.pop_front();
      }
      state_ = STATE_NORMAL;
    } else if (!decoded_frames_.empty()) {
      output_cb_.Run(decoded_frames_.front());
      decoded_frames_.pop_front();
    }
  }

  std::move(decode_cb).Run(DecoderStatus::Codes::kOk);
}

void FakeVideoDecoder::DoReset() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(held_decode_callbacks_.empty());
  DCHECK(!reset_cb_.IsNull());

  reset_cb_.RunOrHold();
}

}  // namespace media