| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "media/capture/content/screen_capture_device_core.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/memory/weak_ptr.h" | |
| 14 #include "base/strings/string_number_conversions.h" | |
| 15 #include "base/strings/stringprintf.h" | |
| 16 #include "base/threading/thread_checker.h" | |
| 17 | |
| 18 namespace media { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 void DeleteCaptureMachine( | |
| 23 std::unique_ptr<VideoCaptureMachine> capture_machine) { | |
| 24 capture_machine.reset(); | |
| 25 } | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 29 VideoCaptureMachine::VideoCaptureMachine() { | |
| 30 } | |
| 31 | |
| 32 VideoCaptureMachine::~VideoCaptureMachine() { | |
| 33 } | |
| 34 | |
| 35 bool VideoCaptureMachine::IsAutoThrottlingEnabled() const { | |
| 36 return false; | |
| 37 } | |
| 38 | |
| 39 void ScreenCaptureDeviceCore::AllocateAndStart( | |
| 40 const VideoCaptureParams& params, | |
| 41 std::unique_ptr<VideoCaptureDevice::Client> client) { | |
| 42 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 43 | |
| 44 if (state_ != kIdle) { | |
| 45 DVLOG(1) << "Allocate() invoked when not in state Idle."; | |
| 46 return; | |
| 47 } | |
| 48 | |
| 49 if (params.requested_format.pixel_format != PIXEL_FORMAT_I420 || | |
| 50 params.requested_format.pixel_storage != PIXEL_STORAGE_CPU) { | |
| 51 client->OnError( | |
| 52 FROM_HERE, | |
| 53 base::StringPrintf( | |
| 54 "unsupported format: %s", | |
| 55 VideoCaptureFormat::ToString(params.requested_format).c_str())); | |
| 56 return; | |
| 57 } | |
| 58 | |
| 59 oracle_proxy_ = new ThreadSafeCaptureOracle( | |
| 60 std::move(client), params, capture_machine_->IsAutoThrottlingEnabled()); | |
| 61 | |
| 62 capture_machine_->Start( | |
| 63 oracle_proxy_, params, | |
| 64 base::Bind(&ScreenCaptureDeviceCore::CaptureStarted, AsWeakPtr())); | |
| 65 | |
| 66 TransitionStateTo(kCapturing); | |
| 67 } | |
| 68 | |
| 69 void ScreenCaptureDeviceCore::RequestRefreshFrame() { | |
| 70 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 71 | |
| 72 if (state_ != kCapturing) | |
| 73 return; | |
| 74 | |
| 75 if (oracle_proxy_->AttemptPassiveRefresh()) | |
| 76 return; | |
| 77 capture_machine_->MaybeCaptureForRefresh(); | |
| 78 } | |
| 79 | |
| 80 void ScreenCaptureDeviceCore::StopAndDeAllocate() { | |
| 81 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 82 | |
| 83 if (state_ != kCapturing) | |
| 84 return; | |
| 85 | |
| 86 oracle_proxy_->Stop(); | |
| 87 oracle_proxy_ = NULL; | |
| 88 | |
| 89 TransitionStateTo(kIdle); | |
| 90 | |
| 91 capture_machine_->Stop(base::Bind(&base::DoNothing)); | |
| 92 } | |
| 93 | |
| 94 void ScreenCaptureDeviceCore::CaptureStarted(bool success) { | |
| 95 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 96 if (!success) | |
| 97 Error(FROM_HERE, "Failed to start capture machine."); | |
| 98 } | |
| 99 | |
| 100 ScreenCaptureDeviceCore::ScreenCaptureDeviceCore( | |
| 101 std::unique_ptr<VideoCaptureMachine> capture_machine) | |
| 102 : state_(kIdle), capture_machine_(std::move(capture_machine)) { | |
| 103 DCHECK(capture_machine_.get()); | |
| 104 } | |
| 105 | |
| 106 ScreenCaptureDeviceCore::~ScreenCaptureDeviceCore() { | |
| 107 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 108 DCHECK_NE(state_, kCapturing); | |
| 109 if (capture_machine_) { | |
| 110 capture_machine_->Stop( | |
| 111 base::Bind(&DeleteCaptureMachine, base::Passed(&capture_machine_))); | |
| 112 } | |
| 113 DVLOG(1) << "ScreenCaptureDeviceCore@" << this << " destroying."; | |
| 114 } | |
| 115 | |
| 116 void ScreenCaptureDeviceCore::TransitionStateTo(State next_state) { | |
| 117 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 118 | |
| 119 #ifndef NDEBUG | |
| 120 static const char* kStateNames[] = {"Idle", "Capturing", "Error"}; | |
| 121 static_assert(arraysize(kStateNames) == kLastCaptureState, | |
| 122 "Different number of states and textual descriptions"); | |
| 123 DVLOG(1) << "State change: " << kStateNames[state_] << " --> " | |
| 124 << kStateNames[next_state]; | |
| 125 #endif | |
| 126 | |
| 127 state_ = next_state; | |
| 128 } | |
| 129 | |
| 130 void ScreenCaptureDeviceCore::Error(const tracked_objects::Location& from_here, | |
| 131 const std::string& reason) { | |
| 132 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 133 | |
| 134 if (state_ == kIdle) | |
| 135 return; | |
| 136 | |
| 137 if (oracle_proxy_.get()) | |
| 138 oracle_proxy_->ReportError(from_here, reason); | |
| 139 | |
| 140 StopAndDeAllocate(); | |
| 141 TransitionStateTo(kError); | |
| 142 } | |
| 143 | |
| 144 } // namespace media | |
| OLD | NEW |