zijiehe | 99d036c | 2016-11-11 05:57:10 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "webrtc/modules/desktop_capture/fake_desktop_capturer.h" |
| 12 | |
| 13 | #include <utility> |
| 14 | |
| 15 | namespace webrtc { |
| 16 | |
zijiehe | 062a159 | 2017-02-17 22:32:04 | [diff] [blame] | 17 | FakeDesktopCapturer::FakeDesktopCapturer() = default; |
zijiehe | 99d036c | 2016-11-11 05:57:10 | [diff] [blame] | 18 | FakeDesktopCapturer::~FakeDesktopCapturer() = default; |
| 19 | |
| 20 | void FakeDesktopCapturer::set_result(DesktopCapturer::Result result) { |
| 21 | result_ = result; |
| 22 | } |
| 23 | |
zijiehe | 062a159 | 2017-02-17 22:32:04 | [diff] [blame] | 24 | int FakeDesktopCapturer::num_frames_captured() const { |
| 25 | return num_frames_captured_; |
| 26 | } |
| 27 | |
| 28 | int FakeDesktopCapturer::num_capture_attempts() const { |
| 29 | return num_capture_attempts_; |
| 30 | } |
| 31 | |
zijiehe | 99d036c | 2016-11-11 05:57:10 | [diff] [blame] | 32 | // Uses the |generator| provided as DesktopFrameGenerator, FakeDesktopCapturer |
| 33 | // does |
| 34 | // not take the ownership of |generator|. |
| 35 | void FakeDesktopCapturer::set_frame_generator( |
| 36 | DesktopFrameGenerator* generator) { |
| 37 | generator_ = generator; |
| 38 | } |
| 39 | |
| 40 | void FakeDesktopCapturer::Start(DesktopCapturer::Callback* callback) { |
| 41 | callback_ = callback; |
| 42 | } |
| 43 | |
| 44 | void FakeDesktopCapturer::CaptureFrame() { |
zijiehe | 062a159 | 2017-02-17 22:32:04 | [diff] [blame] | 45 | num_capture_attempts_++; |
zijiehe | 99d036c | 2016-11-11 05:57:10 | [diff] [blame] | 46 | if (generator_) { |
zijiehe | 062a159 | 2017-02-17 22:32:04 | [diff] [blame] | 47 | if (result_ != DesktopCapturer::Result::SUCCESS) { |
| 48 | callback_->OnCaptureResult(result_, nullptr); |
| 49 | return; |
| 50 | } |
| 51 | |
zijiehe | 99d036c | 2016-11-11 05:57:10 | [diff] [blame] | 52 | std::unique_ptr<DesktopFrame> frame( |
| 53 | generator_->GetNextFrame(shared_memory_factory_.get())); |
| 54 | if (frame) { |
zijiehe | 062a159 | 2017-02-17 22:32:04 | [diff] [blame] | 55 | num_frames_captured_++; |
zijiehe | 99d036c | 2016-11-11 05:57:10 | [diff] [blame] | 56 | callback_->OnCaptureResult(result_, std::move(frame)); |
| 57 | } else { |
| 58 | callback_->OnCaptureResult(DesktopCapturer::Result::ERROR_TEMPORARY, |
| 59 | nullptr); |
| 60 | } |
| 61 | return; |
| 62 | } |
| 63 | callback_->OnCaptureResult(DesktopCapturer::Result::ERROR_PERMANENT, nullptr); |
| 64 | } |
| 65 | |
| 66 | void FakeDesktopCapturer::SetSharedMemoryFactory( |
| 67 | std::unique_ptr<SharedMemoryFactory> shared_memory_factory) { |
| 68 | shared_memory_factory_ = std::move(shared_memory_factory); |
| 69 | } |
| 70 | |
| 71 | bool FakeDesktopCapturer::GetSourceList(DesktopCapturer::SourceList* sources) { |
| 72 | sources->push_back({kWindowId, "A-Fake-DesktopCapturer-Window"}); |
| 73 | sources->push_back({kScreenId}); |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | bool FakeDesktopCapturer::SelectSource(DesktopCapturer::SourceId id) { |
| 78 | return id == kWindowId || id == kScreenId || id == kFullDesktopScreenId; |
| 79 | } |
| 80 | |
| 81 | } // namespace webrtc |