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"
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(
20 scoped_ptr
<VideoCaptureMachine
> capture_machine
) {
21 capture_machine
.reset();
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.";
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
);
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
);
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
);
63 oracle_proxy_
= new ThreadSafeCaptureOracle(client
.Pass(), params
);
65 capture_machine_
->Start(
68 base::Bind(&ScreenCaptureDeviceCore::CaptureStarted
, AsWeakPtr()));
70 TransitionStateTo(kCapturing
);
73 void ScreenCaptureDeviceCore::StopAndDeAllocate() {
74 DCHECK(thread_checker_
.CalledOnValidThread());
76 if (state_
!= kCapturing
)
79 oracle_proxy_
->Stop();
82 TransitionStateTo(kIdle
);
84 capture_machine_
->Stop(base::Bind(&base::DoNothing
));
87 void ScreenCaptureDeviceCore::CaptureStarted(bool success
) {
88 DCHECK(thread_checker_
.CalledOnValidThread());
90 std::string
reason("Failed to start capture machine.");
96 ScreenCaptureDeviceCore::ScreenCaptureDeviceCore(
97 scoped_ptr
<VideoCaptureMachine
> capture_machine
)
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());
117 static const char* kStateNames
[] = {
118 "Idle", "Allocated", "Capturing", "Error"
120 DVLOG(1) << "State change: " << kStateNames
[state_
]
121 << " --> " << kStateNames
[next_state
];
127 void ScreenCaptureDeviceCore::Error(const std::string
& reason
) {
128 DCHECK(thread_checker_
.CalledOnValidThread());
133 if (oracle_proxy_
.get())
134 oracle_proxy_
->ReportError(reason
);
137 TransitionStateTo(kError
);