Remove INJECT_EVENTS permissions from test APKs.
[chromium-blink-merge.git] / media / capture / screen_capture_device_core.cc
blob3705eadab1338eb332550c27e892d5001df1bbaf
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/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(
20 scoped_ptr<VideoCaptureMachine> capture_machine) {
21 capture_machine.reset();
24 } // namespace
26 void ScreenCaptureDeviceCore::AllocateAndStart(
27 const VideoCaptureParams& params,
28 scoped_ptr<VideoCaptureDevice::Client> client) {
29 DCHECK(thread_checker_.CalledOnValidThread());
31 if (state_ != kIdle) {
32 DVLOG(1) << "Allocate() invoked when not in state Idle.";
33 return;
36 if (params.requested_format.frame_rate <= 0) {
37 std::string error_msg("Invalid frame_rate: ");
38 error_msg += base::DoubleToString(params.requested_format.frame_rate);
39 DVLOG(1) << error_msg;
40 client->OnError(error_msg);
41 return;
44 if (!(params.requested_format.pixel_format == PIXEL_FORMAT_I420 &&
45 params.requested_format.pixel_storage == PIXEL_STORAGE_CPU) &&
46 !(params.requested_format.pixel_format == PIXEL_FORMAT_ARGB &&
47 params.requested_format.pixel_storage == PIXEL_STORAGE_TEXTURE)) {
48 const std::string error_msg = base::StringPrintf(
49 "unsupported format: %s", params.requested_format.ToString().c_str());
50 DVLOG(1) << error_msg;
51 client->OnError(error_msg);
52 return;
55 if (params.requested_format.frame_size.IsEmpty()) {
56 const std::string error_msg =
57 "invalid frame size: " + params.requested_format.frame_size.ToString();
58 DVLOG(1) << error_msg;
59 client->OnError(error_msg);
60 return;
63 oracle_proxy_ = new ThreadSafeCaptureOracle(client.Pass(), params);
65 capture_machine_->Start(
66 oracle_proxy_,
67 params,
68 base::Bind(&ScreenCaptureDeviceCore::CaptureStarted, AsWeakPtr()));
70 TransitionStateTo(kCapturing);
73 void ScreenCaptureDeviceCore::StopAndDeAllocate() {
74 DCHECK(thread_checker_.CalledOnValidThread());
76 if (state_ != kCapturing)
77 return;
79 oracle_proxy_->Stop();
80 oracle_proxy_ = NULL;
82 TransitionStateTo(kIdle);
84 capture_machine_->Stop(base::Bind(&base::DoNothing));
87 void ScreenCaptureDeviceCore::CaptureStarted(bool success) {
88 DCHECK(thread_checker_.CalledOnValidThread());
89 if (!success) {
90 std::string reason("Failed to start capture machine.");
91 DVLOG(1) << reason;
92 Error(reason);
96 ScreenCaptureDeviceCore::ScreenCaptureDeviceCore(
97 scoped_ptr<VideoCaptureMachine> capture_machine)
98 : state_(kIdle),
99 capture_machine_(capture_machine.Pass()) {
100 DCHECK(capture_machine_.get());
103 ScreenCaptureDeviceCore::~ScreenCaptureDeviceCore() {
104 DCHECK(thread_checker_.CalledOnValidThread());
105 DCHECK_NE(state_, kCapturing);
106 if (capture_machine_) {
107 capture_machine_->Stop(base::Bind(&DeleteCaptureMachine,
108 base::Passed(&capture_machine_)));
110 DVLOG(1) << "ScreenCaptureDeviceCore@" << this << " destroying.";
113 void ScreenCaptureDeviceCore::TransitionStateTo(State next_state) {
114 DCHECK(thread_checker_.CalledOnValidThread());
116 #ifndef NDEBUG
117 static const char* kStateNames[] = {
118 "Idle", "Allocated", "Capturing", "Error"
120 DVLOG(1) << "State change: " << kStateNames[state_]
121 << " --> " << kStateNames[next_state];
122 #endif
124 state_ = next_state;
127 void ScreenCaptureDeviceCore::Error(const std::string& reason) {
128 DCHECK(thread_checker_.CalledOnValidThread());
130 if (state_ == kIdle)
131 return;
133 if (oracle_proxy_.get())
134 oracle_proxy_->ReportError(reason);
136 StopAndDeAllocate();
137 TransitionStateTo(kError);
140 } // namespace media