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"
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"
19 void DeleteCaptureMachine(scoped_ptr
<VideoCaptureMachine
> capture_machine
) {
20 capture_machine
.reset();
25 VideoCaptureMachine::VideoCaptureMachine() {
28 VideoCaptureMachine::~VideoCaptureMachine() {
31 bool VideoCaptureMachine::IsAutoThrottlingEnabled() const {
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.";
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
);
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
)
73 oracle_proxy_
->Stop();
76 TransitionStateTo(kIdle
);
78 capture_machine_
->Stop(base::Bind(&base::DoNothing
));
81 void ScreenCaptureDeviceCore::CaptureStarted(bool success
) {
82 DCHECK(thread_checker_
.CalledOnValidThread());
84 std::string
reason("Failed to start capture machine.");
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());
110 static const char* kStateNames
[] = {
111 "Idle", "Allocated", "Capturing", "Error"};
112 DVLOG(1) << "State change: " << kStateNames
[state_
] << " --> "
113 << kStateNames
[next_state
];
119 void ScreenCaptureDeviceCore::Error(const std::string
& reason
) {
120 DCHECK(thread_checker_
.CalledOnValidThread());
125 if (oracle_proxy_
.get())
126 oracle_proxy_
->ReportError(reason
);
129 TransitionStateTo(kError
);