Skip to content

Commit

Permalink
Hololens - Trigger MediaSessionActions for play and pause buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
ferjm committed Dec 5, 2019
1 parent d633c8b commit d5200ba
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 22 deletions.
8 changes: 8 additions & 0 deletions ports/libsimpleservo/capi/src/lib.rs
Expand Up @@ -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 {
Expand Down
37 changes: 23 additions & 14 deletions support/hololens/ServoApp/BrowserPage.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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 &) {
Expand Down Expand Up @@ -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<int32_t>(servo::MediaSessionAction::PLAY));
}
void BrowserPage::OnMediaControlsPauseClicked(
Windows::Foundation::IInspectable const &,
Windows::UI::Xaml::RoutedEventArgs const &) {}
Windows::UI::Xaml::RoutedEventArgs const &) {
servoControl().SendMediaSessionAction(
static_cast<int32_t>(servo::MediaSessionAction::PAUSE));
}

} // namespace winrt::ServoApp::implementation
4 changes: 0 additions & 4 deletions support/hololens/ServoApp/BrowserPage.xaml
Expand Up @@ -140,10 +140,6 @@
<CommandBar Grid.Row="3" x:Name="mediaControls" Visibility="Collapsed">
<AppBarButton Icon="Play" Label="Play" x:Name="playButton" Visibility="Collapsed" Click="OnMediaControlsPlayClicked"/>
<AppBarButton Icon="Pause" Label="Pause" x:Name="pauseButton" Click="OnMediaControlsPauseClicked"/>

<CommandBar.Content>
<TextBlock Text="Now playing..." Margin="12,14"/>
</CommandBar.Content>
</CommandBar>
</Grid>

Expand Down
21 changes: 21 additions & 0 deletions 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
};
}
1 change: 1 addition & 0 deletions support/hololens/ServoApp/ServoApp.vcxproj
Expand Up @@ -122,6 +122,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="logs.h" />
<ClInclude Include="MediaSession.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="App.h">
<DependentUpon>App.xaml</DependentUpon>
Expand Down
3 changes: 3 additions & 0 deletions support/hololens/ServoApp/ServoApp.vcxproj.filters
Expand Up @@ -40,6 +40,9 @@
</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">
Expand Down
3 changes: 1 addition & 2 deletions support/hololens/ServoApp/ServoControl/Servo.cpp
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions support/hololens/ServoApp/ServoControl/Servo.h
Expand Up @@ -88,6 +88,9 @@ class Servo {
capi::resize(mWindowWidth, mWindowHeight);
}
}
void SendMediaSessionAction(int32_t action) {
capi::media_session_action(action);
}

private:
ServoDelegate &mDelegate;
Expand Down
4 changes: 4 additions & 0 deletions support/hololens/ServoApp/ServoControl/ServoControl.cpp
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions support/hololens/ServoApp/ServoControl/ServoControl.h
Expand Up @@ -3,6 +3,7 @@
#include "OpenGLES.h"
#include "Servo.h"
#include "DefaultUrl.h"
#include "MediaSession.h"

namespace winrt::ServoApp::implementation {
struct ServoControl : ServoControlT<ServoControl>, public servo::ServoDelegate {
Expand All @@ -15,6 +16,7 @@ struct ServoControl : ServoControlT<ServoControl>, public servo::ServoDelegate {
void Stop();
void Shutdown();
hstring LoadURIOrSearch(hstring);
void SendMediaSessionAction(int32_t);

void OnLoaded(IInspectable const &,
Windows::UI::Xaml::RoutedEventArgs const &);
Expand Down
5 changes: 3 additions & 2 deletions support/hololens/ServoApp/ServoControl/ServoControl.idl
Expand Up @@ -14,14 +14,15 @@ 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;
event EventDelegate OnCaptureGesturesEnded;
event HistoryChangedDelegate OnHistoryChanged;
event Windows.Foundation.EventHandler<String> OnTitleChanged;
event Windows.Foundation.EventHandler<String> OnURLChanged;
event MediaSessionMetadataDelegate OnMediaSessionMetadata;
event Windows.Foundation.EventHandler<int> OnMediaSessionPlaybackStateChange;
event MediaSessionMetadataDelegate OnMediaSessionMetadata;
event Windows.Foundation.EventHandler<int> OnMediaSessionPlaybackStateChange;
}
} // namespace ServoApp

0 comments on commit d5200ba

Please sign in to comment.