22using namespace std::literals;
33 static std::vector<output_rect_t> output_rects(
const std::vector<std::unique_ptr<monitor_t>> &monitors) {
34 std::vector<output_rect_t> rects;
35 rects.reserve(monitors.size());
36 for (
const auto &monitor : monitors) {
37 const auto &viewport = monitor->viewport;
38 const int width = monitor->logical_width > 0 ? monitor->logical_width : viewport.width;
39 const int height = monitor->logical_height > 0 ? monitor->logical_height : viewport.height;
40 rects.push_back({viewport.offset_x, viewport.offset_y, width, height});
54 int init(
platf::mem_type_e hwdevice_type,
const std::string &display_name, const ::video::config_t &config) {
55 delay = std::chrono::nanoseconds {1s} / config.framerate;
56 mem_type = hwdevice_type;
62 interface.listen(display.registry());
67 BOOST_LOG(error) <<
"Missing Wayland wire for xdg_output"sv;
86 const char *forced = std::getenv(
"HERMES_WAYLAND_CAPTURE");
87 if (forced && !std::strcmp(forced,
"icc")) {
90 BOOST_LOG(error) <<
"HERMES_WAYLAND_CAPTURE=icc, but this compositor does not offer "sv
91 <<
"ext-image-copy-capture with linux-dmabuf."sv;
94 }
else if (forced && !std::strcmp(forced,
"screencopy")) {
96 if (!have_screencopy) {
97 BOOST_LOG(error) <<
"HERMES_WAYLAND_CAPTURE=screencopy, but this compositor does not offer "sv
98 <<
"wlr-screencopy."sv;
102 use_icc = !have_screencopy && have_icc;
105 if (!have_screencopy && !have_icc) {
106 BOOST_LOG(error) <<
"This compositor offers neither wlr-screencopy nor ext-image-copy-capture; "sv
107 <<
"there is no way to capture it."sv;
111 BOOST_LOG(info) <<
"Wayland capture protocol: "sv
112 << (use_icc ?
"ext-image-copy-capture"sv :
"wlr-screencopy"sv);
114 if (interface.monitors.empty()) {
115 BOOST_LOG(error) <<
"Wayland compositor did not advertise any enabled outputs."sv;
126 for (
auto &candidate : interface.monitors) {
127 candidate->listen(interface.output_manager);
142 auto monitor = interface.monitors[0].get();
144 if (!display_name.empty() && display_name.rfind(
"VIRTUAL-", 0) != 0) {
146 auto streamedMonitor = util::from_view(display_name);
148 if (streamedMonitor >= 0 && streamedMonitor < interface.monitors.size()) {
149 monitor = interface.monitors[streamedMonitor].get();
156 const char *backend_label =
"Hyprland headless";
157 if (connector.empty()) {
159 backend_label =
"Hermes-KMS";
161 if (connector.empty()) {
163 backend_label =
"EVDI";
165 const auto monitor_it = std::find_if(interface.monitors.begin(), interface.monitors.end(), [&](
const auto &candidate) {
166 return candidate->name == connector || candidate->description.find(connector) != std::string::npos;
168 if (connector.empty() || monitor_it == interface.monitors.end()) {
169 BOOST_LOG(error) <<
"Wayland "sv << backend_label <<
" output " << display_name
170 <<
" is not enabled by the compositor; refusing to capture a physical monitor."sv;
173 monitor = monitor_it->get();
174 BOOST_LOG(info) <<
"Selected Wayland "sv << backend_label <<
" output " << connector;
177 output = monitor->output;
179 offset_x = monitor->viewport.offset_x;
180 offset_y = monitor->viewport.offset_y;
181 width = monitor->viewport.width;
182 height = monitor->viewport.height;
192 const output_rect_t streamed {offset_x, offset_y, monitor->logical_width, monitor->logical_height};
193 const auto geometry =
input_geometry(envelope, streamed, width, height);
194 offset_x = geometry.offset_x;
195 offset_y = geometry.offset_y;
196 env_width = geometry.env_width;
197 env_height = geometry.env_height;
199 BOOST_LOG(info) <<
"Selected monitor ["sv << monitor->description <<
"] for streaming"sv;
200 BOOST_LOG(debug) <<
"Resolution: "sv << width <<
'x' << height;
201 BOOST_LOG(info) <<
"Desktop envelope "sv << envelope.width <<
'x' << envelope.height <<
" at "sv << envelope.x <<
',' << envelope.y
202 <<
" (logical), streamed output "sv << streamed.width <<
'x' << streamed.height <<
" at "sv << streamed.x <<
',' << streamed.y
203 <<
"; in its "sv << width <<
'x' << height <<
" pixels: envelope "sv << env_width <<
'x' << env_height
204 <<
", offset "sv << offset_x <<
',' << offset_y;
214 auto to = std::chrono::steady_clock::now() + timeout;
225 if (!dmabuf.icc_active() &&
227 interface.image_copy_capture_manager,
228 interface.image_capture_source_manager,
229 interface.dmabuf_interface,
239 dmabuf.icc_capture(cursor);
244 dmabuf.listen(interface.screencopy_manager, interface.dmabuf_interface, output, cursor);
248 auto remaining_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(to - std::chrono::steady_clock::now());
249 if (remaining_time_ms.count() < 0 || !display.dispatch(remaining_time_ms)) {
254 if (
auto err = display.connection_error()) {
265 dmabuf.icc_capture(cursor);
269 auto current_frame = dmabuf.current_frame;
273 current_frame->sd.width != width ||
274 current_frame->sd.height != height
284 BOOST_LOG(error) <<
"Wayland capture has failed on every frame and is being stopped. "sv
285 <<
"Streaming would have continued as a black screen. See the errors "sv
286 <<
"above; on a machine with more than one GPU, set adapter_name to the "sv
287 <<
"render node the compositor renders on."sv;
298 std::chrono::nanoseconds delay;
305 bool use_icc {
false};
311 auto next_frame = std::chrono::steady_clock::now();
313 sleep_overshoot_logger.reset();
316 auto now = std::chrono::steady_clock::now();
318 if (next_frame > now) {
319 std::this_thread::sleep_for(next_frame - now);
320 sleep_overshoot_logger.first_point(next_frame);
321 sleep_overshoot_logger.second_point_now_and_log();
325 if (next_frame < now) {
326 next_frame = now + delay;
329 std::shared_ptr<platf::img_t> img_out;
330 auto status = snapshot(pull_free_image_cb, img_out, 1000ms, *cursor);
337 if (!push_captured_image_cb(std::move(img_out),
false)) {
342 if (!push_captured_image_cb(std::move(img_out),
true)) {
347 BOOST_LOG(error) <<
"Unrecognized capture status ["sv << (int) status <<
']';
356 auto status = wlr_t::snapshot(pull_free_image_cb, img_out, timeout, cursor);
360 auto frame_timestamp = std::chrono::steady_clock::now();
362 auto current_frame = dmabuf.current_frame;
364 auto rgb_opt = egl::import_source(egl_display.get(), current_frame->sd);
370 if (!pull_free_image_cb(img_out)) {
371 return platf::capture_e::interrupted;
374 gl::ctx.BindTexture(GL_TEXTURE_2D, (*rgb_opt)->tex[0]);
378 gl::ctx.GetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w);
379 gl::ctx.GetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &h);
380 BOOST_LOG(debug) <<
"width and height: w "sv << w <<
" h "sv << h;
382 gl::ctx.GetTextureSubImage((*rgb_opt)->tex[0], 0, 0, 0, 0, width, height, 1, GL_BGRA, GL_UNSIGNED_BYTE, img_out->height * img_out->row_pitch, img_out->data);
383 gl::ctx.BindTexture(GL_TEXTURE_2D, 0);
384 img_out->frame_timestamp = frame_timestamp;
386 return platf::capture_e::ok;
389 int init(platf::mem_type_e hwdevice_type,
const std::string &display_name, const ::video::config_t &config) {
390 if (wlr_t::init(hwdevice_type, display_name, config)) {
394 egl_display = egl::make_display(display.get());
399 auto ctx_opt = egl::make_ctx(egl_display.get());
404 ctx = std::move(*ctx_opt);
411 return pyrowave::make_ram_encode_device(width, height);
415 std::unique_ptr<platf::avcodec_encode_device_t> make_avcodec_encode_device(platf::pix_fmt_e pix_fmt)
override {
416#ifdef HERMES_BUILD_VAAPI
417 if (mem_type == platf::mem_type_e::vaapi) {
418 return va::make_avcodec_encode_device(width, height,
false);
422#ifdef HERMES_BUILD_CUDA
423 if (mem_type == platf::mem_type_e::cuda) {
424 return cuda::make_avcodec_encode_device(width, height,
false);
428 return std::make_unique<platf::avcodec_encode_device_t>();
431 std::shared_ptr<platf::img_t> alloc_img()
override {
432 auto img = std::make_shared<img_t>();
434 img->height = height;
435 img->pixel_pitch = 4;
436 img->row_pitch = img->pixel_pitch * width;
437 img->data =
new std::uint8_t[height * img->row_pitch];
442 egl::display_t egl_display;
449 auto next_frame = std::chrono::steady_clock::now();
451 sleep_overshoot_logger.reset();
454 auto now = std::chrono::steady_clock::now();
456 if (next_frame > now) {
457 std::this_thread::sleep_for(next_frame - now);
458 sleep_overshoot_logger.first_point(next_frame);
459 sleep_overshoot_logger.second_point_now_and_log();
463 if (next_frame < now) {
464 next_frame = now + delay;
467 std::shared_ptr<platf::img_t> img_out;
468 auto status = snapshot(pull_free_image_cb, img_out, 1000ms, *cursor);
475 if (!push_captured_image_cb(std::move(img_out),
false)) {
480 if (!push_captured_image_cb(std::move(img_out),
true)) {
485 BOOST_LOG(error) <<
"Unrecognized capture status ["sv << (int) status <<
']';
494 auto status = wlr_t::snapshot(pull_free_image_cb, img_out, timeout, cursor);
498 auto frame_timestamp = std::chrono::steady_clock::now();
500 if (!pull_free_image_cb(img_out)) {
503 auto img = (egl::img_descriptor_t *) img_out.get();
506 auto current_frame = dmabuf.current_frame;
509 img->sequence = sequence;
511 img->sd = current_frame->sd;
512 img->frame_timestamp = frame_timestamp;
515 std::fill_n(current_frame->sd.fds, 4, -1);
517 return platf::capture_e::ok;
520 std::shared_ptr<platf::img_t> alloc_img()
override {
521 auto img = std::make_shared<egl::img_descriptor_t>();
524 img->height = height;
526 img->serial = std::numeric_limits<
decltype(img->serial)>::max();
530 std::fill_n(img->sd.fds, 4, -1);
537 return pyrowave::make_gpu_encode_device(width, height, 0, 0);
541 std::unique_ptr<platf::avcodec_encode_device_t> make_avcodec_encode_device(platf::pix_fmt_e pix_fmt)
override {
542#ifdef HERMES_BUILD_VAAPI
543 if (mem_type == platf::mem_type_e::vaapi) {
544 return va::make_avcodec_encode_device(width, height, 0, 0,
true);
548#ifdef HERMES_BUILD_CUDA
549 if (mem_type == platf::mem_type_e::cuda) {
554 return std::make_unique<platf::avcodec_encode_device_t>();
557 int dummy_img(platf::img_t *img)
override {
562 std::uint64_t sequence {};
568 std::shared_ptr<display_t> wl_display(
mem_type_e hwdevice_type,
const std::string &display_name,
const video::config_t &config) {
569 if (hwdevice_type != platf::mem_type_e::system && hwdevice_type != platf::mem_type_e::vaapi && hwdevice_type != platf::mem_type_e::cuda) {
570 BOOST_LOG(
error) <<
"Could not initialize display with the given hw device type."sv;
574 if (hwdevice_type == platf::mem_type_e::vaapi || hwdevice_type == platf::mem_type_e::cuda) {
575 auto wlr = std::make_shared<wl::wlr_vram_t>();
576 if (wlr->init(hwdevice_type, display_name, config)) {
583 auto wlr = std::make_shared<wl::wlr_ram_t>();
584 if (wlr->init(hwdevice_type, display_name, config)) {
591 std::vector<std::string> wl_display_names() {
592 std::vector<std::string> display_names;
599 wl::interface_t interface;
600 interface.listen(
display.registry());
605 BOOST_LOG(warning) <<
"Missing Wayland wire for xdg_output"sv;
610 BOOST_LOG(warning) <<
"Missing Wayland wire for wlr-export-dmabuf"sv;
614 for (
auto &monitor : interface.monitors) {
615 monitor->listen(interface.output_manager);
620 BOOST_LOG(info) <<
"-------- Start of Wayland monitor list --------"sv;
622 for (
int x = 0; x < interface.monitors.size(); ++x) {
623 auto monitor = interface.monitors[x].get();
625 BOOST_LOG(info) <<
"Monitor " << x <<
" is "sv << monitor->name <<
": "sv << monitor->description;
627 display_names.emplace_back(std::to_string(x));
631 BOOST_LOG(debug) <<
"Desktop envelope: "sv << envelope.width <<
'x' << envelope.height <<
" at "sv << envelope.x <<
',' << envelope.y <<
" (logical)"sv;
633 BOOST_LOG(info) <<
"--------- End of Wayland monitor list ---------"sv;
635 return display_names;
std::function< bool(std::shared_ptr< img_t > &&img, bool frame_captured)> push_captured_image_cb_t
Callback for when a new image is ready. When display has a new image ready or a timeout occurs,...
std::function< bool(std::shared_ptr< img_t > &img_out)> pull_free_image_cb_t
Get free image from pool. Calls must be synchronized. Blocks until there is free image in the pool or...
virtual std::unique_ptr< avcodec_encode_device_t > make_pyrowave_encode_device()
Make a device that converts captured frames into PyroWave's GPU planes.
@ WAITING
Waiting for a frame.
@ REINIT
Reinitialize the frame.
@ IMAGE_COPY_CAPTURE
ext-image-copy-capture manager
@ IMAGE_CAPTURE_SOURCE
ext-image-capture-source manager, names an output as a source
@ WLR_EXPORT_DMABUF
screencopy manager
@ LINUX_DMABUF
linux-dmabuf protocol
platf::capture_e capture(const push_captured_image_cb_t &push_captured_image_cb, const pull_free_image_cb_t &pull_free_image_cb, bool *cursor) override
Capture a frame.
platf::capture_e capture(const push_captured_image_cb_t &push_captured_image_cb, const pull_free_image_cb_t &pull_free_image_cb, bool *cursor) override
Capture a frame.
Declarations for common platform specific utilities.
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...
@ reinit
Need to reinitialize.
@ interrupted
Capture was interrupted.
std::unique_ptr< platf::avcodec_encode_device_t > make_avcodec_gl_encode_device(int width, int height, int offset_x, int offset_y)
Create a GL->CUDA encoding device for consuming captured dmabufs.
Definitions for CUDA implementation.
Declarations for logging related functions.
bool dmabuf_capture_exhausted()
Whether the dmabuf capture path has failed too often to be retried.
output_rect_t desktop_envelope(const std::vector< output_rect_t > &outputs)
The desktop envelope: the bounding box of every output in logical coordinates, origin included....
input_geometry_t input_geometry(const output_rect_t &envelope, const output_rect_t &output, int mode_width, int mode_height)
Express an output's offset within the envelope, and the envelope, in that output's own capture pixels...
std::string compositor_render_node(display_t &display, zwp_linux_dmabuf_v1 *dmabuf_interface)
Ask the compositor which GPU it renders on.
GPU conversion for the experimental PyroWave codec.
Declarations for VA-API hardware accelerated capture.
std::string getEvdiConnectorName(const std::string &displayName)
std::string getHermesKmsConnectorName(const std::string &displayName)
bool isHermesKmsDisplay(const std::string &displayName)
std::string getHyprlandOutputName(const std::string &displayName)
The Hyprland output name backing a virtual display, or empty.
Virtual display declarations for Linux.
Declarations for Wayland capture.