Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / capture / content / screen_capture_device_core.cc
blobd99ca5a29581d8dbee55f2b8712edbb05bba6875
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/capture/content/screen_capture_device_core.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/threading/thread_checker.h"
15 namespace media {
17 namespace {
19 void DeleteCaptureMachine(scoped_ptr<VideoCaptureMachine> capture_machine) {
20 capture_machine.reset();
23 } // namespace
25 VideoCaptureMachine::VideoCaptureMachine() {
28 VideoCaptureMachine::~VideoCaptureMachine() {
31 bool VideoCaptureMachine::IsAutoThrottlingEnabled() const {
32 return false;
35 void ScreenCaptureDeviceCore::AllocateAndStart(
36 const VideoCaptureParams& params,
37 scoped_ptr<VideoCaptureDevice::Client> client) {
38 DCHECK(thread_checker_.CalledOnValidThread());
40 if (state_ != kIdle) {
41 DVLOG(1) << "Allocate() invoked when not in state Idle.";
42 return;
45 if (!(params.requested_format.pixel_format == PIXEL_FORMAT_I420 &&
46 params.requested_format.pixel_storage == PIXEL_STORAGE_CPU) &&
47 !(params.requested_format.pixel_format == PIXEL_FORMAT_ARGB &&
48 params.requested_format.pixel_storage == PIXEL_STORAGE_TEXTURE)) {
49 const std::string error_msg = base::StringPrintf(
50 "unsupported format: %s",
51 VideoCaptureFormat::ToString(params.requested_format).c_str());
52 DVLOG(1) << error_msg;
53 client->OnError(error_msg);
54 return;
57 oracle_proxy_ = new ThreadSafeCaptureOracle(
58 client.Pass(), params, capture_machine_->IsAutoThrottlingEnabled());
60 capture_machine_->Start(
61 oracle_proxy_, params,
62 base::Bind(&ScreenCaptureDeviceCore::CaptureStarted, AsWeakPtr()));
64 TransitionStateTo(kCapturing);
67 void ScreenCaptureDeviceCore::StopAndDeAllocate() {
68 DCHECK(thread_checker_.CalledOnValidThread());
70 if (state_ != kCapturing)
71 return;
73 oracle_proxy_->Stop();
74 oracle_proxy_ = NULL;
76 TransitionStateTo(kIdle);
78 capture_machine_->Stop(base::Bind(&base::DoNothing));
81 void ScreenCaptureDeviceCore::CaptureStarted(bool success) {
82 DCHECK(thread_checker_.CalledOnValidThread());
83 if (!success) {
84 std::string reason("Failed to start capture machine.");
85 DVLOG(1) << reason;
86 Error(reason);
90 ScreenCaptureDeviceCore::ScreenCaptureDeviceCore(
91 scoped_ptr<VideoCaptureMachine> capture_machine)
92 : state_(kIdle), capture_machine_(capture_machine.Pass()) {
93 DCHECK(capture_machine_.get());
96 ScreenCaptureDeviceCore::~ScreenCaptureDeviceCore() {
97 DCHECK(thread_checker_.CalledOnValidThread());
98 DCHECK_NE(state_, kCapturing);
99 if (capture_machine_) {
100 capture_machine_->Stop(
101 base::Bind(&DeleteCaptureMachine, base::Passed(&capture_machine_)));
103 DVLOG(1) << "ScreenCaptureDeviceCore@" << this << " destroying.";
106 void ScreenCaptureDeviceCore::TransitionStateTo(State next_state) {
107 DCHECK(thread_checker_.CalledOnValidThread());
109 #ifndef NDEBUG
110 static const char* kStateNames[] = {
111 "Idle", "Allocated", "Capturing", "Error"};
112 DVLOG(1) << "State change: " << kStateNames[state_] << " --> "
113 << kStateNames[next_state];
114 #endif
116 state_ = next_state;
119 void ScreenCaptureDeviceCore::Error(const std::string& reason) {
120 DCHECK(thread_checker_.CalledOnValidThread());
122 if (state_ == kIdle)
123 return;
125 if (oracle_proxy_.get())
126 oracle_proxy_->ReportError(reason);
128 StopAndDeAllocate();
129 TransitionStateTo(kError);
132 } // namespace media