17 struct kwin_image_t final:
img_t {
18 std::vector<std::uint8_t> pixels;
21 class kwin_display_t final:
public display_t {
23 bool init(mem_type_e memory,
const video::config_t &config) {
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";
33 frame_rate = config.framerate;
34 pipewire_socket = config.kwin_pipewire_socket;
36 connection = kwin::connection_t::open(
37 {config.kwin_wayland_socket, config.kwin_pipewire_socket, {}},
43 BOOST_LOG(::error) <<
"Private KWin capture: " << error;
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";
52 height = output.height;
55 offset_x = offset_y = 0;
61 std::shared_ptr<img_t> alloc_img()
override {
62 auto image = std::make_shared<kwin_image_t>();
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();
72 int dummy_img(img_t *image)
override {
73 auto *mapped =
dynamic_cast<kwin_image_t *
>(image);
77 std::fill(mapped->pixels.begin(), mapped->pixels.end(), 0);
78 mapped->frame_timestamp.reset();
82 capture_e capture(
const push_captured_image_cb_t &push,
const pull_free_image_cb_t &pull,
bool *cursor)
override {
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;
91 receiver = kwin::video_receiver_t::open(pipewire_socket, serial, width, height, frame_rate, error);
93 BOOST_LOG(::error) <<
"Private KWin capture: " << error;
94 return capture_e::error;
97 if (!connection->dispatch(std::chrono::milliseconds(0))) {
98 BOOST_LOG(::error) <<
"Private KWin capture: " << connection->error();
99 return capture_e::error;
103 if (*cursor != capture_cursor) {
104 return capture_e::reinit;
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;
112 if (!connection->dispatch(std::chrono::milliseconds(0))) {
113 BOOST_LOG(::error) <<
"Private KWin capture: " << connection->error();
114 return capture_e::error;
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;
124 return capture_e::interrupted;
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;
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;
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) {
147 BOOST_LOG(::error) <<
"Private KWin capture: no GPU to encode on";
150 return va::make_avcodec_encode_device(width, height, std::move(card), 0, 0,
false);
153#ifdef HERMES_BUILD_CUDA
154 if (memory_type == mem_type_e::cuda) {
155 return cuda::make_avcodec_encode_device(width, height,
false);
158 return std::make_unique<avcodec_encode_device_t>();
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;
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)) {
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...
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.