fake_desktop_capturer.cc 5.35 KB
Newer Older
1
// Copyright 2015 The Chromium Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include "remoting/protocol/fake_desktop_capturer.h"
6

7 8
#include <stdint.h>

9
#include "base/bind.h"
10
#include "base/logging.h"
11
#include "base/macros.h"
12
#include "base/time/time.h"
13
#include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
14

15
namespace remoting {
16
namespace protocol {
17

18
// FakeDesktopCapturer generates a white picture of size kWidth x kHeight
19 20
// with a rectangle of size kBoxWidth x kBoxHeight. The rectangle moves kSpeed
// pixels per frame along both axes, and bounces off the sides of the screen.
21 22
static const int kWidth = FakeDesktopCapturer::kWidth;
static const int kHeight = FakeDesktopCapturer::kHeight;
23 24 25 26
static const int kBoxWidth = 140;
static const int kBoxHeight = 140;
static const int kSpeed = 20;

27 28 29 30
static_assert(kBoxWidth < kWidth && kBoxHeight < kHeight, "bad box size");
static_assert((kBoxWidth % kSpeed == 0) && (kWidth % kSpeed == 0) &&
              (kBoxHeight % kSpeed == 0) && (kHeight % kSpeed == 0),
              "sizes must be multiple of kSpeed");
31

32 33 34 35 36 37
namespace {

class DefaultFrameGenerator
    : public base::RefCountedThreadSafe<DefaultFrameGenerator> {
 public:
  DefaultFrameGenerator()
38
      : box_pos_x_(0),
39 40 41 42 43
        box_pos_y_(0),
        box_speed_x_(kSpeed),
        box_speed_y_(kSpeed),
        first_frame_(true) {}

44
  std::unique_ptr<webrtc::DesktopFrame> GenerateFrame(
45
      webrtc::SharedMemoryFactory* shared_memory_factory);
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

 private:
  friend class base::RefCountedThreadSafe<DefaultFrameGenerator>;
  ~DefaultFrameGenerator() {}

  webrtc::DesktopSize size_;
  int box_pos_x_;
  int box_pos_y_;
  int box_speed_x_;
  int box_speed_y_;
  bool first_frame_;

  DISALLOW_COPY_AND_ASSIGN(DefaultFrameGenerator);
};

61
std::unique_ptr<webrtc::DesktopFrame> DefaultFrameGenerator::GenerateFrame(
62
    webrtc::SharedMemoryFactory* shared_memory_factory) {
63
  const int kBytesPerPixel = webrtc::DesktopFrame::kBytesPerPixel;
64
  std::unique_ptr<webrtc::DesktopFrame> frame;
65 66
  if (shared_memory_factory) {
    int buffer_size = kWidth * kHeight * kBytesPerPixel;
67
    frame.reset(new webrtc::SharedMemoryDesktopFrame(
68 69
        webrtc::DesktopSize(kWidth, kHeight), kWidth * kBytesPerPixel,
        shared_memory_factory->CreateSharedMemory(buffer_size).release()));
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  } else {
    frame.reset(
        new webrtc::BasicDesktopFrame(webrtc::DesktopSize(kWidth, kHeight)));
  }

  // Move the box.
  bool old_box_pos_x = box_pos_x_;
  box_pos_x_ += box_speed_x_;
  if (box_pos_x_ + kBoxWidth >= kWidth || box_pos_x_ == 0)
    box_speed_x_ = -box_speed_x_;

  bool old_box_pos_y = box_pos_y_;
  box_pos_y_ += box_speed_y_;
  if (box_pos_y_ + kBoxHeight >= kHeight || box_pos_y_ == 0)
    box_speed_y_ = -box_speed_y_;

  memset(frame->data(), 0xff, kHeight * frame->stride());

  // Draw rectangle with the following colors in its corners:
  //     cyan....yellow
  //     ..............
  //     blue.......red
92 93
  uint8_t* row = frame->data() +
                 (box_pos_y_ * size_.width() + box_pos_x_) * kBytesPerPixel;
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  for (int y = 0; y < kBoxHeight; ++y) {
    for (int x = 0; x < kBoxWidth; ++x) {
      int r = x * 255 / kBoxWidth;
      int g = y * 255 / kBoxHeight;
      int b = 255 - (x * 255 / kBoxWidth);
      row[x * kBytesPerPixel] = r;
      row[x * kBytesPerPixel + 1] = g;
      row[x * kBytesPerPixel + 2] = b;
      row[x * kBytesPerPixel + 3] = 0xff;
    }
    row += frame->stride();
  }

  if (first_frame_) {
    frame->mutable_updated_region()->SetRect(
        webrtc::DesktopRect::MakeXYWH(0, 0, kWidth, kHeight));
    first_frame_ = false;
  } else {
    frame->mutable_updated_region()->SetRect(webrtc::DesktopRect::MakeXYWH(
        old_box_pos_x, old_box_pos_y, kBoxWidth, kBoxHeight));
    frame->mutable_updated_region()->AddRect(webrtc::DesktopRect::MakeXYWH(
        box_pos_x_, box_pos_y_, kBoxWidth, kBoxHeight));
  }

118
  return frame;
119 120 121 122
}

}  // namespace

123
FakeDesktopCapturer::FakeDesktopCapturer()
124
    : callback_(nullptr) {
125 126
  frame_generator_ = base::Bind(&DefaultFrameGenerator::GenerateFrame,
                                new DefaultFrameGenerator());
127 128
}

129
FakeDesktopCapturer::~FakeDesktopCapturer() {}
130

131
void FakeDesktopCapturer::set_frame_generator(
132 133 134
    const FrameGenerator& frame_generator) {
  DCHECK(!callback_);
  frame_generator_ = frame_generator;
135 136
}

137
void FakeDesktopCapturer::Start(Callback* callback) {
138 139 140 141 142
  DCHECK(!callback_);
  DCHECK(callback);
  callback_ = callback;
}

143
void FakeDesktopCapturer::SetSharedMemoryFactory(
144 145
    std::unique_ptr<webrtc::SharedMemoryFactory> shared_memory_factory) {
  shared_memory_factory_ = std::move(shared_memory_factory);
146 147
}

148
void FakeDesktopCapturer::CaptureFrame() {
149
  base::Time capture_start_time = base::Time::Now();
150
  std::unique_ptr<webrtc::DesktopFrame> frame =
151
      frame_generator_.Run(shared_memory_factory_.get());
152 153 154 155
  if (frame) {
    frame->set_capture_time_ms(
        (base::Time::Now() - capture_start_time).InMillisecondsRoundedUp());
  }
156 157 158 159
  callback_->OnCaptureResult(
      frame ? webrtc::DesktopCapturer::Result::SUCCESS
            : webrtc::DesktopCapturer::Result::ERROR_TEMPORARY,
      std::move(frame));
160
}
161

162 163 164 165 166 167 168 169 170 171
bool FakeDesktopCapturer::GetSourceList(SourceList* sources) {
  NOTIMPLEMENTED();
  return false;
}

bool FakeDesktopCapturer::SelectSource(SourceId id) {
  NOTIMPLEMENTED();
  return false;
}

172
}  // namespace protocol
173
}  // namespace remoting