Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / renderer_host / media / video_capture_device_client.h
blob80838ee3287f63ca9b6f831c9c08dbd76419bc4f
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 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_DEVICE_CLIENT_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_DEVICE_CLIENT_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
11 #include "content/common/content_export.h"
12 #include "media/capture/video/video_capture_device.h"
14 namespace content {
15 class VideoCaptureBufferPool;
16 class VideoCaptureController;
17 class VideoCaptureGpuJpegDecoder;
19 // Receives events from the VideoCaptureDevice and posts them to a |controller_|
20 // on the IO thread. An instance of this class may safely outlive its target
21 // VideoCaptureController. This is a shallow class meant to convert incoming
22 // frames and holds no significant state.
24 // Methods of this class may be called from any thread, and in practice will
25 // often be called on some auxiliary thread depending on the platform and the
26 // device type; including, for example, the DirectShow thread on Windows, the
27 // v4l2_thread on Linux, and the UI thread for tab capture.
29 // It has an internal ref counted TextureWrapHelper class used to wrap incoming
30 // GpuMemoryBuffers into Texture backed VideoFrames. This class creates and
31 // manages the necessary entities to interact with the GPU process, notably an
32 // offscreen Context to avoid janking the UI thread.
33 class CONTENT_EXPORT VideoCaptureDeviceClient
34 : public media::VideoCaptureDevice::Client,
35 public base::SupportsWeakPtr<VideoCaptureDeviceClient> {
36 public:
37 VideoCaptureDeviceClient(
38 const base::WeakPtr<VideoCaptureController>& controller,
39 const scoped_refptr<VideoCaptureBufferPool>& buffer_pool,
40 const scoped_refptr<base::SingleThreadTaskRunner>& capture_task_runner);
41 ~VideoCaptureDeviceClient() override;
43 // VideoCaptureDevice::Client implementation.
44 void OnIncomingCapturedData(const uint8* data,
45 int length,
46 const media::VideoCaptureFormat& frame_format,
47 int rotation,
48 const base::TimeTicks& timestamp) override;
49 void OnIncomingCapturedYuvData(const uint8* y_data,
50 const uint8* u_data,
51 const uint8* v_data,
52 size_t y_stride,
53 size_t u_stride,
54 size_t v_stride,
55 const media::VideoCaptureFormat& frame_format,
56 int clockwise_rotation,
57 const base::TimeTicks& timestamp) override;
58 scoped_ptr<Buffer> ReserveOutputBuffer(
59 const gfx::Size& dimensions,
60 media::VideoPixelFormat format,
61 media::VideoPixelStorage storage) override;
62 void OnIncomingCapturedBuffer(scoped_ptr<Buffer> buffer,
63 const media::VideoCaptureFormat& frame_format,
64 const base::TimeTicks& timestamp) override;
65 void OnIncomingCapturedVideoFrame(
66 scoped_ptr<Buffer> buffer,
67 const scoped_refptr<media::VideoFrame>& frame,
68 const base::TimeTicks& timestamp) override;
69 void OnError(const std::string& reason) override;
70 void OnLog(const std::string& message) override;
71 double GetBufferPoolUtilization() const override;
73 private:
74 // Reserve output buffer into which I420 contents can be copied directly.
75 // The dimensions of the frame is described by |dimensions|, and requested
76 // output buffer format is specified by |storage|. The reserved output buffer
77 // is returned; and the pointer to Y plane is stored at [y_plane_data], U
78 // plane is stored at [u_plane_data], V plane is stored at [v_plane_data].
79 // Returns an nullptr if allocation fails.
81 // When requested |storage| is PIXEL_STORAGE_CPU, a single shared memory
82 // chunk is reserved; whereas for PIXEL_STORAGE_GPUMEMORYBUFFER, three
83 // GpuMemoryBuffers in R_8 format representing I420 planes are reserved. The
84 // output buffers stay reserved and mapped for use until the Buffer objects
85 // are destroyed or returned.
86 scoped_ptr<Buffer> ReserveI420OutputBuffer(const gfx::Size& dimensions,
87 media::VideoPixelStorage storage,
88 uint8** y_plane_data,
89 uint8** u_plane_data,
90 uint8** v_plane_data);
92 // The controller to which we post events.
93 const base::WeakPtr<VideoCaptureController> controller_;
95 // Hardware JPEG decoder.
96 scoped_ptr<VideoCaptureGpuJpegDecoder> external_jpeg_decoder_;
98 // Whether |external_jpeg_decoder_| has been initialized.
99 bool external_jpeg_decoder_initialized_;
101 // The pool of shared-memory buffers used for capturing.
102 const scoped_refptr<VideoCaptureBufferPool> buffer_pool_;
104 // Indication to the Client to copy-transform the incoming data into
105 // GpuMemoryBuffers.
106 const bool use_gpu_memory_buffers_;
108 // Internal delegate for GpuMemoryBuffer-into-VideoFrame wrapping.
109 class TextureWrapHelper;
110 scoped_refptr<TextureWrapHelper> texture_wrap_helper_;
111 // Reference to Capture Thread task runner, where |texture_wrap_helper_|
112 // lives.
113 const scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner_;
115 media::VideoPixelFormat last_captured_pixel_format_;
117 DISALLOW_COPY_AND_ASSIGN(VideoCaptureDeviceClient);
121 } // namespace content
123 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_DEVICE_CLIENT_H_