jellyfin-web/dashboard-ui/scripts/remotecontrol.js

399 lines
10 KiB
JavaScript
Raw Normal View History

2016-03-15 22:33:31 -07:00
(function (window, document) {
2013-05-28 18:45:39 -07:00
2014-03-29 11:20:42 -07:00
function sendPlayCommand(options, playType) {
var sessionId = MediaController.getPlayerInfo().id;
var ids = options.ids || options.items.map(function (i) {
return i.Id;
});
var remoteOptions = {
ItemIds: ids.join(','),
PlayCommand: playType
};
2014-04-06 10:53:23 -07:00
2014-03-29 11:20:42 -07:00
if (options.startPositionTicks) {
remoteOptions.startPositionTicks = options.startPositionTicks;
}
ApiClient.sendPlayCommand(sessionId, remoteOptions);
}
function sendPlayStateCommand(command, options) {
var sessionId = MediaController.getPlayerInfo().id;
ApiClient.sendPlayStateCommand(sessionId, command, options);
}
2014-03-29 09:58:49 -07:00
function remoteControlPlayer() {
var self = this;
self.name = 'Remote Control';
2014-04-27 18:57:29 -07:00
function sendCommandByName(name, options) {
var command = {
Name: name
};
if (options) {
command.Arguments = options;
}
self.sendCommand(command);
}
self.sendCommand = function (command) {
var sessionId = MediaController.getPlayerInfo().id;
ApiClient.sendCommand(sessionId, command);
};
2014-03-29 09:58:49 -07:00
self.play = function (options) {
2014-03-29 11:20:42 -07:00
sendPlayCommand(options, 'PlayNow');
2014-03-29 09:58:49 -07:00
};
self.shuffle = function (id) {
2014-03-29 11:20:42 -07:00
sendPlayCommand({ ids: [id] }, 'PlayShuffle');
2014-03-29 09:58:49 -07:00
};
self.instantMix = function (id) {
2014-03-29 11:20:42 -07:00
sendPlayCommand({ ids: [id] }, 'PlayInstantMix');
2014-03-29 09:58:49 -07:00
};
self.queue = function (options) {
2014-03-29 11:20:42 -07:00
sendPlayCommand(options, 'PlayNext');
2014-03-29 09:58:49 -07:00
};
self.queueNext = function (options) {
2014-03-29 11:20:42 -07:00
sendPlayCommand(options, 'PlayLast');
2014-03-29 09:58:49 -07:00
};
self.canQueueMediaType = function (mediaType) {
2014-03-29 11:20:42 -07:00
return mediaType == 'Audio' || mediaType == 'Video';
2014-03-29 09:58:49 -07:00
};
self.stop = function () {
sendPlayStateCommand('stop');
};
self.nextTrack = function () {
sendPlayStateCommand('nextTrack');
};
self.previousTrack = function () {
sendPlayStateCommand('previousTrack');
};
self.seek = function (positionTicks) {
sendPlayStateCommand('seek',
{
SeekPositionTicks: positionTicks
});
};
self.pause = function () {
sendPlayStateCommand('Pause');
};
self.unpause = function () {
sendPlayStateCommand('Unpause');
};
self.mute = function () {
2014-04-27 18:57:29 -07:00
sendCommandByName('Mute');
};
self.unMute = function () {
2014-04-27 18:57:29 -07:00
sendCommandByName('Unmute');
};
self.toggleMute = function () {
2014-04-27 18:57:29 -07:00
sendCommandByName('ToggleMute');
};
self.setVolume = function (vol) {
2014-04-27 18:57:29 -07:00
sendCommandByName('SetVolume', {
Volume: vol
});
};
2014-04-30 20:24:55 -07:00
self.volumeUp = function () {
sendCommandByName('VolumeUp');
};
self.volumeDown = function () {
sendCommandByName('VolumeDown');
};
self.toggleFullscreen = function () {
sendCommandByName('ToggleFullscreen');
};
2014-05-07 22:04:39 -07:00
self.setAudioStreamIndex = function (index) {
sendCommandByName('SetAudioStreamIndex', {
Index: index
});
};
self.setSubtitleStreamIndex = function (index) {
sendCommandByName('SetSubtitleStreamIndex', {
Index: index
});
};
2015-07-26 14:02:23 -07:00
self.setRepeatMode = function (mode) {
sendCommandByName('SetRepeatMode', {
RepeatMode: mode
});
};
2014-04-13 10:27:13 -07:00
self.displayContent = function (options) {
2014-08-25 19:30:52 -07:00
sendCommandByName('DisplayContent', options);
2014-04-13 10:27:13 -07:00
};
self.getPlayerState = function () {
2015-12-30 13:25:17 -07:00
return new Promise(function (resolve, reject) {
2015-12-30 13:25:17 -07:00
var apiClient = window.ApiClient;
2015-12-30 13:25:17 -07:00
if (apiClient) {
apiClient.getSessions().then(function (sessions) {
2014-04-15 19:17:48 -07:00
2015-12-30 13:25:17 -07:00
var currentTargetId = MediaController.getPlayerInfo().id;
2015-12-30 13:25:17 -07:00
// Update existing data
//updateSessionInfo(popup, msg.Data);
var session = sessions.filter(function (s) {
return s.Id == currentTargetId;
})[0];
2014-10-25 11:32:58 -07:00
2015-12-30 13:25:17 -07:00
if (session) {
session = getPlayerState(session);
}
2015-12-30 13:25:17 -07:00
resolve(session);
});
} else {
resolve({});
}
});
};
2015-07-14 09:39:34 -07:00
var pollInterval;
function onPollIntervalFired() {
if (!ApiClient.isWebSocketOpen()) {
var apiClient = window.ApiClient;
if (apiClient) {
2015-12-14 08:43:03 -07:00
apiClient.getSessions().then(processUpdatedSessions);
2015-07-14 09:39:34 -07:00
}
}
}
2014-04-28 20:56:20 -07:00
self.subscribeToPlayerUpdates = function () {
self.isUpdating = true;
if (ApiClient.isWebSocketOpen()) {
2014-05-13 17:46:45 -07:00
ApiClient.sendWebSocketMessage("SessionsStart", "100,800");
}
2015-07-14 09:39:34 -07:00
if (pollInterval) {
clearInterval(pollInterval);
pollInterval = null;
}
2015-11-12 11:05:51 -07:00
pollInterval = setInterval(onPollIntervalFired, 5000);
2014-04-28 20:56:20 -07:00
};
function unsubscribeFromPlayerUpdates() {
2015-07-14 09:39:34 -07:00
self.isUpdating = true;
2014-04-28 20:56:20 -07:00
if (ApiClient.isWebSocketOpen()) {
ApiClient.sendWebSocketMessage("SessionsStop");
}
2015-07-14 09:39:34 -07:00
if (pollInterval) {
clearInterval(pollInterval);
pollInterval = null;
}
}
var playerListenerCount = 0;
self.beginPlayerUpdates = function () {
if (playerListenerCount <= 0) {
playerListenerCount = 0;
2014-04-28 20:56:20 -07:00
self.subscribeToPlayerUpdates();
}
playerListenerCount++;
};
self.endPlayerUpdates = function () {
playerListenerCount--;
if (playerListenerCount <= 0) {
unsubscribeFromPlayerUpdates();
playerListenerCount = 0;
}
};
2014-03-29 09:58:49 -07:00
self.getTargets = function () {
2015-12-30 13:25:17 -07:00
return new Promise(function (resolve, reject) {
2014-03-29 09:58:49 -07:00
2015-12-30 13:25:17 -07:00
var sessionQuery = {
ControllableByUserId: Dashboard.getCurrentUserId()
};
2014-03-29 11:20:42 -07:00
2015-12-30 13:25:17 -07:00
var apiClient = window.ApiClient;
2014-03-29 09:58:49 -07:00
2015-12-30 13:25:17 -07:00
if (apiClient) {
apiClient.getSessions(sessionQuery).then(function (sessions) {
2014-03-29 09:58:49 -07:00
2015-12-30 13:25:17 -07:00
var targets = sessions.filter(function (s) {
2014-03-29 09:58:49 -07:00
2015-12-30 13:25:17 -07:00
return s.DeviceId != apiClient.deviceId();
2014-03-29 09:58:49 -07:00
2015-12-30 13:25:17 -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
};
});
2014-03-29 11:20:42 -07:00
2015-12-30 13:25:17 -07:00
resolve(targets);
2014-10-25 11:32:58 -07:00
2015-12-30 13:25:17 -07:00
}, function () {
2015-12-14 08:43:03 -07:00
2015-12-30 13:25:17 -07:00
reject();
});
2014-03-29 09:58:49 -07:00
2015-12-30 13:25:17 -07:00
} else {
resolve([]);
}
});
2014-03-29 09:58:49 -07:00
};
2015-05-18 09:40:20 -07:00
self.tryPair = function(target) {
2015-12-30 13:25:17 -07:00
return new Promise(function (resolve, reject) {
resolve();
});
2015-05-18 09:40:20 -07:00
};
2014-03-29 09:58:49 -07:00
}
var player = new remoteControlPlayer();
MediaController.registerPlayer(player);
function getPlayerState(session) {
2014-04-22 10:25:54 -07:00
return session;
}
function firePlaybackEvent(name, session) {
2015-06-29 18:56:25 -07:00
Events.trigger(player, name, [getPlayerState(session)]);
}
2014-03-29 09:58:49 -07:00
2014-04-28 20:56:20 -07:00
function onWebSocketConnectionChange() {
2014-05-07 22:04:39 -07:00
2014-04-28 20:56:20 -07:00
// Reconnect
if (player.isUpdating) {
player.subscribeToPlayerUpdates();
}
}
2015-07-14 09:39:34 -07:00
function processUpdatedSessions(sessions) {
var currentTargetId = MediaController.getPlayerInfo().id;
// Update existing data
//updateSessionInfo(popup, msg.Data);
var session = sessions.filter(function (s) {
return s.Id == currentTargetId;
})[0];
if (session) {
firePlaybackEvent('playstatechange', session);
}
}
2014-04-06 10:53:23 -07:00
function onWebSocketMessageReceived(e, msg) {
2014-10-25 11:32:58 -07:00
var apiClient = this;
if (msg.MessageType === "Sessions") {
2015-07-14 09:39:34 -07:00
processUpdatedSessions(msg.Data);
}
else if (msg.MessageType === "SessionEnded") {
2014-04-06 10:53:23 -07:00
2015-12-23 10:46:01 -07:00
console.log("Server reports another session ended");
2014-04-06 10:53:23 -07:00
if (MediaController.getPlayerInfo().id == msg.Data.Id) {
MediaController.setDefaultPlayerActive();
}
}
else if (msg.MessageType === "PlaybackStart") {
2014-05-31 07:30:59 -07:00
2014-10-25 11:32:58 -07:00
if (msg.Data.DeviceId != apiClient.deviceId()) {
2014-05-31 07:30:59 -07:00
if (MediaController.getPlayerInfo().id == msg.Data.Id) {
firePlaybackEvent('playbackstart', msg.Data);
}
}
}
else if (msg.MessageType === "PlaybackStopped") {
2014-05-31 07:30:59 -07:00
2014-10-25 11:32:58 -07:00
if (msg.Data.DeviceId != apiClient.deviceId()) {
2014-05-31 07:30:59 -07:00
if (MediaController.getPlayerInfo().id == msg.Data.Id) {
firePlaybackEvent('playbackstop', msg.Data);
}
}
}
2014-04-06 10:53:23 -07:00
}
2014-10-25 11:32:58 -07:00
function initializeApiClient(apiClient) {
2015-12-23 10:46:01 -07:00
Events.on(apiClient, "websocketmessage", onWebSocketMessageReceived);
Events.on(apiClient, "websocketopen", onWebSocketConnectionChange);
2014-10-25 11:32:58 -07:00
}
2015-12-14 08:43:03 -07:00
if (window.ApiClient) {
initializeApiClient(window.ApiClient);
}
2015-05-19 12:15:40 -07:00
2015-12-23 10:46:01 -07:00
Events.on(ConnectionManager, 'apiclientcreated', function (e, apiClient) {
2015-12-14 08:43:03 -07:00
initializeApiClient(apiClient);
2014-10-25 11:32:58 -07:00
});
2014-04-06 10:53:23 -07:00
2016-03-15 22:33:31 -07:00
})(window, document);