OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/test/cyclic_frame_generator.h" | 5 #include "remoting/test/cyclic_frame_generator.h" |
6 | 6 |
| 7 #include "base/base_paths.h" |
| 8 #include "base/files/file_util.h" |
| 9 #include "base/path_service.h" |
7 #include "base/time/default_tick_clock.h" | 10 #include "base/time/default_tick_clock.h" |
8 #include "remoting/test/frame_generator_util.h" | 11 #include "third_party/skia/include/core/SkBitmap.h" |
9 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" | 12 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
10 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h" | 13 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h" |
| 14 #include "ui/gfx/codec/png_codec.h" |
11 | 15 |
12 namespace remoting { | 16 namespace remoting { |
13 namespace test { | 17 namespace test { |
14 | 18 |
| 19 namespace { |
| 20 |
| 21 const int kBarcodeCellWidth = 8; |
| 22 const int kBarcodeCellHeight = 8; |
| 23 const int kBarcodeBits = 10; |
| 24 const int kBarcodeBlackThreshold = 85; |
| 25 const int kBarcodeWhiteThreshold = 170; |
| 26 |
| 27 std::unique_ptr<webrtc::DesktopFrame> LoadDesktopFrameFromPng( |
| 28 const base::FilePath& file_path) { |
| 29 std::string file_content; |
| 30 if (!base::ReadFileToString(file_path, &file_content)) |
| 31 LOG(FATAL) << "Failed to read " << file_path.MaybeAsASCII() |
| 32 << ". Please run remoting/test/data/download.sh"; |
| 33 SkBitmap bitmap; |
| 34 gfx::PNGCodec::Decode(reinterpret_cast<const uint8_t*>(file_content.data()), |
| 35 file_content.size(), &bitmap); |
| 36 std::unique_ptr<webrtc::DesktopFrame> frame(new webrtc::BasicDesktopFrame( |
| 37 webrtc::DesktopSize(bitmap.width(), bitmap.height()))); |
| 38 bitmap.copyPixelsTo(frame->data(), |
| 39 frame->stride() * frame->size().height(), |
| 40 frame->stride()); |
| 41 return frame; |
| 42 } |
| 43 |
| 44 void DrawRect(webrtc::DesktopFrame* frame, |
| 45 webrtc::DesktopRect rect, |
| 46 uint32_t color) { |
| 47 for (int y = rect.top(); y < rect.bottom(); ++y) { |
| 48 uint32_t* data = reinterpret_cast<uint32_t*>( |
| 49 frame->data() + y * frame->stride() + |
| 50 rect.left() * webrtc::DesktopFrame::kBytesPerPixel); |
| 51 for (int x = 0; x < rect.width(); ++x) { |
| 52 data[x] = color; |
| 53 } |
| 54 } |
| 55 } |
| 56 |
| 57 } // namespace |
| 58 |
15 CyclicFrameGenerator::ChangeInfo::ChangeInfo() = default; | 59 CyclicFrameGenerator::ChangeInfo::ChangeInfo() = default; |
16 CyclicFrameGenerator::ChangeInfo::ChangeInfo(ChangeType type, | 60 CyclicFrameGenerator::ChangeInfo::ChangeInfo(ChangeType type, |
17 base::TimeTicks timestamp) | 61 base::TimeTicks timestamp) |
18 : type(type), timestamp(timestamp) {} | 62 : type(type), timestamp(timestamp) {} |
19 | 63 |
20 // static | 64 // static |
21 scoped_refptr<CyclicFrameGenerator> CyclicFrameGenerator::Create() { | 65 scoped_refptr<CyclicFrameGenerator> CyclicFrameGenerator::Create() { |
| 66 base::FilePath test_data_path; |
| 67 PathService::Get(base::DIR_SOURCE_ROOT, &test_data_path); |
| 68 test_data_path = test_data_path.Append(FILE_PATH_LITERAL("remoting")); |
| 69 test_data_path = test_data_path.Append(FILE_PATH_LITERAL("test")); |
| 70 test_data_path = test_data_path.Append(FILE_PATH_LITERAL("data")); |
| 71 |
22 std::vector<std::unique_ptr<webrtc::DesktopFrame>> frames; | 72 std::vector<std::unique_ptr<webrtc::DesktopFrame>> frames; |
23 frames.push_back(LoadDesktopFrameFromPng("test_frame1.png")); | 73 frames.push_back( |
24 frames.push_back(LoadDesktopFrameFromPng("test_frame2.png")); | 74 LoadDesktopFrameFromPng(test_data_path.AppendASCII("test_frame1.png"))); |
| 75 frames.push_back( |
| 76 LoadDesktopFrameFromPng(test_data_path.AppendASCII("test_frame2.png"))); |
25 return new CyclicFrameGenerator(std::move(frames)); | 77 return new CyclicFrameGenerator(std::move(frames)); |
26 } | 78 } |
27 | 79 |
28 CyclicFrameGenerator::CyclicFrameGenerator( | 80 CyclicFrameGenerator::CyclicFrameGenerator( |
29 std::vector<std::unique_ptr<webrtc::DesktopFrame>> reference_frames) | 81 std::vector<std::unique_ptr<webrtc::DesktopFrame>> reference_frames) |
30 : reference_frames_(std::move(reference_frames)), | 82 : reference_frames_(std::move(reference_frames)), |
31 clock_(&default_tick_clock_), | 83 clock_(&default_tick_clock_), |
32 started_time_(clock_->NowTicks()) { | 84 started_time_(clock_->NowTicks()) { |
33 CHECK(!reference_frames_.empty()); | 85 CHECK(!reference_frames_.empty()); |
34 screen_size_ = reference_frames_[0]->size(); | 86 screen_size_ = reference_frames_[0]->size(); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 last_frame_type_ = ChangeType::FULL; | 125 last_frame_type_ = ChangeType::FULL; |
74 } else if (last_cursor_state_ != cursor_state) { | 126 } else if (last_cursor_state_ != cursor_state) { |
75 // Cursor state has changed. | 127 // Cursor state has changed. |
76 frame->mutable_updated_region()->AddRect(cursor_rect); | 128 frame->mutable_updated_region()->AddRect(cursor_rect); |
77 last_frame_type_ = ChangeType::CURSOR; | 129 last_frame_type_ = ChangeType::CURSOR; |
78 } else { | 130 } else { |
79 // No changes. | 131 // No changes. |
80 last_frame_type_ = ChangeType::NO_CHANGES; | 132 last_frame_type_ = ChangeType::NO_CHANGES; |
81 } | 133 } |
82 | 134 |
83 if (draw_barcode_) | 135 // Render barcode. |
84 DrawBarcode(frame_id, frame_id != last_frame_id_, frame.get()); | 136 if (draw_barcode_) { |
| 137 uint32_t value = static_cast<uint32_t>(frame_id); |
| 138 CHECK(value < (1U << kBarcodeBits)); |
| 139 for (int i = 0; i < kBarcodeBits; ++i) { |
| 140 DrawRect(frame.get(), webrtc::DesktopRect::MakeXYWH(i * kBarcodeCellWidth, |
| 141 0, kBarcodeCellWidth, |
| 142 kBarcodeCellHeight), |
| 143 (value & 1) ? 0xffffffff : 0xff000000); |
| 144 value >>= 1; |
| 145 } |
| 146 |
| 147 if (frame_id != last_frame_id_) { |
| 148 frame->mutable_updated_region()->AddRect(webrtc::DesktopRect::MakeXYWH( |
| 149 0, 0, kBarcodeCellWidth * kBarcodeBits, kBarcodeCellHeight)); |
| 150 } |
| 151 } |
85 | 152 |
86 last_reference_frame_ = reference_frame; | 153 last_reference_frame_ = reference_frame; |
87 last_cursor_state_ = cursor_state; | 154 last_cursor_state_ = cursor_state; |
88 last_frame_id_ = frame_id; | 155 last_frame_id_ = frame_id; |
89 | 156 |
90 return frame; | 157 return frame; |
91 } | 158 } |
92 | 159 |
93 CyclicFrameGenerator::ChangeInfoList CyclicFrameGenerator::GetChangeList( | 160 CyclicFrameGenerator::ChangeInfoList CyclicFrameGenerator::GetChangeList( |
94 webrtc::DesktopFrame* frame) { | 161 webrtc::DesktopFrame* frame) { |
95 CHECK(draw_barcode_); | 162 CHECK(draw_barcode_); |
96 int frame_id = ReadBarcode(*frame); | 163 int frame_id = 0; |
| 164 for (int i = kBarcodeBits - 1; i >= 0; --i) { |
| 165 // Sample barcode in the center of the cell for each bit. |
| 166 int x = i * kBarcodeCellWidth + kBarcodeCellWidth / 2; |
| 167 int y = kBarcodeCellHeight / 2; |
| 168 uint8_t* data = (frame->data() + y * frame->stride() + |
| 169 x * webrtc::DesktopFrame::kBytesPerPixel); |
| 170 int b = data[0]; |
| 171 int g = data[1]; |
| 172 int r = data[2]; |
| 173 bool bit = 0; |
| 174 if (b > kBarcodeWhiteThreshold && g > kBarcodeWhiteThreshold && |
| 175 r > kBarcodeWhiteThreshold) { |
| 176 bit = 1; |
| 177 } else if (b < kBarcodeBlackThreshold && g < kBarcodeBlackThreshold && |
| 178 r < kBarcodeBlackThreshold) { |
| 179 bit = 0; |
| 180 } else { |
| 181 LOG(FATAL) << "Invalid barcode."; |
| 182 } |
| 183 frame_id <<= 1; |
| 184 if (bit) |
| 185 frame_id |= 1; |
| 186 } |
| 187 |
97 CHECK_GE(frame_id, last_identifier_frame_); | 188 CHECK_GE(frame_id, last_identifier_frame_); |
98 | 189 |
99 ChangeInfoList result; | 190 ChangeInfoList result; |
100 for (int i = last_identifier_frame_ + 1; i <= frame_id; ++i) { | 191 for (int i = last_identifier_frame_ + 1; i <= frame_id; ++i) { |
101 ChangeType type = (i % (frame_cycle_period_ / cursor_blink_period_) == 0) | 192 ChangeType type = (i % (frame_cycle_period_ / cursor_blink_period_) == 0) |
102 ? ChangeType::FULL | 193 ? ChangeType::FULL |
103 : ChangeType::CURSOR; | 194 : ChangeType::CURSOR; |
104 base::TimeTicks timestamp = | 195 base::TimeTicks timestamp = |
105 started_time_ + i * base::TimeDelta(cursor_blink_period_); | 196 started_time_ + i * base::TimeDelta(cursor_blink_period_); |
106 result.push_back(ChangeInfo(type, timestamp)); | 197 result.push_back(ChangeInfo(type, timestamp)); |
107 } | 198 } |
108 last_identifier_frame_ = frame_id; | 199 last_identifier_frame_ = frame_id; |
109 | 200 |
110 return result; | 201 return result; |
111 } | 202 } |
112 | 203 |
113 } // namespace test | 204 } // namespace test |
114 } // namespace remoting | 205 } // namespace remoting |
OLD | NEW |