Chromium Code Reviews| 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 callback_ = callback; |
|
alexeypa (please no reviews)
2013/04/26 21:33:58
DCHECK(!callback_);
DCHECK(callback);
Add the abo
Sergey Ulanov
2013/05/07 22:25:50
Done.
| |
| 42 | |
| 43 // Create memory for the buffers. | |
| 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 } | 43 } |
| 55 | 44 |
| 56 void ScreenCapturerFake::CaptureFrame() { | 45 void ScreenCapturerFake::Capture(const webrtc::DesktopRegion& region) { |
| 57 base::Time capture_start_time = base::Time::Now(); | 46 base::Time capture_start_time = base::Time::Now(); |
| 58 | 47 |
| 48 if (queue_.current_frame_needs_update()) { | |
|
Wez
2013/04/26 18:48:14
See comment on queue; I think it's better to just
Sergey Ulanov
2013/05/07 22:25:50
Done.
| |
| 49 int buffer_size = size_.height() * bytes_per_row_; | |
| 50 webrtc::SharedMemory* shared_memory = | |
| 51 callback_->CreateSharedMemory(buffer_size); | |
| 52 scoped_ptr<webrtc::DesktopFrame> frame; | |
| 53 webrtc::DesktopSize frame_size(size_.width(), size_.height()); | |
| 54 if (shared_memory) { | |
| 55 frame.reset(new webrtc::SharedMemoryDesktopFrame( | |
| 56 frame_size, bytes_per_row_, shared_memory)); | |
| 57 } else { | |
| 58 frame.reset(new webrtc::BasicDesktopFrame(frame_size)); | |
| 59 } | |
| 60 queue_.ReplaceCurrentFrame(frame.Pass()); | |
| 61 } | |
| 62 | |
| 63 DCHECK(queue_.current_frame()); | |
| 59 GenerateImage(); | 64 GenerateImage(); |
| 60 helper_.InvalidateScreen(size_); | |
| 61 | 65 |
| 62 SkRegion invalid_region; | 66 queue_.current_frame()->mutable_updated_region()->SetRect( |
| 63 helper_.SwapInvalidRegion(&invalid_region); | 67 webrtc::DesktopRect::MakeSize(size_)); |
| 68 queue_.current_frame()->set_capture_time_ms( | |
| 69 (base::Time::Now() - capture_start_time).InMillisecondsRoundedUp()); | |
| 64 | 70 |
| 65 current_buffer_ = (current_buffer_ + 1) % kNumBuffers; | 71 callback_->OnCaptureCompleted(queue_.EmitCurrentFrameAndMoveToNext()); |
| 72 } | |
| 66 | 73 |
| 67 scoped_refptr<ScreenCaptureData> capture_data(new ScreenCaptureData( | 74 void ScreenCapturerFake::SetMouseShapeObserver( |
| 68 buffers_[current_buffer_], bytes_per_row_, size_)); | 75 MouseShapeObserver* mouse_shape_observer) { |
| 69 capture_data->mutable_dirty_region() = invalid_region; | 76 mouse_shape_observer_ = mouse_shape_observer; |
|
alexeypa (please no reviews)
2013/04/26 21:33:58
DCHECK(!mouse_shape_observer_);
DCHECK(mouse_shape
Sergey Ulanov
2013/05/07 22:25:50
Done.
| |
| 70 | |
| 71 helper_.set_size_most_recent(size_); | |
| 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 } | 77 } |
| 79 | 78 |
| 80 void ScreenCapturerFake::GenerateImage() { | 79 void ScreenCapturerFake::GenerateImage() { |
| 81 memset(buffers_[current_buffer_], 0xff, | 80 webrtc::DesktopFrame* frame = queue_.current_frame(); |
| 82 size_.width() * size_.height() * ScreenCaptureData::kBytesPerPixel); | |
| 83 | 81 |
| 84 uint8* row = buffers_[current_buffer_] + | 82 const int kBytesPerPixel = webrtc::DesktopFrame::kBytesPerPixel; |
| 85 (box_pos_y_ * size_.width() + box_pos_x_) * | 83 |
| 86 ScreenCaptureData::kBytesPerPixel; | 84 memset(frame->data(), 0xff, |
| 85 size_.width() * size_.height() * kBytesPerPixel); | |
| 86 | |
| 87 uint8* row = frame->data() + | |
| 88 (box_pos_y_ * size_.width() + box_pos_x_) * kBytesPerPixel; | |
| 87 | 89 |
| 88 box_pos_x_ += box_speed_x_; | 90 box_pos_x_ += box_speed_x_; |
| 89 if (box_pos_x_ + kBoxWidth >= size_.width() || box_pos_x_ == 0) | 91 if (box_pos_x_ + kBoxWidth >= size_.width() || box_pos_x_ == 0) |
| 90 box_speed_x_ = -box_speed_x_; | 92 box_speed_x_ = -box_speed_x_; |
| 91 | 93 |
| 92 box_pos_y_ += box_speed_y_; | 94 box_pos_y_ += box_speed_y_; |
| 93 if (box_pos_y_ + kBoxHeight >= size_.height() || box_pos_y_ == 0) | 95 if (box_pos_y_ + kBoxHeight >= size_.height() || box_pos_y_ == 0) |
| 94 box_speed_y_ = -box_speed_y_; | 96 box_speed_y_ = -box_speed_y_; |
| 95 | 97 |
| 96 // Draw rectangle with the following colors in its corners: | 98 // Draw rectangle with the following colors in its corners: |
| 97 // cyan....yellow | 99 // cyan....yellow |
| 98 // .............. | 100 // .............. |
| 99 // blue.......red | 101 // blue.......red |
| 100 for (int y = 0; y < kBoxHeight; ++y) { | 102 for (int y = 0; y < kBoxHeight; ++y) { |
| 101 for (int x = 0; x < kBoxWidth; ++x) { | 103 for (int x = 0; x < kBoxWidth; ++x) { |
| 102 int r = x * 255 / kBoxWidth; | 104 int r = x * 255 / kBoxWidth; |
| 103 int g = y * 255 / kBoxHeight; | 105 int g = y * 255 / kBoxHeight; |
| 104 int b = 255 - (x * 255 / kBoxWidth); | 106 int b = 255 - (x * 255 / kBoxWidth); |
| 105 row[x * ScreenCaptureData::kBytesPerPixel] = r; | 107 row[x * kBytesPerPixel] = r; |
| 106 row[x * ScreenCaptureData::kBytesPerPixel + 1] = g; | 108 row[x * kBytesPerPixel + 1] = g; |
| 107 row[x * ScreenCaptureData::kBytesPerPixel + 2] = b; | 109 row[x * kBytesPerPixel + 2] = b; |
| 108 row[x * ScreenCaptureData::kBytesPerPixel + 3] = 0xff; | 110 row[x * kBytesPerPixel + 3] = 0xff; |
| 109 } | 111 } |
| 110 row += bytes_per_row_; | 112 row += bytes_per_row_; |
| 111 } | 113 } |
| 112 } | 114 } |
| 113 | 115 |
| 114 void ScreenCapturerFake::ScreenConfigurationChanged() { | 116 void ScreenCapturerFake::ScreenConfigurationChanged() { |
| 115 size_ = SkISize::Make(kWidth, kHeight); | 117 size_.set(kWidth, kHeight); |
| 116 bytes_per_row_ = size_.width() * ScreenCaptureData::kBytesPerPixel; | 118 queue_.SetAllFramesNeedUpdate(); |
| 119 bytes_per_row_ = size_.width() * webrtc::DesktopFrame::kBytesPerPixel; | |
| 117 } | 120 } |
| 118 | 121 |
| 119 } // namespace media | 122 } // namespace media |
| OLD | NEW |