mirror of
https://github.com/arabianq/pipewire-soundpad.git
synced 2026-04-27 22:11:22 +00:00
refactor: move PipeWire initialization into a reusable helper function (#69)
Extract duplicated `pipewire::init()`, `MainLoopRc::new()`, and `ContextRc::new()` setup code from `pw_get_global_objects_thread`, `create_virtual_mic`, and `create_link` into a shared `setup_pipewire_context` helper in `src/utils/pipewire.rs`. Also ran codebase-wide linters to improve code quality. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
302f153b91
commit
7f8b7194b6
+4
-4
@@ -42,10 +42,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
lock_file.lock()?;
|
||||
|
||||
let socket_path = runtime_dir.join("daemon.sock");
|
||||
if let Err(e) = fs::remove_file(&socket_path) {
|
||||
if e.kind() != std::io::ErrorKind::NotFound {
|
||||
return Err(e.into());
|
||||
}
|
||||
if let Err(e) = fs::remove_file(&socket_path)
|
||||
&& e.kind() != std::io::ErrorKind::NotFound
|
||||
{
|
||||
return Err(e.into());
|
||||
}
|
||||
|
||||
let listener = UnixListener::bind(&socket_path)?;
|
||||
|
||||
+10
-12
@@ -9,6 +9,13 @@ use tokio::{
|
||||
time::{Duration, timeout},
|
||||
};
|
||||
|
||||
pub fn setup_pipewire_context() -> (MainLoopRc, ContextRc) {
|
||||
pipewire::init();
|
||||
let main_loop = MainLoopRc::new(None).expect("Failed to initialize pipewire main loop");
|
||||
let context = ContextRc::new(&main_loop, None).expect("Failed to create pipewire context");
|
||||
(main_loop, context)
|
||||
}
|
||||
|
||||
fn parse_global_object(
|
||||
global_object: &GlobalObject<&DictRef>,
|
||||
) -> (Option<AudioDevice>, Option<Port>) {
|
||||
@@ -79,9 +86,7 @@ async fn pw_get_global_objects_thread(
|
||||
main_sender: mpsc::Sender<(Option<AudioDevice>, Option<Port>)>,
|
||||
pw_receiver: pipewire::channel::Receiver<Terminate>,
|
||||
) {
|
||||
pipewire::init();
|
||||
|
||||
let main_loop = MainLoopRc::new(None).expect("Failed to initialize pipewire main loop");
|
||||
let (main_loop, context) = setup_pipewire_context();
|
||||
|
||||
// Stop main loop on Terminate message
|
||||
let _receiver = pw_receiver.attach(main_loop.loop_(), {
|
||||
@@ -89,7 +94,6 @@ async fn pw_get_global_objects_thread(
|
||||
move |_| _main_loop.quit()
|
||||
});
|
||||
|
||||
let context = ContextRc::new(&main_loop, None).expect("Failed to create pipewire context");
|
||||
let core = context
|
||||
.connect(None)
|
||||
.expect("Failed to connect to pipewire context");
|
||||
@@ -224,10 +228,7 @@ pub fn create_virtual_mic() -> Result<pipewire::channel::Sender<Terminate>, Box<
|
||||
let (pw_sender, pw_receiver) = pipewire::channel::channel::<Terminate>();
|
||||
|
||||
let _pw_thread = thread::spawn(move || {
|
||||
pipewire::init();
|
||||
|
||||
let main_loop = MainLoopRc::new(None).expect("Failed to initialize pipewire main loop");
|
||||
let context = ContextRc::new(&main_loop, None).expect("Failed to create pipewire context");
|
||||
let (main_loop, context) = setup_pipewire_context();
|
||||
let core = context
|
||||
.connect(None)
|
||||
.expect("Failed to connect to pipewire context");
|
||||
@@ -305,10 +306,7 @@ pub fn create_link(
|
||||
let (pw_sender, pw_receiver) = pipewire::channel::channel::<Terminate>();
|
||||
|
||||
let _pw_thread = thread::spawn(move || {
|
||||
pipewire::init();
|
||||
|
||||
let main_loop = MainLoopRc::new(None).expect("Failed to initialize pipewire main loop");
|
||||
let context = ContextRc::new(&main_loop, None).expect("Failed to create pipewire context");
|
||||
let (main_loop, context) = setup_pipewire_context();
|
||||
let core = context
|
||||
.connect(None)
|
||||
.expect("Failed to connect to pipewire context");
|
||||
|
||||
Reference in New Issue
Block a user