sim_lib_stream_host/
backend.rs1use std::{cell::Cell, rc::Rc, sync::Arc};
4
5use sim_kernel::Result;
6use sim_lib_stream_core::StreamValue;
7
8use crate::{
9 HostBackendInfo, HostCallbackQueue, HostDeviceInventory, HostStreamConfig,
10 HostStreamConfigRequest,
11};
12
13pub trait HostBackend: Send + Sync {
15 fn info(&self) -> &HostBackendInfo;
17
18 fn enumerate(&self) -> Result<HostDeviceInventory>;
20
21 fn open(&self, request: HostStreamConfigRequest) -> Result<HostOpenStream>;
29}
30
31pub trait HostStreamDriver {
40 fn shutdown(&self) -> Result<()>;
42}
43
44#[derive(Clone)]
46pub struct HostOpenStream {
47 config: HostStreamConfig,
48 queue: HostCallbackQueue,
49 driver: Option<Rc<HostDriverHandle>>,
50}
51
52struct HostDriverHandle {
53 driver: Rc<dyn HostStreamDriver>,
54 closed: Cell<bool>,
55}
56
57impl HostDriverHandle {
58 fn new(driver: Rc<dyn HostStreamDriver>) -> Self {
59 Self {
60 driver,
61 closed: Cell::new(false),
62 }
63 }
64
65 fn shutdown_once(&self) -> Result<()> {
66 if self.closed.replace(true) {
67 return Ok(());
68 }
69 self.driver.shutdown()
70 }
71}
72
73impl HostOpenStream {
74 pub fn new(config: HostStreamConfig) -> Self {
76 let stream = Arc::new(StreamValue::push(config.metadata()));
77 Self {
78 config,
79 queue: HostCallbackQueue::new(stream),
80 driver: None,
81 }
82 }
83
84 pub fn new_with_driver(config: HostStreamConfig, driver: Rc<dyn HostStreamDriver>) -> Self {
86 let stream = Arc::new(StreamValue::push(config.metadata()));
87 Self {
88 config,
89 queue: HostCallbackQueue::new(stream),
90 driver: Some(Rc::new(HostDriverHandle::new(driver))),
91 }
92 }
93
94 pub fn try_new_with_driver(
97 config: HostStreamConfig,
98 build_driver: impl FnOnce(HostCallbackQueue) -> Result<Rc<dyn HostStreamDriver>>,
99 ) -> Result<Self> {
100 let stream = Arc::new(StreamValue::push(config.metadata()));
101 let queue = HostCallbackQueue::new(stream);
102 let driver = build_driver(queue.clone())?;
103 Ok(Self {
104 config,
105 queue,
106 driver: Some(Rc::new(HostDriverHandle::new(driver))),
107 })
108 }
109
110 pub fn new_realtime_local_audio(config: HostStreamConfig) -> Result<Self> {
112 config.validate_realtime_local_audio()?;
113 Ok(Self::new(config))
114 }
115
116 pub fn new_realtime_local_audio_with_driver(
118 config: HostStreamConfig,
119 driver: Rc<dyn HostStreamDriver>,
120 ) -> Result<Self> {
121 config.validate_realtime_local_audio()?;
122 Ok(Self::new_with_driver(config, driver))
123 }
124
125 pub fn try_new_realtime_local_audio_with_driver(
128 config: HostStreamConfig,
129 build_driver: impl FnOnce(HostCallbackQueue) -> Result<Rc<dyn HostStreamDriver>>,
130 ) -> Result<Self> {
131 config.validate_realtime_local_audio()?;
132 Self::try_new_with_driver(config, build_driver)
133 }
134
135 pub fn new_lan_midi_control(config: HostStreamConfig) -> Result<Self> {
137 config.validate_lan_midi_control()?;
138 Ok(Self::new(config))
139 }
140
141 pub fn new_lan_buffered_audio_preview(config: HostStreamConfig) -> Result<Self> {
143 config.validate_lan_buffered_audio_preview()?;
144 Ok(Self::new(config))
145 }
146
147 pub fn config(&self) -> &HostStreamConfig {
149 &self.config
150 }
151
152 pub fn queue(&self) -> &HostCallbackQueue {
154 &self.queue
155 }
156
157 pub fn stream(&self) -> Arc<StreamValue> {
159 self.queue.stream()
160 }
161
162 pub fn close(&self) -> Result<()> {
164 self.queue.close()?;
165 self.shutdown_driver()
166 }
167
168 pub fn cancel(&self) -> Result<()> {
170 self.queue.cancel()?;
171 self.shutdown_driver()
172 }
173
174 fn shutdown_driver(&self) -> Result<()> {
175 if let Some(driver) = &self.driver {
176 driver.shutdown_once()?;
177 }
178 Ok(())
179 }
180}