| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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. | |
| 4 | |
| 5 #include "remoting/host/fake_screen_capturer.h" | |
| 6 | |
| 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" | |
| 12 | |
| 13 namespace remoting { | |
| 14 | |
| 15 // FakeScreenCapturer 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 = FakeScreenCapturer::kWidth; | |
| 19 static const int kHeight = FakeScreenCapturer::kHeight; | |
| 20 static const int kBoxWidth = 140; | |
| 21 static const int kBoxHeight = 140; | |
| 22 static const int kSpeed = 20; | |
| 23 | |
| 24 COMPILE_ASSERT(kBoxWidth < kWidth && kBoxHeight < kHeight, bad_box_size); | |
| 25 COMPILE_ASSERT((kBoxWidth % kSpeed == 0) && (kWidth % kSpeed == 0) && | |
| 26 (kBoxHeight % kSpeed == 0) && (kHeight % kSpeed == 0), | |
| 27 sizes_must_be_multiple_of_kSpeed); | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 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) {} | |
| 41 | |
| 42 scoped_ptr<webrtc::DesktopFrame> GenerateFrame( | |
| 43 webrtc::ScreenCapturer::Callback* callback); | |
| 44 | |
| 45 private: | |
| 46 friend class base::RefCountedThreadSafe<DefaultFrameGenerator>; | |
| 47 ~DefaultFrameGenerator() {} | |
| 48 | |
| 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_; | |
| 56 | |
| 57 DISALLOW_COPY_AND_ASSIGN(DefaultFrameGenerator); | |
| 58 }; | |
| 59 | |
| 60 scoped_ptr<webrtc::DesktopFrame> DefaultFrameGenerator::GenerateFrame( | |
| 61 webrtc::ScreenCapturer::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))); | |
| 73 } | |
| 74 | |
| 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_; | |
| 80 | |
| 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_; | |
| 85 | |
| 86 memset(frame->data(), 0xff, kHeight * frame->stride()); | |
| 87 | |
| 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; | |
| 103 } | |
| 104 row += frame->stride(); | |
| 105 } | |
| 106 | |
| 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)); | |
| 116 } | |
| 117 | |
| 118 return frame.Pass(); | |
| 119 } | |
| 120 | |
| 121 } // namespace | |
| 122 | |
| 123 FakeScreenCapturer::FakeScreenCapturer() | |
| 124 : callback_(NULL), | |
| 125 mouse_shape_observer_(NULL) { | |
| 126 frame_generator_ = base::Bind(&DefaultFrameGenerator::GenerateFrame, | |
| 127 new DefaultFrameGenerator()); | |
| 128 } | |
| 129 | |
| 130 FakeScreenCapturer::~FakeScreenCapturer() {} | |
| 131 | |
| 132 void FakeScreenCapturer::set_frame_generator( | |
| 133 const FrameGenerator& frame_generator) { | |
| 134 DCHECK(!callback_); | |
| 135 frame_generator_ = frame_generator; | |
| 136 } | |
| 137 | |
| 138 void FakeScreenCapturer::Start(Callback* callback) { | |
| 139 DCHECK(!callback_); | |
| 140 DCHECK(callback); | |
| 141 callback_ = callback; | |
| 142 } | |
| 143 | |
| 144 void FakeScreenCapturer::Capture(const webrtc::DesktopRegion& region) { | |
| 145 base::Time capture_start_time = base::Time::Now(); | |
| 146 scoped_ptr<webrtc::DesktopFrame> frame = frame_generator_.Run(callback_); | |
| 147 frame->set_capture_time_ms( | |
| 148 (base::Time::Now() - capture_start_time).InMillisecondsRoundedUp()); | |
| 149 callback_->OnCaptureCompleted(frame.release()); | |
| 150 } | |
| 151 | |
| 152 void FakeScreenCapturer::SetMouseShapeObserver( | |
| 153 MouseShapeObserver* mouse_shape_observer) { | |
| 154 DCHECK(!mouse_shape_observer_); | |
| 155 DCHECK(mouse_shape_observer); | |
| 156 mouse_shape_observer_ = mouse_shape_observer; | |
| 157 } | |
| 158 | |
| 159 bool FakeScreenCapturer::GetScreenList(ScreenList* screens) { | |
| 160 NOTIMPLEMENTED(); | |
| 161 return false; | |
| 162 } | |
| 163 | |
| 164 bool FakeScreenCapturer::SelectScreen(webrtc::ScreenId id) { | |
| 165 NOTIMPLEMENTED(); | |
| 166 return false; | |
| 167 } | |
| 168 | |
| 169 } // namespace remoting | |
| OLD | NEW |