OLD | NEW |
(Empty) | |
| 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 #ifndef WEBRTC_MODULES_DESKTOP_CAPTURE_FAKE_DESKTOP_CAPTURER_H_ |
| 12 #define WEBRTC_MODULES_DESKTOP_CAPTURE_FAKE_DESKTOP_CAPTURER_H_ |
| 13 |
| 14 #include <memory> |
| 15 #include <utility> |
| 16 |
| 17 #include "webrtc/modules/desktop_capture/desktop_capturer.h" |
| 18 #include "webrtc/modules/desktop_capture/desktop_frame_generator.h" |
| 19 #include "webrtc/modules/desktop_capture/shared_desktop_frame.h" |
| 20 #include "webrtc/modules/desktop_capture/shared_memory.h" |
| 21 |
| 22 namespace webrtc { |
| 23 |
| 24 // A fake implementation of DesktopCapturer or its derived interfaces to |
| 25 // generate DesktopFrame for testing |
| 26 // purpose. |
| 27 // |
| 28 // Consumers can provide a FrameGenerator instance to generate instances of |
| 29 // DesktopFrame to return for each Capture() function call. |
| 30 // If no FrameGenerator provided, FakeDesktopCapturer will always return a |
| 31 // nullptr DesktopFrame. |
| 32 // |
| 33 // Double buffering is guaranteed by the FrameGenerator. FrameGenerator |
| 34 // implements in desktop_frame_generator.h guarantee double buffering, they |
| 35 // creates a |
| 36 // new instance of DesktopFrame each time. |
| 37 // |
| 38 // T must be DesktopCapturer or its derived interfaces. |
| 39 // |
| 40 // TODO(zijiehe): Remove template T once we merge ScreenCapturer and |
| 41 // WindowCapturer. |
| 42 template <typename T> |
| 43 class FakeDesktopCapturer : public T { |
| 44 public: |
| 45 FakeDesktopCapturer() |
| 46 : callback_(nullptr), |
| 47 result_(DesktopCapturer::Result::SUCCESS), |
| 48 generator_(nullptr) {} |
| 49 |
| 50 ~FakeDesktopCapturer() override {} |
| 51 |
| 52 // Decides the result which will be returned in next Capture() callback. |
| 53 void set_result(DesktopCapturer::Result result) { result_ = result; } |
| 54 |
| 55 // Uses the |generator| provided as DesktopFrameGenerator, FakeDesktopCapturer |
| 56 // does |
| 57 // not take the ownership of |generator|. |
| 58 void set_frame_generator(DesktopFrameGenerator* generator) { |
| 59 generator_ = generator; |
| 60 } |
| 61 |
| 62 // DesktopCapturer interface |
| 63 void Start(DesktopCapturer::Callback* callback) override { |
| 64 callback_ = callback; |
| 65 } |
| 66 |
| 67 void Capture(const DesktopRegion& region) override { |
| 68 if (generator_) { |
| 69 std::unique_ptr<DesktopFrame> frame( |
| 70 (*generator_)(shared_memory_factory_.get())); |
| 71 if (frame) { |
| 72 callback_->OnCaptureResult(result_, std::move(frame)); |
| 73 } else { |
| 74 callback_->OnCaptureResult(DesktopCapturer::Result::ERROR_TEMPORARY, |
| 75 nullptr); |
| 76 } |
| 77 return; |
| 78 } |
| 79 callback_->OnCaptureResult(DesktopCapturer::Result::ERROR_PERMANENT, |
| 80 nullptr); |
| 81 } |
| 82 |
| 83 void SetSharedMemoryFactory( |
| 84 std::unique_ptr<SharedMemoryFactory> shared_memory_factory) override { |
| 85 shared_memory_factory_ = std::move(shared_memory_factory); |
| 86 } |
| 87 |
| 88 private: |
| 89 DesktopCapturer::Callback* callback_; |
| 90 std::unique_ptr<SharedMemoryFactory> shared_memory_factory_; |
| 91 DesktopCapturer::Result result_; |
| 92 DesktopFrameGenerator* generator_; |
| 93 }; |
| 94 |
| 95 } // namespace webrtc |
| 96 |
| 97 #endif // WEBRTC_MODULES_DESKTOP_CAPTURE_FAKE_DESKTOP_CAPTURER_H_ |
OLD | NEW |