Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/capture.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! X11 screen capture: MIT-SHM GetImage into a persistent BGRX frame, driven by XDamage.

use crate::env::Debugging;
use std::os::fd::{AsFd, BorrowedFd};
use x11rb::connection::{Connection, RequestConnection};
use x11rb::protocol::damage::{self, ConnectionExt as _};
Expand Down Expand Up @@ -104,10 +105,11 @@ pub struct Capturer {
pub mb_cols: usize,
/// Areas (x0, y0, x1, y1) changed since the video thread last took them, for `/wait`.
pub changes: Vec<(u32, u32, u32, u32)>,
debug: Debugging,
}

impl Capturer {
pub fn new(display: Option<&str>) -> Result<Self, String> {
pub fn new(display: Option<&str>, debug: Debugging) -> Result<Self, String> {
let (conn, screen_num) =
RustConnection::connect(display).map_err(|e| format!("X connect: {e}"))?;
let screen = &conn.setup().roots[screen_num];
Expand Down Expand Up @@ -149,7 +151,7 @@ impl Capturer {
let seg = conn.generate_id().map_err(|e| e.to_string())?;
let server_pid = display.and_then(xserver_pid);
let (buf, offset, direct) =
match find_xvfb_framebuffer(&conn, root, width, height, server_pid) {
match find_xvfb_framebuffer(&conn, root, width, height, server_pid, debug) {
Some((buf, offset, fb_stride)) => {
log::info!(
"capturing directly from the Xvfb framebuffer (shmid {})",
Expand Down Expand Up @@ -195,6 +197,7 @@ impl Capturer {
mb_dirty: vec![0; (width as usize).div_ceil(16) * (height as usize).div_ceil(16)],
mb_cols: (width as usize).div_ceil(16),
changes: Vec::new(),
debug,
})
}

Expand All @@ -214,7 +217,7 @@ impl Capturer {
}
if let Event::DamageNotify(e) = ev {
DAMAGE_EVENTS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
if std::env::var("VOXL_DEBUG_DAMAGE").is_ok() {
if self.debug.damage {
log::info!(
"dmg {:?} x{} y{} {}x{}",
std::time::SystemTime::now()
Expand Down Expand Up @@ -393,8 +396,9 @@ fn find_xvfb_framebuffer(
width: u32,
height: u32,
server_pid: Option<u32>,
debug: Debugging,
) -> Option<(ShmBuf, usize, usize)> {
if std::env::var("VOXL_NO_DIRECT_FB").is_ok() {
if !debug.direct_fb {
return None;
}
let table = std::fs::read_to_string("/proc/sysvipc/shm").ok()?;
Expand Down
8 changes: 6 additions & 2 deletions src/corners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//! whatever is behind it through the corners. Nothing is repainted per frame, so
//! damage stays precise.

use crate::env::Debugging;
use std::collections::HashMap;
use x11rb::connection::Connection;
use x11rb::protocol::shape::{self, ConnectionExt as _};
Expand All @@ -28,6 +29,7 @@ struct State {
shaped: HashMap<u32, Option<(u16, u16, u16)>>,
/// Last size seen per window; moves (same size) need no work.
seen: HashMap<u32, (u16, u16)>,
debug: Debugging,
}

impl State {
Expand Down Expand Up @@ -102,7 +104,7 @@ impl State {
if self.shaped.get(&win) == Some(&want) {
return;
}
if std::env::var("VOXL_DEBUG_CORNERS").is_ok() {
if self.debug.corners {
log::info!(
"corners: window {win:#x} {width}x{height} -> {want:?} (was {:?})",
self.shaped.get(&win)
Expand Down Expand Up @@ -163,6 +165,7 @@ pub fn spawn(
display: Option<String>,
radius: u16,
named: Vec<(String, u16)>,
debug: Debugging,
) -> Result<(), String> {
let (conn, n) = RustConnection::connect(display.as_deref()).map_err(|e| e.to_string())?;
conn.shape_query_version()
Expand Down Expand Up @@ -191,6 +194,7 @@ pub fn spawn(
net_wm_window_type,
shaped: HashMap::new(),
seen: HashMap::new(),
debug,
};
std::thread::Builder::new()
.name("corners".into())
Expand All @@ -211,7 +215,7 @@ pub fn spawn(
}
}
Ok(Event::ConfigureNotify(e)) if e.event == st.root => {
if std::env::var("VOXL_DEBUG_CORNERS").is_ok() {
if st.debug.corners {
log::info!(
"configure {:#x} {}x{} at {},{}",
e.window,
Expand Down
19 changes: 11 additions & 8 deletions src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod ffi {
include!(concat!(env!("OUT_DIR"), "/x264.rs"));
}

use crate::env::{Debugging, X264Log};
use std::ffi::CString;
use std::ptr;

Expand All @@ -20,6 +21,7 @@ pub struct EncoderConfig {
pub fps: u32,
pub preset: String,
pub threads: u32,
pub debug: Debugging,
pub crf: f32,
/// VBV cap in kbit/s (0 = uncapped).
pub max_kbps: u32,
Expand All @@ -32,6 +34,7 @@ pub struct Encoder {
pts: i64,
pub width: u32,
pub height: u32,
debug: Debugging,
}

unsafe impl Send for Encoder {}
Expand Down Expand Up @@ -73,13 +76,11 @@ impl Encoder {
param.b_annexb = 1;
param.b_repeat_headers = 1;
param.b_aud = 0;
param.i_log_level = if std::env::var("VOXL_X264_INFO").as_deref() == Ok("debug") {
param.i_log_level = match cfg.debug.x264_log {
X264Log::Warning => ffi::X264_LOG_WARNING,
X264Log::Info => ffi::X264_LOG_INFO,
// Per-frame QP, macroblock types and size.
ffi::X264_LOG_DEBUG
} else if std::env::var("VOXL_X264_INFO").is_ok() {
ffi::X264_LOG_INFO
} else {
ffi::X264_LOG_WARNING
X264Log::Debug => ffi::X264_LOG_DEBUG,
} as i32;
param.rc.i_rc_method = ffi::X264_RC_CRF as i32;
param.rc.f_rf_constant = cfg.crf;
Expand All @@ -90,7 +91,7 @@ impl Encoder {
param.analyse.i_me_method = ffi::X264_ME_UMH as i32;
param.analyse.i_me_range = 64;
// Per-macroblock "unchanged" hints from X damage let x264 skip analysis.
param.analyse.b_mb_info = (std::env::var("VOXL_NO_MBINFO").is_err()) as i32;
param.analyse.b_mb_info = cfg.debug.mb_info as i32;
param.i_sync_lookahead = 0;
param.rc.b_mb_tree = 0;
if cfg.max_kbps > 0 {
Expand Down Expand Up @@ -122,6 +123,7 @@ impl Encoder {
pts: 0,
width: cfg.width,
height: cfg.height,
debug: cfg.debug,
})
}
}
Expand Down Expand Up @@ -191,7 +193,7 @@ impl Encoder {
let first = &*nals;
out.extend_from_slice(std::slice::from_raw_parts(first.p_payload, size as usize));
}
if std::env::var("VOXL_DEBUG_MARKER").is_ok() {
if self.debug.marker.is_some() {
log::info!(
"x264 out {} bytes, delayed {}, pts in {} out {}",
size,
Expand Down Expand Up @@ -266,6 +268,7 @@ mod bench {
fps: 60,
preset: "ultrafast".into(),
threads,
debug: crate::env::Debugging::read(|_| None).0,
crf: 24.0,
max_kbps: 30000,
})
Expand Down
Loading