Nacre/vendor/servo-script/dom/media/sourcebufferlist.rs
astar76 a361bf31f6 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

77 lines
2.6 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 dom_struct::dom_struct;
use js::context::JSContext;
use script_bindings::cell::DomRefCell;
use script_bindings::reflector::reflect_dom_object;
use stylo_atoms::Atom;
use crate::dom::bindings::codegen::Bindings::SourceBufferListBinding::SourceBufferListMethods;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::eventtarget::EventTarget;
use crate::dom::globalscope::GlobalScope;
use crate::dom::sourcebuffer::SourceBuffer;
#[dom_struct]
pub(crate) struct SourceBufferList {
eventtarget: EventTarget,
buffers: DomRefCell<Vec<Dom<SourceBuffer>>>,
}
impl SourceBufferList {
pub(crate) fn new(cx: &mut JSContext, global: &GlobalScope) -> DomRoot<SourceBufferList> {
reflect_dom_object(
cx,
Box::new(SourceBufferList {
eventtarget: EventTarget::new_inherited(),
buffers: Default::default(),
}),
global,
)
}
pub(crate) fn len(&self) -> usize {
self.buffers.borrow().len()
}
pub(crate) fn item(&self, index: usize) -> Option<DomRoot<SourceBuffer>> {
self.buffers
.borrow()
.get(index)
.map(|buffer| DomRoot::from_ref(&**buffer))
}
pub(crate) fn push(&self, cx: &mut JSContext, buffer: &SourceBuffer) {
self.buffers.borrow_mut().push(Dom::from_ref(buffer));
self.upcast::<EventTarget>()
.fire_event(cx, Atom::from("addsourcebuffer"));
}
pub(crate) fn remove(&self, cx: &mut JSContext, buffer: &SourceBuffer) -> bool {
let Some(index) = self.buffers.borrow().iter().position(|b| &**b == buffer) else {
return false;
};
self.buffers.borrow_mut().remove(index);
self.upcast::<EventTarget>()
.fire_event(cx, Atom::from("removesourcebuffer"));
true
}
}
impl SourceBufferListMethods<crate::DomTypeHolder> for SourceBufferList {
/// <https://w3c.github.io/media-source/#dom-sourcebufferlist-length>
fn Length(&self) -> u32 {
self.len() as u32
}
event_handler!(addsourcebuffer, GetOnaddsourcebuffer, SetOnaddsourcebuffer);
event_handler!(removesourcebuffer, GetOnremovesourcebuffer, SetOnremovesourcebuffer);
/// <https://w3c.github.io/media-source/#dfn-sourcebufferlist-getter>
fn IndexedGetter(&self, index: u32) -> Option<DomRoot<SourceBuffer>> {
self.item(index as usize)
}
}