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 std::vector
<VideoEncodeAccelerator::SupportedProfile
>
30 FakeVideoEncodeAccelerator::GetSupportedProfiles() {
31 std::vector
<VideoEncodeAccelerator::SupportedProfile
> 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(
45 VideoFrame::Format input_format
,
46 const gfx::Size
& input_visible_size
,
47 VideoCodecProfile output_profile
,
48 uint32 initial_bitrate
,
50 if (!will_initialization_succeed_
) {
53 if (output_profile
== VIDEO_CODEC_PROFILE_UNKNOWN
||
54 output_profile
> VIDEO_CODEC_PROFILE_MAX
) {
58 task_runner_
->PostTask(
60 base::Bind(&FakeVideoEncodeAccelerator::DoRequireBitstreamBuffers
,
61 weak_this_factory_
.GetWeakPtr(),
64 kMinimumOutputBufferSize
));
68 void FakeVideoEncodeAccelerator::Encode(
69 const scoped_refptr
<VideoFrame
>& frame
,
70 bool force_keyframe
) {
72 queued_frames_
.push(force_keyframe
);
76 void FakeVideoEncodeAccelerator::UseOutputBitstreamBuffer(
77 const BitstreamBuffer
& buffer
) {
78 available_buffers_
.push_back(buffer
);
82 void FakeVideoEncodeAccelerator::RequestEncodingParametersChange(
85 stored_bitrates_
.push_back(bitrate
);
88 void FakeVideoEncodeAccelerator::Destroy() { delete this; }
90 void FakeVideoEncodeAccelerator::SendDummyFrameForTesting(bool key_frame
) {
91 task_runner_
->PostTask(
93 base::Bind(&FakeVideoEncodeAccelerator::DoBitstreamBufferReady
,
94 weak_this_factory_
.GetWeakPtr(),
100 void FakeVideoEncodeAccelerator::SetWillInitializationSucceed(
101 bool will_initialization_succeed
) {
102 will_initialization_succeed_
= will_initialization_succeed
;
105 void FakeVideoEncodeAccelerator::DoRequireBitstreamBuffers(
106 unsigned int input_count
,
107 const gfx::Size
& input_coded_size
,
108 size_t output_buffer_size
) const {
109 client_
->RequireBitstreamBuffers(
110 input_count
, input_coded_size
, output_buffer_size
);
113 void FakeVideoEncodeAccelerator::EncodeTask() {
114 while (!queued_frames_
.empty() && !available_buffers_
.empty()) {
115 bool force_key_frame
= queued_frames_
.front();
116 queued_frames_
.pop();
117 int32 bitstream_buffer_id
= available_buffers_
.front().id();
118 available_buffers_
.pop_front();
119 bool key_frame
= next_frame_is_first_frame_
|| force_key_frame
;
120 next_frame_is_first_frame_
= false;
121 task_runner_
->PostTask(
123 base::Bind(&FakeVideoEncodeAccelerator::DoBitstreamBufferReady
,
124 weak_this_factory_
.GetWeakPtr(),
126 kMinimumOutputBufferSize
,
131 void FakeVideoEncodeAccelerator::DoBitstreamBufferReady(
132 int32 bitstream_buffer_id
,
134 bool key_frame
) const {
135 client_
->BitstreamBufferReady(bitstream_buffer_id
,