OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "media/video/capture/screen/screen_capturer_fake.h" | 5 #include "media/video/capture/screen/screen_capturer_fake.h" |
6 | 6 |
| 7 #include "base/logging.h" |
7 #include "base/time.h" | 8 #include "base/time.h" |
8 #include "media/video/capture/screen/screen_capture_data.h" | 9 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
9 | 10 |
10 namespace media { | 11 namespace media { |
11 | 12 |
12 // ScreenCapturerFake generates a white picture of size kWidth x kHeight | 13 // ScreenCapturerFake generates a white picture of size kWidth x kHeight |
13 // with a rectangle of size kBoxWidth x kBoxHeight. The rectangle moves kSpeed | 14 // with a rectangle of size kBoxWidth x kBoxHeight. The rectangle moves kSpeed |
14 // pixels per frame along both axes, and bounces off the sides of the screen. | 15 // pixels per frame along both axes, and bounces off the sides of the screen. |
15 static const int kWidth = ScreenCapturerFake::kWidth; | 16 static const int kWidth = ScreenCapturerFake::kWidth; |
16 static const int kHeight = ScreenCapturerFake::kHeight; | 17 static const int kHeight = ScreenCapturerFake::kHeight; |
17 static const int kBoxWidth = 140; | 18 static const int kBoxWidth = 140; |
18 static const int kBoxHeight = 140; | 19 static const int kBoxHeight = 140; |
19 static const int kSpeed = 20; | 20 static const int kSpeed = 20; |
20 | 21 |
21 COMPILE_ASSERT(kBoxWidth < kWidth && kBoxHeight < kHeight, bad_box_size); | 22 COMPILE_ASSERT(kBoxWidth < kWidth && kBoxHeight < kHeight, bad_box_size); |
22 COMPILE_ASSERT((kBoxWidth % kSpeed == 0) && (kWidth % kSpeed == 0) && | 23 COMPILE_ASSERT((kBoxWidth % kSpeed == 0) && (kWidth % kSpeed == 0) && |
23 (kBoxHeight % kSpeed == 0) && (kHeight % kSpeed == 0), | 24 (kBoxHeight % kSpeed == 0) && (kHeight % kSpeed == 0), |
24 sizes_must_be_multiple_of_kSpeed); | 25 sizes_must_be_multiple_of_kSpeed); |
25 | 26 |
26 ScreenCapturerFake::ScreenCapturerFake() | 27 ScreenCapturerFake::ScreenCapturerFake() |
27 : size_(SkISize::Make(0, 0)), | 28 : callback_(NULL), |
| 29 mouse_shape_observer_(NULL), |
28 bytes_per_row_(0), | 30 bytes_per_row_(0), |
29 box_pos_x_(0), | 31 box_pos_x_(0), |
30 box_pos_y_(0), | 32 box_pos_y_(0), |
31 box_speed_x_(kSpeed), | 33 box_speed_x_(kSpeed), |
32 box_speed_y_(kSpeed), | 34 box_speed_y_(kSpeed) { |
33 current_buffer_(0) { | |
34 ScreenConfigurationChanged(); | 35 ScreenConfigurationChanged(); |
35 } | 36 } |
36 | 37 |
37 ScreenCapturerFake::~ScreenCapturerFake() { | 38 ScreenCapturerFake::~ScreenCapturerFake() { |
38 } | 39 } |
39 | 40 |
40 void ScreenCapturerFake::Start(Delegate* delegate) { | 41 void ScreenCapturerFake::Start(Callback* callback) { |
41 delegate_ = delegate; | 42 DCHECK(!callback_); |
42 | 43 DCHECK(callback); |
43 // Create memory for the buffers. | 44 callback_ = callback; |
44 int buffer_size = size_.height() * bytes_per_row_; | |
45 for (int i = 0; i < kNumBuffers; i++) { | |
46 shared_buffers_[i] = delegate_->CreateSharedBuffer(buffer_size); | |
47 if (shared_buffers_[i]) { | |
48 buffers_[i] = reinterpret_cast<uint8*>(shared_buffers_[i]->ptr()); | |
49 } else { | |
50 private_buffers_[i].reset(new uint8[buffer_size]); | |
51 buffers_[i] = private_buffers_[i].get(); | |
52 } | |
53 } | |
54 } | 45 } |
55 | 46 |
56 void ScreenCapturerFake::CaptureFrame() { | 47 void ScreenCapturerFake::Capture(const webrtc::DesktopRegion& region) { |
57 base::Time capture_start_time = base::Time::Now(); | 48 base::Time capture_start_time = base::Time::Now(); |
58 | 49 |
| 50 queue_.MoveToNextFrame(); |
| 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.Pass()); |
| 65 } |
| 66 |
| 67 DCHECK(queue_.current_frame()); |
59 GenerateImage(); | 68 GenerateImage(); |
60 helper_.InvalidateScreen(size_); | |
61 | 69 |
62 SkRegion invalid_region; | 70 queue_.current_frame()->mutable_updated_region()->SetRect( |
63 helper_.SwapInvalidRegion(&invalid_region); | 71 webrtc::DesktopRect::MakeSize(size_)); |
| 72 queue_.current_frame()->set_capture_time_ms( |
| 73 (base::Time::Now() - capture_start_time).InMillisecondsRoundedUp()); |
64 | 74 |
65 current_buffer_ = (current_buffer_ + 1) % kNumBuffers; | 75 callback_->OnCaptureCompleted(queue_.current_frame()->Share()); |
| 76 } |
66 | 77 |
67 scoped_refptr<ScreenCaptureData> capture_data(new ScreenCaptureData( | 78 void ScreenCapturerFake::SetMouseShapeObserver( |
68 buffers_[current_buffer_], bytes_per_row_, size_)); | 79 MouseShapeObserver* mouse_shape_observer) { |
69 capture_data->mutable_dirty_region() = invalid_region; | 80 DCHECK(!mouse_shape_observer_); |
70 | 81 DCHECK(mouse_shape_observer); |
71 helper_.set_size_most_recent(size_); | 82 mouse_shape_observer_ = mouse_shape_observer; |
72 | |
73 capture_data->set_shared_buffer(shared_buffers_[current_buffer_]); | |
74 | |
75 capture_data->set_capture_time_ms( | |
76 (base::Time::Now() - capture_start_time).InMillisecondsRoundedUp()); | |
77 delegate_->OnCaptureCompleted(capture_data); | |
78 } | 83 } |
79 | 84 |
80 void ScreenCapturerFake::GenerateImage() { | 85 void ScreenCapturerFake::GenerateImage() { |
81 memset(buffers_[current_buffer_], 0xff, | 86 webrtc::DesktopFrame* frame = queue_.current_frame(); |
82 size_.width() * size_.height() * ScreenCaptureData::kBytesPerPixel); | |
83 | 87 |
84 uint8* row = buffers_[current_buffer_] + | 88 const int kBytesPerPixel = webrtc::DesktopFrame::kBytesPerPixel; |
85 (box_pos_y_ * size_.width() + box_pos_x_) * | 89 |
86 ScreenCaptureData::kBytesPerPixel; | 90 memset(frame->data(), 0xff, |
| 91 size_.width() * size_.height() * kBytesPerPixel); |
| 92 |
| 93 uint8* row = frame->data() + |
| 94 (box_pos_y_ * size_.width() + box_pos_x_) * kBytesPerPixel; |
87 | 95 |
88 box_pos_x_ += box_speed_x_; | 96 box_pos_x_ += box_speed_x_; |
89 if (box_pos_x_ + kBoxWidth >= size_.width() || box_pos_x_ == 0) | 97 if (box_pos_x_ + kBoxWidth >= size_.width() || box_pos_x_ == 0) |
90 box_speed_x_ = -box_speed_x_; | 98 box_speed_x_ = -box_speed_x_; |
91 | 99 |
92 box_pos_y_ += box_speed_y_; | 100 box_pos_y_ += box_speed_y_; |
93 if (box_pos_y_ + kBoxHeight >= size_.height() || box_pos_y_ == 0) | 101 if (box_pos_y_ + kBoxHeight >= size_.height() || box_pos_y_ == 0) |
94 box_speed_y_ = -box_speed_y_; | 102 box_speed_y_ = -box_speed_y_; |
95 | 103 |
96 // Draw rectangle with the following colors in its corners: | 104 // Draw rectangle with the following colors in its corners: |
97 // cyan....yellow | 105 // cyan....yellow |
98 // .............. | 106 // .............. |
99 // blue.......red | 107 // blue.......red |
100 for (int y = 0; y < kBoxHeight; ++y) { | 108 for (int y = 0; y < kBoxHeight; ++y) { |
101 for (int x = 0; x < kBoxWidth; ++x) { | 109 for (int x = 0; x < kBoxWidth; ++x) { |
102 int r = x * 255 / kBoxWidth; | 110 int r = x * 255 / kBoxWidth; |
103 int g = y * 255 / kBoxHeight; | 111 int g = y * 255 / kBoxHeight; |
104 int b = 255 - (x * 255 / kBoxWidth); | 112 int b = 255 - (x * 255 / kBoxWidth); |
105 row[x * ScreenCaptureData::kBytesPerPixel] = r; | 113 row[x * kBytesPerPixel] = r; |
106 row[x * ScreenCaptureData::kBytesPerPixel + 1] = g; | 114 row[x * kBytesPerPixel + 1] = g; |
107 row[x * ScreenCaptureData::kBytesPerPixel + 2] = b; | 115 row[x * kBytesPerPixel + 2] = b; |
108 row[x * ScreenCaptureData::kBytesPerPixel + 3] = 0xff; | 116 row[x * kBytesPerPixel + 3] = 0xff; |
109 } | 117 } |
110 row += bytes_per_row_; | 118 row += bytes_per_row_; |
111 } | 119 } |
112 } | 120 } |
113 | 121 |
114 void ScreenCapturerFake::ScreenConfigurationChanged() { | 122 void ScreenCapturerFake::ScreenConfigurationChanged() { |
115 size_ = SkISize::Make(kWidth, kHeight); | 123 size_.set(kWidth, kHeight); |
116 bytes_per_row_ = size_.width() * ScreenCaptureData::kBytesPerPixel; | 124 queue_.Reset(); |
| 125 bytes_per_row_ = size_.width() * webrtc::DesktopFrame::kBytesPerPixel; |
117 } | 126 } |
118 | 127 |
119 } // namespace media | 128 } // namespace media |
OLD | NEW |