Nacre/vendor/servo-media/lib.rs
astar76 1c078655e2 S022: MSE prototype on gst_mse (vendored script, script-bindings, servo-media*)
MediaSource, SourceBuffer, SourceBufferList over GStreamer's MSE library;
URL.createObjectURL(MediaSource); HTMLMediaElement attaches via mse://.
tests/smoke/mse.txt plays chunk-appended fMP4 and WebM.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-12 10:21:01 -06:00

129 lines
4.9 KiB
Rust

/* 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/. */
pub extern crate servo_media_audio as audio;
pub extern crate servo_media_player as player;
pub extern crate servo_media_streams as streams;
pub extern crate servo_media_traits as traits;
pub extern crate servo_media_webrtc as webrtc;
use std::ops::Deref;
use std::sync::{Arc, Mutex, OnceLock};
use std::thread;
use audio::context::{AudioContext, AudioContextOptions};
use audio::sink::AudioSinkError;
use player::audio::AudioRenderer;
use player::context::PlayerGLContext;
use player::video::VideoFrameRenderer;
use player::mse::{MediaSourceBackend, MseEventSink};
use player::{Player, PlayerEvent, StreamType};
use servo_base::generic_channel::GenericCallback;
use streams::capture::MediaTrackConstraintSet;
use streams::device_monitor::MediaDeviceMonitor;
use streams::registry::MediaStreamId;
use streams::{MediaOutput, MediaSocket, MediaStreamType};
pub use traits::*;
use webrtc::{WebRtcController, WebRtcSignaller};
pub struct ServoMedia(Box<dyn Backend>);
static INSTANCE: OnceLock<Arc<ServoMedia>> = OnceLock::new();
pub trait BackendInit {
fn init() -> Box<dyn Backend>;
}
pub trait BackendDeInit {
fn deinit(&self) {}
}
pub trait Backend: Send + Sync {
fn create_player(
&self,
id: &ClientContextId,
stream_type: StreamType,
sender: GenericCallback<PlayerEvent>,
video_renderer: Option<Arc<Mutex<dyn VideoFrameRenderer>>>,
audio_renderer: Option<Arc<Mutex<dyn AudioRenderer>>>,
gl_context: Box<dyn PlayerGLContext>,
) -> Arc<Mutex<dyn Player>>;
fn create_audiostream(&self) -> MediaStreamId;
fn create_videostream(&self) -> MediaStreamId;
fn create_stream_output(&self) -> Box<dyn MediaOutput>;
fn create_stream_and_socket(
&self,
ty: MediaStreamType,
) -> (Box<dyn MediaSocket>, MediaStreamId);
fn create_audioinput_stream(&self, set: MediaTrackConstraintSet) -> Option<MediaStreamId>;
fn create_videoinput_stream(&self, set: MediaTrackConstraintSet) -> Option<MediaStreamId>;
fn create_audio_context(
&self,
id: &ClientContextId,
options: AudioContextOptions,
) -> Result<Arc<Mutex<AudioContext>>, AudioSinkError>;
fn create_webrtc(&self, signaller: Box<dyn WebRtcSignaller>) -> WebRtcController;
fn can_play_type(&self, media_type: &str) -> SupportsMediaType;
/// Media Source Extensions: `None` when the backend has no MSE support.
fn create_media_source(&self, _events: MseEventSink) -> Option<Arc<dyn MediaSourceBackend>> {
None
}
/// <https://w3c.github.io/media-source/#dom-mediasource-istypesupported>
fn is_media_source_type_supported(&self, _mime: &str) -> bool {
false
}
fn set_capture_mocking(&self, _mock: bool) {}
/// Allow muting/unmuting the media instances associated with the given client context identifier.
/// Backend implementations are responsible for keeping a match between client contexts
/// and the media instances created for these contexts.
/// The client context identifier is currently an abstraction of Servo's PipelineId.
fn mute(&self, _id: &ClientContextId, _val: bool) {}
/// Allow suspending the activity of all media instances associated with the given client
/// context identifier.
/// Note that suspending does not involve releasing any resources, so media playback can
/// be restarted.
/// Backend implementations are responsible for keeping a match between client contexts
/// and the media instances created for these contexts.
/// The client context identifier is currently an abstraction of Servo's PipelineId.
fn suspend(&self, _id: &ClientContextId) {}
/// Allow resuming the activity of all the media instances associated with the given client
/// context identifier.
/// Backend implementations are responsible for keeping a match between client contexts
/// and the media instances created for these contexts.
/// The client context identifier is currently an abstraction of Servo's PipelineId.
fn resume(&self, _id: &ClientContextId) {}
fn get_device_monitor(&self) -> Box<dyn MediaDeviceMonitor>;
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum SupportsMediaType {
Maybe,
No,
Probably,
}
impl ServoMedia {
pub fn init<B: BackendInit>() {
thread::spawn(|| INSTANCE.get_or_init(|| Arc::new(ServoMedia(B::init()))));
}
pub fn init_with_backend<F>(backend_factory: F)
where
F: Fn() -> Box<dyn Backend> + Send + 'static,
{
thread::spawn(move || INSTANCE.get_or_init(|| Arc::new(ServoMedia(backend_factory()))));
}
pub fn get() -> Arc<ServoMedia> {
INSTANCE.wait().clone()
}
}
impl Deref for ServoMedia {
type Target = dyn Backend + 'static;
fn deref(&self) -> &(dyn Backend + 'static) {
&*self.0
}
}