Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/browser/renderer_host/media/video_capture_device_client.h" | 5 #include "content/browser/renderer_host/media/video_capture_device_client.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/trace_event/trace_event.h" | 9 #include "base/trace_event/trace_event.h" |
| 10 #include "content/browser/compositor/image_transport_factory.h" | 10 #include "content/browser/compositor/image_transport_factory.h" |
| 11 #include "content/browser/gpu/browser_gpu_channel_host_factory.h" | 11 #include "content/browser/gpu/browser_gpu_channel_host_factory.h" |
| 12 #include "content/browser/gpu/browser_gpu_memory_buffer_manager.h" | 12 #include "content/browser/gpu/browser_gpu_memory_buffer_manager.h" |
| 13 #include "content/browser/gpu/gpu_data_manager_impl.h" | 13 #include "content/browser/gpu/gpu_data_manager_impl.h" |
| 14 #include "content/browser/renderer_host/gpu_jpeg_decoder.h" | |
| 14 #include "content/browser/renderer_host/media/video_capture_buffer_pool.h" | 15 #include "content/browser/renderer_host/media/video_capture_buffer_pool.h" |
| 15 #include "content/browser/renderer_host/media/video_capture_controller.h" | 16 #include "content/browser/renderer_host/media/video_capture_controller.h" |
| 16 #include "content/common/gpu/client/context_provider_command_buffer.h" | 17 #include "content/common/gpu/client/context_provider_command_buffer.h" |
| 17 #include "content/common/gpu/client/gl_helper.h" | 18 #include "content/common/gpu/client/gl_helper.h" |
| 18 #include "content/common/gpu/client/gpu_channel_host.h" | 19 #include "content/common/gpu/client/gpu_channel_host.h" |
| 19 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h" | 20 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h" |
| 20 #include "content/common/gpu/gpu_process_launch_causes.h" | 21 #include "content/common/gpu/gpu_process_launch_causes.h" |
| 21 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
| 22 #include "gpu/command_buffer/common/mailbox_holder.h" | 23 #include "gpu/command_buffer/common/mailbox_holder.h" |
| 23 #include "media/base/bind_to_current_loop.h" | 24 #include "media/base/bind_to_current_loop.h" |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 116 } | 117 } |
| 117 int id() const override { return id_; } | 118 int id() const override { return id_; } |
| 118 size_t size() const override { return buffer_handle_->size(); } | 119 size_t size() const override { return buffer_handle_->size(); } |
| 119 void* data() override { return buffer_handle_->data(); } | 120 void* data() override { return buffer_handle_->data(); } |
| 120 gfx::GpuMemoryBufferType GetType() override { | 121 gfx::GpuMemoryBufferType GetType() override { |
| 121 return buffer_handle_->GetType(); | 122 return buffer_handle_->GetType(); |
| 122 } | 123 } |
| 123 ClientBuffer AsClientBuffer() override { | 124 ClientBuffer AsClientBuffer() override { |
| 124 return buffer_handle_->AsClientBuffer(); | 125 return buffer_handle_->AsClientBuffer(); |
| 125 } | 126 } |
| 127 base::PlatformFile AsPlatformHandle() override { | |
| 128 return buffer_handle_->AsPlatformHandle(); | |
| 129 } | |
| 126 | 130 |
| 127 private: | 131 private: |
| 128 ~AutoReleaseBuffer() override { pool_->RelinquishProducerReservation(id_); } | 132 ~AutoReleaseBuffer() override { pool_->RelinquishProducerReservation(id_); } |
| 129 | 133 |
| 130 const int id_; | 134 const int id_; |
| 131 const scoped_refptr<VideoCaptureBufferPool> pool_; | 135 const scoped_refptr<VideoCaptureBufferPool> pool_; |
| 132 const scoped_ptr<VideoCaptureBufferPool::BufferHandle> buffer_handle_; | 136 const scoped_ptr<VideoCaptureBufferPool::BufferHandle> buffer_handle_; |
| 133 }; | 137 }; |
| 134 | 138 |
| 135 // Internal ref-counted class wrapping an incoming GpuMemoryBuffer into a | 139 // Internal ref-counted class wrapping an incoming GpuMemoryBuffer into a |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 189 scoped_ptr<GLHelper> gl_helper_; | 193 scoped_ptr<GLHelper> gl_helper_; |
| 190 | 194 |
| 191 DISALLOW_COPY_AND_ASSIGN(TextureWrapHelper); | 195 DISALLOW_COPY_AND_ASSIGN(TextureWrapHelper); |
| 192 }; | 196 }; |
| 193 | 197 |
| 194 VideoCaptureDeviceClient::VideoCaptureDeviceClient( | 198 VideoCaptureDeviceClient::VideoCaptureDeviceClient( |
| 195 const base::WeakPtr<VideoCaptureController>& controller, | 199 const base::WeakPtr<VideoCaptureController>& controller, |
| 196 const scoped_refptr<VideoCaptureBufferPool>& buffer_pool, | 200 const scoped_refptr<VideoCaptureBufferPool>& buffer_pool, |
| 197 const scoped_refptr<base::SingleThreadTaskRunner>& capture_task_runner) | 201 const scoped_refptr<base::SingleThreadTaskRunner>& capture_task_runner) |
| 198 : controller_(controller), | 202 : controller_(controller), |
| 203 initialized_external_jpeg_decoder_(false), | |
| 199 buffer_pool_(buffer_pool), | 204 buffer_pool_(buffer_pool), |
| 200 capture_task_runner_(capture_task_runner), | 205 capture_task_runner_(capture_task_runner), |
| 201 last_captured_pixel_format_(media::PIXEL_FORMAT_UNKNOWN) { | 206 last_captured_pixel_format_(media::PIXEL_FORMAT_UNKNOWN) { |
| 202 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 207 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 203 } | 208 } |
| 204 | 209 |
| 205 VideoCaptureDeviceClient::~VideoCaptureDeviceClient() {} | 210 VideoCaptureDeviceClient::~VideoCaptureDeviceClient() { |
| 211 } | |
| 206 | 212 |
| 207 void VideoCaptureDeviceClient::OnIncomingCapturedData( | 213 void VideoCaptureDeviceClient::OnIncomingCapturedData( |
| 208 const uint8* data, | 214 const uint8* data, |
| 209 int length, | 215 int length, |
| 210 const VideoCaptureFormat& frame_format, | 216 const VideoCaptureFormat& frame_format, |
| 211 int rotation, | 217 int rotation, |
| 212 const base::TimeTicks& timestamp) { | 218 const base::TimeTicks& timestamp) { |
| 213 TRACE_EVENT0("video", "VideoCaptureDeviceClient::OnIncomingCapturedData"); | 219 TRACE_EVENT0("video", "VideoCaptureDeviceClient::OnIncomingCapturedData"); |
| 214 | 220 |
| 215 if (last_captured_pixel_format_ != frame_format.pixel_format) { | 221 if (last_captured_pixel_format_ != frame_format.pixel_format) { |
| 216 OnLog("Pixel format: " + media::VideoCaptureFormat::PixelFormatToString( | 222 OnLog("Pixel format: " + media::VideoCaptureFormat::PixelFormatToString( |
| 217 frame_format.pixel_format)); | 223 frame_format.pixel_format)); |
| 218 last_captured_pixel_format_ = frame_format.pixel_format; | 224 last_captured_pixel_format_ = frame_format.pixel_format; |
| 225 | |
| 226 if (frame_format.pixel_format == media::PIXEL_FORMAT_MJPEG && | |
| 227 !initialized_external_jpeg_decoder_) { | |
| 228 initialized_external_jpeg_decoder_ = true; | |
| 229 if (GpuJpegDecoder::Supported()) { | |
| 230 external_jpeg_decoder_.reset(new GpuJpegDecoder( | |
| 231 base::Bind(&VideoCaptureDeviceClient::OnIncomingCapturedVideoFrame, | |
| 232 AsWeakPtr()), | |
| 233 // TODO(kcwu): fallback to software decode if error | |
| 234 base::Bind(&VideoCaptureDeviceClient::OnError, AsWeakPtr()))); | |
| 235 if (!external_jpeg_decoder_->Initialize()) | |
| 236 external_jpeg_decoder_.reset(); | |
| 237 } | |
| 238 } | |
| 219 } | 239 } |
| 220 | 240 |
| 221 if (!frame_format.IsValid()) | 241 if (!frame_format.IsValid()) |
| 222 return; | 242 return; |
| 223 | 243 |
| 224 // |chopped_{width,height} and |new_unrotated_{width,height}| are the lowest | 244 // |chopped_{width,height} and |new_unrotated_{width,height}| are the lowest |
| 225 // bit decomposition of {width, height}, grabbing the odd and even parts. | 245 // bit decomposition of {width, height}, grabbing the odd and even parts. |
| 226 const int chopped_width = frame_format.frame_size.width() & 1; | 246 const int chopped_width = frame_format.frame_size.width() & 1; |
| 227 const int chopped_height = frame_format.frame_size.height() & 1; | 247 const int chopped_height = frame_format.frame_size.height() & 1; |
| 228 const int new_unrotated_width = frame_format.frame_size.width() & ~1; | 248 const int new_unrotated_width = frame_format.frame_size.width() & ~1; |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 321 origin_colorspace = libyuv::FOURCC_MJPG; | 341 origin_colorspace = libyuv::FOURCC_MJPG; |
| 322 break; | 342 break; |
| 323 default: | 343 default: |
| 324 NOTREACHED(); | 344 NOTREACHED(); |
| 325 } | 345 } |
| 326 | 346 |
| 327 // The input |length| can be greater than the required buffer size because of | 347 // The input |length| can be greater than the required buffer size because of |
| 328 // paddings and/or alignments, but it cannot be smaller. | 348 // paddings and/or alignments, but it cannot be smaller. |
| 329 DCHECK_GE(static_cast<size_t>(length), frame_format.ImageAllocationSize()); | 349 DCHECK_GE(static_cast<size_t>(length), frame_format.ImageAllocationSize()); |
| 330 | 350 |
| 351 if (frame_format.pixel_format == media::PIXEL_FORMAT_MJPEG && rotation == 0 && | |
| 352 external_jpeg_decoder_) { | |
|
wuchengli
2015/05/13 15:04:06
&& !flip
kcwu
2015/05/25 18:20:35
Done.
| |
| 353 external_jpeg_decoder_->DecodeCapturedData(data, length, frame_format, | |
| 354 timestamp, buffer.Pass()); | |
| 355 return; | |
| 356 } | |
| 357 | |
| 331 if (libyuv::ConvertToI420(data, | 358 if (libyuv::ConvertToI420(data, |
| 332 length, | 359 length, |
| 333 yplane, | 360 yplane, |
| 334 yplane_stride, | 361 yplane_stride, |
| 335 uplane, | 362 uplane, |
| 336 uv_plane_stride, | 363 uv_plane_stride, |
| 337 vplane, | 364 vplane, |
| 338 uv_plane_stride, | 365 uv_plane_stride, |
| 339 crop_x, | 366 crop_x, |
| 340 crop_y, | 367 crop_y, |
| (...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 663 void VideoCaptureDeviceClient::TextureWrapHelper::OnError( | 690 void VideoCaptureDeviceClient::TextureWrapHelper::OnError( |
| 664 const std::string& message) { | 691 const std::string& message) { |
| 665 DCHECK(capture_task_runner_->BelongsToCurrentThread()); | 692 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
| 666 DLOG(ERROR) << message; | 693 DLOG(ERROR) << message; |
| 667 BrowserThread::PostTask( | 694 BrowserThread::PostTask( |
| 668 BrowserThread::IO, FROM_HERE, | 695 BrowserThread::IO, FROM_HERE, |
| 669 base::Bind(&VideoCaptureController::DoErrorOnIOThread, controller_)); | 696 base::Bind(&VideoCaptureController::DoErrorOnIOThread, controller_)); |
| 670 } | 697 } |
| 671 | 698 |
| 672 } // namespace content | 699 } // namespace content |
| OLD | NEW |