Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
kwin_display.cpp
Go to the documentation of this file.
1
4#include "kwin_display.h"
5
6#include "cuda.h"
7#include "kwin_session.h"
8#include "misc.h"
9#include "pipewire_session.h"
10#include "src/video.h"
11#include "vaapi.h"
12
13#include <algorithm>
14
15namespace platf {
16 namespace {
17 struct kwin_image_t final: img_t {
18 std::vector<std::uint8_t> pixels;
19 };
20
21 class kwin_display_t final: public display_t {
22 public:
23 bool init(mem_type_e memory, const video::config_t &config) {
24 // The private KWin output is SDR (is_hdr() stays false), so a client
25 // that asks for HDR gets SDR in its 10-bit stream, as it does from an
26 // SDR monitor.
27 if (config.framerate <= 0 ||
28 (memory != mem_type_e::system && memory != mem_type_e::vaapi && memory != mem_type_e::cuda)) {
29 BOOST_LOG(::error) << "Private KWin capture requires a supported mapped-frame encoder";
30 return false;
31 }
32 memory_type = memory;
33 frame_rate = config.framerate;
34 pipewire_socket = config.kwin_pipewire_socket;
35 std::string error;
36 connection = kwin::connection_t::open(
37 {config.kwin_wayland_socket, config.kwin_pipewire_socket, {}},
38 true,
39 false,
40 error
41 );
42 if (!connection) {
43 BOOST_LOG(::error) << "Private KWin capture: " << error;
44 return false;
45 }
46 const auto &output = connection->output();
47 if (output.width <= 0 || output.height <= 0 || output.width > 16384 || output.height > 16384) {
48 BOOST_LOG(::error) << "Private KWin output dimensions are unsupported";
49 return false;
50 }
51 width = output.width;
52 height = output.height;
53 // This adapter deliberately selects a single-output compositor. Its
54 // input coordinates are local to that desktop, never the host envelope.
55 offset_x = offset_y = 0;
56 env_width = width;
57 env_height = height;
58 return true;
59 }
60
61 std::shared_ptr<img_t> alloc_img() override {
62 auto image = std::make_shared<kwin_image_t>();
63 image->width = width;
64 image->height = height;
65 image->pixel_pitch = 4;
66 image->row_pitch = width * 4;
67 image->pixels.resize(static_cast<std::size_t>(image->row_pitch) * height);
68 image->data = image->pixels.data();
69 return image;
70 }
71
72 int dummy_img(img_t *image) override {
73 auto *mapped = dynamic_cast<kwin_image_t *>(image);
74 if (!mapped) {
75 return -1;
76 }
77 std::fill(mapped->pixels.begin(), mapped->pixels.end(), 0);
78 mapped->frame_timestamp.reset();
79 return 0;
80 }
81
82 capture_e capture(const push_captured_image_cb_t &push, const pull_free_image_cb_t &pull, bool *cursor) override {
83 std::string error;
84 std::uint64_t serial {};
85 const bool capture_cursor = *cursor;
86 if (!connection->start_capture(capture_cursor, serial, error)) {
87 BOOST_LOG(::error) << "Private KWin capture: " << error;
88 return capture_e::error;
89 }
90 // Declared after connection in the class so PipeWire is torn down first.
91 receiver = kwin::video_receiver_t::open(pipewire_socket, serial, width, height, frame_rate, error);
92 if (!receiver) {
93 BOOST_LOG(::error) << "Private KWin capture: " << error;
94 return capture_e::error;
95 }
96 for (;;) {
97 if (!connection->dispatch(std::chrono::milliseconds(0))) {
98 BOOST_LOG(::error) << "Private KWin capture: " << connection->error();
99 return capture_e::error;
100 }
101 // Cursor mode is a stream property; recreate on the same explicit
102 // endpoint when the client changes it. Disconnects never fall back.
103 if (*cursor != capture_cursor) {
104 return capture_e::reinit;
105 }
106 kwin::frame_t frame;
107 auto result = receiver->next(frame, std::chrono::milliseconds(100), error);
108 if (result == kwin::frame_result_t::failed) {
109 BOOST_LOG(::error) << "Private KWin capture: " << error;
110 return capture_e::error;
111 }
112 if (!connection->dispatch(std::chrono::milliseconds(0))) {
113 BOOST_LOG(::error) << "Private KWin capture: " << connection->error();
114 return capture_e::error;
115 }
116 std::shared_ptr<img_t> image;
117 if (result == kwin::frame_result_t::timeout) {
118 if (!push(std::move(image), false)) {
119 return capture_e::ok;
120 }
121 continue;
122 }
123 if (!pull(image)) {
124 return capture_e::interrupted;
125 }
126 auto *mapped = dynamic_cast<kwin_image_t *>(image.get());
127 if (!mapped || frame.width != static_cast<std::uint32_t>(width) ||
128 frame.height != static_cast<std::uint32_t>(height)) {
129 return capture_e::error;
130 }
131 mapped->pixels = std::move(frame.bgrx);
132 mapped->data = mapped->pixels.data();
133 mapped->frame_timestamp = frame.timestamp;
134 if (!push(std::move(image), true)) {
135 return capture_e::ok;
136 }
137 }
138 }
139
140 std::unique_ptr<avcodec_encode_device_t> make_avcodec_encode_device(pix_fmt_e) override {
141#ifdef HERMES_BUILD_VAAPI
142 if (memory_type == mem_type_e::vaapi) {
143 // The best GPU, as the encoder probe chose it, not whichever one
144 // happens to be renderD128 this boot.
145 file_t card {open_encode_render_node()};
146 if (card.el < 0) {
147 BOOST_LOG(::error) << "Private KWin capture: no GPU to encode on";
148 return nullptr;
149 }
150 return va::make_avcodec_encode_device(width, height, std::move(card), 0, 0, false);
151 }
152#endif
153#ifdef HERMES_BUILD_CUDA
154 if (memory_type == mem_type_e::cuda) {
155 return cuda::make_avcodec_encode_device(width, height, false);
156 }
157#endif
158 return std::make_unique<avcodec_encode_device_t>();
159 }
160
161 private:
162 mem_type_e memory_type {mem_type_e::system};
163 std::uint32_t frame_rate {};
164 std::string pipewire_socket;
165 std::unique_ptr<kwin::connection_t> connection;
166 std::unique_ptr<kwin::video_receiver_t> receiver;
167 };
168 } // namespace
169
170 std::shared_ptr<display_t> kwin_display(mem_type_e memory, const video::config_t &config) {
171 auto display = std::make_shared<kwin_display_t>();
172 if (!display->init(memory, config)) {
173 return nullptr;
174 }
175 return display;
176 }
177} // namespace platf
mem_type_e
Definition common.h:211
std::shared_ptr< display_t > display(mem_type_e hwdevice_type, const std::string &display_name, const video::config_t &config)
Get the display_t instance for the given hwdevice_type. If display_name is empty, use the first monit...
Definition misc.cpp:1658
capture_e
Definition common.h:435
Definitions for CUDA implementation.
std::unique_ptr< platf::deinit_t > init(const std::filesystem::path &persistence_filepath, const config::video_t &video_config)
Initialize the implementation and perform the initial state recovery (if needed).
int open_encode_render_node(bool nvenc)
Open the render node of the best GPU to encode a stream on.
Hermes capture adapter for an explicitly selected private KWin.
Explicit, generation-bound connections to a private KWin session.
Miscellaneous declarations for Linux.
Video from an explicitly addressed private-session PipeWire node.
Declarations for VA-API hardware accelerated capture.
Declarations for video.