Home | History | Annotate | Download | only in test
      1 // Copyright 2014 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/cast/test/fake_video_encode_accelerator.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/location.h"
      9 #include "base/logging.h"
     10 #include "base/single_thread_task_runner.h"
     11 
     12 namespace media {
     13 namespace cast {
     14 namespace test {
     15 
     16 static const unsigned int kMinimumInputCount = 1;
     17 static const size_t kMinimumOutputBufferSize = 123456;
     18 
     19 FakeVideoEncodeAccelerator::FakeVideoEncodeAccelerator(
     20     const scoped_refptr<base::SingleThreadTaskRunner>& task_runner)
     21     : task_runner_(task_runner),
     22       client_(NULL),
     23       first_(true),
     24       weak_this_factory_(this) {}
     25 
     26 FakeVideoEncodeAccelerator::~FakeVideoEncodeAccelerator() {
     27   weak_this_factory_.InvalidateWeakPtrs();
     28 }
     29 
     30 bool FakeVideoEncodeAccelerator::Initialize(
     31     media::VideoFrame::Format input_format,
     32     const gfx::Size& input_visible_size,
     33     VideoCodecProfile output_profile,
     34     uint32 initial_bitrate,
     35     Client* client) {
     36   client_ = client;
     37   if (output_profile != media::VP8PROFILE_MAIN &&
     38       output_profile != media::H264PROFILE_MAIN) {
     39     return false;
     40   }
     41   task_runner_->PostTask(
     42       FROM_HERE,
     43       base::Bind(&FakeVideoEncodeAccelerator::DoRequireBitstreamBuffers,
     44                  weak_this_factory_.GetWeakPtr(),
     45                  kMinimumInputCount,
     46                  input_visible_size,
     47                  kMinimumOutputBufferSize));
     48   return true;
     49 }
     50 
     51 void FakeVideoEncodeAccelerator::Encode(const scoped_refptr<VideoFrame>& frame,
     52                                         bool force_keyframe) {
     53   DCHECK(client_);
     54   DCHECK(!available_buffer_ids_.empty());
     55 
     56   // Fake that we have encoded the frame; resulting in using the full output
     57   // buffer.
     58   int32 id = available_buffer_ids_.front();
     59   available_buffer_ids_.pop_front();
     60 
     61   bool is_key_fame = force_keyframe;
     62   if (first_) {
     63     is_key_fame = true;
     64     first_ = false;
     65   }
     66   task_runner_->PostTask(
     67       FROM_HERE,
     68       base::Bind(&FakeVideoEncodeAccelerator::DoBitstreamBufferReady,
     69                  weak_this_factory_.GetWeakPtr(),
     70                  id,
     71                  kMinimumOutputBufferSize,
     72                  is_key_fame));
     73 }
     74 
     75 void FakeVideoEncodeAccelerator::UseOutputBitstreamBuffer(
     76     const BitstreamBuffer& buffer) {
     77   available_buffer_ids_.push_back(buffer.id());
     78 }
     79 
     80 void FakeVideoEncodeAccelerator::RequestEncodingParametersChange(
     81     uint32 bitrate,
     82     uint32 framerate) {
     83   // No-op.
     84 }
     85 
     86 void FakeVideoEncodeAccelerator::Destroy() { delete this; }
     87 
     88 void FakeVideoEncodeAccelerator::SendDummyFrameForTesting(bool key_frame) {
     89   DoBitstreamBufferReady(0, 23, key_frame);
     90 }
     91 
     92 void FakeVideoEncodeAccelerator::DoRequireBitstreamBuffers(
     93     unsigned int input_count,
     94     const gfx::Size& input_coded_size,
     95     size_t output_buffer_size) const {
     96   client_->RequireBitstreamBuffers(
     97       input_count, input_coded_size, output_buffer_size);
     98 }
     99 
    100 void FakeVideoEncodeAccelerator::DoBitstreamBufferReady(
    101     int32 bitstream_buffer_id,
    102     size_t payload_size,
    103     bool key_frame) const {
    104   client_->BitstreamBufferReady(bitstream_buffer_id, payload_size, key_frame);
    105 }
    106 
    107 }  // namespace test
    108 }  // namespace cast
    109 }  // namespace media
    110