use std::sync::{Arc, Mutex};
use std::thread;
use fnv::FnvHashMap;
use log::{trace, warn};
use webrender_api::ExternalImageId;
use webrender_traits::{WebrenderExternalImageRegistry, WebrenderImageHandlerType};
use crate::media_channel::{glplayer_channel, GLPlayerSender};
use crate::{GLPlayerMsg, GLPlayerMsgForward};
pub struct GLPlayerThread {
players: FnvHashMap<u64, GLPlayerSender<GLPlayerMsgForward>>,
external_images: Arc<Mutex<WebrenderExternalImageRegistry>>,
}
impl GLPlayerThread {
pub fn new(external_images: Arc<Mutex<WebrenderExternalImageRegistry>>) -> Self {
GLPlayerThread {
players: Default::default(),
external_images,
}
}
pub fn start(
external_images: Arc<Mutex<WebrenderExternalImageRegistry>>,
) -> GLPlayerSender<GLPlayerMsg> {
let (sender, receiver) = glplayer_channel::<GLPlayerMsg>().unwrap();
thread::Builder::new()
.name("GLPlayer".to_owned())
.spawn(move || {
let mut renderer = GLPlayerThread::new(external_images);
loop {
let msg = receiver.recv().unwrap();
let exit = renderer.handle_msg(msg);
if exit {
return;
}
}
})
.expect("Thread spawning failed");
sender
}
#[inline]
fn handle_msg(&mut self, msg: GLPlayerMsg) -> bool {
trace!("processing {:?}", msg);
match msg {
GLPlayerMsg::RegisterPlayer(sender) => {
let id = self
.external_images
.lock()
.unwrap()
.next_id(WebrenderImageHandlerType::Media)
.0;
self.players.insert(id, sender.clone());
sender.send(GLPlayerMsgForward::PlayerId(id)).unwrap();
},
GLPlayerMsg::UnregisterPlayer(id) => {
self.external_images
.lock()
.unwrap()
.remove(&ExternalImageId(id));
if self.players.remove(&id).is_none() {
warn!("Tried to remove an unknown player");
}
},
GLPlayerMsg::Lock(id, handler_sender) => {
if let Some(sender) = self.players.get(&id) {
sender.send(GLPlayerMsgForward::Lock(handler_sender)).ok();
}
},
GLPlayerMsg::Unlock(id) => {
if let Some(sender) = self.players.get(&id) {
sender.send(GLPlayerMsgForward::Unlock()).ok();
}
},
GLPlayerMsg::Exit => return true,
}
false
}
}