Skip to content

Commit

Permalink
Media element duration param
Browse files Browse the repository at this point in the history
  • Loading branch information
ferjm committed Oct 8, 2018
1 parent 6904535 commit f4ba7e5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
1 change: 1 addition & 0 deletions components/atoms/static_atoms.txt
Expand Up @@ -18,6 +18,7 @@ cursive
date
datetime-local
dir
durationchange
email
emptied
ended
Expand Down
35 changes: 26 additions & 9 deletions components/script/dom/htmlmediaelement.rs
Expand Up @@ -49,6 +49,7 @@ use servo_media::ServoMedia;
use servo_url::ServoUrl;
use std::cell::Cell;
use std::collections::VecDeque;
use std::f64;
use std::mem;
use std::rc::Rc;
use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -165,6 +166,8 @@ pub struct HTMLMediaElement {
fetch_canceller: DomRefCell<FetchCanceller>,
/// https://html.spec.whatwg.org/multipage/media.html#show-poster-flag
show_poster: Cell<bool>,
/// https://html.spec.whatwg.org/multipage/media.html#dom-media-duration
duration: Cell<f64>,
}

/// <https://html.spec.whatwg.org/multipage/#dom-media-networkstate>
Expand Down Expand Up @@ -211,6 +214,7 @@ impl HTMLMediaElement {
Arc::new(Mutex::new(MediaFrameRenderer::new(document.window().get_webrender_api_sender()))),
fetch_canceller: DomRefCell::new(Default::default()),
show_poster: Cell::new(true),
duration: Cell::new(f64::NAN),
}
}

Expand Down Expand Up @@ -838,7 +842,7 @@ impl HTMLMediaElement {
// FIXME(nox): Set timeline offset to NaN.

// Step 6.10.
// FIXME(nox): Set duration to NaN.
self.duration.set(f64::NAN);
}

// Step 7.
Expand Down Expand Up @@ -968,27 +972,35 @@ impl HTMLMediaElement {
// to the earliest possible position.

// Step 4.
// XXX(ferjm) Update duration.
if let Some(duration) = metadata.duration {
self.duration.set(duration.as_secs() as f64);
} else {
self.duration.set(f64::INFINITY);
}
let window = window_from_node(self);
let task_source = window.dom_manipulation_task_source();
task_source.queue_simple_event(
self.upcast(),
atom!("durationchange"),
&window,
);

// Step 5.
if self.is::<HTMLVideoElement>() {
assert_ne!(self.ready_state.get(), ReadyState::HaveNothing);
let video_elem = self.downcast::<HTMLVideoElement>().unwrap();
video_elem.set_video_width(metadata.width);
video_elem.set_video_height(metadata.height);
let window = window_from_node(self);
window.dom_manipulation_task_source().queue_simple_event(
task_source.queue_simple_event(
self.upcast(),
atom!("resize"),
&window,
);
}

if let Some(_dur) = metadata.duration {
// Step 6.
self.change_ready_state(ReadyState::HaveMetadata);
self.have_metadata.set(true);
}
// Step 6.
self.change_ready_state(ReadyState::HaveMetadata);
self.have_metadata.set(true);
} else {
// => set the element's delaying-the-load-event flag to false
self.change_ready_state(ReadyState::HaveCurrentData);
Expand Down Expand Up @@ -1104,6 +1116,11 @@ impl HTMLMediaElementMethods for HTMLMediaElement {
fn Paused(&self) -> bool {
self.paused.get()
}

// https://html.spec.whatwg.org/multipage/media.html#dom-media-duration
fn Duration(&self) -> f64 {
self.duration.get()
}
}

impl VirtualMethods for HTMLMediaElement {
Expand Down
2 changes: 2 additions & 0 deletions components/script/dom/htmlvideoelement.rs
Expand Up @@ -15,7 +15,9 @@ use std::cell::Cell;
#[dom_struct]
pub struct HTMLVideoElement {
htmlmediaelement: HTMLMediaElement,
/// https://html.spec.whatwg.org/multipage/media.html#dom-video-videowidth
video_width: Cell<u32>,
/// https://html.spec.whatwg.org/multipage/media.html#dom-video-videoheight
video_height: Cell<u32>,
}

Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/webidls/HTMLMediaElement.webidl
Expand Up @@ -39,7 +39,7 @@ interface HTMLMediaElement : HTMLElement {
// playback state
// attribute double currentTime;
// void fastSeek(double time);
// readonly attribute unrestricted double duration;
readonly attribute unrestricted double duration;
// Date getStartDate();
readonly attribute boolean paused;
// attribute double defaultPlaybackRate;
Expand Down

0 comments on commit f4ba7e5

Please sign in to comment.