1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
use pipewire::context::ContextRc;
use pipewire::main_loop::MainLoopRc;
use std::any::Any;
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::{Arc, Mutex};
use tokio::sync::watch;
use wayclip_core::models::error::WayclipError;
pub const MAX_CONNECT_ATTEMPTS: usize = 10;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PipewireNodeType {
Source,
Sink,
Unknown,
}
impl From<&str> for PipewireNodeType {
fn from(value: &str) -> Self {
match value {
"Audio/Source" => Self::Source,
"Audio/Sink" => Self::Sink,
_ => Self::Unknown,
}
}
}
#[derive(Debug, Clone)]
pub struct PipewireDevice {
pub node_type: PipewireNodeType,
pub node_name: String,
pub node_description: String,
pub node_id: u32,
}
#[derive(Clone, Debug, Default)]
pub struct PipewireState {
pub devices: Vec<PipewireDevice>,
pub default_sink_node_name: Option<String>,
pub default_source_node_name: Option<String>,
}
#[derive(Clone, Debug)]
pub struct PipewireManager {
state_rx: watch::Receiver<PipewireState>,
}
impl PipewireManager {
pub fn new() -> Result<Self, WayclipError> {
pipewire::init();
let (state_tx, state_rx) = watch::channel(PipewireState::default());
std::thread::spawn(move || {
let run_loop = || -> Result<(), WayclipError> {
let mainloop = MainLoopRc::new(None)
.map_err(|e| WayclipError::Pipewire(e.to_string().into()))?;
let context = ContextRc::new(&mainloop, None)
.map_err(|e| WayclipError::Pipewire(e.to_string().into()))?;
let mut connection_attempts = 0;
let core = loop {
match context.connect_rc(None) {
Ok(core) => break core,
Err(error) if connection_attempts < MAX_CONNECT_ATTEMPTS => {
log::warn!(
"PipeWire connection failed on attempt {}: {}",
connection_attempts + 1,
error
);
connection_attempts += 1;
std::thread::sleep(std::time::Duration::from_millis(500));
}
Err(error) => {
return Err(WayclipError::Pipewire(
format!(
"Failed to connect to PipeWire after {} attempts: {}",
connection_attempts, error,
)
.into(),
));
}
}
};
let registry = core
.get_registry_rc()
.map_err(|e| WayclipError::Pipewire(e.to_string().into()))?;
let state = Arc::new(Mutex::new(PipewireState::default()));
let listeners: Rc<RefCell<Vec<Box<dyn Any>>>> = Rc::new(RefCell::new(Vec::new()));
let listeners_weak = Rc::downgrade(&listeners);
let registry_for_metadata = registry.clone();
let core_for_metadata = core.clone();
let state_for_add = Arc::clone(&state);
let state_tx_for_add = state_tx.clone();
let state_for_remove = Arc::clone(&state);
let state_tx_for_remove = state_tx.clone();
let registry_listener = registry
.add_listener_local()
.global(move |global| {
log::trace!(
"PipeWire global: id={}, type={:?}, props={:?}",
global.id,
global.type_,
global.props
);
if global.type_ == pipewire::types::ObjectType::Node
&& let Some(props) = global.props.as_ref()
&& let Some(media_class) = props.get("media.class")
&& let Some(node_name) = props.get("node.name")
&& media_class.starts_with("Audio/")
&& media_class != "Audio/Stream"
{
let node_description =
props.get("node.description").unwrap_or(node_name);
let mut state = match state_for_add.lock() {
Ok(guard) => guard,
Err(error) => {
log::error!(
"Could not acquire PipeWire state lock: {}",
error
);
return;
}
};
if !state
.devices
.iter()
.any(|device| device.node_id == global.id)
{
state.devices.push(PipewireDevice {
node_type: media_class.into(),
node_name: node_name.to_owned(),
node_description: node_description.to_owned(),
node_id: global.id,
});
log::debug!(
"Discovered audio node: id={}, class={}, name={}, description={}",
global.id,
media_class,
node_name,
node_description
);
}
let _ = state_tx_for_add.send(state.clone());
}
if global.type_ == pipewire::types::ObjectType::Metadata
&& let Some(props) = global.props.as_ref()
&& props.get("metadata.name") == Some("default")
{
let metadata = match registry_for_metadata
.bind::<pipewire::metadata::Metadata, _>(global)
{
Ok(metadata) => metadata,
Err(error) => {
log::error!(
"Failed to bind PipeWire default metadata: {}",
error
);
return;
}
};
let state_for_metadata = Arc::clone(&state_for_add);
let state_tx_for_metadata = state_tx_for_add.clone();
let core_for_sync = core_for_metadata.clone();
let metadata_listener = metadata
.add_listener_local()
.property(move |_subject, key, _type, value| {
let Some(key) = key else {
return 0;
};
if key != "default.audio.sink"
&& key != "default.audio.source"
{
return 0;
}
let parsed_name = value.and_then(|value| {
serde_json::from_str::<serde_json::Value>(value)
.ok()
.and_then(|json| {
json.get("name")?
.as_str()
.map(ToOwned::to_owned)
})
});
let Ok(mut state) = state_for_metadata.lock() else {
log::error!(
"Could not acquire PipeWire state lock while reading metadata"
);
return 0;
};
match key {
"default.audio.sink" => {
state.default_sink_node_name = parsed_name.clone();
log::debug!(
"PipeWire default sink changed: {:?}",
parsed_name
);
}
"default.audio.source" => {
state.default_source_node_name = parsed_name.clone();
log::debug!(
"PipeWire default source changed: {:?}",
parsed_name
);
}
_ => {}
}
let _ = state_tx_for_metadata.send(state.clone());
0
})
.register();
if let Some(listeners_rc) = listeners_weak.upgrade() {
let mut listeners = listeners_rc.borrow_mut();
listeners.push(Box::new(metadata_listener));
listeners.push(Box::new(metadata));
}
if let Err(error) = core_for_sync.sync(0) {
log::error!(
"Failed to synchronize PipeWire metadata: {}",
error
);
}
}
})
.global_remove(move |id| {
let Ok(mut state) = state_for_remove.lock() else {
log::error!(
"Could not acquire PipeWire state lock while removing node"
);
return;
};
let old_len = state.devices.len();
state.devices.retain(|device| device.node_id != id);
if state.devices.len() != old_len {
log::debug!("Removed PipeWire node with id={}", id);
let _ = state_tx_for_remove.send(state.clone());
}
})
.register();
listeners.borrow_mut().push(Box::new(registry_listener));
core.sync(0)
.map_err(|e| WayclipError::Pipewire(e.to_string().into()))?;
mainloop.run();
Ok(())
};
if let Err(error) = run_loop() {
log::error!("PipeWire background thread stopped: {:?}", error);
}
});
Ok(Self { state_rx })
}
pub fn get_node_id_from_node_name(&self, node_name: &str) -> Option<u32> {
self.current_state()
.devices
.iter()
.find(|device| device.node_name == node_name)
.map(|device| device.node_id)
}
pub fn is_node_name_valid(&self, node_name: &str) -> bool {
self.current_state()
.devices
.iter()
.any(|device| device.node_name == node_name)
}
pub fn subscribe(&self) -> watch::Receiver<PipewireState> {
self.state_rx.clone()
}
pub fn current_state(&self) -> PipewireState {
self.state_rx.borrow().clone()
}
pub fn get_default_sink_name(&self) -> Option<String> {
self.current_state().default_sink_node_name
}
pub fn get_default_source_name(&self) -> Option<String> {
self.current_state().default_source_node_name
}
}