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 MEDIA_CAPTURE_VIDEO_VIDEO_CAPTURE_DEVICE_CLIENT_H_
6 #define MEDIA_CAPTURE_VIDEO_VIDEO_CAPTURE_DEVICE_CLIENT_H_
14 #include "base/macros.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/threading/thread_collision_warner.h"
17 #include "media/capture/capture_export.h"
18 #include "media/capture/mojom/video_capture_types.mojom.h"
19 #include "media/capture/video/video_capture_device.h"
22 class VideoCaptureBufferPool;
23 class VideoFrameReceiver;
24 class VideoCaptureJpegDecoder;
26 using VideoCaptureJpegDecoderFactoryCB =
27 base::OnceCallback<std::unique_ptr<VideoCaptureJpegDecoder>()>;
29 // Implementation of VideoCaptureDevice::Client that uses a buffer pool
30 // to provide buffers and converts incoming data to the I420 format for
31 // consumption by a given VideoFrameReceiver. If
32 // |optional_jpeg_decoder_factory_callback| is provided, the
33 // VideoCaptureDeviceClient will attempt to use it for decoding of MJPEG frames.
34 // Otherwise, it will use libyuv to perform MJPEG to I420 conversion in
37 // Methods of this class may be called from any thread, and in practice will
38 // often be called on some auxiliary thread depending on the platform and the
39 // device type; including, for example, the DirectShow thread on Windows, the
40 // v4l2_thread on Linux, and the UI thread for tab capture.
41 // The owner is responsible for making sure that the instance outlives these
43 class CAPTURE_EXPORT VideoCaptureDeviceClient
44 : public VideoCaptureDevice::Client {
46 VideoCaptureDeviceClient(
47 VideoCaptureBufferType target_buffer_type,
48 std::unique_ptr<VideoFrameReceiver> receiver,
49 scoped_refptr<VideoCaptureBufferPool> buffer_pool,
50 VideoCaptureJpegDecoderFactoryCB optional_jpeg_decoder_factory_callback);
51 ~VideoCaptureDeviceClient() override;
53 static Buffer MakeBufferStruct(
54 scoped_refptr<VideoCaptureBufferPool> buffer_pool,
56 int frame_feedback_id);
58 // VideoCaptureDevice::Client implementation.
59 void OnIncomingCapturedData(const uint8_t* data,
61 const VideoCaptureFormat& frame_format,
62 int clockwise_rotation,
63 base::TimeTicks reference_time,
64 base::TimeDelta timestamp,
65 int frame_feedback_id = 0) override;
66 void OnIncomingCapturedGfxBuffer(gfx::GpuMemoryBuffer* buffer,
67 const VideoCaptureFormat& frame_format,
68 int clockwise_rotation,
69 base::TimeTicks reference_time,
70 base::TimeDelta timestamp,
71 int frame_feedback_id = 0) override;
72 Buffer ReserveOutputBuffer(const gfx::Size& dimensions,
73 VideoPixelFormat format,
74 int frame_feedback_id) override;
75 void OnIncomingCapturedBuffer(Buffer buffer,
76 const VideoCaptureFormat& format,
77 base::TimeTicks reference_time,
78 base::TimeDelta timestamp) override;
79 void OnIncomingCapturedBufferExt(
81 const VideoCaptureFormat& format,
82 base::TimeTicks reference_time,
83 base::TimeDelta timestamp,
84 gfx::Rect visible_rect,
85 const VideoFrameMetadata& additional_metadata) override;
86 Buffer ResurrectLastOutputBuffer(const gfx::Size& dimensions,
87 VideoPixelFormat format,
88 int new_frame_feedback_id) override;
89 void OnError(const base::Location& from_here,
90 const std::string& reason) override;
91 void OnLog(const std::string& message) override;
92 void OnStarted() override;
93 double GetBufferPoolUtilization() const override;
96 // A branch of OnIncomingCapturedData for Y16 frame_format.pixel_format.
97 void OnIncomingCapturedY16Data(const uint8_t* data,
99 const VideoCaptureFormat& frame_format,
100 base::TimeTicks reference_time,
101 base::TimeDelta timestamp,
102 int frame_feedback_id);
104 const VideoCaptureBufferType target_buffer_type_;
106 // The receiver to which we post events.
107 const std::unique_ptr<VideoFrameReceiver> receiver_;
108 std::vector<int> buffer_ids_known_by_receiver_;
110 VideoCaptureJpegDecoderFactoryCB optional_jpeg_decoder_factory_callback_;
111 std::unique_ptr<VideoCaptureJpegDecoder> external_jpeg_decoder_;
112 base::OnceClosure on_started_using_gpu_cb_;
114 // The pool of shared-memory buffers used for capturing.
115 const scoped_refptr<VideoCaptureBufferPool> buffer_pool_;
118 // Counter used to track the number of times consecutive capture buffers are
120 int dropped_frame_counter_ = 0;
122 static const int kMaxDroppedFrames = 150;
123 #endif // DCHECK_IS_ON()
125 VideoPixelFormat last_captured_pixel_format_;
127 // Thread collision warner to ensure that producer-facing API is not called
128 // concurrently. Producers are allowed to call from multiple threads, but not
130 DFAKE_MUTEX(call_from_producer_);
132 DISALLOW_COPY_AND_ASSIGN(VideoCaptureDeviceClient);
137 #endif // MEDIA_CAPTURE_VIDEO_VIDEO_CAPTURE_DEVICE_CLIENT_H_