121 lines
4.1 KiB
Rust
121 lines
4.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 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::refcounted::Trusted;
|
|
use crate::dom::bindings::reflector::DomGlobal;
|
|
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 contains(&self, buffer: &SourceBuffer) -> bool {
|
|
self.buffers.borrow().iter().any(|b| &**b == buffer)
|
|
}
|
|
|
|
pub(crate) fn buffers(&self) -> Vec<DomRoot<SourceBuffer>> {
|
|
self.buffers
|
|
.borrow()
|
|
.iter()
|
|
.map(|buffer| DomRoot::from_ref(&**buffer))
|
|
.collect()
|
|
}
|
|
|
|
/// Inserts at `index` (clamped) and queues `addsourcebuffer`.
|
|
pub(crate) fn insert(&self, index: usize, buffer: &SourceBuffer) {
|
|
let mut buffers = self.buffers.borrow_mut();
|
|
let index = index.min(buffers.len());
|
|
buffers.insert(index, Dom::from_ref(buffer));
|
|
drop(buffers);
|
|
self.queue_event("addsourcebuffer");
|
|
}
|
|
|
|
pub(crate) fn push(&self, buffer: &SourceBuffer) {
|
|
self.insert(usize::MAX, buffer);
|
|
}
|
|
|
|
/// Removes the buffer and queues `removesourcebuffer`; false if it wasn't in the list.
|
|
pub(crate) fn remove(&self, buffer: &SourceBuffer) -> bool {
|
|
let Some(index) = self.buffers.borrow().iter().position(|b| &**b == buffer) else {
|
|
return false;
|
|
};
|
|
self.buffers.borrow_mut().remove(index);
|
|
self.queue_event("removesourcebuffer");
|
|
true
|
|
}
|
|
|
|
/// Empties the list, queueing one `removesourcebuffer` if it had any.
|
|
pub(crate) fn clear(&self) {
|
|
if self.buffers.borrow_mut().drain(..).next().is_some() {
|
|
self.queue_event("removesourcebuffer");
|
|
}
|
|
}
|
|
|
|
fn queue_event(&self, name: &'static str) {
|
|
queue_event(self.upcast(), name);
|
|
}
|
|
}
|
|
|
|
/// Queues a media element task that fires a plain event `name` at `target`. MSE events go through
|
|
/// tasks in the order they were queued, never straight from a backend thread.
|
|
pub(crate) fn queue_event(target: &EventTarget, name: &'static str) {
|
|
let task_manager = target.global().task_manager();
|
|
let target = Trusted::new(target);
|
|
task_manager
|
|
.media_element_task_source()
|
|
.queue(task!(mse_event: move |cx| {
|
|
target.root().fire_event(cx, Atom::from(name));
|
|
}));
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|