pulseaudio 0.3.1

A native rust implementation of the PulseAudio protocol.
Documentation
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! Provides helpers for converting IPC types to and from raw byte streams.

pub mod channel_map;
pub mod format_info;
pub mod port_info;
pub mod props;
pub mod sample_spec;
pub mod stream;
pub mod volume;

pub use channel_map::{ChannelMap, ChannelPosition};
pub use format_info::*;
pub use props::{Prop, Props};
pub use sample_spec::{SampleFormat, SampleSpec};
pub use stream::CorkStreamParams;
pub use volume::{ChannelVolume, Volume};

use super::ProtocolError;

use byteorder::{NetworkEndian, ReadBytesExt, WriteBytesExt};
use enum_primitive_derive::Primitive;
use num_traits::FromPrimitive;
use std::ffi::{CStr, CString};
use std::io::prelude::*;
use std::time;

/// A tag preceding a value in a tagstruct.
#[allow(missing_docs)]
#[repr(u8)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Primitive)]
pub enum Tag {
    String = b't',
    StringNull = b'N',
    U32 = b'L',
    U8 = b'B',
    U64 = b'R',
    S64 = b'r',
    SampleSpec = b'a',
    Arbitrary = b'x',
    BooleanTrue = b'1',
    BooleanFalse = b'0',
    TimeVal = b'T',
    Usec = b'U',
    ChannelMap = b'm',
    CVolume = b'v',
    PropList = b'P',
    Volume = b'V',
    FormatInfo = b'f',
}

/// A streaming reader for tagstruct-encoded data.
pub struct TagStructReader<'a> {
    inner: &'a mut dyn BufRead,
    protocol_version: u16,
}

impl<'a> TagStructReader<'a> {
    /// Creates a tagstruct reader from a `BufRead` instance.
    pub fn new(inner: &'a mut dyn BufRead, protocol_version: u16) -> Self {
        TagStructReader {
            inner,
            protocol_version,
        }
    }

    /// Reads a tag from the stream.
    pub fn read_tag(&mut self) -> Result<Tag, ProtocolError> {
        let v = self.inner.read_u8()?;
        Tag::from_u8(v)
            .ok_or_else(|| ProtocolError::Invalid(format!("invalid tag 0x{v:02X} in tagstruct")))
    }

    /// Reads a tag from the stream, and returns an error if it's not equal
    /// to the given tag.
    fn expect_tag(&mut self, next: Tag) -> Result<(), ProtocolError> {
        let t = self.read_tag()?;
        if t == next {
            Ok(())
        } else {
            Err(ProtocolError::Invalid(format!(
                "expected {next:?}, got {t:?}",
            )))
        }
    }

    /// Reads a single byte.
    pub fn read_u8(&mut self) -> Result<u8, ProtocolError> {
        self.expect_tag(Tag::U8)?;
        Ok(self.inner.read_u8()?)
    }

    /// Reads an unsigned 32-bit integer.
    pub fn read_u32(&mut self) -> Result<u32, ProtocolError> {
        self.expect_tag(Tag::U32)?;
        Ok(self.inner.read_u32::<NetworkEndian>()?)
    }

    /// Reads an unsigned 64-bit integer.
    pub fn read_u64(&mut self) -> Result<u64, ProtocolError> {
        self.expect_tag(Tag::U64)?;
        Ok(self.inner.read_u64::<NetworkEndian>()?)
    }

    /// Reads a signed 64-bit integer.
    pub fn read_i64(&mut self) -> Result<i64, ProtocolError> {
        self.expect_tag(Tag::S64)?;
        Ok(self.inner.read_i64::<NetworkEndian>()?)
    }

    /// Reads a boolean value.
    pub fn read_bool(&mut self) -> Result<bool, ProtocolError> {
        let tag = self.read_tag()?;
        match tag {
            Tag::BooleanTrue => Ok(true),
            Tag::BooleanFalse => Ok(false),
            _ => Err(ProtocolError::Invalid(format!(
                "expected boolean, got {tag:?}"
            ))),
        }
    }

    /// Reads a "usec" value, which is a 64-bit unsigned integer representing
    /// a number of microseconds.
    pub fn read_usec(&mut self) -> Result<u64, ProtocolError> {
        self.expect_tag(Tag::Usec)?;
        Ok(self.inner.read_u64::<NetworkEndian>()?)
    }

    /// Reads a timestamp, or "timeval", with microsecond precision.
    pub fn read_timeval(&mut self) -> Result<time::SystemTime, ProtocolError> {
        self.expect_tag(Tag::TimeVal)?;

        let secs = self.inner.read_u32::<NetworkEndian>()?;
        let usecs = self.inner.read_u32::<NetworkEndian>()?;

        let duration = time::Duration::new(secs as u64, usecs * 1000);
        Ok(time::UNIX_EPOCH + duration)
    }

    /// Reads an "arbitrary", a byte blob. Allocates a vec.
    pub fn read_arbitrary(&mut self) -> Result<Vec<u8>, ProtocolError> {
        self.expect_tag(Tag::Arbitrary)?;

        let len = self.inner.read_u32::<NetworkEndian>()?;
        let mut buf = vec![0; len as usize];
        self.inner.read_exact(&mut buf)?;

        Ok(buf)
    }

    /// Reads an "arbitrary" without length prefix.
    pub fn read_arbitrary_unprefixed(&mut self, len: u32) -> Result<Vec<u8>, ProtocolError> {
        self.expect_tag(Tag::Arbitrary)?;

        let mut buf = vec![0; len as usize];
        self.inner.read_exact(&mut buf)?;
        Ok(buf)
    }

    /// Reads a null-terminated string (which may be a special null string tag).
    ///
    /// Note that strings in a tagstruct aren't necessarily encoded in UTF-8.
    pub fn read_string(&mut self) -> Result<Option<CString>, ProtocolError> {
        match self.read_tag()? {
            Tag::String => {
                let mut buf = Vec::new();
                self.inner.read_until(0x00, &mut buf)?;
                Ok(Some(CString::from_vec_with_nul(buf).map_err(|e| {
                    ProtocolError::Invalid(format!("invalid string in tagstruct: {e}"))
                })?))
            }
            Tag::StringNull => Ok(None),
            tag => Err(ProtocolError::Invalid(format!(
                "expected string or null string, got {tag:?}"
            ))),
        }
    }

    /// Reads a null-terminated string.
    pub fn read_string_non_null(&mut self) -> Result<CString, ProtocolError> {
        self.expect_tag(Tag::String)?;

        let mut buf = Vec::new();
        self.inner.read_until(0x00, &mut buf)?;
        CString::from_vec_with_nul(buf)
            .map_err(|e| ProtocolError::Invalid(format!("invalid string in tagstruct: {e}")))
    }

    /// Reads a u32, and checks it against PA_INVALID_INDEX (-1).
    pub fn read_index(&mut self) -> Result<Option<u32>, ProtocolError> {
        const INVALID_INDEX: u32 = u32::MAX;

        let v = self.read_u32()?;
        if v == INVALID_INDEX {
            Ok(None)
        } else {
            Ok(Some(v))
        }
    }

    /// Reads a u32 and checks that it is valid for the given enum.
    pub fn read_enum<T: FromPrimitive>(&mut self) -> Result<T, ProtocolError> {
        let v = self.read_u32()?;
        T::from_u32(v).ok_or_else(|| {
            ProtocolError::Invalid(format!(
                "invalid enum value {} for {}",
                v,
                std::any::type_name::<T>()
            ))
        })
    }

    /// Reads a value which implements [`TagStructRead`].
    pub fn read<T: TagStructRead>(&mut self) -> Result<T, ProtocolError> {
        T::read(self, self.protocol_version)
    }

    /// Returns whether there is any data left in the input stream.
    pub fn has_data_left(&mut self) -> Result<bool, ProtocolError> {
        Ok(self.inner.fill_buf().map(|b| !b.is_empty())?)
    }
}

impl std::fmt::Debug for TagStructReader<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TagStructReader")
            .field("protocol_version", &self.protocol_version)
            .finish()
    }
}

/// A streaming writer for tagstruct-encoded data.
pub struct TagStructWriter<'a> {
    inner: &'a mut dyn Write,
    protocol_version: u16,
}

impl<'a> TagStructWriter<'a> {
    /// Creates a tagstruct writer that writes to a reusable buffer.
    pub fn new(inner: &'a mut dyn Write, protocol_version: u16) -> Self {
        Self {
            inner,
            protocol_version,
        }
    }

    /// Writes a boolean value.
    pub fn write_bool(&mut self, b: bool) -> Result<(), ProtocolError> {
        self.inner.write_u8(if b {
            Tag::BooleanTrue as u8
        } else {
            Tag::BooleanFalse as u8
        })?;

        Ok(())
    }

    /// Writes a single byte.
    pub fn write_u8(&mut self, value: u8) -> Result<(), ProtocolError> {
        self.inner.write_u8(Tag::U8 as u8)?;
        self.inner.write_u8(value)?;
        Ok(())
    }

    /// Writes an unsigned 32-bit integer.
    pub fn write_u32(&mut self, value: u32) -> Result<(), ProtocolError> {
        self.inner.write_u8(Tag::U32 as u8)?;
        self.inner.write_u32::<NetworkEndian>(value)?;
        Ok(())
    }

    /// Writes an unsigned 64-bit integer.
    pub fn write_u64(&mut self, value: u64) -> Result<(), ProtocolError> {
        self.inner.write_u8(Tag::U64 as u8)?;
        self.inner.write_u64::<NetworkEndian>(value)?;
        Ok(())
    }

    /// Writes a signed 64-bit integer.
    pub fn write_i64(&mut self, value: i64) -> Result<(), ProtocolError> {
        self.inner.write_u8(Tag::S64 as u8)?;
        self.inner.write_i64::<NetworkEndian>(value)?;
        Ok(())
    }

    /// Writes an index as a u32, or PA_INVALID_INDEX (-1) in the case of None.
    pub fn write_index(&mut self, index: Option<u32>) -> Result<(), ProtocolError> {
        match index {
            Some(index) => self.write_u32(index),
            None => self.write_u32(u32::MAX),
        }
    }

    /// Writes a string, or a special "null string" tag.
    pub fn write_string<S: AsRef<CStr>>(&mut self, value: Option<S>) -> Result<(), ProtocolError> {
        match value {
            Some(value) => {
                self.inner.write_u8(Tag::String as u8)?;
                self.inner.write_all(value.as_ref().to_bytes_with_nul())?;
            }
            None => self.write_null_string()?,
        }

        Ok(())
    }

    /// Writes a special "null string" tag.
    pub fn write_null_string(&mut self) -> Result<(), ProtocolError> {
        self.inner.write_u8(Tag::StringNull as u8)?;
        Ok(())
    }

    /// Writes a "usec" value, which is a 64-bit unsigned integer representing
    /// a number of microseconds.
    pub fn write_usec(&mut self, n: u64) -> Result<(), ProtocolError> {
        self.inner.write_u8(Tag::Usec as u8)?;
        self.inner.write_u64::<NetworkEndian>(n)?;
        Ok(())
    }

    /// Writes a timestamp as a "timeval" with microsecond precision.
    pub fn write_timeval(&mut self, t: time::SystemTime) -> Result<(), ProtocolError> {
        let d = t.duration_since(time::UNIX_EPOCH).unwrap();
        let (secs, usecs) = (d.as_secs() as u32, d.subsec_micros());

        self.write_timeval_raw(secs, usecs)
    }

    /// Writes a "timeval" with microsecond precision.
    pub fn write_timeval_raw(&mut self, secs: u32, usecs: u32) -> Result<(), ProtocolError> {
        self.inner.write_u8(Tag::TimeVal as u8)?;
        self.inner.write_u32::<NetworkEndian>(secs)?;
        self.inner.write_u32::<NetworkEndian>(usecs)?;
        Ok(())
    }

    ///  Write an "arbitrary", a byte blob, with prefix length.
    pub fn write_arbitrary<T: AsRef<[u8]>>(&mut self, bytes: T) -> Result<(), ProtocolError> {
        let bytes = bytes.as_ref();

        assert!(bytes.len() < u32::MAX as usize);
        self.inner.write_u8(Tag::Arbitrary as u8)?;
        self.inner.write_u32::<NetworkEndian>(bytes.len() as u32)?;
        self.inner.write_all(bytes)?;
        Ok(())
    }

    /// Appends a single value to the tagstruct.
    ///
    /// To append multiple values at once, use the `Extend` implementation.
    pub fn write<T: TagStructWrite>(&mut self, value: T) -> Result<(), ProtocolError> {
        // this cannot fail when we're writing into a `Vec<u8>`
        value.write(self, self.protocol_version)
    }
}

impl std::fmt::Debug for TagStructWriter<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TagStructWriter")
            .field("protocol_version", &self.protocol_version)
            .finish()
    }
}

/// Implemented by types that can be serialized into a tagstruct stream.
pub trait TagStructWrite {
    /// Write `self` into a tagstruct.
    fn write(
        &self,
        w: &mut TagStructWriter<'_>,
        protocol_version: u16,
    ) -> Result<(), ProtocolError>;
}

/// Implemented by types that can be deserialized from a tagstruct stream.
pub trait TagStructRead: Sized {
    /// Read an instance of `Self` from a tagstruct.
    fn read(ts: &mut TagStructReader<'_>, protocol_version: u16) -> Result<Self, ProtocolError>;
}

impl<T: ?Sized> TagStructWrite for &T
where
    T: TagStructWrite,
{
    fn write(
        &self,
        w: &mut TagStructWriter<'_>,
        protocol_version: u16,
    ) -> Result<(), ProtocolError> {
        (*self).write(w, protocol_version)
    }
}

#[cfg(test)]
mod tests {
    use std::io::Cursor;

    use super::*;

    #[test]
    fn usec_serde() {
        let usec = 1234567890;
        let mut buf = Vec::new();
        {
            let mut w = TagStructWriter::new(&mut buf, 0);
            w.write_usec(usec).unwrap();
        }

        let mut cursor = Cursor::new(&buf);
        let mut r = TagStructReader::new(&mut cursor, 0);
        let usec2 = r.read_usec().unwrap();

        assert_eq!(usec, usec2);
    }

    #[test]
    fn timeval_serde() {
        // Has to be truncated to micros.
        let t1 = time::UNIX_EPOCH + time::Duration::new(1234567890, 123000);

        let mut buf = Vec::new();
        {
            let mut w = TagStructWriter::new(&mut buf, 0);
            w.write_timeval(t1).unwrap();
        }

        let mut cursor = Cursor::new(&buf);
        let mut r = TagStructReader::new(&mut cursor, 0);
        let t2 = r.read_timeval().unwrap();

        assert_eq!(t1, t2);
    }
}

/// Internal test utilities.
#[cfg(test)]
pub mod test_util {
    use super::*;
    use crate::protocol::{MAX_VERSION, MIN_VERSION};

    use anyhow::Context as _;
    use pretty_assertions::assert_eq;
    use std::io::Cursor;

    /// Tests that each version of the protocol can roundtrip the given tagstruct.
    pub fn test_serde<T>(v: &T) -> anyhow::Result<()>
    where
        T: TagStructRead + TagStructWrite + PartialEq + std::fmt::Debug,
        for<'a> &'a T: PartialEq,
    {
        for version in MIN_VERSION..MAX_VERSION {
            test_serde_version(v, version)
                .context(format!("roundtrip failed for protocol version {}", version))?;
        }

        Ok(())
    }

    /// Tests that a given version of the protocol can roundtrip the given tagstruct.
    pub fn test_serde_version<T>(v: &T, version: u16) -> anyhow::Result<()>
    where
        T: TagStructRead + TagStructWrite + std::fmt::Debug,
        for<'a> &'a T: PartialEq,
    {
        let mut buf = Vec::new();

        {
            let mut ts = TagStructWriter::new(&mut buf, version);
            ts.write(v)?;
        }

        let mut cursor = Cursor::new(buf);
        let mut ts = TagStructReader::new(&mut cursor, version);
        let v2 = T::read(&mut ts, version)?;

        assert_eq!(v, &v2, "roundtrip failed for protocol version {}", version);

        Ok(())
    }
}