Implement dynamic software fallback in PPB_VideoDecoder API.
[chromium-blink-merge.git] / media / video / fake_video_encode_accelerator.cc
blobf0087db1f41964b3eab3b8583d78e4fe59a3a368
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.
5 #include "media/video/fake_video_encode_accelerator.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/single_thread_task_runner.h"
12 namespace media {
14 static const unsigned int kMinimumInputCount = 1;
15 static const size_t kMinimumOutputBufferSize = 123456;
17 FakeVideoEncodeAccelerator::FakeVideoEncodeAccelerator(
18 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner)
19 : task_runner_(task_runner),
20 will_initialization_succeed_(true),
21 client_(NULL),
22 next_frame_is_first_frame_(true),
23 weak_this_factory_(this) {}
25 FakeVideoEncodeAccelerator::~FakeVideoEncodeAccelerator() {
26 weak_this_factory_.InvalidateWeakPtrs();
29 VideoEncodeAccelerator::SupportedProfiles
30 FakeVideoEncodeAccelerator::GetSupportedProfiles() {
31 SupportedProfiles profiles;
32 SupportedProfile profile;
33 profile.max_resolution.SetSize(1920, 1088);
34 profile.max_framerate_numerator = 30;
35 profile.max_framerate_denominator = 1;
37 profile.profile = media::H264PROFILE_MAIN;
38 profiles.push_back(profile);
39 profile.profile = media::VP8PROFILE_ANY;
40 profiles.push_back(profile);
41 return profiles;
44 bool FakeVideoEncodeAccelerator::Initialize(VideoPixelFormat input_format,
45 const gfx::Size& input_visible_size,
46 VideoCodecProfile output_profile,
47 uint32 initial_bitrate,
48 Client* client) {
49 if (!will_initialization_succeed_) {
50 return false;
52 if (output_profile == VIDEO_CODEC_PROFILE_UNKNOWN ||
53 output_profile > VIDEO_CODEC_PROFILE_MAX) {
54 return false;
56 client_ = client;
57 task_runner_->PostTask(
58 FROM_HERE,
59 base::Bind(&FakeVideoEncodeAccelerator::DoRequireBitstreamBuffers,
60 weak_this_factory_.GetWeakPtr(),
61 kMinimumInputCount,
62 input_visible_size,
63 kMinimumOutputBufferSize));
64 return true;
67 void FakeVideoEncodeAccelerator::Encode(
68 const scoped_refptr<VideoFrame>& frame,
69 bool force_keyframe) {
70 DCHECK(client_);
71 queued_frames_.push(force_keyframe);
72 EncodeTask();
75 void FakeVideoEncodeAccelerator::UseOutputBitstreamBuffer(
76 const BitstreamBuffer& buffer) {
77 available_buffers_.push_back(buffer);
78 EncodeTask();
81 void FakeVideoEncodeAccelerator::RequestEncodingParametersChange(
82 uint32 bitrate,
83 uint32 framerate) {
84 stored_bitrates_.push_back(bitrate);
87 void FakeVideoEncodeAccelerator::Destroy() { delete this; }
89 void FakeVideoEncodeAccelerator::SendDummyFrameForTesting(bool key_frame) {
90 task_runner_->PostTask(
91 FROM_HERE,
92 base::Bind(&FakeVideoEncodeAccelerator::DoBitstreamBufferReady,
93 weak_this_factory_.GetWeakPtr(),
95 23,
96 key_frame));
99 void FakeVideoEncodeAccelerator::SetWillInitializationSucceed(
100 bool will_initialization_succeed) {
101 will_initialization_succeed_ = will_initialization_succeed;
104 void FakeVideoEncodeAccelerator::DoRequireBitstreamBuffers(
105 unsigned int input_count,
106 const gfx::Size& input_coded_size,
107 size_t output_buffer_size) const {
108 client_->RequireBitstreamBuffers(
109 input_count, input_coded_size, output_buffer_size);
112 void FakeVideoEncodeAccelerator::EncodeTask() {
113 while (!queued_frames_.empty() && !available_buffers_.empty()) {
114 bool force_key_frame = queued_frames_.front();
115 queued_frames_.pop();
116 int32 bitstream_buffer_id = available_buffers_.front().id();
117 available_buffers_.pop_front();
118 bool key_frame = next_frame_is_first_frame_ || force_key_frame;
119 next_frame_is_first_frame_ = false;
120 task_runner_->PostTask(
121 FROM_HERE,
122 base::Bind(&FakeVideoEncodeAccelerator::DoBitstreamBufferReady,
123 weak_this_factory_.GetWeakPtr(),
124 bitstream_buffer_id,
125 kMinimumOutputBufferSize,
126 key_frame));
130 void FakeVideoEncodeAccelerator::DoBitstreamBufferReady(
131 int32 bitstream_buffer_id,
132 size_t payload_size,
133 bool key_frame) const {
134 client_->BitstreamBufferReady(bitstream_buffer_id,
135 payload_size,
136 key_frame);
139 } // namespace media