Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / capture / content / screen_capture_device_core.cc
blob53dd3818ac805491bfb2a764e41e592b447223dc
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 ==
46 VIDEO_CAPTURE_PIXEL_FORMAT_I420 &&
47 params.requested_format.pixel_storage == PIXEL_STORAGE_CPU) &&
48 !(params.requested_format.pixel_format ==
49 VIDEO_CAPTURE_PIXEL_FORMAT_ARGB &&
50 params.requested_format.pixel_storage == PIXEL_STORAGE_TEXTURE)) {
51 const std::string error_msg = base::StringPrintf(
52 "unsupported format: %s",
53 VideoCaptureFormat::ToString(params.requested_format).c_str());
54 DVLOG(1) << error_msg;
55 client->OnError(error_msg);
56 return;
59 oracle_proxy_ = new ThreadSafeCaptureOracle(
60 client.Pass(), params, capture_machine_->IsAutoThrottlingEnabled());
62 capture_machine_->Start(
63 oracle_proxy_, params,
64 base::Bind(&ScreenCaptureDeviceCore::CaptureStarted, AsWeakPtr()));
66 TransitionStateTo(kCapturing);
69 void ScreenCaptureDeviceCore::StopAndDeAllocate() {
70 DCHECK(thread_checker_.CalledOnValidThread());
72 if (state_ != kCapturing)
73 return;
75 oracle_proxy_->Stop();
76 oracle_proxy_ = NULL;
78 TransitionStateTo(kIdle);
80 capture_machine_->Stop(base::Bind(&base::DoNothing));
83 void ScreenCaptureDeviceCore::CaptureStarted(bool success) {
84 DCHECK(thread_checker_.CalledOnValidThread());
85 if (!success) {
86 std::string reason("Failed to start capture machine.");
87 DVLOG(1) << reason;
88 Error(reason);
92 ScreenCaptureDeviceCore::ScreenCaptureDeviceCore(
93 scoped_ptr<VideoCaptureMachine> capture_machine)
94 : state_(kIdle), capture_machine_(capture_machine.Pass()) {
95 DCHECK(capture_machine_.get());
98 ScreenCaptureDeviceCore::~ScreenCaptureDeviceCore() {
99 DCHECK(thread_checker_.CalledOnValidThread());
100 DCHECK_NE(state_, kCapturing);
101 if (capture_machine_) {
102 capture_machine_->Stop(
103 base::Bind(&DeleteCaptureMachine, base::Passed(&capture_machine_)));
105 DVLOG(1) << "ScreenCaptureDeviceCore@" << this << " destroying.";
108 void ScreenCaptureDeviceCore::TransitionStateTo(State next_state) {
109 DCHECK(thread_checker_.CalledOnValidThread());
111 #ifndef NDEBUG
112 static const char* kStateNames[] = {
113 "Idle", "Allocated", "Capturing", "Error"};
114 DVLOG(1) << "State change: " << kStateNames[state_] << " --> "
115 << kStateNames[next_state];
116 #endif
118 state_ = next_state;
121 void ScreenCaptureDeviceCore::Error(const std::string& reason) {
122 DCHECK(thread_checker_.CalledOnValidThread());
124 if (state_ == kIdle)
125 return;
127 if (oracle_proxy_.get())
128 oracle_proxy_->ReportError(reason);
130 StopAndDeAllocate();
131 TransitionStateTo(kError);
134 } // namespace media