forked from rustfixbot/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list-sinks.rs
65 lines (56 loc) · 2.16 KB
/
list-sinks.rs
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
use std::{ffi::CString, os::unix::net::UnixStream};
use pulseaudio::protocol;
pub fn main() -> Result<(), Box<dyn std::error::Error>> {
// Find and connect to PulseAudio. The socket is usually in a well-known
// location under XDG_RUNTIME_DIR.
let socket_path = pulseaudio::socket_path_from_env().ok_or("PulseAudio not available")?;
let mut sock = std::io::BufReader::new(UnixStream::connect(socket_path)?);
// PulseAudio usually puts an authentication "cookie" in ~/.config/pulse/cookie.
let cookie = pulseaudio::cookie_path_from_env()
.and_then(|path| std::fs::read(path).ok())
.unwrap_or_default();
let auth = protocol::AuthParams {
version: protocol::MAX_VERSION,
supports_shm: false,
supports_memfd: false,
cookie,
};
// Write the auth "command" to the socket, and read the reply. The reply
// contains the negotiated protocol version.
protocol::write_command_message(
sock.get_mut(),
0,
protocol::Command::Auth(auth),
protocol::MAX_VERSION,
)?;
let (_, auth_info) =
protocol::read_reply_message::<protocol::AuthReply>(&mut sock, protocol::MAX_VERSION)?;
let protocol_version = std::cmp::min(protocol::MAX_VERSION, auth_info.version);
// The next step is to set the client name.
let mut props = protocol::Props::new();
props.set(
protocol::Prop::ApplicationName,
CString::new("list-sinks").unwrap(),
);
protocol::write_command_message(
sock.get_mut(),
1,
protocol::Command::SetClientName(props),
protocol_version,
)?;
let _ =
protocol::read_reply_message::<protocol::SetClientNameReply>(&mut sock, protocol_version)?;
// Finally, write a command to get the list of sinks. The reply contains the information we're after.
protocol::write_command_message(
sock.get_mut(),
2,
protocol::Command::GetSinkInfoList,
protocol_version,
)?;
let (_, info_list) =
protocol::read_reply_message::<protocol::SinkInfoList>(&mut sock, protocol_version)?;
for info in info_list {
println!("{:#?}", info);
}
Ok(())
}