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) | 20 #if defined(OS_CHROMEOS) |
21 | 21 |
22 #if defined(ARCH_CPU_ARMEL) && defined(USE_X11) | 22 #if defined(ARCH_CPU_ARMEL) && defined(USE_X11) |
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/vaapi_video_encode_accelerator.h" | 25 #include "content/common/gpu/media/vaapi_video_encode_accelerator.h" |
| 26 #if defined(USE_X11) |
| 27 #include "content/common/gpu/media/v4l2_video_encode_accelerator.h" |
| 28 #endif |
26 #endif | 29 #endif |
27 | 30 |
28 #elif defined(OS_ANDROID) && defined(ENABLE_WEBRTC) | 31 #elif defined(OS_ANDROID) && defined(ENABLE_WEBRTC) |
29 #include "content/common/gpu/media/android_video_encode_accelerator.h" | 32 #include "content/common/gpu/media/android_video_encode_accelerator.h" |
30 #endif | 33 #endif |
31 | 34 |
32 namespace content { | 35 namespace content { |
33 | 36 |
34 static bool MakeDecoderContextCurrent( | 37 static bool MakeDecoderContextCurrent( |
35 const base::WeakPtr<GpuCommandBufferStub> stub) { | 38 const base::WeakPtr<GpuCommandBufferStub> stub) { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 if (input_visible_size.width() > media::limits::kMaxDimension || | 90 if (input_visible_size.width() > media::limits::kMaxDimension || |
88 input_visible_size.height() > media::limits::kMaxDimension || | 91 input_visible_size.height() > media::limits::kMaxDimension || |
89 input_visible_size.GetArea() > media::limits::kMaxCanvas) { | 92 input_visible_size.GetArea() > media::limits::kMaxCanvas) { |
90 DLOG(ERROR) << "GpuVideoEncodeAccelerator::Initialize(): " | 93 DLOG(ERROR) << "GpuVideoEncodeAccelerator::Initialize(): " |
91 "input_visible_size " << input_visible_size.ToString() | 94 "input_visible_size " << input_visible_size.ToString() |
92 << " too large"; | 95 << " too large"; |
93 SendCreateEncoderReply(init_done_msg, false); | 96 SendCreateEncoderReply(init_done_msg, false); |
94 return; | 97 return; |
95 } | 98 } |
96 | 99 |
97 encoder_ = CreateEncoder(); | 100 std::vector<GpuVideoEncodeAccelerator::CreateVEACb> |
98 if (!encoder_) { | 101 create_vea_cbs = CreateVEACbs(); |
99 DLOG(ERROR) | 102 // Try all possible encoders and use the first successful encoder. |
100 << "GpuVideoEncodeAccelerator::Initialize(): VEA creation failed"; | 103 for (size_t i = 0; i < create_vea_cbs.size(); ++i) { |
101 SendCreateEncoderReply(init_done_msg, false); | 104 encoder_ = create_vea_cbs[i].Run(); |
102 return; | 105 if (encoder_ && encoder_->Initialize(input_format, |
| 106 input_visible_size, |
| 107 output_profile, |
| 108 initial_bitrate, |
| 109 this)) { |
| 110 input_format_ = input_format; |
| 111 input_visible_size_ = input_visible_size; |
| 112 SendCreateEncoderReply(init_done_msg, true); |
| 113 return; |
| 114 } |
103 } | 115 } |
104 if (!encoder_->Initialize(input_format, | 116 encoder_.reset(); |
105 input_visible_size, | 117 DLOG(ERROR) |
106 output_profile, | 118 << "GpuVideoEncodeAccelerator::Initialize(): VEA initialization failed"; |
107 initial_bitrate, | 119 SendCreateEncoderReply(init_done_msg, false); |
108 this)) { | |
109 DLOG(ERROR) | |
110 << "GpuVideoEncodeAccelerator::Initialize(): VEA initialization failed"; | |
111 SendCreateEncoderReply(init_done_msg, false); | |
112 return; | |
113 } | |
114 input_format_ = input_format; | |
115 input_visible_size_ = input_visible_size; | |
116 SendCreateEncoderReply(init_done_msg, true); | |
117 } | 120 } |
118 | 121 |
119 bool GpuVideoEncodeAccelerator::OnMessageReceived(const IPC::Message& message) { | 122 bool GpuVideoEncodeAccelerator::OnMessageReceived(const IPC::Message& message) { |
120 bool handled = true; | 123 bool handled = true; |
121 IPC_BEGIN_MESSAGE_MAP(GpuVideoEncodeAccelerator, message) | 124 IPC_BEGIN_MESSAGE_MAP(GpuVideoEncodeAccelerator, message) |
122 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderMsg_Encode, OnEncode) | 125 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderMsg_Encode, OnEncode) |
123 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderMsg_UseOutputBitstreamBuffer, | 126 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderMsg_UseOutputBitstreamBuffer, |
124 OnUseOutputBitstreamBuffer) | 127 OnUseOutputBitstreamBuffer) |
125 IPC_MESSAGE_HANDLER( | 128 IPC_MESSAGE_HANDLER( |
126 AcceleratedVideoEncoderMsg_RequestEncodingParametersChange, | 129 AcceleratedVideoEncoderMsg_RequestEncodingParametersChange, |
(...skipping 30 matching lines...) Expand all Loading... |
157 DCHECK(stub_); | 160 DCHECK(stub_); |
158 stub_->channel()->RemoveRoute(host_route_id_); | 161 stub_->channel()->RemoveRoute(host_route_id_); |
159 stub_->RemoveDestructionObserver(this); | 162 stub_->RemoveDestructionObserver(this); |
160 encoder_.reset(); | 163 encoder_.reset(); |
161 delete this; | 164 delete this; |
162 } | 165 } |
163 | 166 |
164 // static | 167 // static |
165 std::vector<gpu::VideoEncodeAcceleratorSupportedProfile> | 168 std::vector<gpu::VideoEncodeAcceleratorSupportedProfile> |
166 GpuVideoEncodeAccelerator::GetSupportedProfiles() { | 169 GpuVideoEncodeAccelerator::GetSupportedProfiles() { |
167 scoped_ptr<media::VideoEncodeAccelerator> encoder = CreateEncoder(); | 170 std::vector<media::VideoEncodeAccelerator::SupportedProfile> profiles; |
168 if (!encoder) | 171 std::vector<GpuVideoEncodeAccelerator::CreateVEACb> |
169 return std::vector<gpu::VideoEncodeAcceleratorSupportedProfile>(); | 172 create_vea_cbs = CreateVEACbs(); |
170 return ConvertMediaToGpuProfiles(encoder->GetSupportedProfiles()); | 173 |
| 174 for (size_t i = 0; i < create_vea_cbs.size(); ++i) { |
| 175 scoped_ptr<media::VideoEncodeAccelerator> encoder = create_vea_cbs[i].Run(); |
| 176 if (!encoder) |
| 177 continue; |
| 178 std::vector<media::VideoEncodeAccelerator::SupportedProfile> |
| 179 vea_profiles = encoder->GetSupportedProfiles(); |
| 180 profiles.insert(profiles.end(), vea_profiles.begin(), vea_profiles.end()); |
| 181 } |
| 182 return ConvertMediaToGpuProfiles(profiles); |
171 } | 183 } |
172 | 184 |
| 185 // static |
173 std::vector<gpu::VideoEncodeAcceleratorSupportedProfile> | 186 std::vector<gpu::VideoEncodeAcceleratorSupportedProfile> |
174 GpuVideoEncodeAccelerator::ConvertMediaToGpuProfiles(const std::vector< | 187 GpuVideoEncodeAccelerator::ConvertMediaToGpuProfiles(const std::vector< |
175 media::VideoEncodeAccelerator::SupportedProfile>& media_profiles) { | 188 media::VideoEncodeAccelerator::SupportedProfile>& media_profiles) { |
176 std::vector<gpu::VideoEncodeAcceleratorSupportedProfile> profiles; | 189 std::vector<gpu::VideoEncodeAcceleratorSupportedProfile> profiles; |
177 for (size_t i = 0; i < media_profiles.size(); i++) { | 190 for (size_t i = 0; i < media_profiles.size(); i++) { |
178 gpu::VideoEncodeAcceleratorSupportedProfile profile; | 191 gpu::VideoEncodeAcceleratorSupportedProfile profile; |
179 profile.profile = | 192 profile.profile = |
180 static_cast<gpu::VideoCodecProfile>(media_profiles[i].profile); | 193 static_cast<gpu::VideoCodecProfile>(media_profiles[i].profile); |
181 profile.max_resolution = media_profiles[i].max_resolution; | 194 profile.max_resolution = media_profiles[i].max_resolution; |
182 profile.max_framerate_numerator = media_profiles[i].max_framerate_numerator; | 195 profile.max_framerate_numerator = media_profiles[i].max_framerate_numerator; |
183 profile.max_framerate_denominator = | 196 profile.max_framerate_denominator = |
184 media_profiles[i].max_framerate_denominator; | 197 media_profiles[i].max_framerate_denominator; |
185 profiles.push_back(profile); | 198 profiles.push_back(profile); |
186 } | 199 } |
187 return profiles; | 200 return profiles; |
188 } | 201 } |
189 | 202 |
| 203 // static |
| 204 std::vector<GpuVideoEncodeAccelerator::CreateVEACb> |
| 205 GpuVideoEncodeAccelerator::CreateVEACbs() { |
| 206 std::vector<GpuVideoEncodeAccelerator::CreateVEACb> create_vea_cbs; |
| 207 create_vea_cbs.push_back(base::Bind( |
| 208 &GpuVideoEncodeAccelerator::CreateV4L2VEA)); |
| 209 create_vea_cbs.push_back(base::Bind( |
| 210 &GpuVideoEncodeAccelerator::CreateVaapiVEA)); |
| 211 create_vea_cbs.push_back(base::Bind( |
| 212 &GpuVideoEncodeAccelerator::CreateAndroidVEA)); |
| 213 return create_vea_cbs; |
| 214 } |
| 215 |
| 216 // static |
190 scoped_ptr<media::VideoEncodeAccelerator> | 217 scoped_ptr<media::VideoEncodeAccelerator> |
191 GpuVideoEncodeAccelerator::CreateEncoder() { | 218 GpuVideoEncodeAccelerator::CreateV4L2VEA() { |
192 scoped_ptr<media::VideoEncodeAccelerator> encoder; | 219 scoped_ptr<media::VideoEncodeAccelerator> encoder; |
193 #if defined(OS_CHROMEOS) | 220 #if defined(OS_CHROMEOS) && defined(USE_X11) |
194 #if defined(ARCH_CPU_ARMEL) && defined(USE_X11) | |
195 scoped_ptr<V4L2Device> device = V4L2Device::Create(V4L2Device::kEncoder); | 221 scoped_ptr<V4L2Device> device = V4L2Device::Create(V4L2Device::kEncoder); |
196 if (device) | 222 if (device) |
197 encoder.reset(new V4L2VideoEncodeAccelerator(device.Pass())); | 223 encoder.reset(new V4L2VideoEncodeAccelerator(device.Pass())); |
198 #elif defined(ARCH_CPU_X86_FAMILY) | 224 #endif |
| 225 return encoder.Pass(); |
| 226 } |
| 227 |
| 228 // static |
| 229 scoped_ptr<media::VideoEncodeAccelerator> |
| 230 GpuVideoEncodeAccelerator::CreateVaapiVEA() { |
| 231 scoped_ptr<media::VideoEncodeAccelerator> encoder; |
| 232 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) |
199 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | 233 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); |
200 if (!cmd_line->HasSwitch(switches::kDisableVaapiAcceleratedVideoEncode)) | 234 if (!cmd_line->HasSwitch(switches::kDisableVaapiAcceleratedVideoEncode)) |
201 encoder.reset(new VaapiVideoEncodeAccelerator()); | 235 encoder.reset(new VaapiVideoEncodeAccelerator()); |
202 #endif | 236 #endif |
203 #elif defined(OS_ANDROID) && defined(ENABLE_WEBRTC) | 237 return encoder.Pass(); |
| 238 } |
| 239 |
| 240 // static |
| 241 scoped_ptr<media::VideoEncodeAccelerator> |
| 242 GpuVideoEncodeAccelerator::CreateAndroidVEA() { |
| 243 scoped_ptr<media::VideoEncodeAccelerator> encoder; |
| 244 #if defined(OS_ANDROID) && defined(ENABLE_WEBRTC) |
204 encoder.reset(new AndroidVideoEncodeAccelerator()); | 245 encoder.reset(new AndroidVideoEncodeAccelerator()); |
205 #endif | 246 #endif |
206 return encoder.Pass(); | 247 return encoder.Pass(); |
207 } | 248 } |
208 | 249 |
209 void GpuVideoEncodeAccelerator::OnEncode(int32 frame_id, | 250 void GpuVideoEncodeAccelerator::OnEncode(int32 frame_id, |
210 base::SharedMemoryHandle buffer_handle, | 251 base::SharedMemoryHandle buffer_handle, |
211 uint32 buffer_size, | 252 uint32 buffer_size, |
212 bool force_keyframe) { | 253 bool force_keyframe) { |
213 DVLOG(3) << "GpuVideoEncodeAccelerator::OnEncode(): frame_id=" << frame_id | 254 DVLOG(3) << "GpuVideoEncodeAccelerator::OnEncode(): frame_id=" << frame_id |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 stub_->channel()->Send(message); | 355 stub_->channel()->Send(message); |
315 } | 356 } |
316 | 357 |
317 void GpuVideoEncodeAccelerator::SendCreateEncoderReply(IPC::Message* message, | 358 void GpuVideoEncodeAccelerator::SendCreateEncoderReply(IPC::Message* message, |
318 bool succeeded) { | 359 bool succeeded) { |
319 GpuCommandBufferMsg_CreateVideoEncoder::WriteReplyParams(message, succeeded); | 360 GpuCommandBufferMsg_CreateVideoEncoder::WriteReplyParams(message, succeeded); |
320 Send(message); | 361 Send(message); |
321 } | 362 } |
322 | 363 |
323 } // namespace content | 364 } // namespace content |
OLD | NEW |