feat(client): add RU/EN localization and native capture preview
This commit is contained in:
parent
8e5ab56611
commit
8460fb28fd
43 changed files with 4103 additions and 275 deletions
398
src/media/capture.rs
Normal file
398
src/media/capture.rs
Normal file
|
|
@ -0,0 +1,398 @@
|
|||
use super::portal::PortalSession;
|
||||
use super::{AudioChoice, CaptureOptions, VideoFrame, audio_sources, validate_audio_choice};
|
||||
use gst::prelude::*;
|
||||
use gstreamer as gst;
|
||||
use std::{
|
||||
os::fd::AsRawFd,
|
||||
sync::{
|
||||
Arc, Mutex,
|
||||
atomic::{AtomicBool, Ordering},
|
||||
},
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
/// Latest owned preview frame and selected audio level; queues never grow.
|
||||
#[derive(Clone, Default)]
|
||||
pub struct CaptureFeed {
|
||||
video: Arc<Mutex<Option<VideoFrame>>>,
|
||||
audio: Arc<Mutex<f32>>,
|
||||
received_audio: Arc<AtomicBool>,
|
||||
}
|
||||
impl CaptureFeed {
|
||||
pub fn take_frame(&self) -> Option<VideoFrame> {
|
||||
self.video.lock().ok()?.take()
|
||||
}
|
||||
pub fn audio_level(&self) -> f32 {
|
||||
self.audio.lock().map(|v| *v).unwrap_or(0.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// Live local capture. Dropping it stops both pipelines and revokes portal access.
|
||||
pub struct CaptureSession {
|
||||
pipelines: Vec<gst::Pipeline>,
|
||||
portal: Option<PortalSession>,
|
||||
pub feed: CaptureFeed,
|
||||
started: Instant,
|
||||
expects_audio: bool,
|
||||
}
|
||||
|
||||
pub fn check_backend() -> Result<(), String> {
|
||||
gst::init().map_err(error)?;
|
||||
for name in [
|
||||
"pipewiresrc",
|
||||
"videoconvert",
|
||||
"videoscale",
|
||||
"videorate",
|
||||
"audioconvert",
|
||||
"audioresample",
|
||||
"appsink",
|
||||
] {
|
||||
if gst::ElementFactory::find(name).is_none() {
|
||||
return Err(format!(
|
||||
"Missing GStreamer plugin: {name}. Install the PipeWire and base plugins."
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl CaptureSession {
|
||||
pub async fn choose(options: CaptureOptions) -> Result<Self, String> {
|
||||
options.validate()?;
|
||||
check_backend()?;
|
||||
if options.audio != AudioChoice::Disabled {
|
||||
validate_audio_choice(options.audio, &audio_sources().await?)?;
|
||||
}
|
||||
let portal = PortalSession::choose().await?;
|
||||
Self::from_portal(portal, options)
|
||||
}
|
||||
|
||||
fn from_portal(portal: PortalSession, options: CaptureOptions) -> Result<Self, String> {
|
||||
let feed = CaptureFeed::default();
|
||||
let mut session = Self {
|
||||
pipelines: Vec::new(),
|
||||
portal: Some(portal),
|
||||
feed,
|
||||
started: Instant::now(),
|
||||
expects_audio: options.audio != AudioChoice::Disabled,
|
||||
};
|
||||
let portal = session.portal.as_ref().ok_or("Missing portal session")?;
|
||||
let source = gst::ElementFactory::make("pipewiresrc")
|
||||
.property("fd", portal.remote.as_raw_fd())
|
||||
.build()
|
||||
.map_err(error)?;
|
||||
if let Some(serial) = portal.serial {
|
||||
source.set_property("target-object", serial.to_string());
|
||||
} else {
|
||||
source.set_property("path", portal.node.to_string());
|
||||
}
|
||||
strict_source(&source);
|
||||
let pipeline = video_pipeline(source, &session.feed, options)?;
|
||||
session.pipelines.push(pipeline);
|
||||
if let AudioChoice::Application(serial) | AudioChoice::System(serial) = options.audio {
|
||||
let source = gst::ElementFactory::make("pipewiresrc")
|
||||
.property("target-object", serial.to_string())
|
||||
.build()
|
||||
.map_err(error)?;
|
||||
strict_source(&source);
|
||||
// A monitor stream targets exactly this serial. It must neither move nor
|
||||
// connect to a default source if that application/device vanishes.
|
||||
let props = gst::Structure::builder("properties")
|
||||
.field("stream.capture.sink", true)
|
||||
.field("stream.monitor", true)
|
||||
.field("node.dont-reconnect", true)
|
||||
.field("node.dont-fallback", true)
|
||||
.field("node.dont-move", true)
|
||||
.field("media.name", "VNOX selected audio preview")
|
||||
.build();
|
||||
source.set_property("stream-properties", props);
|
||||
session
|
||||
.pipelines
|
||||
.push(audio_pipeline(source, &session.feed)?);
|
||||
}
|
||||
for pipeline in &session.pipelines {
|
||||
pipeline.set_state(gst::State::Playing).map_err(error)?;
|
||||
}
|
||||
Ok(session)
|
||||
}
|
||||
|
||||
/// Polling drains bus errors and EOS. Callers must stop the session on Err.
|
||||
pub fn check(&self) -> Result<(), String> {
|
||||
if self.expects_audio
|
||||
&& !self.feed.received_audio.load(Ordering::Relaxed)
|
||||
&& self.started.elapsed() > Duration::from_secs(10)
|
||||
{
|
||||
return Err("Selected audio source produced no samples. Capture stopped; no fallback source was enabled.".into());
|
||||
}
|
||||
for pipeline in &self.pipelines {
|
||||
if let Some(bus) = pipeline.bus() {
|
||||
while let Some(message) = bus.pop() {
|
||||
match message.view() {
|
||||
gst::MessageView::Error(e) => {
|
||||
return Err(format!("Capture stopped: {}", e.error()));
|
||||
}
|
||||
gst::MessageView::Eos(..) => {
|
||||
return Err("Capture source closed. Choose it again to resume.".into());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for CaptureSession {
|
||||
fn drop(&mut self) {
|
||||
for pipeline in &self.pipelines {
|
||||
let _ = pipeline.set_state(gst::State::Null);
|
||||
}
|
||||
// The descriptor remains alive until all pipeline streams have stopped.
|
||||
drop(self.portal.take());
|
||||
if let Ok(mut frame) = self.feed.video.lock() {
|
||||
*frame = None;
|
||||
}
|
||||
if let Ok(mut level) = self.feed.audio.lock() {
|
||||
*level = 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn strict_source(source: &gst::Element) {
|
||||
source.set_property_from_str("on-disconnect", "error");
|
||||
if source.find_property("dont-reconnect").is_some() {
|
||||
source.set_property("dont-reconnect", true);
|
||||
}
|
||||
}
|
||||
|
||||
fn video_pipeline(
|
||||
source: gst::Element,
|
||||
feed: &CaptureFeed,
|
||||
options: CaptureOptions,
|
||||
) -> Result<gst::Pipeline, String> {
|
||||
let sink = gstreamer_app::AppSink::builder()
|
||||
.max_buffers(1)
|
||||
.drop(true)
|
||||
.sync(false)
|
||||
.build();
|
||||
let latest = feed.video.clone();
|
||||
sink.set_callbacks(
|
||||
gstreamer_app::AppSinkCallbacks::builder()
|
||||
.new_sample(move |sink| {
|
||||
let sample = sink.pull_sample().map_err(|_| gst::FlowError::Eos)?;
|
||||
let frame = copy_video(&sample).map_err(|_| gst::FlowError::Error)?;
|
||||
*latest.lock().map_err(|_| gst::FlowError::Error)? = Some(frame);
|
||||
Ok(gst::FlowSuccess::Ok)
|
||||
})
|
||||
.build(),
|
||||
);
|
||||
let convert = gst::ElementFactory::make("videoconvert")
|
||||
.build()
|
||||
.map_err(error)?;
|
||||
let scale = gst::ElementFactory::make("videoscale")
|
||||
.build()
|
||||
.map_err(error)?;
|
||||
let rate = gst::ElementFactory::make("videorate")
|
||||
.property("drop-only", true)
|
||||
.build()
|
||||
.map_err(error)?;
|
||||
let caps: gst::Caps = format!(
|
||||
"video/x-raw,format=RGBA,width=[1,{}],height=[1,{}],framerate=[1/1,{}/1]",
|
||||
options.max_width, options.max_height, options.max_fps
|
||||
)
|
||||
.parse()
|
||||
.map_err(error)?;
|
||||
let filter = gst::ElementFactory::make("capsfilter")
|
||||
.property("caps", caps)
|
||||
.build()
|
||||
.map_err(error)?;
|
||||
let pipeline = gst::Pipeline::new();
|
||||
let elements = [&source, &convert, &scale, &rate, &filter, sink.upcast_ref()];
|
||||
pipeline.add_many(elements).map_err(error)?;
|
||||
gst::Element::link_many(elements).map_err(error)?;
|
||||
Ok(pipeline)
|
||||
}
|
||||
|
||||
fn copy_video(sample: &gst::Sample) -> Result<VideoFrame, String> {
|
||||
let caps = sample.caps().ok_or("Missing video caps")?;
|
||||
let info = gstreamer_video::VideoInfo::from_caps(caps).map_err(error)?;
|
||||
if info.format() != gstreamer_video::VideoFormat::Rgba
|
||||
|| info.width() > 1920
|
||||
|| info.height() > 1080
|
||||
{
|
||||
return Err("Unbounded or invalid video frame".into());
|
||||
}
|
||||
let buffer = sample.buffer().ok_or("Missing video buffer")?;
|
||||
let frame =
|
||||
gstreamer_video::VideoFrameRef::from_buffer_ref_readable(buffer, &info).map_err(error)?;
|
||||
use gstreamer_video::prelude::VideoFrameExt;
|
||||
let stride = usize::try_from(frame.plane_stride()[0]).map_err(error)?;
|
||||
let bytes = frame.plane_data(0).map_err(error)?;
|
||||
let width = info.width() as usize * 4;
|
||||
let height = info.height() as usize;
|
||||
if stride < width
|
||||
|| bytes.len()
|
||||
< stride
|
||||
.saturating_mul(height.saturating_sub(1))
|
||||
.saturating_add(width)
|
||||
{
|
||||
return Err("Invalid video stride".into());
|
||||
}
|
||||
let mut rgba = Vec::with_capacity(width * height);
|
||||
for row in 0..height {
|
||||
rgba.extend_from_slice(&bytes[row * stride..row * stride + width]);
|
||||
}
|
||||
let result = VideoFrame {
|
||||
width: info.width(),
|
||||
height: info.height(),
|
||||
rgba,
|
||||
timestamp_ns: buffer.pts().map(|v| v.nseconds()),
|
||||
};
|
||||
result.validate()?;
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn audio_pipeline(source: gst::Element, feed: &CaptureFeed) -> Result<gst::Pipeline, String> {
|
||||
let sink = gstreamer_app::AppSink::builder()
|
||||
.max_buffers(2)
|
||||
.drop(true)
|
||||
.sync(false)
|
||||
.build();
|
||||
let level = feed.audio.clone();
|
||||
let received = feed.received_audio.clone();
|
||||
sink.set_callbacks(
|
||||
gstreamer_app::AppSinkCallbacks::builder()
|
||||
.new_sample(move |sink| {
|
||||
let sample = sink.pull_sample().map_err(|_| gst::FlowError::Eos)?;
|
||||
let map = sample
|
||||
.buffer()
|
||||
.ok_or(gst::FlowError::Error)?
|
||||
.map_readable()
|
||||
.map_err(|_| gst::FlowError::Error)?;
|
||||
if map.len() > 48_000 * 8 {
|
||||
return Err(gst::FlowError::Error);
|
||||
}
|
||||
let mut sum = 0.0f64;
|
||||
let mut count = 0u32;
|
||||
for bytes in map.as_slice().as_chunks::<4>().0 {
|
||||
let value = f32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]);
|
||||
if value.is_finite() {
|
||||
sum += f64::from(value) * f64::from(value);
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
received.store(true, Ordering::Relaxed);
|
||||
*level.lock().map_err(|_| gst::FlowError::Error)? = if count == 0 {
|
||||
0.0
|
||||
} else {
|
||||
(sum / f64::from(count)).sqrt().min(1.0) as f32
|
||||
};
|
||||
Ok(gst::FlowSuccess::Ok)
|
||||
})
|
||||
.build(),
|
||||
);
|
||||
let convert = gst::ElementFactory::make("audioconvert")
|
||||
.build()
|
||||
.map_err(error)?;
|
||||
let resample = gst::ElementFactory::make("audioresample")
|
||||
.build()
|
||||
.map_err(error)?;
|
||||
let caps = gst::Caps::builder("audio/x-raw")
|
||||
.field("format", "F32LE")
|
||||
.field("rate", 48_000i32)
|
||||
.field("channels", 2i32)
|
||||
.field("layout", "interleaved")
|
||||
.build();
|
||||
let filter = gst::ElementFactory::make("capsfilter")
|
||||
.property("caps", caps)
|
||||
.build()
|
||||
.map_err(error)?;
|
||||
let pipeline = gst::Pipeline::new();
|
||||
let elements = [&source, &convert, &resample, &filter, sink.upcast_ref()];
|
||||
pipeline.add_many(elements).map_err(error)?;
|
||||
gst::Element::link_many(elements).map_err(error)?;
|
||||
Ok(pipeline)
|
||||
}
|
||||
fn error(e: impl std::fmt::Display) -> String {
|
||||
e.to_string()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn silent_unavailable_source_times_out_and_drop_clears_preview() {
|
||||
let feed = CaptureFeed::default();
|
||||
*feed.video.lock().expect("frame lock") = Some(VideoFrame {
|
||||
width: 1,
|
||||
height: 1,
|
||||
rgba: vec![1, 2, 3, 255],
|
||||
timestamp_ns: None,
|
||||
});
|
||||
*feed.audio.lock().expect("level lock") = 0.5;
|
||||
let session = CaptureSession {
|
||||
pipelines: Vec::new(),
|
||||
portal: None,
|
||||
feed: feed.clone(),
|
||||
started: Instant::now() - Duration::from_secs(11),
|
||||
expects_audio: true,
|
||||
};
|
||||
assert!(
|
||||
session
|
||||
.check()
|
||||
.expect_err("no audio was received")
|
||||
.contains("no fallback")
|
||||
);
|
||||
drop(session);
|
||||
assert!(feed.take_frame().is_none());
|
||||
assert_eq!(feed.audio_level(), 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn synthetic_video_and_audio_reach_bounded_owned_sinks() {
|
||||
gst::init().expect("GStreamer initialization");
|
||||
let feed = CaptureFeed::default();
|
||||
let source = gst::ElementFactory::make("videotestsrc")
|
||||
.property("num-buffers", 3i32)
|
||||
.build()
|
||||
.expect("fixture");
|
||||
let video = video_pipeline(
|
||||
source,
|
||||
&feed,
|
||||
CaptureOptions {
|
||||
max_width: 64,
|
||||
max_height: 48,
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.expect("video pipeline");
|
||||
let source = gst::ElementFactory::make("audiotestsrc")
|
||||
.property("num-buffers", 3i32)
|
||||
.build()
|
||||
.expect("fixture");
|
||||
let audio = audio_pipeline(source, &feed).expect("audio pipeline");
|
||||
for pipeline in [&video, &audio] {
|
||||
pipeline
|
||||
.set_state(gst::State::Playing)
|
||||
.expect("play fixture");
|
||||
let message = pipeline
|
||||
.bus()
|
||||
.expect("bus")
|
||||
.timed_pop_filtered(
|
||||
gst::ClockTime::from_seconds(5),
|
||||
&[gst::MessageType::Eos, gst::MessageType::Error],
|
||||
)
|
||||
.expect("fixture completes");
|
||||
pipeline.set_state(gst::State::Null).expect("stop fixture");
|
||||
assert!(
|
||||
matches!(message.view(), gst::MessageView::Eos(..)),
|
||||
"{message:?}"
|
||||
);
|
||||
}
|
||||
let frame = feed.take_frame().expect("actual fixture frame");
|
||||
frame.validate().expect("bounded frame");
|
||||
assert!(frame.width <= 64 && frame.height <= 48);
|
||||
assert!(feed.audio_level() > 0.01);
|
||||
}
|
||||
}
|
||||
31
src/media/mod.rs
Normal file
31
src/media/mod.rs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
//! User-consented local screen preview and explicitly selected application audio.
|
||||
//!
|
||||
//! Microphone capture remains owned by the voice pipeline. These types never
|
||||
//! substitute a system monitor for an unavailable application stream.
|
||||
mod policy;
|
||||
mod sources;
|
||||
|
||||
pub use policy::{AudioChoice, AudioKind, AudioSource, CaptureOptions, VideoFrame};
|
||||
pub use sources::{audio_sources, parse_audio_sources, validate_audio_choice};
|
||||
|
||||
#[cfg(all(target_os = "linux", feature = "screen-capture"))]
|
||||
mod capture;
|
||||
#[cfg(all(target_os = "linux", feature = "screen-capture"))]
|
||||
mod portal;
|
||||
|
||||
#[cfg(all(target_os = "linux", feature = "screen-capture"))]
|
||||
pub use capture::{CaptureFeed, CaptureSession, check_backend};
|
||||
|
||||
/// User-visible reason when this build cannot open a native screen preview.
|
||||
pub fn unavailable_reason() -> Option<&'static str> {
|
||||
#[cfg(all(target_os = "linux", feature = "screen-capture"))]
|
||||
{
|
||||
None
|
||||
}
|
||||
#[cfg(not(all(target_os = "linux", feature = "screen-capture")))]
|
||||
{
|
||||
Some(
|
||||
"Native capture requires a Linux build with the screen-capture feature. Other platform adapters are not implemented yet.",
|
||||
)
|
||||
}
|
||||
}
|
||||
109
src/media/policy.rs
Normal file
109
src/media/policy.rs
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// A selected audio endpoint is identified by its stable PipeWire object serial,
|
||||
/// not a reusable node ID. There is deliberately no default-device choice.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum AudioChoice {
|
||||
Disabled,
|
||||
Application(u64),
|
||||
System(u64),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum AudioKind {
|
||||
Application,
|
||||
System,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct AudioSource {
|
||||
pub serial: u64,
|
||||
pub name: String,
|
||||
pub kind: AudioKind,
|
||||
}
|
||||
|
||||
/// Local preview limits. Microphone configuration is intentionally absent.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct CaptureOptions {
|
||||
pub audio: AudioChoice,
|
||||
pub max_width: u32,
|
||||
pub max_height: u32,
|
||||
pub max_fps: u32,
|
||||
}
|
||||
|
||||
impl Default for CaptureOptions {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
audio: AudioChoice::Disabled,
|
||||
max_width: 1920,
|
||||
max_height: 1080,
|
||||
max_fps: 30,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CaptureOptions {
|
||||
pub fn validate(&self) -> Result<(), String> {
|
||||
if !(1..=1920).contains(&self.max_width)
|
||||
|| !(1..=1080).contains(&self.max_height)
|
||||
|| !(1..=30).contains(&self.max_fps)
|
||||
{
|
||||
return Err("Preview must fit within 1920×1080 at 1–30 frames/s.".into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Tightly packed RGBA pixels; no PipeWire/GStreamer memory escapes a callback.
|
||||
#[derive(Clone)]
|
||||
pub struct VideoFrame {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub rgba: Vec<u8>,
|
||||
pub timestamp_ns: Option<u64>,
|
||||
}
|
||||
|
||||
impl VideoFrame {
|
||||
pub fn validate(&self) -> Result<(), String> {
|
||||
let expected = (self.width as usize)
|
||||
.checked_mul(self.height as usize)
|
||||
.and_then(|pixels| pixels.checked_mul(4));
|
||||
if self.width == 0
|
||||
|| self.height == 0
|
||||
|| self.width > 1920
|
||||
|| self.height > 1080
|
||||
|| expected != Some(self.rgba.len())
|
||||
{
|
||||
return Err("Invalid preview frame dimensions or payload.".into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn preview_defaults_to_no_audio_and_rejects_unbounded_frames() {
|
||||
assert_eq!(CaptureOptions::default().audio, AudioChoice::Disabled);
|
||||
assert!(
|
||||
CaptureOptions {
|
||||
max_width: u32::MAX,
|
||||
..Default::default()
|
||||
}
|
||||
.validate()
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
VideoFrame {
|
||||
width: u32::MAX,
|
||||
height: u32::MAX,
|
||||
rgba: vec![],
|
||||
timestamp_ns: None
|
||||
}
|
||||
.validate()
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
}
|
||||
228
src/media/portal.rs
Normal file
228
src/media/portal.rs
Normal file
|
|
@ -0,0 +1,228 @@
|
|||
//! ScreenCast portal session. The portal, rather than VNOX, owns source consent.
|
||||
use std::{collections::HashMap, os::fd::OwnedFd};
|
||||
use zbus::{
|
||||
Connection, OwnedObjectPath, OwnedVariant as OwnedValue, Proxy, Variant as Value,
|
||||
export::futures_core::Stream,
|
||||
};
|
||||
|
||||
type Options<'a> = HashMap<&'a str, Value<'a>>;
|
||||
type Results = HashMap<String, OwnedValue>;
|
||||
|
||||
pub struct PortalSession {
|
||||
_lease: SessionLease,
|
||||
pub remote: OwnedFd,
|
||||
pub node: u32,
|
||||
pub serial: Option<u64>,
|
||||
}
|
||||
|
||||
impl PortalSession {
|
||||
pub async fn choose() -> Result<Self, String> {
|
||||
let connection = Connection::session().await.map_err(err)?;
|
||||
let proxy = Proxy::new(
|
||||
&connection,
|
||||
"org.freedesktop.portal.Desktop",
|
||||
"/org/freedesktop/portal/desktop",
|
||||
"org.freedesktop.portal.ScreenCast",
|
||||
)
|
||||
.await
|
||||
.map_err(err)?;
|
||||
let available: u32 = proxy
|
||||
.get_property("AvailableSourceTypes")
|
||||
.await
|
||||
.map_err(err)?;
|
||||
let source_types = available & 3;
|
||||
if source_types == 0 {
|
||||
return Err("Desktop portal offers no screen or window sources".into());
|
||||
}
|
||||
let cursor_modes: u32 = proxy
|
||||
.get_property("AvailableCursorModes")
|
||||
.await
|
||||
.unwrap_or(1);
|
||||
let version: u32 = proxy.get_property("version").await.unwrap_or(1);
|
||||
let session_token = format!("vnox_{}", uuid::Uuid::new_v4().simple());
|
||||
let mut options = Options::new();
|
||||
options.insert("session_handle_token", Value::from(session_token.as_str()));
|
||||
let result = request(&connection, &proxy, "CreateSession", None, None, options).await?;
|
||||
let path = result
|
||||
.get("session_handle")
|
||||
.ok_or("Portal omitted session handle")?;
|
||||
let path = OwnedObjectPath::try_from(path.try_clone().map_err(err)?).map_err(err)?;
|
||||
let lease = SessionLease {
|
||||
connection: connection.clone(),
|
||||
path: path.clone(),
|
||||
};
|
||||
let result = async {
|
||||
let mut options = Options::new();
|
||||
options.insert("types", Value::from(source_types));
|
||||
options.insert("multiple", Value::from(false));
|
||||
if cursor_modes & 2 != 0 {
|
||||
options.insert("cursor_mode", Value::from(2u32));
|
||||
} else if cursor_modes & 1 != 0 {
|
||||
options.insert("cursor_mode", Value::from(1u32));
|
||||
}
|
||||
if version >= 4 {
|
||||
options.insert("persist_mode", Value::from(0u32));
|
||||
}
|
||||
request(
|
||||
&connection,
|
||||
&proxy,
|
||||
"SelectSources",
|
||||
Some(&path),
|
||||
None,
|
||||
options,
|
||||
)
|
||||
.await?;
|
||||
let mut results = request(
|
||||
&connection,
|
||||
&proxy,
|
||||
"Start",
|
||||
Some(&path),
|
||||
Some(""),
|
||||
Options::new(),
|
||||
)
|
||||
.await?;
|
||||
let streams = results
|
||||
.remove("streams")
|
||||
.ok_or("Portal omitted video stream")?;
|
||||
let streams: Vec<(u32, Results)> = streams.try_into().map_err(err)?;
|
||||
if streams.len() != 1 {
|
||||
return Err("Choose exactly one screen or window".into());
|
||||
}
|
||||
let (node, properties) = streams
|
||||
.into_iter()
|
||||
.next()
|
||||
.ok_or("Portal omitted video stream")?;
|
||||
let serial = properties
|
||||
.get("pipewire-serial")
|
||||
.and_then(|v| u64::try_from(v).ok());
|
||||
let fd: zbus::OwnedFd = proxy
|
||||
.call("OpenPipeWireRemote", &(&path, Options::new()))
|
||||
.await
|
||||
.map_err(err)?;
|
||||
Ok((node, serial, fd.into()))
|
||||
}
|
||||
.await;
|
||||
match result {
|
||||
Ok((node, serial, remote)) => Ok(Self {
|
||||
_lease: lease,
|
||||
remote,
|
||||
node,
|
||||
serial,
|
||||
}),
|
||||
Err(error) => {
|
||||
close(&connection, &path).await;
|
||||
Err(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct SessionLease {
|
||||
connection: Connection,
|
||||
path: OwnedObjectPath,
|
||||
}
|
||||
impl Drop for SessionLease {
|
||||
fn drop(&mut self) {
|
||||
let connection = self.connection.clone();
|
||||
let path = self.path.clone();
|
||||
if let Ok(runtime) = tokio::runtime::Handle::try_current() {
|
||||
runtime.spawn(async move {
|
||||
close(&connection, &path).await;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct RequestLease {
|
||||
connection: Connection,
|
||||
path: String,
|
||||
pending: bool,
|
||||
}
|
||||
impl Drop for RequestLease {
|
||||
fn drop(&mut self) {
|
||||
if !self.pending {
|
||||
return;
|
||||
}
|
||||
let connection = self.connection.clone();
|
||||
let path = self.path.clone();
|
||||
if let Ok(runtime) = tokio::runtime::Handle::try_current() {
|
||||
runtime.spawn(async move {
|
||||
if let Ok(proxy) = Proxy::new(
|
||||
&connection,
|
||||
"org.freedesktop.portal.Desktop",
|
||||
path.as_str(),
|
||||
"org.freedesktop.portal.Request",
|
||||
)
|
||||
.await
|
||||
{
|
||||
let _: Result<(), _> = proxy.call("Close", &()).await;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn close(connection: &Connection, path: &OwnedObjectPath) {
|
||||
if let Ok(proxy) = Proxy::new(
|
||||
connection,
|
||||
"org.freedesktop.portal.Desktop",
|
||||
path,
|
||||
"org.freedesktop.portal.Session",
|
||||
)
|
||||
.await
|
||||
{
|
||||
let _: Result<(), _> = proxy.call("Close", &()).await;
|
||||
}
|
||||
}
|
||||
|
||||
async fn request(
|
||||
connection: &Connection,
|
||||
proxy: &Proxy<'_>,
|
||||
method: &str,
|
||||
session: Option<&OwnedObjectPath>,
|
||||
parent: Option<&str>,
|
||||
mut options: Options<'_>,
|
||||
) -> Result<Results, String> {
|
||||
let token = format!("vnox_{}", uuid::Uuid::new_v4().simple());
|
||||
let sender = connection
|
||||
.unique_name()
|
||||
.ok_or("D-Bus has no unique name")?
|
||||
.as_str()
|
||||
.trim_start_matches(':')
|
||||
.replace('.', "_");
|
||||
let path = format!("/org/freedesktop/portal/desktop/request/{sender}/{token}");
|
||||
let mut lease = RequestLease {
|
||||
connection: connection.clone(),
|
||||
path: path.clone(),
|
||||
pending: true,
|
||||
};
|
||||
let response = Proxy::new(
|
||||
connection,
|
||||
"org.freedesktop.portal.Desktop",
|
||||
path.as_str(),
|
||||
"org.freedesktop.portal.Request",
|
||||
)
|
||||
.await
|
||||
.map_err(err)?;
|
||||
let mut signals = response.receive_signal("Response").await.map_err(err)?;
|
||||
options.insert("handle_token", Value::from(token));
|
||||
let _: OwnedObjectPath = match (session, parent) {
|
||||
(None, _) => proxy.call(method, &(options,)).await,
|
||||
(Some(session), None) => proxy.call(method, &(session, options)).await,
|
||||
(Some(session), Some(parent)) => proxy.call(method, &(session, parent, options)).await,
|
||||
}
|
||||
.map_err(err)?;
|
||||
let message = std::future::poll_fn(|cx| std::pin::Pin::new(&mut signals).poll_next(cx))
|
||||
.await
|
||||
.ok_or("Portal response stream ended")?;
|
||||
lease.pending = false;
|
||||
let (code, result): (u32, Results) = message.body().deserialize().map_err(err)?;
|
||||
match code {
|
||||
0 => Ok(result),
|
||||
1 => Err("Screen selection cancelled".into()),
|
||||
_ => Err("Desktop portal rejected capture".into()),
|
||||
}
|
||||
}
|
||||
fn err(error: impl std::fmt::Display) -> String {
|
||||
error.to_string()
|
||||
}
|
||||
175
src/media/sources.rs
Normal file
175
src/media/sources.rs
Normal file
|
|
@ -0,0 +1,175 @@
|
|||
use super::{AudioChoice, AudioKind, AudioSource};
|
||||
|
||||
const MAX_GRAPH_BYTES: usize = 2 * 1024 * 1024;
|
||||
|
||||
/// Parse only playback applications and output sinks. Input/microphone nodes
|
||||
/// never appear in the screen-audio selector.
|
||||
pub fn parse_audio_sources(json: &[u8], own_pid: u32) -> Result<Vec<AudioSource>, String> {
|
||||
if json.len() > MAX_GRAPH_BYTES {
|
||||
return Err("Audio source inventory exceeds the supported size.".into());
|
||||
}
|
||||
let graph: Vec<serde_json::Value> = serde_json::from_slice(json)
|
||||
.map_err(|_| "Unable to read PipeWire audio source inventory.".to_string())?;
|
||||
let mut result = Vec::new();
|
||||
for item in graph {
|
||||
if item["type"] != "PipeWire:Interface:Node" {
|
||||
continue;
|
||||
}
|
||||
let props = &item["info"]["props"];
|
||||
let kind = match props["media.class"].as_str() {
|
||||
Some("Stream/Output/Audio") => AudioKind::Application,
|
||||
Some("Audio/Sink") => AudioKind::System,
|
||||
_ => continue,
|
||||
};
|
||||
let pid = number(&props["application.process.id"]);
|
||||
let application = props["application.name"].as_str().unwrap_or("");
|
||||
let node_name = props["node.name"].as_str().unwrap_or("");
|
||||
if pid == Some(u64::from(own_pid))
|
||||
|| application.to_lowercase().contains("vnox")
|
||||
|| node_name.to_lowercase().contains("vnox")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let Some(serial) = number(&props["object.serial"]).filter(|serial| *serial > 0) else {
|
||||
continue;
|
||||
};
|
||||
let name = if kind == AudioKind::Application {
|
||||
props["application.name"]
|
||||
.as_str()
|
||||
.or_else(|| props["media.name"].as_str())
|
||||
} else {
|
||||
props["node.description"].as_str()
|
||||
}
|
||||
.unwrap_or(node_name)
|
||||
.chars()
|
||||
.filter(|c| !c.is_control())
|
||||
.take(100)
|
||||
.collect::<String>();
|
||||
if name.trim().is_empty() {
|
||||
continue;
|
||||
}
|
||||
result.push(AudioSource { serial, name, kind });
|
||||
}
|
||||
result.sort_by(|a, b| a.name.cmp(&b.name).then(a.serial.cmp(&b.serial)));
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
result.retain(|source| seen.insert(source.serial));
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn number(value: &serde_json::Value) -> Option<u64> {
|
||||
value.as_u64().or_else(|| value.as_str()?.parse().ok())
|
||||
}
|
||||
|
||||
/// Check against a fresh inventory. Missing application audio is an error,
|
||||
/// never an instruction to use a different endpoint or the system monitor.
|
||||
pub fn validate_audio_choice(choice: AudioChoice, current: &[AudioSource]) -> Result<(), String> {
|
||||
let (serial, kind) = match choice {
|
||||
AudioChoice::Disabled => return Ok(()),
|
||||
AudioChoice::Application(serial) => (serial, AudioKind::Application),
|
||||
AudioChoice::System(serial) => (serial, AudioKind::System),
|
||||
};
|
||||
if current
|
||||
.iter()
|
||||
.any(|source| source.serial == serial && source.kind == kind)
|
||||
{
|
||||
Ok(())
|
||||
} else {
|
||||
Err("The selected audio source is no longer available. Choose it again; system audio was not enabled.".into())
|
||||
}
|
||||
}
|
||||
|
||||
/// Enumerate metadata only. This function does not record or play audio.
|
||||
#[cfg(target_os = "linux")]
|
||||
pub async fn audio_sources() -> Result<Vec<AudioSource>, String> {
|
||||
use std::{process::Stdio, time::Duration};
|
||||
use tokio::{io::AsyncReadExt, process::Command};
|
||||
let scan = async {
|
||||
let mut child = Command::new("pw-dump")
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::null())
|
||||
.kill_on_drop(true)
|
||||
.spawn()
|
||||
.map_err(|_| "PipeWire source discovery requires pw-dump.".to_string())?;
|
||||
let mut bytes = Vec::new();
|
||||
child
|
||||
.stdout
|
||||
.take()
|
||||
.ok_or("Audio inventory pipe unavailable.")?
|
||||
.take(MAX_GRAPH_BYTES as u64 + 1)
|
||||
.read_to_end(&mut bytes)
|
||||
.await
|
||||
.map_err(|_| "Unable to read audio inventory.".to_string())?;
|
||||
if bytes.len() > MAX_GRAPH_BYTES {
|
||||
return Err("Audio inventory exceeds the supported size.".into());
|
||||
}
|
||||
let status = child
|
||||
.wait()
|
||||
.await
|
||||
.map_err(|_| "Audio inventory process failed.".to_string())?;
|
||||
if !status.success() {
|
||||
return Err("PipeWire audio inventory is unavailable.".into());
|
||||
}
|
||||
parse_audio_sources(&bytes, std::process::id())
|
||||
};
|
||||
tokio::time::timeout(Duration::from_secs(3), scan)
|
||||
.await
|
||||
.map_err(|_| "PipeWire audio source discovery timed out.".to_string())?
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
pub async fn audio_sources() -> Result<Vec<AudioSource>, String> {
|
||||
Err("Application-audio selection is not implemented on this platform yet.".into())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn node(class: &str, serial: u64, pid: u32, name: &str) -> serde_json::Value {
|
||||
serde_json::json!({"type":"PipeWire:Interface:Node","info":{"props":{
|
||||
"media.class":class,"object.serial":serial,"application.process.id":pid,
|
||||
"application.name":name,"node.description":name,"node.name":name
|
||||
}}})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inventory_excludes_microphones_own_audio_and_unstable_ids() {
|
||||
let graph = serde_json::to_vec(&vec![
|
||||
node("Stream/Output/Audio", 10, 100, "Game"),
|
||||
node("Audio/Source", 11, 200, "Microphone"),
|
||||
node("Stream/Output/Audio", 12, 999, "Own capture"),
|
||||
node("Stream/Output/Audio", 13, 1, "Vnox playback"),
|
||||
node("Audio/Sink", 14, 1, "Headphones"),
|
||||
node("Stream/Output/Audio", 0, 2, "No serial"),
|
||||
])
|
||||
.unwrap();
|
||||
let sources = parse_audio_sources(&graph, 999).unwrap();
|
||||
assert_eq!(
|
||||
sources
|
||||
.iter()
|
||||
.map(|source| source.serial)
|
||||
.collect::<Vec<_>>(),
|
||||
vec![10, 14]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unavailable_application_never_becomes_system_capture() {
|
||||
let system = AudioSource {
|
||||
serial: 14,
|
||||
name: "Headphones".into(),
|
||||
kind: AudioKind::System,
|
||||
};
|
||||
assert!(
|
||||
validate_audio_choice(AudioChoice::Application(99), std::slice::from_ref(&system))
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
validate_audio_choice(AudioChoice::Application(14), std::slice::from_ref(&system))
|
||||
.is_err()
|
||||
);
|
||||
assert!(validate_audio_choice(AudioChoice::System(14), &[system]).is_ok());
|
||||
assert!(validate_audio_choice(AudioChoice::Disabled, &[]).is_ok());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue