mirror of
https://github.com/kunkundi/crossdesk.git
synced 2026-10-28 08:42:43 +08:00
[refactor] reorganize platform and wire sources
This commit is contained in:
@@ -0,0 +1,575 @@
|
||||
#include "screen_capturer_drm.h"
|
||||
|
||||
#if defined(CROSSDESK_HAS_DRM) && CROSSDESK_HAS_DRM && \
|
||||
defined(__has_include) && __has_include(<xf86drm.h>) && \
|
||||
__has_include(<xf86drmMode.h>)
|
||||
#define CROSSDESK_DRM_BUILD_ENABLED 1
|
||||
#include <xf86drm.h>
|
||||
#include <xf86drmMode.h>
|
||||
#elif defined(CROSSDESK_HAS_DRM) && CROSSDESK_HAS_DRM && \
|
||||
defined(__has_include) && __has_include(<libdrm/xf86drm.h>) && \
|
||||
__has_include(<libdrm/xf86drmMode.h>)
|
||||
#define CROSSDESK_DRM_BUILD_ENABLED 1
|
||||
#include <libdrm/xf86drm.h>
|
||||
#include <libdrm/xf86drmMode.h>
|
||||
#else
|
||||
#define CROSSDESK_DRM_BUILD_ENABLED 0
|
||||
#endif
|
||||
|
||||
#if CROSSDESK_DRM_BUILD_ENABLED
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
#include <display_stream_id.h>
|
||||
#include "libyuv.h"
|
||||
#include "rd_log.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr int kMaxDrmCards = 16;
|
||||
|
||||
const char* ConnectorTypeName(uint32_t type) {
|
||||
switch (type) {
|
||||
case DRM_MODE_CONNECTOR_VGA:
|
||||
return "VGA";
|
||||
case DRM_MODE_CONNECTOR_DVII:
|
||||
return "DVI-I";
|
||||
case DRM_MODE_CONNECTOR_DVID:
|
||||
return "DVI-D";
|
||||
case DRM_MODE_CONNECTOR_DVIA:
|
||||
return "DVI-A";
|
||||
case DRM_MODE_CONNECTOR_HDMIA:
|
||||
return "HDMI-A";
|
||||
case DRM_MODE_CONNECTOR_HDMIB:
|
||||
return "HDMI-B";
|
||||
case DRM_MODE_CONNECTOR_DisplayPort:
|
||||
return "DP";
|
||||
case DRM_MODE_CONNECTOR_eDP:
|
||||
return "eDP";
|
||||
case DRM_MODE_CONNECTOR_LVDS:
|
||||
return "LVDS";
|
||||
#ifdef DRM_MODE_CONNECTOR_VIRTUAL
|
||||
case DRM_MODE_CONNECTOR_VIRTUAL:
|
||||
return "Virtual";
|
||||
#endif
|
||||
default:
|
||||
return "Display";
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
ScreenCapturerDrm::ScreenCapturerDrm() {}
|
||||
|
||||
ScreenCapturerDrm::~ScreenCapturerDrm() { Destroy(); }
|
||||
|
||||
int ScreenCapturerDrm::Init(const int fps, cb_desktop_data cb) {
|
||||
Destroy();
|
||||
|
||||
if (!cb) {
|
||||
LOG_ERROR("DRM screen capturer callback is null");
|
||||
return -1;
|
||||
}
|
||||
|
||||
fps_ = std::max(1, fps);
|
||||
callback_ = cb;
|
||||
monitor_index_ = 0;
|
||||
initial_monitor_index_ = 0;
|
||||
consecutive_failures_ = 0;
|
||||
display_info_list_.clear();
|
||||
outputs_.clear();
|
||||
y_plane_.clear();
|
||||
uv_plane_.clear();
|
||||
|
||||
if (!DiscoverOutputs()) {
|
||||
LOG_ERROR("DRM screen capturer could not find active outputs");
|
||||
callback_ = nullptr;
|
||||
CloseDevices();
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerDrm::Destroy() {
|
||||
Stop();
|
||||
callback_ = nullptr;
|
||||
display_info_list_.clear();
|
||||
outputs_.clear();
|
||||
y_plane_.clear();
|
||||
uv_plane_.clear();
|
||||
CloseDevices();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerDrm::Start(bool show_cursor) {
|
||||
if (running_) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (outputs_.empty()) {
|
||||
LOG_ERROR("DRM screen capturer has no output to capture");
|
||||
return -1;
|
||||
}
|
||||
|
||||
show_cursor_ = show_cursor;
|
||||
paused_ = false;
|
||||
|
||||
int probe_index = monitor_index_.load();
|
||||
if (probe_index < 0 || probe_index >= static_cast<int>(outputs_.size())) {
|
||||
probe_index = 0;
|
||||
}
|
||||
|
||||
if (!CaptureOutputFrame(outputs_[probe_index], false)) {
|
||||
LOG_ERROR("DRM start probe failed on output {}", outputs_[probe_index].name);
|
||||
return -1;
|
||||
}
|
||||
|
||||
running_ = true;
|
||||
thread_ = std::thread([this]() { CaptureLoop(); });
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerDrm::Stop() {
|
||||
if (!running_) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
running_ = false;
|
||||
if (thread_.joinable()) {
|
||||
thread_.join();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerDrm::Pause([[maybe_unused]] int monitor_index) {
|
||||
paused_ = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerDrm::Resume([[maybe_unused]] int monitor_index) {
|
||||
paused_ = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerDrm::SwitchTo(int monitor_index) {
|
||||
if (monitor_index < 0 ||
|
||||
monitor_index >= static_cast<int>(display_info_list_.size())) {
|
||||
LOG_ERROR("Invalid DRM monitor index: {}", monitor_index);
|
||||
return -1;
|
||||
}
|
||||
|
||||
monitor_index_ = monitor_index;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerDrm::ResetToInitialMonitor() {
|
||||
monitor_index_ = initial_monitor_index_;
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::vector<DisplayInfo> ScreenCapturerDrm::GetDisplayInfoList() {
|
||||
return display_info_list_;
|
||||
}
|
||||
|
||||
bool ScreenCapturerDrm::DiscoverOutputs() {
|
||||
for (int card_index = 0; card_index < kMaxDrmCards; ++card_index) {
|
||||
const std::string card_path = "/dev/dri/card" + std::to_string(card_index);
|
||||
const int fd = open(card_path.c_str(), O_RDWR | O_CLOEXEC);
|
||||
if (fd < 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
drmModeRes* resources = drmModeGetResources(fd);
|
||||
if (!resources) {
|
||||
close(fd);
|
||||
continue;
|
||||
}
|
||||
|
||||
DrmDevice device;
|
||||
device.fd = fd;
|
||||
device.path = card_path;
|
||||
devices_.push_back(device);
|
||||
const int device_slot = static_cast<int>(devices_.size()) - 1;
|
||||
const size_t output_count_before = outputs_.size();
|
||||
|
||||
for (int i = 0; i < resources->count_connectors; ++i) {
|
||||
drmModeConnector* connector =
|
||||
drmModeGetConnector(fd, resources->connectors[i]);
|
||||
if (!connector) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (connector->connection != DRM_MODE_CONNECTED ||
|
||||
connector->count_modes <= 0) {
|
||||
drmModeFreeConnector(connector);
|
||||
continue;
|
||||
}
|
||||
|
||||
uint32_t crtc_id = 0;
|
||||
if (connector->encoder_id != 0) {
|
||||
drmModeEncoder* encoder = drmModeGetEncoder(fd, connector->encoder_id);
|
||||
if (encoder) {
|
||||
crtc_id = encoder->crtc_id;
|
||||
drmModeFreeEncoder(encoder);
|
||||
}
|
||||
}
|
||||
|
||||
if (crtc_id == 0) {
|
||||
for (int enc_idx = 0; enc_idx < connector->count_encoders; ++enc_idx) {
|
||||
drmModeEncoder* encoder =
|
||||
drmModeGetEncoder(fd, connector->encoders[enc_idx]);
|
||||
if (!encoder) {
|
||||
continue;
|
||||
}
|
||||
if (encoder->crtc_id != 0) {
|
||||
crtc_id = encoder->crtc_id;
|
||||
drmModeFreeEncoder(encoder);
|
||||
break;
|
||||
}
|
||||
drmModeFreeEncoder(encoder);
|
||||
}
|
||||
}
|
||||
|
||||
if (crtc_id == 0) {
|
||||
drmModeFreeConnector(connector);
|
||||
continue;
|
||||
}
|
||||
|
||||
drmModeCrtc* crtc = drmModeGetCrtc(fd, crtc_id);
|
||||
if (!crtc || !crtc->mode_valid || crtc->width <= 0 || crtc->height <= 0) {
|
||||
if (crtc) {
|
||||
drmModeFreeCrtc(crtc);
|
||||
}
|
||||
drmModeFreeConnector(connector);
|
||||
continue;
|
||||
}
|
||||
|
||||
DrmOutput output;
|
||||
output.device_index = device_slot;
|
||||
output.connector_id = connector->connector_id;
|
||||
output.crtc_id = crtc_id;
|
||||
output.left = crtc->x;
|
||||
output.top = crtc->y;
|
||||
output.width = static_cast<int>(crtc->width);
|
||||
output.height = static_cast<int>(crtc->height);
|
||||
output.name = std::string(ConnectorTypeName(connector->connector_type)) +
|
||||
std::to_string(connector->connector_type_id);
|
||||
output.stream_id = MakeDisplayStreamId(outputs_.size());
|
||||
|
||||
outputs_.push_back(output);
|
||||
display_info_list_.push_back(
|
||||
DisplayInfo(output.name, output.left, output.top,
|
||||
output.left + output.width, output.top + output.height));
|
||||
|
||||
LOG_INFO("DRM output found: {} on {}, {}x{} @ ({}, {})", output.name,
|
||||
card_path, output.width, output.height, output.left, output.top);
|
||||
|
||||
drmModeFreeCrtc(crtc);
|
||||
drmModeFreeConnector(connector);
|
||||
}
|
||||
|
||||
drmModeFreeResources(resources);
|
||||
|
||||
if (outputs_.size() == output_count_before) {
|
||||
close(fd);
|
||||
devices_.pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
if (outputs_.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
LOG_INFO("DRM screen capturer discovered {} output(s)", outputs_.size());
|
||||
return true;
|
||||
}
|
||||
|
||||
void ScreenCapturerDrm::CloseDevices() {
|
||||
for (auto& device : devices_) {
|
||||
if (device.fd >= 0) {
|
||||
close(device.fd);
|
||||
device.fd = -1;
|
||||
}
|
||||
}
|
||||
devices_.clear();
|
||||
}
|
||||
|
||||
void ScreenCapturerDrm::CaptureLoop() {
|
||||
using clock = std::chrono::steady_clock;
|
||||
const auto frame_interval =
|
||||
std::chrono::milliseconds(std::max(1, 1000 / std::max(1, fps_)));
|
||||
|
||||
while (running_) {
|
||||
const auto frame_start = clock::now();
|
||||
if (!paused_) {
|
||||
int index = monitor_index_.load();
|
||||
if (index >= 0 && index < static_cast<int>(outputs_.size())) {
|
||||
const bool ok = CaptureOutputFrame(outputs_[index], true);
|
||||
if (!ok) {
|
||||
++consecutive_failures_;
|
||||
if (consecutive_failures_ == 1 || consecutive_failures_ % 60 == 0) {
|
||||
LOG_WARN("DRM capture failed (consecutive={})",
|
||||
consecutive_failures_);
|
||||
}
|
||||
} else {
|
||||
consecutive_failures_ = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
clock::now() - frame_start);
|
||||
if (elapsed < frame_interval) {
|
||||
std::this_thread::sleep_for(frame_interval - elapsed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool ScreenCapturerDrm::CaptureOutputFrame(const DrmOutput& output,
|
||||
bool emit_callback) {
|
||||
if (output.device_index < 0 ||
|
||||
output.device_index >= static_cast<int>(devices_.size())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const int fd = devices_[output.device_index].fd;
|
||||
if (fd < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
drmModeCrtc* crtc = drmModeGetCrtc(fd, output.crtc_id);
|
||||
if (!crtc) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const uint32_t fb_id = crtc->buffer_id;
|
||||
drmModeFreeCrtc(crtc);
|
||||
if (fb_id == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
drmModeFB* fb = drmModeGetFB(fd, fb_id);
|
||||
if (!fb) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const uint32_t handle = fb->handle;
|
||||
const uint32_t pitch = fb->pitch;
|
||||
const int src_width = static_cast<int>(fb->width);
|
||||
const int src_height = static_cast<int>(fb->height);
|
||||
const int bpp = static_cast<int>(fb->bpp);
|
||||
drmModeFreeFB(fb);
|
||||
|
||||
if (handle == 0 || pitch == 0 || src_width <= 1 || src_height <= 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (bpp != 32) {
|
||||
LOG_WARN("DRM capture unsupported bpp: {}", bpp);
|
||||
return false;
|
||||
}
|
||||
|
||||
const size_t map_size =
|
||||
static_cast<size_t>(pitch) * static_cast<size_t>(src_height);
|
||||
uint8_t* mapped_ptr = nullptr;
|
||||
size_t mapped_size = 0;
|
||||
int prime_fd = -1;
|
||||
if (!MapFramebuffer(fd, handle, map_size, &mapped_ptr, &mapped_size,
|
||||
&prime_fd)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int capture_width = std::min(src_width, output.width);
|
||||
int capture_height = std::min(src_height, output.height);
|
||||
if (capture_width <= 0 || capture_height <= 0) {
|
||||
capture_width = src_width;
|
||||
capture_height = src_height;
|
||||
}
|
||||
|
||||
capture_width &= ~1;
|
||||
capture_height &= ~1;
|
||||
if (capture_width <= 1 || capture_height <= 1) {
|
||||
UnmapFramebuffer(mapped_ptr, mapped_size, prime_fd);
|
||||
return false;
|
||||
}
|
||||
|
||||
const size_t y_size =
|
||||
static_cast<size_t>(capture_width) * static_cast<size_t>(capture_height);
|
||||
const size_t uv_size = y_size / 2;
|
||||
if (y_plane_.size() != y_size) {
|
||||
y_plane_.resize(y_size);
|
||||
}
|
||||
if (uv_plane_.size() != uv_size) {
|
||||
uv_plane_.resize(uv_size);
|
||||
}
|
||||
|
||||
const int convert_ret =
|
||||
libyuv::ARGBToNV12(mapped_ptr, static_cast<int>(pitch), y_plane_.data(),
|
||||
capture_width, uv_plane_.data(), capture_width,
|
||||
capture_width, capture_height);
|
||||
|
||||
if (convert_ret != 0) {
|
||||
UnmapFramebuffer(mapped_ptr, mapped_size, prime_fd);
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> nv12;
|
||||
nv12.reserve(y_plane_.size() + uv_plane_.size());
|
||||
nv12.insert(nv12.end(), y_plane_.begin(), y_plane_.end());
|
||||
nv12.insert(nv12.end(), uv_plane_.begin(), uv_plane_.end());
|
||||
|
||||
if (emit_callback && callback_) {
|
||||
callback_(nv12.data(), static_cast<int>(nv12.size()), capture_width,
|
||||
capture_height, output.stream_id.c_str());
|
||||
}
|
||||
|
||||
UnmapFramebuffer(mapped_ptr, mapped_size, prime_fd);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ScreenCapturerDrm::MapFramebuffer(int fd, uint32_t handle, size_t map_size,
|
||||
uint8_t** mapped_ptr,
|
||||
size_t* mapped_size,
|
||||
int* prime_fd) const {
|
||||
if (!mapped_ptr || !mapped_size || !prime_fd || map_size == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*mapped_ptr = nullptr;
|
||||
*mapped_size = 0;
|
||||
*prime_fd = -1;
|
||||
|
||||
drm_mode_map_dumb map_arg{};
|
||||
map_arg.handle = handle;
|
||||
if (drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg) == 0) {
|
||||
void* mapped = mmap(nullptr, map_size, PROT_READ, MAP_SHARED, fd,
|
||||
static_cast<off_t>(map_arg.offset));
|
||||
if (mapped != MAP_FAILED) {
|
||||
*mapped_ptr = static_cast<uint8_t*>(mapped);
|
||||
*mapped_size = map_size;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
int dma_fd = -1;
|
||||
if (drmPrimeHandleToFD(fd, handle, DRM_CLOEXEC, &dma_fd) == 0) {
|
||||
size_t dma_map_size = map_size;
|
||||
const off_t fd_size = lseek(dma_fd, 0, SEEK_END);
|
||||
if (fd_size > 0) {
|
||||
dma_map_size = std::min(map_size, static_cast<size_t>(fd_size));
|
||||
}
|
||||
|
||||
void* mapped =
|
||||
mmap(nullptr, dma_map_size, PROT_READ, MAP_SHARED, dma_fd, 0);
|
||||
if (mapped != MAP_FAILED) {
|
||||
*mapped_ptr = static_cast<uint8_t*>(mapped);
|
||||
*mapped_size = dma_map_size;
|
||||
*prime_fd = dma_fd;
|
||||
return true;
|
||||
}
|
||||
|
||||
close(dma_fd);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ScreenCapturerDrm::UnmapFramebuffer(uint8_t* mapped_ptr, size_t mapped_size,
|
||||
int prime_fd) const {
|
||||
if (mapped_ptr && mapped_size > 0) {
|
||||
munmap(mapped_ptr, mapped_size);
|
||||
}
|
||||
|
||||
if (prime_fd >= 0) {
|
||||
close(prime_fd);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace crossdesk
|
||||
|
||||
#else
|
||||
|
||||
#include "rd_log.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
ScreenCapturerDrm::ScreenCapturerDrm() {}
|
||||
|
||||
ScreenCapturerDrm::~ScreenCapturerDrm() { Destroy(); }
|
||||
|
||||
int ScreenCapturerDrm::Init([[maybe_unused]] const int fps, cb_desktop_data cb) {
|
||||
Destroy();
|
||||
callback_ = cb;
|
||||
LOG_WARN("DRM screen capturer disabled: libdrm headers not available");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ScreenCapturerDrm::Destroy() {
|
||||
Stop();
|
||||
callback_ = nullptr;
|
||||
display_info_list_.clear();
|
||||
outputs_.clear();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerDrm::Start([[maybe_unused]] bool show_cursor) { return -1; }
|
||||
|
||||
int ScreenCapturerDrm::Stop() {
|
||||
running_ = false;
|
||||
if (thread_.joinable()) {
|
||||
thread_.join();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerDrm::Pause([[maybe_unused]] int monitor_index) { return 0; }
|
||||
|
||||
int ScreenCapturerDrm::Resume([[maybe_unused]] int monitor_index) { return 0; }
|
||||
|
||||
int ScreenCapturerDrm::SwitchTo([[maybe_unused]] int monitor_index) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ScreenCapturerDrm::ResetToInitialMonitor() { return 0; }
|
||||
|
||||
std::vector<DisplayInfo> ScreenCapturerDrm::GetDisplayInfoList() {
|
||||
return display_info_list_;
|
||||
}
|
||||
|
||||
bool ScreenCapturerDrm::DiscoverOutputs() { return false; }
|
||||
|
||||
void ScreenCapturerDrm::CloseDevices() {}
|
||||
|
||||
void ScreenCapturerDrm::CaptureLoop() {}
|
||||
|
||||
bool ScreenCapturerDrm::CaptureOutputFrame(
|
||||
[[maybe_unused]] const DrmOutput& output,
|
||||
[[maybe_unused]] bool emit_callback) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ScreenCapturerDrm::MapFramebuffer([[maybe_unused]] int fd,
|
||||
[[maybe_unused]] uint32_t handle,
|
||||
[[maybe_unused]] size_t map_size,
|
||||
[[maybe_unused]] uint8_t** mapped_ptr,
|
||||
[[maybe_unused]] size_t* mapped_size,
|
||||
[[maybe_unused]] int* prime_fd) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
void ScreenCapturerDrm::UnmapFramebuffer([[maybe_unused]] uint8_t* mapped_ptr,
|
||||
[[maybe_unused]] size_t mapped_size,
|
||||
[[maybe_unused]] int prime_fd) const {}
|
||||
|
||||
} // namespace crossdesk
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2026-03-22
|
||||
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _SCREEN_CAPTURER_DRM_H_
|
||||
#define _SCREEN_CAPTURER_DRM_H_
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "screen_capturer.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
class ScreenCapturerDrm : public ScreenCapturer {
|
||||
public:
|
||||
ScreenCapturerDrm();
|
||||
~ScreenCapturerDrm();
|
||||
|
||||
public:
|
||||
int Init(const int fps, cb_desktop_data cb) override;
|
||||
int Destroy() override;
|
||||
int Start(bool show_cursor) override;
|
||||
int Stop() override;
|
||||
|
||||
int Pause(int monitor_index) override;
|
||||
int Resume(int monitor_index) override;
|
||||
|
||||
int SwitchTo(int monitor_index) override;
|
||||
int ResetToInitialMonitor() override;
|
||||
|
||||
std::vector<DisplayInfo> GetDisplayInfoList() override;
|
||||
|
||||
private:
|
||||
struct DrmDevice {
|
||||
int fd = -1;
|
||||
std::string path;
|
||||
};
|
||||
|
||||
struct DrmOutput {
|
||||
int device_index = -1;
|
||||
uint32_t connector_id = 0;
|
||||
uint32_t crtc_id = 0;
|
||||
std::string name;
|
||||
std::string stream_id;
|
||||
int left = 0;
|
||||
int top = 0;
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
};
|
||||
|
||||
private:
|
||||
bool DiscoverOutputs();
|
||||
void CloseDevices();
|
||||
void CaptureLoop();
|
||||
bool CaptureOutputFrame(const DrmOutput& output, bool emit_callback = true);
|
||||
bool MapFramebuffer(int fd, uint32_t handle, size_t map_size,
|
||||
uint8_t** mapped_ptr, size_t* mapped_size,
|
||||
int* prime_fd) const;
|
||||
void UnmapFramebuffer(uint8_t* mapped_ptr, size_t mapped_size,
|
||||
int prime_fd) const;
|
||||
|
||||
private:
|
||||
std::vector<DrmDevice> devices_;
|
||||
std::vector<DrmOutput> outputs_;
|
||||
std::vector<DisplayInfo> display_info_list_;
|
||||
std::thread thread_;
|
||||
std::atomic<bool> running_{false};
|
||||
std::atomic<bool> paused_{false};
|
||||
std::atomic<int> monitor_index_{0};
|
||||
int initial_monitor_index_ = 0;
|
||||
std::atomic<bool> show_cursor_{true};
|
||||
int fps_ = 60;
|
||||
cb_desktop_data callback_;
|
||||
int consecutive_failures_ = 0;
|
||||
|
||||
std::vector<uint8_t> y_plane_;
|
||||
std::vector<uint8_t> uv_plane_;
|
||||
};
|
||||
|
||||
} // namespace crossdesk
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
#include "screen_capturer_factory.h"
|
||||
|
||||
#include "screen_capturer_linux.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
ScreenCapturer* ScreenCapturerFactory::Create() {
|
||||
return new ScreenCapturerLinux();
|
||||
}
|
||||
|
||||
} // namespace crossdesk
|
||||
@@ -0,0 +1,576 @@
|
||||
#include "screen_capturer_linux.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include <display_stream_id.h>
|
||||
#include "platform.h"
|
||||
#include "rd_log.h"
|
||||
#if defined(CROSSDESK_HAS_DRM) && CROSSDESK_HAS_DRM
|
||||
#include "screen_capturer_drm.h"
|
||||
#endif
|
||||
#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER
|
||||
#include "screen_capturer_wayland.h"
|
||||
#endif
|
||||
#include "screen_capturer_x11.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
namespace {
|
||||
|
||||
#if defined(CROSSDESK_HAS_DRM) && CROSSDESK_HAS_DRM
|
||||
constexpr bool kDrmBuildEnabled = true;
|
||||
#else
|
||||
constexpr bool kDrmBuildEnabled = false;
|
||||
#endif
|
||||
|
||||
#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER
|
||||
constexpr bool kWaylandBuildEnabled = true;
|
||||
#else
|
||||
constexpr bool kWaylandBuildEnabled = false;
|
||||
#endif
|
||||
|
||||
} // namespace
|
||||
|
||||
ScreenCapturerLinux::ScreenCapturerLinux() {}
|
||||
|
||||
ScreenCapturerLinux::~ScreenCapturerLinux() { Destroy(); }
|
||||
|
||||
int ScreenCapturerLinux::Init(const int fps, cb_desktop_data cb) {
|
||||
Destroy();
|
||||
|
||||
if (!cb) {
|
||||
LOG_ERROR("Linux screen capturer callback is null");
|
||||
return -1;
|
||||
}
|
||||
|
||||
fps_ = fps;
|
||||
callback_orig_ = std::move(cb);
|
||||
callback_ = [this](unsigned char* data, int size, int width, int height,
|
||||
const char* reported_stream_id) {
|
||||
const std::string mapped_stream_id = MapStreamId(reported_stream_id);
|
||||
if (mapped_stream_id.empty()) {
|
||||
if (!invalid_stream_id_logged_.exchange(true,
|
||||
std::memory_order_relaxed)) {
|
||||
LOG_WARN("Linux capturer dropping frame without a registered stream "
|
||||
"id: reported='{}', size={}x{}, bytes={}",
|
||||
reported_stream_id ? reported_stream_id : "", width, height,
|
||||
size);
|
||||
}
|
||||
return;
|
||||
}
|
||||
invalid_stream_id_logged_.store(false, std::memory_order_relaxed);
|
||||
if (callback_orig_) {
|
||||
callback_orig_(data, size, width, height, mapped_stream_id.c_str());
|
||||
}
|
||||
};
|
||||
|
||||
const char* force_backend = getenv("CROSSDESK_SCREEN_BACKEND");
|
||||
if (force_backend && force_backend[0] != '\0') {
|
||||
if (strcmp(force_backend, "drm") == 0) {
|
||||
#if defined(CROSSDESK_HAS_DRM) && CROSSDESK_HAS_DRM
|
||||
LOG_INFO("Linux screen capturer forced backend: DRM");
|
||||
return InitDrm();
|
||||
#else
|
||||
LOG_ERROR(
|
||||
"Linux screen capturer forced backend DRM is disabled at build time");
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
if (strcmp(force_backend, "x11") == 0) {
|
||||
LOG_INFO("Linux screen capturer forced backend: X11");
|
||||
return InitX11();
|
||||
}
|
||||
if (strcmp(force_backend, "wayland") == 0) {
|
||||
#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER
|
||||
LOG_INFO("Linux screen capturer forced backend: Wayland");
|
||||
return InitWayland();
|
||||
#else
|
||||
LOG_ERROR(
|
||||
"Linux screen capturer forced backend Wayland is disabled at build "
|
||||
"time");
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
LOG_WARN("Unknown CROSSDESK_SCREEN_BACKEND={}, using auto strategy",
|
||||
force_backend);
|
||||
}
|
||||
|
||||
const bool wayland_session = IsWaylandSession();
|
||||
if (wayland_session) {
|
||||
if (kDrmBuildEnabled) {
|
||||
LOG_INFO("Wayland session detected, prefer DRM -> X11 -> Wayland");
|
||||
if (InitDrm() == 0) {
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
LOG_INFO("Wayland session detected, DRM disabled, prefer X11 -> Wayland");
|
||||
}
|
||||
|
||||
if (InitX11() == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (kDrmBuildEnabled) {
|
||||
LOG_WARN(
|
||||
"DRM and X11 init failed in Wayland session, trying Wayland portal");
|
||||
} else {
|
||||
LOG_WARN("X11 init failed in Wayland session, trying Wayland portal");
|
||||
}
|
||||
if (kWaylandBuildEnabled) {
|
||||
return InitWayland();
|
||||
}
|
||||
LOG_ERROR("Wayland session detected but Wayland backend is disabled");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (InitX11() == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (kDrmBuildEnabled) {
|
||||
LOG_WARN("X11 init failed, trying DRM fallback");
|
||||
return InitDrm();
|
||||
}
|
||||
|
||||
LOG_ERROR("X11 init failed and DRM backend is disabled");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ScreenCapturerLinux::Destroy() {
|
||||
if (impl_) {
|
||||
impl_->Destroy();
|
||||
impl_.reset();
|
||||
}
|
||||
|
||||
backend_ = BackendType::kNone;
|
||||
callback_ = nullptr;
|
||||
callback_orig_ = nullptr;
|
||||
current_monitor_index_.store(0, std::memory_order_relaxed);
|
||||
invalid_stream_id_logged_.store(false, std::memory_order_relaxed);
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(alias_mutex_);
|
||||
canonical_displays_.clear();
|
||||
stream_id_alias_.clear();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerLinux::Start(bool show_cursor) {
|
||||
if (!impl_) {
|
||||
LOG_ERROR("Linux screen capturer backend is not initialized");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// X11 output names, DRM connectors and Wayland stream handles may all change
|
||||
// after an HDMI hotplug, so rebuild the current backend before each session.
|
||||
const int refresh_ret = RefreshCurrentBackend();
|
||||
if (refresh_ret != 0) {
|
||||
LOG_WARN("Linux screen capturer backend refresh failed: {}", refresh_ret);
|
||||
}
|
||||
|
||||
const int ret = impl_->Start(show_cursor);
|
||||
if (ret == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char* backend_name = "None";
|
||||
if (backend_ == BackendType::kX11) {
|
||||
backend_name = "X11";
|
||||
} else if (backend_ == BackendType::kDrm) {
|
||||
backend_name = "DRM";
|
||||
} else if (backend_ == BackendType::kWayland) {
|
||||
backend_name = "Wayland";
|
||||
}
|
||||
|
||||
LOG_WARN("Linux screen capturer backend {} start failed: {}", backend_name,
|
||||
ret);
|
||||
|
||||
if (backend_ == BackendType::kX11 && kDrmBuildEnabled &&
|
||||
TryFallbackToDrm(show_cursor)) {
|
||||
return 0;
|
||||
}
|
||||
if (backend_ == BackendType::kX11 && kWaylandBuildEnabled &&
|
||||
TryFallbackToWayland(show_cursor)) {
|
||||
return 0;
|
||||
}
|
||||
if (backend_ == BackendType::kDrm && kDrmBuildEnabled) {
|
||||
if (TryFallbackToX11(show_cursor)) {
|
||||
return 0;
|
||||
}
|
||||
if (kWaylandBuildEnabled && TryFallbackToWayland(show_cursor)) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (backend_ == BackendType::kWayland && kWaylandBuildEnabled) {
|
||||
if (kDrmBuildEnabled && TryFallbackToDrm(show_cursor)) {
|
||||
return 0;
|
||||
}
|
||||
if (TryFallbackToX11(show_cursor)) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ScreenCapturerLinux::Stop() {
|
||||
if (!impl_) {
|
||||
return 0;
|
||||
}
|
||||
const int ret = impl_->Stop();
|
||||
UpdateAliasesFromBackend(impl_.get());
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ScreenCapturerLinux::Pause(int monitor_index) {
|
||||
if (!impl_) {
|
||||
return -1;
|
||||
}
|
||||
return impl_->Pause(monitor_index);
|
||||
}
|
||||
|
||||
int ScreenCapturerLinux::Resume(int monitor_index) {
|
||||
if (!impl_) {
|
||||
return -1;
|
||||
}
|
||||
return impl_->Resume(monitor_index);
|
||||
}
|
||||
|
||||
int ScreenCapturerLinux::SwitchTo(int monitor_index) {
|
||||
if (!impl_) {
|
||||
return -1;
|
||||
}
|
||||
const int ret = impl_->SwitchTo(monitor_index);
|
||||
if (ret == 0) {
|
||||
current_monitor_index_.store(monitor_index, std::memory_order_relaxed);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ScreenCapturerLinux::ResetToInitialMonitor() {
|
||||
if (!impl_) {
|
||||
return -1;
|
||||
}
|
||||
const int ret = impl_->ResetToInitialMonitor();
|
||||
if (ret == 0) {
|
||||
current_monitor_index_.store(initial_monitor_index_,
|
||||
std::memory_order_relaxed);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::vector<DisplayInfo> ScreenCapturerLinux::GetDisplayInfoList() {
|
||||
if (!impl_) {
|
||||
return std::vector<DisplayInfo>();
|
||||
}
|
||||
|
||||
// Wayland backend may update display geometry/stream handle asynchronously
|
||||
// after Start(). Refresh aliases every time to keep canonical displays fresh.
|
||||
UpdateAliasesFromBackend(impl_.get());
|
||||
|
||||
std::lock_guard<std::mutex> lock(alias_mutex_);
|
||||
if (!canonical_displays_.empty()) {
|
||||
return canonical_displays_;
|
||||
}
|
||||
|
||||
return impl_->GetDisplayInfoList();
|
||||
}
|
||||
|
||||
int ScreenCapturerLinux::InitX11() {
|
||||
auto backend = std::make_unique<ScreenCapturerX11>();
|
||||
const int ret = backend->Init(fps_, callback_);
|
||||
if (ret != 0) {
|
||||
backend->Destroy();
|
||||
LOG_WARN("Linux screen capturer X11 init failed: {}", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
UpdateAliasesFromBackend(backend.get());
|
||||
impl_ = std::move(backend);
|
||||
backend_ = BackendType::kX11;
|
||||
LOG_INFO("Linux screen capturer backend selected: X11");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerLinux::InitDrm() {
|
||||
#if defined(CROSSDESK_HAS_DRM) && CROSSDESK_HAS_DRM
|
||||
auto backend = std::make_unique<ScreenCapturerDrm>();
|
||||
const int ret = backend->Init(fps_, callback_);
|
||||
if (ret != 0) {
|
||||
backend->Destroy();
|
||||
LOG_WARN("Linux screen capturer DRM init failed: {}", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
UpdateAliasesFromBackend(backend.get());
|
||||
impl_ = std::move(backend);
|
||||
backend_ = BackendType::kDrm;
|
||||
LOG_INFO("Linux screen capturer backend selected: DRM");
|
||||
return 0;
|
||||
#else
|
||||
LOG_WARN("Linux screen capturer DRM backend is disabled at build time");
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
int ScreenCapturerLinux::InitWayland() {
|
||||
#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER
|
||||
auto backend = std::make_unique<ScreenCapturerWayland>();
|
||||
const int ret = backend->Init(fps_, callback_);
|
||||
if (ret != 0) {
|
||||
backend->Destroy();
|
||||
LOG_WARN("Linux screen capturer Wayland init failed: {}", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
UpdateAliasesFromBackend(backend.get());
|
||||
impl_ = std::move(backend);
|
||||
backend_ = BackendType::kWayland;
|
||||
LOG_INFO("Linux screen capturer backend selected: Wayland");
|
||||
return 0;
|
||||
#else
|
||||
LOG_WARN("Linux screen capturer Wayland backend is disabled at build time");
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
int ScreenCapturerLinux::RefreshCurrentBackend() {
|
||||
std::unique_ptr<ScreenCapturer> backend;
|
||||
const char* backend_name = "unknown";
|
||||
switch (backend_) {
|
||||
case BackendType::kX11:
|
||||
backend = std::make_unique<ScreenCapturerX11>();
|
||||
backend_name = "X11";
|
||||
break;
|
||||
case BackendType::kDrm:
|
||||
#if defined(CROSSDESK_HAS_DRM) && CROSSDESK_HAS_DRM
|
||||
backend = std::make_unique<ScreenCapturerDrm>();
|
||||
backend_name = "DRM";
|
||||
break;
|
||||
#else
|
||||
return -1;
|
||||
#endif
|
||||
case BackendType::kWayland:
|
||||
#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER
|
||||
backend = std::make_unique<ScreenCapturerWayland>();
|
||||
backend_name = "Wayland";
|
||||
break;
|
||||
#else
|
||||
return -1;
|
||||
#endif
|
||||
case BackendType::kNone:
|
||||
return -1;
|
||||
}
|
||||
|
||||
const int ret = backend->Init(fps_, callback_);
|
||||
if (ret != 0) {
|
||||
backend->Destroy();
|
||||
return ret;
|
||||
}
|
||||
|
||||
const int requested_monitor =
|
||||
current_monitor_index_.load(std::memory_order_relaxed);
|
||||
UpdateAliasesFromBackend(backend.get());
|
||||
if (requested_monitor > 0 && backend->SwitchTo(requested_monitor) != 0) {
|
||||
current_monitor_index_.store(0, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
if (impl_) {
|
||||
impl_->Destroy();
|
||||
}
|
||||
|
||||
impl_ = std::move(backend);
|
||||
LOG_INFO("Linux screen capturer {} backend refreshed before start",
|
||||
backend_name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool ScreenCapturerLinux::TryFallbackToDrm(bool show_cursor) {
|
||||
#if defined(CROSSDESK_HAS_DRM) && CROSSDESK_HAS_DRM
|
||||
auto drm_backend = std::make_unique<ScreenCapturerDrm>();
|
||||
int ret = drm_backend->Init(fps_, callback_);
|
||||
if (ret != 0) {
|
||||
LOG_ERROR("Linux screen capturer fallback DRM init failed: {}", ret);
|
||||
return false;
|
||||
}
|
||||
|
||||
UpdateAliasesFromBackend(drm_backend.get());
|
||||
ret = drm_backend->Start(show_cursor);
|
||||
if (ret != 0) {
|
||||
drm_backend->Destroy();
|
||||
LOG_ERROR("Linux screen capturer fallback DRM start failed: {}", ret);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (impl_) {
|
||||
impl_->Stop();
|
||||
impl_->Destroy();
|
||||
}
|
||||
|
||||
impl_ = std::move(drm_backend);
|
||||
backend_ = BackendType::kDrm;
|
||||
LOG_INFO("Linux screen capturer fallback switched to DRM");
|
||||
return true;
|
||||
#else
|
||||
(void)show_cursor;
|
||||
LOG_WARN("Linux screen capturer DRM fallback is disabled at build time");
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool ScreenCapturerLinux::TryFallbackToX11(bool show_cursor) {
|
||||
auto x11_backend = std::make_unique<ScreenCapturerX11>();
|
||||
int ret = x11_backend->Init(fps_, callback_);
|
||||
if (ret != 0) {
|
||||
LOG_ERROR("Linux screen capturer fallback X11 init failed: {}", ret);
|
||||
return false;
|
||||
}
|
||||
|
||||
UpdateAliasesFromBackend(x11_backend.get());
|
||||
ret = x11_backend->Start(show_cursor);
|
||||
if (ret != 0) {
|
||||
x11_backend->Destroy();
|
||||
LOG_ERROR("Linux screen capturer fallback X11 start failed: {}", ret);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (impl_) {
|
||||
impl_->Stop();
|
||||
impl_->Destroy();
|
||||
}
|
||||
|
||||
impl_ = std::move(x11_backend);
|
||||
backend_ = BackendType::kX11;
|
||||
LOG_INFO("Linux screen capturer fallback switched to X11");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ScreenCapturerLinux::TryFallbackToWayland(bool show_cursor) {
|
||||
#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER
|
||||
auto wayland_backend = std::make_unique<ScreenCapturerWayland>();
|
||||
int ret = wayland_backend->Init(fps_, callback_);
|
||||
if (ret != 0) {
|
||||
LOG_ERROR("Linux screen capturer fallback Wayland init failed: {}", ret);
|
||||
return false;
|
||||
}
|
||||
|
||||
UpdateAliasesFromBackend(wayland_backend.get());
|
||||
ret = wayland_backend->Start(show_cursor);
|
||||
if (ret != 0) {
|
||||
wayland_backend->Destroy();
|
||||
LOG_ERROR("Linux screen capturer fallback Wayland start failed: {}", ret);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (impl_) {
|
||||
impl_->Stop();
|
||||
impl_->Destroy();
|
||||
}
|
||||
|
||||
impl_ = std::move(wayland_backend);
|
||||
backend_ = BackendType::kWayland;
|
||||
LOG_INFO("Linux screen capturer fallback switched to Wayland");
|
||||
return true;
|
||||
#else
|
||||
(void)show_cursor;
|
||||
LOG_WARN("Linux screen capturer Wayland fallback is disabled at build time");
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
void ScreenCapturerLinux::UpdateAliasesFromBackend(ScreenCapturer* backend) {
|
||||
if (!backend) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto backend_displays = backend->GetDisplayInfoList();
|
||||
if (backend_displays.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(alias_mutex_);
|
||||
stream_id_alias_.clear();
|
||||
|
||||
if (canonical_displays_.empty()) {
|
||||
canonical_displays_ = backend_displays;
|
||||
for (size_t i = 0; i < backend_displays.size(); ++i) {
|
||||
auto& canonical = canonical_displays_[i];
|
||||
const std::string stream_id = MakeDisplayStreamId(i);
|
||||
if (canonical.name.empty()) {
|
||||
canonical.name = stream_id;
|
||||
}
|
||||
stream_id_alias_[stream_id] = stream_id;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (canonical_displays_.size() < backend_displays.size()) {
|
||||
LOG_WARN("Linux capturer detected {} additional display(s); they remain "
|
||||
"unavailable until peer streams are reinitialized",
|
||||
backend_displays.size() - canonical_displays_.size());
|
||||
}
|
||||
|
||||
auto similar = [](const DisplayInfo& current, const DisplayInfo& canonical) {
|
||||
return std::abs(current.left - canonical.left) <= 10 &&
|
||||
std::abs(current.top - canonical.top) <= 10 &&
|
||||
std::abs(current.width - canonical.width) <= 20 &&
|
||||
std::abs(current.height - canonical.height) <= 20;
|
||||
};
|
||||
std::vector<bool> used(canonical_displays_.size(), false);
|
||||
for (size_t current_index = 0; current_index < backend_displays.size();
|
||||
++current_index) {
|
||||
const auto& backend_display = backend_displays[current_index];
|
||||
int canonical_index = -1;
|
||||
for (size_t i = 0; i < canonical_displays_.size(); ++i) {
|
||||
if (!used[i] && similar(backend_display, canonical_displays_[i])) {
|
||||
canonical_index = static_cast<int>(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (canonical_index < 0 && current_index < canonical_displays_.size() &&
|
||||
!used[current_index]) {
|
||||
canonical_index = static_cast<int>(current_index);
|
||||
}
|
||||
if (canonical_index < 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
used[canonical_index] = true;
|
||||
const std::string backend_stream_id =
|
||||
MakeDisplayStreamId(current_index);
|
||||
const std::string stable_stream_id =
|
||||
MakeDisplayStreamId(static_cast<size_t>(canonical_index));
|
||||
stream_id_alias_[backend_stream_id] = stable_stream_id;
|
||||
|
||||
// Refresh the user-visible label and geometry. The MiniRTC stream ID is
|
||||
// derived independently from this stable logical index.
|
||||
canonical_displays_[canonical_index] = backend_display;
|
||||
if (canonical_displays_[canonical_index].name.empty()) {
|
||||
canonical_displays_[canonical_index].name = stable_stream_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string ScreenCapturerLinux::MapStreamId(
|
||||
const char* reported_stream_id) const {
|
||||
std::string input_id = reported_stream_id ? reported_stream_id : "";
|
||||
std::lock_guard<std::mutex> lock(alias_mutex_);
|
||||
auto it = stream_id_alias_.find(input_id);
|
||||
if (it != stream_id_alias_.end()) {
|
||||
input_id = it->second;
|
||||
} else {
|
||||
input_id.clear();
|
||||
}
|
||||
|
||||
return ResolveDisplayStreamId(
|
||||
input_id.c_str(), canonical_displays_.size(),
|
||||
current_monitor_index_.load(std::memory_order_relaxed));
|
||||
}
|
||||
|
||||
} // namespace crossdesk
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2026-03-22
|
||||
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _SCREEN_CAPTURER_LINUX_H_
|
||||
#define _SCREEN_CAPTURER_LINUX_H_
|
||||
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "screen_capturer.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
class ScreenCapturerLinux : public ScreenCapturer {
|
||||
public:
|
||||
ScreenCapturerLinux();
|
||||
~ScreenCapturerLinux();
|
||||
|
||||
public:
|
||||
int Init(const int fps, cb_desktop_data cb) override;
|
||||
int Destroy() override;
|
||||
int Start(bool show_cursor) override;
|
||||
int Stop() override;
|
||||
|
||||
int Pause(int monitor_index) override;
|
||||
int Resume(int monitor_index) override;
|
||||
|
||||
int SwitchTo(int monitor_index) override;
|
||||
int ResetToInitialMonitor() override;
|
||||
|
||||
std::vector<DisplayInfo> GetDisplayInfoList() override;
|
||||
|
||||
private:
|
||||
enum class BackendType { kNone, kX11, kDrm, kWayland };
|
||||
|
||||
private:
|
||||
int InitX11();
|
||||
int InitDrm();
|
||||
int InitWayland();
|
||||
int RefreshCurrentBackend();
|
||||
bool TryFallbackToDrm(bool show_cursor);
|
||||
bool TryFallbackToX11(bool show_cursor);
|
||||
bool TryFallbackToWayland(bool show_cursor);
|
||||
void UpdateAliasesFromBackend(ScreenCapturer* backend);
|
||||
std::string MapStreamId(const char* reported_stream_id) const;
|
||||
|
||||
private:
|
||||
std::unique_ptr<ScreenCapturer> impl_;
|
||||
BackendType backend_ = BackendType::kNone;
|
||||
int fps_ = 60;
|
||||
cb_desktop_data callback_;
|
||||
cb_desktop_data callback_orig_;
|
||||
std::vector<DisplayInfo> canonical_displays_;
|
||||
mutable std::mutex alias_mutex_;
|
||||
std::unordered_map<std::string, std::string> stream_id_alias_;
|
||||
std::atomic<int> current_monitor_index_{0};
|
||||
int initial_monitor_index_ = 0;
|
||||
mutable std::atomic<bool> invalid_stream_id_logged_{false};
|
||||
};
|
||||
|
||||
} // namespace crossdesk
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,265 @@
|
||||
#include "screen_capturer_wayland.h"
|
||||
|
||||
#include "screen_capturer_wayland_build.h"
|
||||
|
||||
#if !CROSSDESK_WAYLAND_BUILD_ENABLED
|
||||
#error \
|
||||
"Wayland capturer requires USE_WAYLAND=true and Wayland development headers"
|
||||
#endif
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <thread>
|
||||
|
||||
#include "linux_cursor_shape.h"
|
||||
#include "platform.h"
|
||||
#include "rd_log.h"
|
||||
#include "shared_cursor_state.h"
|
||||
#include "wayland_portal_shared.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
namespace {
|
||||
|
||||
int64_t NowMs() {
|
||||
return std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::steady_clock::now().time_since_epoch())
|
||||
.count();
|
||||
}
|
||||
|
||||
struct PipeWireRecoveryConfig {
|
||||
ScreenCapturerWayland::PipeWireConnectMode mode;
|
||||
bool relaxed_connect = false;
|
||||
};
|
||||
|
||||
constexpr auto kPipeWireCloseSettleDelay = std::chrono::milliseconds(200);
|
||||
|
||||
} // namespace
|
||||
|
||||
ScreenCapturerWayland::ScreenCapturerWayland()
|
||||
: cursor_theme_matcher_(std::make_unique<LinuxCursorThemeMatcher>()) {}
|
||||
|
||||
ScreenCapturerWayland::~ScreenCapturerWayland() { Destroy(); }
|
||||
|
||||
int ScreenCapturerWayland::Init(const int fps, cb_desktop_data cb) {
|
||||
Destroy();
|
||||
|
||||
if (!IsWaylandSession()) {
|
||||
LOG_ERROR("Wayland screen capturer requires a Wayland session");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!cb) {
|
||||
LOG_ERROR("Wayland screen capturer callback is null");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!CheckPortalAvailability()) {
|
||||
LOG_ERROR("xdg-desktop-portal screencast service is unavailable");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!EnsurePipeWireRuntimeAvailable()) {
|
||||
LOG_ERROR("Wayland screen capturer requires PipeWire 0.3 runtime");
|
||||
return -1;
|
||||
}
|
||||
|
||||
fps_ = fps;
|
||||
callback_ = cb;
|
||||
pointer_granted_ = false;
|
||||
shared_session_registered_ = false;
|
||||
cursor_metadata_enabled_ = false;
|
||||
cursor_metadata_seen_ = false;
|
||||
cursor_metadata_missing_buffers_ = 0;
|
||||
ClearSharedCursorState();
|
||||
display_info_list_.clear();
|
||||
display_info_list_.push_back(
|
||||
DisplayInfo(display_name_, 0, 0, kFallbackWidth, kFallbackHeight));
|
||||
monitor_index_ = 0;
|
||||
initial_monitor_index_ = 0;
|
||||
frame_width_ = kFallbackWidth;
|
||||
frame_height_ = kFallbackHeight;
|
||||
frame_stride_ = kFallbackWidth * 4;
|
||||
portal_has_logical_size_ = false;
|
||||
portal_stream_width_ = 0;
|
||||
portal_stream_height_ = 0;
|
||||
logical_width_ = kFallbackWidth;
|
||||
logical_height_ = kFallbackHeight;
|
||||
y_plane_.resize(kFallbackWidth * kFallbackHeight);
|
||||
uv_plane_.resize((kFallbackWidth / 2) * (kFallbackHeight / 2) * 2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerWayland::Destroy() {
|
||||
Stop();
|
||||
y_plane_.clear();
|
||||
uv_plane_.clear();
|
||||
display_info_list_.clear();
|
||||
callback_ = nullptr;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerWayland::Start(bool show_cursor) {
|
||||
if (running_) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
show_cursor_ = show_cursor;
|
||||
cursor_metadata_enabled_ = false;
|
||||
cursor_metadata_seen_ = false;
|
||||
cursor_metadata_missing_buffers_ = 0;
|
||||
ClearSharedCursorState();
|
||||
paused_ = false;
|
||||
pipewire_node_id_ = 0;
|
||||
UpdateDisplayGeometry(
|
||||
logical_width_ > 0 ? logical_width_ : kFallbackWidth,
|
||||
logical_height_ > 0 ? logical_height_ : kFallbackHeight);
|
||||
pipewire_format_ready_.store(false);
|
||||
pipewire_stream_start_ms_.store(0);
|
||||
pipewire_last_frame_ms_.store(0);
|
||||
running_ = true;
|
||||
thread_ = std::thread([this]() { Run(); });
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerWayland::Stop() {
|
||||
running_ = false;
|
||||
if (thread_.joinable()) {
|
||||
thread_.join();
|
||||
}
|
||||
pipewire_node_id_ = 0;
|
||||
cursor_metadata_enabled_ = false;
|
||||
cursor_metadata_seen_ = false;
|
||||
cursor_metadata_missing_buffers_ = 0;
|
||||
ClearSharedCursorState();
|
||||
UpdateDisplayGeometry(
|
||||
logical_width_ > 0 ? logical_width_ : kFallbackWidth,
|
||||
logical_height_ > 0 ? logical_height_ : kFallbackHeight);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerWayland::Pause([[maybe_unused]] int monitor_index) {
|
||||
paused_ = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerWayland::Resume([[maybe_unused]] int monitor_index) {
|
||||
paused_ = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerWayland::SwitchTo(int monitor_index) {
|
||||
if (monitor_index != 0) {
|
||||
LOG_WARN("Wayland screencast currently supports one logical display");
|
||||
return -1;
|
||||
}
|
||||
|
||||
monitor_index_ = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerWayland::ResetToInitialMonitor() {
|
||||
monitor_index_ = initial_monitor_index_;
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::vector<DisplayInfo> ScreenCapturerWayland::GetDisplayInfoList() {
|
||||
return display_info_list_;
|
||||
}
|
||||
|
||||
void ScreenCapturerWayland::Run() {
|
||||
static constexpr PipeWireRecoveryConfig kRecoveryConfigs[] = {
|
||||
{PipeWireConnectMode::kTargetObject, false},
|
||||
{PipeWireConnectMode::kAny, true},
|
||||
{PipeWireConnectMode::kNodeId, false},
|
||||
{PipeWireConnectMode::kNodeId, true},
|
||||
};
|
||||
|
||||
int recovery_index = 0;
|
||||
auto setup_pipewire = [this, &recovery_index]() -> bool {
|
||||
const auto& config = kRecoveryConfigs[recovery_index];
|
||||
return OpenPipeWireRemote() &&
|
||||
SetupPipeWireStream(config.relaxed_connect, config.mode);
|
||||
};
|
||||
auto setup_pipeline = [this, &setup_pipewire]() -> bool {
|
||||
return ConnectSessionBus() && CreatePortalSession() &&
|
||||
SelectPortalDevices() && SelectPortalSource() &&
|
||||
StartPortalSession() && setup_pipewire();
|
||||
};
|
||||
|
||||
if (!setup_pipeline()) {
|
||||
running_ = false;
|
||||
CleanupPipeWire();
|
||||
ClosePortalSession();
|
||||
CleanupDbus();
|
||||
return;
|
||||
}
|
||||
while (running_) {
|
||||
if (!paused_) {
|
||||
const int64_t now = NowMs();
|
||||
const int64_t stream_start = pipewire_stream_start_ms_.load();
|
||||
const int64_t last_frame = pipewire_last_frame_ms_.load();
|
||||
const bool format_ready = pipewire_format_ready_.load();
|
||||
|
||||
const bool format_timeout =
|
||||
stream_start > 0 && !format_ready && (now - stream_start) > 1200;
|
||||
const bool first_frame_timeout = stream_start > 0 && format_ready &&
|
||||
last_frame == 0 &&
|
||||
(now - stream_start) > 4000;
|
||||
const bool frame_stall = last_frame > 0 && (now - last_frame) > 5000;
|
||||
|
||||
if (format_timeout || first_frame_timeout || frame_stall) {
|
||||
if (recovery_index + 1 >=
|
||||
static_cast<int>(sizeof(kRecoveryConfigs) /
|
||||
sizeof(kRecoveryConfigs[0]))) {
|
||||
LOG_ERROR(
|
||||
"Wayland capture stalled and recovery limit reached, "
|
||||
"format_ready={}, stream_start={}, last_frame={}, attempts={}",
|
||||
format_ready, stream_start, last_frame, recovery_index);
|
||||
running_ = false;
|
||||
break;
|
||||
}
|
||||
|
||||
++recovery_index;
|
||||
const char* reason =
|
||||
format_timeout
|
||||
? "format-timeout"
|
||||
: (first_frame_timeout ? "first-frame-timeout" : "frame-stall");
|
||||
const auto& config = kRecoveryConfigs[recovery_index];
|
||||
LOG_WARN(
|
||||
"Wayland capture stalled ({}) - retrying PipeWire only, "
|
||||
"attempt {}/{}, mode={}, relaxed_connect={}",
|
||||
reason, recovery_index,
|
||||
static_cast<int>(sizeof(kRecoveryConfigs) /
|
||||
sizeof(kRecoveryConfigs[0])) -
|
||||
1,
|
||||
config.mode == PipeWireConnectMode::kTargetObject
|
||||
? "target-object"
|
||||
: (config.mode == PipeWireConnectMode::kNodeId ? "node-id"
|
||||
: "any"),
|
||||
config.relaxed_connect);
|
||||
|
||||
CleanupPipeWire();
|
||||
if (!setup_pipewire()) {
|
||||
LOG_ERROR("Wayland PipeWire-only recovery failed at attempt {}",
|
||||
recovery_index);
|
||||
running_ = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
}
|
||||
|
||||
CleanupPipeWire();
|
||||
if (!session_handle_.empty()) {
|
||||
std::this_thread::sleep_for(kPipeWireCloseSettleDelay);
|
||||
}
|
||||
ClosePortalSession();
|
||||
CleanupDbus();
|
||||
}
|
||||
|
||||
} // namespace crossdesk
|
||||
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2026-03-22
|
||||
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _SCREEN_CAPTURER_WAYLAND_H_
|
||||
#define _SCREEN_CAPTURER_WAYLAND_H_
|
||||
|
||||
struct DBusConnection;
|
||||
struct pw_context;
|
||||
struct pw_core;
|
||||
struct pw_stream;
|
||||
struct pw_thread_loop;
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "screen_capturer.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
class LinuxCursorThemeMatcher;
|
||||
|
||||
class ScreenCapturerWayland : public ScreenCapturer {
|
||||
public:
|
||||
enum class PipeWireConnectMode { kTargetObject, kNodeId, kAny };
|
||||
|
||||
public:
|
||||
ScreenCapturerWayland();
|
||||
~ScreenCapturerWayland();
|
||||
|
||||
public:
|
||||
int Init(const int fps, cb_desktop_data cb) override;
|
||||
int Destroy() override;
|
||||
int Start(bool show_cursor) override;
|
||||
int Stop() override;
|
||||
|
||||
int Pause(int monitor_index) override;
|
||||
int Resume(int monitor_index) override;
|
||||
|
||||
int SwitchTo(int monitor_index) override;
|
||||
int ResetToInitialMonitor() override;
|
||||
|
||||
std::vector<DisplayInfo> GetDisplayInfoList() override;
|
||||
|
||||
private:
|
||||
bool CheckPortalAvailability() const;
|
||||
bool ConnectSessionBus();
|
||||
bool CreatePortalSession();
|
||||
bool SelectPortalDevices();
|
||||
bool SelectPortalSource();
|
||||
bool GetAvailableCursorModes(uint32_t* modes) const;
|
||||
bool StartPortalSession();
|
||||
bool EnsurePipeWireRuntimeAvailable() const;
|
||||
bool OpenPipeWireRemote();
|
||||
bool SetupPipeWireStream(bool relaxed_connect, PipeWireConnectMode mode);
|
||||
|
||||
void Run();
|
||||
void CleanupPipeWire();
|
||||
void CleanupDbus();
|
||||
void ClosePortalSession();
|
||||
void HandlePipeWireBuffer();
|
||||
void UpdateDisplayGeometry(int width, int height);
|
||||
|
||||
private:
|
||||
static constexpr int kFallbackWidth = 1920;
|
||||
static constexpr int kFallbackHeight = 1080;
|
||||
|
||||
std::thread thread_;
|
||||
std::atomic<bool> running_{false};
|
||||
std::atomic<bool> paused_{false};
|
||||
std::atomic<int> monitor_index_{0};
|
||||
std::atomic<bool> pipewire_format_ready_{false};
|
||||
std::atomic<int64_t> pipewire_stream_start_ms_{0};
|
||||
std::atomic<int64_t> pipewire_last_frame_ms_{0};
|
||||
int initial_monitor_index_ = 0;
|
||||
std::atomic<bool> show_cursor_{true};
|
||||
int fps_ = 60;
|
||||
cb_desktop_data callback_ = nullptr;
|
||||
std::vector<DisplayInfo> display_info_list_;
|
||||
|
||||
DBusConnection* dbus_connection_ = nullptr;
|
||||
std::string session_handle_;
|
||||
std::string display_name_ = "WAYLAND0";
|
||||
uint32_t pipewire_node_id_ = 0;
|
||||
int pipewire_fd_ = -1;
|
||||
|
||||
pw_thread_loop* pw_thread_loop_ = nullptr;
|
||||
pw_context* pw_context_ = nullptr;
|
||||
pw_core* pw_core_ = nullptr;
|
||||
pw_stream* pw_stream_ = nullptr;
|
||||
void* stream_listener_ = nullptr;
|
||||
bool pipewire_initialized_ = false;
|
||||
bool pipewire_thread_loop_started_ = false;
|
||||
bool pointer_granted_ = false;
|
||||
bool shared_session_registered_ = false;
|
||||
bool cursor_metadata_enabled_ = false;
|
||||
bool cursor_metadata_seen_ = false;
|
||||
uint32_t cursor_metadata_missing_buffers_ = 0;
|
||||
bool portal_has_logical_size_ = false;
|
||||
uint32_t spa_video_format_ = 0;
|
||||
int frame_width_ = 0;
|
||||
int frame_height_ = 0;
|
||||
int frame_stride_ = 0;
|
||||
int portal_stream_width_ = 0;
|
||||
int portal_stream_height_ = 0;
|
||||
int logical_width_ = 0;
|
||||
int logical_height_ = 0;
|
||||
std::unique_ptr<LinuxCursorThemeMatcher> cursor_theme_matcher_;
|
||||
|
||||
std::vector<uint8_t> y_plane_;
|
||||
std::vector<uint8_t> uv_plane_;
|
||||
};
|
||||
|
||||
} // namespace crossdesk
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2026-03-22
|
||||
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _SCREEN_CAPTURER_WAYLAND_BUILD_H_
|
||||
#define _SCREEN_CAPTURER_WAYLAND_BUILD_H_
|
||||
|
||||
#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER
|
||||
|
||||
#define CROSSDESK_WAYLAND_BUILD_ENABLED 1
|
||||
|
||||
#include <dbus/dbus.h>
|
||||
#include <pipewire/keys.h>
|
||||
#include <pipewire/pipewire.h>
|
||||
#include <pipewire/stream.h>
|
||||
#include <pipewire/thread-loop.h>
|
||||
#include <spa/param/param.h>
|
||||
#include <spa/param/format-utils.h>
|
||||
#include <spa/param/video/format-utils.h>
|
||||
#include <spa/param/video/raw.h>
|
||||
#include <spa/buffer/meta.h>
|
||||
#include <spa/utils/result.h>
|
||||
|
||||
#if defined(__has_include)
|
||||
#if __has_include(<spa/param/buffers.h>)
|
||||
#include <spa/param/buffers.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define CROSSDESK_SPA_PARAM_BUFFERS_BUFFERS 1u
|
||||
#define CROSSDESK_SPA_PARAM_BUFFERS_BLOCKS 2u
|
||||
#define CROSSDESK_SPA_PARAM_BUFFERS_SIZE 3u
|
||||
#define CROSSDESK_SPA_PARAM_BUFFERS_STRIDE 4u
|
||||
|
||||
#define CROSSDESK_SPA_PARAM_META_TYPE 1u
|
||||
#define CROSSDESK_SPA_PARAM_META_SIZE 2u
|
||||
|
||||
#else
|
||||
|
||||
#define CROSSDESK_WAYLAND_BUILD_ENABLED 0
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
+1083
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,911 @@
|
||||
#include "screen_capturer_wayland.h"
|
||||
|
||||
#include <remote_action.h>
|
||||
#include "screen_capturer_wayland_build.h"
|
||||
#include "wayland_portal_shared.h"
|
||||
|
||||
#if CROSSDESK_WAYLAND_BUILD_ENABLED
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
#include "rd_log.h"
|
||||
#include "shared_cursor_state.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr const char* kPortalBusName = "org.freedesktop.portal.Desktop";
|
||||
constexpr const char* kPortalObjectPath = "/org/freedesktop/portal/desktop";
|
||||
constexpr const char* kPortalRemoteDesktopInterface =
|
||||
"org.freedesktop.portal.RemoteDesktop";
|
||||
constexpr const char* kPortalScreenCastInterface =
|
||||
"org.freedesktop.portal.ScreenCast";
|
||||
constexpr const char* kPortalRequestInterface =
|
||||
"org.freedesktop.portal.Request";
|
||||
constexpr const char* kPortalSessionInterface =
|
||||
"org.freedesktop.portal.Session";
|
||||
constexpr const char* kPortalRequestPathPrefix =
|
||||
"/org/freedesktop/portal/desktop/request/";
|
||||
constexpr const char* kPortalSessionPathPrefix =
|
||||
"/org/freedesktop/portal/desktop/session/";
|
||||
|
||||
constexpr uint32_t kScreenCastSourceMonitor = 1u;
|
||||
constexpr uint32_t kCursorModeHidden = 1u;
|
||||
constexpr uint32_t kCursorModeEmbedded = 2u;
|
||||
constexpr uint32_t kCursorModeMetadata = 4u;
|
||||
constexpr uint32_t kRemoteDesktopDevicePointer = 2u;
|
||||
|
||||
std::string MakeToken(const char* prefix) {
|
||||
const auto now = std::chrono::steady_clock::now().time_since_epoch().count();
|
||||
return std::string(prefix) + "_" + std::to_string(now);
|
||||
}
|
||||
|
||||
void LogDbusError(const char* action, DBusError* error) {
|
||||
if (error && dbus_error_is_set(error)) {
|
||||
LOG_ERROR("{} failed: {} ({})", action,
|
||||
error->message ? error->message : "unknown",
|
||||
error->name ? error->name : "unknown");
|
||||
} else {
|
||||
LOG_ERROR("{} failed", action);
|
||||
}
|
||||
}
|
||||
|
||||
void AppendDictEntryString(DBusMessageIter* dict, const char* key,
|
||||
const std::string& value) {
|
||||
DBusMessageIter entry;
|
||||
DBusMessageIter variant;
|
||||
const char* key_cstr = key;
|
||||
const char* value_cstr = value.c_str();
|
||||
|
||||
dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, nullptr, &entry);
|
||||
dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key_cstr);
|
||||
dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, "s", &variant);
|
||||
dbus_message_iter_append_basic(&variant, DBUS_TYPE_STRING, &value_cstr);
|
||||
dbus_message_iter_close_container(&entry, &variant);
|
||||
dbus_message_iter_close_container(dict, &entry);
|
||||
}
|
||||
|
||||
void AppendDictEntryUint32(DBusMessageIter* dict, const char* key,
|
||||
uint32_t value) {
|
||||
DBusMessageIter entry;
|
||||
DBusMessageIter variant;
|
||||
const char* key_cstr = key;
|
||||
|
||||
dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, nullptr, &entry);
|
||||
dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key_cstr);
|
||||
dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, "u", &variant);
|
||||
dbus_message_iter_append_basic(&variant, DBUS_TYPE_UINT32, &value);
|
||||
dbus_message_iter_close_container(&entry, &variant);
|
||||
dbus_message_iter_close_container(dict, &entry);
|
||||
}
|
||||
|
||||
void AppendDictEntryBool(DBusMessageIter* dict, const char* key, bool value) {
|
||||
DBusMessageIter entry;
|
||||
DBusMessageIter variant;
|
||||
const char* key_cstr = key;
|
||||
dbus_bool_t bool_value = value ? TRUE : FALSE;
|
||||
|
||||
dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, nullptr, &entry);
|
||||
dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key_cstr);
|
||||
dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, "b", &variant);
|
||||
dbus_message_iter_append_basic(&variant, DBUS_TYPE_BOOLEAN, &bool_value);
|
||||
dbus_message_iter_close_container(&entry, &variant);
|
||||
dbus_message_iter_close_container(dict, &entry);
|
||||
}
|
||||
|
||||
bool ReadIntLike(DBusMessageIter* iter, int* value) {
|
||||
if (!iter || !value) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const int type = dbus_message_iter_get_arg_type(iter);
|
||||
if (type == DBUS_TYPE_INT32) {
|
||||
int32_t temp = 0;
|
||||
dbus_message_iter_get_basic(iter, &temp);
|
||||
*value = static_cast<int>(temp);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (type == DBUS_TYPE_UINT32) {
|
||||
uint32_t temp = 0;
|
||||
dbus_message_iter_get_basic(iter, &temp);
|
||||
*value = static_cast<int>(temp);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ReadPathLikeVariant(DBusMessageIter* variant, std::string* value) {
|
||||
if (!variant || !value) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const int type = dbus_message_iter_get_arg_type(variant);
|
||||
if (type == DBUS_TYPE_OBJECT_PATH || type == DBUS_TYPE_STRING) {
|
||||
const char* temp = nullptr;
|
||||
dbus_message_iter_get_basic(variant, &temp);
|
||||
if (temp && temp[0] != '\0') {
|
||||
*value = temp;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string BuildSessionHandleFromRequestPath(
|
||||
const std::string& request_path, const std::string& session_handle_token) {
|
||||
if (request_path.rfind(kPortalRequestPathPrefix, 0) != 0 ||
|
||||
session_handle_token.empty()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const size_t sender_start = strlen(kPortalRequestPathPrefix);
|
||||
const size_t token_sep = request_path.find('/', sender_start);
|
||||
if (token_sep == std::string::npos || token_sep <= sender_start) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const std::string sender =
|
||||
request_path.substr(sender_start, token_sep - sender_start);
|
||||
if (sender.empty()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return std::string(kPortalSessionPathPrefix) + sender + "/" +
|
||||
session_handle_token;
|
||||
}
|
||||
|
||||
struct PortalResponseState {
|
||||
std::string request_path;
|
||||
bool received = false;
|
||||
DBusMessage* message = nullptr;
|
||||
};
|
||||
|
||||
DBusHandlerResult HandlePortalResponseSignal(DBusConnection* connection,
|
||||
DBusMessage* message,
|
||||
void* user_data) {
|
||||
auto* state = static_cast<PortalResponseState*>(user_data);
|
||||
if (!state || !message) {
|
||||
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
||||
}
|
||||
|
||||
if (!dbus_message_is_signal(message, kPortalRequestInterface, "Response")) {
|
||||
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
||||
}
|
||||
|
||||
const char* path = dbus_message_get_path(message);
|
||||
if (!path || state->request_path != path) {
|
||||
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
||||
}
|
||||
|
||||
if (state->message) {
|
||||
dbus_message_unref(state->message);
|
||||
state->message = nullptr;
|
||||
}
|
||||
|
||||
state->message = dbus_message_ref(message);
|
||||
state->received = true;
|
||||
return DBUS_HANDLER_RESULT_HANDLED;
|
||||
}
|
||||
|
||||
DBusMessage* WaitForPortalResponse(DBusConnection* connection,
|
||||
const std::string& request_path,
|
||||
const std::atomic<bool>& running,
|
||||
int timeout_ms = 120000) {
|
||||
if (!connection || request_path.empty()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
PortalResponseState state;
|
||||
state.request_path = request_path;
|
||||
|
||||
DBusError error;
|
||||
dbus_error_init(&error);
|
||||
|
||||
const std::string match_rule =
|
||||
"type='signal',interface='" + std::string(kPortalRequestInterface) +
|
||||
"',member='Response',path='" + request_path + "'";
|
||||
dbus_bus_add_match(connection, match_rule.c_str(), &error);
|
||||
if (dbus_error_is_set(&error)) {
|
||||
LogDbusError("dbus_bus_add_match", &error);
|
||||
dbus_error_free(&error);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
dbus_connection_add_filter(connection, HandlePortalResponseSignal, &state,
|
||||
nullptr);
|
||||
|
||||
auto deadline =
|
||||
std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout_ms);
|
||||
while (running.load() && !state.received &&
|
||||
std::chrono::steady_clock::now() < deadline) {
|
||||
dbus_connection_read_write(connection, 100);
|
||||
while (dbus_connection_dispatch(connection) == DBUS_DISPATCH_DATA_REMAINS) {
|
||||
}
|
||||
}
|
||||
|
||||
dbus_connection_remove_filter(connection, HandlePortalResponseSignal, &state);
|
||||
|
||||
DBusError remove_error;
|
||||
dbus_error_init(&remove_error);
|
||||
dbus_bus_remove_match(connection, match_rule.c_str(), &remove_error);
|
||||
if (dbus_error_is_set(&remove_error)) {
|
||||
dbus_error_free(&remove_error);
|
||||
}
|
||||
|
||||
return state.message;
|
||||
}
|
||||
|
||||
bool ExtractRequestPath(DBusMessage* reply, std::string* request_path) {
|
||||
if (!reply || !request_path) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* path = nullptr;
|
||||
DBusError error;
|
||||
dbus_error_init(&error);
|
||||
const dbus_bool_t ok = dbus_message_get_args(
|
||||
reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID);
|
||||
if (!ok || !path) {
|
||||
LogDbusError("dbus_message_get_args(request_path)", &error);
|
||||
dbus_error_free(&error);
|
||||
return false;
|
||||
}
|
||||
|
||||
*request_path = path;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ExtractPortalResponse(DBusMessage* message, uint32_t* response_code,
|
||||
DBusMessageIter* results_array) {
|
||||
if (!message || !response_code || !results_array) {
|
||||
return false;
|
||||
}
|
||||
|
||||
DBusMessageIter iter;
|
||||
if (!dbus_message_iter_init(message, &iter) ||
|
||||
dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32) {
|
||||
return false;
|
||||
}
|
||||
|
||||
dbus_message_iter_get_basic(&iter, response_code);
|
||||
if (!dbus_message_iter_next(&iter) ||
|
||||
dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*results_array = iter;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SendPortalRequestAndHandleResponse(
|
||||
DBusConnection* connection, const char* interface_name,
|
||||
const char* method_name, const char* action_name,
|
||||
const std::function<bool(DBusMessage*)>& append_message_args,
|
||||
const std::atomic<bool>& running,
|
||||
const std::function<bool(uint32_t, DBusMessageIter*)>& handle_results,
|
||||
std::string* request_path_out = nullptr) {
|
||||
if (!connection || !interface_name || interface_name[0] == '\0' ||
|
||||
!method_name || method_name[0] == '\0') {
|
||||
return false;
|
||||
}
|
||||
|
||||
DBusMessage* message = dbus_message_new_method_call(
|
||||
kPortalBusName, kPortalObjectPath, interface_name, method_name);
|
||||
if (!message) {
|
||||
LOG_ERROR("Failed to allocate {} message", method_name);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (append_message_args && !append_message_args(message)) {
|
||||
dbus_message_unref(message);
|
||||
LOG_ERROR("{} arguments are malformed", method_name);
|
||||
return false;
|
||||
}
|
||||
|
||||
DBusError error;
|
||||
dbus_error_init(&error);
|
||||
DBusMessage* reply = dbus_connection_send_with_reply_and_block(
|
||||
connection, message, -1, &error);
|
||||
dbus_message_unref(message);
|
||||
if (!reply) {
|
||||
LogDbusError(action_name ? action_name : method_name, &error);
|
||||
dbus_error_free(&error);
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string request_path;
|
||||
const bool got_request_path = ExtractRequestPath(reply, &request_path);
|
||||
dbus_message_unref(reply);
|
||||
if (!got_request_path) {
|
||||
return false;
|
||||
}
|
||||
if (request_path_out) {
|
||||
*request_path_out = request_path;
|
||||
}
|
||||
|
||||
DBusMessage* response =
|
||||
WaitForPortalResponse(connection, request_path, running);
|
||||
if (!response) {
|
||||
LOG_ERROR("Timed out waiting for {} response", method_name);
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t response_code = 1;
|
||||
DBusMessageIter results;
|
||||
const bool parsed = ExtractPortalResponse(response, &response_code, &results);
|
||||
if (!parsed) {
|
||||
dbus_message_unref(response);
|
||||
LOG_ERROR("{} response was malformed", method_name);
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool ok = handle_results ? handle_results(response_code, &results)
|
||||
: (response_code == 0);
|
||||
dbus_message_unref(response);
|
||||
return ok;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool ScreenCapturerWayland::CheckPortalAvailability() const {
|
||||
DBusError error;
|
||||
dbus_error_init(&error);
|
||||
|
||||
DBusConnection* connection = dbus_bus_get(DBUS_BUS_SESSION, &error);
|
||||
if (!connection) {
|
||||
LogDbusError("dbus_bus_get", &error);
|
||||
dbus_error_free(&error);
|
||||
return false;
|
||||
}
|
||||
|
||||
const dbus_bool_t has_owner =
|
||||
dbus_bus_name_has_owner(connection, kPortalBusName, &error);
|
||||
if (dbus_error_is_set(&error)) {
|
||||
LogDbusError("dbus_bus_name_has_owner", &error);
|
||||
dbus_error_free(&error);
|
||||
dbus_connection_unref(connection);
|
||||
return false;
|
||||
}
|
||||
|
||||
dbus_connection_unref(connection);
|
||||
return has_owner == TRUE;
|
||||
}
|
||||
|
||||
bool ScreenCapturerWayland::ConnectSessionBus() {
|
||||
if (dbus_connection_) {
|
||||
return true;
|
||||
}
|
||||
|
||||
DBusError error;
|
||||
dbus_error_init(&error);
|
||||
|
||||
dbus_connection_ = dbus_bus_get_private(DBUS_BUS_SESSION, &error);
|
||||
if (!dbus_connection_) {
|
||||
LogDbusError("dbus_bus_get_private", &error);
|
||||
dbus_error_free(&error);
|
||||
return false;
|
||||
}
|
||||
|
||||
dbus_connection_set_exit_on_disconnect(dbus_connection_, FALSE);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ScreenCapturerWayland::CreatePortalSession() {
|
||||
if (!dbus_connection_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::string session_handle_token = MakeToken("crossdesk_session");
|
||||
std::string request_path;
|
||||
const bool ok = SendPortalRequestAndHandleResponse(
|
||||
dbus_connection_, kPortalRemoteDesktopInterface, "CreateSession",
|
||||
"CreateSession",
|
||||
[&](DBusMessage* message) {
|
||||
DBusMessageIter iter;
|
||||
DBusMessageIter options;
|
||||
dbus_message_iter_init_append(message, &iter);
|
||||
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}",
|
||||
&options);
|
||||
AppendDictEntryString(&options, "session_handle_token",
|
||||
session_handle_token);
|
||||
AppendDictEntryString(&options, "handle_token",
|
||||
MakeToken("crossdesk_req"));
|
||||
dbus_message_iter_close_container(&iter, &options);
|
||||
return true;
|
||||
},
|
||||
running_,
|
||||
[&](uint32_t response_code, DBusMessageIter* results) {
|
||||
if (response_code != 0) {
|
||||
LOG_ERROR("CreateSession was denied or malformed, response={}",
|
||||
response_code);
|
||||
return false;
|
||||
}
|
||||
|
||||
DBusMessageIter dict;
|
||||
dbus_message_iter_recurse(results, &dict);
|
||||
while (dbus_message_iter_get_arg_type(&dict) != DBUS_TYPE_INVALID) {
|
||||
if (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
|
||||
DBusMessageIter entry;
|
||||
dbus_message_iter_recurse(&dict, &entry);
|
||||
|
||||
const char* key = nullptr;
|
||||
dbus_message_iter_get_basic(&entry, &key);
|
||||
if (key && dbus_message_iter_next(&entry) &&
|
||||
dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_VARIANT &&
|
||||
strcmp(key, "session_handle") == 0) {
|
||||
DBusMessageIter variant;
|
||||
std::string parsed_handle;
|
||||
dbus_message_iter_recurse(&entry, &variant);
|
||||
if (ReadPathLikeVariant(&variant, &parsed_handle) &&
|
||||
!parsed_handle.empty()) {
|
||||
session_handle_ = parsed_handle;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
dbus_message_iter_next(&dict);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
&request_path);
|
||||
if (!ok) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (session_handle_.empty()) {
|
||||
const std::string fallback_handle =
|
||||
BuildSessionHandleFromRequestPath(request_path, session_handle_token);
|
||||
if (!fallback_handle.empty()) {
|
||||
LOG_WARN(
|
||||
"CreateSession response missing session_handle, using derived handle "
|
||||
"{}",
|
||||
fallback_handle);
|
||||
session_handle_ = fallback_handle;
|
||||
}
|
||||
}
|
||||
|
||||
if (session_handle_.empty()) {
|
||||
LOG_ERROR("CreateSession response did not include a session handle");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ScreenCapturerWayland::SelectPortalSource() {
|
||||
if (!dbus_connection_ || session_handle_.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t available_cursor_modes = kCursorModeHidden | kCursorModeEmbedded;
|
||||
if (!GetAvailableCursorModes(&available_cursor_modes)) {
|
||||
LOG_WARN(
|
||||
"Unable to query portal cursor modes; using embedded cursor fallback");
|
||||
}
|
||||
|
||||
uint32_t cursor_mode = 0;
|
||||
cursor_metadata_enabled_ = false;
|
||||
if (!show_cursor_ && (available_cursor_modes & kCursorModeMetadata) != 0) {
|
||||
cursor_mode = kCursorModeMetadata;
|
||||
cursor_metadata_enabled_ = true;
|
||||
} else if ((available_cursor_modes & kCursorModeEmbedded) != 0) {
|
||||
cursor_mode = kCursorModeEmbedded;
|
||||
} else if ((available_cursor_modes & kCursorModeMetadata) != 0) {
|
||||
cursor_mode = kCursorModeMetadata;
|
||||
cursor_metadata_enabled_ = true;
|
||||
} else if ((available_cursor_modes & kCursorModeHidden) != 0) {
|
||||
cursor_mode = kCursorModeHidden;
|
||||
}
|
||||
|
||||
if (cursor_mode == 0) {
|
||||
LOG_ERROR("Wayland portal reports no supported cursor mode");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (cursor_mode == kCursorModeEmbedded) {
|
||||
// When metadata is unavailable, embedding is the only way to preserve the
|
||||
// exact Wayland cursor. Hide the controller-side cursor to avoid a double
|
||||
// cursor over the video stream.
|
||||
PublishSharedCursorState(false, RemoteCursorShape::none);
|
||||
} else if (!cursor_metadata_enabled_) {
|
||||
// A portal without metadata or embedded cursor support cannot expose the
|
||||
// native Wayland shape. Keep a visible default cursor on the controller.
|
||||
PublishSharedCursorState(true, RemoteCursorShape::default_cursor);
|
||||
}
|
||||
|
||||
LOG_INFO("Wayland portal cursor mode: {}",
|
||||
cursor_mode == kCursorModeMetadata
|
||||
? "metadata"
|
||||
: (cursor_mode == kCursorModeEmbedded ? "embedded" : "hidden"));
|
||||
|
||||
const char* session_handle = session_handle_.c_str();
|
||||
return SendPortalRequestAndHandleResponse(
|
||||
dbus_connection_, kPortalScreenCastInterface, "SelectSources",
|
||||
"SelectSources",
|
||||
[&](DBusMessage* message) {
|
||||
DBusMessageIter iter;
|
||||
DBusMessageIter options;
|
||||
dbus_message_iter_init_append(message, &iter);
|
||||
dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
|
||||
&session_handle);
|
||||
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}",
|
||||
&options);
|
||||
AppendDictEntryUint32(&options, "types", kScreenCastSourceMonitor);
|
||||
AppendDictEntryBool(&options, "multiple", false);
|
||||
AppendDictEntryUint32(&options, "cursor_mode", cursor_mode);
|
||||
AppendDictEntryString(&options, "handle_token",
|
||||
MakeToken("crossdesk_req"));
|
||||
dbus_message_iter_close_container(&iter, &options);
|
||||
return true;
|
||||
},
|
||||
running_,
|
||||
[](uint32_t response_code, DBusMessageIter*) {
|
||||
if (response_code != 0) {
|
||||
LOG_ERROR("SelectSources was denied or malformed, response={}",
|
||||
response_code);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
bool ScreenCapturerWayland::GetAvailableCursorModes(uint32_t* modes) const {
|
||||
if (!dbus_connection_ || !modes) {
|
||||
return false;
|
||||
}
|
||||
|
||||
DBusMessage* message = dbus_message_new_method_call(
|
||||
kPortalBusName, kPortalObjectPath, "org.freedesktop.DBus.Properties",
|
||||
"Get");
|
||||
if (!message) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* interface_name = kPortalScreenCastInterface;
|
||||
const char* property_name = "AvailableCursorModes";
|
||||
dbus_message_append_args(message, DBUS_TYPE_STRING, &interface_name,
|
||||
DBUS_TYPE_STRING, &property_name,
|
||||
DBUS_TYPE_INVALID);
|
||||
|
||||
DBusError error;
|
||||
dbus_error_init(&error);
|
||||
DBusMessage* reply = dbus_connection_send_with_reply_and_block(
|
||||
dbus_connection_, message, -1, &error);
|
||||
dbus_message_unref(message);
|
||||
if (!reply) {
|
||||
LogDbusError("Get AvailableCursorModes", &error);
|
||||
dbus_error_free(&error);
|
||||
return false;
|
||||
}
|
||||
|
||||
DBusMessageIter iter;
|
||||
bool parsed = false;
|
||||
if (dbus_message_iter_init(reply, &iter) &&
|
||||
dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_VARIANT) {
|
||||
DBusMessageIter variant;
|
||||
dbus_message_iter_recurse(&iter, &variant);
|
||||
if (dbus_message_iter_get_arg_type(&variant) == DBUS_TYPE_UINT32) {
|
||||
dbus_message_iter_get_basic(&variant, modes);
|
||||
parsed = true;
|
||||
}
|
||||
}
|
||||
dbus_message_unref(reply);
|
||||
return parsed;
|
||||
}
|
||||
|
||||
bool ScreenCapturerWayland::SelectPortalDevices() {
|
||||
if (!dbus_connection_ || session_handle_.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* session_handle = session_handle_.c_str();
|
||||
return SendPortalRequestAndHandleResponse(
|
||||
dbus_connection_, kPortalRemoteDesktopInterface, "SelectDevices",
|
||||
"SelectDevices",
|
||||
[&](DBusMessage* message) {
|
||||
DBusMessageIter iter;
|
||||
DBusMessageIter options;
|
||||
dbus_message_iter_init_append(message, &iter);
|
||||
dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
|
||||
&session_handle);
|
||||
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}",
|
||||
&options);
|
||||
AppendDictEntryUint32(&options, "types", kRemoteDesktopDevicePointer);
|
||||
AppendDictEntryString(&options, "handle_token",
|
||||
MakeToken("crossdesk_req"));
|
||||
dbus_message_iter_close_container(&iter, &options);
|
||||
return true;
|
||||
},
|
||||
running_,
|
||||
[](uint32_t response_code, DBusMessageIter*) {
|
||||
if (response_code != 0) {
|
||||
LOG_ERROR("SelectDevices was denied or malformed, response={}",
|
||||
response_code);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
bool ScreenCapturerWayland::StartPortalSession() {
|
||||
if (!dbus_connection_ || session_handle_.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* session_handle = session_handle_.c_str();
|
||||
const char* parent_window = "";
|
||||
pointer_granted_ = false;
|
||||
const bool ok = SendPortalRequestAndHandleResponse(
|
||||
dbus_connection_, kPortalRemoteDesktopInterface, "Start", "Start",
|
||||
[&](DBusMessage* message) {
|
||||
DBusMessageIter iter;
|
||||
DBusMessageIter options;
|
||||
dbus_message_iter_init_append(message, &iter);
|
||||
dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
|
||||
&session_handle);
|
||||
dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &parent_window);
|
||||
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}",
|
||||
&options);
|
||||
AppendDictEntryString(&options, "handle_token",
|
||||
MakeToken("crossdesk_req"));
|
||||
dbus_message_iter_close_container(&iter, &options);
|
||||
return true;
|
||||
},
|
||||
running_,
|
||||
[&](uint32_t response_code, DBusMessageIter* results) {
|
||||
if (response_code != 0) {
|
||||
LOG_ERROR("Start was denied or malformed, response={}",
|
||||
response_code);
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t granted_devices = 0;
|
||||
DBusMessageIter dict;
|
||||
dbus_message_iter_recurse(results, &dict);
|
||||
while (dbus_message_iter_get_arg_type(&dict) != DBUS_TYPE_INVALID) {
|
||||
if (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
|
||||
DBusMessageIter entry;
|
||||
dbus_message_iter_recurse(&dict, &entry);
|
||||
|
||||
const char* key = nullptr;
|
||||
dbus_message_iter_get_basic(&entry, &key);
|
||||
if (key && dbus_message_iter_next(&entry) &&
|
||||
dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_VARIANT) {
|
||||
DBusMessageIter variant;
|
||||
dbus_message_iter_recurse(&entry, &variant);
|
||||
if (strcmp(key, "devices") == 0) {
|
||||
int granted_devices_int = 0;
|
||||
if (ReadIntLike(&variant, &granted_devices_int) &&
|
||||
granted_devices_int >= 0) {
|
||||
granted_devices = static_cast<uint32_t>(granted_devices_int);
|
||||
}
|
||||
} else if (strcmp(key, "streams") == 0) {
|
||||
DBusMessageIter streams;
|
||||
dbus_message_iter_recurse(&variant, &streams);
|
||||
|
||||
if (dbus_message_iter_get_arg_type(&streams) ==
|
||||
DBUS_TYPE_STRUCT) {
|
||||
DBusMessageIter stream;
|
||||
dbus_message_iter_recurse(&streams, &stream);
|
||||
|
||||
if (dbus_message_iter_get_arg_type(&stream) ==
|
||||
DBUS_TYPE_UINT32) {
|
||||
dbus_message_iter_get_basic(&stream, &pipewire_node_id_);
|
||||
}
|
||||
|
||||
if (dbus_message_iter_next(&stream) &&
|
||||
dbus_message_iter_get_arg_type(&stream) ==
|
||||
DBUS_TYPE_ARRAY) {
|
||||
DBusMessageIter props;
|
||||
int stream_width = 0;
|
||||
int stream_height = 0;
|
||||
int logical_width = 0;
|
||||
int logical_height = 0;
|
||||
dbus_message_iter_recurse(&stream, &props);
|
||||
while (dbus_message_iter_get_arg_type(&props) !=
|
||||
DBUS_TYPE_INVALID) {
|
||||
if (dbus_message_iter_get_arg_type(&props) ==
|
||||
DBUS_TYPE_DICT_ENTRY) {
|
||||
DBusMessageIter prop_entry;
|
||||
dbus_message_iter_recurse(&props, &prop_entry);
|
||||
|
||||
const char* prop_key = nullptr;
|
||||
dbus_message_iter_get_basic(&prop_entry, &prop_key);
|
||||
if (prop_key && dbus_message_iter_next(&prop_entry) &&
|
||||
dbus_message_iter_get_arg_type(&prop_entry) ==
|
||||
DBUS_TYPE_VARIANT) {
|
||||
DBusMessageIter prop_variant;
|
||||
dbus_message_iter_recurse(&prop_entry, &prop_variant);
|
||||
if (dbus_message_iter_get_arg_type(&prop_variant) ==
|
||||
DBUS_TYPE_STRUCT) {
|
||||
DBusMessageIter size_iter;
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
dbus_message_iter_recurse(&prop_variant,
|
||||
&size_iter);
|
||||
if (ReadIntLike(&size_iter, &width) &&
|
||||
dbus_message_iter_next(&size_iter) &&
|
||||
ReadIntLike(&size_iter, &height)) {
|
||||
if (strcmp(prop_key, "logical_size") == 0) {
|
||||
logical_width = width;
|
||||
logical_height = height;
|
||||
} else if (strcmp(prop_key, "size") == 0) {
|
||||
stream_width = width;
|
||||
stream_height = height;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
dbus_message_iter_next(&props);
|
||||
}
|
||||
|
||||
const int picked_width =
|
||||
logical_width > 0 ? logical_width : stream_width;
|
||||
const int picked_height =
|
||||
logical_height > 0 ? logical_height : stream_height;
|
||||
LOG_INFO(
|
||||
"Wayland portal stream geometry: stream_size={}x{}, "
|
||||
"logical_size={}x{}, pointer_space={}x{}",
|
||||
stream_width, stream_height, logical_width,
|
||||
logical_height, picked_width, picked_height);
|
||||
|
||||
portal_stream_width_ = stream_width;
|
||||
portal_stream_height_ = stream_height;
|
||||
portal_has_logical_size_ =
|
||||
logical_width > 0 && logical_height > 0;
|
||||
|
||||
if (logical_width > 0 && logical_height > 0) {
|
||||
logical_width_ = logical_width;
|
||||
logical_height_ = logical_height;
|
||||
UpdateDisplayGeometry(logical_width_, logical_height_);
|
||||
} else if (stream_width > 0 && stream_height > 0) {
|
||||
logical_width_ = stream_width;
|
||||
logical_height_ = stream_height;
|
||||
UpdateDisplayGeometry(logical_width_, logical_height_);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dbus_message_iter_next(&dict);
|
||||
}
|
||||
pointer_granted_ = (granted_devices & kRemoteDesktopDevicePointer) != 0;
|
||||
return true;
|
||||
});
|
||||
if (!ok) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pipewire_node_id_ == 0) {
|
||||
LOG_ERROR("Start response did not include a PipeWire node id");
|
||||
return false;
|
||||
}
|
||||
if (!pointer_granted_) {
|
||||
LOG_ERROR("Start response did not grant pointer control");
|
||||
return false;
|
||||
}
|
||||
|
||||
shared_session_registered_ =
|
||||
PublishSharedWaylandPortalSession(SharedWaylandPortalSessionInfo{
|
||||
dbus_connection_, session_handle_, pipewire_node_id_, logical_width_,
|
||||
logical_height_, pointer_granted_});
|
||||
if (!shared_session_registered_) {
|
||||
LOG_WARN("Failed to publish shared Wayland portal session");
|
||||
}
|
||||
|
||||
LOG_INFO("Wayland screencast ready, node_id={}", pipewire_node_id_);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ScreenCapturerWayland::OpenPipeWireRemote() {
|
||||
if (!dbus_connection_ || session_handle_.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
DBusMessage* message = dbus_message_new_method_call(
|
||||
kPortalBusName, kPortalObjectPath, kPortalScreenCastInterface,
|
||||
"OpenPipeWireRemote");
|
||||
if (!message) {
|
||||
LOG_ERROR("Failed to allocate OpenPipeWireRemote message");
|
||||
return false;
|
||||
}
|
||||
|
||||
DBusMessageIter iter;
|
||||
DBusMessageIter options;
|
||||
const char* session_handle = session_handle_.c_str();
|
||||
dbus_message_iter_init_append(message, &iter);
|
||||
dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH, &session_handle);
|
||||
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &options);
|
||||
dbus_message_iter_close_container(&iter, &options);
|
||||
|
||||
DBusError error;
|
||||
dbus_error_init(&error);
|
||||
DBusMessage* reply = dbus_connection_send_with_reply_and_block(
|
||||
dbus_connection_, message, -1, &error);
|
||||
dbus_message_unref(message);
|
||||
if (!reply) {
|
||||
LogDbusError("OpenPipeWireRemote", &error);
|
||||
dbus_error_free(&error);
|
||||
return false;
|
||||
}
|
||||
|
||||
DBusMessageIter reply_iter;
|
||||
if (!dbus_message_iter_init(reply, &reply_iter) ||
|
||||
dbus_message_iter_get_arg_type(&reply_iter) != DBUS_TYPE_UNIX_FD) {
|
||||
LOG_ERROR("OpenPipeWireRemote returned an unexpected payload");
|
||||
dbus_message_unref(reply);
|
||||
return false;
|
||||
}
|
||||
|
||||
int received_fd = -1;
|
||||
dbus_message_iter_get_basic(&reply_iter, &received_fd);
|
||||
dbus_message_unref(reply);
|
||||
|
||||
if (received_fd < 0) {
|
||||
LOG_ERROR("OpenPipeWireRemote returned an invalid fd");
|
||||
return false;
|
||||
}
|
||||
|
||||
pipewire_fd_ = dup(received_fd);
|
||||
if (pipewire_fd_ < 0) {
|
||||
LOG_ERROR("Failed to duplicate PipeWire remote fd");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ScreenCapturerWayland::CleanupDbus() {
|
||||
if (!dbus_connection_) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (shared_session_registered_) {
|
||||
return;
|
||||
}
|
||||
|
||||
dbus_connection_close(dbus_connection_);
|
||||
dbus_connection_unref(dbus_connection_);
|
||||
dbus_connection_ = nullptr;
|
||||
}
|
||||
|
||||
void ScreenCapturerWayland::ClosePortalSession() {
|
||||
if (shared_session_registered_) {
|
||||
DBusConnection* close_connection = nullptr;
|
||||
std::string close_session_handle;
|
||||
ReleaseSharedWaylandPortalSession(&close_connection, &close_session_handle);
|
||||
shared_session_registered_ = false;
|
||||
if (close_connection) {
|
||||
CloseWaylandPortalSessionAndConnection(
|
||||
close_connection, close_session_handle, "Session.Close");
|
||||
}
|
||||
dbus_connection_ = nullptr;
|
||||
} else if (dbus_connection_ && !session_handle_.empty()) {
|
||||
CloseWaylandPortalSessionAndConnection(dbus_connection_, session_handle_,
|
||||
"Session.Close");
|
||||
dbus_connection_ = nullptr;
|
||||
}
|
||||
|
||||
session_handle_.clear();
|
||||
pipewire_node_id_ = 0;
|
||||
UpdateDisplayGeometry(
|
||||
logical_width_ > 0 ? logical_width_ : kFallbackWidth,
|
||||
logical_height_ > 0 ? logical_height_ : kFallbackHeight);
|
||||
pointer_granted_ = false;
|
||||
}
|
||||
|
||||
} // namespace crossdesk
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,437 @@
|
||||
#include "screen_capturer_x11.h"
|
||||
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/Xutil.h>
|
||||
#include <X11/extensions/Xfixes.h>
|
||||
#include <X11/extensions/Xrandr.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
#include <display_stream_id.h>
|
||||
#include "libyuv.h"
|
||||
#include "rd_log.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
namespace {
|
||||
|
||||
std::atomic<int> g_x11_last_error_code{0};
|
||||
std::mutex g_x11_error_handler_mutex;
|
||||
|
||||
int CaptureX11ErrorHandler([[maybe_unused]] Display* display,
|
||||
XErrorEvent* error_event) {
|
||||
if (error_event) {
|
||||
g_x11_last_error_code.store(error_event->error_code);
|
||||
} else {
|
||||
g_x11_last_error_code.store(-1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
class ScopedX11ErrorTrap {
|
||||
public:
|
||||
explicit ScopedX11ErrorTrap(Display* display)
|
||||
: display_(display), lock_(g_x11_error_handler_mutex) {
|
||||
g_x11_last_error_code.store(0);
|
||||
previous_handler_ = XSetErrorHandler(CaptureX11ErrorHandler);
|
||||
}
|
||||
|
||||
~ScopedX11ErrorTrap() {
|
||||
if (display_) {
|
||||
XSync(display_, False);
|
||||
}
|
||||
XSetErrorHandler(previous_handler_);
|
||||
}
|
||||
|
||||
int SyncAndGetError() const {
|
||||
if (display_) {
|
||||
XSync(display_, False);
|
||||
}
|
||||
return g_x11_last_error_code.load();
|
||||
}
|
||||
|
||||
private:
|
||||
Display* display_ = nullptr;
|
||||
int (*previous_handler_)(Display*, XErrorEvent*) = nullptr;
|
||||
std::unique_lock<std::mutex> lock_;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
ScreenCapturerX11::ScreenCapturerX11() {}
|
||||
|
||||
ScreenCapturerX11::~ScreenCapturerX11() { Destroy(); }
|
||||
|
||||
int ScreenCapturerX11::Init(const int fps, cb_desktop_data cb) {
|
||||
Destroy();
|
||||
|
||||
display_ = XOpenDisplay(nullptr);
|
||||
if (!display_) {
|
||||
LOG_ERROR("Cannot connect to X server");
|
||||
return -1;
|
||||
}
|
||||
|
||||
root_ = DefaultRootWindow(display_);
|
||||
screen_res_ = XRRGetScreenResources(display_, root_);
|
||||
if (!screen_res_) {
|
||||
LOG_ERROR("Failed to get screen resources");
|
||||
XCloseDisplay(display_);
|
||||
display_ = nullptr;
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < screen_res_->noutput; ++i) {
|
||||
RROutput output = screen_res_->outputs[i];
|
||||
XRROutputInfo* output_info =
|
||||
XRRGetOutputInfo(display_, screen_res_, output);
|
||||
|
||||
if (output_info->connection == RR_Connected && output_info->crtc != 0) {
|
||||
XRRCrtcInfo* crtc_info =
|
||||
XRRGetCrtcInfo(display_, screen_res_, output_info->crtc);
|
||||
|
||||
std::string name(output_info->name);
|
||||
|
||||
if (name.empty()) {
|
||||
name = "Display" + std::to_string(i + 1);
|
||||
}
|
||||
|
||||
display_info_list_.push_back(DisplayInfo(
|
||||
(void*)display_, name, true, crtc_info->x, crtc_info->y,
|
||||
crtc_info->x + crtc_info->width, crtc_info->y + crtc_info->height));
|
||||
|
||||
XRRFreeCrtcInfo(crtc_info);
|
||||
}
|
||||
|
||||
if (output_info) {
|
||||
XRRFreeOutputInfo(output_info);
|
||||
}
|
||||
}
|
||||
|
||||
XWindowAttributes attr;
|
||||
XGetWindowAttributes(display_, root_, &attr);
|
||||
|
||||
width_ = attr.width;
|
||||
height_ = attr.height;
|
||||
|
||||
if ((width_ & 1) != 0 || (height_ & 1) != 0) {
|
||||
LOG_WARN("X11 root size {}x{} is not even, aligning down to {}x{} for NV12",
|
||||
width_, height_, width_ & ~1, height_ & ~1);
|
||||
width_ &= ~1;
|
||||
height_ &= ~1;
|
||||
}
|
||||
|
||||
if (width_ <= 1 || height_ <= 1) {
|
||||
LOG_ERROR("Invalid capture size after alignment: {}x{}", width_, height_);
|
||||
return -2;
|
||||
}
|
||||
|
||||
fps_ = fps;
|
||||
callback_ = cb;
|
||||
|
||||
y_plane_.resize(width_ * height_);
|
||||
uv_plane_.resize((width_ / 2) * (height_ / 2) * 2);
|
||||
|
||||
if (!ProbeCapture()) {
|
||||
LOG_ERROR("X11 backend probe failed, XGetImage is not usable");
|
||||
return -3;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerX11::Destroy() {
|
||||
Stop();
|
||||
|
||||
y_plane_.clear();
|
||||
uv_plane_.clear();
|
||||
|
||||
if (screen_res_) {
|
||||
XRRFreeScreenResources(screen_res_);
|
||||
screen_res_ = nullptr;
|
||||
}
|
||||
|
||||
if (display_) {
|
||||
XCloseDisplay(display_);
|
||||
display_ = nullptr;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerX11::Start(bool show_cursor) {
|
||||
if (running_) return 0;
|
||||
show_cursor_ = show_cursor;
|
||||
running_ = true;
|
||||
paused_ = false;
|
||||
capture_error_count_ = 0;
|
||||
thread_ = std::thread([this]() {
|
||||
using clock = std::chrono::steady_clock;
|
||||
const auto frame_interval =
|
||||
std::chrono::milliseconds(std::max(1, 1000 / std::max(1, fps_)));
|
||||
|
||||
while (running_) {
|
||||
const auto frame_start = clock::now();
|
||||
if (!paused_) {
|
||||
OnFrame();
|
||||
}
|
||||
|
||||
const auto elapsed =
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(clock::now() -
|
||||
frame_start);
|
||||
if (elapsed < frame_interval) {
|
||||
std::this_thread::sleep_for(frame_interval - elapsed);
|
||||
}
|
||||
}
|
||||
});
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerX11::Stop() {
|
||||
if (!running_) return 0;
|
||||
running_ = false;
|
||||
if (thread_.joinable()) thread_.join();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerX11::Pause(int monitor_index) {
|
||||
paused_ = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerX11::Resume(int monitor_index) {
|
||||
paused_ = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerX11::SwitchTo(int monitor_index) {
|
||||
monitor_index_ = monitor_index;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerX11::ResetToInitialMonitor() {
|
||||
monitor_index_ = initial_monitor_index_;
|
||||
return 0;
|
||||
}
|
||||
std::vector<DisplayInfo> ScreenCapturerX11::GetDisplayInfoList() {
|
||||
return display_info_list_;
|
||||
}
|
||||
|
||||
void ScreenCapturerX11::OnFrame() {
|
||||
if (!display_) {
|
||||
LOG_ERROR("Display is not initialized");
|
||||
return;
|
||||
}
|
||||
|
||||
const int monitor_index = monitor_index_.load();
|
||||
if (monitor_index < 0 ||
|
||||
monitor_index >= static_cast<int>(display_info_list_.size())) {
|
||||
LOG_ERROR("Invalid monitor index: {}", monitor_index);
|
||||
return;
|
||||
}
|
||||
|
||||
left_ = display_info_list_[monitor_index].left;
|
||||
top_ = display_info_list_[monitor_index].top;
|
||||
width_ = display_info_list_[monitor_index].width & ~1;
|
||||
height_ = display_info_list_[monitor_index].height & ~1;
|
||||
|
||||
if (width_ <= 1 || height_ <= 1) {
|
||||
LOG_ERROR("Invalid capture size: {}x{}", width_, height_);
|
||||
return;
|
||||
}
|
||||
|
||||
XImage* image = nullptr;
|
||||
int x11_error = 0;
|
||||
{
|
||||
ScopedX11ErrorTrap trap(display_);
|
||||
image = XGetImage(display_, root_, left_, top_, width_, height_, AllPlanes,
|
||||
ZPixmap);
|
||||
x11_error = trap.SyncAndGetError();
|
||||
}
|
||||
|
||||
if (x11_error != 0 || !image) {
|
||||
if (image) {
|
||||
XDestroyImage(image);
|
||||
}
|
||||
++capture_error_count_;
|
||||
if (capture_error_count_ == 1 || capture_error_count_ % 120 == 0) {
|
||||
LOG_WARN("X11 capture failed: x11_error={}, image={}, consecutive={}",
|
||||
x11_error, image ? "valid" : "null", capture_error_count_);
|
||||
}
|
||||
return;
|
||||
}
|
||||
capture_error_count_ = 0;
|
||||
|
||||
// if enable show cursor, draw cursor
|
||||
if (show_cursor_) {
|
||||
Window root_return, child_return;
|
||||
int root_x, root_y, win_x, win_y;
|
||||
unsigned int mask;
|
||||
if (XQueryPointer(display_, root_, &root_return, &child_return, &root_x,
|
||||
&root_y, &win_x, &win_y, &mask)) {
|
||||
if (root_x >= left_ && root_x < left_ + width_ && root_y >= top_ &&
|
||||
root_y < top_ + height_) {
|
||||
DrawCursor(image, root_x - left_, root_y - top_);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (image->bits_per_pixel != 32 || image->bytes_per_line <= 0) {
|
||||
LOG_WARN(
|
||||
"Unsupported X11 image layout: bits_per_pixel={}, bytes_per_line={}",
|
||||
image->bits_per_pixel, image->bytes_per_line);
|
||||
XDestroyImage(image);
|
||||
return;
|
||||
}
|
||||
|
||||
const uint8_t* src_argb = reinterpret_cast<const uint8_t*>(image->data);
|
||||
const int src_stride_argb = image->bytes_per_line;
|
||||
|
||||
const size_t y_size =
|
||||
static_cast<size_t>(width_) * static_cast<size_t>(height_);
|
||||
const size_t uv_size = y_size / 2;
|
||||
if (y_plane_.size() != y_size) {
|
||||
y_plane_.resize(y_size);
|
||||
}
|
||||
if (uv_plane_.size() != uv_size) {
|
||||
uv_plane_.resize(uv_size);
|
||||
}
|
||||
|
||||
const int convert_ret =
|
||||
use_abgr_to_nv12_
|
||||
? libyuv::ABGRToNV12(src_argb, src_stride_argb, y_plane_.data(),
|
||||
width_, uv_plane_.data(), width_, width_,
|
||||
height_)
|
||||
: libyuv::ARGBToNV12(src_argb, src_stride_argb, y_plane_.data(),
|
||||
width_, uv_plane_.data(), width_, width_,
|
||||
height_);
|
||||
if (convert_ret != 0) {
|
||||
LOG_WARN("X11 {} failed: {}",
|
||||
use_abgr_to_nv12_ ? "ABGRToNV12" : "ARGBToNV12", convert_ret);
|
||||
XDestroyImage(image);
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> nv12;
|
||||
nv12.reserve(y_plane_.size() + uv_plane_.size());
|
||||
nv12.insert(nv12.end(), y_plane_.begin(), y_plane_.end());
|
||||
nv12.insert(nv12.end(), uv_plane_.begin(), uv_plane_.end());
|
||||
|
||||
if (callback_) {
|
||||
const std::string stream_id = MakeDisplayStreamId(monitor_index);
|
||||
callback_(nv12.data(), width_ * height_ * 3 / 2, width_, height_,
|
||||
stream_id.c_str());
|
||||
}
|
||||
|
||||
XDestroyImage(image);
|
||||
}
|
||||
|
||||
void ScreenCapturerX11::DrawCursor(XImage* image, int x, int y) {
|
||||
if (!display_ || !image) {
|
||||
return;
|
||||
}
|
||||
|
||||
// check XFixes extension
|
||||
int event_base, error_base;
|
||||
if (!XFixesQueryExtension(display_, &event_base, &error_base)) {
|
||||
return;
|
||||
}
|
||||
|
||||
XFixesCursorImage* cursor_image = XFixesGetCursorImage(display_);
|
||||
if (!cursor_image) {
|
||||
return;
|
||||
}
|
||||
|
||||
int cursor_width = cursor_image->width;
|
||||
int cursor_height = cursor_image->height;
|
||||
|
||||
int draw_x = x - cursor_image->xhot;
|
||||
int draw_y = y - cursor_image->yhot;
|
||||
|
||||
// draw cursor on image
|
||||
for (int cy = 0; cy < cursor_height; ++cy) {
|
||||
for (int cx = 0; cx < cursor_width; ++cx) {
|
||||
int img_x = draw_x + cx;
|
||||
int img_y = draw_y + cy;
|
||||
|
||||
if (img_x < 0 || img_x >= image->width || img_y < 0 ||
|
||||
img_y >= image->height) {
|
||||
continue;
|
||||
}
|
||||
|
||||
unsigned long cursor_pixel = cursor_image->pixels[cy * cursor_width + cx];
|
||||
unsigned char a = (cursor_pixel >> 24) & 0xFF;
|
||||
|
||||
// if alpha is 0, skip
|
||||
if (a == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
unsigned long img_pixel = XGetPixel(image, img_x, img_y);
|
||||
|
||||
unsigned char img_r = (img_pixel >> 16) & 0xFF;
|
||||
unsigned char img_g = (img_pixel >> 8) & 0xFF;
|
||||
unsigned char img_b = img_pixel & 0xFF;
|
||||
|
||||
unsigned char cursor_r = (cursor_pixel >> 16) & 0xFF;
|
||||
unsigned char cursor_g = (cursor_pixel >> 8) & 0xFF;
|
||||
unsigned char cursor_b = cursor_pixel & 0xFF;
|
||||
|
||||
// alpha mix
|
||||
unsigned char final_r, final_g, final_b;
|
||||
if (a == 255) {
|
||||
// if alpha is 255, use cursor color
|
||||
final_r = cursor_r;
|
||||
final_g = cursor_g;
|
||||
final_b = cursor_b;
|
||||
} else {
|
||||
float alpha = a / 255.0f;
|
||||
float inv_alpha = 1.0f - alpha;
|
||||
final_r =
|
||||
static_cast<unsigned char>(cursor_r * alpha + img_r * inv_alpha);
|
||||
final_g =
|
||||
static_cast<unsigned char>(cursor_g * alpha + img_g * inv_alpha);
|
||||
final_b =
|
||||
static_cast<unsigned char>(cursor_b * alpha + img_b * inv_alpha);
|
||||
}
|
||||
|
||||
// set pixel
|
||||
unsigned long new_pixel = (final_r << 16) | (final_g << 8) | final_b;
|
||||
XPutPixel(image, img_x, img_y, new_pixel);
|
||||
}
|
||||
}
|
||||
|
||||
XFree(cursor_image);
|
||||
}
|
||||
|
||||
bool ScreenCapturerX11::ProbeCapture() {
|
||||
if (!display_ || display_info_list_.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto& first_display = display_info_list_[0];
|
||||
XImage* probe_image = nullptr;
|
||||
int x11_error = 0;
|
||||
{
|
||||
ScopedX11ErrorTrap trap(display_);
|
||||
probe_image = XGetImage(display_, root_, first_display.left,
|
||||
first_display.top, 1, 1, AllPlanes, ZPixmap);
|
||||
x11_error = trap.SyncAndGetError();
|
||||
}
|
||||
|
||||
if (x11_error != 0 || !probe_image) {
|
||||
LOG_WARN("X11 probe XGetImage failed: x11_error={}, image={}", x11_error,
|
||||
probe_image ? "valid" : "null");
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool red_in_low_byte = (probe_image->red_mask & 0x000000FFu) != 0;
|
||||
const bool blue_in_low_byte = (probe_image->blue_mask & 0x000000FFu) != 0;
|
||||
use_abgr_to_nv12_ = red_in_low_byte && !blue_in_low_byte;
|
||||
|
||||
XDestroyImage(probe_image);
|
||||
|
||||
return true;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2025-05-07
|
||||
* Copyright (c) 2025 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _SCREEN_CAPTURER_X11_H_
|
||||
#define _SCREEN_CAPTURER_X11_H_
|
||||
|
||||
// forward declarations for X11 types
|
||||
struct _XDisplay;
|
||||
typedef struct _XDisplay Display;
|
||||
typedef unsigned long Window;
|
||||
struct _XRRScreenResources;
|
||||
typedef struct _XRRScreenResources XRRScreenResources;
|
||||
struct _XImage;
|
||||
typedef struct _XImage XImage;
|
||||
|
||||
#include <atomic>
|
||||
#include <cctype>
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "screen_capturer.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
class ScreenCapturerX11 : public ScreenCapturer {
|
||||
public:
|
||||
ScreenCapturerX11();
|
||||
~ScreenCapturerX11();
|
||||
|
||||
public:
|
||||
int Init(const int fps, cb_desktop_data cb) override;
|
||||
int Destroy() override;
|
||||
int Start(bool show_cursor) override;
|
||||
int Stop() override;
|
||||
|
||||
int Pause(int monitor_index) override;
|
||||
int Resume(int monitor_index) override;
|
||||
|
||||
int SwitchTo(int monitor_index) override;
|
||||
int ResetToInitialMonitor() override;
|
||||
|
||||
std::vector<DisplayInfo> GetDisplayInfoList() override;
|
||||
|
||||
void OnFrame();
|
||||
|
||||
private:
|
||||
void DrawCursor(XImage* image, int x, int y);
|
||||
bool ProbeCapture();
|
||||
|
||||
private:
|
||||
Display* display_ = nullptr;
|
||||
Window root_ = 0;
|
||||
XRRScreenResources* screen_res_ = nullptr;
|
||||
int left_ = 0;
|
||||
int top_ = 0;
|
||||
int width_ = 0;
|
||||
int height_ = 0;
|
||||
std::thread thread_;
|
||||
std::atomic<bool> running_{false};
|
||||
std::atomic<bool> paused_{false};
|
||||
std::atomic<int> monitor_index_{0};
|
||||
int initial_monitor_index_ = 0;
|
||||
std::atomic<bool> show_cursor_{true};
|
||||
int fps_ = 60;
|
||||
cb_desktop_data callback_;
|
||||
std::vector<DisplayInfo> display_info_list_;
|
||||
int capture_error_count_ = 0;
|
||||
bool use_abgr_to_nv12_ = false;
|
||||
|
||||
std::vector<uint8_t> y_plane_;
|
||||
std::vector<uint8_t> uv_plane_;
|
||||
};
|
||||
} // namespace crossdesk
|
||||
#endif
|
||||
Reference in New Issue
Block a user