Skip to content
Permalink
Browse files

GLPlayer thread API implementation

  • Loading branch information...
ceyusa authored and ferjm committed Jun 14, 2019
1 parent 59aacb0 commit 38eb48441c4691e85cf7fba6bedcb99abfd8a7d6
Showing with 34 additions and 5 deletions.
  1. +34 −5 components/canvas/media_thread.rs
@@ -3,6 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use canvas_traits::media::*;
use fnv::FnvHashMap;
use std::thread;

/// GL player threading API entry point that lives in the
@@ -13,19 +14,27 @@ pub use crate::media_mode::GLPlayerThreads;

/// A GLPlayerThrx1ead manages the life cycle and message multiplexign of
/// a set of video players with GL render.
pub struct GLPlayerThread ();
pub struct GLPlayerThread {
// Map of live players.
players: FnvHashMap<u64, GLPlayerSender<GLPlayerMsgForward>>,
/// Id generator for new WebGLContexts.
next_player_id: u64,
}

impl GLPlayerThread {
pub fn new() -> Self {
GLPlayerThread()
GLPlayerThread {
players: Default::default(),
next_player_id: 1,
}
}

pub fn start() -> GLPlayerSender<GLPlayerMsg> {
let (sender, receiver) = glplayer_channel::<GLPlayerMsg>().unwrap();
thread::Builder::new()
.name("GLPlayerThread".to_owned())
.spawn(move || {
let renderer = GLPlayerThread::new();
let mut renderer = GLPlayerThread::new();
loop {
let msg = receiver.recv().unwrap();
let exit = renderer.handle_msg(msg);
@@ -41,11 +50,31 @@ impl GLPlayerThread {

/// Handles a generic WebGLMsg message
#[inline]
fn handle_msg(&self, msg: GLPlayerMsg) -> bool {
fn handle_msg(&mut self, msg: GLPlayerMsg) -> bool {
trace!("processing {:?}", msg);
match msg {
GLPlayerMsg::RegisterPlayer(sender) => {
let id = self.next_player_id;
self.players.insert(id, sender.clone());
sender.send(GLPlayerMsgForward::PlayerId(id)).unwrap();
self.next_player_id += 1;
},
GLPlayerMsg::UnregisterPlayer(id) => {
if self.players.remove(&id).is_none() {
warn!("Tried to remove an unknown player");
}
},
GLPlayerMsg::Lock(id, handler_sender) => {
self.players.get(&id).map(|sender| {
sender.send(GLPlayerMsgForward::Lock(handler_sender)).ok();
});
},
GLPlayerMsg::Unlock(id) => {
self.players.get(&id).map(|sender| {
sender.send(GLPlayerMsgForward::Unlock()).ok();
});
},
GLPlayerMsg::Exit => return true,
_ => (),
}

false

0 comments on commit 38eb484

Please sign in to comment.
You can’t perform that action at this time.