mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-13 19:31:46 +00:00
fix: fix bugs in MPRIS, and improve MPRIS (#1760)
Co-authored-by: JellyBrick <shlee1503@naver.com> Co-authored-by: Totto <32566573+Totto16@users.noreply.github.com>
This commit is contained in:
@ -1,12 +1,27 @@
|
||||
import { BrowserWindow, ipcMain } from 'electron';
|
||||
|
||||
import MprisPlayer, { Track } from '@jellybrick/mpris-service';
|
||||
import MprisPlayer, {
|
||||
Track,
|
||||
LoopStatus,
|
||||
type PlayBackStatus,
|
||||
type PlayerOptions,
|
||||
PLAYBACK_STATUS_STOPPED,
|
||||
PLAYBACK_STATUS_PAUSED,
|
||||
PLAYBACK_STATUS_PLAYING,
|
||||
LOOP_STATUS_NONE,
|
||||
LOOP_STATUS_PLAYLIST,
|
||||
LOOP_STATUS_TRACK,
|
||||
type Position,
|
||||
} from '@jellybrick/mpris-service';
|
||||
|
||||
import registerCallback, { type SongInfo } from '@/providers/song-info';
|
||||
import getSongControls from '@/providers/song-controls';
|
||||
import config from '@/config';
|
||||
import { LoggerPrefix } from '@/utils';
|
||||
|
||||
import type { RepeatMode } from '@/types/datahost-get-state';
|
||||
import type { QueueResponse } from '@/types/youtube-music-desktop-internal';
|
||||
|
||||
class YTPlayer extends MprisPlayer {
|
||||
/**
|
||||
* @type {number} The current position in microseconds
|
||||
@ -14,12 +29,7 @@ class YTPlayer extends MprisPlayer {
|
||||
*/
|
||||
private currentPosition: number;
|
||||
|
||||
constructor(opts: {
|
||||
name: string;
|
||||
identity: string;
|
||||
supportedMimeTypes?: string[];
|
||||
supportedInterfaces?: string[];
|
||||
}) {
|
||||
constructor(opts: PlayerOptions) {
|
||||
super(opts);
|
||||
|
||||
this.currentPosition = 0;
|
||||
@ -33,35 +43,38 @@ class YTPlayer extends MprisPlayer {
|
||||
return this.currentPosition;
|
||||
}
|
||||
|
||||
setLoopStatus(status: string) {
|
||||
setLoopStatus(status: LoopStatus) {
|
||||
this.loopStatus = status;
|
||||
}
|
||||
|
||||
isPlaying(): boolean {
|
||||
return this.playbackStatus === YTPlayer.PLAYBACK_STATUS_PLAYING;
|
||||
return this.playbackStatus === PLAYBACK_STATUS_PLAYING;
|
||||
}
|
||||
|
||||
isPaused(): boolean {
|
||||
return this.playbackStatus === YTPlayer.PLAYBACK_STATUS_PAUSED;
|
||||
return this.playbackStatus === PLAYBACK_STATUS_PAUSED;
|
||||
}
|
||||
|
||||
isStopped(): boolean {
|
||||
return this.playbackStatus === YTPlayer.PLAYBACK_STATUS_STOPPED;
|
||||
return this.playbackStatus === PLAYBACK_STATUS_STOPPED;
|
||||
}
|
||||
|
||||
setPlaybackStatus(status: string) {
|
||||
setPlaybackStatus(status: PlayBackStatus) {
|
||||
this.playbackStatus = status;
|
||||
}
|
||||
}
|
||||
|
||||
function setupMPRIS() {
|
||||
const instance = new YTPlayer({
|
||||
name: 'youtube-music',
|
||||
name: 'YoutubeMusic',
|
||||
identity: 'YouTube Music',
|
||||
supportedMimeTypes: ['audio/mpeg'],
|
||||
supportedInterfaces: ['player'],
|
||||
});
|
||||
|
||||
instance.canRaise = true;
|
||||
instance.canQuit = false;
|
||||
instance.canSetFullscreen = true;
|
||||
instance.supportedUriSchemes = ['http', 'https'];
|
||||
instance.desktopEntry = 'youtube-music';
|
||||
return instance;
|
||||
@ -73,21 +86,27 @@ function registerMPRIS(win: BrowserWindow) {
|
||||
playPause,
|
||||
next,
|
||||
previous,
|
||||
volumeMinus10,
|
||||
volumePlus10,
|
||||
setVolume,
|
||||
shuffle,
|
||||
switchRepeat,
|
||||
setFullscreen,
|
||||
requestFullscreenInformation,
|
||||
requestQueueInformation,
|
||||
} = songControls;
|
||||
try {
|
||||
let currentSongInfo: SongInfo | null = null;
|
||||
const secToMicro = (n: number) => Math.round(Number(n) * 1e6);
|
||||
const microToSec = (n: number) => Math.round(Number(n) / 1e6);
|
||||
|
||||
const seekTo = (event: {
|
||||
trackId: string;
|
||||
position: number;
|
||||
}) => {
|
||||
if (event.trackId === currentSongInfo?.videoId) {
|
||||
const correctId = (videoId: string) => {
|
||||
return videoId.replace('-', '_MINUS_');
|
||||
};
|
||||
|
||||
const seekTo = (event: Position) => {
|
||||
if (
|
||||
currentSongInfo?.videoId &&
|
||||
event.trackId.endsWith(correctId(currentSongInfo.videoId))
|
||||
) {
|
||||
win.webContents.send('ytmd:seek-to', microToSec(event.position ?? 0));
|
||||
}
|
||||
};
|
||||
@ -101,6 +120,10 @@ function registerMPRIS(win: BrowserWindow) {
|
||||
win.webContents.send('ytmd:setup-time-changed-listener', 'mpris');
|
||||
win.webContents.send('ytmd:setup-repeat-changed-listener', 'mpris');
|
||||
win.webContents.send('ytmd:setup-volume-changed-listener', 'mpris');
|
||||
win.webContents.send('ytmd:setup-fullscreen-changed-listener', 'mpris');
|
||||
win.webContents.send('ytmd:setup-autoplay-changed-listener', 'mpris');
|
||||
requestFullscreenInformation();
|
||||
requestQueueInformation();
|
||||
});
|
||||
|
||||
ipcMain.on('ytmd:seeked', (_, t: number) => player.seeked(secToMicro(t)));
|
||||
@ -109,29 +132,85 @@ function registerMPRIS(win: BrowserWindow) {
|
||||
player.setPosition(secToMicro(t));
|
||||
});
|
||||
|
||||
ipcMain.on('ytmd:repeat-changed', (_, mode: string) => {
|
||||
ipcMain.on('ytmd:repeat-changed', (_, mode: RepeatMode) => {
|
||||
switch (mode) {
|
||||
case 'NONE': {
|
||||
player.setLoopStatus(YTPlayer.LOOP_STATUS_NONE);
|
||||
player.setLoopStatus(LOOP_STATUS_NONE);
|
||||
break;
|
||||
}
|
||||
case 'ONE': {
|
||||
player.setLoopStatus(YTPlayer.LOOP_STATUS_TRACK);
|
||||
player.setLoopStatus(LOOP_STATUS_TRACK);
|
||||
break;
|
||||
}
|
||||
case 'ALL': {
|
||||
player.setLoopStatus(YTPlayer.LOOP_STATUS_PLAYLIST);
|
||||
player.setLoopStatus(LOOP_STATUS_PLAYLIST);
|
||||
// No default
|
||||
break;
|
||||
}
|
||||
}
|
||||
requestQueueInformation();
|
||||
});
|
||||
player.on('loopStatus', (status: string) => {
|
||||
|
||||
ipcMain.on('ytmd:fullscreen-changed', (_, changedTo: boolean) => {
|
||||
if (player.fullscreen === undefined || !player.canSetFullscreen) {
|
||||
return;
|
||||
}
|
||||
|
||||
player.fullscreen =
|
||||
changedTo !== undefined ? changedTo : !player.fullscreen;
|
||||
});
|
||||
|
||||
ipcMain.on(
|
||||
'ytmd:set-fullscreen',
|
||||
(_, isFullscreen: boolean | undefined) => {
|
||||
if (!player.canSetFullscreen || isFullscreen === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
player.fullscreen = isFullscreen;
|
||||
},
|
||||
);
|
||||
|
||||
ipcMain.on(
|
||||
'ytmd:fullscreen-changed-supported',
|
||||
(_, isFullscreenSupported: boolean) => {
|
||||
player.canSetFullscreen = isFullscreenSupported;
|
||||
},
|
||||
);
|
||||
ipcMain.on('ytmd:autoplay-changed', (_) => {
|
||||
requestQueueInformation();
|
||||
});
|
||||
|
||||
ipcMain.on('ytmd:get-queue-response', (_, queue: QueueResponse) => {
|
||||
if (!queue) {
|
||||
return;
|
||||
}
|
||||
|
||||
const currentPosition = queue.items?.findIndex((it) =>
|
||||
it?.playlistPanelVideoRenderer?.selected ||
|
||||
it?.playlistPanelVideoWrapperRenderer?.primaryRenderer?.playlistPanelVideoRenderer?.selected
|
||||
) ?? 0;
|
||||
player.canGoPrevious = currentPosition !== 0;
|
||||
|
||||
let hasNext: boolean;
|
||||
if (queue.autoPlaying) {
|
||||
hasNext = true;
|
||||
} else if (player.loopStatus === LOOP_STATUS_PLAYLIST) {
|
||||
hasNext = true;
|
||||
} else {
|
||||
// Example: currentPosition = 0, queue.items.length = 29 -> hasNext = true
|
||||
hasNext = !!(currentPosition - (queue?.items?.length ?? 0 - 1));
|
||||
}
|
||||
|
||||
player.canGoNext = hasNext;
|
||||
});
|
||||
|
||||
player.on('loopStatus', (status: LoopStatus) => {
|
||||
// SwitchRepeat cycles between states in that order
|
||||
const switches = [
|
||||
YTPlayer.LOOP_STATUS_NONE,
|
||||
YTPlayer.LOOP_STATUS_PLAYLIST,
|
||||
YTPlayer.LOOP_STATUS_TRACK,
|
||||
LOOP_STATUS_NONE,
|
||||
LOOP_STATUS_PLAYLIST,
|
||||
LOOP_STATUS_TRACK,
|
||||
];
|
||||
const currentIndex = switches.indexOf(player.loopStatus);
|
||||
const targetIndex = switches.indexOf(status);
|
||||
@ -142,33 +221,44 @@ function registerMPRIS(win: BrowserWindow) {
|
||||
});
|
||||
|
||||
player.on('raise', () => {
|
||||
if (!player.canRaise) {
|
||||
return;
|
||||
}
|
||||
|
||||
win.setSkipTaskbar(false);
|
||||
win.show();
|
||||
});
|
||||
|
||||
player.on('fullscreen', (fullscreenEnabled: boolean) => {
|
||||
setFullscreen(fullscreenEnabled);
|
||||
});
|
||||
|
||||
player.on('play', () => {
|
||||
if (!player.isPlaying()) {
|
||||
player.setPlaybackStatus(YTPlayer.PLAYBACK_STATUS_PLAYING);
|
||||
player.setPlaybackStatus(PLAYBACK_STATUS_PLAYING);
|
||||
playPause();
|
||||
}
|
||||
});
|
||||
player.on('pause', () => {
|
||||
if (player.playbackStatus !== YTPlayer.PLAYBACK_STATUS_PAUSED) {
|
||||
player.setPlaybackStatus(YTPlayer.PLAYBACK_STATUS_PAUSED);
|
||||
if (!player.isPaused()) {
|
||||
player.setPlaybackStatus(PLAYBACK_STATUS_PAUSED);
|
||||
playPause();
|
||||
}
|
||||
});
|
||||
player.on('playpause', () => {
|
||||
player.setPlaybackStatus(
|
||||
player.isPlaying()
|
||||
? YTPlayer.PLAYBACK_STATUS_PAUSED
|
||||
: YTPlayer.PLAYBACK_STATUS_PLAYING
|
||||
player.isPlaying() ? PLAYBACK_STATUS_PAUSED : PLAYBACK_STATUS_PLAYING,
|
||||
);
|
||||
playPause();
|
||||
});
|
||||
|
||||
player.on('next', next);
|
||||
player.on('previous', previous);
|
||||
player.on('next', () => {
|
||||
next();
|
||||
});
|
||||
|
||||
player.on('previous', () => {
|
||||
previous();
|
||||
});
|
||||
|
||||
player.on('seek', seekBy);
|
||||
player.on('position', seekTo);
|
||||
@ -176,10 +266,18 @@ function registerMPRIS(win: BrowserWindow) {
|
||||
player.on('shuffle', (enableShuffle) => {
|
||||
if (enableShuffle) {
|
||||
shuffle();
|
||||
requestQueueInformation();
|
||||
}
|
||||
});
|
||||
player.on('open', (args: { uri: string }) => {
|
||||
win.loadURL(args.uri);
|
||||
win.loadURL(args.uri).then(() => {
|
||||
requestQueueInformation();
|
||||
});
|
||||
});
|
||||
|
||||
player.on('error', (error: Error) => {
|
||||
console.error(LoggerPrefix, 'Error in MPRIS');
|
||||
console.trace(error);
|
||||
});
|
||||
|
||||
let mprisVolNewer = false;
|
||||
@ -198,7 +296,7 @@ function registerMPRIS(win: BrowserWindow) {
|
||||
}
|
||||
});
|
||||
|
||||
player.on('volume', (newVolume) => {
|
||||
player.on('volume', (newVolume: number) => {
|
||||
if (config.plugins.isEnabled('precise-volume')) {
|
||||
// With precise volume we can set the volume to the exact value.
|
||||
const newVol = ~~(newVolume * 100);
|
||||
@ -208,31 +306,23 @@ function registerMPRIS(win: BrowserWindow) {
|
||||
win.webContents.send('setVolume', newVol);
|
||||
}
|
||||
} else {
|
||||
// With keyboard shortcuts we can only change the volume in increments of 10, so round it.
|
||||
let deltaVolume = Math.round((newVolume - player.volume) * 10);
|
||||
while (deltaVolume !== 0 && deltaVolume > 0) {
|
||||
volumePlus10();
|
||||
player.volume += 0.1;
|
||||
deltaVolume--;
|
||||
}
|
||||
|
||||
while (deltaVolume !== 0 && deltaVolume < 0) {
|
||||
volumeMinus10();
|
||||
player.volume -= 0.1;
|
||||
deltaVolume++;
|
||||
}
|
||||
setVolume(newVolume * 100);
|
||||
}
|
||||
});
|
||||
|
||||
registerCallback((songInfo) => {
|
||||
registerCallback((songInfo: SongInfo) => {
|
||||
if (player) {
|
||||
const data: Track = {
|
||||
'mpris:length': secToMicro(songInfo.songDuration),
|
||||
'mpris:artUrl': songInfo.imageSrc ?? undefined,
|
||||
...(songInfo.imageSrc
|
||||
? { 'mpris:artUrl': songInfo.imageSrc }
|
||||
: undefined),
|
||||
'xesam:title': songInfo.title,
|
||||
'xesam:url': songInfo.url,
|
||||
'xesam:artist': [songInfo.artist],
|
||||
'mpris:trackid': songInfo.videoId,
|
||||
'mpris:trackid': player.objectPath(
|
||||
`Track/${correctId(songInfo.videoId)}`,
|
||||
),
|
||||
};
|
||||
if (songInfo.album) {
|
||||
data['xesam:album'] = songInfo.album;
|
||||
@ -241,22 +331,20 @@ function registerMPRIS(win: BrowserWindow) {
|
||||
|
||||
player.metadata = data;
|
||||
|
||||
const currentElapsedMicroSeconds = secToMicro(songInfo.elapsedSeconds ?? 0);
|
||||
const currentElapsedMicroSeconds = secToMicro(
|
||||
songInfo.elapsedSeconds ?? 0,
|
||||
);
|
||||
player.setPosition(currentElapsedMicroSeconds);
|
||||
player.seeked(currentElapsedMicroSeconds);
|
||||
|
||||
player.setPlaybackStatus(
|
||||
songInfo.isPaused ?
|
||||
YTPlayer.PLAYBACK_STATUS_PAUSED :
|
||||
YTPlayer.PLAYBACK_STATUS_PLAYING
|
||||
songInfo.isPaused ? PLAYBACK_STATUS_PAUSED : PLAYBACK_STATUS_PLAYING,
|
||||
);
|
||||
}
|
||||
requestQueueInformation();
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(
|
||||
LoggerPrefix,
|
||||
'Error in MPRIS'
|
||||
);
|
||||
console.error(LoggerPrefix, 'Error in MPRIS');
|
||||
console.trace(error);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user