Removed 'anonymous' from namespace, added whitespace in thread_restrictions.cc
[chromium-blink-merge.git] / remoting / host / fake_desktop_capturer.cc
blob520d396ac2c7b9ad9479a9509aab8a4c8f6f82e2
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"
7 #include "base/bind.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"
13 namespace remoting {
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");
29 namespace {
31 class DefaultFrameGenerator
32 : public base::RefCountedThreadSafe<DefaultFrameGenerator> {
33 public:
34 DefaultFrameGenerator()
35 : bytes_per_row_(0),
36 box_pos_x_(0),
37 box_pos_y_(0),
38 box_speed_x_(kSpeed),
39 box_speed_y_(kSpeed),
40 first_frame_(true) {}
42 scoped_ptr<webrtc::DesktopFrame> GenerateFrame(
43 webrtc::DesktopCapturer::Callback* callback);
45 private:
46 friend class base::RefCountedThreadSafe<DefaultFrameGenerator>;
47 ~DefaultFrameGenerator() {}
49 webrtc::DesktopSize size_;
50 int bytes_per_row_;
51 int box_pos_x_;
52 int box_pos_y_;
53 int box_speed_x_;
54 int box_speed_y_;
55 bool first_frame_;
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;
67 if (shared_memory) {
68 frame.reset(new webrtc::SharedMemoryDesktopFrame(
69 webrtc::DesktopSize(kWidth, kHeight), bytes_per_row_, shared_memory));
70 } else {
71 frame.reset(
72 new webrtc::BasicDesktopFrame(webrtc::DesktopSize(kWidth, kHeight)));
75 // Move the box.
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:
89 // cyan....yellow
90 // ..............
91 // blue.......red
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();
107 if (first_frame_) {
108 frame->mutable_updated_region()->SetRect(
109 webrtc::DesktopRect::MakeXYWH(0, 0, kWidth, kHeight));
110 first_frame_ = false;
111 } else {
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));
118 return frame.Pass();
121 } // namespace
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) {
133 DCHECK(!callback_);
134 frame_generator_ = frame_generator;
137 void FakeDesktopCapturer::Start(Callback* callback) {
138 DCHECK(!callback_);
139 DCHECK(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_);
146 if (frame) {
147 frame->set_capture_time_ms(
148 (base::Time::Now() - capture_start_time).InMillisecondsRoundedUp());
150 callback_->OnCaptureCompleted(frame.release());
153 } // namespace remoting