mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-16 20:52:06 +00:00
Merge pull request #480 from Araxeus/mpris+tuna-fix
Mpris + obs-tuna fixes
This commit is contained in:
@ -2,10 +2,7 @@ const { globalShortcut } = require("electron");
|
|||||||
const is = require("electron-is");
|
const is = require("electron-is");
|
||||||
const electronLocalshortcut = require("electron-localshortcut");
|
const electronLocalshortcut = require("electron-localshortcut");
|
||||||
const getSongControls = require("../../providers/song-controls");
|
const getSongControls = require("../../providers/song-controls");
|
||||||
const { setupMPRIS } = require("./mpris");
|
const registerMPRIS = require("./mpris");
|
||||||
const registerCallback = require("../../providers/song-info");
|
|
||||||
|
|
||||||
let player;
|
|
||||||
|
|
||||||
function _registerGlobalShortcut(webContents, shortcut, action) {
|
function _registerGlobalShortcut(webContents, shortcut, action) {
|
||||||
globalShortcut.register(shortcut, () => {
|
globalShortcut.register(shortcut, () => {
|
||||||
@ -31,54 +28,8 @@ function registerShortcuts(win, options) {
|
|||||||
|
|
||||||
_registerLocalShortcut(win, "CommandOrControl+F", search);
|
_registerLocalShortcut(win, "CommandOrControl+F", search);
|
||||||
_registerLocalShortcut(win, "CommandOrControl+L", 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()) {
|
if (is.linux()) registerMPRIS(win);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const { global, local } = options;
|
const { global, local } = options;
|
||||||
const shortcutOptions = { global, local };
|
const shortcutOptions = { global, local };
|
||||||
|
|||||||
@ -1,4 +1,6 @@
|
|||||||
const mpris = require("mpris-service");
|
const mpris = require("mpris-service");
|
||||||
|
const { ipcMain } = require("electron");
|
||||||
|
const registerCallback = require("../../providers/song-info");
|
||||||
|
|
||||||
function setupMPRIS() {
|
function setupMPRIS() {
|
||||||
const player = mpris({
|
const player = mpris({
|
||||||
@ -14,6 +16,69 @@ function setupMPRIS() {
|
|||||||
return player;
|
return player;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
function registerMPRIS(win) {
|
||||||
setupMPRIS,
|
try {
|
||||||
};
|
const secToMicro = n => Math.round(Number(n) * 1e6);
|
||||||
|
const microToSec = n => Math.round(Number(n) / 1e6);
|
||||||
|
|
||||||
|
const seekTo = e => win.webContents.send("seekTo", microToSec(e.position));
|
||||||
|
const seekBy = o => win.webContents.send("seekBy", microToSec(o));
|
||||||
|
|
||||||
|
const player = setupMPRIS();
|
||||||
|
|
||||||
|
const mprisSeek = player.seeked;
|
||||||
|
|
||||||
|
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', seekBy);
|
||||||
|
player.on('position', seekTo);
|
||||||
|
|
||||||
|
registerCallback(songInfo => {
|
||||||
|
if (player) {
|
||||||
|
const data = {
|
||||||
|
'mpris:length': secToMicro(songInfo.songDuration),
|
||||||
|
'mpris:artUrl': songInfo.imageSrc,
|
||||||
|
'xesam:title': songInfo.title,
|
||||||
|
'xesam:artist': songInfo.artist,
|
||||||
|
'mpris:trackid': '/'
|
||||||
|
};
|
||||||
|
if (songInfo.album) data['xesam:album'] = songInfo.album;
|
||||||
|
player.metadata = data;
|
||||||
|
mprisSeek(secToMicro(songInfo.elapsedSeconds))
|
||||||
|
player.playbackStatus = songInfo.isPaused ? "Paused" : "Playing"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
} catch (e) {
|
||||||
|
console.warn("Error in MPRIS", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = registerMPRIS;
|
||||||
|
|||||||
@ -1,33 +1,52 @@
|
|||||||
const { ipcRenderer } = require("electron");
|
const { ipcMain } = require("electron");
|
||||||
const fetch = require('node-fetch');
|
const fetch = require('node-fetch');
|
||||||
|
|
||||||
const registerCallback = require("../../providers/song-info");
|
const registerCallback = require("../../providers/song-info");
|
||||||
|
|
||||||
const post = (data) => {
|
const secToMilisec = t => Math.round(Number(t) * 1e3);
|
||||||
|
const data = {
|
||||||
|
cover_url: '',
|
||||||
|
title: '',
|
||||||
|
artists: [],
|
||||||
|
status: '',
|
||||||
|
progress: 0,
|
||||||
|
duration: 0,
|
||||||
|
album_url: '',
|
||||||
|
album: undefined
|
||||||
|
};
|
||||||
|
|
||||||
|
const post = async (data) => {
|
||||||
const port = 1608;
|
const port = 1608;
|
||||||
headers = {'Content-Type': 'application/json',
|
headers = {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
'Accept': 'application/json',
|
'Accept': 'application/json',
|
||||||
'Access-Control-Allow-Headers': '*',
|
'Access-Control-Allow-Headers': '*',
|
||||||
'Access-Control-Allow-Origin': '*'}
|
'Access-Control-Allow-Origin': '*'
|
||||||
|
}
|
||||||
const url = `http://localhost:${port}/`;
|
const url = `http://localhost:${port}/`;
|
||||||
fetch(url, {method: 'POST', headers, body:JSON.stringify({data})});
|
fetch(url, { method: 'POST', headers, body: JSON.stringify({ data }) }).catch(e => console.log(`Error: '${e.code || e.errno}' - when trying to access obs-tuna webserver at port ${port}`));
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = async (win) => {
|
module.exports = async (win) => {
|
||||||
registerCallback((songInfo) => {
|
ipcMain.on('timeChanged', async (_, t) => {
|
||||||
|
if (!data.title) return;
|
||||||
|
data.progress = secToMilisec(t);
|
||||||
|
post(data);
|
||||||
|
});
|
||||||
|
|
||||||
// Register the callback
|
registerCallback((songInfo) => {
|
||||||
if (songInfo.title.length === 0 && songInfo.artist.length === 0) {
|
if (!songInfo.title && !songInfo.artist) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const duration = Number(songInfo.songDuration)*1000
|
data.duration = secToMilisec(songInfo.songDuration)
|
||||||
const progress = Number(songInfo.elapsedSeconds)*1000
|
data.progress = secToMilisec(songInfo.elapsedSeconds)
|
||||||
const cover_url = songInfo.imageSrc
|
data.cover_url = songInfo.imageSrc;
|
||||||
const album_url = songInfo.imageSrc
|
data.album_url = songInfo.imageSrc;
|
||||||
const title = songInfo.title
|
data.title = songInfo.title;
|
||||||
const artists = [songInfo.artist]
|
data.artists = [songInfo.artist];
|
||||||
const status = !songInfo.isPaused ? 'Playing': 'Paused'
|
data.status = songInfo.isPaused ? 'stopped' : 'playing';
|
||||||
post({ cover_url, title, artists, status, progress, duration, album_url});
|
data.album = songInfo.album;
|
||||||
|
post(data);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,6 +6,7 @@ const config = require("./config");
|
|||||||
const { fileExists } = require("./plugins/utils");
|
const { fileExists } = require("./plugins/utils");
|
||||||
const setupFrontLogger = require("./providers/front-logger");
|
const setupFrontLogger = require("./providers/front-logger");
|
||||||
const setupSongInfo = require("./providers/song-info-front");
|
const setupSongInfo = require("./providers/song-info-front");
|
||||||
|
const { setupSongControls } = require("./providers/song-controls-front");
|
||||||
|
|
||||||
const plugins = config.plugins.getEnabled();
|
const plugins = config.plugins.getEnabled();
|
||||||
|
|
||||||
@ -45,6 +46,9 @@ document.addEventListener("DOMContentLoaded", () => {
|
|||||||
// inject song-info provider
|
// inject song-info provider
|
||||||
setupSongInfo();
|
setupSongInfo();
|
||||||
|
|
||||||
|
// inject song-controls
|
||||||
|
setupSongControls();
|
||||||
|
|
||||||
// inject front logger
|
// inject front logger
|
||||||
setupFrontLogger();
|
setupFrontLogger();
|
||||||
|
|
||||||
|
|||||||
15
providers/song-controls-front.js
Normal file
15
providers/song-controls-front.js
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
const { ipcRenderer } = require("electron");
|
||||||
|
const config = require("../config");
|
||||||
|
const is = require("electron-is");
|
||||||
|
|
||||||
|
module.exports.setupSongControls = () => {
|
||||||
|
document.addEventListener('apiLoaded', e => {
|
||||||
|
ipcRenderer.on("seekTo", (_, t) => e.target.seekTo(t));
|
||||||
|
ipcRenderer.on("seekBy", (_, t) => e.target.seekBy(t));
|
||||||
|
|
||||||
|
}, { once: true, passive: true })
|
||||||
|
|
||||||
|
if (is.linux() && config.plugins.isEnabled('shortcuts')) { // MPRIS Enabled
|
||||||
|
document.querySelector('video').addEventListener('seeked', () => ipcRenderer.send('seeked', v.currentTime));
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -2,8 +2,12 @@ const { ipcRenderer } = require("electron");
|
|||||||
|
|
||||||
const { getImage } = require("./song-info");
|
const { getImage } = require("./song-info");
|
||||||
|
|
||||||
|
const config = require("../config");
|
||||||
|
|
||||||
global.songInfo = {};
|
global.songInfo = {};
|
||||||
|
|
||||||
|
function $(selector) { return document.querySelector(selector); }
|
||||||
|
|
||||||
ipcRenderer.on("update-song-info", async (_, extractedSongInfo) => {
|
ipcRenderer.on("update-song-info", async (_, extractedSongInfo) => {
|
||||||
global.songInfo = JSON.parse(extractedSongInfo);
|
global.songInfo = JSON.parse(extractedSongInfo);
|
||||||
global.songInfo.image = await getImage(global.songInfo.imageSrc);
|
global.songInfo.image = await getImage(global.songInfo.imageSrc);
|
||||||
@ -14,7 +18,10 @@ const srcChangedEvent = new CustomEvent('srcChanged');
|
|||||||
|
|
||||||
module.exports = () => {
|
module.exports = () => {
|
||||||
document.addEventListener('apiLoaded', apiEvent => {
|
document.addEventListener('apiLoaded', apiEvent => {
|
||||||
const video = document.querySelector('video');
|
if (config.plugins.isEnabled('tuna-obs')) {
|
||||||
|
setupTimeChangeListener();
|
||||||
|
}
|
||||||
|
const video = $('video');
|
||||||
// name = "dataloaded" and abit later "dataupdated"
|
// name = "dataloaded" and abit later "dataupdated"
|
||||||
apiEvent.detail.addEventListener('videodatachange', (name, _dataEvent) => {
|
apiEvent.detail.addEventListener('videodatachange', (name, _dataEvent) => {
|
||||||
if (name !== 'dataloaded') return;
|
if (name !== 'dataloaded') return;
|
||||||
@ -24,7 +31,7 @@ module.exports = () => {
|
|||||||
|
|
||||||
for (const status of ['playing', 'pause']) {
|
for (const status of ['playing', 'pause']) {
|
||||||
video.addEventListener(status, e => {
|
video.addEventListener(status, e => {
|
||||||
if (Math.floor(e.target.currentTime) > 0) {
|
if (Math.round(e.target.currentTime) > 0) {
|
||||||
ipcRenderer.send("playPaused", {
|
ipcRenderer.send("playPaused", {
|
||||||
isPaused: status === 'pause',
|
isPaused: status === 'pause',
|
||||||
elapsedSeconds: Math.floor(e.target.currentTime)
|
elapsedSeconds: Math.floor(e.target.currentTime)
|
||||||
@ -35,9 +42,18 @@ module.exports = () => {
|
|||||||
|
|
||||||
function sendSongInfo() {
|
function sendSongInfo() {
|
||||||
const data = apiEvent.detail.getPlayerResponse();
|
const data = apiEvent.detail.getPlayerResponse();
|
||||||
|
data.videoDetails.album = $('ytmusic-player-page')?.__data?.playerPageWatchMetadata?.albumName?.runs[0].text
|
||||||
data.videoDetails.elapsedSeconds = Math.floor(video.currentTime);
|
data.videoDetails.elapsedSeconds = Math.floor(video.currentTime);
|
||||||
data.videoDetails.isPaused = false;
|
data.videoDetails.isPaused = false;
|
||||||
ipcRenderer.send("video-src-changed", JSON.stringify(data));
|
ipcRenderer.send("video-src-changed", JSON.stringify(data));
|
||||||
}
|
}
|
||||||
}, { once: true, passive: true });
|
}, { 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($('#progress-bar'), { attributeFilter: ["value"] })
|
||||||
|
}
|
||||||
|
|||||||
@ -19,6 +19,7 @@ const songInfo = {
|
|||||||
songDuration: 0,
|
songDuration: 0,
|
||||||
elapsedSeconds: 0,
|
elapsedSeconds: 0,
|
||||||
url: "",
|
url: "",
|
||||||
|
album: undefined,
|
||||||
videoId: "",
|
videoId: "",
|
||||||
playlistId: "",
|
playlistId: "",
|
||||||
};
|
};
|
||||||
@ -57,6 +58,7 @@ const handleData = async (responseText, win) => {
|
|||||||
songInfo.elapsedSeconds = videoDetails.elapsedSeconds;
|
songInfo.elapsedSeconds = videoDetails.elapsedSeconds;
|
||||||
songInfo.isPaused = videoDetails.isPaused;
|
songInfo.isPaused = videoDetails.isPaused;
|
||||||
songInfo.videoId = videoDetails.videoId;
|
songInfo.videoId = videoDetails.videoId;
|
||||||
|
songInfo.album = data?.videoDetails?.album; // Will be undefined if video exist
|
||||||
|
|
||||||
const oldUrl = songInfo.imageSrc;
|
const oldUrl = songInfo.imageSrc;
|
||||||
songInfo.imageSrc = videoDetails.thumbnail?.thumbnails?.pop()?.url.split("?")[0];
|
songInfo.imageSrc = videoDetails.thumbnail?.thumbnails?.pop()?.url.split("?")[0];
|
||||||
|
|||||||
Reference in New Issue
Block a user