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
use crate::protocol::stream::BufferAttr;

use super::*;

/// Parameters for [`super::Command::SetPlaybackStreamName`] and [`super::Command::SetRecordStreamName`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SetStreamNameParams {
    /// The index of the stream to update.
    pub index: u32,

    /// The new name.
    pub name: CString,
}

impl TagStructRead for SetStreamNameParams {
    fn read(ts: &mut TagStructReader<'_>, _protocol_version: u16) -> Result<Self, ProtocolError> {
        let index = ts.read_u32()?;
        let name = ts.read_string_non_null()?;

        Ok(Self { index, name })
    }
}

impl TagStructWrite for SetStreamNameParams {
    fn write(
        &self,
        w: &mut TagStructWriter<'_>,
        _protocol_version: u16,
    ) -> Result<(), ProtocolError> {
        w.write_u32(self.index)?;
        w.write_string(Some(&self.name))?;

        Ok(())
    }
}

/// Parameters for [`super::Command::SetPlaybackStreamBufferAttr`].
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct SetPlaybackStreamBufferAttrParams {
    /// The index of the stream to update.
    pub index: u32,

    /// The new buffer attributes. `fragment_size` is ignored.
    pub buffer_attr: BufferAttr,

    /// Sets the stream flag for adjusting latency. See [`super::stream::StreamFlags`].
    pub adjust_latency: bool,

    /// Sets the stream flag for early requests. See [`super::stream::StreamFlags`].
    pub early_requests: bool,
}

impl TagStructRead for SetPlaybackStreamBufferAttrParams {
    fn read(ts: &mut TagStructReader<'_>, protocol_version: u16) -> Result<Self, ProtocolError> {
        Ok(Self {
            index: ts
                .read_index()?
                .ok_or(ProtocolError::Invalid("invalid index".into()))?,
            buffer_attr: BufferAttr {
                max_length: ts.read_u32()?,
                target_length: ts.read_u32()?,
                pre_buffering: ts.read_u32()?,
                minimum_request_length: ts.read_u32()?,
                fragment_size: 0,
            },
            adjust_latency: ts.read_bool()?,
            early_requests: if protocol_version >= 14 {
                ts.read_bool()?
            } else {
                false
            },
        })
    }
}

impl TagStructWrite for SetPlaybackStreamBufferAttrParams {
    fn write(
        &self,
        w: &mut TagStructWriter<'_>,
        protocol_version: u16,
    ) -> Result<(), ProtocolError> {
        w.write_index(Some(self.index))?;
        w.write_u32(self.buffer_attr.max_length)?;
        w.write_u32(self.buffer_attr.target_length)?;
        w.write_u32(self.buffer_attr.pre_buffering)?;
        w.write_u32(self.buffer_attr.minimum_request_length)?;
        w.write_bool(self.adjust_latency)?;
        if protocol_version >= 14 {
            w.write_bool(self.early_requests)?;
        }
        Ok(())
    }
}

/// The reply to [`super::Command::SetPlaybackStreamBufferAttr`].
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct SetPlaybackStreamBufferAttrReply {
    /// The negotiated buffer attributes. `fragment_size` is always 0.
    pub buffer_attr: BufferAttr,

    /// The configured sink latency, in microseconds.
    pub configured_sink_latency: u64,
}

impl CommandReply for SetPlaybackStreamBufferAttrReply {}

impl TagStructRead for SetPlaybackStreamBufferAttrReply {
    fn read(ts: &mut TagStructReader<'_>, _protocol_version: u16) -> Result<Self, ProtocolError> {
        Ok(Self {
            buffer_attr: BufferAttr {
                max_length: ts.read_u32()?,
                target_length: ts.read_u32()?,
                pre_buffering: ts.read_u32()?,
                minimum_request_length: ts.read_u32()?,
                fragment_size: 0,
            },
            configured_sink_latency: ts.read_usec()?,
        })
    }
}

impl TagStructWrite for SetPlaybackStreamBufferAttrReply {
    fn write(
        &self,
        w: &mut TagStructWriter<'_>,
        _protocol_version: u16,
    ) -> Result<(), ProtocolError> {
        w.write_u32(self.buffer_attr.max_length)?;
        w.write_u32(self.buffer_attr.target_length)?;
        w.write_u32(self.buffer_attr.pre_buffering)?;
        w.write_u32(self.buffer_attr.minimum_request_length)?;
        w.write_usec(self.configured_sink_latency)?;
        Ok(())
    }
}

/// Parameters for [`super::Command::SetRecordStreamBufferAttr`].
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct SetRecordStreamBufferAttrParams {
    /// The index of the stream to update.
    pub index: u32,

    /// The new buffer attributes. Only `max_length` and `fragment_size` are used.
    pub buffer_attr: BufferAttr,

    /// Sets the stream flag for adjusting latency. See [`super::stream::StreamFlags`].
    pub adjust_latency: bool,

    /// Sets the stream flag for early requests. See [`super::stream::StreamFlags`].
    pub early_requests: bool,
}

impl TagStructRead for SetRecordStreamBufferAttrParams {
    fn read(ts: &mut TagStructReader<'_>, protocol_version: u16) -> Result<Self, ProtocolError> {
        Ok(Self {
            index: ts
                .read_index()?
                .ok_or(ProtocolError::Invalid("invalid index".into()))?,
            buffer_attr: BufferAttr {
                max_length: ts.read_u32()?,
                target_length: 0,
                pre_buffering: 0,
                minimum_request_length: 0,
                fragment_size: ts.read_u32()?,
            },
            adjust_latency: ts.read_bool()?,
            early_requests: if protocol_version >= 14 {
                ts.read_bool()?
            } else {
                false
            },
        })
    }
}

impl TagStructWrite for SetRecordStreamBufferAttrParams {
    fn write(
        &self,
        w: &mut TagStructWriter<'_>,
        protocol_version: u16,
    ) -> Result<(), ProtocolError> {
        w.write_index(Some(self.index))?;
        w.write_u32(self.buffer_attr.max_length)?;
        w.write_u32(self.buffer_attr.fragment_size)?;
        w.write_bool(self.adjust_latency)?;
        if protocol_version >= 14 {
            w.write_bool(self.early_requests)?;
        }

        Ok(())
    }
}

/// The reply to [`super::Command::SetRecordStreamBufferAttr`].
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct SetRecordStreamBufferAttrReply {
    /// The negotiated buffer attributes. Only `max_length` and `fragment_size` are used.
    pub buffer_attr: BufferAttr,

    /// The configured source latency, in microseconds.
    pub configured_source_latency: u64,
}

impl CommandReply for SetRecordStreamBufferAttrReply {}

impl TagStructRead for SetRecordStreamBufferAttrReply {
    fn read(ts: &mut TagStructReader<'_>, _protocol_version: u16) -> Result<Self, ProtocolError> {
        Ok(Self {
            buffer_attr: BufferAttr {
                max_length: ts.read_u32()?,
                target_length: 0,
                pre_buffering: 0,
                minimum_request_length: 0,
                fragment_size: ts.read_u32()?,
            },
            configured_source_latency: ts.read_usec()?,
        })
    }
}

impl TagStructWrite for SetRecordStreamBufferAttrReply {
    fn write(
        &self,
        w: &mut TagStructWriter<'_>,
        _protocol_version: u16,
    ) -> Result<(), ProtocolError> {
        w.write_u32(self.buffer_attr.max_length)?;
        w.write_u32(self.buffer_attr.fragment_size)?;
        w.write_usec(self.configured_source_latency)?;
        Ok(())
    }
}

/// Parameters for [`super::Command::UpdatePlaybackStreamProplist`] and [`super::Command::UpdateRecordStreamProplist`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UpdatePropsParams {
    /// The index of the object to update.
    pub index: u32,

    /// The type of update being performed.
    pub mode: props::PropsUpdateMode,

    /// The new props.
    pub props: Props,
}

impl TagStructRead for UpdatePropsParams {
    fn read(ts: &mut TagStructReader<'_>, _protocol_version: u16) -> Result<Self, ProtocolError> {
        let index = ts.read_u32()?;
        let mode = ts.read_enum()?;
        let props = ts.read()?;

        Ok(Self { index, mode, props })
    }
}

impl TagStructWrite for UpdatePropsParams {
    fn write(
        &self,
        w: &mut TagStructWriter<'_>,
        _protocol_version: u16,
    ) -> Result<(), ProtocolError> {
        w.write_u32(self.index)?;
        w.write_u32(self.mode as u32)?;
        w.write(&self.props)?;

        Ok(())
    }
}

/// Parameters for [`super::Command::UpdatePlaybackStreamSampleRate`] and [`super::Command::UpdateRecordStreamSampleRate`].
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct UpdateSampleRateParams {
    /// The index of the stream to update.
    pub index: u32,

    /// The new sample rate.
    pub sample_rate: u32,
}

impl TagStructRead for UpdateSampleRateParams {
    fn read(ts: &mut TagStructReader<'_>, _protocol_version: u16) -> Result<Self, ProtocolError> {
        Ok(Self {
            index: ts
                .read_index()?
                .ok_or(ProtocolError::Invalid("invalid index".into()))?,
            sample_rate: ts.read_u32()?,
        })
    }
}

impl TagStructWrite for UpdateSampleRateParams {
    fn write(
        &self,
        w: &mut TagStructWriter<'_>,
        _protocol_version: u16,
    ) -> Result<(), ProtocolError> {
        w.write_index(Some(self.index))?;
        w.write_u32(self.sample_rate)?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::protocol::test_util::test_serde;

    #[test]
    fn test_set_stream_name_params_serde() -> anyhow::Result<()> {
        let params = SetStreamNameParams {
            index: 0,
            name: CString::new("name").unwrap(),
        };

        test_serde(&params)
    }

    #[test]
    fn test_update_props_params_serde() -> anyhow::Result<()> {
        let params = UpdatePropsParams {
            index: 0,
            mode: props::PropsUpdateMode::Set,
            props: Props::new(),
        };

        test_serde(&params)
    }

    #[test]
    fn test_update_sample_rate_params_serde() -> anyhow::Result<()> {
        let params = UpdateSampleRateParams {
            index: 0,
            sample_rate: 1234,
        };

        test_serde(&params)
    }
}

#[cfg(test)]
#[cfg(feature = "_integration-tests")]
mod integration_tests {
    use assert_matches::assert_matches;
    use std::ffi::CString;

    use anyhow::Ok;

    use crate::{integration_test_util::connect_and_init, protocol::*};

    #[test]
    fn test_set_stream_name() -> anyhow::Result<()> {
        let (mut sock, protocol_version) = connect_and_init()?;

        write_command_message(
            sock.get_mut(),
            0,
            &Command::SetPlaybackStreamName(SetStreamNameParams {
                index: 999,
                name: CString::new("stream").unwrap(),
            }),
            protocol_version,
        )?;

        let resp = read_ack_message(&mut sock);

        assert_matches!(resp, Err(ProtocolError::ServerError(PulseError::NoEntity)));

        Ok(())
    }

    #[test]
    fn test_update_stream_props() -> anyhow::Result<()> {
        let (mut sock, protocol_version) = connect_and_init()?;

        write_command_message(
            sock.get_mut(),
            0,
            &Command::UpdatePlaybackStreamProplist(UpdatePropsParams {
                index: 999,
                mode: props::PropsUpdateMode::Set,
                props: Props::new(),
            }),
            protocol_version,
        )?;

        let resp = read_ack_message(&mut sock);

        assert_matches!(resp, Err(ProtocolError::ServerError(PulseError::NoEntity)));

        Ok(())
    }

    #[test]
    fn test_update_stream_sample_rate() -> anyhow::Result<()> {
        let (mut sock, protocol_version) = connect_and_init()?;

        write_command_message(
            sock.get_mut(),
            0,
            &Command::UpdatePlaybackStreamSampleRate(UpdateSampleRateParams {
                index: 999,
                sample_rate: 44100,
            }),
            protocol_version,
        )?;

        let resp = read_ack_message(&mut sock);

        assert_matches!(resp, Err(ProtocolError::ServerError(PulseError::NoEntity)));

        Ok(())
    }
}