This source file includes following definitions.
- ComputeLetterboxRect
- AllocateAndStart
- StopAndDeAllocate
- SetNotificationWindowId
- CreateSharedMemory
- OnCaptureCompleted
- DoAllocateAndStart
- DoStopAndDeAllocate
- RefreshCaptureFormat
- OnCaptureTimer
- CaptureFrameAndScheduleNext
- DoCapture
- DoSetNotificationWindowId
- Create
- AllocateAndStart
- StopAndDeAllocate
- SetNotificationWindowId
#include "content/browser/media/capture/desktop_capture_device.h"
#include "base/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/sequenced_task_runner.h"
#include "base/strings/string_number_conversions.h"
#include "base/synchronization/lock.h"
#include "base/threading/sequenced_worker_pool.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/desktop_media_id.h"
#include "media/base/video_util.h"
#include "third_party/libyuv/include/libyuv/scale_argb.h"
#include "third_party/webrtc/modules/desktop_capture/desktop_and_cursor_composer.h"
#include "third_party/webrtc/modules/desktop_capture/desktop_capture_options.h"
#include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h"
#include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
#include "third_party/webrtc/modules/desktop_capture/mouse_cursor_monitor.h"
#include "third_party/webrtc/modules/desktop_capture/screen_capturer.h"
#include "third_party/webrtc/modules/desktop_capture/window_capturer.h"
namespace content {
namespace {
const int kMaximumCpuConsumptionPercentage = 50;
webrtc::DesktopRect ComputeLetterboxRect(
const webrtc::DesktopSize& max_size,
const webrtc::DesktopSize& source_size) {
gfx::Rect result = media::ComputeLetterboxRegion(
gfx::Rect(0, 0, max_size.width(), max_size.height()),
gfx::Size(source_size.width(), source_size.height()));
return webrtc::DesktopRect::MakeLTRB(
result.x(), result.y(), result.right(), result.bottom());
}
}
class DesktopCaptureDevice::Core
: public base::RefCountedThreadSafe<Core>,
public webrtc::DesktopCapturer::Callback {
public:
Core(scoped_refptr<base::SequencedTaskRunner> task_runner,
scoped_ptr<webrtc::DesktopCapturer> capturer);
void AllocateAndStart(const media::VideoCaptureParams& params,
scoped_ptr<Client> client);
void StopAndDeAllocate();
void SetNotificationWindowId(gfx::NativeViewId window_id);
private:
friend class base::RefCountedThreadSafe<Core>;
virtual ~Core();
virtual webrtc::SharedMemory* CreateSharedMemory(size_t size) OVERRIDE;
virtual void OnCaptureCompleted(webrtc::DesktopFrame* frame) OVERRIDE;
void DoAllocateAndStart(const media::VideoCaptureParams& params,
scoped_ptr<Client> client);
void DoStopAndDeAllocate();
void RefreshCaptureFormat(const webrtc::DesktopSize& frame_size);
void OnCaptureTimer();
void CaptureFrameAndScheduleNext();
void DoCapture();
void DoSetNotificationWindowId(gfx::NativeViewId window_id);
scoped_refptr<base::SequencedTaskRunner> task_runner_;
scoped_ptr<webrtc::DesktopCapturer> desktop_capturer_;
scoped_ptr<Client> client_;
media::VideoCaptureParams requested_params_;
media::VideoCaptureFormat capture_format_;
webrtc::DesktopSize previous_frame_size_;
scoped_ptr<webrtc::DesktopFrame> output_frame_;
webrtc::DesktopRect output_rect_;
bool capture_task_posted_;
bool capture_in_progress_;
DISALLOW_COPY_AND_ASSIGN(Core);
};
DesktopCaptureDevice::Core::Core(
scoped_refptr<base::SequencedTaskRunner> task_runner,
scoped_ptr<webrtc::DesktopCapturer> capturer)
: task_runner_(task_runner),
desktop_capturer_(capturer.Pass()),
capture_task_posted_(false),
capture_in_progress_(false) {}
DesktopCaptureDevice::Core::~Core() {
}
void DesktopCaptureDevice::Core::AllocateAndStart(
const media::VideoCaptureParams& params,
scoped_ptr<Client> client) {
DCHECK_GT(params.requested_format.frame_size.GetArea(), 0);
DCHECK_GT(params.requested_format.frame_rate, 0);
task_runner_->PostTask(
FROM_HERE,
base::Bind(
&Core::DoAllocateAndStart, this, params, base::Passed(&client)));
}
void DesktopCaptureDevice::Core::StopAndDeAllocate() {
task_runner_->PostTask(FROM_HERE,
base::Bind(&Core::DoStopAndDeAllocate, this));
}
void DesktopCaptureDevice::Core::SetNotificationWindowId(
gfx::NativeViewId window_id) {
task_runner_->PostTask(
FROM_HERE, base::Bind(&Core::DoSetNotificationWindowId, this, window_id));
}
webrtc::SharedMemory*
DesktopCaptureDevice::Core::CreateSharedMemory(size_t size) {
return NULL;
}
void DesktopCaptureDevice::Core::OnCaptureCompleted(
webrtc::DesktopFrame* frame) {
DCHECK(task_runner_->RunsTasksOnCurrentThread());
DCHECK(capture_in_progress_);
capture_in_progress_ = false;
if (!frame) {
std::string log("Failed to capture a frame.");
LOG(ERROR) << log;
client_->OnError(log);
return;
}
if (!client_)
return;
scoped_ptr<webrtc::DesktopFrame> owned_frame(frame);
RefreshCaptureFormat(frame->size());
webrtc::DesktopSize output_size(capture_format_.frame_size.width(),
capture_format_.frame_size.height());
size_t output_bytes = output_size.width() * output_size.height() *
webrtc::DesktopFrame::kBytesPerPixel;
const uint8_t* output_data = NULL;
scoped_ptr<uint8_t[]> flipped_frame_buffer;
if (frame->size().equals(output_size)) {
output_data = frame->data();
if (frame->stride() < 0) {
int height = frame->size().height();
int bytes_per_row =
frame->size().width() * webrtc::DesktopFrame::kBytesPerPixel;
flipped_frame_buffer.reset(new uint8_t[output_bytes]);
uint8_t* dest = flipped_frame_buffer.get();
for (int row = 0; row < height; ++row) {
memcpy(dest, output_data, bytes_per_row);
dest += bytes_per_row;
output_data += frame->stride();
}
output_data = flipped_frame_buffer.get();
}
} else {
if (!output_frame_) {
output_frame_.reset(new webrtc::BasicDesktopFrame(output_size));
memset(output_frame_->data(), 0, output_bytes);
}
DCHECK(output_frame_->size().equals(output_size));
uint8_t* output_rect_data = output_frame_->data() +
output_frame_->stride() * output_rect_.top() +
webrtc::DesktopFrame::kBytesPerPixel * output_rect_.left();
libyuv::ARGBScale(frame->data(), frame->stride(),
frame->size().width(), frame->size().height(),
output_rect_data, output_frame_->stride(),
output_rect_.width(), output_rect_.height(),
libyuv::kFilterBilinear);
output_data = output_frame_->data();
}
client_->OnIncomingCapturedData(
output_data, output_bytes, capture_format_, 0, base::TimeTicks::Now());
}
void DesktopCaptureDevice::Core::DoAllocateAndStart(
const media::VideoCaptureParams& params,
scoped_ptr<Client> client) {
DCHECK(task_runner_->RunsTasksOnCurrentThread());
DCHECK(desktop_capturer_);
DCHECK(client.get());
DCHECK(!client_.get());
client_ = client.Pass();
requested_params_ = params;
capture_format_ = requested_params_.requested_format;
capture_format_.pixel_format = media::PIXEL_FORMAT_ARGB;
desktop_capturer_->Start(this);
CaptureFrameAndScheduleNext();
}
void DesktopCaptureDevice::Core::DoStopAndDeAllocate() {
DCHECK(task_runner_->RunsTasksOnCurrentThread());
client_.reset();
output_frame_.reset();
previous_frame_size_.set(0, 0);
desktop_capturer_.reset();
}
void DesktopCaptureDevice::Core::RefreshCaptureFormat(
const webrtc::DesktopSize& frame_size) {
if (previous_frame_size_.equals(frame_size))
return;
output_frame_.reset();
if (previous_frame_size_.is_empty() ||
requested_params_.allow_resolution_change) {
if (frame_size.width() >
requested_params_.requested_format.frame_size.width() ||
frame_size.height() >
requested_params_.requested_format.frame_size.height()) {
output_rect_ = ComputeLetterboxRect(
webrtc::DesktopSize(
requested_params_.requested_format.frame_size.width(),
requested_params_.requested_format.frame_size.height()),
frame_size);
output_rect_.Translate(-output_rect_.left(), -output_rect_.top());
} else {
output_rect_ = webrtc::DesktopRect::MakeSize(frame_size);
}
capture_format_.frame_size.SetSize(output_rect_.width(),
output_rect_.height());
} else {
output_rect_ = ComputeLetterboxRect(
webrtc::DesktopSize(capture_format_.frame_size.width(),
capture_format_.frame_size.height()),
frame_size);
}
previous_frame_size_ = frame_size;
}
void DesktopCaptureDevice::Core::OnCaptureTimer() {
DCHECK(capture_task_posted_);
capture_task_posted_ = false;
if (!client_)
return;
CaptureFrameAndScheduleNext();
}
void DesktopCaptureDevice::Core::CaptureFrameAndScheduleNext() {
DCHECK(task_runner_->RunsTasksOnCurrentThread());
DCHECK(!capture_task_posted_);
base::TimeTicks started_time = base::TimeTicks::Now();
DoCapture();
base::TimeDelta last_capture_duration = base::TimeTicks::Now() - started_time;
base::TimeDelta capture_period = std::max(
(last_capture_duration * 100) / kMaximumCpuConsumptionPercentage,
base::TimeDelta::FromSeconds(1) / capture_format_.frame_rate);
capture_task_posted_ = true;
task_runner_->PostDelayedTask(
FROM_HERE, base::Bind(&Core::OnCaptureTimer, this),
capture_period - last_capture_duration);
}
void DesktopCaptureDevice::Core::DoCapture() {
DCHECK(task_runner_->RunsTasksOnCurrentThread());
DCHECK(!capture_in_progress_);
capture_in_progress_ = true;
desktop_capturer_->Capture(webrtc::DesktopRegion());
DCHECK(!capture_in_progress_);
}
void DesktopCaptureDevice::Core::DoSetNotificationWindowId(
gfx::NativeViewId window_id) {
DCHECK(task_runner_->RunsTasksOnCurrentThread());
DCHECK(window_id);
desktop_capturer_->SetExcludedWindow(window_id);
}
scoped_ptr<media::VideoCaptureDevice> DesktopCaptureDevice::Create(
const DesktopMediaID& source) {
scoped_refptr<base::SequencedWorkerPool> blocking_pool =
BrowserThread::GetBlockingPool();
scoped_refptr<base::SequencedTaskRunner> task_runner =
blocking_pool->GetSequencedTaskRunner(
blocking_pool->GetSequenceToken());
webrtc::DesktopCaptureOptions options =
webrtc::DesktopCaptureOptions::CreateDefault();
options.set_disable_effects(false);
scoped_ptr<webrtc::DesktopCapturer> capturer;
switch (source.type) {
case DesktopMediaID::TYPE_SCREEN: {
scoped_ptr<webrtc::ScreenCapturer> screen_capturer;
screen_capturer.reset(webrtc::ScreenCapturer::Create(options));
if (screen_capturer && screen_capturer->SelectScreen(source.id)) {
capturer.reset(new webrtc::DesktopAndCursorComposer(
screen_capturer.release(),
webrtc::MouseCursorMonitor::CreateForScreen(options, source.id)));
}
break;
}
case DesktopMediaID::TYPE_WINDOW: {
scoped_ptr<webrtc::WindowCapturer> window_capturer(
webrtc::WindowCapturer::Create(options));
if (window_capturer && window_capturer->SelectWindow(source.id)) {
window_capturer->BringSelectedWindowToFront();
capturer.reset(new webrtc::DesktopAndCursorComposer(
window_capturer.release(),
webrtc::MouseCursorMonitor::CreateForWindow(options, source.id)));
}
break;
}
default: {
NOTREACHED();
}
}
scoped_ptr<media::VideoCaptureDevice> result;
if (capturer)
result.reset(new DesktopCaptureDevice(task_runner, capturer.Pass()));
return result.Pass();
}
DesktopCaptureDevice::DesktopCaptureDevice(
scoped_refptr<base::SequencedTaskRunner> task_runner,
scoped_ptr<webrtc::DesktopCapturer> capturer)
: core_(new Core(task_runner, capturer.Pass())) {}
DesktopCaptureDevice::~DesktopCaptureDevice() {
StopAndDeAllocate();
}
void DesktopCaptureDevice::AllocateAndStart(
const media::VideoCaptureParams& params,
scoped_ptr<Client> client) {
core_->AllocateAndStart(params, client.Pass());
}
void DesktopCaptureDevice::StopAndDeAllocate() {
core_->StopAndDeAllocate();
}
void DesktopCaptureDevice::SetNotificationWindowId(
gfx::NativeViewId window_id) {
core_->SetNotificationWindowId(window_id);
}
}