jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/sessionplayer.js

535 lines
14 KiB
JavaScript
Raw Normal View History

2017-01-09 13:27:39 -07:00
define(['playbackManager', 'events', 'serverNotifications', 'connectionManager'], function (playbackManager, events, serverNotifications, connectionManager) {
2017-01-01 15:34:44 -07:00
'use strict';
2017-01-07 22:52:51 -07:00
function getActivePlayerId() {
var info = playbackManager.getPlayerInfo();
return info ? info.id : null;
}
2017-01-09 13:27:39 -07:00
function sendPlayCommand(apiClient, options, playType) {
2017-01-01 15:34:44 -07:00
2017-01-07 22:52:51 -07:00
var sessionId = getActivePlayerId();
2017-01-01 15:34:44 -07:00
var ids = options.ids || options.items.map(function (i) {
return i.Id;
});
var remoteOptions = {
ItemIds: ids.join(','),
PlayCommand: playType
};
if (options.startPositionTicks) {
remoteOptions.startPositionTicks = options.startPositionTicks;
}
2017-01-09 13:27:39 -07:00
return apiClient.sendPlayCommand(sessionId, remoteOptions);
2017-01-01 15:34:44 -07:00
}
2017-01-09 13:27:39 -07:00
function sendPlayStateCommand(apiClient, command, options) {
2017-01-01 15:34:44 -07:00
2017-01-07 22:52:51 -07:00
var sessionId = getActivePlayerId();
2017-01-01 15:34:44 -07:00
2017-01-09 13:27:39 -07:00
apiClient.sendPlayStateCommand(sessionId, command, options);
2017-01-01 15:34:44 -07:00
}
2017-01-07 22:52:51 -07:00
return function () {
2017-01-01 15:34:44 -07:00
var self = this;
self.name = 'Remote Control';
self.type = 'mediaplayer';
self.isLocalPlayer = false;
self.id = 'remoteplayer';
2017-01-09 13:27:39 -07:00
var currentServerId;
function getCurrentApiClient() {
if (currentServerId) {
return connectionManager.getApiClient(currentServerId);
}
return connectionManager.currentApiClient();
}
2017-01-01 15:34:44 -07:00
function sendCommandByName(name, options) {
var command = {
Name: name
};
if (options) {
command.Arguments = options;
}
self.sendCommand(command);
}
self.sendCommand = function (command) {
2017-01-07 22:52:51 -07:00
var sessionId = getActivePlayerId();
2017-01-01 15:34:44 -07:00
2017-01-09 13:27:39 -07:00
var apiClient = getCurrentApiClient();
apiClient.sendCommand(sessionId, command);
2017-01-01 15:34:44 -07:00
};
self.play = function (options) {
2017-01-06 12:37:57 -07:00
var playOptions = {};
2017-01-07 22:52:51 -07:00
playOptions.ids = options.ids || options.items.map(function (i) {
2017-01-06 12:37:57 -07:00
return i.Id;
});
if (options.startPositionTicks) {
playOptions.startPositionTicks = options.startPositionTicks;
}
2017-01-09 13:27:39 -07:00
return sendPlayCommand(getCurrentApiClient(), playOptions, 'PlayNow');
2017-01-01 15:34:44 -07:00
};
2017-01-06 12:37:57 -07:00
self.shuffle = function (item) {
2017-01-01 15:34:44 -07:00
2017-01-09 13:27:39 -07:00
sendPlayCommand(getCurrentApiClient(), { ids: [item.Id] }, 'PlayShuffle');
2017-01-01 15:34:44 -07:00
};
2017-01-06 12:37:57 -07:00
self.instantMix = function (item) {
2017-01-01 15:34:44 -07:00
2017-01-09 13:27:39 -07:00
sendPlayCommand(getCurrentApiClient(), { ids: [item.Id] }, 'PlayInstantMix');
2017-01-01 15:34:44 -07:00
};
self.queue = function (options) {
2017-01-09 13:27:39 -07:00
sendPlayCommand(getCurrentApiClient(), options, 'PlayNext');
2017-01-01 15:34:44 -07:00
};
self.queueNext = function (options) {
2017-01-09 13:27:39 -07:00
sendPlayCommand(getCurrentApiClient(), options, 'PlayLast');
2017-01-01 15:34:44 -07:00
};
2017-01-05 23:29:35 -07:00
self.canPlayMediaType = function (mediaType) {
mediaType = (mediaType || '').toLowerCase();
return mediaType === 'audio' || mediaType === 'video';
};
2017-01-01 15:34:44 -07:00
2017-01-05 23:29:35 -07:00
self.canQueueMediaType = function (mediaType) {
return self.canPlayMediaType(mediaType);
2017-01-01 15:34:44 -07:00
};
self.stop = function () {
2017-01-09 13:27:39 -07:00
sendPlayStateCommand(getCurrentApiClient(), 'stop');
2017-01-01 15:34:44 -07:00
};
self.nextTrack = function () {
2017-01-09 13:27:39 -07:00
sendPlayStateCommand(getCurrentApiClient(), 'nextTrack');
2017-01-01 15:34:44 -07:00
};
self.previousTrack = function () {
2017-01-09 13:27:39 -07:00
sendPlayStateCommand(getCurrentApiClient(), 'previousTrack');
2017-01-01 15:34:44 -07:00
};
self.seek = function (positionTicks) {
2017-01-09 13:27:39 -07:00
sendPlayStateCommand(getCurrentApiClient(), 'seek',
2017-01-07 22:52:51 -07:00
{
SeekPositionTicks: positionTicks
});
2017-01-01 15:34:44 -07:00
};
2017-01-05 13:18:09 -07:00
self.currentTime = function (val) {
if (val != null) {
return self.seek(val);
}
var state = self.lastPlayerData || {};
state = state.PlayState || {};
return state.PositionTicks;
};
self.duration = function () {
2017-01-09 13:27:39 -07:00
var state = self.lastPlayerData || {};
state = state.NowPlayingItem || {};
return state.RunTimeTicks;
2017-01-05 13:18:09 -07:00
};
self.paused = function () {
2017-01-09 13:27:39 -07:00
var state = self.lastPlayerData || {};
state = state.PlayState || {};
return state.IsPaused;
};
self.getVolume = function () {
var state = self.lastPlayerData || {};
state = state.PlayState || {};
return state.VolumeLevel;
2017-01-05 13:18:09 -07:00
};
2017-01-01 15:34:44 -07:00
self.pause = function () {
2017-01-09 13:27:39 -07:00
sendPlayStateCommand(getCurrentApiClient(), 'Pause');
2017-01-01 15:34:44 -07:00
};
self.unpause = function () {
2017-01-09 13:27:39 -07:00
sendPlayStateCommand(getCurrentApiClient(), 'Unpause');
2017-01-01 15:34:44 -07:00
};
self.setMute = function (isMuted) {
if (isMuted) {
sendCommandByName('Mute');
} else {
sendCommandByName('Unmute');
}
};
self.toggleMute = function () {
sendCommandByName('ToggleMute');
};
self.setVolume = function (vol) {
sendCommandByName('SetVolume', {
Volume: vol
});
};
self.volumeUp = function () {
sendCommandByName('VolumeUp');
};
self.volumeDown = function () {
sendCommandByName('VolumeDown');
};
self.toggleFullscreen = function () {
sendCommandByName('ToggleFullscreen');
};
2017-01-03 12:37:10 -07:00
self.audioTracks = function () {
return [];
};
2017-01-07 22:52:51 -07:00
self.getAudioStreamIndex = function () {
2017-01-05 13:18:09 -07:00
};
2017-01-01 15:34:44 -07:00
self.setAudioStreamIndex = function (index) {
sendCommandByName('SetAudioStreamIndex', {
Index: index
});
};
2017-01-03 12:37:10 -07:00
self.subtitleTracks = function () {
return [];
};
2017-01-05 13:18:09 -07:00
self.getSubtitleStreamIndex = function () {
};
2017-01-01 15:34:44 -07:00
self.setSubtitleStreamIndex = function (index) {
sendCommandByName('SetSubtitleStreamIndex', {
Index: index
});
};
2017-01-05 13:18:09 -07:00
self.getMaxStreamingBitrate = function () {
};
self.setMaxStreamingBitrate = function (bitrate) {
};
self.isFullscreen = function () {
};
self.toggleFullscreen = function () {
};
self.getRepeatMode = function () {
};
2017-01-01 15:34:44 -07:00
self.setRepeatMode = function (mode) {
sendCommandByName('SetRepeatMode', {
RepeatMode: mode
});
};
self.displayContent = function (options) {
sendCommandByName('DisplayContent', options);
};
2017-01-05 23:29:35 -07:00
self.isPlaying = function () {
var state = self.lastPlayerData || {};
return state.NowPlayingItem != null;
};
self.isPlayingVideo = function () {
var state = self.lastPlayerData || {};
state = state.NowPlayingItem || {};
return state.MediaType === 'Video';
};
self.isPlayingAudio = function () {
var state = self.lastPlayerData || {};
state = state.NowPlayingItem || {};
return state.MediaType === 'Audio';
};
2017-01-18 06:33:18 -07:00
self.getPlaylist = function () {
return Promise.resolve([]);
};
2017-01-17 23:06:06 -07:00
self.getCurrentPlaylistIndex = function () {
return 0;
};
2017-01-18 06:33:18 -07:00
self.setCurrentPlaylistItem = function (playlistItemId) {
2017-01-17 23:06:06 -07:00
return Promise.resolve();
};
2017-01-18 06:33:18 -07:00
self.removeFromPlaylist = function (playlistItemIds) {
2017-01-17 23:06:06 -07:00
return Promise.resolve();
};
2017-01-01 15:34:44 -07:00
self.getPlayerState = function () {
2017-01-09 13:27:39 -07:00
var apiClient = getCurrentApiClient();
2017-01-01 15:34:44 -07:00
if (apiClient) {
return apiClient.getSessions().then(function (sessions) {
2017-01-07 22:52:51 -07:00
var currentTargetId = getActivePlayerId();
2017-01-01 15:34:44 -07:00
// Update existing data
//updateSessionInfo(popup, msg.Data);
var session = sessions.filter(function (s) {
2017-01-02 12:43:31 -07:00
return s.Id === currentTargetId;
2017-01-01 15:34:44 -07:00
})[0];
if (session) {
session = getPlayerState(session);
}
return session;
});
} else {
return Promise.resolve({});
}
};
var pollInterval;
function onPollIntervalFired() {
2017-01-09 13:27:39 -07:00
var apiClient = getCurrentApiClient();
if (!apiClient.isWebSocketOpen()) {
2017-01-01 15:34:44 -07:00
if (apiClient) {
2017-01-09 13:27:39 -07:00
apiClient.getSessions().then(function (sessions) {
processUpdatedSessions(sessions, apiClient);
});
2017-01-01 15:34:44 -07:00
}
}
}
self.subscribeToPlayerUpdates = function () {
self.isUpdating = true;
2017-01-09 13:27:39 -07:00
var apiClient = getCurrentApiClient();
if (apiClient.isWebSocketOpen()) {
2017-01-01 15:34:44 -07:00
2017-01-09 13:27:39 -07:00
apiClient.sendWebSocketMessage("SessionsStart", "100,800");
2017-01-01 15:34:44 -07:00
}
if (pollInterval) {
clearInterval(pollInterval);
pollInterval = null;
}
pollInterval = setInterval(onPollIntervalFired, 5000);
};
function unsubscribeFromPlayerUpdates() {
self.isUpdating = true;
2017-01-09 13:27:39 -07:00
var apiClient = getCurrentApiClient();
if (apiClient.isWebSocketOpen()) {
2017-01-01 15:34:44 -07:00
2017-01-09 13:27:39 -07:00
apiClient.sendWebSocketMessage("SessionsStop");
2017-01-01 15:34:44 -07:00
}
if (pollInterval) {
clearInterval(pollInterval);
pollInterval = null;
}
}
var playerListenerCount = 0;
self.beginPlayerUpdates = function () {
if (playerListenerCount <= 0) {
playerListenerCount = 0;
self.subscribeToPlayerUpdates();
}
playerListenerCount++;
};
self.endPlayerUpdates = function () {
playerListenerCount--;
if (playerListenerCount <= 0) {
unsubscribeFromPlayerUpdates();
playerListenerCount = 0;
}
};
self.getTargets = function () {
2017-01-09 13:27:39 -07:00
var apiClient = getCurrentApiClient();
2017-01-01 15:34:44 -07:00
var sessionQuery = {
ControllableByUserId: apiClient.getCurrentUserId()
};
if (apiClient) {
return apiClient.getSessions(sessionQuery).then(function (sessions) {
return sessions.filter(function (s) {
2017-01-02 12:43:31 -07:00
return s.DeviceId !== apiClient.deviceId();
2017-01-01 15:34:44 -07:00
}).map(function (s) {
return {
name: s.DeviceName,
deviceName: s.DeviceName,
id: s.Id,
playerName: self.name,
appName: s.Client,
playableMediaTypes: s.PlayableMediaTypes,
isLocalPlayer: false,
supportedCommands: s.SupportedCommands
};
});
});
} else {
return Promise.resolve([]);
}
};
2017-01-07 22:52:51 -07:00
self.tryPair = function (target) {
2017-01-01 15:34:44 -07:00
return Promise.resolve();
};
function getPlayerState(session) {
return session;
}
2017-01-17 11:00:19 -07:00
function normalizeImages(state) {
2017-01-09 13:27:39 -07:00
if (state && state.NowPlayingItem) {
2017-01-17 11:00:19 -07:00
var item = state.NowPlayingItem;
if (!item.ImageTags || !item.ImageTags.Primary) {
if (item.PrimaryImageTag) {
item.ImageTags = item.ImageTags || {};
item.ImageTags.Primary = item.PrimaryImageTag;
2017-01-09 13:27:39 -07:00
}
}
2017-01-17 11:00:19 -07:00
if (item.BackdropImageTag && item.BackdropItemId === item.Id) {
item.BackdropImageTags = [item.BackdropImageTag];
}
if (item.BackdropImageTag && item.BackdropItemId !== item.Id) {
item.ParentBackdropImageTags = [item.BackdropImageTag];
item.ParentBackdropItemId = item.BackdropItemId;
}
2017-01-09 13:27:39 -07:00
}
}
2017-01-01 15:34:44 -07:00
function firePlaybackEvent(name, session) {
2017-01-09 13:27:39 -07:00
var state = getPlayerState(session);
2017-01-17 11:00:19 -07:00
normalizeImages(state);
2017-01-09 13:27:39 -07:00
self.lastPlayerData = state;
events.trigger(self, name, [state]);
2017-01-01 15:34:44 -07:00
}
function onWebSocketConnectionChange() {
// Reconnect
if (self.isUpdating) {
self.subscribeToPlayerUpdates();
}
}
2017-01-09 13:27:39 -07:00
function processUpdatedSessions(sessions, apiClient) {
2017-01-01 15:34:44 -07:00
2017-01-09 13:27:39 -07:00
var serverId = apiClient.serverId();
2017-01-01 15:34:44 -07:00
2017-01-09 13:27:39 -07:00
sessions.map(function (s) {
if (s.NowPlayingItem) {
s.NowPlayingItem.ServerId = serverId;
}
});
var currentTargetId = getActivePlayerId();
2017-01-01 15:34:44 -07:00
// Update existing data
//updateSessionInfo(popup, msg.Data);
var session = sessions.filter(function (s) {
2017-01-02 12:43:31 -07:00
return s.Id === currentTargetId;
2017-01-01 15:34:44 -07:00
})[0];
if (session) {
2017-01-07 22:52:51 -07:00
firePlaybackEvent('statechange', session);
2017-01-01 15:34:44 -07:00
firePlaybackEvent('timeupdate', session);
firePlaybackEvent('pause', session);
}
}
events.on(serverNotifications, 'Sessions', function (e, apiClient, data) {
2017-01-09 13:27:39 -07:00
processUpdatedSessions(data, apiClient);
2017-01-01 15:34:44 -07:00
});
events.on(serverNotifications, 'SessionEnded', function (e, apiClient, data) {
console.log("Server reports another session ended");
2017-01-07 22:52:51 -07:00
if (getActivePlayerId() === data.Id) {
2017-01-01 15:34:44 -07:00
playbackManager.setDefaultPlayerActive();
}
});
events.on(serverNotifications, 'PlaybackStart', function (e, apiClient, data) {
2017-01-02 12:43:31 -07:00
if (data.DeviceId !== apiClient.deviceId()) {
2017-01-07 22:52:51 -07:00
if (getActivePlayerId() === data.Id) {
2017-01-01 15:34:44 -07:00
firePlaybackEvent('playbackstart', data);
}
}
});
events.on(serverNotifications, 'PlaybackStopped', function (e, apiClient, data) {
2017-01-02 12:43:31 -07:00
if (data.DeviceId !== apiClient.deviceId()) {
2017-01-07 22:52:51 -07:00
if (getActivePlayerId() === data.Id) {
2017-01-01 15:34:44 -07:00
firePlaybackEvent('playbackstop', data);
}
}
});
2017-01-07 22:52:51 -07:00
};
2017-01-01 15:34:44 -07:00
});