th-ch / youtube-music

YouTube Music Desktop App bundled with custom plugins (and built-in ad blocker / downloader)
https://th-ch.github.io/youtube-music/
MIT License
8.65k stars 494 forks source link

Add support for media keys using mpris2 on the linux platform #65

Closed jrabinow closed 2 years ago

jrabinow commented 3 years ago

This app is perfect in every way ❤️ , except for one thing: currently, media keys don't seem to work on my setup (linux).

Would it be possible to add support for the mpris2 protocol? You can find a working implementation here and the full spec is here in case you needed to refer to it

azizLIGHT commented 3 years ago

There is some kind of mpris implementation because it works for me on Manjaro on Gnome. I can press pause/play/prev/next successfully and it carries over to my bluetooth headset buttons too. Maybe its not to spec like you say as some things are working for some and not others? In any case, here's what I noticed related to mpris:

When i query qdbus for track information by mpris, the app name is a random chromium instance number like org.mpris.MediaPlayer2.chromium.instance1820402 instead of something like org.mpris.MediaPlayer2.youtube-music. I also can't query full track information with qdbus org.mpris.MediaPlayer2.chromium.instance1820402 org.freedesktop.DBus.Properties.Get org.mpris.MediaPlayer2.Player Metadata like you would for a player like vlc, I get nothing returned back on the prompt. A Gnome extension I use to display now-playing information in my top panel can find track information, but the Artist has - Topic appended at the end of it and Title seems cutoff image.

The notifications show proper track information however: image

Hope this info helps implementing the mpris spec fully!

jrabinow commented 3 years ago

@azizLIGHT I can only guess why it's working for you but not for me: since you mention you're using Gnome, would it be possible for Gnome to be using its own media keys solution independent of mpris? Maybe even something similar to this

On my end, my DE has no such media key service built in. I'm using a keyboard shortcut tied to https://github.com/mackstann/mpris-remote (actually I updated the code a fair bit, long story short it works with mpris2. Tested and confirmed all players work except this one). Per my understanding, the way dbus works internally is it looks for any mpris-compatible players that registered themselves and dispatches them the message - so my guess is the name of the app doesn't matter, it's just that there's no mpris support at the moment, or that it didn't register.

Would there be a way for you to confirm that the media keys are actually working through mpris on your setup? Possibly dbus-monitor would be of help https://wiki.ubuntu.com/DebuggingDBus I'm not sure it'll display when apps receive a sent message but it's worth a shot IMO

EDIT: I have to say I'm quite mystified by your gnome plugin which doesn't seem to be using anything other than mpris (or maybe I'm missing something, I spent all of 10min looking through the source) - though I'm still rather confident that mpris isn't supported by youtube-music app since grepping the source for mpris didn't reveal a single result

th-ch commented 3 years ago

Hey @jrabinow & @azizLIGHT, thanks for the precise feedback! I have opened a draft PR https://github.com/th-ch/youtube-music/pull/78 but I have no setup to test it 😬 Feel free to test it out and update the issue accordingly! To run it, you need node.js and yarn, then:

git clone https://github.com/th-ch/youtube-music
cd youtube-music
git checkout mpris
yarn
yarn start
jrabinow commented 3 years ago

Hey @th-ch thanks so much! I'm afraid there's a bug somewhere, attached are the tracebacks:

dbus-monitor-out.txt mpris-remote-cmd-out.txt

I'm not sure if you have a linux distro available to test things out on, if you do you can check out playerctl or mpris-remote... otherwise I'll do my best to test things out whenever I get the chance

JoeJoeTV commented 3 years ago

Are there any updates to this? I could maybe test it on my Linux Mint install, if that helps.

itzmanish commented 3 years ago

I have tested on Arch linux with playerctl. So please merge this.

itzmanish commented 3 years ago

@th-ch when this will be release? I am waiting only for this because I don't know how do I install in arch linux. (PS- I am also lazy to go through tutorials)

th-ch commented 3 years ago

Hey @itzmanish, I still need to go through a few PRs (like https://github.com/th-ch/youtube-music/pull/389) and then I'll release a new version, probably in the following weeks!

itzmanish commented 3 years ago

@th-ch I created new PR #431 for this issue. Please check the PR for details.

jrabinow commented 2 years ago

I'm going to close this issue as all changes have shipped with the latest release. If people are still running into problems, please feel free to reopen.

Thanks everyone!

JoeJoeTV commented 2 years ago

I'm still having issues with this after updating to the newest version(1.14.0). The mpris2 control now properly shows YouTube Music with the song info(title and artist) and also the cover, but only the previous and next buttons work. There is also still a "Chromium" control, that shows the song info without a cover art, but there, all buttons(previous, play/pause, stop and skip) work in contrast to the "correct" control.

EDIT: Also the progress bar doesn't work as in, it doesn't reflect the progress of the playing song and changing it in the mpris control interface doesn't seek to that part. The "Chromium" control doesn't even show a progress bar.

EDIT2: I have looked into this using d-feet and qdbusviewer and the Play() and Pause() methods work, but the PlayPause() and Stop() methods do not. Also the Position property stays at 0 and the Seek method does nothing, no matter, what the argument value is.

I'm on Linux Mint Cinnamon 20.2 64x and have installed the .deb version.

Araxeus commented 2 years ago

@JoeJoeTV I have possible fixes in mind but no way to test on windows. would you be willing to help me test?

if so add me on discord Araxeus#0819

fix playPause:

    MPRISPlayer.on("playpause", playpause);

fix displayed time in mpris:

youtubePlayer.onTimeUpdate = time =>
    mprisPlayer.seeked(time)

seek from mpris progress bar:

player.on('seek', (offset) => {
   // note that offset may be negative
   const currentPosition = youtubePlayer.getPositionInMicroseconds();
   const newPosition = currentPosition + offset;
   realPlayer.setPosition(newPosition);
});

docs: https://www.npmjs.com/package/mpris-service https://github.com/dbusjs/mpris-service/blob/master/src/index.js#L26-L132 https://github.com/dbusjs/mpris-service/search?q=seeked https://github.com/dbusjs/mpris-service/blob/master/examples/player.js