1 /*
2  *  Copyright 2022 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_LINUX_WAYLAND_TEST_TEST_SCREENCAST_STREAM_PROVIDER_H_
12 #define MODULES_DESKTOP_CAPTURE_LINUX_WAYLAND_TEST_TEST_SCREENCAST_STREAM_PROVIDER_H_
13 
14 #include <pipewire/pipewire.h>
15 #include <spa/param/video/format-utils.h>
16 
17 #include "modules/desktop_capture/linux/wayland/screencast_stream_utils.h"
18 #include "modules/desktop_capture/rgba_color.h"
19 #include "rtc_base/random.h"
20 
21 namespace webrtc {
22 
23 class TestScreenCastStreamProvider {
24  public:
25   class Observer {
26    public:
27     virtual void OnBufferAdded() = 0;
28     virtual void OnFrameRecorded() = 0;
29     virtual void OnStreamReady(uint32_t stream_node_id) = 0;
30     virtual void OnStartStreaming() = 0;
31     virtual void OnStopStreaming() = 0;
32 
33    protected:
34     Observer() = default;
35     virtual ~Observer() = default;
36   };
37 
38   explicit TestScreenCastStreamProvider(Observer* observer,
39                                         uint32_t width,
40                                         uint32_t height);
41   ~TestScreenCastStreamProvider();
42 
43   uint32_t PipeWireNodeId();
44 
45   void RecordFrame(RgbaColor rgba_color);
46   void StartStreaming();
47   void StopStreaming();
48 
49  private:
50   Observer* observer_;
51 
52   // Resolution parameters.
53   uint32_t width_ = 0;
54   uint32_t height_ = 0;
55 
56   bool is_streaming_ = false;
57   uint32_t pw_node_id_ = 0;
58 
59   // PipeWire types
60   struct pw_context* pw_context_ = nullptr;
61   struct pw_core* pw_core_ = nullptr;
62   struct pw_stream* pw_stream_ = nullptr;
63   struct pw_thread_loop* pw_main_loop_ = nullptr;
64 
65   spa_hook spa_core_listener_;
66   spa_hook spa_stream_listener_;
67 
68   // event handlers
69   pw_core_events pw_core_events_ = {};
70   pw_stream_events pw_stream_events_ = {};
71 
72   struct spa_video_info_raw spa_video_format_;
73 
74   // PipeWire callbacks
75   static void OnCoreError(void* data,
76                           uint32_t id,
77                           int seq,
78                           int res,
79                           const char* message);
80   static void OnStreamAddBuffer(void* data, pw_buffer* buffer);
81   static void OnStreamRemoveBuffer(void* data, pw_buffer* buffer);
82   static void OnStreamParamChanged(void* data,
83                                    uint32_t id,
84                                    const struct spa_pod* format);
85   static void OnStreamStateChanged(void* data,
86                                    pw_stream_state old_state,
87                                    pw_stream_state state,
88                                    const char* error_message);
89 };
90 
91 }  // namespace webrtc
92 
93 #endif  // MODULES_DESKTOP_CAPTURE_LINUX_WAYLAND_TEST_TEST_SCREENCAST_STREAM_PROVIDER_H_
94