Skip to content

feat: Add mediasession support #8309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test handlers to incresase coverage
  • Loading branch information
mister-ben committed Jun 9, 2023
commit 662989b139a02c14cc63907b62ccaa51fe0221f7
6 changes: 3 additions & 3 deletions src/js/mediasession.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const initMediaSession = function() {
return;
}
const ms = window.navigator.mediaSession;
const skipTime = this.options_.mediaSession.skipTime || 15;
const defaultSkipTime = 15;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question, do you think it would be a good idea to use skipButtons values if they are available? This would allow a unified experience between the player and the mediaSession.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that makes sense, yes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about keeping this property as it is for now and adding this modification via another PR? Because it would be nice to have the titleBar values as well.


const actionHandlers = [
['play', () => {
Expand All @@ -28,13 +28,13 @@ export const initMediaSession = function() {
if (this.usingPlugin('ads') && this.ads.inAdBreak()) {
return;
}
this.currentTime(Math.max(0, this.currentTime() - (details.skipOffset || skipTime)));
this.currentTime(Math.max(0, this.currentTime() - (details.skipOffset || defaultSkipTime)));
}],
['seekforward', (details) => {
if (this.usingPlugin('ads') && this.ads.inAdBreak()) {
return;
}
this.currentTime(Math.min(this.duration(), this.currentTime() + (details.skipOffset || skipTime)));
this.currentTime(Math.min(this.duration(), this.currentTime() + (details.skipOffset || defaultSkipTime)));
}],
['seekto', (details) => {
if (this.usingPlugin('ads') && this.ads.inAdBreak()) {
Expand Down
125 changes: 124 additions & 1 deletion test/unit/mediasession.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ QUnit.test('allows for action handlers that are not settable', function(assert)

sinon.stub(this.player.log, 'debug');

this.player.trigger('pluginsetup:playlist');
this.player.trigger('playing');

this.clock.tick(10);

Expand Down Expand Up @@ -286,3 +286,126 @@ QUnit.test('playback and position state', function(assert) {

window.navigator.mediaSession.setPositionState.restore();
});

QUnit.test('action handlers', function(assert) {
const actions = {};

sinon.stub(window.navigator.mediaSession, 'setActionHandler').callsFake((action, handler) => {
actions[action] = handler;
});

this.clock = sinon.useFakeTimers();
this.player = sinon.spy(TestHelpers.makePlayer({
mediaSession: true
}));

this.player.trigger('playing');
this.clock.tick(10);

sinon.resetHistory();

actions.play();
assert.true(this.player.play.called, 'play handler calls play on player');

actions.pause();
assert.true(this.player.pause.called, 'pause handler calls play on player');

sinon.resetHistory();

assert.false(this.player.pause.called, 'pause handler should have been reset');

actions.stop();
assert.true(this.player.pause.called, 'pause handler calls play on player');
assert.true(this.player.currentTime.calledWith(0), 'pause handler calls play on player');

this.player.duration(600);
this.player.currentTime(0);

actions.seekforward({skipOffset: 10});
assert.true(this.player.currentTime.calledWith(10), 'seek handler sets current time to requested offset');
actions.seekforward({});
assert.true(this.player.currentTime.calledWith(10 + 15), 'seek handler sets current time to default offset');
actions.seekbackward({skipOffset: 10});
assert.true(this.player.currentTime.calledWith(10 + 15 - 10), 'seek handler sets current time to requested offset');
actions.seekbackward({});
assert.true(this.player.currentTime.calledWith(10 + 15 - 10 - 15), 'seek handler sets current time to default offset');
actions.seekbackward({});
assert.true(this.player.currentTime.calledWith(10 + 15 - 10 - 15), 'seek handler does not set current time below 0');

actions.seekto({seekTime: 10});
assert.true(this.player.currentTime.calledWith(10), 'seek handler sets current time');

window.navigator.mediaSession.setActionHandler.restore();
});

QUnit.test('seek action handlers ignored in ad breaks', function(assert) {
const actions = {};

sinon.stub(window.navigator.mediaSession, 'setActionHandler').callsFake((action, handler) => {
actions[action] = handler;
});

this.clock = sinon.useFakeTimers();
this.player = sinon.spy(TestHelpers.makePlayer({
mediaSession: true
}));

this.player.trigger('playing');

this.clock.tick(10);

// Mocked contrib-ads
this.player.activePlugins_ = {
ads: true
};
this.player.ads = {
inAdBreak: () => true
};

actions.seekto({seekTime: 15});
assert.false(this.player.currentTime.calledWith(15), 'seek handler does not sets current time in ad break');

actions.seekbackward({skipOffset: 10});
assert.false(this.player.currentTime.called, 'seekbackward handler does not sets current time in ad break');

actions.seekforward({skipOffset: 10});
assert.false(this.player.currentTime.called, 'seekforward handler does not sets current time in ad break');

window.navigator.mediaSession.setActionHandler.restore();
});

QUnit.test('playlist action handlers are called', function(assert) {
const actions = {};

sinon.stub(window.navigator.mediaSession, 'setActionHandler').callsFake((action, handler) => {
actions[action] = handler;
});

this.clock = sinon.useFakeTimers();
this.player = sinon.spy(TestHelpers.makePlayer({
mediaSession: true
}));

this.player.trigger('pluginsetup:playlist');

this.clock.tick(10);

// Mocked playlist
const mockPlaylist = {
next: sinon.spy(),
previous: sinon.spy()
};

this.player.activePlugins_ = {
playlist: true
};
this.player.playlist = mockPlaylist;

actions.previoustrack();
assert.true(mockPlaylist.previous.called, 'playlist previous handler called');

actions.nexttrack();
assert.true(mockPlaylist.next.called, 'playlist next handler called');

window.navigator.mediaSession.setActionHandler.restore();
});