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. 4 5 #include "remoting/host/fake_desktop_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 // 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; 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::DesktopCapturer::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::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))); 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 FakeDesktopCapturer::FakeDesktopCapturer() 124 : callback_(NULL) { 125 frame_generator_ = base::Bind(&DefaultFrameGenerator::GenerateFrame, 126 new DefaultFrameGenerator()); 127 } 128 129 FakeDesktopCapturer::~FakeDesktopCapturer() {} 130 131 void FakeDesktopCapturer::set_frame_generator( 132 const FrameGenerator& frame_generator) { 133 DCHECK(!callback_); 134 frame_generator_ = frame_generator; 135 } 136 137 void FakeDesktopCapturer::Start(Callback* callback) { 138 DCHECK(!callback_); 139 DCHECK(callback); 140 callback_ = callback; 141 } 142 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 frame->set_capture_time_ms( 147 (base::Time::Now() - capture_start_time).InMillisecondsRoundedUp()); 148 callback_->OnCaptureCompleted(frame.release()); 149 } 150 151 } // namespace remoting 152