1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef MEDIA_CAPTURE_VIDEO_VIDEO_CAPTURE_DEVICE_CLIENT_H_
6 #define MEDIA_CAPTURE_VIDEO_VIDEO_CAPTURE_DEVICE_CLIENT_H_
14 #include "base/feature_list.h"
15 #include "base/memory/scoped_refptr.h"
16 #include "base/threading/thread_collision_warner.h"
17 #include "build/build_config.h"
18 #include "build/chromeos_buildflags.h"
19 #include "media/capture/capture_export.h"
20 #include "media/capture/video/video_capture_device.h"
21 #include "media/capture/video/video_frame_receiver.h"
22 #include "mojo/public/cpp/bindings/remote.h"
23 #include "services/video_capture/public/mojom/video_effects_manager.mojom.h"
26 class VideoCaptureBufferPool;
27 class VideoFrameReceiver;
28 class VideoCaptureJpegDecoder;
30 using VideoCaptureJpegDecoderFactoryCB =
31 base::OnceCallback<std::unique_ptr<VideoCaptureJpegDecoder>()>;
34 CAPTURE_EXPORT BASE_DECLARE_FEATURE(kFallbackToSharedMemoryIfNotNv12OnMac);
37 // Implementation of VideoCaptureDevice::Client that uses a buffer pool
38 // to provide buffers and converts incoming data to the I420 format for
39 // consumption by a given VideoFrameReceiver. If
40 // |optional_jpeg_decoder_factory_callback| is provided, the
41 // VideoCaptureDeviceClient will attempt to use it for decoding of MJPEG frames.
42 // Otherwise, it will use libyuv to perform MJPEG to I420 conversion in
45 // Methods of this class may be called from any thread, and in practice will
46 // often be called on some auxiliary thread depending on the platform and the
47 // device type; including, for example, the DirectShow thread on Windows, the
48 // v4l2_thread on Linux, and the UI thread for tab capture.
49 // The owner is responsible for making sure that the instance outlives these
51 class CAPTURE_EXPORT VideoCaptureDeviceClient
52 : public VideoCaptureDevice::Client {
54 #if BUILDFLAG(IS_CHROMEOS_ASH)
55 VideoCaptureDeviceClient(
56 VideoCaptureBufferType target_buffer_type,
57 std::unique_ptr<VideoFrameReceiver> receiver,
58 scoped_refptr<VideoCaptureBufferPool> buffer_pool,
59 VideoCaptureJpegDecoderFactoryCB jpeg_decoder_factory_callback);
61 VideoCaptureDeviceClient(
62 VideoCaptureBufferType target_buffer_type,
63 std::unique_ptr<VideoFrameReceiver> receiver,
64 scoped_refptr<VideoCaptureBufferPool> buffer_pool,
65 mojo::PendingRemote<video_capture::mojom::VideoEffectsManager>
66 video_effects_manager);
67 #endif // BUILDFLAG(IS_CHROMEOS_ASH)
69 VideoCaptureDeviceClient(const VideoCaptureDeviceClient&) = delete;
70 VideoCaptureDeviceClient& operator=(const VideoCaptureDeviceClient&) = delete;
72 ~VideoCaptureDeviceClient() override;
74 static Buffer MakeBufferStruct(
75 scoped_refptr<VideoCaptureBufferPool> buffer_pool,
77 int frame_feedback_id);
79 // VideoCaptureDevice::Client implementation.
80 void OnCaptureConfigurationChanged() override;
81 void OnIncomingCapturedData(const uint8_t* data,
83 const VideoCaptureFormat& frame_format,
84 const gfx::ColorSpace& color_space,
85 int clockwise_rotation,
87 base::TimeTicks reference_time,
88 base::TimeDelta timestamp,
89 int frame_feedback_id) override;
90 void OnIncomingCapturedGfxBuffer(gfx::GpuMemoryBuffer* buffer,
91 const VideoCaptureFormat& frame_format,
92 int clockwise_rotation,
93 base::TimeTicks reference_time,
94 base::TimeDelta timestamp,
95 int frame_feedback_id) override;
96 void OnIncomingCapturedExternalBuffer(
97 CapturedExternalVideoBuffer buffer,
98 base::TimeTicks reference_time,
99 base::TimeDelta timestamp,
100 const gfx::Rect& visible_rect) override;
101 ReserveResult ReserveOutputBuffer(const gfx::Size& dimensions,
102 VideoPixelFormat format,
103 int frame_feedback_id,
104 Buffer* buffer) override;
105 void OnIncomingCapturedBuffer(Buffer buffer,
106 const VideoCaptureFormat& format,
107 base::TimeTicks reference_time,
108 base::TimeDelta timestamp) override;
109 void OnIncomingCapturedBufferExt(
111 const VideoCaptureFormat& format,
112 const gfx::ColorSpace& color_space,
113 base::TimeTicks reference_time,
114 base::TimeDelta timestamp,
115 gfx::Rect visible_rect,
116 const VideoFrameMetadata& additional_metadata,
117 unsigned int encoded_data_size = 0) override;
118 void OnError(VideoCaptureError error,
119 const base::Location& from_here,
120 const std::string& reason) override;
121 void OnFrameDropped(VideoCaptureFrameDropReason reason) override;
122 void OnLog(const std::string& message) override;
123 void OnStarted() override;
124 double GetBufferPoolUtilization() const override;
127 VideoCaptureDevice::Client::ReserveResult CreateReadyFrameFromExternalBuffer(
128 CapturedExternalVideoBuffer buffer,
129 base::TimeTicks reference_time,
130 base::TimeDelta timestamp,
131 const gfx::Rect& visible_rect,
132 ReadyFrameInBuffer* ready_buffer);
134 // A branch of OnIncomingCapturedData for Y16 frame_format.pixel_format.
135 void OnIncomingCapturedY16Data(const uint8_t* data,
137 const VideoCaptureFormat& frame_format,
138 base::TimeTicks reference_time,
139 base::TimeDelta timestamp,
140 int frame_feedback_id);
142 const VideoCaptureBufferType target_buffer_type_;
144 // The receiver to which we post events.
145 const std::unique_ptr<VideoFrameReceiver> receiver_;
146 std::vector<int> buffer_ids_known_by_receiver_;
148 #if BUILDFLAG(IS_CHROMEOS_ASH)
149 VideoCaptureJpegDecoderFactoryCB optional_jpeg_decoder_factory_callback_;
150 std::unique_ptr<VideoCaptureJpegDecoder> external_jpeg_decoder_;
151 base::OnceClosure on_started_using_gpu_cb_;
152 #endif // BUILDFLAG(IS_CHROMEOS_ASH)
154 // The pool of shared-memory buffers used for capturing.
155 const scoped_refptr<VideoCaptureBufferPool> buffer_pool_;
157 VideoPixelFormat last_captured_pixel_format_;
159 #if !BUILDFLAG(IS_CHROMEOS_ASH)
160 scoped_refptr<base::SequencedTaskRunner> mojo_task_runner_;
161 mojo::Remote<video_capture::mojom::VideoEffectsManager> effects_manager_;
162 #endif // !BUILDFLAG(IS_CHROMEOS_ASH)
164 // Thread collision warner to ensure that producer-facing API is not called
165 // concurrently. Producers are allowed to call from multiple threads, but not
167 DFAKE_MUTEX(call_from_producer_);
172 #endif // MEDIA_CAPTURE_VIDEO_VIDEO_CAPTURE_DEVICE_CLIENT_H_