Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Implementation of a fake VideoCaptureDevice class. Used for testing other | |
| 6 // video capture classes when no real hardware is available. | |
| 7 | |
| 8 #ifndef MEDIA_CAPTURE_VIDEO_FAKE_VIDEO_CAPTURE_DEVICE_H_ | 5 #ifndef MEDIA_CAPTURE_VIDEO_FAKE_VIDEO_CAPTURE_DEVICE_H_ |
| 9 #define MEDIA_CAPTURE_VIDEO_FAKE_VIDEO_CAPTURE_DEVICE_H_ | 6 #define MEDIA_CAPTURE_VIDEO_FAKE_VIDEO_CAPTURE_DEVICE_H_ |
| 10 | 7 |
| 11 #include <stdint.h> | 8 #include <stdint.h> |
| 12 | 9 |
| 13 #include <memory> | 10 #include <memory> |
| 14 #include <string> | 11 #include <string> |
| 15 | 12 |
| 16 #include "base/atomicops.h" | 13 #include "base/atomicops.h" |
| 17 #include "base/macros.h" | 14 #include "base/macros.h" |
| 18 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 19 #include "base/threading/thread_checker.h" | 16 #include "base/threading/thread_checker.h" |
| 20 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 21 #include "media/capture/video/video_capture_device.h" | 18 #include "media/capture/video/video_capture_device.h" |
| 22 | 19 |
| 23 namespace media { | 20 namespace media { |
| 24 | 21 |
| 22 class FakeVideoCaptureDevice; | |
| 23 | |
| 24 // Encapsulates factory logic to make a working FakeVideoCaptureDevice based | |
| 25 // on a given target OutputMode and frame rate. | |
| 26 class CAPTURE_EXPORT FakeVideoCaptureDeviceMaker { | |
|
mcasas
2017/01/12 03:19:16
s/FakeVideoCaptureDeviceMaker/FakeVideoCaptureDevi
chfremer
2017/01/18 01:29:52
Actually, I intentionally avoided calling it Build
mcasas
2017/02/15 00:44:19
Well, if it's a Factory-like thing it should be ca
chfremer
2017/02/15 01:30:11
I would say it is a sign of delegation happening,
| |
| 27 public: | |
| 28 enum class DeliveryMode { USE_OWN_BUFFERS, USE_CLIENT_BUFFERS }; | |
|
mcasas
2017/01/12 03:19:16
I see what you mean with DeliveryMode, but the ent
chfremer
2017/01/18 01:29:52
I chose DeliveryMode as a name to highlight the fa
mcasas
2017/02/15 00:44:19
Hmm makes sense, use the API naming and not the
in
chfremer
2017/02/15 01:30:11
Excellent suggestion. For maximum clarity I'd like
mcasas
2017/02/15 18:23:46
ok!!
| |
| 29 | |
| 30 static std::unique_ptr<VideoCaptureDevice> MakeInstance( | |
| 31 VideoPixelFormat pixel_format, | |
| 32 DeliveryMode delivery_mode, | |
| 33 float frame_rate); | |
| 34 }; | |
| 35 | |
| 36 // Represents the current state of a FakeVideoCaptureDevice. | |
| 37 // This is a separate struct because read-access to it is shared with several | |
| 38 // collaborating classes. | |
| 39 struct CAPTURE_EXPORT FakeDeviceState { | |
|
mcasas
2017/01/12 03:19:16
Classes only used inside a compilation/linking uni
chfremer
2017/01/18 01:29:52
Done.
| |
| 40 FakeDeviceState(float zoom, float frame_rate, VideoPixelFormat pixel_format) | |
| 41 : zoom(zoom), | |
| 42 format(gfx::Size(), frame_rate, pixel_format, PIXEL_STORAGE_CPU) {} | |
| 43 | |
| 44 uint32_t zoom; | |
| 45 VideoCaptureFormat format; | |
| 46 }; | |
| 47 | |
| 48 // Paints a frame into the given |target_buffer|. | |
| 49 class CAPTURE_EXPORT FramePainter { | |
| 50 public: | |
| 51 virtual void PaintFrame(base::TimeDelta elapsed_time, | |
| 52 uint8_t* target_buffer) = 0; | |
| 53 }; | |
| 54 | |
| 55 // Paints a "pacman-like" animated circle including textual information such | |
| 56 // as a frame count and timer. | |
| 57 class CAPTURE_EXPORT PacmanFramePainter : public FramePainter { | |
| 58 public: | |
| 59 // Currently, only the following values are supported for |pixel_format|: | |
| 60 // PIXEL_FORMAT_I420 | |
| 61 // PIXEL_FORMAT_Y16 | |
| 62 // PIXEL_FORMAT_ARGB | |
| 63 PacmanFramePainter(VideoPixelFormat pixel_format, | |
| 64 const FakeDeviceState* fake_device_state); | |
| 65 | |
| 66 // Implementation of FramePainter | |
| 67 void PaintFrame(base::TimeDelta elapsed_time, | |
| 68 uint8_t* target_buffer) override; | |
| 69 | |
| 70 private: | |
| 71 void DrawGradientSquares(base::TimeDelta elapsed_time, | |
| 72 uint8_t* target_buffer); | |
| 73 | |
| 74 void DrawPacman(base::TimeDelta elapsed_time, uint8_t* target_buffer); | |
| 75 | |
| 76 const VideoPixelFormat pixel_format_; | |
| 77 const FakeDeviceState* fake_device_state_ = nullptr; | |
| 78 }; | |
| 79 | |
| 80 // Delivers frames to a client, which is set via Initialize(). | |
| 81 class CAPTURE_EXPORT FrameDeliveryStrategy { | |
| 82 public: | |
| 83 virtual ~FrameDeliveryStrategy() {} | |
| 84 virtual void Initialize(VideoPixelFormat pixel_format, | |
| 85 std::unique_ptr<VideoCaptureDevice::Client> client, | |
| 86 const FakeDeviceState* device_state) = 0; | |
| 87 virtual void Uninitialize() = 0; | |
| 88 virtual uint8_t* PrepareBufferForNextFrame() = 0; | |
| 89 virtual void DeliverFrame() = 0; | |
| 90 }; | |
| 91 | |
| 92 // Delivers frames using its own buffers via OnIncomingCapturedData(). | |
| 93 class CAPTURE_EXPORT OwnBufferFrameDeliveryStrategy | |
| 94 : public FrameDeliveryStrategy { | |
| 95 public: | |
| 96 OwnBufferFrameDeliveryStrategy(); | |
| 97 ~OwnBufferFrameDeliveryStrategy() override; | |
| 98 | |
| 99 // Implementation of FrameDeliveryStrategy | |
| 100 void Initialize(VideoPixelFormat pixel_format, | |
| 101 std::unique_ptr<VideoCaptureDevice::Client> client, | |
| 102 const FakeDeviceState* device_state) override; | |
| 103 void Uninitialize() override; | |
| 104 uint8_t* PrepareBufferForNextFrame() override; | |
| 105 void DeliverFrame() override; | |
| 106 | |
| 107 private: | |
| 108 const FakeDeviceState* device_state_ = nullptr; | |
| 109 std::unique_ptr<uint8_t[]> buffer_; | |
| 110 std::unique_ptr<VideoCaptureDevice::Client> client_; | |
| 111 // The system time when we receive the first frame. | |
| 112 base::TimeTicks first_ref_time_; | |
| 113 }; | |
| 114 | |
| 115 // Delivers frames using buffers provided by the client via | |
| 116 // OnIncomingCapturedBuffer(). | |
| 117 class CAPTURE_EXPORT ClientBufferFrameDeliveryStrategy | |
| 118 : public FrameDeliveryStrategy { | |
| 119 public: | |
| 120 ClientBufferFrameDeliveryStrategy(); | |
| 121 ~ClientBufferFrameDeliveryStrategy() override; | |
| 122 | |
| 123 // Implementation of FrameDeliveryStrategy | |
| 124 void Initialize(VideoPixelFormat pixel_format, | |
| 125 std::unique_ptr<VideoCaptureDevice::Client> client, | |
| 126 const FakeDeviceState* device_state) override; | |
| 127 void Uninitialize() override; | |
| 128 uint8_t* PrepareBufferForNextFrame() override; | |
| 129 void DeliverFrame() override; | |
| 130 | |
| 131 private: | |
| 132 const FakeDeviceState* device_state_ = nullptr; | |
| 133 std::unique_ptr<VideoCaptureDevice::Client> client_; | |
| 134 std::unique_ptr<VideoCaptureDevice::Client::Buffer> capture_buffer_; | |
| 135 // The system time when we receive the first frame. | |
| 136 base::TimeTicks first_ref_time_; | |
|
mcasas
2017/01/12 03:19:16
|device_state_|, |client_| and |first_ref_time_| a
chfremer
2017/01/18 01:29:52
In general, I am trying to avoid inheritance of ba
| |
| 137 }; | |
| 138 | |
| 139 // Implements the photo functionality of a VideoCaptureDevice | |
| 140 class CAPTURE_EXPORT FakePhotoDevice { | |
|
mcasas
2017/01/12 03:19:16
All these classes would be better as inner classes
chfremer
2017/01/18 01:29:52
I don't see what the advantage of nesting classes
| |
| 141 public: | |
| 142 FakePhotoDevice(std::unique_ptr<FramePainter> argb_painter, | |
| 143 const FakeDeviceState* fake_device_state); | |
| 144 ~FakePhotoDevice(); | |
| 145 | |
| 146 void GetPhotoCapabilities( | |
| 147 VideoCaptureDevice::GetPhotoCapabilitiesCallback callback); | |
| 148 void TakePhoto(VideoCaptureDevice::TakePhotoCallback callback, | |
| 149 base::TimeDelta elapsed_time); | |
| 150 | |
| 151 private: | |
| 152 const std::unique_ptr<FramePainter> argb_painter_; | |
| 153 const FakeDeviceState* const fake_device_state_; | |
| 154 }; | |
| 155 | |
| 156 // Implementation of VideoCaptureDevice that generates test frames. This is | |
| 157 // useful for testing the video capture components without having to use real | |
| 158 // devices. The implementation schedules delayed tasks to itself to generate and | |
| 159 // deliver frames at the requested rate. | |
| 25 class CAPTURE_EXPORT FakeVideoCaptureDevice : public VideoCaptureDevice { | 160 class CAPTURE_EXPORT FakeVideoCaptureDevice : public VideoCaptureDevice { |
| 26 public: | 161 public: |
| 27 enum class BufferOwnership { | 162 FakeVideoCaptureDevice( |
| 28 OWN_BUFFERS, | 163 std::unique_ptr<FramePainter> frame_painter, |
| 29 CLIENT_BUFFERS, | 164 std::unique_ptr<FrameDeliveryStrategy> frame_delivery_strategy, |
| 30 }; | 165 std::unique_ptr<FakePhotoDevice> photo_device, |
| 166 std::unique_ptr<FakeDeviceState> device_state); | |
| 167 ~FakeVideoCaptureDevice() override; | |
| 31 | 168 |
| 32 FakeVideoCaptureDevice(BufferOwnership buffer_ownership, | 169 static void GetSupportedSizes(std::vector<gfx::Size>* supported_sizes); |
| 33 float fake_capture_rate, | |
| 34 VideoPixelFormat pixel_format = PIXEL_FORMAT_I420); | |
| 35 ~FakeVideoCaptureDevice() override; | |
| 36 | 170 |
| 37 // VideoCaptureDevice implementation. | 171 // VideoCaptureDevice implementation. |
| 38 void AllocateAndStart(const VideoCaptureParams& params, | 172 void AllocateAndStart(const VideoCaptureParams& params, |
| 39 std::unique_ptr<Client> client) override; | 173 std::unique_ptr<Client> client) override; |
| 40 void StopAndDeAllocate() override; | 174 void StopAndDeAllocate() override; |
| 41 void GetPhotoCapabilities(GetPhotoCapabilitiesCallback callback) override; | 175 void GetPhotoCapabilities(GetPhotoCapabilitiesCallback callback) override; |
| 42 void SetPhotoOptions(mojom::PhotoSettingsPtr settings, | 176 void SetPhotoOptions(mojom::PhotoSettingsPtr settings, |
| 43 SetPhotoOptionsCallback callback) override; | 177 SetPhotoOptionsCallback callback) override; |
| 44 void TakePhoto(TakePhotoCallback callback) override; | 178 void TakePhoto(TakePhotoCallback callback) override; |
| 45 | 179 |
| 46 private: | 180 private: |
| 47 void CaptureUsingOwnBuffers(base::TimeTicks expected_execution_time); | 181 static gfx::Size SnapToSupportedSize(const gfx::Size& requested_size); |
|
mcasas
2017/01/12 03:19:16
This doesn't need to be static in the class, can b
chfremer
2017/01/18 01:29:52
Done.
| |
| 48 void CaptureUsingClientBuffers(base::TimeTicks expected_execution_time); | 182 void BeepAndScheduleNextCapture(base::TimeTicks expected_execution_time); |
| 49 void BeepAndScheduleNextCapture( | 183 void OnNextFrameDue(base::TimeTicks expected_execution_time, int session_id); |
| 50 base::TimeTicks expected_execution_time, | |
| 51 const base::Callback<void(base::TimeTicks)>& next_capture); | |
| 52 | 184 |
| 53 // |thread_checker_| is used to check that all methods are called in the | 185 const std::unique_ptr<FramePainter> frame_painter_; |
| 54 // correct thread that owns the object. | 186 const std::unique_ptr<FrameDeliveryStrategy> frame_delivery_strategy_; |
| 55 base::ThreadChecker thread_checker_; | 187 const std::unique_ptr<FakePhotoDevice> photo_device_; |
| 188 const std::unique_ptr<FakeDeviceState> device_state_; | |
| 189 bool device_running_ = false; | |
| 190 int current_session_id_ = 0; | |
| 56 | 191 |
| 57 const BufferOwnership buffer_ownership_; | |
| 58 // Frame rate of the fake video device. | |
| 59 const float fake_capture_rate_; | |
| 60 // Pixel format of all device streams. | |
| 61 const VideoPixelFormat pixel_format_; | |
| 62 | |
| 63 std::unique_ptr<VideoCaptureDevice::Client> client_; | |
| 64 // |fake_frame_| is used for capturing on Own Buffers. | |
| 65 std::unique_ptr<uint8_t[]> fake_frame_; | |
| 66 // Time when the next beep occurs. | 192 // Time when the next beep occurs. |
| 67 base::TimeDelta beep_time_; | 193 base::TimeDelta beep_time_; |
| 68 // Time since the fake video started rendering frames. | 194 // Time since the fake video started rendering frames. |
| 69 base::TimeDelta elapsed_time_; | 195 base::TimeDelta elapsed_time_; |
| 70 VideoCaptureFormat capture_format_; | |
| 71 | 196 |
| 72 double current_zoom_; | 197 base::ThreadChecker thread_checker_; |
| 73 | 198 |
| 74 // The system time when we receive the first frame. | |
| 75 base::TimeTicks first_ref_time_; | |
| 76 // FakeVideoCaptureDevice post tasks to itself for frame construction and | 199 // FakeVideoCaptureDevice post tasks to itself for frame construction and |
| 77 // needs to deal with asynchronous StopAndDeallocate(). | 200 // needs to deal with asynchronous StopAndDeallocate(). |
| 78 base::WeakPtrFactory<FakeVideoCaptureDevice> weak_factory_; | 201 base::WeakPtrFactory<FakeVideoCaptureDevice> weak_factory_; |
| 79 | 202 |
| 80 DISALLOW_COPY_AND_ASSIGN(FakeVideoCaptureDevice); | 203 DISALLOW_COPY_AND_ASSIGN(FakeVideoCaptureDevice); |
| 81 }; | 204 }; |
| 82 | 205 |
| 83 } // namespace media | 206 } // namespace media |
| 84 | 207 |
| 85 #endif // MEDIA_CAPTURE_VIDEO_FAKE_VIDEO_CAPTURE_DEVICE_H_ | 208 #endif // MEDIA_CAPTURE_VIDEO_FAKE_VIDEO_CAPTURE_DEVICE_H_ |
| OLD | NEW |