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 MODULES_DESKTOP_CAPTURE_FAKE_DESKTOP_CAPTURER_H_ 12 #define MODULES_DESKTOP_CAPTURE_FAKE_DESKTOP_CAPTURER_H_ 13 14 #include <memory> 15 16 #include "modules/desktop_capture/desktop_capturer.h" 17 #include "modules/desktop_capture/desktop_frame_generator.h" 18 #include "modules/desktop_capture/shared_memory.h" 19 #include "rtc_base/system/rtc_export.h" 20 21 namespace webrtc { 22 23 // A fake implementation of DesktopCapturer or its derived interfaces to 24 // generate DesktopFrame for testing purpose. 25 // 26 // Consumers can provide a FrameGenerator instance to generate instances of 27 // DesktopFrame to return for each Capture() function call. 28 // If no FrameGenerator provided, FakeDesktopCapturer will always return a 29 // nullptr DesktopFrame. 30 // 31 // Double buffering is guaranteed by the FrameGenerator. FrameGenerator 32 // implements in desktop_frame_generator.h guarantee double buffering, they 33 // creates a new instance of DesktopFrame each time. 34 class RTC_EXPORT FakeDesktopCapturer : public DesktopCapturer { 35 public: 36 FakeDesktopCapturer(); 37 ~FakeDesktopCapturer() override; 38 39 // Decides the result which will be returned in next Capture() callback. 40 void set_result(DesktopCapturer::Result result); 41 42 // Uses the `generator` provided as DesktopFrameGenerator, FakeDesktopCapturer 43 // does not take the ownership of `generator`. 44 void set_frame_generator(DesktopFrameGenerator* generator); 45 46 // Count of DesktopFrame(s) have been returned by this instance. This field 47 // would never be negative. 48 int num_frames_captured() const; 49 50 // Count of CaptureFrame() calls have been made. This field would never be 51 // negative. 52 int num_capture_attempts() const; 53 54 // DesktopCapturer interface 55 void Start(DesktopCapturer::Callback* callback) override; 56 void CaptureFrame() override; 57 void SetSharedMemoryFactory( 58 std::unique_ptr<SharedMemoryFactory> shared_memory_factory) override; 59 bool GetSourceList(DesktopCapturer::SourceList* sources) override; 60 bool SelectSource(DesktopCapturer::SourceId id) override; 61 62 private: 63 static constexpr DesktopCapturer::SourceId kWindowId = 1378277495; 64 static constexpr DesktopCapturer::SourceId kScreenId = 1378277496; 65 66 DesktopCapturer::Callback* callback_ = nullptr; 67 std::unique_ptr<SharedMemoryFactory> shared_memory_factory_; 68 DesktopCapturer::Result result_ = Result::SUCCESS; 69 DesktopFrameGenerator* generator_ = nullptr; 70 int num_frames_captured_ = 0; 71 int num_capture_attempts_ = 0; 72 }; 73 74 } // namespace webrtc 75 76 #endif // MODULES_DESKTOP_CAPTURE_FAKE_DESKTOP_CAPTURER_H_ 77