OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_MODULES_DESKTOP_CAPTURE_FAKE_DESKTOP_CAPTURER_H_ | 11 #ifndef WEBRTC_MODULES_DESKTOP_CAPTURE_FAKE_DESKTOP_CAPTURER_H_ |
12 #define WEBRTC_MODULES_DESKTOP_CAPTURE_FAKE_DESKTOP_CAPTURER_H_ | 12 #define WEBRTC_MODULES_DESKTOP_CAPTURE_FAKE_DESKTOP_CAPTURER_H_ |
13 | 13 |
14 #include <memory> | 14 #include <memory> |
15 #include <utility> | |
16 | 15 |
17 #include "webrtc/modules/desktop_capture/desktop_capturer.h" | 16 #include "webrtc/modules/desktop_capture/desktop_capturer.h" |
18 #include "webrtc/modules/desktop_capture/desktop_capture_types.h" | 17 #include "webrtc/modules/desktop_capture/desktop_capture_types.h" |
19 #include "webrtc/modules/desktop_capture/desktop_frame_generator.h" | 18 #include "webrtc/modules/desktop_capture/desktop_frame_generator.h" |
20 #include "webrtc/modules/desktop_capture/shared_desktop_frame.h" | |
21 #include "webrtc/modules/desktop_capture/shared_memory.h" | 19 #include "webrtc/modules/desktop_capture/shared_memory.h" |
22 | 20 |
23 namespace webrtc { | 21 namespace webrtc { |
24 | 22 |
25 // A fake implementation of DesktopCapturer or its derived interfaces to | 23 // A fake implementation of DesktopCapturer or its derived interfaces to |
26 // generate DesktopFrame for testing purpose. | 24 // generate DesktopFrame for testing purpose. |
27 // | 25 // |
28 // Consumers can provide a FrameGenerator instance to generate instances of | 26 // Consumers can provide a FrameGenerator instance to generate instances of |
29 // DesktopFrame to return for each Capture() function call. | 27 // DesktopFrame to return for each Capture() function call. |
30 // If no FrameGenerator provided, FakeDesktopCapturer will always return a | 28 // If no FrameGenerator provided, FakeDesktopCapturer will always return a |
31 // nullptr DesktopFrame. | 29 // nullptr DesktopFrame. |
32 // | 30 // |
33 // Double buffering is guaranteed by the FrameGenerator. FrameGenerator | 31 // Double buffering is guaranteed by the FrameGenerator. FrameGenerator |
34 // implements in desktop_frame_generator.h guarantee double buffering, they | 32 // implements in desktop_frame_generator.h guarantee double buffering, they |
35 // creates a new instance of DesktopFrame each time. | 33 // creates a new instance of DesktopFrame each time. |
36 // | 34 class FakeDesktopCapturer : public DesktopCapturer { |
37 // T must be DesktopCapturer or its derived interfaces. | |
38 // | |
39 // TODO(zijiehe): Remove template T once we merge ScreenCapturer and | |
40 // WindowCapturer. | |
41 template <typename T = DesktopCapturer> | |
42 class FakeDesktopCapturer : public T { | |
43 public: | 35 public: |
44 FakeDesktopCapturer() | 36 FakeDesktopCapturer(); |
45 : callback_(nullptr), | 37 ~FakeDesktopCapturer() override; |
46 result_(DesktopCapturer::Result::SUCCESS), | |
47 generator_(nullptr) {} | |
48 | |
49 ~FakeDesktopCapturer() override {} | |
50 | 38 |
51 // Decides the result which will be returned in next Capture() callback. | 39 // Decides the result which will be returned in next Capture() callback. |
52 void set_result(DesktopCapturer::Result result) { result_ = result; } | 40 void set_result(DesktopCapturer::Result result); |
53 | 41 |
54 // Uses the |generator| provided as DesktopFrameGenerator, FakeDesktopCapturer | 42 // Uses the |generator| provided as DesktopFrameGenerator, FakeDesktopCapturer |
55 // does | 43 // does not take the ownership of |generator|. |
56 // not take the ownership of |generator|. | 44 void set_frame_generator(DesktopFrameGenerator* generator); |
57 void set_frame_generator(DesktopFrameGenerator* generator) { | |
58 generator_ = generator; | |
59 } | |
60 | 45 |
61 // DesktopCapturer interface | 46 // DesktopCapturer interface |
62 void Start(DesktopCapturer::Callback* callback) override { | 47 void Start(DesktopCapturer::Callback* callback) override; |
63 callback_ = callback; | 48 void CaptureFrame() override; |
64 } | |
65 | |
66 void CaptureFrame() override { | |
67 if (generator_) { | |
68 std::unique_ptr<DesktopFrame> frame( | |
69 generator_->GetNextFrame(shared_memory_factory_.get())); | |
70 if (frame) { | |
71 callback_->OnCaptureResult(result_, std::move(frame)); | |
72 } else { | |
73 callback_->OnCaptureResult(DesktopCapturer::Result::ERROR_TEMPORARY, | |
74 nullptr); | |
75 } | |
76 return; | |
77 } | |
78 callback_->OnCaptureResult(DesktopCapturer::Result::ERROR_PERMANENT, | |
79 nullptr); | |
80 } | |
81 | |
82 void SetSharedMemoryFactory( | 49 void SetSharedMemoryFactory( |
83 std::unique_ptr<SharedMemoryFactory> shared_memory_factory) override { | 50 std::unique_ptr<SharedMemoryFactory> shared_memory_factory) override; |
84 shared_memory_factory_ = std::move(shared_memory_factory); | 51 bool GetSourceList(DesktopCapturer::SourceList* sources) override; |
85 } | 52 bool SelectSource(DesktopCapturer::SourceId id) override; |
86 | |
87 bool GetSourceList(DesktopCapturer::SourceList* sources) override { | |
88 sources->push_back({kWindowId, "A-Fake-DesktopCapturer-Window"}); | |
89 sources->push_back({kScreenId}); | |
90 return true; | |
91 } | |
92 | |
93 bool SelectSource(DesktopCapturer::SourceId id) override { | |
94 return id == kWindowId || id == kScreenId || id == kFullDesktopScreenId; | |
95 } | |
96 | 53 |
97 private: | 54 private: |
98 static constexpr DesktopCapturer::SourceId kWindowId = 1378277495; | 55 static constexpr DesktopCapturer::SourceId kWindowId = 1378277495; |
99 static constexpr DesktopCapturer::SourceId kScreenId = 1378277496; | 56 static constexpr DesktopCapturer::SourceId kScreenId = 1378277496; |
100 | 57 |
101 DesktopCapturer::Callback* callback_; | 58 DesktopCapturer::Callback* callback_; |
102 std::unique_ptr<SharedMemoryFactory> shared_memory_factory_; | 59 std::unique_ptr<SharedMemoryFactory> shared_memory_factory_; |
103 DesktopCapturer::Result result_; | 60 DesktopCapturer::Result result_; |
104 DesktopFrameGenerator* generator_; | 61 DesktopFrameGenerator* generator_; |
105 }; | 62 }; |
106 | 63 |
107 } // namespace webrtc | 64 } // namespace webrtc |
108 | 65 |
109 #endif // WEBRTC_MODULES_DESKTOP_CAPTURE_FAKE_DESKTOP_CAPTURER_H_ | 66 #endif // WEBRTC_MODULES_DESKTOP_CAPTURE_FAKE_DESKTOP_CAPTURER_H_ |
OLD | NEW |