Skip to content

Commit

Permalink
fix mpris
Browse files Browse the repository at this point in the history
  • Loading branch information
Araxeus committed Nov 10, 2021
1 parent f40ed04 commit ccfe743
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 50 deletions.
117 changes: 67 additions & 50 deletions plugins/shortcuts/back.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
const { globalShortcut } = require("electron");
const { globalShortcut, ipcMain } = require("electron");
const is = require("electron-is");
const electronLocalshortcut = require("electron-localshortcut");
const getSongControls = require("../../providers/song-controls");
const { setupMPRIS } = require("./mpris");
const registerCallback = require("../../providers/song-info");

let player;

function _registerGlobalShortcut(webContents, shortcut, action) {
globalShortcut.register(shortcut, () => {
action(webContents);
Expand All @@ -31,54 +29,8 @@ function registerShortcuts(win, options) {

_registerLocalShortcut(win, "CommandOrControl+F", search);
_registerLocalShortcut(win, "CommandOrControl+L", search);
registerCallback(songInfo => {
if (player) {
player.metadata = {
'mpris:length': songInfo.songDuration * 60 * 1000 * 1000, // In microseconds
'mpris:artUrl': songInfo.imageSrc,
'xesam:title': songInfo.title,
'xesam:artist': songInfo.artist
};
if (!songInfo.isPaused) {
player.playbackStatus = "Playing"
}
}
}
)

if (is.linux()) {
try {
const MPRISPlayer = setupMPRIS();

MPRISPlayer.on("raise", () => {
win.setSkipTaskbar(false);
win.show();
});
MPRISPlayer.on("play", () => {
if (MPRISPlayer.playbackStatus !== 'Playing') {
MPRISPlayer.playbackStatus = 'Playing';
playPause()
}
});
MPRISPlayer.on("pause", () => {
if (MPRISPlayer.playbackStatus !== 'Paused') {
MPRISPlayer.playbackStatus = 'Paused';
playPause()
}
});
MPRISPlayer.on("next", () => {
next()
});
MPRISPlayer.on("previous", () => {
previous()
});

player = MPRISPlayer

} catch (e) {
console.warn("Error in MPRIS", e);
}
}
if (is.linux()) registerMPRIS();

const { global, local } = options;
const shortcutOptions = { global, local };
Expand Down Expand Up @@ -106,6 +58,71 @@ function registerShortcuts(win, options) {
}
}
}
function registerMPRIS() {
try {
const secToMicro = n => Math.round(Number(n) * (1000 * 1000));
const microToSec = n => Math.round(Number(n) / 1000 / 1000);

const seekTo = e => win.webContents.send("seekTo", microToSec(e.position));
const seek = o => win.webContents.send("seek", microToSec(o));

const player = setupMPRIS();

const mprisSeek = p => {
player.seeked(p);
}
win.webContents.send("registerOnSeek");

ipcMain.on('seeked', (_, t) => mprisSeek(secToMicro(t)));

let currentSeconds = 0;
ipcMain.on('timeChanged', (_, t) => currentSeconds = t);

player.getPosition = () => secToMicro(currentSeconds)

player.on("raise", () => {
win.setSkipTaskbar(false);
win.show();
});

player.on("play", () => {
if (player.playbackStatus !== 'Playing') {
player.playbackStatus = 'Playing';
playPause()
}
});
player.on("pause", () => {
if (player.playbackStatus !== 'Paused') {
player.playbackStatus = 'Paused';
playPause()
}
});

player.on("playpause", playPause);
player.on("next", next);
player.on("previous", previous);

player.on('seek', seek);
player.on('position', seekTo);

registerCallback(songInfo => {
if (player) {
player.metadata = {
'mpris:length': secToMicro(songInfo.songDuration),
'mpris:artUrl': songInfo.imageSrc,
'xesam:title': songInfo.title,
'xesam:artist': songInfo.artist,
'mpris:trackid': '/'
};;
mprisSeek(secToMicro(songInfo.elapsedSeconds))
player.playbackStatus = songInfo.isPaused ? "Paused" : "Playing"
}
})

} catch (e) {
console.warn("Error in MPRIS", e);
}
}
}

module.exports = registerShortcuts;
4 changes: 4 additions & 0 deletions preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const config = require("./config");
const { fileExists } = require("./plugins/utils");
const setupFrontLogger = require("./providers/front-logger");
const setupSongInfo = require("./providers/song-info-front");
const { setupSongControls } = require("./providers/song-controls-front");

const plugins = config.plugins.getEnabled();

Expand Down Expand Up @@ -45,6 +46,9 @@ document.addEventListener("DOMContentLoaded", () => {
// inject song-info provider
setupSongInfo();

// inject song-controls
setupSongControls();

// inject front logger
setupFrontLogger();

Expand Down
30 changes: 30 additions & 0 deletions providers/song-controls-front.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const { ipcRenderer } = require("electron");

module.exports.seekTo = seekTo;
function seekTo(t) {
document.querySelector('video').currentTime = t;
}

module.exports.seek = seek;
function seek(o) {
document.querySelector('video').currentTime += o;
}

module.exports.setupSongControls = () => {
ipcRenderer.on("seekTo", async (_, t) => seekTo(t));
ipcRenderer.on("seek", async (_, t) => seek(t));
ipcRenderer.once("registerOnSeek", registerOnSeek)
};

async function registerOnSeek() {
const register = v => v.addEventListener('seeked', () => ipcRenderer.send('seeked', v.currentTime));
let video = document.querySelector('video');
if (video) {
register(video);
}
else {
document.addEventListener('apiLoaded', () => {
register(document.querySelector('video'))
}, { once: true, passive: true })
}
}
10 changes: 10 additions & 0 deletions providers/song-info-front.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,19 @@ ipcRenderer.on("update-song-info", async (_, extractedSongInfo) => {

module.exports = () => {
document.addEventListener('apiLoaded', e => {
setupTimeChangeListener();

document.querySelector('video').addEventListener('loadedmetadata', () => {
const data = e.detail.getPlayerResponse();
ipcRenderer.send("song-info-request", JSON.stringify(data));
});
}, { once: true, passive: true })
};

function setupTimeChangeListener() {
const progressObserver = new MutationObserver(mutations => {
ipcRenderer.send('timeChanged', mutations[0].target.value);
global.songInfo.elapsedSeconds = mutations[0].target.value;
});
progressObserver.observe(document.querySelector('#progress-bar'), { attributeFilter: ["value"] })
}
2 changes: 2 additions & 0 deletions providers/song-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const songInfo = {
songDuration: 0,
elapsedSeconds: 0,
url: "",
videoId: "",
};

const handleData = async (responseText, win) => {
Expand All @@ -57,6 +58,7 @@ const handleData = async (responseText, win) => {
songInfo.image = await getImage(songInfo.imageSrc);
songInfo.uploadDate = data?.microformat?.microformatDataRenderer?.uploadDate;
songInfo.url = data?.microformat?.microformatDataRenderer?.urlCanonical?.split("&")[0];
songInfo.videoId = data?.videoDetails?.videoId;

// used for options.resumeOnStart
config.set("url", data?.microformat?.microformatDataRenderer?.urlCanonical);
Expand Down

0 comments on commit ccfe743

Please sign in to comment.