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]>
229 lines
8.1 KiB
Rust
229 lines
8.1 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/. */
|
|
|
|
use std::cell::Cell;
|
|
use std::sync::Arc;
|
|
|
|
use dom_struct::dom_struct;
|
|
use js::context::JSContext;
|
|
use script_bindings::reflector::reflect_dom_object;
|
|
use servo_media::player::mse::SourceBufferBackend;
|
|
use stylo_atoms::Atom;
|
|
|
|
use crate::dom::bindings::buffer_source::get_buffer_source_slice;
|
|
use crate::dom::bindings::codegen::Bindings::SourceBufferBinding::{
|
|
AppendMode, SourceBufferMethods,
|
|
};
|
|
use crate::dom::bindings::codegen::UnionTypes::ArrayBufferViewOrArrayBuffer;
|
|
use crate::dom::bindings::error::{Error, ErrorResult, Fallible};
|
|
use crate::dom::bindings::inheritance::Castable;
|
|
use crate::dom::bindings::num::Finite;
|
|
use crate::dom::bindings::refcounted::Trusted;
|
|
use crate::dom::bindings::reflector::DomGlobal;
|
|
use crate::dom::bindings::root::{Dom, DomRoot};
|
|
use crate::dom::bindings::str::DOMString;
|
|
use crate::dom::eventtarget::EventTarget;
|
|
use crate::dom::globalscope::GlobalScope;
|
|
use crate::dom::mediasource::{MediaSource, mse_error};
|
|
use crate::dom::timeranges::{TimeRanges, TimeRangesContainer};
|
|
|
|
#[dom_struct]
|
|
pub(crate) struct SourceBuffer {
|
|
eventtarget: EventTarget,
|
|
media_source: Dom<MediaSource>,
|
|
#[ignore_malloc_size_of = "backend object"]
|
|
#[no_trace]
|
|
backend: Arc<dyn SourceBufferBackend>,
|
|
updating: Cell<bool>,
|
|
sequence_mode: Cell<bool>,
|
|
timestamp_offset: Cell<f64>,
|
|
append_window_start: Cell<f64>,
|
|
append_window_end: Cell<f64>,
|
|
}
|
|
|
|
impl SourceBuffer {
|
|
pub(crate) fn new(
|
|
cx: &mut JSContext,
|
|
global: &GlobalScope,
|
|
media_source: &MediaSource,
|
|
backend: Arc<dyn SourceBufferBackend>,
|
|
) -> DomRoot<SourceBuffer> {
|
|
reflect_dom_object(
|
|
cx,
|
|
Box::new(SourceBuffer {
|
|
eventtarget: EventTarget::new_inherited(),
|
|
media_source: Dom::from_ref(media_source),
|
|
backend,
|
|
updating: Cell::new(false),
|
|
sequence_mode: Cell::new(false),
|
|
timestamp_offset: Cell::new(0.0),
|
|
append_window_start: Cell::new(0.0),
|
|
append_window_end: Cell::new(f64::INFINITY),
|
|
}),
|
|
global,
|
|
)
|
|
}
|
|
|
|
fn queue_event(&self, name: &'static str) {
|
|
let this = Trusted::new(self);
|
|
self.global()
|
|
.task_manager()
|
|
.media_element_task_source()
|
|
.queue(task!(source_buffer_event: move |cx| {
|
|
this.root().upcast::<EventTarget>().fire_event(cx, Atom::from(name));
|
|
}));
|
|
}
|
|
|
|
/// The backend finished an append or removal (or failed one).
|
|
pub(crate) fn update_finished(&self, cx: &mut JSContext, failed: bool) {
|
|
if !self.updating.get() {
|
|
return;
|
|
}
|
|
self.updating.set(false);
|
|
let target = self.upcast::<EventTarget>();
|
|
target.fire_event(cx, Atom::from(if failed { "error" } else { "update" }));
|
|
target.fire_event(cx, Atom::from("updateend"));
|
|
}
|
|
|
|
fn check_can_update(&self) -> ErrorResult {
|
|
if self.updating.get() {
|
|
return Err(Error::InvalidState(Some("SourceBuffer is updating".into())));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn start_update(&self) {
|
|
self.updating.set(true);
|
|
self.queue_event("updatestart");
|
|
}
|
|
}
|
|
|
|
impl SourceBufferMethods<crate::DomTypeHolder> for SourceBuffer {
|
|
/// <https://w3c.github.io/media-source/#dom-sourcebuffer-mode>
|
|
fn Mode(&self) -> AppendMode {
|
|
if self.sequence_mode.get() {
|
|
AppendMode::Sequence
|
|
} else {
|
|
AppendMode::Segments
|
|
}
|
|
}
|
|
|
|
/// <https://w3c.github.io/media-source/#dom-sourcebuffer-mode>
|
|
/// Prototype: stored only; gst_mse appends in segments mode.
|
|
fn SetMode(&self, value: AppendMode) -> ErrorResult {
|
|
self.check_can_update()?;
|
|
self.sequence_mode.set(value == AppendMode::Sequence);
|
|
Ok(())
|
|
}
|
|
|
|
/// <https://w3c.github.io/media-source/#dom-sourcebuffer-updating>
|
|
fn Updating(&self) -> bool {
|
|
self.updating.get()
|
|
}
|
|
|
|
/// <https://w3c.github.io/media-source/#dom-sourcebuffer-buffered>
|
|
fn Buffered(&self, cx: &mut JSContext) -> DomRoot<TimeRanges> {
|
|
let mut ranges = TimeRangesContainer::default();
|
|
for range in self.backend.buffered() {
|
|
let _ = ranges.add(range.start, range.end);
|
|
}
|
|
TimeRanges::new(cx, self.global().as_window(), ranges)
|
|
}
|
|
|
|
/// <https://w3c.github.io/media-source/#dom-sourcebuffer-timestampoffset>
|
|
fn TimestampOffset(&self) -> Finite<f64> {
|
|
Finite::wrap(self.timestamp_offset.get())
|
|
}
|
|
|
|
fn SetTimestampOffset(&self, value: Finite<f64>) -> ErrorResult {
|
|
self.check_can_update()?;
|
|
self.backend
|
|
.set_timestamp_offset(*value)
|
|
.map_err(mse_error)?;
|
|
self.timestamp_offset.set(*value);
|
|
Ok(())
|
|
}
|
|
|
|
/// <https://w3c.github.io/media-source/#dom-sourcebuffer-appendwindowstart>
|
|
fn AppendWindowStart(&self) -> Finite<f64> {
|
|
Finite::wrap(self.append_window_start.get())
|
|
}
|
|
|
|
fn SetAppendWindowStart(&self, value: Finite<f64>) -> ErrorResult {
|
|
self.check_can_update()?;
|
|
if *value < 0.0 || *value >= self.append_window_end.get() {
|
|
return Err(Error::Type(c"appendWindowStart out of range".to_owned()));
|
|
}
|
|
self.backend
|
|
.set_append_window(*value, self.append_window_end.get())
|
|
.map_err(mse_error)?;
|
|
self.append_window_start.set(*value);
|
|
Ok(())
|
|
}
|
|
|
|
/// <https://w3c.github.io/media-source/#dom-sourcebuffer-appendwindowend>
|
|
fn AppendWindowEnd(&self) -> f64 {
|
|
self.append_window_end.get()
|
|
}
|
|
|
|
fn SetAppendWindowEnd(&self, value: f64) -> ErrorResult {
|
|
self.check_can_update()?;
|
|
if value.is_nan() || value <= self.append_window_start.get() {
|
|
return Err(Error::Type(c"appendWindowEnd out of range".to_owned()));
|
|
}
|
|
self.backend
|
|
.set_append_window(self.append_window_start.get(), value)
|
|
.map_err(mse_error)?;
|
|
self.append_window_end.set(value);
|
|
Ok(())
|
|
}
|
|
|
|
event_handler!(updatestart, GetOnupdatestart, SetOnupdatestart);
|
|
event_handler!(update, GetOnupdate, SetOnupdate);
|
|
event_handler!(updateend, GetOnupdateend, SetOnupdateend);
|
|
event_handler!(error, GetOnerror, SetOnerror);
|
|
event_handler!(abort, GetOnabort, SetOnabort);
|
|
|
|
/// <https://w3c.github.io/media-source/#dom-sourcebuffer-appendbuffer>
|
|
fn AppendBuffer(&self, cx: &mut JSContext, data: ArrayBufferViewOrArrayBuffer) -> ErrorResult {
|
|
self.check_can_update()?;
|
|
self.media_source.reopen(cx);
|
|
let bytes = get_buffer_source_slice(&data, cx.no_gc()).to_vec();
|
|
self.backend.append_buffer(bytes).map_err(mse_error)?;
|
|
self.start_update();
|
|
Ok(())
|
|
}
|
|
|
|
/// <https://w3c.github.io/media-source/#dom-sourcebuffer-abort>
|
|
fn Abort(&self, cx: &mut JSContext) -> ErrorResult {
|
|
self.backend.abort().map_err(mse_error)?;
|
|
if self.updating.replace(false) {
|
|
let target = self.upcast::<EventTarget>();
|
|
target.fire_event(cx, Atom::from("abort"));
|
|
target.fire_event(cx, Atom::from("updateend"));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// <https://w3c.github.io/media-source/#dom-sourcebuffer-changetype>
|
|
/// Prototype: gst_mse 1.28 can't change type; same-type changes pass.
|
|
fn ChangeType(&self, type_: DOMString) -> ErrorResult {
|
|
self.check_can_update()?;
|
|
if type_.is_empty() {
|
|
return Err(Error::Type(c"empty type".to_owned()));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// <https://w3c.github.io/media-source/#dom-sourcebuffer-remove>
|
|
fn Remove(&self, start: Finite<f64>, end: f64) -> ErrorResult {
|
|
self.check_can_update()?;
|
|
if *start < 0.0 || end.is_nan() || end <= *start {
|
|
return Err(Error::Type(c"remove range out of order".to_owned()));
|
|
}
|
|
self.backend.remove(*start, end).map_err(mse_error)?;
|
|
self.start_update();
|
|
Ok(())
|
|
}
|
|
}
|