OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/common/gpu/media/gpu_video_encode_accelerator.h" | 5 #include "content/common/gpu/media/gpu_video_encode_accelerator.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/shared_memory.h" | 10 #include "base/memory/shared_memory.h" |
11 #include "base/message_loop/message_loop_proxy.h" | 11 #include "base/message_loop/message_loop_proxy.h" |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
13 #include "content/common/gpu/gpu_channel.h" | 13 #include "content/common/gpu/gpu_channel.h" |
14 #include "content/common/gpu/gpu_messages.h" | 14 #include "content/common/gpu/gpu_messages.h" |
15 #include "content/public/common/content_switches.h" | 15 #include "content/public/common/content_switches.h" |
16 #include "ipc/ipc_message_macros.h" | 16 #include "ipc/ipc_message_macros.h" |
17 #include "media/base/limits.h" | 17 #include "media/base/limits.h" |
18 #include "media/base/video_frame.h" | 18 #include "media/base/video_frame.h" |
19 | 19 |
20 #if defined(OS_CHROMEOS) && defined(USE_X11) | 20 #if defined(OS_CHROMEOS) && defined(USE_X11) |
21 | 21 |
22 #if defined(ARCH_CPU_ARMEL) | 22 #if defined(ARCH_CPU_ARMEL) |
23 #include "content/common/gpu/media/v4l2_video_encode_accelerator.h" | 23 #include "content/common/gpu/media/v4l2_video_encode_accelerator.h" |
24 #elif defined(ARCH_CPU_X86_FAMILY) | 24 #elif defined(ARCH_CPU_X86_FAMILY) |
25 #include "content/common/gpu/media/v4l2_video_encode_accelerator.h" | |
25 #include "content/common/gpu/media/vaapi_video_encode_accelerator.h" | 26 #include "content/common/gpu/media/vaapi_video_encode_accelerator.h" |
26 #include "ui/gfx/x/x11_types.h" | 27 #include "ui/gfx/x/x11_types.h" |
27 #endif | 28 #endif |
28 | 29 |
29 #elif defined(OS_ANDROID) && defined(ENABLE_WEBRTC) | 30 #elif defined(OS_ANDROID) && defined(ENABLE_WEBRTC) |
30 #include "content/common/gpu/media/android_video_encode_accelerator.h" | 31 #include "content/common/gpu/media/android_video_encode_accelerator.h" |
31 #endif | 32 #endif |
32 | 33 |
33 namespace content { | 34 namespace content { |
34 | 35 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
88 if (input_visible_size.width() > media::limits::kMaxDimension || | 89 if (input_visible_size.width() > media::limits::kMaxDimension || |
89 input_visible_size.height() > media::limits::kMaxDimension || | 90 input_visible_size.height() > media::limits::kMaxDimension || |
90 input_visible_size.GetArea() > media::limits::kMaxCanvas) { | 91 input_visible_size.GetArea() > media::limits::kMaxCanvas) { |
91 DLOG(ERROR) << "GpuVideoEncodeAccelerator::Initialize(): " | 92 DLOG(ERROR) << "GpuVideoEncodeAccelerator::Initialize(): " |
92 "input_visible_size " << input_visible_size.ToString() | 93 "input_visible_size " << input_visible_size.ToString() |
93 << " too large"; | 94 << " too large"; |
94 SendCreateEncoderReply(init_done_msg, false); | 95 SendCreateEncoderReply(init_done_msg, false); |
95 return; | 96 return; |
96 } | 97 } |
97 | 98 |
98 encoder_ = CreateEncoder(); | 99 std::vector<media::VideoEncodeAccelerator*> encoders = CreateEncoders(); |
99 if (!encoder_) { | 100 if (encoders.empty()) { |
100 DLOG(ERROR) | 101 DLOG(ERROR) |
101 << "GpuVideoEncodeAccelerator::Initialize(): VEA creation failed"; | 102 << "GpuVideoEncodeAccelerator::Initialize(): VEA creation failed"; |
102 SendCreateEncoderReply(init_done_msg, false); | 103 SendCreateEncoderReply(init_done_msg, false); |
103 return; | 104 return; |
104 } | 105 } |
105 if (!encoder_->Initialize(input_format, | 106 for (size_t i = 0; i < encoders.size(); ++i) { |
Pawel Osciak
2014/12/28 23:28:03
Please add comments what's going on here.
henryhsu
2014/12/29 09:43:26
Done.
| |
106 input_visible_size, | 107 if (encoders[i]->Initialize(input_format, |
107 output_profile, | 108 input_visible_size, |
108 initial_bitrate, | 109 output_profile, |
109 this)) { | 110 initial_bitrate, |
111 this)) { | |
112 encoder_.reset(encoders[i]); | |
Pawel Osciak
2014/12/28 23:28:03
We keep looping even if successful and overwrite t
henryhsu
2014/12/29 09:43:26
ok. let us keep the first successful encoder.
| |
113 input_format_ = input_format; | |
Pawel Osciak
2014/12/28 23:28:03
This could still stay outside of the loop.
henryhsu
2014/12/29 09:43:26
will return directly if initialization succeeded.
| |
114 input_visible_size_ = input_visible_size; | |
115 } else { | |
116 encoders[i]->Destroy(); | |
Pawel Osciak
2014/12/28 23:28:03
This assumes Destroy() deletes this, which it does
henryhsu
2014/12/29 09:43:26
back to scoped_ptr
| |
117 } | |
118 } | |
119 if (!encoder_.get()) { | |
110 DLOG(ERROR) | 120 DLOG(ERROR) |
111 << "GpuVideoEncodeAccelerator::Initialize(): VEA initialization failed"; | 121 << "GpuVideoEncodeAccelerator::Initialize(): VEA initialization failed"; |
112 SendCreateEncoderReply(init_done_msg, false); | 122 SendCreateEncoderReply(init_done_msg, false); |
113 return; | 123 } else { |
124 SendCreateEncoderReply(init_done_msg, true); | |
114 } | 125 } |
115 input_format_ = input_format; | |
116 input_visible_size_ = input_visible_size; | |
117 SendCreateEncoderReply(init_done_msg, true); | |
118 } | 126 } |
119 | 127 |
120 bool GpuVideoEncodeAccelerator::OnMessageReceived(const IPC::Message& message) { | 128 bool GpuVideoEncodeAccelerator::OnMessageReceived(const IPC::Message& message) { |
121 bool handled = true; | 129 bool handled = true; |
122 IPC_BEGIN_MESSAGE_MAP(GpuVideoEncodeAccelerator, message) | 130 IPC_BEGIN_MESSAGE_MAP(GpuVideoEncodeAccelerator, message) |
123 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderMsg_Encode, OnEncode) | 131 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderMsg_Encode, OnEncode) |
124 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderMsg_UseOutputBitstreamBuffer, | 132 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderMsg_UseOutputBitstreamBuffer, |
125 OnUseOutputBitstreamBuffer) | 133 OnUseOutputBitstreamBuffer) |
126 IPC_MESSAGE_HANDLER( | 134 IPC_MESSAGE_HANDLER( |
127 AcceleratedVideoEncoderMsg_RequestEncodingParametersChange, | 135 AcceleratedVideoEncoderMsg_RequestEncodingParametersChange, |
(...skipping 29 matching lines...) Expand all Loading... | |
157 void GpuVideoEncodeAccelerator::OnWillDestroyStub() { | 165 void GpuVideoEncodeAccelerator::OnWillDestroyStub() { |
158 DCHECK(stub_); | 166 DCHECK(stub_); |
159 stub_->channel()->RemoveRoute(host_route_id_); | 167 stub_->channel()->RemoveRoute(host_route_id_); |
160 stub_->RemoveDestructionObserver(this); | 168 stub_->RemoveDestructionObserver(this); |
161 encoder_.reset(); | 169 encoder_.reset(); |
162 delete this; | 170 delete this; |
163 } | 171 } |
164 | 172 |
165 // static | 173 // static |
166 std::vector<gpu::VideoEncodeAcceleratorSupportedProfile> | 174 std::vector<gpu::VideoEncodeAcceleratorSupportedProfile> |
167 GpuVideoEncodeAccelerator::GetSupportedProfiles() { | 175 GpuVideoEncodeAccelerator::GetSupportedProfiles() { |
Pawel Osciak
2014/12/28 23:28:03
Can we keep the profile list for later in a single
henryhsu
2014/12/29 09:43:26
GetSupportedProfiels is only called one time when
| |
168 scoped_ptr<media::VideoEncodeAccelerator> encoder = CreateEncoder(); | 176 std::vector<media::VideoEncodeAccelerator::SupportedProfile> profiles; |
169 if (!encoder) | 177 std::vector<media::VideoEncodeAccelerator*> encoders = CreateEncoders(); |
Pawel Osciak
2014/12/28 23:28:03
Scopers please.
henryhsu
2014/12/29 09:43:26
SocopedVector doesn't support DefaultDeleter. I'll
| |
178 if (encoders.empty()) | |
170 return std::vector<gpu::VideoEncodeAcceleratorSupportedProfile>(); | 179 return std::vector<gpu::VideoEncodeAcceleratorSupportedProfile>(); |
171 return ConvertMediaToGpuProfiles(encoder->GetSupportedProfiles()); | 180 |
181 std::set<media::VideoEncodeAccelerator::SupportedProfile> profile_set; | |
182 for (size_t i = 0; i < encoders.size(); ++i) { | |
183 std::vector<media::VideoEncodeAccelerator::SupportedProfile> | |
184 vea_profiles = encoders[i]->GetSupportedProfiles(); | |
185 for (size_t j = 0; j < vea_profiles.size(); ++j) | |
186 profile_set.insert(vea_profiles[j]); | |
187 encoders[i]->Destroy(); | |
Pawel Osciak
2014/12/28 23:28:03
Please don't depend on delete this in Destroy().
henryhsu
2014/12/29 09:43:26
Done.
| |
188 } | |
189 profiles.assign(profile_set.begin(), profile_set.end()); | |
190 return ConvertMediaToGpuProfiles(profiles); | |
172 } | 191 } |
173 | 192 |
193 // static | |
174 std::vector<gpu::VideoEncodeAcceleratorSupportedProfile> | 194 std::vector<gpu::VideoEncodeAcceleratorSupportedProfile> |
175 GpuVideoEncodeAccelerator::ConvertMediaToGpuProfiles(const std::vector< | 195 GpuVideoEncodeAccelerator::ConvertMediaToGpuProfiles(const std::vector< |
176 media::VideoEncodeAccelerator::SupportedProfile>& media_profiles) { | 196 media::VideoEncodeAccelerator::SupportedProfile>& media_profiles) { |
177 std::vector<gpu::VideoEncodeAcceleratorSupportedProfile> profiles; | 197 std::vector<gpu::VideoEncodeAcceleratorSupportedProfile> profiles; |
178 for (size_t i = 0; i < media_profiles.size(); i++) { | 198 for (size_t i = 0; i < media_profiles.size(); i++) { |
179 gpu::VideoEncodeAcceleratorSupportedProfile profile; | 199 gpu::VideoEncodeAcceleratorSupportedProfile profile; |
180 profile.profile = | 200 profile.profile = |
181 static_cast<gpu::VideoCodecProfile>(media_profiles[i].profile); | 201 static_cast<gpu::VideoCodecProfile>(media_profiles[i].profile); |
182 profile.max_resolution = media_profiles[i].max_resolution; | 202 profile.max_resolution = media_profiles[i].max_resolution; |
183 profile.max_framerate_numerator = media_profiles[i].max_framerate_numerator; | 203 profile.max_framerate_numerator = media_profiles[i].max_framerate_numerator; |
184 profile.max_framerate_denominator = | 204 profile.max_framerate_denominator = |
185 media_profiles[i].max_framerate_denominator; | 205 media_profiles[i].max_framerate_denominator; |
186 profiles.push_back(profile); | 206 profiles.push_back(profile); |
187 } | 207 } |
188 return profiles; | 208 return profiles; |
189 } | 209 } |
190 | 210 |
191 scoped_ptr<media::VideoEncodeAccelerator> | 211 // static |
192 GpuVideoEncodeAccelerator::CreateEncoder() { | 212 std::vector<media::VideoEncodeAccelerator*> |
193 scoped_ptr<media::VideoEncodeAccelerator> encoder; | 213 GpuVideoEncodeAccelerator::CreateEncoders() { |
214 std::vector<media::VideoEncodeAccelerator*> encoders; | |
194 #if defined(OS_CHROMEOS) && defined(USE_X11) | 215 #if defined(OS_CHROMEOS) && defined(USE_X11) |
195 #if defined(ARCH_CPU_ARMEL) | |
196 scoped_ptr<V4L2Device> device = V4L2Device::Create(V4L2Device::kEncoder); | 216 scoped_ptr<V4L2Device> device = V4L2Device::Create(V4L2Device::kEncoder); |
197 if (device) | 217 if (device) |
198 encoder.reset(new V4L2VideoEncodeAccelerator(device.Pass())); | 218 encoders.push_back(new V4L2VideoEncodeAccelerator(device.Pass())); |
199 #elif defined(ARCH_CPU_X86_FAMILY) | 219 #if defined(ARCH_CPU_X86_FAMILY) |
200 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | 220 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); |
201 if (!cmd_line->HasSwitch(switches::kDisableVaapiAcceleratedVideoEncode)) | 221 if (!cmd_line->HasSwitch(switches::kDisableVaapiAcceleratedVideoEncode)) |
202 encoder.reset(new VaapiVideoEncodeAccelerator(gfx::GetXDisplay())); | 222 encoders.push_back(new VaapiVideoEncodeAccelerator(gfx::GetXDisplay())); |
203 #endif | 223 #endif |
204 #elif defined(OS_ANDROID) && defined(ENABLE_WEBRTC) | 224 #elif defined(OS_ANDROID) && defined(ENABLE_WEBRTC) |
205 encoder.reset(new AndroidVideoEncodeAccelerator()); | 225 encoders.push_back(new AndroidVideoEncodeAccelerator()); |
206 #endif | 226 #endif |
207 return encoder.Pass(); | 227 return encoders; |
208 } | 228 } |
209 | 229 |
210 void GpuVideoEncodeAccelerator::OnEncode(int32 frame_id, | 230 void GpuVideoEncodeAccelerator::OnEncode(int32 frame_id, |
211 base::SharedMemoryHandle buffer_handle, | 231 base::SharedMemoryHandle buffer_handle, |
212 uint32 buffer_size, | 232 uint32 buffer_size, |
213 bool force_keyframe) { | 233 bool force_keyframe) { |
214 DVLOG(3) << "GpuVideoEncodeAccelerator::OnEncode(): frame_id=" << frame_id | 234 DVLOG(3) << "GpuVideoEncodeAccelerator::OnEncode(): frame_id=" << frame_id |
215 << ", buffer_size=" << buffer_size | 235 << ", buffer_size=" << buffer_size |
216 << ", force_keyframe=" << force_keyframe; | 236 << ", force_keyframe=" << force_keyframe; |
217 if (!encoder_) | 237 if (!encoder_) |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
315 stub_->channel()->Send(message); | 335 stub_->channel()->Send(message); |
316 } | 336 } |
317 | 337 |
318 void GpuVideoEncodeAccelerator::SendCreateEncoderReply(IPC::Message* message, | 338 void GpuVideoEncodeAccelerator::SendCreateEncoderReply(IPC::Message* message, |
319 bool succeeded) { | 339 bool succeeded) { |
320 GpuCommandBufferMsg_CreateVideoEncoder::WriteReplyParams(message, succeeded); | 340 GpuCommandBufferMsg_CreateVideoEncoder::WriteReplyParams(message, succeeded); |
321 Send(message); | 341 Send(message); |
322 } | 342 } |
323 | 343 |
324 } // namespace content | 344 } // namespace content |
OLD | NEW |