use std::sync::Arc;
use crate::port::{DiscoveryCallback, VmbRuntime};
use crate::types::{DiscoveryCallbackId, DiscoveryEvent, DiscoveryRegistrationHandle};
use crate::Result;
pub struct DiscoveryRegistration<R: VmbRuntime> {
runtime: Arc<R>,
handle: Option<DiscoveryRegistrationHandle>,
callback_id: Option<DiscoveryCallbackId>,
}
pub fn register_camera_discovery<R, F>(
runtime: Arc<R>,
callback: F,
) -> Result<DiscoveryRegistration<R>>
where
R: VmbRuntime,
F: Fn(DiscoveryEvent) + Send + Sync + 'static,
{
let cb = Arc::new(DiscoveryCallback::new(callback));
let callback_id = runtime.install_discovery_callback(cb);
match runtime.register_discovery(callback_id) {
Ok(handle) => Ok(DiscoveryRegistration {
runtime,
handle: Some(handle),
callback_id: Some(callback_id),
}),
Err(e) => {
runtime.uninstall_discovery_callback(callback_id);
Err(e)
}
}
}
impl<R: VmbRuntime> Drop for DiscoveryRegistration<R> {
fn drop(&mut self) {
if let Some(h) = self.handle.take() {
self.runtime.unregister_discovery(h);
}
if let Some(id) = self.callback_id.take() {
self.runtime.uninstall_discovery_callback(id);
}
}
}
impl<R: VmbRuntime> std::fmt::Debug for DiscoveryRegistration<R> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DiscoveryRegistration")
.field("handle", &self.handle)
.field("callback_id", &self.callback_id)
.finish()
}
}