1+ use crate :: traits:: DeviceTrait ;
12use crate :: {
23 BackendSpecificError , BuildStreamError , Data , DefaultStreamConfigError , DeviceNameError ,
34 InputCallbackInfo , OutputCallbackInfo , SampleFormat , SampleRate , StreamConfig , StreamError ,
@@ -6,7 +7,6 @@ use crate::{
67} ;
78use std:: hash:: { Hash , Hasher } ;
89use std:: rc:: Rc ;
9- use crate :: traits:: DeviceTrait ;
1010
1111use super :: stream:: Stream ;
1212use super :: PIPEWIRE_SAMPLE_FORMAT ;
@@ -26,12 +26,10 @@ pub enum DeviceType {
2626}
2727#[ derive( Clone ) ]
2828pub 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 > ,
3533}
3634
3735impl Device {
@@ -41,22 +39,29 @@ impl Device {
4139 device_type : DeviceType ,
4240 client : Rc < super :: conn:: PWClient > ,
4341 ) -> 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+ { }
4553
4654 let settings = client. get_settings ( ) . unwrap ( ) ;
4755
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" ) ;
4959
5060 Ok ( Device {
5161 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- } ,
5762 device_type,
5863 connect_ports_automatically,
59- client
64+ client,
6065 } )
6166 }
6267
@@ -89,9 +94,14 @@ impl Device {
8994 }
9095
9196 pub fn default_config ( & self ) -> Result < SupportedStreamConfig , DefaultStreamConfigError > {
97+ let settings = self . client . get_settings ( ) . unwrap ( ) ;
9298 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+ } ;
95105 // The sample format for JACK audio ports is always "32-bit float mono audio" in the current implementation.
96106 // Custom formats are allowed within JACK, but this is of niche interest.
97107 // The format can be found programmatically by calling jack::PortSpec::port_type() on a created port.
@@ -105,6 +115,7 @@ impl Device {
105115 }
106116
107117 pub fn supported_configs ( & self ) -> Vec < SupportedStreamConfigRange > {
118+ let settings = self . client . get_settings ( ) . unwrap ( ) ;
108119 let f = match self . default_config ( ) {
109120 Err ( _) => return vec ! [ ] ,
110121 Ok ( f) => f,
@@ -115,7 +126,8 @@ impl Device {
115126 for & channels in DEFAULT_SUPPORTED_CHANNELS . iter ( ) {
116127 supported_configs. push ( SupportedStreamConfigRange {
117128 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
119131 max_sample_rate : f. sample_rate ,
120132 buffer_size : f. buffer_size . clone ( ) ,
121133 sample_format : f. sample_format ,
@@ -179,15 +191,25 @@ impl DeviceTrait for Device {
179191 D : FnMut ( & Data , & InputCallbackInfo ) + Send + ' static ,
180192 E : FnMut ( StreamError ) + Send + ' static ,
181193 {
194+ let settings = self . client . get_settings ( ) . unwrap ( ) ;
182195 if let DeviceType :: OutputDevice = & self . device_type {
183196 // Trying to create an input stream from an output device
184197 return Err ( BuildStreamError :: StreamConfigNotSupported ) ;
185198 }
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+ {
187204 return Err ( BuildStreamError :: StreamConfigNotSupported ) ;
188205 }
189206
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+ ) ;
191213
192214 if self . connect_ports_automatically {
193215 stream. connect_to_system_inputs ( ) ;
@@ -207,15 +229,23 @@ impl DeviceTrait for Device {
207229 D : FnMut ( & mut Data , & OutputCallbackInfo ) + Send + ' static ,
208230 E : FnMut ( StreamError ) + Send + ' static ,
209231 {
232+ let settings = self . client . get_settings ( ) . unwrap ( ) ;
210233 if let DeviceType :: InputDevice = & self . device_type {
211234 // Trying to create an output stream from an input device
212235 return Err ( BuildStreamError :: StreamConfigNotSupported ) ;
213236 }
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+ {
215240 return Err ( BuildStreamError :: StreamConfigNotSupported ) ;
216241 }
217242
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+ ) ;
219249
220250 if self . connect_ports_automatically {
221251 stream. connect_to_system_outputs ( ) ;
0 commit comments