mirror of
https://gitlab.freedesktop.org/pipewire/helvum
synced 2026-03-15 11:36:11 +08:00
Modify architecture to run pipewire loop in second thread.
The pipewire loop now runs without interruption in a second thread and communicates with the GTK thread via a channel in each direction, instead of checking for events once a second and using callbacks. This allows changes to appear instantly in the view, instead of having to wait.
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
use std::{cell::RefCell, collections::HashMap, rc::Rc};
|
||||
|
||||
use gtk::glib::{self, clone};
|
||||
use libspa::{ForeignDict, ReadableDict};
|
||||
use gtk::glib::{self, clone, Continue, Receiver};
|
||||
use log::{info, warn};
|
||||
use pipewire::{port::Direction, registry::GlobalObject, types::ObjectType};
|
||||
use pipewire::spa::Direction;
|
||||
|
||||
use crate::{pipewire_connection::PipewireConnection, view};
|
||||
use crate::{view, PipewireLink, PipewireMessage};
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub enum MediaType {
|
||||
Audio,
|
||||
Video,
|
||||
@@ -41,7 +40,6 @@ enum Item {
|
||||
///
|
||||
/// It also keeps and manages a state object that contains the current state of objects present on the remote.
|
||||
pub struct Controller {
|
||||
con: Rc<RefCell<PipewireConnection>>,
|
||||
state: HashMap<u32, Item>,
|
||||
view: Rc<view::View>,
|
||||
}
|
||||
@@ -56,95 +54,52 @@ impl Controller {
|
||||
/// will also drop the controller, unless the `Rc` is cloned outside of this function.
|
||||
pub(super) fn new(
|
||||
view: Rc<view::View>,
|
||||
con: Rc<RefCell<PipewireConnection>>,
|
||||
gtk_receiver: Receiver<PipewireMessage>,
|
||||
) -> Rc<RefCell<Controller>> {
|
||||
let result = Rc::new(RefCell::new(Controller {
|
||||
con,
|
||||
view,
|
||||
state: HashMap::new(),
|
||||
}));
|
||||
|
||||
result
|
||||
.borrow()
|
||||
.con
|
||||
.borrow_mut()
|
||||
.on_global_add(Some(Box::new(
|
||||
clone!(@weak result as this => move |global| {
|
||||
this.borrow_mut().global_add(global);
|
||||
}),
|
||||
)));
|
||||
result
|
||||
.borrow()
|
||||
.con
|
||||
.borrow_mut()
|
||||
.on_global_remove(Some(Box::new(clone!(@weak result as this => move |id| {
|
||||
this.borrow_mut().global_remove(id);
|
||||
}))));
|
||||
// React to messages received from the pipewire thread.
|
||||
gtk_receiver.attach(
|
||||
None,
|
||||
clone!(
|
||||
@weak result as controller => @default-return Continue(true),
|
||||
move |msg| {
|
||||
match msg {
|
||||
PipewireMessage::NodeAdded {
|
||||
id,
|
||||
name,
|
||||
media_type,
|
||||
} => controller.borrow_mut().add_node(id, name, media_type),
|
||||
PipewireMessage::PortAdded {
|
||||
id,
|
||||
node_id,
|
||||
name,
|
||||
direction,
|
||||
} => controller
|
||||
.borrow_mut()
|
||||
.add_port(id, node_id, name, direction),
|
||||
PipewireMessage::LinkAdded { id, link } => controller.borrow_mut().add_link(id, link),
|
||||
PipewireMessage::ObjectRemoved { id } => controller.borrow_mut().remove_global(id),
|
||||
};
|
||||
Continue(true)
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
/// Handle a new global object being added.
|
||||
/// Relevant objects are displayed to the user and/or stored to the state.
|
||||
///
|
||||
/// It is called from the `PipewireConnection` via callback.
|
||||
fn global_add(&mut self, global: &GlobalObject<ForeignDict>) {
|
||||
match global.type_ {
|
||||
ObjectType::Node => {
|
||||
self.add_node(global);
|
||||
}
|
||||
ObjectType::Port => {
|
||||
self.add_port(global);
|
||||
}
|
||||
ObjectType::Link => {
|
||||
self.add_link(global);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle a node object being added.
|
||||
fn add_node(&mut self, node: &GlobalObject<ForeignDict>) {
|
||||
info!("Adding node to graph: id {}", node.id);
|
||||
pub(super) fn add_node(&mut self, id: u32, name: String, media_type: Option<MediaType>) {
|
||||
info!("Adding node to graph: id {}", id);
|
||||
|
||||
// Get the nicest possible name for the node, using a fallback chain of possible name attributes.
|
||||
let node_name = &node
|
||||
.props
|
||||
.as_ref()
|
||||
.map(|dict| {
|
||||
String::from(
|
||||
dict.get("node.nick")
|
||||
.or_else(|| dict.get("node.description"))
|
||||
.or_else(|| dict.get("node.name"))
|
||||
.unwrap_or_default(),
|
||||
)
|
||||
})
|
||||
.unwrap_or_default();
|
||||
|
||||
// FIXME: This relies on the node being passed to us by the pipwire server before its port.
|
||||
let media_type = node
|
||||
.props
|
||||
.as_ref()
|
||||
.map(|props| {
|
||||
props.get("media.class").map(|class| {
|
||||
if class.contains("Audio") {
|
||||
Some(MediaType::Audio)
|
||||
} else if class.contains("Video") {
|
||||
Some(MediaType::Video)
|
||||
} else if class.contains("Midi") {
|
||||
Some(MediaType::Midi)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
})
|
||||
.flatten()
|
||||
.flatten();
|
||||
|
||||
self.view.add_node(node.id, node_name);
|
||||
self.view.add_node(id, name.as_str());
|
||||
|
||||
self.state.insert(
|
||||
node.id,
|
||||
id,
|
||||
Item::Node {
|
||||
// widget: node_widget,
|
||||
media_type,
|
||||
@@ -153,94 +108,43 @@ impl Controller {
|
||||
}
|
||||
|
||||
/// Handle a port object being added.
|
||||
fn add_port(&mut self, port: &GlobalObject<ForeignDict>) {
|
||||
info!("Adding port to graph: id {}", port.id);
|
||||
pub(super) fn add_port(&mut self, id: u32, node_id: u32, name: String, direction: Direction) {
|
||||
info!("Adding port to graph: id {}", id);
|
||||
|
||||
// Update graph to contain the new port.
|
||||
let props = port
|
||||
.props
|
||||
.as_ref()
|
||||
.expect("Port object is missing properties");
|
||||
let port_label = props.get("port.name").unwrap_or_default().to_string();
|
||||
let node_id: u32 = props
|
||||
.get("node.id")
|
||||
.expect("Port has no node.id property!")
|
||||
.parse()
|
||||
.expect("Could not parse node.id property");
|
||||
|
||||
// Find out the nodes media type so that the port can be colored.
|
||||
let media_type = if let Some(Item::Node { media_type, .. }) = self.state.get(&node_id) {
|
||||
media_type.to_owned()
|
||||
} else {
|
||||
warn!("Node not found for Port {}", port.id);
|
||||
warn!("Node not found for Port {}", id);
|
||||
None
|
||||
};
|
||||
|
||||
self.view.add_port(
|
||||
node_id,
|
||||
port.id,
|
||||
&port_label,
|
||||
if matches!(props.get("port.direction"), Some("in")) {
|
||||
Direction::Input
|
||||
} else {
|
||||
Direction::Output
|
||||
},
|
||||
media_type,
|
||||
);
|
||||
self.view
|
||||
.add_port(node_id, id, &name, direction, media_type);
|
||||
|
||||
// Save node_id so we can delete this port easily.
|
||||
self.state.insert(port.id, Item::Port { node_id });
|
||||
self.state.insert(id, Item::Port { node_id });
|
||||
}
|
||||
|
||||
/// Handle a link object being added.
|
||||
fn add_link(&mut self, link: &GlobalObject<ForeignDict>) {
|
||||
info!("Adding link to graph: id {}", link.id);
|
||||
pub(super) fn add_link(&mut self, id: u32, link: PipewireLink) {
|
||||
info!("Adding link to graph: id {}", id);
|
||||
|
||||
// FIXME: Links should be colored depending on the data they carry (video, audio, midi) like ports are.
|
||||
|
||||
self.state.insert(link.id, Item::Link);
|
||||
self.state.insert(id, Item::Link);
|
||||
|
||||
// Update graph to contain the new link.
|
||||
let props = link
|
||||
.props
|
||||
.as_ref()
|
||||
.expect("Link object is missing properties");
|
||||
let input_node: u32 = props
|
||||
.get("link.input.node")
|
||||
.expect("Link has no link.input.node property")
|
||||
.parse()
|
||||
.expect("Could not parse link.input.node property");
|
||||
let input_port: u32 = props
|
||||
.get("link.input.port")
|
||||
.expect("Link has no link.input.port property")
|
||||
.parse()
|
||||
.expect("Could not parse link.input.port property");
|
||||
let output_node: u32 = props
|
||||
.get("link.output.node")
|
||||
.expect("Link has no link.input.node property")
|
||||
.parse()
|
||||
.expect("Could not parse link.input.node property");
|
||||
let output_port: u32 = props
|
||||
.get("link.output.port")
|
||||
.expect("Link has no link.output.port property")
|
||||
.parse()
|
||||
.expect("Could not parse link.output.port property");
|
||||
self.view.add_link(
|
||||
link.id,
|
||||
crate::PipewireLink {
|
||||
node_from: output_node,
|
||||
port_from: output_port,
|
||||
node_to: input_node,
|
||||
port_to: input_port,
|
||||
},
|
||||
);
|
||||
self.view.add_link(id, link);
|
||||
}
|
||||
|
||||
/// Handle a globalobject being removed.
|
||||
/// Relevant objects are removed from the view and/or the state.
|
||||
///
|
||||
/// This is called from the `PipewireConnection` via callback.
|
||||
fn global_remove(&mut self, id: u32) {
|
||||
pub(super) fn remove_global(&mut self, id: u32) {
|
||||
if let Some(item) = self.state.remove(&id) {
|
||||
match item {
|
||||
Item::Node { .. } => {
|
||||
|
||||
Reference in New Issue
Block a user