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" | 7 #include "base/base_paths.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/time/default_tick_clock.h" | 10 #include "base/time/default_tick_clock.h" |
11 #include "third_party/skia/include/core/SkBitmap.h" | 11 #include "third_party/skia/include/core/SkBitmap.h" |
12 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" | 12 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
13 #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" | 14 #include "ui/gfx/codec/png_codec.h" |
15 | 15 |
16 namespace remoting { | 16 namespace remoting { |
17 namespace test { | 17 namespace test { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 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 |
21 scoped_ptr<webrtc::DesktopFrame> LoadDesktopFrameFromPng( | 27 scoped_ptr<webrtc::DesktopFrame> LoadDesktopFrameFromPng( |
22 const base::FilePath& file_path) { | 28 const base::FilePath& file_path) { |
23 std::string file_content; | 29 std::string file_content; |
24 if (!base::ReadFileToString(file_path, &file_content)) | 30 if (!base::ReadFileToString(file_path, &file_content)) |
25 LOG(FATAL) << "Failed to read " << file_path.MaybeAsASCII() | 31 LOG(FATAL) << "Failed to read " << file_path.MaybeAsASCII() |
26 << ". Please run remoting/test/data/download.sh"; | 32 << ". Please run remoting/test/data/download.sh"; |
27 SkBitmap bitmap; | 33 SkBitmap bitmap; |
28 gfx::PNGCodec::Decode(reinterpret_cast<const uint8_t*>(file_content.data()), | 34 gfx::PNGCodec::Decode(reinterpret_cast<const uint8_t*>(file_content.data()), |
29 file_content.size(), &bitmap); | 35 file_content.size(), &bitmap); |
30 scoped_ptr<webrtc::DesktopFrame> frame(new webrtc::BasicDesktopFrame( | 36 scoped_ptr<webrtc::DesktopFrame> frame(new webrtc::BasicDesktopFrame( |
31 webrtc::DesktopSize(bitmap.width(), bitmap.height()))); | 37 webrtc::DesktopSize(bitmap.width(), bitmap.height()))); |
32 bitmap.copyPixelsTo(frame->data(), | 38 bitmap.copyPixelsTo(frame->data(), |
33 frame->stride() * frame->size().height(), | 39 frame->stride() * frame->size().height(), |
34 frame->stride()); | 40 frame->stride()); |
35 return frame; | 41 return frame; |
36 } | 42 } |
37 | 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 |
38 } // namespace | 57 } // namespace |
| 58 CyclicFrameGenerator::FrameInfo::FrameInfo() = default; |
| 59 |
| 60 CyclicFrameGenerator::FrameInfo::FrameInfo(int frame_id, |
| 61 FrameType type, |
| 62 base::TimeTicks timestamp) |
| 63 : frame_id(frame_id), type(type), timestamp(timestamp) {} |
39 | 64 |
40 // static | 65 // static |
41 scoped_refptr<CyclicFrameGenerator> CyclicFrameGenerator::Create() { | 66 scoped_refptr<CyclicFrameGenerator> CyclicFrameGenerator::Create() { |
42 base::FilePath test_data_path; | 67 base::FilePath test_data_path; |
43 PathService::Get(base::DIR_SOURCE_ROOT, &test_data_path); | 68 PathService::Get(base::DIR_SOURCE_ROOT, &test_data_path); |
44 test_data_path = test_data_path.Append(FILE_PATH_LITERAL("remoting")); | 69 test_data_path = test_data_path.Append(FILE_PATH_LITERAL("remoting")); |
45 test_data_path = test_data_path.Append(FILE_PATH_LITERAL("test")); | 70 test_data_path = test_data_path.Append(FILE_PATH_LITERAL("test")); |
46 test_data_path = test_data_path.Append(FILE_PATH_LITERAL("data")); | 71 test_data_path = test_data_path.Append(FILE_PATH_LITERAL("data")); |
47 | 72 |
48 std::vector<scoped_ptr<webrtc::DesktopFrame>> frames; | 73 std::vector<scoped_ptr<webrtc::DesktopFrame>> frames; |
49 frames.push_back( | 74 frames.push_back( |
50 LoadDesktopFrameFromPng(test_data_path.AppendASCII("test_frame1.png"))); | 75 LoadDesktopFrameFromPng(test_data_path.AppendASCII("test_frame1.png"))); |
51 frames.push_back( | 76 frames.push_back( |
52 LoadDesktopFrameFromPng(test_data_path.AppendASCII("test_frame2.png"))); | 77 LoadDesktopFrameFromPng(test_data_path.AppendASCII("test_frame2.png"))); |
53 return new CyclicFrameGenerator(std::move(frames)); | 78 return new CyclicFrameGenerator(std::move(frames)); |
54 } | 79 } |
55 | 80 |
56 CyclicFrameGenerator::CyclicFrameGenerator( | 81 CyclicFrameGenerator::CyclicFrameGenerator( |
57 std::vector<scoped_ptr<webrtc::DesktopFrame>> reference_frames) | 82 std::vector<scoped_ptr<webrtc::DesktopFrame>> reference_frames) |
58 : reference_frames_(std::move(reference_frames)), | 83 : reference_frames_(std::move(reference_frames)), |
59 clock_(&default_tick_clock_), | 84 clock_(&default_tick_clock_), |
60 started_time_(clock_->NowTicks()) { | 85 started_time_(clock_->NowTicks()) { |
61 CHECK(!reference_frames_.empty()); | 86 CHECK(!reference_frames_.empty()); |
62 screen_size_ = reference_frames_[0]->size(); | 87 screen_size_ = reference_frames_[0]->size(); |
63 for (const auto& frame : reference_frames_) { | 88 for (const auto& frame : reference_frames_) { |
64 CHECK(screen_size_.equals(frame->size())) | 89 CHECK(screen_size_.equals(frame->size())) |
65 << "All reference frames should have the same size."; | 90 << "All reference frames should have the same size."; |
66 } | 91 } |
67 } | 92 } |
| 93 |
68 CyclicFrameGenerator::~CyclicFrameGenerator() {} | 94 CyclicFrameGenerator::~CyclicFrameGenerator() {} |
69 | 95 |
70 void CyclicFrameGenerator::SetTickClock(base::TickClock* tick_clock) { | 96 void CyclicFrameGenerator::SetTickClock(base::TickClock* tick_clock) { |
71 clock_ = tick_clock; | 97 clock_ = tick_clock; |
72 started_time_ = clock_->NowTicks(); | 98 started_time_ = clock_->NowTicks(); |
73 } | 99 } |
74 | 100 |
75 scoped_ptr<webrtc::DesktopFrame> CyclicFrameGenerator::GenerateFrame( | 101 scoped_ptr<webrtc::DesktopFrame> CyclicFrameGenerator::GenerateFrame( |
76 webrtc::DesktopCapturer::Callback* callback) { | 102 webrtc::DesktopCapturer::Callback* callback) { |
77 base::TimeTicks now = clock_->NowTicks(); | 103 base::TimeTicks now = clock_->NowTicks(); |
78 int reference_frame = | 104 int reference_frame = |
79 ((now - started_time_) / frame_cycle_period_) % reference_frames_.size(); | 105 ((now - started_time_) / frame_cycle_period_) % reference_frames_.size(); |
80 bool cursor_state = ((now - started_time_) / cursor_blink_period_) % 2; | 106 bool cursor_state = ((now - started_time_) / cursor_blink_period_) % 2; |
81 | 107 |
82 scoped_ptr<webrtc::DesktopFrame> frame( | 108 scoped_ptr<webrtc::DesktopFrame> frame( |
83 new webrtc::BasicDesktopFrame(screen_size_)); | 109 new webrtc::BasicDesktopFrame(screen_size_)); |
84 frame->CopyPixelsFrom(*reference_frames_[reference_frame], | 110 frame->CopyPixelsFrom(*reference_frames_[reference_frame], |
85 webrtc::DesktopVector(), | 111 webrtc::DesktopVector(), |
86 webrtc::DesktopRect::MakeSize(screen_size_)); | 112 webrtc::DesktopRect::MakeSize(screen_size_)); |
87 | 113 |
88 // Render the cursor. | 114 // Render the cursor. |
89 webrtc::DesktopRect cursor_rect = | 115 webrtc::DesktopRect cursor_rect = |
90 webrtc::DesktopRect::MakeXYWH(20, 20, 2, 20); | 116 webrtc::DesktopRect::MakeXYWH(20, 20, 2, 20); |
91 if (cursor_state) { | 117 if (cursor_state) { |
92 for (int y = cursor_rect.top(); y < cursor_rect.bottom(); ++y) { | 118 DrawRect(frame.get(), cursor_rect, 0); |
93 memset(frame->data() + y * frame->stride() + | |
94 cursor_rect.left() * webrtc::DesktopFrame::kBytesPerPixel, | |
95 0, cursor_rect.width() * webrtc::DesktopFrame::kBytesPerPixel); | |
96 } | |
97 } | 119 } |
98 | 120 |
99 if (last_reference_frame_ != reference_frame) { | 121 if (last_reference_frame_ != reference_frame) { |
100 // The whole frame has changed. | 122 // The whole frame has changed. |
101 frame->mutable_updated_region()->AddRect( | 123 frame->mutable_updated_region()->AddRect( |
102 webrtc::DesktopRect::MakeSize(screen_size_)); | 124 webrtc::DesktopRect::MakeSize(screen_size_)); |
103 last_frame_type_ = FrameType::FULL; | 125 last_frame_type_ = FrameType::FULL; |
104 } else if (last_cursor_state_ != cursor_state) { | 126 } else if (last_cursor_state_ != cursor_state) { |
105 // Cursor state has changed. | 127 // Cursor state has changed. |
106 frame->mutable_updated_region()->AddRect(cursor_rect); | 128 frame->mutable_updated_region()->AddRect(cursor_rect); |
107 last_frame_type_ = FrameType::CURSOR; | 129 last_frame_type_ = FrameType::CURSOR; |
108 } else { | 130 } else { |
109 // No changes. | 131 // No changes. |
110 last_frame_type_ = FrameType::EMPTY; | 132 last_frame_type_ = FrameType::EMPTY; |
111 } | 133 } |
| 134 |
| 135 // Increment frame_id for non-empty frames. |
| 136 int frame_id = (last_frame_type_ == FrameType::EMPTY) ? last_frame_id_ |
| 137 : last_frame_id_ + 1; |
| 138 |
| 139 // Render barcode. |
| 140 if (draw_barcode_) { |
| 141 uint32_t value = static_cast<uint32_t>(frame_id); |
| 142 CHECK(value < (1U << kBarcodeBits)); |
| 143 for (int i = 0; i < kBarcodeBits; ++i) { |
| 144 DrawRect(frame.get(), webrtc::DesktopRect::MakeXYWH(i * kBarcodeCellWidth, |
| 145 0, kBarcodeCellWidth, |
| 146 kBarcodeCellHeight), |
| 147 (value & 1) ? 0xffffffff : 0xff000000); |
| 148 value >>= 1; |
| 149 } |
| 150 |
| 151 if (frame_id != last_frame_id_) { |
| 152 frame->mutable_updated_region()->AddRect(webrtc::DesktopRect::MakeXYWH( |
| 153 0, 0, kBarcodeCellWidth * kBarcodeBits, kBarcodeCellHeight)); |
| 154 } |
| 155 } |
| 156 |
| 157 // Add non-empty frames to |generated_frames_info_|. |
| 158 if (last_frame_type_ != FrameType::EMPTY) { |
| 159 generated_frames_info_[frame_id] = |
| 160 FrameInfo(frame_id, last_frame_type_, clock_->NowTicks()); |
| 161 } |
| 162 |
112 last_reference_frame_ = reference_frame; | 163 last_reference_frame_ = reference_frame; |
113 last_cursor_state_ = cursor_state; | 164 last_cursor_state_ = cursor_state; |
| 165 last_frame_id_ = frame_id; |
114 | 166 |
115 return frame; | 167 return frame; |
116 } | 168 } |
117 | 169 |
| 170 CyclicFrameGenerator::FrameInfo CyclicFrameGenerator::IdentifyFrame( |
| 171 webrtc::DesktopFrame* frame) { |
| 172 CHECK(draw_barcode_); |
| 173 int frame_id = 0; |
| 174 for (int i = kBarcodeBits - 1; i >= 0; --i) { |
| 175 // Sample barcode in the center of the cell for each bit. |
| 176 int x = i * kBarcodeCellWidth + kBarcodeCellWidth / 2; |
| 177 int y = kBarcodeCellHeight / 2; |
| 178 uint8_t* data = (frame->data() + y * frame->stride() + |
| 179 x * webrtc::DesktopFrame::kBytesPerPixel); |
| 180 int b = data[0]; |
| 181 int g = data[1]; |
| 182 int r = data[2]; |
| 183 bool bit = 0; |
| 184 if (b > kBarcodeWhiteThreshold && g > kBarcodeWhiteThreshold && |
| 185 r > kBarcodeWhiteThreshold) { |
| 186 bit = 1; |
| 187 } else if (b < kBarcodeBlackThreshold && g < kBarcodeBlackThreshold && |
| 188 r < kBarcodeBlackThreshold) { |
| 189 bit = 0; |
| 190 } else { |
| 191 LOG(FATAL) << "Invalid barcode."; |
| 192 } |
| 193 frame_id <<= 1; |
| 194 if (bit) |
| 195 frame_id |= 1; |
| 196 } |
| 197 |
| 198 if (!generated_frames_info_.count(frame_id)) |
| 199 LOG(FATAL) << "Barcode contains unknown frame_id: " << frame_id; |
| 200 |
| 201 return generated_frames_info_[frame_id]; |
| 202 } |
| 203 |
118 } // namespace test | 204 } // namespace test |
119 } // namespace remoting | 205 } // namespace remoting |
OLD | NEW |