Skip to content
Permalink
Browse files

Introduce CMediaSessionPlaybackState and CMediaSessionActionType

  • Loading branch information
ferjm committed Dec 2, 2019
1 parent d5200ba commit b01e3fdc7ee2c3cd8b11455ec31a5c86ffec7e80
@@ -7,7 +7,8 @@ extern crate log;

pub mod gl_glue;

pub use servo::script_traits::MouseButton;
pub use servo::embedder_traits::MediaSessionPlaybackState;
pub use servo::script_traits::{MediaSessionActionType, MouseButton};

use getopts::Options;
use servo::compositing::windowing::{
@@ -133,7 +134,7 @@ pub trait HostTrait {
/// Called when we get the media session metadata/
fn on_media_session_metadata(&self, title: String, artist: String, album: String);
/// Called when the media session playback state changes.
fn on_media_session_playback_state_change(&self, state: i32);
fn on_media_session_playback_state_change(&self, state: MediaSessionPlaybackState);
/// Called when the media session position state is set.
fn on_media_session_set_position_state(&self, duration: f64, position: f64, playback_rate: f64);
}
@@ -472,9 +473,12 @@ impl ServoGlue {
self.process_event(WindowEvent::Keyboard(key_event))
}

pub fn media_session_action(&mut self, action: i32) -> Result<(), &'static str> {
pub fn media_session_action(
&mut self,
action: MediaSessionActionType,
) -> Result<(), &'static str> {
info!("Media session action {:?}", action);
self.process_event(WindowEvent::MediaSessionAction(action.into()))
self.process_event(WindowEvent::MediaSessionAction(action))
}

fn process_event(&mut self, event: WindowEvent) -> Result<(), &'static str> {
@@ -595,7 +599,7 @@ impl ServoGlue {
MediaSessionEvent::PlaybackStateChange(state) => self
.callbacks
.host_callbacks
.on_media_session_playback_state_change(state as i32),
.on_media_session_playback_state_change(state),
MediaSessionEvent::SetPositionState(position_state) => self
.callbacks
.host_callbacks
@@ -17,7 +17,8 @@ use env_logger;
use log::LevelFilter;
use simpleservo::{self, gl_glue, ServoGlue, SERVO};
use simpleservo::{
Coordinates, EventLoopWaker, HostTrait, InitOptions, MouseButton, VRInitOptions,
Coordinates, EventLoopWaker, HostTrait, InitOptions, MediaSessionActionType,
MediaSessionPlaybackState, MouseButton, VRInitOptions,
};
use std::ffi::{CStr, CString};
#[cfg(target_os = "windows")]
@@ -218,7 +219,7 @@ pub struct CHostCallbacks {
pub set_clipboard_contents: extern "C" fn(contents: *const c_char),
pub on_media_session_metadata:
extern "C" fn(title: *const c_char, album: *const c_char, artist: *const c_char),
pub on_media_session_playback_state_change: extern "C" fn(state: i32),
pub on_media_session_playback_state_change: extern "C" fn(state: CMediaSessionPlaybackState),
pub on_media_session_set_position_state:
extern "C" fn(duration: f64, position: f64, playback_rate: f64),
}
@@ -254,6 +255,52 @@ impl CMouseButton {
}
}

#[repr(C)]
pub enum CMediaSessionPlaybackState {
None = 1,
Playing,
Paused,
}

impl From<MediaSessionPlaybackState> for CMediaSessionPlaybackState {
fn from(state: MediaSessionPlaybackState) -> Self {
match state {
MediaSessionPlaybackState::None_ => CMediaSessionPlaybackState::None,
MediaSessionPlaybackState::Playing => CMediaSessionPlaybackState::Playing,
MediaSessionPlaybackState::Paused => CMediaSessionPlaybackState::Paused,
}
}
}

#[repr(C)]
pub enum CMediaSessionActionType {
Play = 1,
Pause,
SeekBackward,
SeekForward,
PreviousTrack,
NextTrack,
SkipAd,
Stop,
SeekTo,
}

impl CMediaSessionActionType {
pub fn convert(&self) -> MediaSessionActionType {
match self {
CMediaSessionActionType::Play => MediaSessionActionType::Play,
CMediaSessionActionType::Pause => MediaSessionActionType::Pause,
CMediaSessionActionType::SeekBackward => MediaSessionActionType::SeekBackward,
CMediaSessionActionType::SeekForward => MediaSessionActionType::SeekForward,
CMediaSessionActionType::PreviousTrack => MediaSessionActionType::PreviousTrack,
CMediaSessionActionType::NextTrack => MediaSessionActionType::NextTrack,
CMediaSessionActionType::SkipAd => MediaSessionActionType::SkipAd,
CMediaSessionActionType::Stop => MediaSessionActionType::Stop,
CMediaSessionActionType::SeekTo => MediaSessionActionType::SeekTo,
}
}
}

/// The returned string is not freed. This will leak.
#[no_mangle]
pub extern "C" fn servo_version() -> *const c_char {
@@ -608,10 +655,10 @@ pub extern "C" fn click(x: f32, y: f32) {
}

#[no_mangle]
pub extern "C" fn media_session_action(action: i32) {
pub extern "C" fn media_session_action(action: CMediaSessionActionType) {
catch_any_panic(|| {
debug!("media_session_action");
call(|s| s.media_session_action(action));
call(|s| s.media_session_action(action.convert()));
});
}

@@ -733,9 +780,9 @@ impl HostTrait for HostCallbacks {
(self.0.on_media_session_metadata)(title.as_ptr(), artist.as_ptr(), album.as_ptr());
}

fn on_media_session_playback_state_change(&self, state: i32) {
fn on_media_session_playback_state_change(&self, state: MediaSessionPlaybackState) {
debug!("on_media_session_playback_state_change {:?}", state);
(self.0.on_media_session_playback_state_change)(state);
(self.0.on_media_session_playback_state_change)(state.into());
}

fn on_media_session_set_position_state(
@@ -7,7 +7,6 @@
#include "BrowserPage.h"
#include "BrowserPage.g.cpp"
#include "DefaultUrl.h"
#include "MediaSession.h"

using namespace std::placeholders;
using namespace winrt::Windows::Foundation;

This file was deleted.

@@ -122,7 +122,6 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="logs.h" />
<ClInclude Include="MediaSession.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="App.h">
<DependentUpon>App.xaml</DependentUpon>
@@ -40,9 +40,6 @@
</ClInclude>
<ClInclude Include="DefaultUrl.h" />
<ClInclude Include="XRPkgChecker.h" />
<ClInclude Include="MediaSession.h">
<Filter>ServoControl</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Image Include="Assets\Wide310x150Logo.scale-200.png">
@@ -62,7 +62,8 @@ void on_media_session_metadata(const char *title, const char *album,
char2hstring(title), char2hstring(album), char2hstring(artist));
}

void on_media_session_playback_state_change(const int state) {
void on_media_session_playback_state_change(
const capi::CMediaSessionPlaybackState state) {
return sServo->Delegate().OnServoMediaSessionPlaybackStateChange(state);
}

@@ -50,6 +50,8 @@ class Servo {
ServoDelegate &Delegate() { return mDelegate; }

typedef capi::CMouseButton MouseButton;
typedef capi::CMediaSessionActionType MediaSessionActionType;
typedef capi::CMediaSessionPlaybackState MediaSessionPlaybackState;

void PerformUpdates() { capi::perform_updates(); }
void DeInit() { capi::deinit(); }
@@ -88,7 +90,7 @@ class Servo {
capi::resize(mWindowWidth, mWindowHeight);
}
}
void SendMediaSessionAction(int32_t action) {
void SendMediaSessionAction(capi::CMediaSessionActionType action) {
capi::media_session_action(action);
}

@@ -272,7 +272,10 @@ hstring ServoControl::LoadURIOrSearch(hstring input) {
}

void ServoControl::SendMediaSessionAction(int32_t action) {
RunOnGLThread([=] { mServo->SendMediaSessionAction(action); });
RunOnGLThread([=] {
mServo->SendMediaSessionAction(
static_cast<Servo::MediaSessionActionType>(action));
});
}

void ServoControl::TryLoadUri(hstring input) {
@@ -3,7 +3,6 @@
#include "OpenGLES.h"
#include "Servo.h"
#include "DefaultUrl.h"
#include "MediaSession.h"

namespace winrt::ServoApp::implementation {
struct ServoControl : ServoControlT<ServoControl>, public servo::ServoDelegate {

0 comments on commit b01e3fd

Please sign in to comment.
You can’t perform that action at this time.