2020-04-15 08:49:46 +02:00
|
|
|
import playbackManager from 'playbackManager';
|
|
|
|
import nowPlayingHelper from 'nowPlayingHelper';
|
|
|
|
import events from 'events';
|
|
|
|
import connectionManager from 'connectionManager';
|
|
|
|
/* eslint-disable indent */
|
2018-10-23 01:05:09 +03:00
|
|
|
|
2019-01-10 15:39:37 +03:00
|
|
|
// Reports media playback to the device for lock screen control
|
|
|
|
|
2020-04-15 08:49:46 +02:00
|
|
|
let currentPlayer;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-04-15 08:14:08 +02:00
|
|
|
function seriesImageUrl(item, options = {}, type = options.type || 'Primary') {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (item.Type !== 'Episode') {
|
|
|
|
return null;
|
2020-04-15 08:14:08 +02:00
|
|
|
} else if (type === 'Primary' && item.SeriesPrimaryImageTag) {
|
|
|
|
options.tag = item.SeriesPrimaryImageTag;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-04-15 08:14:08 +02:00
|
|
|
return connectionManager.getApiClient(item.ServerId).getScaledImageUrl(item.SeriesId, options);
|
|
|
|
} else if (type === 'Thumb') {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (item.SeriesThumbImageTag) {
|
|
|
|
options.tag = item.SeriesThumbImageTag;
|
|
|
|
|
|
|
|
return connectionManager.getApiClient(item.ServerId).getScaledImageUrl(item.SeriesId, options);
|
2020-04-15 08:14:08 +02:00
|
|
|
} else if (item.ParentThumbImageTag) {
|
2019-01-10 15:39:37 +03:00
|
|
|
options.tag = item.ParentThumbImageTag;
|
|
|
|
|
|
|
|
return connectionManager.getApiClient(item.ServerId).getScaledImageUrl(item.ParentThumbItemId, options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2020-04-15 08:14:08 +02:00
|
|
|
function imageUrl(item, options = {}, type = options.type || 'Primary') {
|
|
|
|
if (item.ImageTags && item.ImageTags[type]) {
|
|
|
|
options.tag = item.ImageTags[type];
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
return connectionManager.getApiClient(item.ServerId).getScaledImageUrl(item.Id, options);
|
2020-04-15 08:14:08 +02:00
|
|
|
} else if (item.AlbumId && item.AlbumPrimaryImageTag) {
|
2019-01-10 15:39:37 +03:00
|
|
|
options.tag = item.AlbumPrimaryImageTag;
|
2020-04-15 08:14:08 +02:00
|
|
|
|
2019-01-10 15:39:37 +03:00
|
|
|
return connectionManager.getApiClient(item.ServerId).getScaledImageUrl(item.AlbumId, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2020-04-15 07:54:33 +02:00
|
|
|
function pushImageUrl(item, imageOptions = {}) {
|
2020-04-15 08:49:46 +02:00
|
|
|
const url = seriesImageUrl(item, imageOptions) || imageUrl(item, imageOptions);
|
2019-03-17 00:31:56 +00:00
|
|
|
|
2019-01-10 15:39:37 +03:00
|
|
|
if (url) {
|
2020-04-15 08:49:46 +02:00
|
|
|
const height = imageOptions.height || imageOptions.maxHeight;
|
2019-03-17 00:31:56 +00:00
|
|
|
|
2020-04-15 07:54:33 +02:00
|
|
|
return {
|
2019-03-17 00:31:56 +00:00
|
|
|
src: url,
|
|
|
|
sizes: height + 'x' + height
|
2020-04-15 07:54:33 +02:00
|
|
|
};
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2020-04-15 08:14:08 +02:00
|
|
|
function getImageUrls(item, imageSizes = [96, 128, 192, 256, 384, 512]) {
|
2020-04-15 08:49:46 +02:00
|
|
|
const list = [];
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-04-15 07:54:33 +02:00
|
|
|
imageSizes.forEach((size) => {
|
|
|
|
list.push(pushImageUrl(item, {height: size}));
|
|
|
|
});
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
return list;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function updatePlayerState(player, state, eventName) {
|
2020-01-26 14:40:32 +01:00
|
|
|
// Don't go crazy reporting position changes
|
2020-04-15 08:49:46 +02:00
|
|
|
if (eventName === 'timeupdate') {
|
2020-01-26 14:40:32 +01:00
|
|
|
// Only report if this item hasn't been reported yet, or if there's an actual playback change.
|
|
|
|
// Don't report on simple time updates
|
|
|
|
return;
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-04-15 08:49:46 +02:00
|
|
|
const item = state.NowPlayingItem;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (!item) {
|
|
|
|
hideMediaControls();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-15 08:49:46 +02:00
|
|
|
if (eventName === 'init') { // transform "init" event into "timeupdate" to restraint update rate
|
2019-03-17 00:31:56 +00:00
|
|
|
eventName = 'timeupdate';
|
|
|
|
}
|
|
|
|
|
2020-04-15 08:49:46 +02:00
|
|
|
const isVideo = item.MediaType === 'Video';
|
|
|
|
const isLocalPlayer = player.isLocalPlayer || false;
|
2019-03-17 00:31:56 +00:00
|
|
|
|
|
|
|
// Local players do their own notifications
|
|
|
|
if (isLocalPlayer && isVideo) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-15 08:49:46 +02:00
|
|
|
const playState = state.PlayState || {};
|
|
|
|
const parts = nowPlayingHelper.getNowPlayingNames(item);
|
|
|
|
const artist = parts[parts.length - 1].text;
|
|
|
|
const title = parts.length === 1 ? '' : parts[0].text;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-04-15 08:49:46 +02:00
|
|
|
const album = item.Album || '';
|
|
|
|
const itemId = item.Id;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
// Convert to ms
|
2020-04-15 08:49:46 +02:00
|
|
|
const duration = parseInt(item.RunTimeTicks ? (item.RunTimeTicks / 10000) : 0);
|
|
|
|
const currentTime = parseInt(playState.PositionTicks ? (playState.PositionTicks / 10000) : 0);
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-04-15 08:49:46 +02:00
|
|
|
const isPaused = playState.IsPaused || false;
|
|
|
|
const canSeek = playState.CanSeek || false;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-04-15 07:54:33 +02:00
|
|
|
if ('mediaSession' in navigator) {
|
2019-03-17 00:31:56 +00:00
|
|
|
navigator.mediaSession.metadata = new MediaMetadata({
|
2020-01-26 14:40:32 +01:00
|
|
|
title: title,
|
|
|
|
artist: artist,
|
2019-03-17 00:31:56 +00:00
|
|
|
album: album,
|
2020-04-15 07:54:33 +02:00
|
|
|
artwork: getImageUrls(item)
|
2019-03-17 00:31:56 +00:00
|
|
|
});
|
|
|
|
} else {
|
2020-04-15 08:49:46 +02:00
|
|
|
let imageUrl = [];
|
2020-04-15 07:54:33 +02:00
|
|
|
imageUrl.push(pushImageUrl(item));
|
2019-03-17 00:31:56 +00:00
|
|
|
|
|
|
|
if (imageUrl.length) {
|
|
|
|
imageUrl = imageUrl[0].src;
|
|
|
|
} else {
|
|
|
|
imageUrl = null;
|
|
|
|
}
|
2019-03-21 21:54:36 +00:00
|
|
|
|
2019-03-17 00:31:56 +00:00
|
|
|
window.NativeShell.updateMediaSession({
|
|
|
|
action: eventName,
|
|
|
|
isLocalPlayer: isLocalPlayer,
|
|
|
|
itemId: itemId,
|
2020-01-26 14:40:32 +01:00
|
|
|
title: title,
|
|
|
|
artist: artist,
|
2019-03-17 00:31:56 +00:00
|
|
|
album: album,
|
|
|
|
duration: duration,
|
|
|
|
position: currentTime,
|
|
|
|
imageUrl: imageUrl,
|
|
|
|
canSeek: canSeek,
|
|
|
|
isPaused: isPaused
|
|
|
|
});
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function onGeneralEvent(e) {
|
2020-04-15 08:49:46 +02:00
|
|
|
const state = playbackManager.getPlayerState(this);
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-04-15 08:14:08 +02:00
|
|
|
updatePlayerState(this, state, e.type);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function onStateChanged(e, state) {
|
2020-04-15 08:14:08 +02:00
|
|
|
updatePlayerState(this, state, 'statechange');
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function onPlaybackStart(e, state) {
|
2020-04-15 08:14:08 +02:00
|
|
|
updatePlayerState(this, state, e.type);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2020-04-15 08:14:08 +02:00
|
|
|
function onPlaybackStopped() {
|
2019-01-10 15:39:37 +03:00
|
|
|
hideMediaControls();
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function releaseCurrentPlayer() {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (currentPlayer) {
|
|
|
|
events.off(currentPlayer, 'playbackstart', onPlaybackStart);
|
|
|
|
events.off(currentPlayer, 'playbackstop', onPlaybackStopped);
|
|
|
|
events.off(currentPlayer, 'unpause', onGeneralEvent);
|
|
|
|
events.off(currentPlayer, 'pause', onGeneralEvent);
|
|
|
|
events.off(currentPlayer, 'statechange', onStateChanged);
|
|
|
|
events.off(currentPlayer, 'timeupdate', onGeneralEvent);
|
|
|
|
|
|
|
|
currentPlayer = null;
|
|
|
|
|
|
|
|
hideMediaControls();
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function hideMediaControls() {
|
2019-03-17 00:31:56 +00:00
|
|
|
if (navigator.mediaSession) {
|
|
|
|
navigator.mediaSession.metadata = null;
|
|
|
|
} else {
|
|
|
|
window.NativeShell.hideMediaSession();
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function bindToPlayer(player) {
|
2019-01-10 15:39:37 +03:00
|
|
|
releaseCurrentPlayer();
|
|
|
|
|
|
|
|
if (!player) {
|
|
|
|
return;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
currentPlayer = player;
|
|
|
|
|
2020-04-15 08:49:46 +02:00
|
|
|
const state = playbackManager.getPlayerState(player);
|
2019-01-10 15:39:37 +03:00
|
|
|
updatePlayerState(player, state, 'init');
|
|
|
|
|
|
|
|
events.on(currentPlayer, 'playbackstart', onPlaybackStart);
|
|
|
|
events.on(currentPlayer, 'playbackstop', onPlaybackStopped);
|
|
|
|
events.on(currentPlayer, 'unpause', onGeneralEvent);
|
|
|
|
events.on(currentPlayer, 'pause', onGeneralEvent);
|
|
|
|
events.on(currentPlayer, 'statechange', onStateChanged);
|
|
|
|
events.on(currentPlayer, 'timeupdate', onGeneralEvent);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2019-05-05 23:55:42 +02:00
|
|
|
function execute(name) {
|
|
|
|
playbackManager[name](currentPlayer);
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-04-15 08:14:08 +02:00
|
|
|
if (navigator.mediaSession) {
|
2019-03-17 00:31:56 +00:00
|
|
|
navigator.mediaSession.setActionHandler('previoustrack', function () {
|
|
|
|
execute('previousTrack');
|
|
|
|
});
|
2019-03-21 21:54:36 +00:00
|
|
|
|
2019-03-17 00:31:56 +00:00
|
|
|
navigator.mediaSession.setActionHandler('nexttrack', function () {
|
|
|
|
execute('nextTrack');
|
|
|
|
});
|
2019-03-21 21:54:36 +00:00
|
|
|
|
2019-03-17 00:31:56 +00:00
|
|
|
navigator.mediaSession.setActionHandler('play', function () {
|
|
|
|
execute('unpause');
|
|
|
|
});
|
2019-03-21 21:54:36 +00:00
|
|
|
|
2019-03-17 00:31:56 +00:00
|
|
|
navigator.mediaSession.setActionHandler('pause', function () {
|
|
|
|
execute('pause');
|
|
|
|
});
|
2019-03-21 21:54:36 +00:00
|
|
|
|
2019-03-17 00:31:56 +00:00
|
|
|
navigator.mediaSession.setActionHandler('seekbackward', function () {
|
|
|
|
execute('rewind');
|
|
|
|
});
|
2019-03-21 21:54:36 +00:00
|
|
|
|
2019-03-17 00:31:56 +00:00
|
|
|
navigator.mediaSession.setActionHandler('seekforward', function () {
|
|
|
|
execute('fastForward');
|
|
|
|
});
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
events.on(playbackManager, 'playerchange', function () {
|
|
|
|
bindToPlayer(playbackManager.getCurrentPlayer());
|
|
|
|
});
|
|
|
|
|
|
|
|
bindToPlayer(playbackManager.getCurrentPlayer());
|
2020-04-15 08:49:46 +02:00
|
|
|
|
|
|
|
/* eslint-enable indent */
|