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"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/single_thread_task_runner.h"
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),
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
);
44 bool FakeVideoEncodeAccelerator::Initialize(VideoPixelFormat input_format
,
45 const gfx::Size
& input_visible_size
,
46 VideoCodecProfile output_profile
,
47 uint32 initial_bitrate
,
49 if (!will_initialization_succeed_
) {
52 if (output_profile
== VIDEO_CODEC_PROFILE_UNKNOWN
||
53 output_profile
> VIDEO_CODEC_PROFILE_MAX
) {
57 task_runner_
->PostTask(
59 base::Bind(&FakeVideoEncodeAccelerator::DoRequireBitstreamBuffers
,
60 weak_this_factory_
.GetWeakPtr(),
63 kMinimumOutputBufferSize
));
67 void FakeVideoEncodeAccelerator::Encode(
68 const scoped_refptr
<VideoFrame
>& frame
,
69 bool force_keyframe
) {
71 queued_frames_
.push(force_keyframe
);
75 void FakeVideoEncodeAccelerator::UseOutputBitstreamBuffer(
76 const BitstreamBuffer
& buffer
) {
77 available_buffers_
.push_back(buffer
);
81 void FakeVideoEncodeAccelerator::RequestEncodingParametersChange(
84 stored_bitrates_
.push_back(bitrate
);
87 void FakeVideoEncodeAccelerator::Destroy() { delete this; }
89 void FakeVideoEncodeAccelerator::SendDummyFrameForTesting(bool key_frame
) {
90 task_runner_
->PostTask(
92 base::Bind(&FakeVideoEncodeAccelerator::DoBitstreamBufferReady
,
93 weak_this_factory_
.GetWeakPtr(),
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(
122 base::Bind(&FakeVideoEncodeAccelerator::DoBitstreamBufferReady
,
123 weak_this_factory_
.GetWeakPtr(),
125 kMinimumOutputBufferSize
,
130 void FakeVideoEncodeAccelerator::DoBitstreamBufferReady(
131 int32 bitstream_buffer_id
,
133 bool key_frame
) const {
134 client_
->BitstreamBufferReady(bitstream_buffer_id
,