use std::sync::{Arc, OnceLock};
use crate::error::PlatformError;
use crate::traits::video_player::{
VideoPlayerCommand, VideoPlayerHandle, VideoPlayerHandleImpl, VideoPlayerManager,
};
use super::app::Platform;
pub type WindowsVideoCommandDispatcher =
Arc<dyn Fn(&str, &VideoPlayerCommand) -> Result<(), String> + Send + Sync>;
static DISPATCHER: OnceLock<WindowsVideoCommandDispatcher> = OnceLock::new();
pub fn register_windows_video_command_dispatcher(dispatcher: WindowsVideoCommandDispatcher) {
if DISPATCHER.set(dispatcher).is_err() {
log::warn!("a Windows video command dispatcher is already registered; ignoring");
}
}
impl VideoPlayerManager for Platform {
fn bind_player(&self, component_id: &str) -> Result<Box<dyn VideoPlayerHandle>, PlatformError> {
let dispatcher = DISPATCHER
.get()
.ok_or_else(|| {
PlatformError::NotSupported(
"no native video host is registered on Windows".to_string(),
)
})?
.clone();
let component_id = component_id.to_string();
Ok(Box::new(VideoPlayerHandleImpl::new(move |command| {
dispatcher(&component_id, &command).map_err(PlatformError::Platform)
})))
}
}