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

357 lines
9.0 KiB
JavaScript
Raw Normal View History

2013-05-28 18:45:39 -07:00
(function (window, document, $) {
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
});
};
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 () {
var deferred = $.Deferred();
2014-10-25 11:32:58 -07:00
var apiClient = ConnectionManager.currentApiClient();
2014-10-25 11:32:58 -07:00
if (apiClient) {
apiClient.getSessions().done(function (sessions) {
2014-10-25 11:32:58 -07:00
var currentTargetId = MediaController.getPlayerInfo().id;
2014-04-15 19:17:48 -07:00
2014-10-25 11:32:58 -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
if (session) {
session = getPlayerState(session);
}
deferred.resolveWith(null, [session]);
});
} else {
deferred.resolveWith(null, [{}]);
}
return deferred.promise();
};
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");
}
2014-04-28 20:56:20 -07:00
};
function unsubscribeFromPlayerUpdates() {
2014-04-28 20:56:20 -07:00
self.false = true;
if (ApiClient.isWebSocketOpen()) {
ApiClient.sendWebSocketMessage("SessionsStop");
}
}
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 () {
var deferred = $.Deferred();
2014-03-29 12:02:43 -07:00
var sessionQuery = {
ControllableByUserId: Dashboard.getCurrentUserId()
};
2014-03-29 09:58:49 -07:00
2014-10-25 11:32:58 -07:00
var apiClient = ConnectionManager.currentApiClient();
2014-03-29 11:20:42 -07:00
2014-10-25 11:32:58 -07:00
if (apiClient) {
apiClient.getSessions(sessionQuery).done(function (sessions) {
2014-03-29 09:58:49 -07:00
2014-10-25 11:32:58 -07:00
var targets = sessions.filter(function (s) {
2014-03-29 09:58:49 -07:00
2014-10-25 11:32:58 -07:00
return s.DeviceId != apiClient.deviceId();
2014-03-29 09:58:49 -07:00
2014-10-25 11:32:58 -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 09:58:49 -07:00
2014-10-25 11:32:58 -07:00
deferred.resolveWith(null, [targets]);
2014-03-29 11:20:42 -07:00
2014-10-25 11:32:58 -07:00
}).fail(function () {
deferred.reject();
});
} else {
deferred.resolveWith(null, []);
}
2014-03-29 09:58:49 -07:00
return deferred.promise();
};
}
var player = new remoteControlPlayer();
MediaController.registerPlayer(player);
function getPlayerState(session) {
2014-04-22 10:25:54 -07:00
return session;
}
function firePlaybackEvent(name, session) {
$(player).trigger(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();
}
}
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") {
var currentTargetId = MediaController.getPlayerInfo().id;
// Update existing data
//updateSessionInfo(popup, msg.Data);
var session = msg.Data.filter(function (s) {
return s.Id == currentTargetId;
})[0];
if (session) {
firePlaybackEvent('playstatechange', session);
}
}
else if (msg.MessageType === "SessionEnded") {
2014-04-06 10:53:23 -07:00
console.log("Server reports another session ended");
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) {
$(apiClient).on("websocketmessage", onWebSocketMessageReceived).on("websocketopen", onWebSocketConnectionChange);
}
2014-10-30 21:57:24 -07:00
$(ConnectionManager).on('apiclientcreated', function (e, apiClient) {
2014-10-25 11:32:58 -07:00
2014-10-30 21:57:24 -07:00
initializeApiClient(apiClient);
2014-10-25 11:32:58 -07:00
});
2014-04-06 10:53:23 -07:00
2013-05-28 18:45:39 -07:00
})(window, document, jQuery);