Skip to content

Commit

Permalink
extended MediaSessionEvent with SetPositionState
Browse files Browse the repository at this point in the history
  • Loading branch information
shnmorimoto committed Dec 2, 2019
1 parent 3e83b0b commit 8bdee36
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
20 changes: 20 additions & 0 deletions components/embedder_traits/lib.rs
Expand Up @@ -240,11 +240,31 @@ pub enum MediaSessionPlaybackState {
Paused,
}

/// https://w3c.github.io/mediasession/#dictdef-mediapositionstate
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct MediaPositionState {
pub duration: Option<f64>,
pub playback_rate: Option<f64>,
pub position: Option<f64>,
}

impl MediaPositionState {
pub fn new(duration: f64, playback_rate: f64, position: f64) -> Self {
Self {
duration,
playback_rate,
position,
}
}
}

/// Type of events sent from script to the embedder about the media session.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum MediaSessionEvent {
/// Indicates that the media metadata is available.
SetMetadata(MediaMetadata),
/// Indicates that the playback state has changed.
PlaybackStateChange(MediaSessionPlaybackState),
/// Indicates that the position state is set.
SetPositionState(MediaPositionState),
}
11 changes: 10 additions & 1 deletion components/script/dom/htmlmediaelement.rs
Expand Up @@ -67,7 +67,7 @@ use crate::script_thread::ScriptThread;
use crate::task_source::TaskSource;
use dom_struct::dom_struct;
use embedder_traits::resources::{self, Resource as EmbedderResource};
use embedder_traits::{MediaSessionEvent, MediaSessionPlaybackState};
use embedder_traits::{MediaPositionState, MediaSessionEvent, MediaSessionPlaybackState};
use euclid::default::Size2D;
use headers::{ContentLength, ContentRange, HeaderMapExt};
use html5ever::{LocalName, Prefix};
Expand Down Expand Up @@ -1780,6 +1780,15 @@ impl HTMLMediaElement {
.add(self.playback_position.get(), position);
self.playback_position.set(position);
self.time_marches_on();
let media_position_state =
MediaPositionState::new(self.duration.get(), self.playbackRate.get(), position);
debug!(
"Sending media session event set position state {:?}",
media_position_state
);
self.send_media_session_event(MediaSessionEvent::SetPositionState(
media_position_state,
));
},
PlayerEvent::SeekData(p, ref seek_lock) => {
self.fetch_request(Some(p), Some(seek_lock.clone()));
Expand Down

0 comments on commit 8bdee36

Please sign in to comment.