Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(101)

Side by Side Diff: media/capture/video/fake_video_capture_device.cc

Issue 2619503003: Split FakeVideoCaptureDevice into classes with single responsibility (Closed)
Patch Set: Removed FramePainter interface Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "media/capture/video/fake_video_capture_device.h" 5 #include "media/capture/video/fake_video_capture_device.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <algorithm> 8 #include <algorithm>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/atomicops.h"
mcasas 2017/02/15 19:06:52 Probably not needed anymore :-)
chfremer 2017/02/15 20:52:06 Done.
11 #include "base/bind.h" 12 #include "base/bind.h"
12 #include "base/location.h" 13 #include "base/location.h"
14 #include "base/macros.h"
15 #include "base/memory/weak_ptr.h"
13 #include "base/single_thread_task_runner.h" 16 #include "base/single_thread_task_runner.h"
14 #include "base/strings/stringprintf.h" 17 #include "base/strings/stringprintf.h"
18 #include "base/threading/thread_checker.h"
15 #include "base/threading/thread_task_runner_handle.h" 19 #include "base/threading/thread_task_runner_handle.h"
20 #include "base/time/time.h"
16 #include "media/audio/fake_audio_input_stream.h" 21 #include "media/audio/fake_audio_input_stream.h"
17 #include "media/base/video_frame.h" 22 #include "media/base/video_frame.h"
18 #include "third_party/skia/include/core/SkBitmap.h" 23 #include "third_party/skia/include/core/SkBitmap.h"
19 #include "third_party/skia/include/core/SkCanvas.h" 24 #include "third_party/skia/include/core/SkCanvas.h"
20 #include "third_party/skia/include/core/SkMatrix.h" 25 #include "third_party/skia/include/core/SkMatrix.h"
21 #include "third_party/skia/include/core/SkPaint.h" 26 #include "third_party/skia/include/core/SkPaint.h"
22 #include "ui/gfx/codec/png_codec.h" 27 #include "ui/gfx/codec/png_codec.h"
23 28
24 namespace media { 29 namespace media {
25 30
31 namespace {
26 // Sweep at 600 deg/sec. 32 // Sweep at 600 deg/sec.
27 static const float kPacmanAngularVelocity = 600; 33 static const float kPacmanAngularVelocity = 600;
28 // Beep every 500 ms. 34 // Beep every 500 ms.
29 static const int kBeepInterval = 500; 35 static const int kBeepInterval = 500;
30 // Gradient travels from bottom to top in 5 seconds. 36 // Gradient travels from bottom to top in 5 seconds.
31 static const float kGradientFrequency = 1.f / 5; 37 static const float kGradientFrequency = 1.f / 5;
32 38
33 static const double kMinZoom = 100.0; 39 static const double kMinZoom = 100.0;
34 static const double kMaxZoom = 400.0; 40 static const double kMaxZoom = 400.0;
35 static const double kZoomStep = 1.0; 41 static const double kZoomStep = 1.0;
42 static const double kInitialZoom = 100.0;
43
44 static const gfx::Size kSupportedSizesOrderedByIncreasingWidth[] = {
45 gfx::Size(96, 96), gfx::Size(320, 240), gfx::Size(640, 480),
46 gfx::Size(1280, 720), gfx::Size(1920, 1080)};
47 static const int kSupportedSizesCount =
48 arraysize(kSupportedSizesOrderedByIncreasingWidth);
49
50 static const VideoPixelFormat kSupportedPixelFormats[] = {
51 PIXEL_FORMAT_I420, PIXEL_FORMAT_Y16, PIXEL_FORMAT_ARGB};
52
53 static gfx::Size SnapToSupportedSize(const gfx::Size& requested_size) {
54 for (const gfx::Size& supported_size :
55 kSupportedSizesOrderedByIncreasingWidth) {
56 if (requested_size.width() <= supported_size.width())
57 return supported_size;
58 }
59 return kSupportedSizesOrderedByIncreasingWidth[kSupportedSizesCount - 1];
60 }
61
62 // Represents the current state of a FakeVideoCaptureDevice.
63 // This is a separate struct because read-access to it is shared with several
64 // collaborating classes.
65 struct FakeDeviceState {
66 FakeDeviceState(float zoom, float frame_rate, VideoPixelFormat pixel_format)
67 : zoom(zoom),
68 format(gfx::Size(), frame_rate, pixel_format, PIXEL_STORAGE_CPU) {}
69
70 uint32_t zoom;
71 VideoCaptureFormat format;
72 };
73
74 // Paints a "pacman-like" animated circle including textual information such
75 // as a frame count and timer.
76 class PacmanFramePainter {
77 public:
78 // Currently, only the following values are supported for |pixel_format|:
79 // PIXEL_FORMAT_I420
80 // PIXEL_FORMAT_Y16
81 // PIXEL_FORMAT_ARGB
mcasas 2017/02/15 19:06:52 nit: Documentation is hard to maintain. Instead, D
chfremer 2017/02/15 20:52:06 I am adding the DCHECKs in order to fail early. I'
82 PacmanFramePainter(VideoPixelFormat pixel_format,
83 const FakeDeviceState* fake_device_state);
84
85 void PaintFrame(base::TimeDelta elapsed_time, uint8_t* target_buffer);
86
87 private:
88 void DrawGradientSquares(base::TimeDelta elapsed_time,
89 uint8_t* target_buffer);
90
91 void DrawPacman(base::TimeDelta elapsed_time, uint8_t* target_buffer);
92
93 const VideoPixelFormat pixel_format_;
94 const FakeDeviceState* fake_device_state_ = nullptr;
95 };
96
97 // Delivers frames to a client, which is set via Initialize().
98 class FrameDeliveryStrategy {
mcasas 2017/02/15 19:06:52 I'm a bit confused as to this name, I'd say this i
chfremer 2017/02/15 20:52:05 Thanks, I tried your suggestion and moved |frame_p
mcasas 2017/02/15 20:56:52 I like it better! PS8 lgtm, thanks!
99 public:
100 virtual ~FrameDeliveryStrategy() {}
101 virtual void Initialize(VideoPixelFormat pixel_format,
102 std::unique_ptr<VideoCaptureDevice::Client> client,
103 const FakeDeviceState* device_state) = 0;
104 virtual void Uninitialize() = 0;
105 virtual uint8_t* PrepareBufferForNextFrame() = 0;
106 virtual void DeliverFrame() = 0;
107
108 protected:
109 const FakeDeviceState* device_state_ = nullptr;
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 its own buffers via OnIncomingCapturedData().
116 class OwnBufferFrameDeliveryStrategy : public FrameDeliveryStrategy {
117 public:
118 OwnBufferFrameDeliveryStrategy();
119 ~OwnBufferFrameDeliveryStrategy() override;
120
121 // Implementation of FrameDeliveryStrategy
122 void Initialize(VideoPixelFormat pixel_format,
123 std::unique_ptr<VideoCaptureDevice::Client> client,
124 const FakeDeviceState* device_state) override;
125 void Uninitialize() override;
126 uint8_t* PrepareBufferForNextFrame() override;
127 void DeliverFrame() override;
128
129 private:
130 std::unique_ptr<uint8_t[]> buffer_;
131 };
132
133 // Delivers frames using buffers provided by the client via
134 // OnIncomingCapturedBuffer().
135 class ClientBufferFrameDeliveryStrategy : public FrameDeliveryStrategy {
136 public:
137 ClientBufferFrameDeliveryStrategy();
138 ~ClientBufferFrameDeliveryStrategy() override;
139
140 // Implementation of FrameDeliveryStrategy
141 void Initialize(VideoPixelFormat pixel_format,
142 std::unique_ptr<VideoCaptureDevice::Client> client,
143 const FakeDeviceState* device_state) override;
144 void Uninitialize() override;
145 uint8_t* PrepareBufferForNextFrame() override;
146 void DeliverFrame() override;
147
148 private:
149 VideoCaptureDevice::Client::Buffer capture_buffer_;
150 };
151
152 // Implements the photo functionality of a VideoCaptureDevice
153 class FakePhotoDevice {
154 public:
155 FakePhotoDevice(std::unique_ptr<PacmanFramePainter> argb_painter,
156 const FakeDeviceState* fake_device_state);
157 ~FakePhotoDevice();
158
159 void GetPhotoCapabilities(
160 VideoCaptureDevice::GetPhotoCapabilitiesCallback callback);
161 void TakePhoto(VideoCaptureDevice::TakePhotoCallback callback,
162 base::TimeDelta elapsed_time);
163
164 private:
165 const std::unique_ptr<PacmanFramePainter> argb_painter_;
166 const FakeDeviceState* const fake_device_state_;
167 };
168
169 // Implementation of VideoCaptureDevice that generates test frames. This is
170 // useful for testing the video capture components without having to use real
171 // devices. The implementation schedules delayed tasks to itself to generate and
172 // deliver frames at the requested rate.
173 class FakeVideoCaptureDevice : public VideoCaptureDevice {
174 public:
175 FakeVideoCaptureDevice(
176 std::unique_ptr<PacmanFramePainter> frame_painter,
177 std::unique_ptr<FrameDeliveryStrategy> frame_delivery_strategy,
178 std::unique_ptr<FakePhotoDevice> photo_device,
179 std::unique_ptr<FakeDeviceState> device_state);
180 ~FakeVideoCaptureDevice() override;
181
182 // VideoCaptureDevice implementation.
183 void AllocateAndStart(const VideoCaptureParams& params,
184 std::unique_ptr<Client> client) override;
185 void StopAndDeAllocate() override;
186 void GetPhotoCapabilities(GetPhotoCapabilitiesCallback callback) override;
187 void SetPhotoOptions(mojom::PhotoSettingsPtr settings,
188 SetPhotoOptionsCallback callback) override;
189 void TakePhoto(TakePhotoCallback callback) override;
190
191 private:
192 void BeepAndScheduleNextCapture(base::TimeTicks expected_execution_time);
193 void OnNextFrameDue(base::TimeTicks expected_execution_time, int session_id);
194
195 const std::unique_ptr<PacmanFramePainter> frame_painter_;
196 const std::unique_ptr<FrameDeliveryStrategy> frame_delivery_strategy_;
197 const std::unique_ptr<FakePhotoDevice> photo_device_;
198 const std::unique_ptr<FakeDeviceState> device_state_;
199 int current_session_id_ = 0;
200
201 // Time when the next beep occurs.
202 base::TimeDelta beep_time_;
203 // Time since the fake video started rendering frames.
204 base::TimeDelta elapsed_time_;
205
206 base::ThreadChecker thread_checker_;
207
208 // FakeVideoCaptureDevice post tasks to itself for frame construction and
209 // needs to deal with asynchronous StopAndDeallocate().
210 base::WeakPtrFactory<FakeVideoCaptureDevice> weak_factory_;
211
212 DISALLOW_COPY_AND_ASSIGN(FakeVideoCaptureDevice);
213 };
214
215 } // anonymous namespace
216
217 // static
218 void FakeVideoCaptureDeviceMaker::GetSupportedSizes(
219 std::vector<gfx::Size>* supported_sizes) {
220 for (int i = 0; i < kSupportedSizesCount; i++)
221 supported_sizes->push_back(kSupportedSizesOrderedByIncreasingWidth[i]);
222 }
223
224 // static
225 std::unique_ptr<VideoCaptureDevice> FakeVideoCaptureDeviceMaker::MakeInstance(
226 VideoPixelFormat pixel_format,
227 DeliveryMode delivery_mode,
228 float frame_rate) {
229 bool pixel_format_supported = false;
230 for (const auto& supported_pixel_format : kSupportedPixelFormats) {
231 if (pixel_format == supported_pixel_format) {
232 pixel_format_supported = true;
233 break;
234 }
235 }
236 if (!pixel_format_supported) {
237 DLOG(ERROR) << "Requested an unsupported pixel format "
238 << VideoPixelFormatToString(pixel_format);
239 return nullptr;
240 }
241
242 auto device_state =
243 base::MakeUnique<FakeDeviceState>(kInitialZoom, frame_rate, pixel_format);
244 auto video_frame_painter =
245 base::MakeUnique<PacmanFramePainter>(pixel_format, device_state.get());
246 std::unique_ptr<FrameDeliveryStrategy> frame_delivery_strategy;
247 switch (delivery_mode) {
248 case DeliveryMode::USE_DEVICE_INTERNAL_BUFFERS:
249 frame_delivery_strategy =
250 base::MakeUnique<OwnBufferFrameDeliveryStrategy>();
251 break;
252 case DeliveryMode::USE_CLIENT_PROVIDED_BUFFERS:
253 frame_delivery_strategy =
254 base::MakeUnique<ClientBufferFrameDeliveryStrategy>();
255 break;
256 }
257
258 auto photo_frame_painter = base::MakeUnique<PacmanFramePainter>(
259 PIXEL_FORMAT_ARGB, device_state.get());
260 auto photo_device = base::MakeUnique<FakePhotoDevice>(
261 std::move(photo_frame_painter), device_state.get());
262
263 return base::MakeUnique<FakeVideoCaptureDevice>(
264 std::move(video_frame_painter), std::move(frame_delivery_strategy),
265 std::move(photo_device), std::move(device_state));
266 }
267
268 PacmanFramePainter::PacmanFramePainter(VideoPixelFormat pixel_format,
269 const FakeDeviceState* fake_device_state)
270 : pixel_format_(pixel_format), fake_device_state_(fake_device_state) {}
271
272 void PacmanFramePainter::PaintFrame(base::TimeDelta elapsed_time,
273 uint8_t* target_buffer) {
274 DrawPacman(elapsed_time, target_buffer);
275 DrawGradientSquares(elapsed_time, target_buffer);
276 }
36 277
37 // Starting from top left, -45 deg gradient. Value at point (row, column) is 278 // Starting from top left, -45 deg gradient. Value at point (row, column) is
38 // calculated as (top_left_value + (row + column) * step) % MAX_VALUE, where 279 // calculated as (top_left_value + (row + column) * step) % MAX_VALUE, where
39 // step is MAX_VALUE / (width + height). MAX_VALUE is 255 (for 8 bit per 280 // step is MAX_VALUE / (width + height). MAX_VALUE is 255 (for 8 bit per
40 // component) or 65535 for Y16. 281 // component) or 65535 for Y16.
41 // This is handy for pixel tests where we use the squares to verify rendering. 282 // This is handy for pixel tests where we use the squares to verify rendering.
42 void DrawGradientSquares(VideoPixelFormat frame_format, 283 void PacmanFramePainter::DrawGradientSquares(base::TimeDelta elapsed_time,
43 uint8_t* const pixels, 284 uint8_t* target_buffer) {
44 base::TimeDelta elapsed_time, 285 const int width = fake_device_state_->format.frame_size.width();
45 const gfx::Size& frame_size) { 286 const int height = fake_device_state_->format.frame_size.height();
46 const int width = frame_size.width(); 287
47 const int height = frame_size.height();
48 const int side = width / 16; // square side length. 288 const int side = width / 16; // square side length.
49 DCHECK(side); 289 DCHECK(side);
50 const gfx::Point squares[] = {{0, 0}, 290 const gfx::Point squares[] = {{0, 0},
51 {width - side, 0}, 291 {width - side, 0},
52 {0, height - side}, 292 {0, height - side},
53 {width - side, height - side}}; 293 {width - side, height - side}};
54 const float start = 294 const float start =
55 fmod(65536 * elapsed_time.InSecondsF() * kGradientFrequency, 65536); 295 fmod(65536 * elapsed_time.InSecondsF() * kGradientFrequency, 65536);
56 const float color_step = 65535 / static_cast<float>(width + height); 296 const float color_step = 65535 / static_cast<float>(width + height);
57 for (const auto& corner : squares) { 297 for (const auto& corner : squares) {
58 for (int y = corner.y(); y < corner.y() + side; ++y) { 298 for (int y = corner.y(); y < corner.y() + side; ++y) {
59 for (int x = corner.x(); x < corner.x() + side; ++x) { 299 for (int x = corner.x(); x < corner.x() + side; ++x) {
60 const unsigned int value = 300 const unsigned int value =
61 static_cast<unsigned int>(start + (x + y) * color_step) & 0xFFFF; 301 static_cast<unsigned int>(start + (x + y) * color_step) & 0xFFFF;
62 size_t offset = (y * width) + x; 302 size_t offset = (y * width) + x;
63 switch (frame_format) { 303 switch (pixel_format_) {
64 case PIXEL_FORMAT_Y16: 304 case PIXEL_FORMAT_Y16:
65 pixels[offset * sizeof(uint16_t)] = value & 0xFF; 305 target_buffer[offset * sizeof(uint16_t)] = value & 0xFF;
66 pixels[offset * sizeof(uint16_t) + 1] = value >> 8; 306 target_buffer[offset * sizeof(uint16_t) + 1] = value >> 8;
67 break; 307 break;
68 case PIXEL_FORMAT_ARGB: 308 case PIXEL_FORMAT_ARGB:
69 pixels[offset * sizeof(uint32_t) + 1] = value >> 8; 309 target_buffer[offset * sizeof(uint32_t) + 1] = value >> 8;
70 pixels[offset * sizeof(uint32_t) + 2] = value >> 8; 310 target_buffer[offset * sizeof(uint32_t) + 2] = value >> 8;
71 pixels[offset * sizeof(uint32_t) + 3] = value >> 8; 311 target_buffer[offset * sizeof(uint32_t) + 3] = value >> 8;
72 break; 312 break;
73 default: 313 default:
74 pixels[offset] = value >> 8; 314 target_buffer[offset] = value >> 8;
75 break; 315 break;
76 } 316 }
77 } 317 }
78 } 318 }
79 } 319 }
80 } 320 }
81 321
82 void DrawPacman(VideoPixelFormat frame_format, 322 void PacmanFramePainter::DrawPacman(base::TimeDelta elapsed_time,
83 uint8_t* const data, 323 uint8_t* target_buffer) {
84 base::TimeDelta elapsed_time, 324 const int width = fake_device_state_->format.frame_size.width();
85 float frame_rate, 325 const int height = fake_device_state_->format.frame_size.height();
86 const gfx::Size& frame_size, 326
87 double zoom) {
88 // |kN32_SkColorType| stands for the appropriate RGBA/BGRA format. 327 // |kN32_SkColorType| stands for the appropriate RGBA/BGRA format.
89 const SkColorType colorspace = (frame_format == PIXEL_FORMAT_ARGB) 328 const SkColorType colorspace = (pixel_format_ == PIXEL_FORMAT_ARGB)
90 ? kN32_SkColorType 329 ? kN32_SkColorType
91 : kAlpha_8_SkColorType; 330 : kAlpha_8_SkColorType;
92 // Skia doesn't support 16 bit alpha rendering, so we 8 bit alpha and then use 331 // Skia doesn't support 16 bit alpha rendering, so we 8 bit alpha and then use
93 // this as high byte values in 16 bit pixels. 332 // this as high byte values in 16 bit pixels.
94 const SkImageInfo info = SkImageInfo::Make( 333 const SkImageInfo info =
95 frame_size.width(), frame_size.height(), colorspace, kOpaque_SkAlphaType); 334 SkImageInfo::Make(width, height, colorspace, kOpaque_SkAlphaType);
96 SkBitmap bitmap; 335 SkBitmap bitmap;
97 bitmap.setInfo(info); 336 bitmap.setInfo(info);
98 bitmap.setPixels(data); 337 bitmap.setPixels(target_buffer);
99 SkPaint paint; 338 SkPaint paint;
100 paint.setStyle(SkPaint::kFill_Style); 339 paint.setStyle(SkPaint::kFill_Style);
101 SkCanvas canvas(bitmap); 340 SkCanvas canvas(bitmap);
102 341
103 const SkScalar unscaled_zoom = zoom / 100.f; 342 const SkScalar unscaled_zoom = fake_device_state_->zoom / 100.f;
104 SkMatrix matrix; 343 SkMatrix matrix;
105 matrix.setScale(unscaled_zoom, unscaled_zoom, frame_size.width() / 2, 344 matrix.setScale(unscaled_zoom, unscaled_zoom, width / 2, height / 2);
106 frame_size.height() / 2);
107 canvas.setMatrix(matrix); 345 canvas.setMatrix(matrix);
108 346
109 // Equalize Alpha_8 that has light green background while RGBA has white. 347 // Equalize Alpha_8 that has light green background while RGBA has white.
110 if (frame_format == PIXEL_FORMAT_ARGB) { 348 if (pixel_format_ == PIXEL_FORMAT_ARGB) {
111 const SkRect full_frame = 349 const SkRect full_frame = SkRect::MakeWH(width, height);
112 SkRect::MakeWH(frame_size.width(), frame_size.height());
113 paint.setARGB(255, 0, 127, 0); 350 paint.setARGB(255, 0, 127, 0);
114 canvas.drawRect(full_frame, paint); 351 canvas.drawRect(full_frame, paint);
115 } 352 }
116 paint.setColor(SK_ColorGREEN); 353 paint.setColor(SK_ColorGREEN);
117 354
118 // Draw a sweeping circle to show an animation. 355 // Draw a sweeping circle to show an animation.
119 const float end_angle = 356 const float end_angle =
120 fmod(kPacmanAngularVelocity * elapsed_time.InSecondsF(), 361); 357 fmod(kPacmanAngularVelocity * elapsed_time.InSecondsF(), 361);
121 const int radius = std::min(frame_size.width(), frame_size.height()) / 4; 358 const int radius = std::min(width, height) / 4;
122 const SkRect rect = SkRect::MakeXYWH(frame_size.width() / 2 - radius, 359 const SkRect rect = SkRect::MakeXYWH(width / 2 - radius, height / 2 - radius,
123 frame_size.height() / 2 - radius,
124 2 * radius, 2 * radius); 360 2 * radius, 2 * radius);
125 canvas.drawArc(rect, 0, end_angle, true, paint); 361 canvas.drawArc(rect, 0, end_angle, true, paint);
126 362
127 // Draw current time. 363 // Draw current time.
128 const int milliseconds = elapsed_time.InMilliseconds() % 1000; 364 const int milliseconds = elapsed_time.InMilliseconds() % 1000;
129 const int seconds = elapsed_time.InSeconds() % 60; 365 const int seconds = elapsed_time.InSeconds() % 60;
130 const int minutes = elapsed_time.InMinutes() % 60; 366 const int minutes = elapsed_time.InMinutes() % 60;
131 const int hours = elapsed_time.InHours(); 367 const int hours = elapsed_time.InHours();
132 const int frame_count = elapsed_time.InMilliseconds() * frame_rate / 1000; 368 const int frame_count = elapsed_time.InMilliseconds() *
369 fake_device_state_->format.frame_rate / 1000;
133 370
134 const std::string time_string = 371 const std::string time_string =
135 base::StringPrintf("%d:%02d:%02d:%03d %d", hours, minutes, seconds, 372 base::StringPrintf("%d:%02d:%02d:%03d %d", hours, minutes, seconds,
136 milliseconds, frame_count); 373 milliseconds, frame_count);
137 canvas.scale(3, 3); 374 canvas.scale(3, 3);
138 canvas.drawText(time_string.data(), time_string.length(), 30, 20, paint); 375 canvas.drawText(time_string.data(), time_string.length(), 30, 20, paint);
139 376
140 if (frame_format == PIXEL_FORMAT_Y16) { 377 if (pixel_format_ == PIXEL_FORMAT_Y16) {
141 // Use 8 bit bitmap rendered to first half of the buffer as high byte values 378 // Use 8 bit bitmap rendered to first half of the buffer as high byte values
142 // for the whole buffer. Low byte values are not important. 379 // for the whole buffer. Low byte values are not important.
143 for (int i = frame_size.GetArea() - 1; i >= 0; --i) 380 for (int i = (width * height) - 1; i >= 0; --i)
144 data[i * 2 + 1] = data[i]; 381 target_buffer[i * 2 + 1] = target_buffer[i];
145 } 382 }
146 DrawGradientSquares(frame_format, data, elapsed_time, frame_size);
147 } 383 }
148 384
149 // Creates a PNG-encoded frame and sends it back to |callback|. The other 385 FakePhotoDevice::FakePhotoDevice(
150 // parameters are used to replicate the PacMan rendering. 386 std::unique_ptr<PacmanFramePainter> argb_painter,
151 void DoTakeFakePhoto(VideoCaptureDevice::TakePhotoCallback callback, 387 const FakeDeviceState* fake_device_state)
152 const VideoCaptureFormat& capture_format, 388 : argb_painter_(std::move(argb_painter)),
153 base::TimeDelta elapsed_time, 389 fake_device_state_(fake_device_state) {}
154 float fake_capture_rate, 390
155 uint32_t zoom) { 391 FakePhotoDevice::~FakePhotoDevice() = default;
392
393 void FakePhotoDevice::TakePhoto(VideoCaptureDevice::TakePhotoCallback callback,
394 base::TimeDelta elapsed_time) {
395 // Create a PNG-encoded frame and send it back to |callback|.
156 std::unique_ptr<uint8_t[]> buffer(new uint8_t[VideoFrame::AllocationSize( 396 std::unique_ptr<uint8_t[]> buffer(new uint8_t[VideoFrame::AllocationSize(
157 PIXEL_FORMAT_ARGB, capture_format.frame_size)]); 397 PIXEL_FORMAT_ARGB, fake_device_state_->format.frame_size)]);
158 398 argb_painter_->PaintFrame(elapsed_time, buffer.get());
159 DrawPacman(PIXEL_FORMAT_ARGB, buffer.get(), elapsed_time, fake_capture_rate,
160 capture_format.frame_size, zoom);
161
162 mojom::BlobPtr blob = mojom::Blob::New(); 399 mojom::BlobPtr blob = mojom::Blob::New();
163 const bool result = gfx::PNGCodec::Encode( 400 const bool result =
164 buffer.get(), gfx::PNGCodec::FORMAT_RGBA, capture_format.frame_size, 401 gfx::PNGCodec::Encode(buffer.get(), gfx::PNGCodec::FORMAT_RGBA,
165 capture_format.frame_size.width() * 4, true /* discard_transparency */, 402 fake_device_state_->format.frame_size,
166 std::vector<gfx::PNGCodec::Comment>(), &blob->data); 403 fake_device_state_->format.frame_size.width() * 4,
404 true /* discard_transparency */,
405 std::vector<gfx::PNGCodec::Comment>(), &blob->data);
167 DCHECK(result); 406 DCHECK(result);
168 407
169 blob->mime_type = "image/png"; 408 blob->mime_type = "image/png";
170 callback.Run(std::move(blob)); 409 callback.Run(std::move(blob));
171 } 410 }
172 411
173 FakeVideoCaptureDevice::FakeVideoCaptureDevice(BufferOwnership buffer_ownership, 412 FakeVideoCaptureDevice::FakeVideoCaptureDevice(
174 float fake_capture_rate, 413 std::unique_ptr<PacmanFramePainter> frame_painter,
175 VideoPixelFormat pixel_format) 414 std::unique_ptr<FrameDeliveryStrategy> frame_delivery_strategy,
176 : buffer_ownership_(buffer_ownership), 415 std::unique_ptr<FakePhotoDevice> photo_device,
177 fake_capture_rate_(fake_capture_rate), 416 std::unique_ptr<FakeDeviceState> device_state)
178 pixel_format_(pixel_format), 417 : frame_painter_(std::move(frame_painter)),
179 current_zoom_(kMinZoom), 418 frame_delivery_strategy_(std::move(frame_delivery_strategy)),
419 photo_device_(std::move(photo_device)),
420 device_state_(std::move(device_state)),
180 weak_factory_(this) {} 421 weak_factory_(this) {}
181 422
182 FakeVideoCaptureDevice::~FakeVideoCaptureDevice() { 423 FakeVideoCaptureDevice::~FakeVideoCaptureDevice() {
183 DCHECK(thread_checker_.CalledOnValidThread()); 424 DCHECK(thread_checker_.CalledOnValidThread());
184 } 425 }
185 426
186 void FakeVideoCaptureDevice::AllocateAndStart( 427 void FakeVideoCaptureDevice::AllocateAndStart(
187 const VideoCaptureParams& params, 428 const VideoCaptureParams& params,
188 std::unique_ptr<VideoCaptureDevice::Client> client) { 429 std::unique_ptr<VideoCaptureDevice::Client> client) {
189 DCHECK(thread_checker_.CalledOnValidThread()); 430 DCHECK(thread_checker_.CalledOnValidThread());
190 431
191 client_ = std::move(client);
192
193 // Incoming |params| can be none of the supported formats, so we get the
194 // closest thing rounded up. TODO(mcasas): Use the |params|, if they belong to
195 // the supported ones, when http://crbug.com/309554 is verified.
196 capture_format_.frame_rate = fake_capture_rate_;
197 if (params.requested_format.frame_size.width() > 1280)
198 capture_format_.frame_size.SetSize(1920, 1080);
199 else if (params.requested_format.frame_size.width() > 640)
200 capture_format_.frame_size.SetSize(1280, 720);
201 else if (params.requested_format.frame_size.width() > 320)
202 capture_format_.frame_size.SetSize(640, 480);
203 else if (params.requested_format.frame_size.width() > 96)
204 capture_format_.frame_size.SetSize(320, 240);
205 else
206 capture_format_.frame_size.SetSize(96, 96);
207
208 capture_format_.pixel_format = pixel_format_;
209 if (buffer_ownership_ == BufferOwnership::CLIENT_BUFFERS) {
210 capture_format_.pixel_storage = PIXEL_STORAGE_CPU;
211 capture_format_.pixel_format = PIXEL_FORMAT_ARGB;
212 DVLOG(1) << "starting with client argb buffers";
213 } else if (buffer_ownership_ == BufferOwnership::OWN_BUFFERS) {
214 capture_format_.pixel_storage = PIXEL_STORAGE_CPU;
215 DVLOG(1) << "starting with own " << VideoPixelFormatToString(pixel_format_)
216 << " buffers";
217 }
218
219 if (buffer_ownership_ == BufferOwnership::OWN_BUFFERS) {
220 fake_frame_.reset(new uint8_t[VideoFrame::AllocationSize(
221 pixel_format_, capture_format_.frame_size)]);
222 }
223
224 beep_time_ = base::TimeDelta(); 432 beep_time_ = base::TimeDelta();
225 elapsed_time_ = base::TimeDelta(); 433 elapsed_time_ = base::TimeDelta();
226 434 device_state_->format.frame_size =
227 if (buffer_ownership_ == BufferOwnership::CLIENT_BUFFERS) 435 SnapToSupportedSize(params.requested_format.frame_size);
228 BeepAndScheduleNextCapture( 436 frame_delivery_strategy_->Initialize(device_state_->format.pixel_format,
229 base::TimeTicks::Now(), 437 std::move(client), device_state_.get());
230 base::Bind(&FakeVideoCaptureDevice::CaptureUsingClientBuffers, 438 current_session_id_++;
231 weak_factory_.GetWeakPtr())); 439 BeepAndScheduleNextCapture(base::TimeTicks::Now());
232 else if (buffer_ownership_ == BufferOwnership::OWN_BUFFERS)
233 BeepAndScheduleNextCapture(
234 base::TimeTicks::Now(),
235 base::Bind(&FakeVideoCaptureDevice::CaptureUsingOwnBuffers,
236 weak_factory_.GetWeakPtr()));
237 } 440 }
238 441
239 void FakeVideoCaptureDevice::StopAndDeAllocate() { 442 void FakeVideoCaptureDevice::StopAndDeAllocate() {
240 DCHECK(thread_checker_.CalledOnValidThread()); 443 DCHECK(thread_checker_.CalledOnValidThread());
241 client_.reset(); 444
445 // Invalidate WeakPtr to stop the perpetual scheduling of tasks.
446 weak_factory_.InvalidateWeakPtrs();
447 frame_delivery_strategy_->Uninitialize();
242 } 448 }
243 449
244 void FakeVideoCaptureDevice::GetPhotoCapabilities( 450 void FakeVideoCaptureDevice::GetPhotoCapabilities(
245 GetPhotoCapabilitiesCallback callback) { 451 GetPhotoCapabilitiesCallback callback) {
452 DCHECK(thread_checker_.CalledOnValidThread());
453 photo_device_->GetPhotoCapabilities(std::move(callback));
454 }
455
456 void FakePhotoDevice::GetPhotoCapabilities(
457 VideoCaptureDevice::GetPhotoCapabilitiesCallback callback) {
246 mojom::PhotoCapabilitiesPtr photo_capabilities = 458 mojom::PhotoCapabilitiesPtr photo_capabilities =
247 mojom::PhotoCapabilities::New(); 459 mojom::PhotoCapabilities::New();
248 photo_capabilities->iso = mojom::Range::New(); 460 photo_capabilities->iso = mojom::Range::New();
249 photo_capabilities->iso->current = 100.0; 461 photo_capabilities->iso->current = 100.0;
250 photo_capabilities->iso->max = 100.0; 462 photo_capabilities->iso->max = 100.0;
251 photo_capabilities->iso->min = 100.0; 463 photo_capabilities->iso->min = 100.0;
252 photo_capabilities->iso->step = 0.0; 464 photo_capabilities->iso->step = 0.0;
253 photo_capabilities->height = mojom::Range::New(); 465 photo_capabilities->height = mojom::Range::New();
254 photo_capabilities->height->current = capture_format_.frame_size.height(); 466 photo_capabilities->height->current =
467 fake_device_state_->format.frame_size.height();
255 photo_capabilities->height->max = 1080.0; 468 photo_capabilities->height->max = 1080.0;
256 photo_capabilities->height->min = 96.0; 469 photo_capabilities->height->min = 96.0;
257 photo_capabilities->height->step = 1.0; 470 photo_capabilities->height->step = 1.0;
258 photo_capabilities->width = mojom::Range::New(); 471 photo_capabilities->width = mojom::Range::New();
259 photo_capabilities->width->current = capture_format_.frame_size.width(); 472 photo_capabilities->width->current =
473 fake_device_state_->format.frame_size.width();
260 photo_capabilities->width->max = 1920.0; 474 photo_capabilities->width->max = 1920.0;
261 photo_capabilities->width->min = 96.0; 475 photo_capabilities->width->min = 96.0;
262 photo_capabilities->width->step = 1; 476 photo_capabilities->width->step = 1.0;
263 photo_capabilities->zoom = mojom::Range::New(); 477 photo_capabilities->zoom = mojom::Range::New();
264 photo_capabilities->zoom->current = current_zoom_; 478 photo_capabilities->zoom->current = fake_device_state_->zoom;
265 photo_capabilities->zoom->max = kMaxZoom; 479 photo_capabilities->zoom->max = kMaxZoom;
266 photo_capabilities->zoom->min = kMinZoom; 480 photo_capabilities->zoom->min = kMinZoom;
267 photo_capabilities->zoom->step = kZoomStep; 481 photo_capabilities->zoom->step = kZoomStep;
268 photo_capabilities->focus_mode = mojom::MeteringMode::NONE; 482 photo_capabilities->focus_mode = mojom::MeteringMode::NONE;
269 photo_capabilities->exposure_mode = mojom::MeteringMode::NONE; 483 photo_capabilities->exposure_mode = mojom::MeteringMode::NONE;
270 photo_capabilities->exposure_compensation = mojom::Range::New(); 484 photo_capabilities->exposure_compensation = mojom::Range::New();
271 photo_capabilities->white_balance_mode = mojom::MeteringMode::NONE; 485 photo_capabilities->white_balance_mode = mojom::MeteringMode::NONE;
272 photo_capabilities->fill_light_mode = mojom::FillLightMode::NONE; 486 photo_capabilities->fill_light_mode = mojom::FillLightMode::NONE;
273 photo_capabilities->red_eye_reduction = false; 487 photo_capabilities->red_eye_reduction = false;
274 photo_capabilities->color_temperature = mojom::Range::New(); 488 photo_capabilities->color_temperature = mojom::Range::New();
275 photo_capabilities->brightness = media::mojom::Range::New(); 489 photo_capabilities->brightness = media::mojom::Range::New();
276 photo_capabilities->contrast = media::mojom::Range::New(); 490 photo_capabilities->contrast = media::mojom::Range::New();
277 photo_capabilities->saturation = media::mojom::Range::New(); 491 photo_capabilities->saturation = media::mojom::Range::New();
278 photo_capabilities->sharpness = media::mojom::Range::New(); 492 photo_capabilities->sharpness = media::mojom::Range::New();
279 callback.Run(std::move(photo_capabilities)); 493 callback.Run(std::move(photo_capabilities));
280 } 494 }
281 495
282 void FakeVideoCaptureDevice::SetPhotoOptions(mojom::PhotoSettingsPtr settings, 496 void FakeVideoCaptureDevice::SetPhotoOptions(mojom::PhotoSettingsPtr settings,
283 SetPhotoOptionsCallback callback) { 497 SetPhotoOptionsCallback callback) {
284 if (settings->has_zoom) 498 DCHECK(thread_checker_.CalledOnValidThread());
285 current_zoom_ = std::max(kMinZoom, std::min(settings->zoom, kMaxZoom)); 499 if (settings->has_zoom) {
500 device_state_->zoom =
501 std::max(kMinZoom, std::min(settings->zoom, kMaxZoom));
502 }
503
286 callback.Run(true); 504 callback.Run(true);
287 } 505 }
288 506
289 void FakeVideoCaptureDevice::TakePhoto(TakePhotoCallback callback) { 507 void FakeVideoCaptureDevice::TakePhoto(TakePhotoCallback callback) {
508 DCHECK(thread_checker_.CalledOnValidThread());
290 base::ThreadTaskRunnerHandle::Get()->PostTask( 509 base::ThreadTaskRunnerHandle::Get()->PostTask(
291 FROM_HERE, 510 FROM_HERE, base::Bind(&FakePhotoDevice::TakePhoto,
292 base::Bind(&DoTakeFakePhoto, base::Passed(&callback), capture_format_, 511 base::Unretained(photo_device_.get()),
293 elapsed_time_, fake_capture_rate_, current_zoom_)); 512 base::Passed(&callback), elapsed_time_));
294 } 513 }
295 514
296 void FakeVideoCaptureDevice::CaptureUsingOwnBuffers( 515 OwnBufferFrameDeliveryStrategy::OwnBufferFrameDeliveryStrategy() = default;
297 base::TimeTicks expected_execution_time) {
298 DCHECK(thread_checker_.CalledOnValidThread());
299 const size_t frame_size = capture_format_.ImageAllocationSize();
300 516
301 memset(fake_frame_.get(), 0, frame_size); 517 OwnBufferFrameDeliveryStrategy::~OwnBufferFrameDeliveryStrategy() = default;
302 DrawPacman(capture_format_.pixel_format, fake_frame_.get(), elapsed_time_, 518
303 fake_capture_rate_, capture_format_.frame_size, current_zoom_); 519 void OwnBufferFrameDeliveryStrategy::Initialize(
304 // Give the captured frame to the client. 520 VideoPixelFormat pixel_format,
521 std::unique_ptr<VideoCaptureDevice::Client> client,
522 const FakeDeviceState* device_state) {
523 client_ = std::move(client);
524 device_state_ = device_state;
525 buffer_.reset(new uint8_t[VideoFrame::AllocationSize(
526 pixel_format, device_state_->format.frame_size)]);
527 }
528
529 void OwnBufferFrameDeliveryStrategy::Uninitialize() {
530 client_.reset();
531 device_state_ = nullptr;
532 buffer_.reset();
533 }
534
535 uint8_t* OwnBufferFrameDeliveryStrategy::PrepareBufferForNextFrame() {
536 if (!client_)
537 return nullptr;
538
539 const size_t frame_size = device_state_->format.ImageAllocationSize();
540 memset(buffer_.get(), 0, frame_size);
541 return buffer_.get();
542 }
543
544 void OwnBufferFrameDeliveryStrategy::DeliverFrame() {
545 if (!client_)
546 return;
547 const size_t frame_size = device_state_->format.ImageAllocationSize();
305 base::TimeTicks now = base::TimeTicks::Now(); 548 base::TimeTicks now = base::TimeTicks::Now();
306 if (first_ref_time_.is_null()) 549 if (first_ref_time_.is_null())
307 first_ref_time_ = now; 550 first_ref_time_ = now;
308 client_->OnIncomingCapturedData(fake_frame_.get(), frame_size, 551 client_->OnIncomingCapturedData(buffer_.get(), frame_size,
309 capture_format_, 0 /* rotation */, now, 552 device_state_->format, 0 /* rotation */, now,
310 now - first_ref_time_); 553 now - first_ref_time_);
311 BeepAndScheduleNextCapture(
312 expected_execution_time,
313 base::Bind(&FakeVideoCaptureDevice::CaptureUsingOwnBuffers,
314 weak_factory_.GetWeakPtr()));
315 } 554 }
316 555
317 void FakeVideoCaptureDevice::CaptureUsingClientBuffers( 556 ClientBufferFrameDeliveryStrategy::ClientBufferFrameDeliveryStrategy() =
318 base::TimeTicks expected_execution_time) { 557 default;
319 DCHECK(thread_checker_.CalledOnValidThread()); 558
559 ClientBufferFrameDeliveryStrategy::~ClientBufferFrameDeliveryStrategy() =
560 default;
561
562 void ClientBufferFrameDeliveryStrategy::Initialize(
563 VideoPixelFormat,
564 std::unique_ptr<VideoCaptureDevice::Client> client,
565 const FakeDeviceState* device_state) {
566 client_ = std::move(client);
567 device_state_ = device_state;
568 }
569
570 void ClientBufferFrameDeliveryStrategy::Uninitialize() {
571 client_.reset();
572 device_state_ = nullptr;
573 }
574
575 uint8_t* ClientBufferFrameDeliveryStrategy::PrepareBufferForNextFrame() {
576 if (client_ == nullptr)
577 return nullptr;
320 578
321 const int arbitrary_frame_feedback_id = 0; 579 const int arbitrary_frame_feedback_id = 0;
322 VideoCaptureDevice::Client::Buffer capture_buffer = 580 capture_buffer_ = client_->ReserveOutputBuffer(
323 client_->ReserveOutputBuffer( 581 device_state_->format.frame_size, device_state_->format.pixel_format,
324 capture_format_.frame_size, capture_format_.pixel_format, 582 device_state_->format.pixel_storage, arbitrary_frame_feedback_id);
325 capture_format_.pixel_storage, arbitrary_frame_feedback_id); 583 DLOG_IF(ERROR, !capture_buffer_.is_valid())
326 DLOG_IF(ERROR, !capture_buffer.is_valid())
327 << "Couldn't allocate Capture Buffer"; 584 << "Couldn't allocate Capture Buffer";
328 auto buffer_access = 585 auto buffer_access =
329 capture_buffer.handle_provider()->GetHandleForInProcessAccess(); 586 capture_buffer_.handle_provider()->GetHandleForInProcessAccess();
330 DCHECK(buffer_access->data()) << "Buffer has NO backing memory"; 587 DCHECK(buffer_access->data()) << "Buffer has NO backing memory";
331 588
332 DCHECK_EQ(PIXEL_STORAGE_CPU, capture_format_.pixel_storage); 589 DCHECK_EQ(device_state_->format.pixel_storage, PIXEL_STORAGE_CPU);
mcasas 2017/02/15 19:06:52 Expected goes first (as opposed to the JS test fra
chfremer 2017/02/15 20:52:06 Done.
590
333 uint8_t* data_ptr = buffer_access->data(); 591 uint8_t* data_ptr = buffer_access->data();
334 memset(data_ptr, 0, buffer_access->mapped_size()); 592 memset(data_ptr, 0, buffer_access->mapped_size());
335 DrawPacman(capture_format_.pixel_format, data_ptr, elapsed_time_, 593 return data_ptr;
336 fake_capture_rate_, capture_format_.frame_size, current_zoom_); 594 }
337 595
338 // Give the captured frame to the client. 596 void ClientBufferFrameDeliveryStrategy::DeliverFrame() {
597 if (client_ == nullptr)
598 return;
599
339 base::TimeTicks now = base::TimeTicks::Now(); 600 base::TimeTicks now = base::TimeTicks::Now();
340 if (first_ref_time_.is_null()) 601 if (first_ref_time_.is_null())
341 first_ref_time_ = now; 602 first_ref_time_ = now;
342 client_->OnIncomingCapturedBuffer(std::move(capture_buffer), capture_format_, 603 client_->OnIncomingCapturedBuffer(std::move(capture_buffer_),
343 now, now - first_ref_time_); 604 device_state_->format, now,
344 605 now - first_ref_time_);
345 BeepAndScheduleNextCapture(
346 expected_execution_time,
347 base::Bind(&FakeVideoCaptureDevice::CaptureUsingClientBuffers,
348 weak_factory_.GetWeakPtr()));
349 } 606 }
350 607
351 void FakeVideoCaptureDevice::BeepAndScheduleNextCapture( 608 void FakeVideoCaptureDevice::BeepAndScheduleNextCapture(
352 base::TimeTicks expected_execution_time, 609 base::TimeTicks expected_execution_time) {
353 const base::Callback<void(base::TimeTicks)>& next_capture) { 610 DCHECK(thread_checker_.CalledOnValidThread());
354 const base::TimeDelta beep_interval = 611 const base::TimeDelta beep_interval =
355 base::TimeDelta::FromMilliseconds(kBeepInterval); 612 base::TimeDelta::FromMilliseconds(kBeepInterval);
356 const base::TimeDelta frame_interval = 613 const base::TimeDelta frame_interval =
357 base::TimeDelta::FromMicroseconds(1e6 / fake_capture_rate_); 614 base::TimeDelta::FromMicroseconds(1e6 / device_state_->format.frame_rate);
358 beep_time_ += frame_interval; 615 beep_time_ += frame_interval;
359 elapsed_time_ += frame_interval; 616 elapsed_time_ += frame_interval;
360 617
361 // Generate a synchronized beep twice per second. 618 // Generate a synchronized beep twice per second.
362 if (beep_time_ >= beep_interval) { 619 if (beep_time_ >= beep_interval) {
363 FakeAudioInputStream::BeepOnce(); 620 FakeAudioInputStream::BeepOnce();
364 beep_time_ -= beep_interval; 621 beep_time_ -= beep_interval;
365 } 622 }
366 623
367 // Reschedule next CaptureTask. 624 // Reschedule next CaptureTask.
368 const base::TimeTicks current_time = base::TimeTicks::Now(); 625 const base::TimeTicks current_time = base::TimeTicks::Now();
369 // Don't accumulate any debt if we are lagging behind - just post the next 626 // Don't accumulate any debt if we are lagging behind - just post the next
370 // frame immediately and continue as normal. 627 // frame immediately and continue as normal.
371 const base::TimeTicks next_execution_time = 628 const base::TimeTicks next_execution_time =
372 std::max(current_time, expected_execution_time + frame_interval); 629 std::max(current_time, expected_execution_time + frame_interval);
373 const base::TimeDelta delay = next_execution_time - current_time; 630 const base::TimeDelta delay = next_execution_time - current_time;
374 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 631 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
375 FROM_HERE, base::Bind(next_capture, next_execution_time), delay); 632 FROM_HERE, base::Bind(&FakeVideoCaptureDevice::OnNextFrameDue,
633 weak_factory_.GetWeakPtr(), next_execution_time,
634 current_session_id_),
635 delay);
636 }
637
638 void FakeVideoCaptureDevice::OnNextFrameDue(
639 base::TimeTicks expected_execution_time,
640 int session_id) {
641 DCHECK(thread_checker_.CalledOnValidThread());
642 if (session_id != current_session_id_)
643 return;
644
645 uint8_t* const buffer = frame_delivery_strategy_->PrepareBufferForNextFrame();
646 frame_painter_->PaintFrame(elapsed_time_, buffer);
647 frame_delivery_strategy_->DeliverFrame();
648
649 BeepAndScheduleNextCapture(expected_execution_time);
376 } 650 }
377 651
378 } // namespace media 652 } // namespace media
OLDNEW
« no previous file with comments | « media/capture/video/fake_video_capture_device.h ('k') | media/capture/video/fake_video_capture_device_factory.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698