summaryrefslogtreecommitdiffstats
path: root/remoting/test/cyclic_frame_generator.cc
blob: 3ba38365ae6034ae7b52b2a61a8d1ca1c67058fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/test/cyclic_frame_generator.h"

#include "base/base_paths.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "base/time/default_tick_clock.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
#include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h"
#include "ui/gfx/codec/png_codec.h"

namespace remoting {
namespace test {

namespace {

const int kBarcodeCellWidth = 8;
const int kBarcodeCellHeight = 8;
const int kBarcodeBits = 10;
const int kBarcodeBlackThreshold = 85;
const int kBarcodeWhiteThreshold = 170;

scoped_ptr<webrtc::DesktopFrame> LoadDesktopFrameFromPng(
    const base::FilePath& file_path) {
  std::string file_content;
  if (!base::ReadFileToString(file_path, &file_content))
    LOG(FATAL) << "Failed to read " << file_path.MaybeAsASCII()
               << ". Please run remoting/test/data/download.sh";
  SkBitmap bitmap;
  gfx::PNGCodec::Decode(reinterpret_cast<const uint8_t*>(file_content.data()),
                        file_content.size(), &bitmap);
  scoped_ptr<webrtc::DesktopFrame> frame(new webrtc::BasicDesktopFrame(
      webrtc::DesktopSize(bitmap.width(), bitmap.height())));
  bitmap.copyPixelsTo(frame->data(),
                      frame->stride() * frame->size().height(),
                      frame->stride());
  return frame;
}

void DrawRect(webrtc::DesktopFrame* frame,
              webrtc::DesktopRect rect,
              uint32_t color) {
  for (int y = rect.top(); y < rect.bottom(); ++y) {
    uint32_t* data = reinterpret_cast<uint32_t*>(
        frame->data() + y * frame->stride() +
        rect.left() * webrtc::DesktopFrame::kBytesPerPixel);
    for (int x = 0; x < rect.width(); ++x) {
      data[x] = color;
    }
  }
}

}  // namespace
CyclicFrameGenerator::FrameInfo::FrameInfo() = default;

CyclicFrameGenerator::FrameInfo::FrameInfo(int frame_id,
                                           FrameType type,
                                           base::TimeTicks timestamp)
    : frame_id(frame_id), type(type), timestamp(timestamp) {}

// static
scoped_refptr<CyclicFrameGenerator> CyclicFrameGenerator::Create() {
  base::FilePath test_data_path;
  PathService::Get(base::DIR_SOURCE_ROOT, &test_data_path);
  test_data_path = test_data_path.Append(FILE_PATH_LITERAL("remoting"));
  test_data_path = test_data_path.Append(FILE_PATH_LITERAL("test"));
  test_data_path = test_data_path.Append(FILE_PATH_LITERAL("data"));

  std::vector<scoped_ptr<webrtc::DesktopFrame>> frames;
  frames.push_back(
      LoadDesktopFrameFromPng(test_data_path.AppendASCII("test_frame1.png")));
  frames.push_back(
      LoadDesktopFrameFromPng(test_data_path.AppendASCII("test_frame2.png")));
  return new CyclicFrameGenerator(std::move(frames));
}

CyclicFrameGenerator::CyclicFrameGenerator(
    std::vector<scoped_ptr<webrtc::DesktopFrame>> reference_frames)
    : reference_frames_(std::move(reference_frames)),
      clock_(&default_tick_clock_),
      started_time_(clock_->NowTicks()) {
  CHECK(!reference_frames_.empty());
  screen_size_ = reference_frames_[0]->size();
  for (const auto& frame : reference_frames_) {
    CHECK(screen_size_.equals(frame->size()))
        << "All reference frames should have the same size.";
  }
}

CyclicFrameGenerator::~CyclicFrameGenerator() {}

void CyclicFrameGenerator::SetTickClock(base::TickClock* tick_clock) {
  clock_ = tick_clock;
  started_time_ = clock_->NowTicks();
}

scoped_ptr<webrtc::DesktopFrame> CyclicFrameGenerator::GenerateFrame(
    webrtc::SharedMemoryFactory* shared_memory_factory) {
  base::TimeTicks now = clock_->NowTicks();
  int reference_frame =
      ((now - started_time_) / frame_cycle_period_) % reference_frames_.size();
  bool cursor_state = ((now - started_time_) / cursor_blink_period_) % 2;

  scoped_ptr<webrtc::DesktopFrame> frame(
      new webrtc::BasicDesktopFrame(screen_size_));
  frame->CopyPixelsFrom(*reference_frames_[reference_frame],
                        webrtc::DesktopVector(),
                        webrtc::DesktopRect::MakeSize(screen_size_));

  // Render the cursor.
  webrtc::DesktopRect cursor_rect =
      webrtc::DesktopRect::MakeXYWH(20, 20, 2, 20);
  if (cursor_state) {
    DrawRect(frame.get(), cursor_rect, 0);
  }

  if (last_reference_frame_ != reference_frame) {
    // The whole frame has changed.
    frame->mutable_updated_region()->AddRect(
        webrtc::DesktopRect::MakeSize(screen_size_));
    last_frame_type_ = FrameType::FULL;
  } else if (last_cursor_state_ != cursor_state) {
    // Cursor state has changed.
    frame->mutable_updated_region()->AddRect(cursor_rect);
    last_frame_type_ = FrameType::CURSOR;
  } else {
    // No changes.
    last_frame_type_ = FrameType::EMPTY;
  }

  // Increment frame_id for non-empty frames.
  int frame_id = (last_frame_type_ == FrameType::EMPTY) ? last_frame_id_
                                                        : last_frame_id_ + 1;

  // Render barcode.
  if (draw_barcode_) {
    uint32_t value = static_cast<uint32_t>(frame_id);
    CHECK(value < (1U << kBarcodeBits));
    for (int i = 0; i < kBarcodeBits; ++i) {
      DrawRect(frame.get(), webrtc::DesktopRect::MakeXYWH(i * kBarcodeCellWidth,
                                                          0, kBarcodeCellWidth,
                                                          kBarcodeCellHeight),
               (value & 1) ? 0xffffffff : 0xff000000);
      value >>= 1;
    }

    if (frame_id != last_frame_id_) {
      frame->mutable_updated_region()->AddRect(webrtc::DesktopRect::MakeXYWH(
          0, 0, kBarcodeCellWidth * kBarcodeBits, kBarcodeCellHeight));
    }
  }

  // Add non-empty frames to |generated_frames_info_|.
  if (last_frame_type_ != FrameType::EMPTY) {
    generated_frames_info_[frame_id] =
        FrameInfo(frame_id, last_frame_type_, clock_->NowTicks());
  }

  last_reference_frame_ = reference_frame;
  last_cursor_state_ = cursor_state;
  last_frame_id_ = frame_id;

  return frame;
}

CyclicFrameGenerator::FrameInfo CyclicFrameGenerator::IdentifyFrame(
    webrtc::DesktopFrame* frame) {
  CHECK(draw_barcode_);
  int frame_id = 0;
  for (int i = kBarcodeBits - 1; i >= 0; --i) {
    // Sample barcode in the center of the cell for each bit.
    int x = i * kBarcodeCellWidth + kBarcodeCellWidth / 2;
    int y = kBarcodeCellHeight / 2;
    uint8_t* data = (frame->data() + y * frame->stride() +
                     x * webrtc::DesktopFrame::kBytesPerPixel);
    int b = data[0];
    int g = data[1];
    int r = data[2];
    bool bit = 0;
    if (b > kBarcodeWhiteThreshold && g > kBarcodeWhiteThreshold &&
        r > kBarcodeWhiteThreshold) {
      bit = 1;
    } else if (b < kBarcodeBlackThreshold && g < kBarcodeBlackThreshold &&
        r < kBarcodeBlackThreshold) {
      bit = 0;
    } else {
      LOG(FATAL) << "Invalid barcode.";
    }
    frame_id <<= 1;
    if (bit)
      frame_id |= 1;
  }

  if (!generated_frames_info_.count(frame_id))
    LOG(FATAL) << "Barcode contains unknown frame_id: " << frame_id;

  return generated_frames_info_[frame_id];
}

}  // namespace test
}  // namespace remoting