mirror of
https://github.com/jellyfin/jellyfin-web.git
synced 2024-11-18 03:18:19 -07:00
329 lines
8.6 KiB
JavaScript
329 lines
8.6 KiB
JavaScript
(function (window, document, $) {
|
|
|
|
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
|
|
};
|
|
|
|
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);
|
|
}
|
|
|
|
function sendCommand(command, options) {
|
|
|
|
var sessionId = MediaController.getPlayerInfo().id;
|
|
|
|
ApiClient.sendCommand(sessionId, command, options);
|
|
}
|
|
|
|
function remoteControlPlayer() {
|
|
|
|
var self = this;
|
|
|
|
self.name = 'Remote Control';
|
|
|
|
self.play = function (options) {
|
|
|
|
sendPlayCommand(options, 'PlayNow');
|
|
};
|
|
|
|
self.shuffle = function (id) {
|
|
|
|
sendPlayCommand({ ids: [id] }, 'PlayShuffle');
|
|
};
|
|
|
|
self.instantMix = function (id) {
|
|
|
|
sendPlayCommand({ ids: [id] }, 'PlayInstantMix');
|
|
};
|
|
|
|
self.queue = function (options) {
|
|
|
|
sendPlayCommand(options, 'PlayNext');
|
|
};
|
|
|
|
self.queueNext = function (options) {
|
|
|
|
sendPlayCommand(options, 'PlayLast');
|
|
};
|
|
|
|
self.canQueueMediaType = function (mediaType) {
|
|
|
|
return mediaType == 'Audio' || mediaType == 'Video';
|
|
};
|
|
|
|
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 () {
|
|
sendCommand('Mute');
|
|
};
|
|
|
|
self.unMute = function () {
|
|
sendCommand('Unmnute');
|
|
};
|
|
|
|
self.toggleMute = function () {
|
|
sendCommand('ToggleMute');
|
|
};
|
|
|
|
self.setVolume = function (vol) {
|
|
sendCommand('SetVolume', {
|
|
|
|
Volume: vol
|
|
|
|
});
|
|
};
|
|
|
|
self.displayContent = function (options) {
|
|
|
|
sendCommand('DisplayContent', {
|
|
|
|
ItemName: options.itemName,
|
|
ItemType: options.itemType,
|
|
ItemId: options.itemId,
|
|
Context: options.context
|
|
|
|
});
|
|
};
|
|
|
|
self.getPlayerState = function () {
|
|
|
|
var deferred = $.Deferred();
|
|
|
|
ApiClient.getSessions().done(function (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) {
|
|
session = getPlayerState(session);
|
|
}
|
|
|
|
deferred.resolveWith(null, [session]);
|
|
});
|
|
|
|
return deferred.promise();
|
|
};
|
|
|
|
function subscribeToPlayerUpdates() {
|
|
|
|
if (ApiClient.isWebSocketOpen()) {
|
|
|
|
ApiClient.sendWebSocketMessage("SessionsStart", "100,700");
|
|
}
|
|
}
|
|
|
|
function unsubscribeFromPlayerUpdates() {
|
|
|
|
if (ApiClient.isWebSocketOpen()) {
|
|
|
|
ApiClient.sendWebSocketMessage("SessionsStop");
|
|
}
|
|
}
|
|
|
|
var playerListenerCount = 0;
|
|
self.beginPlayerUpdates = function () {
|
|
|
|
if (playerListenerCount <= 0) {
|
|
|
|
playerListenerCount = 0;
|
|
|
|
subscribeToPlayerUpdates();
|
|
}
|
|
|
|
playerListenerCount++;
|
|
};
|
|
|
|
self.endPlayerUpdates = function () {
|
|
|
|
playerListenerCount--;
|
|
|
|
if (playerListenerCount <= 0) {
|
|
|
|
unsubscribeFromPlayerUpdates();
|
|
playerListenerCount = 0;
|
|
}
|
|
};
|
|
|
|
self.getTargets = function () {
|
|
|
|
var deferred = $.Deferred();
|
|
|
|
var sessionQuery = {
|
|
SupportsRemoteControl: true,
|
|
ControllableByUserId: Dashboard.getCurrentUserId()
|
|
};
|
|
|
|
ApiClient.getSessions(sessionQuery).done(function (sessions) {
|
|
|
|
var targets = sessions.filter(function (s) {
|
|
|
|
return s.DeviceId != ApiClient.deviceId();
|
|
|
|
}).map(function (s) {
|
|
return {
|
|
name: s.DeviceName,
|
|
id: s.Id,
|
|
playerName: self.name,
|
|
appName: s.Client,
|
|
playableMediaTypes: s.PlayableMediaTypes,
|
|
isLocalPlayer: false,
|
|
supportedCommands: s.SupportedCommands
|
|
};
|
|
});
|
|
|
|
deferred.resolveWith(null, [targets]);
|
|
|
|
}).fail(function () {
|
|
|
|
deferred.reject();
|
|
});
|
|
|
|
return deferred.promise();
|
|
};
|
|
}
|
|
|
|
var player = new remoteControlPlayer();
|
|
|
|
MediaController.registerPlayer(player);
|
|
|
|
function getPlayerState(session) {
|
|
|
|
var state = {};
|
|
|
|
var playerState = session.PlayState;
|
|
|
|
if (playerState) {
|
|
|
|
state.isMuted = playerState.IsMuted;
|
|
state.isPaused = playerState.IsPaused;
|
|
state.canSeek = playerState.CanSeek;
|
|
state.positionTicks = playerState.PositionTicks || 0;
|
|
|
|
state.mediaSource = playerState.MediaSourceId;
|
|
state.volumeLevel = playerState.VolumeLevel;
|
|
state.audioStreamIndex = playerState.AudioStreamIndex;
|
|
state.subtitleStreamIndex = playerState.SubtitleStreamIndex;
|
|
}
|
|
|
|
var item = session.NowPlayingItem;
|
|
|
|
if (item) {
|
|
|
|
state.itemId = item.Id;
|
|
state.mediaType = item.MediaType;
|
|
state.itemType = item.Type;
|
|
state.indexNumber = item.IndexNumber;
|
|
state.indexNumberEnd = item.IndexNumberEnd;
|
|
state.parentIndexNumber = item.ParentIndexNumber;
|
|
state.productionYear = item.ProductionYear;
|
|
state.premiereDate = item.PremiereDate;
|
|
state.seriesName = item.SeriesName;
|
|
state.album = item.Album;
|
|
state.itemName = item.Name;
|
|
state.artists = item.Artists;
|
|
|
|
state.primaryImageItemId = item.PrimaryImageItemId;
|
|
state.primaryImageTag = item.PrimaryImageTag;
|
|
|
|
state.backdropItemId = item.BackdropItemId;
|
|
state.backdropImageTag = item.BackdropImageTag;
|
|
|
|
state.thumbItemId = item.ThumbItemId;
|
|
state.thumbImageTag = item.ThumbImageTag;
|
|
|
|
state.runtimeTicks = item.RunTimeTicks;
|
|
}
|
|
|
|
return state;
|
|
}
|
|
|
|
function firePlaybackEvent(name, session) {
|
|
|
|
$(player).trigger(name, [getPlayerState(session)]);
|
|
}
|
|
|
|
function onWebSocketMessageReceived(e, msg) {
|
|
|
|
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") {
|
|
|
|
console.log("Server reports another session ended");
|
|
|
|
if (MediaController.getPlayerInfo().id == msg.Data.Id) {
|
|
MediaController.setDefaultPlayerActive();
|
|
}
|
|
}
|
|
else if (msg.MessageType === "PlaybackStart") {
|
|
firePlaybackEvent('playbackstart', msg.Data);
|
|
}
|
|
else if (msg.MessageType === "PlaybackStopped") {
|
|
firePlaybackEvent('playbackstop', msg.Data);
|
|
}
|
|
}
|
|
|
|
$(ApiClient).on("websocketmessage", onWebSocketMessageReceived);
|
|
|
|
})(window, document, jQuery); |