| 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" | |
| 10 #include "base/time/default_tick_clock.h" | 7 #include "base/time/default_tick_clock.h" |
| 11 #include "third_party/skia/include/core/SkBitmap.h" | 8 #include "remoting/test/frame_generator_util.h" |
| 12 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" | 9 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
| 13 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h" | 10 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h" |
| 14 #include "ui/gfx/codec/png_codec.h" | |
| 15 | 11 |
| 16 namespace remoting { | 12 namespace remoting { |
| 17 namespace test { | 13 namespace test { |
| 18 | 14 |
| 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 | |
| 59 CyclicFrameGenerator::ChangeInfo::ChangeInfo() = default; | 15 CyclicFrameGenerator::ChangeInfo::ChangeInfo() = default; |
| 60 CyclicFrameGenerator::ChangeInfo::ChangeInfo(ChangeType type, | 16 CyclicFrameGenerator::ChangeInfo::ChangeInfo(ChangeType type, |
| 61 base::TimeTicks timestamp) | 17 base::TimeTicks timestamp) |
| 62 : type(type), timestamp(timestamp) {} | 18 : type(type), timestamp(timestamp) {} |
| 63 | 19 |
| 64 // static | 20 // static |
| 65 scoped_refptr<CyclicFrameGenerator> CyclicFrameGenerator::Create() { | 21 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 | |
| 72 std::vector<std::unique_ptr<webrtc::DesktopFrame>> frames; | 22 std::vector<std::unique_ptr<webrtc::DesktopFrame>> frames; |
| 73 frames.push_back( | 23 frames.push_back(LoadDesktopFrameFromPng("test_frame1.png")); |
| 74 LoadDesktopFrameFromPng(test_data_path.AppendASCII("test_frame1.png"))); | 24 frames.push_back(LoadDesktopFrameFromPng("test_frame2.png")); |
| 75 frames.push_back( | |
| 76 LoadDesktopFrameFromPng(test_data_path.AppendASCII("test_frame2.png"))); | |
| 77 return new CyclicFrameGenerator(std::move(frames)); | 25 return new CyclicFrameGenerator(std::move(frames)); |
| 78 } | 26 } |
| 79 | 27 |
| 80 CyclicFrameGenerator::CyclicFrameGenerator( | 28 CyclicFrameGenerator::CyclicFrameGenerator( |
| 81 std::vector<std::unique_ptr<webrtc::DesktopFrame>> reference_frames) | 29 std::vector<std::unique_ptr<webrtc::DesktopFrame>> reference_frames) |
| 82 : reference_frames_(std::move(reference_frames)), | 30 : reference_frames_(std::move(reference_frames)), |
| 83 clock_(&default_tick_clock_), | 31 clock_(&default_tick_clock_), |
| 84 started_time_(clock_->NowTicks()) { | 32 started_time_(clock_->NowTicks()) { |
| 85 CHECK(!reference_frames_.empty()); | 33 CHECK(!reference_frames_.empty()); |
| 86 screen_size_ = reference_frames_[0]->size(); | 34 screen_size_ = reference_frames_[0]->size(); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 last_frame_type_ = ChangeType::FULL; | 73 last_frame_type_ = ChangeType::FULL; |
| 126 } else if (last_cursor_state_ != cursor_state) { | 74 } else if (last_cursor_state_ != cursor_state) { |
| 127 // Cursor state has changed. | 75 // Cursor state has changed. |
| 128 frame->mutable_updated_region()->AddRect(cursor_rect); | 76 frame->mutable_updated_region()->AddRect(cursor_rect); |
| 129 last_frame_type_ = ChangeType::CURSOR; | 77 last_frame_type_ = ChangeType::CURSOR; |
| 130 } else { | 78 } else { |
| 131 // No changes. | 79 // No changes. |
| 132 last_frame_type_ = ChangeType::NO_CHANGES; | 80 last_frame_type_ = ChangeType::NO_CHANGES; |
| 133 } | 81 } |
| 134 | 82 |
| 135 // Render barcode. | 83 if (draw_barcode_) |
| 136 if (draw_barcode_) { | 84 DrawBarcode(frame_id, frame_id != last_frame_id_, frame.get()); |
| 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 } | |
| 152 | 85 |
| 153 last_reference_frame_ = reference_frame; | 86 last_reference_frame_ = reference_frame; |
| 154 last_cursor_state_ = cursor_state; | 87 last_cursor_state_ = cursor_state; |
| 155 last_frame_id_ = frame_id; | 88 last_frame_id_ = frame_id; |
| 156 | 89 |
| 157 return frame; | 90 return frame; |
| 158 } | 91 } |
| 159 | 92 |
| 160 CyclicFrameGenerator::ChangeInfoList CyclicFrameGenerator::GetChangeList( | 93 CyclicFrameGenerator::ChangeInfoList CyclicFrameGenerator::GetChangeList( |
| 161 webrtc::DesktopFrame* frame) { | 94 webrtc::DesktopFrame* frame) { |
| 162 CHECK(draw_barcode_); | 95 CHECK(draw_barcode_); |
| 163 int frame_id = 0; | 96 int frame_id = ReadBarcode(*frame); |
| 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 | |
| 188 CHECK_GE(frame_id, last_identifier_frame_); | 97 CHECK_GE(frame_id, last_identifier_frame_); |
| 189 | 98 |
| 190 ChangeInfoList result; | 99 ChangeInfoList result; |
| 191 for (int i = last_identifier_frame_ + 1; i <= frame_id; ++i) { | 100 for (int i = last_identifier_frame_ + 1; i <= frame_id; ++i) { |
| 192 ChangeType type = (i % (frame_cycle_period_ / cursor_blink_period_) == 0) | 101 ChangeType type = (i % (frame_cycle_period_ / cursor_blink_period_) == 0) |
| 193 ? ChangeType::FULL | 102 ? ChangeType::FULL |
| 194 : ChangeType::CURSOR; | 103 : ChangeType::CURSOR; |
| 195 base::TimeTicks timestamp = | 104 base::TimeTicks timestamp = |
| 196 started_time_ + i * base::TimeDelta(cursor_blink_period_); | 105 started_time_ + i * base::TimeDelta(cursor_blink_period_); |
| 197 result.push_back(ChangeInfo(type, timestamp)); | 106 result.push_back(ChangeInfo(type, timestamp)); |
| 198 } | 107 } |
| 199 last_identifier_frame_ = frame_id; | 108 last_identifier_frame_ = frame_id; |
| 200 | 109 |
| 201 return result; | 110 return result; |
| 202 } | 111 } |
| 203 | 112 |
| 204 } // namespace test | 113 } // namespace test |
| 205 } // namespace remoting | 114 } // namespace remoting |
| OLD | NEW |