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
more tests
  • Loading branch information
mister-ben committed Jun 3, 2023
commit b67a9ee9873e4ce1e5c3a96a7422875f6ca70d7a
42 changes: 31 additions & 11 deletions src/js/mediasession.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import window from 'global/window';
import {getMimetype} from './utils/mimetypes';

/**
*
* Sets up media session if supported and configured
*
* @this { import('./player').default }
* @method initMediaSession Sets up media session if supported and configured
* @this { import('./player').default } Player
*/
export const initMediaSession = function() {
if (!this.options_.mediaSession || !('mediaSession' in window.navigator)) {
Expand All @@ -25,6 +23,7 @@ export const initMediaSession = function() {
this.pause();
this.currentTime(0);
}],
// videojs-contrib-ads
['seekbackward', (details) => {
if (this.usingPlugin('ads') && this.ads.inAdBreak()) {
return;
Expand All @@ -45,6 +44,9 @@ export const initMediaSession = function() {
}]
];

// Using Googles' recommendation that expects some handler may not be settable, especially as we
// want to support older Chrome
// https://web.dev/media-session/#let-users-control-whats-playing
for (const [action, handler] of actionHandlers) {
try {
ms.setActionHandler(action, handler);
Expand Down Expand Up @@ -74,23 +76,33 @@ export const initMediaSession = function() {
}

/**
*
* Updates the mediaSession metadata. Fires `updatemediasession` as an
* opportunity to modify the metadata
*
* @fires Player#updatemediasession
*/
const updateMediaSession = () => {
this.log('updatems');
const currentMedia = this.getMedia();
const playlistItem = this.usingPlugin('playlist') ? Object.assign({}, this.playlist()[this.playlist.currentItem()]) : {};
const mediaSessionData = {
artwork: currentMedia.artwork || playlistItem.artwork || this.poster() ? [{
src: this.poster(),
type: getMimetype(this.poster())
}] : [],
title: currentMedia.title || playlistItem.name || '',
artist: currentMedia.artist || playlistItem.artist || '',
album: currentMedia.album || playlistItem.album || ''
};

// This allows the metadata to be updated before being set, e.g. if loadmedia() is not used.
if (currentMedia.artwork) {
mediaSessionData.artwork = currentMedia.artwork;
} else if (playlistItem.artwork) {
mediaSessionData.artwork = playlistItem.artwork;
} else if (this.poster()) {
mediaSessionData.artwork = {
src: this.poster(),
type: getMimetype(this.poster())
};
}

// This allows the metadata to be updated before being set, e.g. if loadMedia() is not used.
this.trigger('updatemediasession', mediaSessionData);

ms.metadata = new window.MediaMetadata(mediaSessionData);
Expand All @@ -108,7 +120,15 @@ export const initMediaSession = function() {
}
};

this.on('playing', updateMediaSession);
this.on('playing', () => {
updateMediaSession();
ms.playbackState = 'playing';
});

this.on(['paused', 'paused'], () => {
updateMediaSession();
ms.playbackState = 'playing';
});

if ('setPositionState' in ms) {
this.on(['playing', 'seeked', 'ratechange'], updatePositionState);
Expand Down
33 changes: 32 additions & 1 deletion test/unit/player-mediasession.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ QUnit.test('mediasession data set', function(assert) {
this.clock.restore();
});

QUnit.test('mediasession can be customised befire being set', function(assert) {
QUnit.test('mediasession can be customised before being set', function(assert) {
assert.expect(3);

this.clock = sinon.useFakeTimers();
Expand Down Expand Up @@ -172,3 +172,34 @@ QUnit.test('mediasession can be customised befire being set', function(assert) {
this.clock.restore();
});

QUnit.test('action handlers set up', function(assert) {
const spy = sinon.spy(window.navigator.mediaSession, 'setActionHandler');

this.player = TestHelpers.makePlayer({
mediaSession: true
});

assert.true(spy.calledWith('play'), 'play handler set');
assert.false(spy.calledWith('previoustrack'), 'playlist handler not set');

spy.restore();
});

QUnit.test('playlist action handlers set up', function(assert) {
const spy = sinon.spy(window.navigator.mediaSession, 'setActionHandler');

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

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

this.clock.tick(10);

assert.true(spy.calledWith('play'), 'play handler set');
assert.true(spy.calledWith('previoustrack'), 'playlist handler set');

spy.restore();
this.clock.restore();
});