forked from rustfixbot/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rs
451 lines (392 loc) · 15.9 KB
/
server.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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//! A simple pulseaudio server that accepts playback streams and drops any
//! samples sent to it.
//!
//! To start the server, run:
//! cargo run --example server -- /tmp/pulseaudio.sock
//!
//! Then you can (for example) list sinks with:
//! PULSE_SERVER="unix:/tmp/pulseaudio.sock" pactl list sinks
use std::collections::{HashMap, VecDeque};
use std::io::{Cursor, Read, Write};
use std::time;
use anyhow::bail;
use mio::net::UnixListener;
use mio::{Events, Poll, Token};
use pulseaudio::protocol::{self, write_error};
const CLOCK_SPEED_HZ: u64 = 100;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StreamState {
Prebuffering(u64), // The number of bytes remaining before we can start 'playback'.
Playing,
Draining(u32), // The seq of the drain request, so we can ack it.
}
#[derive(Debug)]
struct PlaybackStream {
state: StreamState,
sample_spec: protocol::SampleSpec,
buffer_attr: protocol::stream::BufferAttr,
buffer: VecDeque<u8>,
requested_bytes: usize,
}
#[derive(Debug)]
struct Client {
id: u32,
socket: mio::net::UnixStream,
playback_streams: HashMap<u32, PlaybackStream>,
authenticated: bool,
protocol_version: u16,
props: Option<protocol::Props>,
}
struct ServerState {
sinks: Vec<protocol::SinkInfo>,
next_playback_channel_index: u32,
}
fn main() -> anyhow::Result<()> {
let args: Vec<String> = std::env::args().collect();
if args.len() != 2 {
println!("Usage: {} <socket>", args[0]);
return Ok(());
}
// Create a new event loop that accepts connections on a unix socket.
const ACCEPT: Token = Token(0);
const CLOCK: Token = Token(1);
let mut poll = Poll::new().unwrap();
let mut listener = UnixListener::bind(&args[1])?;
poll.registry()
.register(&mut listener, ACCEPT, mio::Interest::READABLE)?;
// Client tokens start at 1024.
let mut next_client_token = 1024;
// This is a timer for reading audio data. The slower we run, the more data
// we would have to buffer (if we weren't throwing it away).
let mut clock = mio_timerfd::TimerFd::new(mio_timerfd::ClockId::Monotonic)?;
clock.set_timeout_interval(&time::Duration::from_nanos(1_000_000_000 / CLOCK_SPEED_HZ))?;
poll.registry()
.register(&mut clock, CLOCK, mio::Interest::READABLE)?;
// Keep track of our client connections.
let mut clients: HashMap<Token, Client> = HashMap::new();
// Global server state.
let mut server = ServerState {
sinks: vec![protocol::SinkInfo::new_dummy(1)],
next_playback_channel_index: 0,
};
// A reusable buffer for reading incoming messages.
let mut scratch = Vec::new();
let mut events = Events::with_capacity(256);
loop {
poll.poll(&mut events, None).unwrap();
for event in &events {
match event.token() {
ACCEPT => {
// Accept a new connection, set a token for it, and store
// the socket in the state.
let (mut socket, _) = listener.accept()?;
let index = next_client_token as u32;
let token = Token(next_client_token);
next_client_token += 1;
poll.registry()
.register(&mut socket, token, mio::Interest::READABLE)?;
eprintln!("new client connected!");
clients.insert(
token,
Client {
id: index,
socket,
playback_streams: HashMap::new(),
authenticated: false,
protocol_version: 0,
props: None,
},
);
}
CLOCK => {
clock.read()?;
for client in clients.values_mut() {
let mut done_draining = Vec::new();
for (id, stream) in client.playback_streams.iter_mut() {
// This removes samples to play (drop) them.
play_samples(stream)?;
// If we've drained the buffer, we can drop the stream.
if matches!(stream.state, StreamState::Draining(_))
&& stream.buffer.is_empty()
{
done_draining.push(*id)
}
let bytes_needed = (stream.buffer_attr.target_length as usize)
.saturating_sub(stream.buffer.len() + stream.requested_bytes);
if stream.state == StreamState::Playing
&& bytes_needed > stream.buffer_attr.minimum_request_length as usize
{
// We should request more bytes to fill the buffer.
eprintln!("channel {}: requesting {} bytes", id, bytes_needed);
stream.requested_bytes += bytes_needed;
protocol::write_command_message(
&mut client.socket,
u32::MAX,
protocol::Command::Request(protocol::Request {
channel: *id,
length: bytes_needed as u32,
}),
client.protocol_version,
)?;
}
}
for id in done_draining {
let stream = client.playback_streams.remove(&id).unwrap();
eprintln!("channel {} finished playback!", id);
if let StreamState::Draining(drain_seq) = stream.state {
protocol::write_ack_message(&mut client.socket, drain_seq)?;
} else {
unreachable!()
}
}
}
}
client_token if event.is_read_closed() => {
// The client disconnected.
if let Some(mut client) = clients.remove(&client_token) {
eprintln!("client disconnected!");
poll.registry().deregister(&mut client.socket)?;
}
}
client_token if event.is_readable() => {
let client = match clients.get_mut(&client_token) {
Some(v) => v,
None => continue,
};
'read: loop {
// Read the message header.
let desc = match protocol::read_descriptor(&mut client.socket) {
Ok(v) => v,
Err(protocol::ProtocolError::Io(e))
if e.kind() == std::io::ErrorKind::WouldBlock =>
{
break 'read;
}
Err(e) => bail!("reading descriptor: {:?}", e),
};
// Read the payload.
scratch.resize(desc.length as usize, 0);
let payload = &mut scratch[..desc.length as usize];
client.socket.read_exact(payload)?;
// A channel of -1 means a command message. Everything
// else is stream data.
if desc.channel == u32::MAX {
let (seq, cmd) = match protocol::Command::read_tag_prefixed(
&mut Cursor::new(payload),
client.protocol_version,
) {
Ok(v) => v,
Err(e) => {
eprintln!("error reading command: {:?}", e);
write_error(
&mut client.socket,
u32::MAX,
protocol::PulseError::Protocol,
)?;
break 'read;
}
};
handle_command(&mut server, client, seq, cmd)?;
} else {
handle_stream_write(client, desc, payload)?;
}
}
}
_ => (),
}
}
}
}
fn handle_command(
server: &mut ServerState,
client: &mut Client,
seq: u32,
cmd: protocol::Command,
) -> anyhow::Result<()> {
eprintln!("got command {:?}", cmd.tag());
match cmd {
// The client has to auth first.
_ if !client.authenticated => {
if let protocol::Command::Auth(params) = cmd {
// Here is where we would check the client's
// auth cookie. We won't do that in this
// example, though.
client.authenticated = true;
client.protocol_version = std::cmp::min(params.version, protocol::MAX_VERSION);
protocol::write_reply_message(
&mut client.socket,
seq,
&protocol::AuthReply {
version: client.protocol_version,
..Default::default()
},
client.protocol_version,
)?;
} else {
// The client needs to authenticate.
protocol::write_error(&mut client.socket, seq, protocol::PulseError::AccessDenied)?;
}
}
// Then it has to set props.
_ if client.props.is_none() => {
if let protocol::Command::SetClientName(props) = cmd {
client.props = Some(props);
protocol::write_reply_message(
&mut client.socket,
seq,
&protocol::SetClientNameReply {
client_id: client.id,
},
client.protocol_version,
)?;
} else {
// PulseAudio requires this as part of the
// handshake, so we will too.
protocol::write_error(&mut client.socket, seq, protocol::PulseError::Protocol)?;
}
}
// We won't implement all the introspection
// commands, but here's an example.
protocol::Command::GetSinkInfoList => {
protocol::write_reply_message(
&mut client.socket,
seq,
&server.sinks,
client.protocol_version,
)?;
}
// Here we'll handle playback streams.
protocol::Command::CreatePlaybackStream(mut params) => {
// Check if the client set any buffer attrs
// to -1, which indicates that we should
// set the value.
apply_buffer_defaults(&mut params.buffer_attr);
let stream = PlaybackStream {
state: StreamState::Prebuffering(params.buffer_attr.pre_buffering as u64),
sample_spec: params.sample_spec,
buffer_attr: params.buffer_attr,
buffer: VecDeque::new(),
requested_bytes: 0,
};
let channel = server.next_playback_channel_index;
server.next_playback_channel_index += 1;
client.playback_streams.insert(channel, stream);
// When writing the reply, we can
// immediately request bytes to be written.
let reply = protocol::CreatePlaybackStreamReply {
channel,
stream_index: 0,
sample_spec: params.sample_spec,
channel_map: params.channel_map,
buffer_attr: params.buffer_attr,
requested_bytes: params.buffer_attr.pre_buffering,
sink_name: Some(server.sinks[0].name.clone()),
..Default::default()
};
eprintln!("created playback stream {:#?}", reply);
protocol::write_reply_message(
&mut client.socket,
seq,
&reply,
client.protocol_version,
)?;
}
// The drain command means we should play the rest of the data we have.
protocol::Command::DrainPlaybackStream(channel) => {
if let Some(stream) = client.playback_streams.get_mut(&channel) {
stream.state = StreamState::Draining(seq);
}
}
_ => {
eprintln!("ignoring command {:?}", cmd.tag());
protocol::write_error(
&mut client.socket,
seq,
protocol::PulseError::NotImplemented,
)?;
}
}
Ok(())
}
fn handle_stream_write(
client: &mut Client,
desc: protocol::Descriptor,
payload: &[u8],
) -> anyhow::Result<()> {
eprintln!(
"got stream write to channel {} ({} bytes)",
desc.channel,
payload.len()
);
let stream = match client.playback_streams.get_mut(&desc.channel) {
Some(v) => v,
None => {
bail!("invalid channel")
}
};
// We don't handle seeks in this example.
assert_eq!(0, desc.offset);
// Check for overrun.
let remaining = (stream.buffer_attr.max_length as usize).saturating_sub(stream.buffer.len());
let overflow = payload.len().saturating_sub(remaining);
let payload = if overflow > 0 {
protocol::write_command_message(
&mut client.socket,
u32::MAX,
protocol::Command::Overflow(overflow as u32),
client.protocol_version,
)?;
&payload[..remaining as usize]
} else {
payload
};
if let StreamState::Prebuffering(n) = stream.state {
let needed = n.saturating_sub(payload.len() as u64);
if needed > 0 {
stream.state = StreamState::Prebuffering(needed)
} else {
protocol::write_command_message(
&mut client.socket,
u32::MAX,
protocol::Command::Started(desc.channel),
client.protocol_version,
)?;
stream.state = StreamState::Playing
}
}
// Read the data into the buffer.
stream.buffer.write_all(payload)?;
stream.requested_bytes = stream.requested_bytes.saturating_sub(payload.len());
Ok(())
}
fn play_samples(stream: &mut PlaybackStream) -> anyhow::Result<()> {
// We'll simulate playback by discarding samples from the front of the buffer.
if matches!(
stream.state,
StreamState::Playing | StreamState::Draining(_)
) {
let sample_size = stream.sample_spec.format.bytes_per_sample();
let bytes_per_tick = sample_size
* stream.sample_spec.channels as usize
* stream.sample_spec.sample_rate as usize
/ CLOCK_SPEED_HZ as usize;
stream
.buffer
.drain(..std::cmp::min(bytes_per_tick, stream.buffer.len()));
}
Ok(())
}
fn apply_buffer_defaults(attr: &mut protocol::stream::BufferAttr) {
if attr.max_length == u32::MAX {
attr.max_length = 4096 * 1024;
}
if attr.target_length == u32::MAX {
attr.target_length = 128 * 1024;
}
if attr.pre_buffering == u32::MAX {
attr.pre_buffering = attr.target_length;
}
if attr.minimum_request_length == u32::MAX {
attr.minimum_request_length = attr.target_length / 8;
}
}