DevTools: cut host and port from webSocketDebuggerUrl in addition to ws:// prefix
[chromium-blink-merge.git] / content / browser / renderer_host / media / video_capture_device_client.cc
blobdd24208d9b271ab51bf2b7d5e18f1782d144ab6e
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
3 // found in the LICENSE file.
5 #include "content/browser/renderer_host/media/video_capture_device_client.h"
7 #include <algorithm>
9 #include "base/bind.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/trace_event/trace_event.h"
12 #include "content/browser/compositor/image_transport_factory.h"
13 #include "content/browser/gpu/browser_gpu_channel_host_factory.h"
14 #include "content/browser/gpu/browser_gpu_memory_buffer_manager.h"
15 #include "content/browser/gpu/gpu_data_manager_impl.h"
16 #include "content/browser/renderer_host/media/video_capture_buffer_pool.h"
17 #include "content/browser/renderer_host/media/video_capture_controller.h"
18 #include "content/browser/renderer_host/media/video_capture_gpu_jpeg_decoder.h"
19 #include "content/common/gpu/client/context_provider_command_buffer.h"
20 #include "content/common/gpu/client/gl_helper.h"
21 #include "content/common/gpu/client/gpu_channel_host.h"
22 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
23 #include "content/common/gpu/gpu_process_launch_causes.h"
24 #include "content/public/browser/browser_thread.h"
25 #include "gpu/command_buffer/common/mailbox_holder.h"
26 #include "media/base/bind_to_current_loop.h"
27 #include "media/base/video_capture_types.h"
28 #include "media/base/video_frame.h"
29 #include "third_party/khronos/GLES2/gl2ext.h"
30 #include "third_party/libyuv/include/libyuv.h"
32 using media::VideoCaptureFormat;
33 using media::VideoFrame;
34 using media::VideoFrameMetadata;
36 namespace content {
38 namespace {
40 #if !defined(OS_ANDROID)
41 // Modelled after GpuProcessTransportFactory::CreateContextCommon().
42 scoped_ptr<content::WebGraphicsContext3DCommandBufferImpl> CreateContextCommon(
43 scoped_refptr<content::GpuChannelHost> gpu_channel_host,
44 int surface_id) {
45 if (!content::GpuDataManagerImpl::GetInstance()->
46 CanUseGpuBrowserCompositor()) {
47 DLOG(ERROR) << "No accelerated graphics found. Check chrome://gpu";
48 return scoped_ptr<content::WebGraphicsContext3DCommandBufferImpl>();
50 blink::WebGraphicsContext3D::Attributes attrs;
51 attrs.shareResources = true;
52 attrs.depth = false;
53 attrs.stencil = false;
54 attrs.antialias = false;
55 attrs.noAutomaticFlushes = true;
57 if (!gpu_channel_host.get()) {
58 DLOG(ERROR) << "Failed to establish GPU channel.";
59 return scoped_ptr<content::WebGraphicsContext3DCommandBufferImpl>();
61 GURL url("chrome://gpu/GpuProcessTransportFactory::CreateCaptureContext");
62 return make_scoped_ptr(
63 new WebGraphicsContext3DCommandBufferImpl(
64 surface_id,
65 url,
66 gpu_channel_host.get(),
67 attrs,
68 true /* lose_context_when_out_of_memory */,
69 content::WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits(),
70 NULL));
73 // Modelled after
74 // GpuProcessTransportFactory::CreateOffscreenCommandBufferContext().
75 scoped_ptr<content::WebGraphicsContext3DCommandBufferImpl>
76 CreateOffscreenCommandBufferContext() {
77 content::CauseForGpuLaunch cause = content::CAUSE_FOR_GPU_LAUNCH_CANVAS_2D;
78 // Android does not support synchronous opening of GPU channels. Should use
79 // EstablishGpuChannel() instead.
80 if (!content::BrowserGpuChannelHostFactory::instance())
81 return scoped_ptr<content::WebGraphicsContext3DCommandBufferImpl>();
82 scoped_refptr<content::GpuChannelHost> gpu_channel_host(
83 content::BrowserGpuChannelHostFactory::instance()->
84 EstablishGpuChannelSync(cause));
85 DCHECK(gpu_channel_host);
86 return CreateContextCommon(gpu_channel_host, 0);
88 #endif
90 typedef base::Callback<void(scoped_refptr<ContextProviderCommandBuffer>)>
91 ProcessContextCallback;
93 void CreateContextOnUIThread(ProcessContextCallback bottom_half) {
94 DCHECK_CURRENTLY_ON(BrowserThread::UI);
95 #if !defined(OS_ANDROID)
96 bottom_half.Run(ContextProviderCommandBuffer::Create(
97 CreateOffscreenCommandBufferContext(), OFFSCREEN_VIDEO_CAPTURE_CONTEXT));
98 return;
99 #endif
102 void ResetLostContextCallback(
103 const scoped_refptr<ContextProviderCommandBuffer>& capture_thread_context) {
104 capture_thread_context->SetLostContextCallback(
105 cc::ContextProvider::LostContextCallback());
108 } // anonymous namespace
110 // Class combining a Client::Buffer interface implementation and a pool buffer
111 // implementation to guarantee proper cleanup on destruction on our side.
112 class AutoReleaseBuffer : public media::VideoCaptureDevice::Client::Buffer {
113 public:
114 AutoReleaseBuffer(const scoped_refptr<VideoCaptureBufferPool>& pool,
115 int buffer_id)
116 : id_(buffer_id),
117 pool_(pool),
118 buffer_handle_(pool_->GetBufferHandle(buffer_id).Pass()) {
119 DCHECK(pool_.get());
121 int id() const override { return id_; }
122 size_t size() const override { return buffer_handle_->size(); }
123 void* data() override { return buffer_handle_->data(); }
124 ClientBuffer AsClientBuffer() override {
125 return buffer_handle_->AsClientBuffer();
127 #if defined(OS_POSIX)
128 base::FileDescriptor AsPlatformFile() override {
129 return buffer_handle_->AsPlatformFile();
131 #endif
133 private:
134 ~AutoReleaseBuffer() override { pool_->RelinquishProducerReservation(id_); }
136 const int id_;
137 const scoped_refptr<VideoCaptureBufferPool> pool_;
138 const scoped_ptr<VideoCaptureBufferPool::BufferHandle> buffer_handle_;
141 // Internal ref-counted class wrapping an incoming GpuMemoryBuffer into a
142 // Texture backed VideoFrame. This VideoFrame creation is balanced by a waiting
143 // on the associated |sync_point|. After VideoFrame consumption the inserted
144 // ReleaseCallback() will be called, where the Texture is destroyed.
146 // This class jumps between threads due to GPU-related thread limitations, i.e.
147 // some objects cannot be accessed from IO Thread whereas others need to be
148 // constructed on UI Thread. For this reason most of the operations are carried
149 // out on Capture Thread (|capture_task_runner_|).
150 class VideoCaptureDeviceClient::TextureWrapHelper final
151 : public base::RefCountedThreadSafe<TextureWrapHelper> {
152 public:
153 TextureWrapHelper(
154 const base::WeakPtr<VideoCaptureController>& controller,
155 const scoped_refptr<base::SingleThreadTaskRunner>& capture_task_runner);
157 // Wraps the GpuMemoryBuffer-backed |buffer| into a Texture, and sends it to
158 // |controller_| wrapped in a VideoFrame.
159 void OnIncomingCapturedGpuMemoryBuffer(
160 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> buffer,
161 const media::VideoCaptureFormat& frame_format,
162 const base::TimeTicks& timestamp);
164 private:
165 friend class base::RefCountedThreadSafe<TextureWrapHelper>;
166 ~TextureWrapHelper();
168 // Creates some necessary members in |capture_task_runner_|.
169 void Init();
170 // Runs the bottom half of the GlHelper creation.
171 void CreateGlHelper(
172 scoped_refptr<ContextProviderCommandBuffer> capture_thread_context);
174 // Recycles |memory_buffer|, deletes Image and Texture on VideoFrame release.
175 void ReleaseCallback(GLuint image_id,
176 GLuint texture_id,
177 uint32 sync_point);
179 // The Command Buffer lost the GL context, f.i. GPU process crashed. Signal
180 // error to our owner so the capture can be torn down.
181 void LostContextCallback();
183 // Prints the error |message| and notifies |controller_| of an error.
184 void OnError(const std::string& message);
186 // |controller_| should only be used on IO thread.
187 const base::WeakPtr<VideoCaptureController> controller_;
188 const scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner_;
190 // Command buffer reference, needs to be destroyed when unused. It is created
191 // on UI Thread and bound to Capture Thread. In particular, it cannot be used
192 // from IO Thread.
193 scoped_refptr<ContextProviderCommandBuffer> capture_thread_context_;
194 // Created and used from Capture Thread. Cannot be used from IO Thread.
195 scoped_ptr<GLHelper> gl_helper_;
197 DISALLOW_COPY_AND_ASSIGN(TextureWrapHelper);
200 VideoCaptureDeviceClient::VideoCaptureDeviceClient(
201 const base::WeakPtr<VideoCaptureController>& controller,
202 const scoped_refptr<VideoCaptureBufferPool>& buffer_pool,
203 const scoped_refptr<base::SingleThreadTaskRunner>& capture_task_runner)
204 : controller_(controller),
205 external_jpeg_decoder_initialized_(false),
206 buffer_pool_(buffer_pool),
207 capture_task_runner_(capture_task_runner),
208 last_captured_pixel_format_(media::PIXEL_FORMAT_UNKNOWN) {
209 DCHECK_CURRENTLY_ON(BrowserThread::IO);
212 VideoCaptureDeviceClient::~VideoCaptureDeviceClient() {
213 // This should be on the platform auxiliary thread since
214 // |external_jpeg_decoder_| need to be destructed on the same thread as
215 // OnIncomingCapturedData.
218 void VideoCaptureDeviceClient::OnIncomingCapturedData(
219 const uint8* data,
220 int length,
221 const VideoCaptureFormat& frame_format,
222 int rotation,
223 const base::TimeTicks& timestamp) {
224 TRACE_EVENT0("video", "VideoCaptureDeviceClient::OnIncomingCapturedData");
225 DCHECK_EQ(media::PIXEL_STORAGE_CPU, frame_format.pixel_storage);
227 if (last_captured_pixel_format_ != frame_format.pixel_format) {
228 OnLog("Pixel format: " +
229 VideoCaptureFormat::PixelFormatToString(frame_format.pixel_format));
230 last_captured_pixel_format_ = frame_format.pixel_format;
232 if (frame_format.pixel_format == media::PIXEL_FORMAT_MJPEG &&
233 VideoCaptureGpuJpegDecoder::Supported()) {
234 if (!external_jpeg_decoder_initialized_) {
235 external_jpeg_decoder_initialized_ = true;
236 // base::Unretained is safe because |this| outlives
237 // |external_jpeg_decoder_| and the callbacks are never called after
238 // |external_jpeg_decoder_| is destroyed.
239 external_jpeg_decoder_.reset(new VideoCaptureGpuJpegDecoder(
240 base::Bind(
241 &VideoCaptureController::DoIncomingCapturedVideoFrameOnIOThread,
242 controller_),
243 // TODO(kcwu): fallback to software decode if error.
244 // https://crbug.com/503532
245 base::Bind(&VideoCaptureDeviceClient::OnError,
246 base::Unretained(this))));
247 external_jpeg_decoder_->Initialize();
252 if (!frame_format.IsValid())
253 return;
255 // |chopped_{width,height} and |new_unrotated_{width,height}| are the lowest
256 // bit decomposition of {width, height}, grabbing the odd and even parts.
257 const int chopped_width = frame_format.frame_size.width() & 1;
258 const int chopped_height = frame_format.frame_size.height() & 1;
259 const int new_unrotated_width = frame_format.frame_size.width() & ~1;
260 const int new_unrotated_height = frame_format.frame_size.height() & ~1;
262 int destination_width = new_unrotated_width;
263 int destination_height = new_unrotated_height;
264 if (rotation == 90 || rotation == 270)
265 std::swap(destination_width, destination_height);
267 DCHECK_EQ(0, rotation % 90)
268 << " Rotation must be a multiple of 90, now: " << rotation;
269 libyuv::RotationMode rotation_mode = libyuv::kRotate0;
270 if (rotation == 90)
271 rotation_mode = libyuv::kRotate90;
272 else if (rotation == 180)
273 rotation_mode = libyuv::kRotate180;
274 else if (rotation == 270)
275 rotation_mode = libyuv::kRotate270;
277 const gfx::Size dimensions(destination_width, destination_height);
278 if (!VideoFrame::IsValidConfig(VideoFrame::I420,
279 VideoFrame::STORAGE_UNKNOWN,
280 dimensions,
281 gfx::Rect(dimensions),
282 dimensions)) {
283 return;
286 scoped_ptr<Buffer> buffer(ReserveOutputBuffer(
287 dimensions, media::PIXEL_FORMAT_I420, media::PIXEL_STORAGE_CPU));
288 if (!buffer.get())
289 return;
291 const size_t y_plane_size = VideoFrame::PlaneSize(
292 VideoFrame::I420, VideoFrame::kYPlane, dimensions).GetArea();
293 const size_t u_plane_size = VideoFrame::PlaneSize(
294 VideoFrame::I420, VideoFrame::kUPlane, dimensions).GetArea();
295 uint8* const yplane = reinterpret_cast<uint8*>(buffer->data());
296 uint8* const uplane = yplane + y_plane_size;
297 uint8* const vplane = uplane + u_plane_size;
299 const int yplane_stride = dimensions.width();
300 const int uv_plane_stride = yplane_stride / 2;
301 int crop_x = 0;
302 int crop_y = 0;
303 libyuv::FourCC origin_colorspace = libyuv::FOURCC_ANY;
305 bool flip = false;
306 switch (frame_format.pixel_format) {
307 case media::PIXEL_FORMAT_UNKNOWN: // Color format not set.
308 break;
309 case media::PIXEL_FORMAT_I420:
310 DCHECK(!chopped_width && !chopped_height);
311 origin_colorspace = libyuv::FOURCC_I420;
312 break;
313 case media::PIXEL_FORMAT_YV12:
314 DCHECK(!chopped_width && !chopped_height);
315 origin_colorspace = libyuv::FOURCC_YV12;
316 break;
317 case media::PIXEL_FORMAT_NV12:
318 DCHECK(!chopped_width && !chopped_height);
319 origin_colorspace = libyuv::FOURCC_NV12;
320 break;
321 case media::PIXEL_FORMAT_NV21:
322 DCHECK(!chopped_width && !chopped_height);
323 origin_colorspace = libyuv::FOURCC_NV21;
324 break;
325 case media::PIXEL_FORMAT_YUY2:
326 DCHECK(!chopped_width && !chopped_height);
327 origin_colorspace = libyuv::FOURCC_YUY2;
328 break;
329 case media::PIXEL_FORMAT_UYVY:
330 DCHECK(!chopped_width && !chopped_height);
331 origin_colorspace = libyuv::FOURCC_UYVY;
332 break;
333 case media::PIXEL_FORMAT_RGB24:
334 origin_colorspace = libyuv::FOURCC_24BG;
335 #if defined(OS_WIN)
336 // TODO(wjia): Currently, for RGB24 on WIN, capture device always
337 // passes in positive src_width and src_height. Remove this hardcoded
338 // value when nagative src_height is supported. The negative src_height
339 // indicates that vertical flipping is needed.
340 flip = true;
341 #endif
342 break;
343 case media::PIXEL_FORMAT_RGB32:
344 // Fallback to PIXEL_FORMAT_ARGB setting |flip| in Windows platforms.
345 #if defined(OS_WIN)
346 flip = true;
347 #endif
348 case media::PIXEL_FORMAT_ARGB:
349 origin_colorspace = libyuv::FOURCC_ARGB;
350 break;
351 case media::PIXEL_FORMAT_MJPEG:
352 origin_colorspace = libyuv::FOURCC_MJPG;
353 break;
354 default:
355 NOTREACHED();
358 // The input |length| can be greater than the required buffer size because of
359 // paddings and/or alignments, but it cannot be smaller.
360 DCHECK_GE(static_cast<size_t>(length), frame_format.ImageAllocationSize());
362 if (external_jpeg_decoder_ &&
363 frame_format.pixel_format == media::PIXEL_FORMAT_MJPEG && rotation == 0 &&
364 !flip && external_jpeg_decoder_->ReadyToDecode()) {
365 external_jpeg_decoder_->DecodeCapturedData(data, length, frame_format,
366 timestamp, buffer.Pass());
367 return;
370 if (libyuv::ConvertToI420(data,
371 length,
372 yplane,
373 yplane_stride,
374 uplane,
375 uv_plane_stride,
376 vplane,
377 uv_plane_stride,
378 crop_x,
379 crop_y,
380 frame_format.frame_size.width(),
381 (flip ? -1 : 1) * frame_format.frame_size.height(),
382 new_unrotated_width,
383 new_unrotated_height,
384 rotation_mode,
385 origin_colorspace) != 0) {
386 DLOG(WARNING) << "Failed to convert buffer's pixel format to I420 from "
387 << VideoCaptureFormat::PixelFormatToString(
388 frame_format.pixel_format);
389 return;
392 const VideoCaptureFormat output_format = VideoCaptureFormat(
393 dimensions, frame_format.frame_rate, media::PIXEL_FORMAT_I420,
394 media::PIXEL_STORAGE_CPU);
395 OnIncomingCapturedBuffer(buffer.Pass(), output_format, timestamp);
398 void
399 VideoCaptureDeviceClient::OnIncomingCapturedYuvData(
400 const uint8* y_data,
401 const uint8* u_data,
402 const uint8* v_data,
403 size_t y_stride,
404 size_t u_stride,
405 size_t v_stride,
406 const VideoCaptureFormat& frame_format,
407 int clockwise_rotation,
408 const base::TimeTicks& timestamp) {
409 TRACE_EVENT0("video", "VideoCaptureDeviceClient::OnIncomingCapturedYuvData");
410 DCHECK_EQ(media::PIXEL_FORMAT_I420, frame_format.pixel_format);
411 DCHECK_EQ(media::PIXEL_STORAGE_CPU, frame_format.pixel_storage);
412 DCHECK_EQ(0, clockwise_rotation) << "Rotation not supported";
414 scoped_ptr<Buffer> buffer(ReserveOutputBuffer(frame_format.frame_size,
415 frame_format.pixel_format,
416 frame_format.pixel_storage));
417 if (!buffer.get())
418 return;
420 // Blit (copy) here from y,u,v into buffer.data()). Needed so we can return
421 // the parameter buffer synchronously to the driver.
422 const size_t y_plane_size = VideoFrame::PlaneSize(
423 VideoFrame::I420, VideoFrame::kYPlane, frame_format.frame_size).GetArea();
424 const size_t u_plane_size = VideoFrame::PlaneSize(
425 VideoFrame::I420, VideoFrame::kUPlane, frame_format.frame_size).GetArea();
426 uint8* const dst_y = reinterpret_cast<uint8*>(buffer->data());
427 uint8* const dst_u = dst_y + y_plane_size;
428 uint8* const dst_v = dst_u + u_plane_size;
430 const size_t dst_y_stride = VideoFrame::RowBytes(
431 VideoFrame::kYPlane, VideoFrame::I420, frame_format.frame_size.width());
432 const size_t dst_u_stride = VideoFrame::RowBytes(
433 VideoFrame::kUPlane, VideoFrame::I420, frame_format.frame_size.width());
434 const size_t dst_v_stride = VideoFrame::RowBytes(
435 VideoFrame::kVPlane, VideoFrame::I420, frame_format.frame_size.width());
436 DCHECK_GE(y_stride, dst_y_stride);
437 DCHECK_GE(u_stride, dst_u_stride);
438 DCHECK_GE(v_stride, dst_v_stride);
440 if (libyuv::I420Copy(y_data, y_stride,
441 u_data, u_stride,
442 v_data, v_stride,
443 dst_y, dst_y_stride,
444 dst_u, dst_u_stride,
445 dst_v, dst_v_stride,
446 frame_format.frame_size.width(),
447 frame_format.frame_size.height())) {
448 DLOG(WARNING) << "Failed to copy buffer";
449 return;
452 OnIncomingCapturedBuffer(buffer.Pass(), frame_format, timestamp);
455 scoped_ptr<media::VideoCaptureDevice::Client::Buffer>
456 VideoCaptureDeviceClient::ReserveOutputBuffer(
457 const gfx::Size& frame_size,
458 media::VideoPixelFormat pixel_format,
459 media::VideoPixelStorage pixel_storage) {
460 DCHECK(pixel_format == media::PIXEL_FORMAT_I420 ||
461 pixel_format == media::PIXEL_FORMAT_ARGB);
462 DCHECK_GT(frame_size.width(), 0);
463 DCHECK_GT(frame_size.height(), 0);
465 if (pixel_storage == media::PIXEL_STORAGE_GPUMEMORYBUFFER &&
466 !texture_wrap_helper_) {
467 texture_wrap_helper_ =
468 new TextureWrapHelper(controller_, capture_task_runner_);
471 // TODO(mcasas): For PIXEL_STORAGE_GPUMEMORYBUFFER, find a way to indicate if
472 // it's a ShMem GMB or a DmaBuf GMB.
473 int buffer_id_to_drop = VideoCaptureBufferPool::kInvalidId;
474 const int buffer_id = buffer_pool_->ReserveForProducer(
475 pixel_format, pixel_storage, frame_size, &buffer_id_to_drop);
476 if (buffer_id == VideoCaptureBufferPool::kInvalidId)
477 return NULL;
479 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> output_buffer(
480 new AutoReleaseBuffer(buffer_pool_, buffer_id));
482 if (buffer_id_to_drop != VideoCaptureBufferPool::kInvalidId) {
483 BrowserThread::PostTask(BrowserThread::IO,
484 FROM_HERE,
485 base::Bind(&VideoCaptureController::DoBufferDestroyedOnIOThread,
486 controller_, buffer_id_to_drop));
489 return output_buffer.Pass();
492 void VideoCaptureDeviceClient::OnIncomingCapturedBuffer(
493 scoped_ptr<Buffer> buffer,
494 const VideoCaptureFormat& frame_format,
495 const base::TimeTicks& timestamp) {
496 if (frame_format.pixel_storage == media::PIXEL_STORAGE_GPUMEMORYBUFFER) {
497 capture_task_runner_->PostTask(
498 FROM_HERE,
499 base::Bind(&TextureWrapHelper::OnIncomingCapturedGpuMemoryBuffer,
500 texture_wrap_helper_,
501 base::Passed(&buffer),
502 frame_format,
503 timestamp));
504 } else {
505 DCHECK(frame_format.pixel_format == media::PIXEL_FORMAT_I420 ||
506 frame_format.pixel_format == media::PIXEL_FORMAT_ARGB);
507 scoped_refptr<VideoFrame> video_frame =
508 VideoFrame::WrapExternalData(
509 VideoFrame::I420,
510 frame_format.frame_size,
511 gfx::Rect(frame_format.frame_size),
512 frame_format.frame_size,
513 reinterpret_cast<uint8*>(buffer->data()),
514 VideoFrame::AllocationSize(VideoFrame::I420,
515 frame_format.frame_size),
516 base::TimeDelta());
517 DCHECK(video_frame.get());
518 video_frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE,
519 frame_format.frame_rate);
520 OnIncomingCapturedVideoFrame(buffer.Pass(), video_frame, timestamp);
524 void VideoCaptureDeviceClient::OnIncomingCapturedVideoFrame(
525 scoped_ptr<Buffer> buffer,
526 const scoped_refptr<VideoFrame>& frame,
527 const base::TimeTicks& timestamp) {
528 BrowserThread::PostTask(
529 BrowserThread::IO,
530 FROM_HERE,
531 base::Bind(
532 &VideoCaptureController::DoIncomingCapturedVideoFrameOnIOThread,
533 controller_,
534 base::Passed(&buffer),
535 frame,
536 timestamp));
539 void VideoCaptureDeviceClient::OnError(
540 const std::string& reason) {
541 const std::string log_message = base::StringPrintf(
542 "Error on video capture: %s, OS message: %s",
543 reason.c_str(),
544 logging::SystemErrorCodeToString(
545 logging::GetLastSystemErrorCode()).c_str());
546 DLOG(ERROR) << log_message;
547 OnLog(log_message);
548 BrowserThread::PostTask(BrowserThread::IO,
549 FROM_HERE,
550 base::Bind(&VideoCaptureController::DoErrorOnIOThread, controller_));
553 void VideoCaptureDeviceClient::OnLog(
554 const std::string& message) {
555 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
556 base::Bind(&VideoCaptureController::DoLogOnIOThread,
557 controller_, message));
560 double VideoCaptureDeviceClient::GetBufferPoolUtilization() const {
561 // VideoCaptureBufferPool::GetBufferPoolUtilization() is thread-safe.
562 return buffer_pool_->GetBufferPoolUtilization();
565 VideoCaptureDeviceClient::TextureWrapHelper::TextureWrapHelper(
566 const base::WeakPtr<VideoCaptureController>& controller,
567 const scoped_refptr<base::SingleThreadTaskRunner>& capture_task_runner)
568 : controller_(controller),
569 capture_task_runner_(capture_task_runner) {
570 capture_task_runner_->PostTask(FROM_HERE,
571 base::Bind(&TextureWrapHelper::Init, this));
574 void
575 VideoCaptureDeviceClient::TextureWrapHelper::OnIncomingCapturedGpuMemoryBuffer(
576 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> buffer,
577 const media::VideoCaptureFormat& frame_format,
578 const base::TimeTicks& timestamp) {
579 DCHECK(capture_task_runner_->BelongsToCurrentThread());
580 DCHECK_EQ(media::PIXEL_FORMAT_ARGB, frame_format.pixel_format);
581 DCHECK_EQ(media::PIXEL_STORAGE_GPUMEMORYBUFFER, frame_format.pixel_storage);
582 if (!gl_helper_) {
583 // |gl_helper_| might not exist due to asynchronous initialization not
584 // finished or due to termination in process after a context loss.
585 DVLOG(1) << " Skipping ingress frame, no GL context.";
586 return;
589 gpu::gles2::GLES2Interface* gl = capture_thread_context_->ContextGL();
590 GLuint image_id = gl->CreateImageCHROMIUM(buffer->AsClientBuffer(),
591 frame_format.frame_size.width(),
592 frame_format.frame_size.height(),
593 GL_BGRA_EXT);
594 DCHECK(image_id);
596 const GLuint texture_id = gl_helper_->CreateTexture();
597 DCHECK(texture_id);
599 content::ScopedTextureBinder<GL_TEXTURE_2D> texture_binder(gl, texture_id);
600 gl->BindTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id);
603 const gpu::MailboxHolder& mailbox_holder(
604 gl_helper_->ProduceMailboxHolderFromTexture(texture_id));
605 DCHECK(!mailbox_holder.mailbox.IsZero());
606 DCHECK(mailbox_holder.mailbox.Verify());
607 DCHECK(mailbox_holder.texture_target);
608 DCHECK(mailbox_holder.sync_point);
610 scoped_refptr<media::VideoFrame> video_frame =
611 media::VideoFrame::WrapNativeTexture(
612 media::VideoFrame::ARGB,
613 mailbox_holder,
614 media::BindToCurrentLoop(base::Bind(
615 &VideoCaptureDeviceClient::TextureWrapHelper::ReleaseCallback,
616 this, image_id, texture_id)),
617 frame_format.frame_size, gfx::Rect(frame_format.frame_size),
618 frame_format.frame_size, base::TimeDelta());
619 video_frame->metadata()->SetBoolean(VideoFrameMetadata::ALLOW_OVERLAY, true);
620 video_frame->metadata()->SetDouble(VideoFrameMetadata::FRAME_RATE,
621 frame_format.frame_rate);
622 #if defined(OS_LINUX)
623 // TODO(mcasas): After http://crev.com/1179323002, use |frame_format| to query
624 // the storage type of the buffer and use the appropriate |video_frame| method.
625 #if defined(USE_OZONE)
626 DCHECK_EQ(1u, media::VideoFrame::NumPlanes(video_frame->format()));
627 video_frame->DuplicateFileDescriptors(
628 std::vector<int>(1, buffer->AsPlatformFile().fd));
629 #else
630 video_frame->AddSharedMemoryHandle(buffer->AsPlatformFile());
631 #endif
633 #endif
634 //TODO(mcasas): use AddSharedMemoryHandle() for gfx::SHARED_MEMORY_BUFFER.
636 BrowserThread::PostTask(
637 BrowserThread::IO, FROM_HERE,
638 base::Bind(
639 &VideoCaptureController::DoIncomingCapturedVideoFrameOnIOThread,
640 controller_, base::Passed(&buffer), video_frame, timestamp));
643 VideoCaptureDeviceClient::TextureWrapHelper::~TextureWrapHelper() {
644 // Might not be running on capture_task_runner_'s thread. Ensure owned objects
645 // are destroyed on the correct threads.
646 if (gl_helper_)
647 capture_task_runner_->DeleteSoon(FROM_HERE, gl_helper_.release());
649 if (capture_thread_context_) {
650 capture_task_runner_->PostTask(
651 FROM_HERE,
652 base::Bind(&ResetLostContextCallback, capture_thread_context_));
653 capture_thread_context_->AddRef();
654 ContextProviderCommandBuffer* raw_capture_thread_context =
655 capture_thread_context_.get();
656 capture_thread_context_ = nullptr;
657 capture_task_runner_->ReleaseSoon(FROM_HERE, raw_capture_thread_context);
661 void VideoCaptureDeviceClient::TextureWrapHelper::Init() {
662 DCHECK(capture_task_runner_->BelongsToCurrentThread());
664 // In threaded compositing mode, we have to create our own context for Capture
665 // to avoid using the GPU command queue from multiple threads. Context
666 // creation must happen on UI thread; then the context needs to be bound to
667 // the appropriate thread, which is done in CreateGlHelper().
668 BrowserThread::PostTask(
669 BrowserThread::UI, FROM_HERE,
670 base::Bind(
671 &CreateContextOnUIThread,
672 media::BindToCurrentLoop(base::Bind(
673 &VideoCaptureDeviceClient::TextureWrapHelper::CreateGlHelper,
674 this))));
677 void VideoCaptureDeviceClient::TextureWrapHelper::CreateGlHelper(
678 scoped_refptr<ContextProviderCommandBuffer> capture_thread_context) {
679 DCHECK(capture_task_runner_->BelongsToCurrentThread());
681 if (!capture_thread_context.get()) {
682 DLOG(ERROR) << "No offscreen GL Context!";
683 return;
685 // This may not happen in IO Thread. The destructor resets the context lost
686 // callback, so base::Unretained is safe; otherwise it'd be a circular ref
687 // counted dependency.
688 capture_thread_context->SetLostContextCallback(media::BindToCurrentLoop(
689 base::Bind(
690 &VideoCaptureDeviceClient::TextureWrapHelper::LostContextCallback,
691 base::Unretained(this))));
692 if (!capture_thread_context->BindToCurrentThread()) {
693 capture_thread_context = NULL;
694 DLOG(ERROR) << "Couldn't bind the Capture Context to the Capture Thread.";
695 return;
697 DCHECK(capture_thread_context);
698 capture_thread_context_ = capture_thread_context;
700 // At this point, |capture_thread_context| is a cc::ContextProvider. Creation
701 // of our GLHelper should happen on Capture Thread.
702 gl_helper_.reset(new GLHelper(capture_thread_context->ContextGL(),
703 capture_thread_context->ContextSupport()));
704 DCHECK(gl_helper_);
707 void VideoCaptureDeviceClient::TextureWrapHelper::ReleaseCallback(
708 GLuint image_id,
709 GLuint texture_id,
710 uint32 sync_point) {
711 DCHECK(capture_task_runner_->BelongsToCurrentThread());
713 if (gl_helper_) {
714 gl_helper_->DeleteTexture(texture_id);
715 capture_thread_context_->ContextGL()->DestroyImageCHROMIUM(image_id);
719 void VideoCaptureDeviceClient::TextureWrapHelper::LostContextCallback() {
720 DCHECK(capture_task_runner_->BelongsToCurrentThread());
721 // Prevent incoming frames from being processed while OnError gets groked.
722 gl_helper_.reset();
723 OnError("GLContext lost");
726 void VideoCaptureDeviceClient::TextureWrapHelper::OnError(
727 const std::string& message) {
728 DCHECK(capture_task_runner_->BelongsToCurrentThread());
729 DLOG(ERROR) << message;
730 BrowserThread::PostTask(
731 BrowserThread::IO, FROM_HERE,
732 base::Bind(&VideoCaptureController::DoErrorOnIOThread, controller_));
735 } // namespace content