Media crate

This commit is contained in:
Fernando Jiménez Moreno 2019-06-24 18:45:31 +02:00
parent 2b3a8bf490
commit 7d589ed4f5
23 changed files with 152 additions and 128 deletions

View file

@ -16,7 +16,5 @@ mod raqote_backend;
pub mod canvas_data;
pub mod canvas_paint_thread;
pub mod gl_context;
mod media_mode;
pub mod media_thread;
mod webgl_mode;
pub mod webgl_thread;

View file

@ -1,83 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use crate::media_thread::{GLPlayerExternalImageApi, GLPlayerExternalImageHandler, GLPlayerThread};
use canvas_traits::media::glplayer_channel;
use canvas_traits::media::{
GLPlayerChan, GLPlayerMsg, GLPlayerPipeline, GLPlayerReceiver, GLPlayerSender,
};
use euclid::Size2D;
/// GLPlayer Threading API entry point that lives in the constellation.
pub struct GLPlayerThreads(GLPlayerSender<GLPlayerMsg>);
impl GLPlayerThreads {
pub fn new() -> (GLPlayerThreads, Box<dyn webrender::ExternalImageHandler>) {
let channel = GLPlayerThread::start();
let external =
GLPlayerExternalImageHandler::new(GLPlayerExternalImages::new(channel.clone()));
(GLPlayerThreads(channel), Box::new(external))
}
/// Gets the GLPlayerThread handle for each script pipeline.
pub fn pipeline(&self) -> GLPlayerPipeline {
// This mode creates a single thread, so the existing
// GLPlayerChan is just cloned.
GLPlayerPipeline(GLPlayerChan(self.0.clone()))
}
/// Sends an exit message to close the GLPlayerThreads
pub fn exit(&self) -> Result<(), &'static str> {
self.0
.send(GLPlayerMsg::Exit)
.map_err(|_| "Failed to send Exit message")
}
}
/// Bridge between the webrender::ExternalImage callbacks and the
/// GLPlayerThreads.
struct GLPlayerExternalImages {
// @FIXME(victor): this should be added when GstGLSyncMeta is
// added
//webrender_gl: Rc<dyn gl::Gl>,
glplayer_channel: GLPlayerSender<GLPlayerMsg>,
// Used to avoid creating a new channel on each received WebRender
// request.
lock_channel: (
GLPlayerSender<(u32, Size2D<i32>, usize)>,
GLPlayerReceiver<(u32, Size2D<i32>, usize)>,
),
}
impl GLPlayerExternalImages {
fn new(channel: GLPlayerSender<GLPlayerMsg>) -> Self {
Self {
glplayer_channel: channel,
lock_channel: glplayer_channel().unwrap(),
}
}
}
impl GLPlayerExternalImageApi for GLPlayerExternalImages {
fn lock(&mut self, id: u64) -> (u32, Size2D<i32>) {
// The GLPlayerMsgForward::Lock message inserts a fence in the
// GLPlayer command queue.
self.glplayer_channel
.send(GLPlayerMsg::Lock(id, self.lock_channel.0.clone()))
.unwrap();
let (image_id, size, _gl_sync) = self.lock_channel.1.recv().unwrap();
// The next glWaitSync call is run on the WR thread and it's
// used to synchronize the two flows of OpenGL commands in
// order to avoid WR using a semi-ready GLPlayer texture.
// glWaitSync doesn't block WR thread, it affects only
// internal OpenGL subsystem.
//self.webrender_gl
// .wait_sync(gl_sync as gl::GLsync, 0, gl::TIMEOUT_IGNORED);
(image_id, size)
}
fn unlock(&mut self, id: u64) {
self.glplayer_channel.send(GLPlayerMsg::Unlock(id)).unwrap();
}
}

View file

@ -1,6 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
mod inprocess;
pub use self::inprocess::GLPlayerThreads;

View file

@ -1,140 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use canvas_traits::media::*;
use euclid::Size2D;
use fnv::FnvHashMap;
use std::thread;
/// GL player threading API entry point that lives in the
/// constellation.
///
/// It allows to get a GLPlayerThead handle for each script pipeline.
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 {
// 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 {
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 mut renderer = GLPlayerThread::new();
loop {
let msg = receiver.recv().unwrap();
let exit = renderer.handle_msg(msg);
if exit {
return;
}
}
})
.expect("Thread spawning failed");
sender
}
/// Handles a generic WebGLMsg message
#[inline]
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
}
}
/// This trait is used as a bridge between the `GLPlayerThreads`
/// implementation and the WR ExternalImageHandler API implemented in
/// the `GLPlayerExternalImageHandler` struct.
//
/// `GLPlayerExternalImageHandler<T>` takes care of type conversions
/// between WR and GLPlayer info (e.g keys, uvs).
//
/// It uses this trait to notify lock/unlock messages and get the
/// required info that WR needs.
//
/// `GLPlayerThreads` receives lock/unlock message notifications and
/// takes care of sending the unlock/lock messages to the appropiate
/// `GLPlayerThread`.
pub trait GLPlayerExternalImageApi {
fn lock(&mut self, id: u64) -> (u32, Size2D<i32>);
fn unlock(&mut self, id: u64);
}
/// WebRender External Image Handler implementation
pub struct GLPlayerExternalImageHandler<T: GLPlayerExternalImageApi> {
handler: T,
}
impl<T: GLPlayerExternalImageApi> GLPlayerExternalImageHandler<T> {
pub fn new(handler: T) -> Self {
Self { handler: handler }
}
}
impl<T: GLPlayerExternalImageApi> webrender::ExternalImageHandler
for GLPlayerExternalImageHandler<T>
{
/// Lock the external image. Then, WR could start to read the
/// image content.
/// The WR client should not change the image content until the
/// unlock() call.
fn lock(
&mut self,
key: webrender_api::ExternalImageId,
_channel_index: u8,
_rendering: webrender_api::ImageRendering,
) -> webrender::ExternalImage {
let (texture_id, size) = self.handler.lock(key.0);
webrender::ExternalImage {
uv: webrender_api::TexelRect::new(0.0, 0.0, size.width as f32, size.height as f32),
source: webrender::ExternalImageSource::NativeTexture(texture_id),
}
}
/// Unlock the external image. The WR should not read the image
/// content after this call.
fn unlock(&mut self, key: webrender_api::ExternalImageId, _channel_index: u8) {
self.handler.unlock(key.0);
}
}