1 // Copyright 2014 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 #include "remoting/host/fake_desktop_capturer.h"
8 #include "base/logging.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/time/time.h"
11 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
15 // FakeDesktopCapturer generates a white picture of size kWidth x kHeight
16 // with a rectangle of size kBoxWidth x kBoxHeight. The rectangle moves kSpeed
17 // pixels per frame along both axes, and bounces off the sides of the screen.
18 static const int kWidth
= FakeDesktopCapturer::kWidth
;
19 static const int kHeight
= FakeDesktopCapturer::kHeight
;
20 static const int kBoxWidth
= 140;
21 static const int kBoxHeight
= 140;
22 static const int kSpeed
= 20;
24 static_assert(kBoxWidth
< kWidth
&& kBoxHeight
< kHeight
, "bad box size");
25 static_assert((kBoxWidth
% kSpeed
== 0) && (kWidth
% kSpeed
== 0) &&
26 (kBoxHeight
% kSpeed
== 0) && (kHeight
% kSpeed
== 0),
27 "sizes must be multiple of kSpeed");
31 class DefaultFrameGenerator
32 : public base::RefCountedThreadSafe
<DefaultFrameGenerator
> {
34 DefaultFrameGenerator()
42 scoped_ptr
<webrtc::DesktopFrame
> GenerateFrame(
43 webrtc::DesktopCapturer::Callback
* callback
);
46 friend class base::RefCountedThreadSafe
<DefaultFrameGenerator
>;
47 ~DefaultFrameGenerator() {}
49 webrtc::DesktopSize size_
;
57 DISALLOW_COPY_AND_ASSIGN(DefaultFrameGenerator
);
60 scoped_ptr
<webrtc::DesktopFrame
> DefaultFrameGenerator::GenerateFrame(
61 webrtc::DesktopCapturer::Callback
* callback
) {
62 const int kBytesPerPixel
= webrtc::DesktopFrame::kBytesPerPixel
;
63 int buffer_size
= kWidth
* kHeight
* kBytesPerPixel
;
64 webrtc::SharedMemory
* shared_memory
=
65 callback
->CreateSharedMemory(buffer_size
);
66 scoped_ptr
<webrtc::DesktopFrame
> frame
;
68 frame
.reset(new webrtc::SharedMemoryDesktopFrame(
69 webrtc::DesktopSize(kWidth
, kHeight
), bytes_per_row_
, shared_memory
));
72 new webrtc::BasicDesktopFrame(webrtc::DesktopSize(kWidth
, kHeight
)));
76 bool old_box_pos_x
= box_pos_x_
;
77 box_pos_x_
+= box_speed_x_
;
78 if (box_pos_x_
+ kBoxWidth
>= kWidth
|| box_pos_x_
== 0)
79 box_speed_x_
= -box_speed_x_
;
81 bool old_box_pos_y
= box_pos_y_
;
82 box_pos_y_
+= box_speed_y_
;
83 if (box_pos_y_
+ kBoxHeight
>= kHeight
|| box_pos_y_
== 0)
84 box_speed_y_
= -box_speed_y_
;
86 memset(frame
->data(), 0xff, kHeight
* frame
->stride());
88 // Draw rectangle with the following colors in its corners:
92 uint8
* row
= frame
->data() +
93 (box_pos_y_
* size_
.width() + box_pos_x_
) * kBytesPerPixel
;
94 for (int y
= 0; y
< kBoxHeight
; ++y
) {
95 for (int x
= 0; x
< kBoxWidth
; ++x
) {
96 int r
= x
* 255 / kBoxWidth
;
97 int g
= y
* 255 / kBoxHeight
;
98 int b
= 255 - (x
* 255 / kBoxWidth
);
99 row
[x
* kBytesPerPixel
] = r
;
100 row
[x
* kBytesPerPixel
+ 1] = g
;
101 row
[x
* kBytesPerPixel
+ 2] = b
;
102 row
[x
* kBytesPerPixel
+ 3] = 0xff;
104 row
+= frame
->stride();
108 frame
->mutable_updated_region()->SetRect(
109 webrtc::DesktopRect::MakeXYWH(0, 0, kWidth
, kHeight
));
110 first_frame_
= false;
112 frame
->mutable_updated_region()->SetRect(webrtc::DesktopRect::MakeXYWH(
113 old_box_pos_x
, old_box_pos_y
, kBoxWidth
, kBoxHeight
));
114 frame
->mutable_updated_region()->AddRect(webrtc::DesktopRect::MakeXYWH(
115 box_pos_x_
, box_pos_y_
, kBoxWidth
, kBoxHeight
));
123 FakeDesktopCapturer::FakeDesktopCapturer()
124 : callback_(nullptr) {
125 frame_generator_
= base::Bind(&DefaultFrameGenerator::GenerateFrame
,
126 new DefaultFrameGenerator());
129 FakeDesktopCapturer::~FakeDesktopCapturer() {}
131 void FakeDesktopCapturer::set_frame_generator(
132 const FrameGenerator
& frame_generator
) {
134 frame_generator_
= frame_generator
;
137 void FakeDesktopCapturer::Start(Callback
* callback
) {
140 callback_
= callback
;
143 void FakeDesktopCapturer::Capture(const webrtc::DesktopRegion
& region
) {
144 base::Time capture_start_time
= base::Time::Now();
145 scoped_ptr
<webrtc::DesktopFrame
> frame
= frame_generator_
.Run(callback_
);
147 frame
->set_capture_time_ms(
148 (base::Time::Now() - capture_start_time
).InMillisecondsRoundedUp());
150 callback_
->OnCaptureCompleted(frame
.release());
153 } // namespace remoting