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
use pulse::{
    channelmap,
    context::introspect,
    def,
    def::PortAvailable,
    format,
    proplist::Proplist,
    sample,
    time::MicroSeconds,
    volume::{ChannelVolumes, Volume},
};

/// These structs are direct representations of what libpulse_binding gives
/// created to be copyable / cloneable for use in and out of callbacks

/// This is a wrapper around SinkPortInfo and SourcePortInfo as they have the same members
#[derive(Debug, Clone)]
pub struct DevicePortInfo {
    /// Name of the sink.
    pub name: Option<String>,
    /// Description of this sink.
    pub description: Option<String>,
    /// The higher this value is, the more useful this port is as a default.
    pub priority: u32,
    /// A flag indicating availability status of this port.
    pub available: PortAvailable,
}

impl<'a> From<&'a Box<introspect::SinkPortInfo<'a>>> for DevicePortInfo {
    fn from(item: &'a Box<introspect::SinkPortInfo<'a>>) -> Self {
        DevicePortInfo {
            name: item.name.as_ref().map(|cow| cow.to_string()),
            description: item.description.as_ref().map(|cow| cow.to_string()),
            priority: item.priority,
            available: item.available,
        }
    }
}

impl<'a> From<&'a introspect::SinkPortInfo<'a>> for DevicePortInfo {
    fn from(item: &'a introspect::SinkPortInfo<'a>) -> Self {
        DevicePortInfo {
            name: item.name.as_ref().map(|cow| cow.to_string()),
            description: item.description.as_ref().map(|cow| cow.to_string()),
            priority: item.priority,
            available: item.available,
        }
    }
}

impl<'a> From<&'a Box<introspect::SourcePortInfo<'a>>> for DevicePortInfo {
    fn from(item: &'a Box<introspect::SourcePortInfo<'a>>) -> Self {
        DevicePortInfo {
            name: item.name.as_ref().map(|cow| cow.to_string()),
            description: item.description.as_ref().map(|cow| cow.to_string()),
            priority: item.priority,
            available: item.available,
        }
    }
}

impl<'a> From<&'a introspect::SourcePortInfo<'a>> for DevicePortInfo {
    fn from(item: &'a introspect::SourcePortInfo<'a>) -> Self {
        DevicePortInfo {
            name: item.name.as_ref().map(|cow| cow.to_string()),
            description: item.description.as_ref().map(|cow| cow.to_string()),
            priority: item.priority,
            available: item.available,
        }
    }
}

/// This is a wrapper around SinkState and SourceState as they have the same values
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum DevState {
    /// This state is used when the server does not support sink state introspection.
    Invalid = -1,
    /// Running, sink is playing and used by at least one non-corked sink-input.
    Running = 0,
    /// When idle, the sink is playing but there is no non-corked sink-input attached to it.
    Idle = 1,
    /// When suspended, actual sink access can be closed, for instance.
    Suspended = 2,
}

impl<'a> From<def::SourceState> for DevState {
    fn from(s: def::SourceState) -> Self {
        match s {
            def::SourceState::Idle => DevState::Idle,
            def::SourceState::Invalid => DevState::Invalid,
            def::SourceState::Running => DevState::Running,
            def::SourceState::Suspended => DevState::Suspended,
        }
    }
}

impl<'a> From<def::SinkState> for DevState {
    fn from(s: def::SinkState) -> Self {
        match s {
            def::SinkState::Idle => DevState::Idle,
            def::SinkState::Invalid => DevState::Invalid,
            def::SinkState::Running => DevState::Running,
            def::SinkState::Suspended => DevState::Suspended,
        }
    }
}

#[derive(Debug, Clone)]
pub enum Flags {
    SourceFLags(def::SourceFlagSet),
    SinkFlags(def::SinkFlagSet),
}

#[derive(Debug, Clone)]
pub struct DeviceInfo {
    /// Index of the sink.
    pub index: u32,
    /// Name of the sink.
    pub name: Option<String>,
    /// Description of this sink.
    pub description: Option<String>,
    /// Sample spec of this sink.
    pub sample_spec: sample::Spec,
    /// Channel map.
    pub channel_map: channelmap::Map,
    /// Index of the owning module of this sink, or `None` if is invalid.
    pub owner_module: Option<u32>,
    /// Volume of the sink.
    pub volume: ChannelVolumes,
    /// Mute switch of the sink.
    pub mute: bool,
    /// Index of the monitor source connected to this sink.
    pub monitor: Option<u32>,
    /// The name of the monitor source.
    pub monitor_name: Option<String>,
    /// Length of queued audio in the output buffer.
    pub latency: MicroSeconds,
    /// Driver name.
    pub driver: Option<String>,
    /// Flags.
    pub flags: Flags,
    /// Property list.
    pub proplist: Proplist,
    /// The latency this device has been configured to.
    pub configured_latency: MicroSeconds,
    /// Some kind of “base” volume that refers to unamplified/unattenuated volume in the context of
    /// the output device.
    pub base_volume: Volume,
    /// State.
    pub state: DevState,
    /// Number of volume steps for sinks which do not support arbitrary volumes.
    pub n_volume_steps: u32,
    /// Card index, or `None` if invalid.
    pub card: Option<u32>,
    /// Set of available ports.
    pub ports: Vec<DevicePortInfo>,
    // Pointer to active port in the set, or None.
    pub active_port: Option<DevicePortInfo>,
    /// Set of formats supported by the sink.
    pub formats: Vec<format::Info>,
}

impl<'a> From<&'a introspect::SinkInfo<'a>> for DeviceInfo {
    fn from(item: &'a introspect::SinkInfo<'a>) -> Self {
        DeviceInfo {
            name: item.name.as_ref().map(|cow| cow.to_string()),
            index: item.index,
            description: item.description.as_ref().map(|cow| cow.to_string()),
            sample_spec: item.sample_spec,
            channel_map: item.channel_map,
            owner_module: item.owner_module,
            volume: item.volume,
            mute: item.mute,
            monitor: Some(item.monitor_source),
            monitor_name: item.monitor_source_name.as_ref().map(|cow| cow.to_string()),
            latency: item.latency,
            driver: item.driver.as_ref().map(|cow| cow.to_string()),
            flags: Flags::SinkFlags(item.flags),
            proplist: item.proplist.clone(),
            configured_latency: item.configured_latency,
            base_volume: item.base_volume,
            state: DevState::from(item.state),
            n_volume_steps: item.n_volume_steps,
            card: item.card,
            ports: item.ports.iter().map(From::from).collect(),
            active_port: item.active_port.as_ref().map(From::from),
            formats: item.formats.clone(),
        }
    }
}

impl<'a> From<&'a introspect::SourceInfo<'a>> for DeviceInfo {
    fn from(item: &'a introspect::SourceInfo<'a>) -> Self {
        DeviceInfo {
            name: item.name.as_ref().map(|cow| cow.to_string()),
            index: item.index,
            description: item.description.as_ref().map(|cow| cow.to_string()),
            sample_spec: item.sample_spec,
            channel_map: item.channel_map,
            owner_module: item.owner_module,
            volume: item.volume,
            mute: item.mute,
            monitor: item.monitor_of_sink,
            monitor_name: item
                .monitor_of_sink_name
                .as_ref()
                .map(|cow| cow.to_string()),
            latency: item.latency,
            driver: item.driver.as_ref().map(|cow| cow.to_string()),
            flags: Flags::SourceFLags(item.flags),
            proplist: item.proplist.clone(),
            configured_latency: item.configured_latency,
            base_volume: item.base_volume,
            state: DevState::from(item.state),
            n_volume_steps: item.n_volume_steps,
            card: item.card,
            ports: item.ports.iter().map(From::from).collect(),
            active_port: item.active_port.as_ref().map(From::from),
            formats: item.formats.clone(),
        }
    }
}

#[derive(Debug, Clone)]
pub struct ApplicationInfo {
    /// Index of the sink input.
    pub index: u32,
    /// Name of the sink input.
    pub name: Option<String>,
    /// Index of the module this sink input belongs to, or `None` when it does not belong to any
    /// module.
    pub owner_module: Option<u32>,
    /// Index of the client this sink input belongs to, or invalid when it does not belong to any
    /// client.
    pub client: Option<u32>,
    /// Index of the connected sink/source.
    pub connection_id: u32,
    /// The sample specification of the sink input.
    pub sample_spec: sample::Spec,
    /// Channel map.
    pub channel_map: channelmap::Map,
    /// The volume of this sink input.
    pub volume: ChannelVolumes,
    /// Latency due to buffering in sink input, see
    /// [`def::TimingInfo`](../../def/struct.TimingInfo.html) for details.
    pub buffer_usec: MicroSeconds,
    /// Latency of the sink device, see
    /// [`def::TimingInfo`](../../def/struct.TimingInfo.html) for details.
    pub connection_usec: MicroSeconds,
    /// The resampling method used by this sink input.
    pub resample_method: Option<String>,
    /// Driver name.
    pub driver: Option<String>,
    /// Stream muted.
    pub mute: bool,
    /// Property list.
    pub proplist: Proplist,
    /// Stream corked.
    pub corked: bool,
    /// Stream has volume. If not set, then the meaning of this struct’s volume member is unspecified.
    pub has_volume: bool,
    /// The volume can be set. If not set, the volume can still change even though clients can’t
    /// control the volume.
    pub volume_writable: bool,
    /// Stream format information.
    pub format: format::Info,
}

impl<'a> From<&'a introspect::SinkInputInfo<'a>> for ApplicationInfo {
    fn from(item: &'a introspect::SinkInputInfo<'a>) -> Self {
        ApplicationInfo {
            index: item.index,
            name: item.name.as_ref().map(|cow| cow.to_string()),
            owner_module: item.owner_module,
            client: item.client,
            connection_id: item.sink,
            sample_spec: item.sample_spec,
            channel_map: item.channel_map,
            volume: item.volume,
            buffer_usec: item.buffer_usec,
            connection_usec: item.sink_usec,
            resample_method: item.resample_method.as_ref().map(|cow| cow.to_string()),
            driver: item.driver.as_ref().map(|cow| cow.to_string()),
            mute: item.mute,
            proplist: item.proplist.clone(),
            corked: item.corked,
            has_volume: item.has_volume,
            volume_writable: item.volume_writable,
            format: item.format.clone(),
        }
    }
}

impl<'a> From<&'a introspect::SourceOutputInfo<'a>> for ApplicationInfo {
    fn from(item: &'a introspect::SourceOutputInfo<'a>) -> Self {
        ApplicationInfo {
            index: item.index,
            name: item.name.as_ref().map(|cow| cow.to_string()),
            owner_module: item.owner_module,
            client: item.client,
            connection_id: item.source,
            sample_spec: item.sample_spec,
            channel_map: item.channel_map,
            volume: item.volume,
            buffer_usec: item.buffer_usec,
            connection_usec: item.source_usec,
            resample_method: item.resample_method.as_ref().map(|cow| cow.to_string()),
            driver: item.driver.as_ref().map(|cow| cow.to_string()),
            mute: item.mute,
            proplist: item.proplist.clone(),
            corked: item.corked,
            has_volume: item.has_volume,
            volume_writable: item.volume_writable,
            format: item.format.clone(),
        }
    }
}

#[derive(Debug)]
pub struct ServerInfo {
    /// User name of the daemon process.
    pub user_name: Option<String>,
    /// Host name the daemon is running on.
    pub host_name: Option<String>,
    /// Version string of the daemon.
    pub server_version: Option<String>,
    /// Server package name (usually “pulseaudio”).
    pub server_name: Option<String>,
    /// Default sample specification.
    pub sample_spec: sample::Spec,
    /// Name of default sink.
    pub default_sink_name: Option<String>,
    /// Name of default source.
    pub default_source_name: Option<String>,
    /// A random cookie for identifying this instance of PulseAudio.
    pub cookie: u32,
    /// Default channel map.
    pub channel_map: channelmap::Map,
}

impl<'a> From<&'a introspect::ServerInfo<'a>> for ServerInfo {
    fn from(info: &'a introspect::ServerInfo<'a>) -> Self {
        ServerInfo {
            user_name: info.user_name.as_ref().map(|cow| cow.to_string()),
            host_name: info.host_name.as_ref().map(|cow| cow.to_string()),
            server_version: info.server_version.as_ref().map(|cow| cow.to_string()),
            server_name: info.server_name.as_ref().map(|cow| cow.to_string()),
            sample_spec: info.sample_spec,
            default_sink_name: info.default_sink_name.as_ref().map(|cow| cow.to_string()),
            default_source_name: info.default_source_name.as_ref().map(|cow| cow.to_string()),
            cookie: info.cookie,
            channel_map: info.channel_map,
        }
    }
}