1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/video/capture/screen/screen_capture_device.h"
#include "base/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/sequenced_task_runner.h"
#include "base/synchronization/lock.h"
#include "media/video/capture/screen/mouse_cursor_shape.h"
#include "media/video/capture/screen/screen_capture_data.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkDevice.h"
namespace media {
namespace {
const int kBytesPerPixel = 4;
} // namespace
class ScreenCaptureDevice::Core
: public base::RefCountedThreadSafe<Core>,
public ScreenCapturer::Delegate {
public:
explicit Core(scoped_refptr<base::SequencedTaskRunner> task_runner);
// Helper used in tests to supply a fake capturer.
void SetScreenCapturerForTest(scoped_ptr<ScreenCapturer> capturer) {
screen_capturer_ = capturer.Pass();
}
// Implementation of VideoCaptureDevice methods.
void Allocate(int width, int height,
int frame_rate,
EventHandler* event_handler);
void Start();
void Stop();
void DeAllocate();
// ScreenCapturer::Delegate interface. Called by |screen_capturer_| on the
// |task_runner_|.
virtual void OnCaptureCompleted(
scoped_refptr<ScreenCaptureData> capture_data) OVERRIDE;
virtual void OnCursorShapeChanged(
scoped_ptr<MouseCursorShape> cursor_shape) OVERRIDE;
private:
friend class base::RefCountedThreadSafe<Core>;
virtual ~Core();
// Helper methods that run on the |task_runner_|. Posted from the
// corresponding public methods.
void DoAllocate(int frame_rate);
void DoStart();
void DoStop();
void DoDeAllocate();
// Helper to schedule capture tasks.
void ScheduleCaptureTimer();
// Method that is scheduled on |task_runner_| to be called on regular interval
// to capture the screen.
void OnCaptureTimer();
// Captures a single frame.
void DoCapture();
// Task runner used for screen capturing operations.
scoped_refptr<base::SequencedTaskRunner> task_runner_;
// |event_handler_lock_| must be locked whenever |event_handler_| is used.
// It's necessary because DeAllocate() needs to reset it on the calling thread
// to ensure that the event handler is not called once DeAllocate() returns.
base::Lock event_handler_lock_;
EventHandler* event_handler_;
// Frame rate specified to Allocate().
int frame_rate_;
// The underlying ScreenCapturer instance used to capture frames.
scoped_ptr<ScreenCapturer> screen_capturer_;
// After Allocate() is called we need to call OnFrameInfo() method of the
// |event_handler_| to specify the size of the frames this capturer will
// produce. In order to get screen size from |screen_capturer_| we need to
// capture at least one frame. Once screen size is known it's stored in
// |frame_size_|.
bool waiting_for_frame_size_;
SkISize frame_size_;
SkBitmap resized_bitmap_;
// True between DoStart() and DoStop(). Can't just check |event_handler_|
// because |event_handler_| is used on the caller thread.
bool started_;
// True when we have delayed OnCaptureTimer() task posted on
// |task_runner_|.
bool capture_task_posted_;
// True when waiting for |screen_capturer_| to capture current frame.
bool capture_in_progress_;
DISALLOW_COPY_AND_ASSIGN(Core);
};
ScreenCaptureDevice::Core::Core(
scoped_refptr<base::SequencedTaskRunner> task_runner)
: task_runner_(task_runner),
event_handler_(NULL),
waiting_for_frame_size_(false),
started_(false),
capture_task_posted_(false),
capture_in_progress_(false) {
}
ScreenCaptureDevice::Core::~Core() {
}
void ScreenCaptureDevice::Core::Allocate(int width, int height,
int frame_rate,
EventHandler* event_handler) {
DCHECK_GT(width, 0);
DCHECK_GT(height, 0);
DCHECK_GT(frame_rate, 0);
{
base::AutoLock auto_lock(event_handler_lock_);
event_handler_ = event_handler;
}
task_runner_->PostTask(
FROM_HERE, base::Bind(&Core::DoAllocate, this, frame_rate));
}
void ScreenCaptureDevice::Core::Start() {
task_runner_->PostTask(
FROM_HERE, base::Bind(&Core::DoStart, this));
}
void ScreenCaptureDevice::Core::Stop() {
task_runner_->PostTask(
FROM_HERE, base::Bind(&Core::DoStop, this));
}
void ScreenCaptureDevice::Core::DeAllocate() {
{
base::AutoLock auto_lock(event_handler_lock_);
event_handler_ = NULL;
}
task_runner_->PostTask(FROM_HERE, base::Bind(&Core::DoDeAllocate, this));
}
void ScreenCaptureDevice::Core::OnCaptureCompleted(
scoped_refptr<ScreenCaptureData> capture_data) {
DCHECK(task_runner_->RunsTasksOnCurrentThread());
DCHECK(capture_in_progress_);
DCHECK(!capture_data->size().isEmpty());
capture_in_progress_ = false;
if (waiting_for_frame_size_) {
frame_size_ = capture_data->size();
waiting_for_frame_size_ = false;
// Inform the EventHandler of the video frame dimensions and format.
VideoCaptureCapability caps;
caps.width = frame_size_.width();
caps.height = frame_size_.height();
caps.frame_rate = frame_rate_;
caps.color = VideoCaptureCapability::kARGB;
caps.expected_capture_delay =
base::Time::kMillisecondsPerSecond / frame_rate_;
caps.interlaced = false;
base::AutoLock auto_lock(event_handler_lock_);
if (event_handler_)
event_handler_->OnFrameInfo(caps);
}
if (!started_)
return;
size_t buffer_size = frame_size_.width() * frame_size_.height() *
ScreenCaptureData::kBytesPerPixel;
if (capture_data->size() == frame_size_) {
// If the captured frame matches the requested size, we don't need to
// resize it.
resized_bitmap_.reset();
base::AutoLock auto_lock(event_handler_lock_);
if (event_handler_) {
event_handler_->OnIncomingCapturedFrame(
capture_data->data(), buffer_size, base::Time::Now());
}
return;
}
// In case screen size has changed we need to resize the image. Resized image
// is stored to |resized_bitmap_|. Only regions of the screen that are
// changing are copied.
SkRegion dirty_region = capture_data->dirty_region();
if (resized_bitmap_.width() != frame_size_.width() ||
resized_bitmap_.height() != frame_size_.height()) {
resized_bitmap_.setConfig(SkBitmap::kARGB_8888_Config,
frame_size_.width(), frame_size_.height());
resized_bitmap_.setIsOpaque(true);
resized_bitmap_.allocPixels();
dirty_region.setRect(SkIRect::MakeSize(frame_size_));
}
float scale_x = static_cast<float>(frame_size_.width()) /
capture_data->size().width();
float scale_y = static_cast<float>(frame_size_.height()) /
capture_data->size().height();
float scale;
float x, y;
// Center the image in case aspect ratio is different.
if (scale_x > scale_y) {
scale = scale_y;
x = (scale_x - scale_y) / scale * frame_size_.width() / 2.0;
y = 0.f;
} else {
scale = scale_x;
x = 0.f;
y = (scale_y - scale_x) / scale * frame_size_.height() / 2.0;
}
// Create skia device and canvas that draw to |resized_bitmap_|.
SkDevice device(resized_bitmap_);
SkCanvas canvas(&device);
canvas.scale(scale, scale);
int source_stride = capture_data->stride();
for (SkRegion::Iterator i(dirty_region); !i.done(); i.next()) {
SkBitmap source_bitmap;
source_bitmap.setConfig(SkBitmap::kARGB_8888_Config,
i.rect().width(), i.rect().height(),
source_stride);
source_bitmap.setIsOpaque(true);
source_bitmap.setPixels(
capture_data->data() + i.rect().top() * source_stride +
i.rect().left() * ScreenCaptureData::kBytesPerPixel);
canvas.drawBitmap(source_bitmap, i.rect().left() + x / scale,
i.rect().top() + y / scale, NULL);
}
base::AutoLock auto_lock(event_handler_lock_);
if (event_handler_) {
event_handler_->OnIncomingCapturedFrame(
reinterpret_cast<uint8*>(resized_bitmap_.getPixels()), buffer_size,
base::Time::Now());
}
}
void ScreenCaptureDevice::Core::OnCursorShapeChanged(
scoped_ptr<MouseCursorShape> cursor_shape) {
// TODO(sergeyu): Store mouse cursor shape and then render it to each captured
// frame. crbug.com/173265 .
DCHECK(task_runner_->RunsTasksOnCurrentThread());
}
void ScreenCaptureDevice::Core::DoAllocate(int frame_rate) {
DCHECK(task_runner_->RunsTasksOnCurrentThread());
frame_rate_ = frame_rate;
// Create and start frame capturer.
if (!screen_capturer_)
screen_capturer_ = ScreenCapturer::Create();
if (screen_capturer_)
screen_capturer_->Start(this);
// Capture first frame, so that we can call OnFrameInfo() callback.
waiting_for_frame_size_ = true;
DoCapture();
}
void ScreenCaptureDevice::Core::DoStart() {
DCHECK(task_runner_->RunsTasksOnCurrentThread());
started_ = true;
if (!capture_task_posted_) {
ScheduleCaptureTimer();
DoCapture();
}
}
void ScreenCaptureDevice::Core::DoStop() {
DCHECK(task_runner_->RunsTasksOnCurrentThread());
started_ = false;
resized_bitmap_.reset();
}
void ScreenCaptureDevice::Core::DoDeAllocate() {
DCHECK(task_runner_->RunsTasksOnCurrentThread());
DoStop();
if (screen_capturer_) {
screen_capturer_->Stop();
screen_capturer_.reset();
}
waiting_for_frame_size_ = false;
}
void ScreenCaptureDevice::Core::ScheduleCaptureTimer() {
DCHECK(!capture_task_posted_);
capture_task_posted_ = true;
task_runner_->PostDelayedTask(
FROM_HERE, base::Bind(&Core::OnCaptureTimer, this),
base::TimeDelta::FromSeconds(1) / frame_rate_);
}
void ScreenCaptureDevice::Core::OnCaptureTimer() {
DCHECK(capture_task_posted_);
capture_task_posted_ = false;
if (!started_)
return;
// Schedule a task for the next frame.
ScheduleCaptureTimer();
DoCapture();
}
void ScreenCaptureDevice::Core::DoCapture() {
DCHECK(!capture_in_progress_);
capture_in_progress_ = true;
screen_capturer_->CaptureFrame();
// Assume that ScreenCapturer always calls OnCaptureCompleted()
// callback before it returns.
//
// TODO(sergeyu): Fix ScreenCapturer to return video frame
// synchronously instead of using Delegate interface.
DCHECK(!capture_in_progress_);
}
ScreenCaptureDevice::ScreenCaptureDevice(
scoped_refptr<base::SequencedTaskRunner> task_runner)
: core_(new Core(task_runner)) {
name_.device_name = "Screen";
}
ScreenCaptureDevice::~ScreenCaptureDevice() {
DeAllocate();
}
void ScreenCaptureDevice::SetScreenCapturerForTest(
scoped_ptr<ScreenCapturer> capturer) {
core_->SetScreenCapturerForTest(capturer.Pass());
}
void ScreenCaptureDevice::Allocate(int width, int height,
int frame_rate,
EventHandler* event_handler) {
core_->Allocate(width, height, frame_rate, event_handler);
}
void ScreenCaptureDevice::Start() {
core_->Start();
}
void ScreenCaptureDevice::Stop() {
core_->Stop();
}
void ScreenCaptureDevice::DeAllocate() {
core_->DeAllocate();
}
const VideoCaptureDevice::Name& ScreenCaptureDevice::device_name() {
return name_;
}
} // namespace media
|