From d5200ba453a5c70780498a894b1194a6398027a7 Mon Sep 17 00:00:00 2001 From: Fernando Jimenez Moreno Date: Fri, 29 Nov 2019 12:44:26 +0100 Subject: [PATCH] Hololens - Trigger MediaSessionActions for play and pause buttons --- ports/libsimpleservo/capi/src/lib.rs | 8 ++++ support/hololens/ServoApp/BrowserPage.cpp | 37 ++++++++++++------- support/hololens/ServoApp/BrowserPage.xaml | 4 -- support/hololens/ServoApp/MediaSession.h | 21 +++++++++++ support/hololens/ServoApp/ServoApp.vcxproj | 1 + .../ServoApp/ServoApp.vcxproj.filters | 3 ++ .../hololens/ServoApp/ServoControl/Servo.cpp | 3 +- .../hololens/ServoApp/ServoControl/Servo.h | 3 ++ .../ServoApp/ServoControl/ServoControl.cpp | 4 ++ .../ServoApp/ServoControl/ServoControl.h | 2 + .../ServoApp/ServoControl/ServoControl.idl | 5 ++- 11 files changed, 69 insertions(+), 22 deletions(-) create mode 100644 support/hololens/ServoApp/MediaSession.h diff --git a/ports/libsimpleservo/capi/src/lib.rs b/ports/libsimpleservo/capi/src/lib.rs index 8ca727213a39..26c2046df88b 100644 --- a/ports/libsimpleservo/capi/src/lib.rs +++ b/ports/libsimpleservo/capi/src/lib.rs @@ -607,6 +607,14 @@ pub extern "C" fn click(x: f32, y: f32) { }); } +#[no_mangle] +pub extern "C" fn media_session_action(action: i32) { + catch_any_panic(|| { + debug!("media_session_action"); + call(|s| s.media_session_action(action)); + }); +} + pub struct WakeupCallback(extern "C" fn()); impl WakeupCallback { diff --git a/support/hololens/ServoApp/BrowserPage.cpp b/support/hololens/ServoApp/BrowserPage.cpp index 536f1f378b1f..695a62555da3 100644 --- a/support/hololens/ServoApp/BrowserPage.cpp +++ b/support/hololens/ServoApp/BrowserPage.cpp @@ -7,6 +7,7 @@ #include "BrowserPage.h" #include "BrowserPage.g.cpp" #include "DefaultUrl.h" +#include "MediaSession.h" using namespace std::placeholders; using namespace winrt::Windows::Foundation; @@ -72,18 +73,20 @@ void BrowserPage::BindServoEvents() { urlTextbox().GotFocus(std::bind(&BrowserPage::OnURLFocused, this, _1)); servoControl().OnMediaSessionMetadata( [=](hstring title, hstring artist, hstring album) {}); - servoControl().OnMediaSessionPlaybackStateChange([=](const auto &, - int state) { - if (state == 1 /* none */) { - mediaControls().Visibility(Visibility::Collapsed); - return; - } - mediaControls().Visibility(Visibility::Visible); - playButton().Visibility(state == 3 /* paused */ ? Visibility::Visible - : Visibility::Collapsed); - pauseButton().Visibility(state == 3 /* paused */ ? Visibility::Collapsed - : Visibility::Visible); - }); + servoControl().OnMediaSessionPlaybackStateChange( + [=](const auto &, int state) { + if (state == servo::PlaybackState::NONE) { + mediaControls().Visibility(Visibility::Collapsed); + return; + } + mediaControls().Visibility(Visibility::Visible); + playButton().Visibility(state == servo::PlaybackState::PAUSED + ? Visibility::Visible + : Visibility::Collapsed); + pauseButton().Visibility(state == servo::PlaybackState::PAUSED + ? Visibility::Collapsed + : Visibility::Visible); + }); } void BrowserPage::OnURLFocused(Windows::Foundation::IInspectable const &) { @@ -159,9 +162,15 @@ void BrowserPage::OnURLEdited(IInspectable const &, void BrowserPage::OnMediaControlsPlayClicked( Windows::Foundation::IInspectable const &, - Windows::UI::Xaml::RoutedEventArgs const &) {} + Windows::UI::Xaml::RoutedEventArgs const &) { + servoControl().SendMediaSessionAction( + static_cast(servo::MediaSessionAction::PLAY)); +} void BrowserPage::OnMediaControlsPauseClicked( Windows::Foundation::IInspectable const &, - Windows::UI::Xaml::RoutedEventArgs const &) {} + Windows::UI::Xaml::RoutedEventArgs const &) { + servoControl().SendMediaSessionAction( + static_cast(servo::MediaSessionAction::PAUSE)); +} } // namespace winrt::ServoApp::implementation diff --git a/support/hololens/ServoApp/BrowserPage.xaml b/support/hololens/ServoApp/BrowserPage.xaml index 727e73e11ccd..f735e0ab993c 100644 --- a/support/hololens/ServoApp/BrowserPage.xaml +++ b/support/hololens/ServoApp/BrowserPage.xaml @@ -140,10 +140,6 @@ - - - - diff --git a/support/hololens/ServoApp/MediaSession.h b/support/hololens/ServoApp/MediaSession.h new file mode 100644 index 000000000000..78deae854241 --- /dev/null +++ b/support/hololens/ServoApp/MediaSession.h @@ -0,0 +1,21 @@ +#pragma once + +namespace winrt::servo { + enum PlaybackState { + NONE = 1, + PLAYING, + PAUSED + }; + + enum MediaSessionAction { + PLAY = 1, + PAUSE, + SEEK_BACKWARD, + SEEK_FORWARD, + PREVIOUS_TRACK, + NEXT_TRACK, + SKIP_AD, + STOP, + SEEK_TO + }; +} \ No newline at end of file diff --git a/support/hololens/ServoApp/ServoApp.vcxproj b/support/hololens/ServoApp/ServoApp.vcxproj index f752011dff0e..9a21cb89afef 100644 --- a/support/hololens/ServoApp/ServoApp.vcxproj +++ b/support/hololens/ServoApp/ServoApp.vcxproj @@ -122,6 +122,7 @@ + App.xaml diff --git a/support/hololens/ServoApp/ServoApp.vcxproj.filters b/support/hololens/ServoApp/ServoApp.vcxproj.filters index 3cf5c9555d51..367a42238764 100644 --- a/support/hololens/ServoApp/ServoApp.vcxproj.filters +++ b/support/hololens/ServoApp/ServoApp.vcxproj.filters @@ -40,6 +40,9 @@ + + ServoControl + diff --git a/support/hololens/ServoApp/ServoControl/Servo.cpp b/support/hololens/ServoApp/ServoControl/Servo.cpp index afe3539efe93..d3e13c5b1d99 100644 --- a/support/hololens/ServoApp/ServoControl/Servo.cpp +++ b/support/hololens/ServoApp/ServoControl/Servo.cpp @@ -73,8 +73,7 @@ Servo::Servo(hstring url, hstring args, GLsizei width, GLsizei height, capi::CInitOptions o; hstring defaultPrefs = L" --pref dom.webxr.enabled"; o.args = *hstring2char(args + defaultPrefs); - o.url = - "https://ferjm.github.io/web-api-tests/video/mp4.html"; //*hstring2char(url); + o.url = *hstring2char(url); o.width = mWindowWidth; o.height = mWindowHeight; o.density = dpi; diff --git a/support/hololens/ServoApp/ServoControl/Servo.h b/support/hololens/ServoApp/ServoControl/Servo.h index 2e76256f021a..e21bd4cafbd1 100644 --- a/support/hololens/ServoApp/ServoControl/Servo.h +++ b/support/hololens/ServoApp/ServoControl/Servo.h @@ -88,6 +88,9 @@ class Servo { capi::resize(mWindowWidth, mWindowHeight); } } + void SendMediaSessionAction(int32_t action) { + capi::media_session_action(action); + } private: ServoDelegate &mDelegate; diff --git a/support/hololens/ServoApp/ServoControl/ServoControl.cpp b/support/hololens/ServoApp/ServoControl/ServoControl.cpp index d1d19118a290..032e36422768 100644 --- a/support/hololens/ServoApp/ServoControl/ServoControl.cpp +++ b/support/hololens/ServoApp/ServoControl/ServoControl.cpp @@ -271,6 +271,10 @@ hstring ServoControl::LoadURIOrSearch(hstring input) { return searchUri; } +void ServoControl::SendMediaSessionAction(int32_t action) { + RunOnGLThread([=] { mServo->SendMediaSessionAction(action); }); +} + void ServoControl::TryLoadUri(hstring input) { if (!mLooping) { mInitialURL = input; diff --git a/support/hololens/ServoApp/ServoControl/ServoControl.h b/support/hololens/ServoApp/ServoControl/ServoControl.h index 5daa227f1269..a7c2a1f9698f 100644 --- a/support/hololens/ServoApp/ServoControl/ServoControl.h +++ b/support/hololens/ServoApp/ServoControl/ServoControl.h @@ -3,6 +3,7 @@ #include "OpenGLES.h" #include "Servo.h" #include "DefaultUrl.h" +#include "MediaSession.h" namespace winrt::ServoApp::implementation { struct ServoControl : ServoControlT, public servo::ServoDelegate { @@ -15,6 +16,7 @@ struct ServoControl : ServoControlT, public servo::ServoDelegate { void Stop(); void Shutdown(); hstring LoadURIOrSearch(hstring); + void SendMediaSessionAction(int32_t); void OnLoaded(IInspectable const &, Windows::UI::Xaml::RoutedEventArgs const &); diff --git a/support/hololens/ServoApp/ServoControl/ServoControl.idl b/support/hololens/ServoApp/ServoControl/ServoControl.idl index 3a5698e16017..90a5fae8fe36 100644 --- a/support/hololens/ServoApp/ServoControl/ServoControl.idl +++ b/support/hololens/ServoApp/ServoControl/ServoControl.idl @@ -14,6 +14,7 @@ namespace ServoApp { void SetTransientMode(Boolean transient); void SetArgs(String args); void Shutdown(); + void SendMediaSessionAction(UInt32 action); event EventDelegate OnLoadStarted; event EventDelegate OnLoadEnded; event EventDelegate OnCaptureGesturesStarted; @@ -21,7 +22,7 @@ namespace ServoApp { event HistoryChangedDelegate OnHistoryChanged; event Windows.Foundation.EventHandler OnTitleChanged; event Windows.Foundation.EventHandler OnURLChanged; - event MediaSessionMetadataDelegate OnMediaSessionMetadata; - event Windows.Foundation.EventHandler OnMediaSessionPlaybackStateChange; + event MediaSessionMetadataDelegate OnMediaSessionMetadata; + event Windows.Foundation.EventHandler OnMediaSessionPlaybackStateChange; } } // namespace ServoApp