OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/host/fake_screen_capturer.h" | 5 #include "remoting/host/fake_screen_capturer.h" |
6 | 6 |
7 #include "base/bind.h" | |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/ref_counted.h" | |
8 #include "base/time/time.h" | 10 #include "base/time/time.h" |
9 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" | 11 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
10 | 12 |
11 namespace remoting { | 13 namespace remoting { |
12 | 14 |
13 // FakeScreenCapturer generates a white picture of size kWidth x kHeight | 15 // FakeScreenCapturer generates a white picture of size kWidth x kHeight |
14 // with a rectangle of size kBoxWidth x kBoxHeight. The rectangle moves kSpeed | 16 // with a rectangle of size kBoxWidth x kBoxHeight. The rectangle moves kSpeed |
15 // pixels per frame along both axes, and bounces off the sides of the screen. | 17 // pixels per frame along both axes, and bounces off the sides of the screen. |
16 static const int kWidth = FakeScreenCapturer::kWidth; | 18 static const int kWidth = FakeScreenCapturer::kWidth; |
17 static const int kHeight = FakeScreenCapturer::kHeight; | 19 static const int kHeight = FakeScreenCapturer::kHeight; |
18 static const int kBoxWidth = 140; | 20 static const int kBoxWidth = 140; |
19 static const int kBoxHeight = 140; | 21 static const int kBoxHeight = 140; |
20 static const int kSpeed = 20; | 22 static const int kSpeed = 20; |
21 | 23 |
22 COMPILE_ASSERT(kBoxWidth < kWidth && kBoxHeight < kHeight, bad_box_size); | 24 COMPILE_ASSERT(kBoxWidth < kWidth && kBoxHeight < kHeight, bad_box_size); |
23 COMPILE_ASSERT((kBoxWidth % kSpeed == 0) && (kWidth % kSpeed == 0) && | 25 COMPILE_ASSERT((kBoxWidth % kSpeed == 0) && (kWidth % kSpeed == 0) && |
24 (kBoxHeight % kSpeed == 0) && (kHeight % kSpeed == 0), | 26 (kBoxHeight % kSpeed == 0) && (kHeight % kSpeed == 0), |
25 sizes_must_be_multiple_of_kSpeed); | 27 sizes_must_be_multiple_of_kSpeed); |
26 | 28 |
29 namespace { | |
30 | |
31 class DefaultFrameGenerator | |
32 : public base::RefCountedThreadSafe<DefaultFrameGenerator> { | |
33 public: | |
34 DefaultFrameGenerator() | |
35 : bytes_per_row_(0), | |
36 box_pos_x_(0), | |
37 box_pos_y_(0), | |
38 box_speed_x_(kSpeed), | |
39 box_speed_y_(kSpeed) {} | |
40 | |
41 scoped_ptr<webrtc::DesktopFrame> GenerateFrame( | |
42 webrtc::ScreenCapturer::Callback* callback); | |
43 | |
44 private: | |
45 ~DefaultFrameGenerator() {} | |
46 friend class RefCountedThreadSafe<DefaultFrameGenerator>; | |
47 | |
48 webrtc::DesktopSize size_; | |
49 int bytes_per_row_; | |
50 int box_pos_x_; | |
51 int box_pos_y_; | |
52 int box_speed_x_; | |
53 int box_speed_y_; | |
54 | |
55 DISALLOW_COPY_AND_ASSIGN(DefaultFrameGenerator); | |
56 }; | |
57 | |
58 scoped_ptr<webrtc::DesktopFrame> DefaultFrameGenerator::GenerateFrame( | |
59 webrtc::ScreenCapturer::Callback* callback) { | |
60 const int kBytesPerPixel = webrtc::DesktopFrame::kBytesPerPixel; | |
61 int buffer_size = kWidth * kHeight * kBytesPerPixel; | |
62 webrtc::SharedMemory* shared_memory = | |
63 callback->CreateSharedMemory(buffer_size); | |
64 scoped_ptr<webrtc::DesktopFrame> frame; | |
65 if (shared_memory) { | |
66 frame.reset(new webrtc::SharedMemoryDesktopFrame( | |
67 webrtc::DesktopSize(kWidth, kHeight), bytes_per_row_, shared_memory)); | |
68 } else { | |
69 frame.reset( | |
70 new webrtc::BasicDesktopFrame(webrtc::DesktopSize(kWidth, kHeight))); | |
71 } | |
72 | |
73 // Move the box. | |
74 box_pos_x_ += box_speed_x_; | |
75 if (box_pos_x_ + kBoxWidth >= kWidth || box_pos_x_ == 0) | |
76 box_speed_x_ = -box_speed_x_; | |
77 | |
78 box_pos_y_ += box_speed_y_; | |
79 if (box_pos_y_ + kBoxHeight >= kHeight || box_pos_y_ == 0) | |
80 box_speed_y_ = -box_speed_y_; | |
81 | |
82 memset(frame->data(), 0xff, kHeight * frame->stride()); | |
83 | |
84 // Draw rectangle with the following colors in its corners: | |
85 // cyan....yellow | |
86 // .............. | |
87 // blue.......red | |
88 uint8* row = frame->data() + | |
89 (box_pos_y_ * size_.width() + box_pos_x_) * kBytesPerPixel; | |
90 for (int y = 0; y < kBoxHeight; ++y) { | |
91 for (int x = 0; x < kBoxWidth; ++x) { | |
92 int r = x * 255 / kBoxWidth; | |
93 int g = y * 255 / kBoxHeight; | |
94 int b = 255 - (x * 255 / kBoxWidth); | |
95 row[x * kBytesPerPixel] = r; | |
96 row[x * kBytesPerPixel + 1] = g; | |
97 row[x * kBytesPerPixel + 2] = b; | |
98 row[x * kBytesPerPixel + 3] = 0xff; | |
99 } | |
100 row += frame->stride(); | |
101 } | |
102 | |
103 frame->mutable_updated_region()->SetRect( | |
rmsousa
2014/07/16 23:06:00
I'm not sure how this updated region detection wor
Sergey Ulanov
2014/07/17 01:47:58
Yes, it hints the VP8 which parts of the frame hav
| |
104 webrtc::DesktopRect::MakeXYWH(0, 0, kWidth, kHeight)); | |
105 | |
106 return frame.Pass(); | |
107 } | |
108 | |
109 } // namespace | |
110 | |
27 FakeScreenCapturer::FakeScreenCapturer() | 111 FakeScreenCapturer::FakeScreenCapturer() |
28 : callback_(NULL), | 112 : callback_(NULL), |
29 mouse_shape_observer_(NULL), | 113 mouse_shape_observer_(NULL) { |
30 bytes_per_row_(0), | 114 frame_generator_ = base::Bind(&DefaultFrameGenerator::GenerateFrame, |
31 box_pos_x_(0), | 115 new DefaultFrameGenerator()); |
32 box_pos_y_(0), | |
33 box_speed_x_(kSpeed), | |
34 box_speed_y_(kSpeed) { | |
35 ScreenConfigurationChanged(); | |
36 } | 116 } |
37 | 117 |
38 FakeScreenCapturer::~FakeScreenCapturer() { | 118 FakeScreenCapturer::~FakeScreenCapturer() {} |
119 | |
120 void FakeScreenCapturer::set_frame_generator( | |
121 const FrameGenerator& frame_generator) { | |
122 DCHECK(!callback_); | |
123 frame_generator_ = frame_generator; | |
39 } | 124 } |
40 | 125 |
41 void FakeScreenCapturer::Start(Callback* callback) { | 126 void FakeScreenCapturer::Start(Callback* callback) { |
42 DCHECK(!callback_); | 127 DCHECK(!callback_); |
43 DCHECK(callback); | 128 DCHECK(callback); |
44 callback_ = callback; | 129 callback_ = callback; |
45 } | 130 } |
46 | 131 |
47 void FakeScreenCapturer::Capture(const webrtc::DesktopRegion& region) { | 132 void FakeScreenCapturer::Capture(const webrtc::DesktopRegion& region) { |
48 base::Time capture_start_time = base::Time::Now(); | 133 base::Time capture_start_time = base::Time::Now(); |
49 | 134 scoped_ptr<webrtc::DesktopFrame> frame = frame_generator_.Run(callback_); |
50 queue_.MoveToNextFrame(); | 135 frame->set_capture_time_ms( |
51 | |
52 if (!queue_.current_frame()) { | |
53 int buffer_size = size_.height() * bytes_per_row_; | |
54 webrtc::SharedMemory* shared_memory = | |
55 callback_->CreateSharedMemory(buffer_size); | |
56 scoped_ptr<webrtc::DesktopFrame> frame; | |
57 webrtc::DesktopSize frame_size(size_.width(), size_.height()); | |
58 if (shared_memory) { | |
59 frame.reset(new webrtc::SharedMemoryDesktopFrame( | |
60 frame_size, bytes_per_row_, shared_memory)); | |
61 } else { | |
62 frame.reset(new webrtc::BasicDesktopFrame(frame_size)); | |
63 } | |
64 queue_.ReplaceCurrentFrame(frame.release()); | |
65 } | |
66 | |
67 DCHECK(queue_.current_frame()); | |
68 GenerateImage(); | |
69 | |
70 queue_.current_frame()->mutable_updated_region()->SetRect( | |
71 webrtc::DesktopRect::MakeSize(size_)); | |
72 queue_.current_frame()->set_capture_time_ms( | |
73 (base::Time::Now() - capture_start_time).InMillisecondsRoundedUp()); | 136 (base::Time::Now() - capture_start_time).InMillisecondsRoundedUp()); |
74 | 137 callback_->OnCaptureCompleted(frame.release()); |
75 callback_->OnCaptureCompleted(queue_.current_frame()->Share()); | |
76 } | 138 } |
77 | 139 |
78 void FakeScreenCapturer::SetMouseShapeObserver( | 140 void FakeScreenCapturer::SetMouseShapeObserver( |
79 MouseShapeObserver* mouse_shape_observer) { | 141 MouseShapeObserver* mouse_shape_observer) { |
80 DCHECK(!mouse_shape_observer_); | 142 DCHECK(!mouse_shape_observer_); |
81 DCHECK(mouse_shape_observer); | 143 DCHECK(mouse_shape_observer); |
82 mouse_shape_observer_ = mouse_shape_observer; | 144 mouse_shape_observer_ = mouse_shape_observer; |
83 } | 145 } |
84 | 146 |
85 bool FakeScreenCapturer::GetScreenList(ScreenList* screens) { | 147 bool FakeScreenCapturer::GetScreenList(ScreenList* screens) { |
86 NOTIMPLEMENTED(); | 148 NOTIMPLEMENTED(); |
87 return false; | 149 return false; |
88 } | 150 } |
89 | 151 |
90 bool FakeScreenCapturer::SelectScreen(webrtc::ScreenId id) { | 152 bool FakeScreenCapturer::SelectScreen(webrtc::ScreenId id) { |
91 NOTIMPLEMENTED(); | 153 NOTIMPLEMENTED(); |
92 return false; | 154 return false; |
93 } | 155 } |
94 | 156 |
95 void FakeScreenCapturer::GenerateImage() { | |
96 webrtc::DesktopFrame* frame = queue_.current_frame(); | |
97 | |
98 const int kBytesPerPixel = webrtc::DesktopFrame::kBytesPerPixel; | |
99 | |
100 memset(frame->data(), 0xff, | |
101 size_.width() * size_.height() * kBytesPerPixel); | |
102 | |
103 uint8* row = frame->data() + | |
104 (box_pos_y_ * size_.width() + box_pos_x_) * kBytesPerPixel; | |
105 | |
106 box_pos_x_ += box_speed_x_; | |
107 if (box_pos_x_ + kBoxWidth >= size_.width() || box_pos_x_ == 0) | |
108 box_speed_x_ = -box_speed_x_; | |
109 | |
110 box_pos_y_ += box_speed_y_; | |
111 if (box_pos_y_ + kBoxHeight >= size_.height() || box_pos_y_ == 0) | |
112 box_speed_y_ = -box_speed_y_; | |
113 | |
114 // Draw rectangle with the following colors in its corners: | |
115 // cyan....yellow | |
116 // .............. | |
117 // blue.......red | |
118 for (int y = 0; y < kBoxHeight; ++y) { | |
119 for (int x = 0; x < kBoxWidth; ++x) { | |
120 int r = x * 255 / kBoxWidth; | |
121 int g = y * 255 / kBoxHeight; | |
122 int b = 255 - (x * 255 / kBoxWidth); | |
123 row[x * kBytesPerPixel] = r; | |
124 row[x * kBytesPerPixel + 1] = g; | |
125 row[x * kBytesPerPixel + 2] = b; | |
126 row[x * kBytesPerPixel + 3] = 0xff; | |
127 } | |
128 row += bytes_per_row_; | |
129 } | |
130 } | |
131 | |
132 void FakeScreenCapturer::ScreenConfigurationChanged() { | |
133 size_.set(kWidth, kHeight); | |
134 queue_.Reset(); | |
135 bytes_per_row_ = size_.width() * webrtc::DesktopFrame::kBytesPerPixel; | |
136 } | |
137 | |
138 } // namespace remoting | 157 } // namespace remoting |
OLD | NEW |