1
+ use crate :: traits:: DeviceTrait ;
1
2
use crate :: {
2
3
BackendSpecificError , BuildStreamError , Data , DefaultStreamConfigError , DeviceNameError ,
3
4
InputCallbackInfo , OutputCallbackInfo , SampleFormat , SampleRate , StreamConfig , StreamError ,
@@ -6,7 +7,6 @@ use crate::{
6
7
} ;
7
8
use std:: hash:: { Hash , Hasher } ;
8
9
use std:: rc:: Rc ;
9
- use crate :: traits:: DeviceTrait ;
10
10
11
11
use super :: stream:: Stream ;
12
12
use super :: PIPEWIRE_SAMPLE_FORMAT ;
@@ -26,12 +26,10 @@ pub enum DeviceType {
26
26
}
27
27
#[ derive( Clone ) ]
28
28
pub struct Device {
29
- name : String ,
30
- sample_rate : SampleRate ,
31
- buffer_size : SupportedBufferSize ,
32
- device_type : DeviceType ,
33
- connect_ports_automatically : bool ,
34
- client : Rc < super :: conn:: PWClient >
29
+ pub ( crate ) name : String ,
30
+ pub ( crate ) device_type : DeviceType ,
31
+ pub ( crate ) connect_ports_automatically : bool ,
32
+ pub ( crate ) client : Rc < super :: conn:: PWClient > ,
35
33
}
36
34
37
35
impl Device {
@@ -41,22 +39,29 @@ impl Device {
41
39
device_type : DeviceType ,
42
40
client : Rc < super :: conn:: PWClient > ,
43
41
) -> Result < Self , String > {
44
- while client. get_settings ( ) . and_then ( |s| if s. sample_rate == 0 { Err ( String :: new ( ) ) } else { Ok ( true ) } ) . is_err ( ) { }
42
+ while client
43
+ . get_settings ( )
44
+ . and_then ( |s| {
45
+ if s. allowed_sample_rates . is_empty ( ) {
46
+ Err ( String :: new ( ) )
47
+ } else {
48
+ Ok ( true )
49
+ }
50
+ } )
51
+ . is_err ( )
52
+ { }
45
53
46
54
let settings = client. get_settings ( ) . unwrap ( ) ;
47
55
48
- let info = client. create_device_node ( name, device_type. clone ( ) , connect_ports_automatically) . expect ( "Error creating device" ) ;
56
+ let info = client
57
+ . create_device_node ( name, device_type. clone ( ) , connect_ports_automatically)
58
+ . expect ( "Error creating device" ) ;
49
59
50
60
Ok ( Device {
51
61
name : info. name ,
52
- sample_rate : SampleRate ( settings. sample_rate ) ,
53
- buffer_size : SupportedBufferSize :: Range {
54
- min : settings. min_buffer_size ,
55
- max : settings. max_buffer_size ,
56
- } ,
57
62
device_type,
58
63
connect_ports_automatically,
59
- client
64
+ client,
60
65
} )
61
66
}
62
67
@@ -89,9 +94,14 @@ impl Device {
89
94
}
90
95
91
96
pub fn default_config ( & self ) -> Result < SupportedStreamConfig , DefaultStreamConfigError > {
97
+ let settings = self . client . get_settings ( ) . unwrap ( ) ;
92
98
let channels = DEFAULT_NUM_CHANNELS ;
93
- let sample_rate = self . sample_rate ;
94
- let buffer_size = self . buffer_size . clone ( ) ;
99
+ // Default is highest sample rate possible
100
+ let sample_rate = SampleRate ( * settings. allowed_sample_rates . last ( ) . unwrap ( ) ) ;
101
+ let buffer_size = SupportedBufferSize :: Range {
102
+ min : settings. min_buffer_size ,
103
+ max : settings. max_buffer_size ,
104
+ } ;
95
105
// The sample format for JACK audio ports is always "32-bit float mono audio" in the current implementation.
96
106
// Custom formats are allowed within JACK, but this is of niche interest.
97
107
// The format can be found programmatically by calling jack::PortSpec::port_type() on a created port.
@@ -105,6 +115,7 @@ impl Device {
105
115
}
106
116
107
117
pub fn supported_configs ( & self ) -> Vec < SupportedStreamConfigRange > {
118
+ let settings = self . client . get_settings ( ) . unwrap ( ) ;
108
119
let f = match self . default_config ( ) {
109
120
Err ( _) => return vec ! [ ] ,
110
121
Ok ( f) => f,
@@ -115,7 +126,8 @@ impl Device {
115
126
for & channels in DEFAULT_SUPPORTED_CHANNELS . iter ( ) {
116
127
supported_configs. push ( SupportedStreamConfigRange {
117
128
channels,
118
- min_sample_rate : f. sample_rate ,
129
+ min_sample_rate : SampleRate ( * settings. allowed_sample_rates . first ( ) . unwrap ( ) ) ,
130
+ // Default is maximum possible, so just use that
119
131
max_sample_rate : f. sample_rate ,
120
132
buffer_size : f. buffer_size . clone ( ) ,
121
133
sample_format : f. sample_format ,
@@ -179,15 +191,25 @@ impl DeviceTrait for Device {
179
191
D : FnMut ( & Data , & InputCallbackInfo ) + Send + ' static ,
180
192
E : FnMut ( StreamError ) + Send + ' static ,
181
193
{
194
+ let settings = self . client . get_settings ( ) . unwrap ( ) ;
182
195
if let DeviceType :: OutputDevice = & self . device_type {
183
196
// Trying to create an input stream from an output device
184
197
return Err ( BuildStreamError :: StreamConfigNotSupported ) ;
185
198
}
186
- if conf. sample_rate != self . sample_rate || sample_format != PIPEWIRE_SAMPLE_FORMAT {
199
+ // FIXME: Not sure if we should go to the nearest neighbour sample rate
200
+ // This issue also happens on build_output_stream_raw()
201
+ if settings. allowed_sample_rates . contains ( & conf. sample_rate . 0 )
202
+ || sample_format != PIPEWIRE_SAMPLE_FORMAT
203
+ {
187
204
return Err ( BuildStreamError :: StreamConfigNotSupported ) ;
188
205
}
189
206
190
- let mut stream = Stream :: new_input ( self . client . clone ( ) , conf. channels , data_callback, error_callback) ;
207
+ let mut stream = Stream :: new_input (
208
+ self . client . clone ( ) ,
209
+ conf. channels ,
210
+ data_callback,
211
+ error_callback,
212
+ ) ;
191
213
192
214
if self . connect_ports_automatically {
193
215
stream. connect_to_system_inputs ( ) ;
@@ -207,15 +229,23 @@ impl DeviceTrait for Device {
207
229
D : FnMut ( & mut Data , & OutputCallbackInfo ) + Send + ' static ,
208
230
E : FnMut ( StreamError ) + Send + ' static ,
209
231
{
232
+ let settings = self . client . get_settings ( ) . unwrap ( ) ;
210
233
if let DeviceType :: InputDevice = & self . device_type {
211
234
// Trying to create an output stream from an input device
212
235
return Err ( BuildStreamError :: StreamConfigNotSupported ) ;
213
236
}
214
- if conf. sample_rate != self . sample_rate || sample_format != PIPEWIRE_SAMPLE_FORMAT {
237
+ if settings. allowed_sample_rates . contains ( & conf. sample_rate . 0 )
238
+ || sample_format != PIPEWIRE_SAMPLE_FORMAT
239
+ {
215
240
return Err ( BuildStreamError :: StreamConfigNotSupported ) ;
216
241
}
217
242
218
- let mut stream = Stream :: new_output ( self . client . clone ( ) , conf. channels , data_callback, error_callback) ;
243
+ let mut stream = Stream :: new_output (
244
+ self . client . clone ( ) ,
245
+ conf. channels ,
246
+ data_callback,
247
+ error_callback,
248
+ ) ;
219
249
220
250
if self . connect_ports_automatically {
221
251
stream. connect_to_system_outputs ( ) ;
0 commit comments