Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix queue when playing on remote device (partial) #3381

Merged
merged 14 commits into from
Oct 17, 2024
4 changes: 4 additions & 0 deletions src/components/remotecontrol/remotecontrol.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,10 @@ export default function () {

function loadPlaylist(context, player) {
getPlaylistItems(player).then(function (items) {
if (items.length === 0) {
return;
}

let html = '';
let favoritesEnabled = true;
if (layoutManager.mobile) {
Expand Down
138 changes: 136 additions & 2 deletions src/plugins/sessionPlayer/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,66 @@
}
}

async function updatePlaylist(instance, queue) {
instance.playlist = [];
const max = 100;
const apiClient = getCurrentApiClient(instance);

const fetch = async (newQueue) => {
const options = {
ids: newQueue.map(i => i.Id ),
serverId: apiClient.serverId()
};

const result = await playbackManager.getItemsForPlayback(options.serverId, {
Ids: options.ids.join(',')
});

const items = await playbackManager.translateItemsForPlayback(result.Items, options);

for (let i = 0, length = items.length; i < length; i++) {
items[i].PlaylistItemId = newQueue[i].PlaylistItemId;
}
return items;
};

const n = Math.floor(queue.length / max) + 1;

for (let i = 0; i < n; i++) {
instance.playlist.push(...await fetch(queue.slice(max * i, max * (i + 1))));
}
}

function compareQueues(q1, q2) {
if (q1.length !== q2.length)
return true;

Check failure on line 119 in src/plugins/sessionPlayer/plugin.js

View workflow job for this annotation

GitHub Actions / Quality checks 👌🧪 / Run lint 🕵️‍♂️

Expected { after 'if' condition
github-actions[bot] marked this conversation as resolved.
Show resolved Hide resolved

for (let i = 0, length = q1.length; i < length; i++) {
if (q1[i].Id !== q2[i].Id || q1[i].PlaylistItemId !== q2[i].PlaylistItemId) {
return true;
}
}
return false;
}

function updateCurrentQueue(instance, session) {
const current = session.NowPlayingQueue;
if (instance.updating_playlist)
return;

Check failure on line 132 in src/plugins/sessionPlayer/plugin.js

View workflow job for this annotation

GitHub Actions / Quality checks 👌🧪 / Run lint 🕵️‍♂️

Expected { after 'if' condition
github-actions[bot] marked this conversation as resolved.
Show resolved Hide resolved

if (instance.lastPlayerData && !compareQueues(current, instance.playlist))
return;

Check failure on line 135 in src/plugins/sessionPlayer/plugin.js

View workflow job for this annotation

GitHub Actions / Quality checks 👌🧪 / Run lint 🕵️‍♂️

Expected { after 'if' condition
github-actions[bot] marked this conversation as resolved.
Show resolved Hide resolved

instance.updating_playlist = true;

const finish = () => {
instance.updating_playlist = false;
instance.render_playlist = true;
};

updatePlaylist(instance, current).then(finish, finish);
}

function processUpdatedSessions(instance, sessions, apiClient) {
const serverId = apiClient.serverId();

Expand All @@ -103,6 +163,8 @@
normalizeImages(session, apiClient);

const eventNames = getChangedEvents(instance.lastPlayerData);
updateCurrentQueue(instance, session);

instance.lastPlayerData = session;

for (let i = 0, length = eventNames.length; i < length; i++) {
Expand Down Expand Up @@ -186,6 +248,11 @@
this.isLocalPlayer = false;
this.id = 'remoteplayer';

this.playlist = [];
this.render_playlist = true;
this.updating_playlist = false;
this.last_song_playlist_id = 0;

Events.on(serverNotifications, 'Sessions', function (e, apiClient, data) {
processUpdatedSessions(self, data, apiClient);
});
Expand Down Expand Up @@ -484,16 +551,83 @@
return state.MediaType === 'Audio';
}

getTrackIndex(PlaylistItemId) {
for (let i = 0, length = this.playlist.length; i < length; i++) {
if (this.playlist[i].PlaylistItemId === PlaylistItemId) {
return i;
}
}
}

getPlaylist() {
let song_id = 0;

if (this.lastPlayerData) {
song_id = this.lastPlayerData.PlaylistItemId;
}

if (this.playlist.length > 0 && (this.render_playlist || song_id !== this.last_song_playlist_id)) {
this.render_playlist = false;
this.last_song_playlist_id = song_id;
return Promise.resolve(this.playlist);
}
return Promise.resolve([]);
}

movePlaylistItem(playlistItemId, newIndex) {
const index = this.getTrackIndex(playlistItemId);
if (index === newIndex)
return;

Check failure on line 580 in src/plugins/sessionPlayer/plugin.js

View workflow job for this annotation

GitHub Actions / Quality checks 👌🧪 / Run lint 🕵️‍♂️

Expected { after 'if' condition
github-actions[bot] marked this conversation as resolved.
Show resolved Hide resolved

const current = this.getCurrentPlaylistItemId();
let current_pos = 0;

if (current === playlistItemId)
current_pos = newIndex;

Check failure on line 586 in src/plugins/sessionPlayer/plugin.js

View workflow job for this annotation

GitHub Actions / Quality checks 👌🧪 / Run lint 🕵️‍♂️

Expected { after 'if' condition
github-actions[bot] marked this conversation as resolved.
Show resolved Hide resolved

const append = (newIndex + 1 >= this.playlist.length);

if (newIndex > index)
newIndex++;

Check failure on line 591 in src/plugins/sessionPlayer/plugin.js

View workflow job for this annotation

GitHub Actions / Quality checks 👌🧪 / Run lint 🕵️‍♂️

Expected { after 'if' condition
github-actions[bot] marked this conversation as resolved.
Show resolved Hide resolved

const ids = [];
const item = this.playlist[index];

for (let i = 0, length = this.playlist.length; i < length; i++) {
if (i === index)
continue;

Check failure on line 598 in src/plugins/sessionPlayer/plugin.js

View workflow job for this annotation

GitHub Actions / Quality checks 👌🧪 / Run lint 🕵️‍♂️

Expected { after 'if' condition
github-actions[bot] marked this conversation as resolved.
Show resolved Hide resolved

if (i === newIndex)
ids.push(item.Id);

Check failure on line 601 in src/plugins/sessionPlayer/plugin.js

View workflow job for this annotation

GitHub Actions / Quality checks 👌🧪 / Run lint 🕵️‍♂️

Expected { after 'if' condition
github-actions[bot] marked this conversation as resolved.
Show resolved Hide resolved

if (this.playlist[i].PlaylistItemId === current)
current_pos = ids.length;

Check failure on line 604 in src/plugins/sessionPlayer/plugin.js

View workflow job for this annotation

GitHub Actions / Quality checks 👌🧪 / Run lint 🕵️‍♂️

Expected { after 'if' condition
github-actions[bot] marked this conversation as resolved.
Show resolved Hide resolved

ids.push(this.playlist[i].Id);
}

if (append)
ids.push(item.Id);

Check failure on line 610 in src/plugins/sessionPlayer/plugin.js

View workflow job for this annotation

GitHub Actions / Quality checks 👌🧪 / Run lint 🕵️‍♂️

Expected { after 'if' condition
github-actions[bot] marked this conversation as resolved.
Show resolved Hide resolved

const options = {
ids: ids,
startIndex: current_pos
};

return sendPlayCommand(getCurrentApiClient(this), options, 'PlayNow');
}

getCurrentPlaylistItemId() {
return this.lastPlayerData.PlaylistItemId;
// not supported?
}

setCurrentPlaylistItem() {
return Promise.resolve();
setCurrentPlaylistItem(PlaylistItemId) {
const options = {
ids: this.playlist.map(i => i.Id),
startIndex: this.getTrackIndex(PlaylistItemId)
};
return sendPlayCommand(getCurrentApiClient(this), options, 'PlayNow');
}

removeFromPlaylist() {
Expand Down
Loading