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

@ -0,0 +1,23 @@
[package]
name = "media"
version = "0.0.1"
authors = ["The Servo Project Developers"]
license = "MPL-2.0"
edition = "2018"
publish = false
[lib]
name = "media"
path = "lib.rs"
[dependencies]
euclid = "0.19"
fnv = "1.0"
ipc-channel = "0.11"
lazy_static = "1"
log = "0.4"
serde = "1.0"
servo_config = {path = "../config"}
servo-media = {git = "https://github.com/servo/media"}
webrender = {git = "https://github.com/servo/webrender"}
webrender_api = {git = "https://github.com/servo/webrender", features = ["ipc"]}

170
components/media/lib.rs Normal file
View file

@ -0,0 +1,170 @@
/* 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/. */
#![crate_name = "media"]
#![crate_type = "rlib"]
#![deny(unsafe_code)]
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate log;
#[macro_use]
extern crate serde;
use euclid::Size2D;
use servo_media::player::context::{GlApi, GlContext, NativeDisplay, PlayerGLContext};
mod media_channel;
mod media_thread;
pub use crate::media_channel::glplayer_channel;
use crate::media_channel::{GLPlayerChan, GLPlayerPipeline, GLPlayerReceiver, GLPlayerSender};
use crate::media_thread::{GLPlayerExternalImageApi, GLPlayerExternalImageHandler, GLPlayerThread};
/// These are the messages that the GLPlayer thread will forward to
/// the video player which lives in htmlmediaelement
#[derive(Debug, Deserialize, Serialize)]
pub enum GLPlayerMsgForward {
PlayerId(u64),
Lock(GLPlayerSender<(u32, Size2D<i32>, usize)>),
Unlock(),
}
/// GLPlayer thread Message API
///
/// These are the message that the thread will receive from the
/// constellation, the webrender::ExternalImageHandle multiplexor
/// implementation, or a htmlmediaelement
#[derive(Debug, Deserialize, Serialize)]
pub enum GLPlayerMsg {
/// Registers an instantiated player in DOM
RegisterPlayer(GLPlayerSender<GLPlayerMsgForward>),
/// Unregisters a player's ID
UnregisterPlayer(u64),
/// Locks a specific texture from a player. Lock messages are used
/// for a correct synchronization with WebRender external image
/// API.
///
/// WR locks a external texture when it wants to use the shared
/// texture contents.
///
/// The WR client should not change the shared texture content
/// until the Unlock call.
///
/// Currently OpenGL Sync Objects are used to implement the
/// synchronization mechanism.
Lock(u64, GLPlayerSender<(u32, Size2D<i32>, usize)>),
/// Unlocks a specific texture from a player. Unlock messages are
/// used for a correct synchronization with WebRender external
/// image API.
///
/// The WR unlocks a context when it finished reading the shared
/// texture contents.
///
/// Unlock messages are always sent after a Lock message.
Unlock(u64),
/// Frees all resources and closes the thread.
Exit,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct WindowGLContext {
/// Application's GL Context
pub gl_context: GlContext,
/// Application's GL Api
pub gl_api: GlApi,
/// Application's native display
pub native_display: NativeDisplay,
/// A channel to the GLPlayer thread.
pub glplayer_chan: Option<GLPlayerPipeline>,
}
impl PlayerGLContext for WindowGLContext {
fn get_gl_context(&self) -> GlContext {
self.gl_context.clone()
}
fn get_native_display(&self) -> NativeDisplay {
self.native_display.clone()
}
fn get_gl_api(&self) -> GlApi {
self.gl_api.clone()
}
}
/// 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

@ -0,0 +1,14 @@
/* 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 serde::{Deserialize, Serialize};
use std::io;
pub type GLPlayerSender<T> = ipc_channel::ipc::IpcSender<T>;
pub type GLPlayerReceiver<T> = ipc_channel::ipc::IpcReceiver<T>;
pub fn glplayer_channel<T: Serialize + for<'de> Deserialize<'de>>(
) -> Result<(GLPlayerSender<T>, GLPlayerReceiver<T>), io::Error> {
ipc_channel::ipc::channel()
}

View file

@ -0,0 +1,114 @@
/* 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/. */
//! Enum wrappers to be able to select different channel implementations at runtime.
mod ipc;
mod mpsc;
use crate::GLPlayerMsg;
use serde::{Deserialize, Serialize};
use servo_config::opts;
use std::fmt;
lazy_static! {
static ref IS_MULTIPROCESS: bool = { opts::multiprocess() };
}
#[derive(Deserialize, Serialize)]
pub enum GLPlayerSender<T: Serialize> {
Ipc(ipc::GLPlayerSender<T>),
Mpsc(mpsc::GLPlayerSender<T>),
}
impl<T> Clone for GLPlayerSender<T>
where
T: Serialize,
{
fn clone(&self) -> Self {
match *self {
GLPlayerSender::Ipc(ref chan) => GLPlayerSender::Ipc(chan.clone()),
GLPlayerSender::Mpsc(ref chan) => GLPlayerSender::Mpsc(chan.clone()),
}
}
}
impl<T: Serialize> fmt::Debug for GLPlayerSender<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "GLPlayerSender(..)")
}
}
impl<T: Serialize> GLPlayerSender<T> {
#[inline]
pub fn send(&self, msg: T) -> GLPlayerSendResult {
match *self {
GLPlayerSender::Ipc(ref sender) => sender.send(msg).map_err(|_| ()),
GLPlayerSender::Mpsc(ref sender) => sender.send(msg).map_err(|_| ()),
}
}
}
pub type GLPlayerSendResult = Result<(), ()>;
pub enum GLPlayerReceiver<T>
where
T: for<'de> Deserialize<'de> + Serialize,
{
Ipc(ipc::GLPlayerReceiver<T>),
Mpsc(mpsc::GLPlayerReceiver<T>),
}
impl<T> GLPlayerReceiver<T>
where
T: for<'de> Deserialize<'de> + Serialize,
{
pub fn recv(&self) -> Result<T, ()> {
match *self {
GLPlayerReceiver::Ipc(ref receiver) => receiver.recv().map_err(|_| ()),
GLPlayerReceiver::Mpsc(ref receiver) => receiver.recv().map_err(|_| ()),
}
}
pub fn to_opaque(self) -> ipc_channel::ipc::OpaqueIpcReceiver {
match self {
GLPlayerReceiver::Ipc(receiver) => receiver.to_opaque(),
_ => unreachable!(),
}
}
}
pub fn glplayer_channel<T>() -> Result<(GLPlayerSender<T>, GLPlayerReceiver<T>), ()>
where
T: for<'de> Deserialize<'de> + Serialize,
{
// Let's use Ipc until we move the Player instance into GPlayerThread
if true {
ipc::glplayer_channel()
.map(|(tx, rx)| (GLPlayerSender::Ipc(tx), GLPlayerReceiver::Ipc(rx)))
.map_err(|_| ())
} else {
mpsc::glplayer_channel()
.map(|(tx, rx)| (GLPlayerSender::Mpsc(tx), GLPlayerReceiver::Mpsc(rx)))
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct GLPlayerChan(pub GLPlayerSender<GLPlayerMsg>);
impl GLPlayerChan {
#[inline]
pub fn send(&self, msg: GLPlayerMsg) -> GLPlayerSendResult {
self.0.send(msg)
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct GLPlayerPipeline(pub GLPlayerChan);
impl GLPlayerPipeline {
pub fn channel(&self) -> GLPlayerChan {
self.0.clone()
}
}

View file

@ -0,0 +1,58 @@
/* 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 serde::{Deserialize, Serialize};
use serde::{Deserializer, Serializer};
use std::sync::mpsc;
#[macro_use]
macro_rules! unreachable_serializable {
($name:ident) => {
impl<T> Serialize for $name<T> {
fn serialize<S: Serializer>(&self, _: S) -> Result<S::Ok, S::Error> {
unreachable!();
}
}
impl<'a, T> Deserialize<'a> for $name<T> {
fn deserialize<D>(_: D) -> Result<$name<T>, D::Error>
where
D: Deserializer<'a>,
{
unreachable!();
}
}
};
}
pub struct GLPlayerSender<T>(mpsc::Sender<T>);
pub struct GLPlayerReceiver<T>(mpsc::Receiver<T>);
impl<T> Clone for GLPlayerSender<T> {
fn clone(&self) -> Self {
GLPlayerSender(self.0.clone())
}
}
impl<T> GLPlayerSender<T> {
#[inline]
pub fn send(&self, data: T) -> Result<(), mpsc::SendError<T>> {
self.0.send(data)
}
}
impl<T> GLPlayerReceiver<T> {
#[inline]
pub fn recv(&self) -> Result<T, mpsc::RecvError> {
self.0.recv()
}
}
pub fn glplayer_channel<T>() -> Result<(GLPlayerSender<T>, GLPlayerReceiver<T>), ()> {
let (sender, receiver) = mpsc::channel();
Ok((GLPlayerSender(sender), GLPlayerReceiver(receiver)))
}
unreachable_serializable!(GLPlayerReceiver);
unreachable_serializable!(GLPlayerSender);

View file

@ -0,0 +1,137 @@
/* 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_channel::{glplayer_channel, GLPlayerSender};
/// GL player threading API entry point that lives in the
/// constellation.
use crate::{GLPlayerMsg, GLPlayerMsgForward};
use euclid::Size2D;
use fnv::FnvHashMap;
use std::thread;
/// 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);
}
}