Mastermindzh / tidal-hifi

The web version of Tidal running in electron with hifi support thanks to widevine.
Other
918 stars 53 forks source link

Tidal HiFi Juggling Having Discord See Updates of Tracks/Songs/Etc #351

Closed irishgordo closed 3 months ago

irishgordo commented 4 months ago

Hey there, this project is awesome!

I was noticing some strange issues with Tidal HiFi updating Discord with either a:

On my loadout (system) of:

pop-os➜  tidal-hifi : master ✘ :✹ ᐅ  cat /etc/os-release
NAME="Pop!_OS"
VERSION="22.04 LTS"
ID=pop
ID_LIKE="ubuntu debian"
PRETTY_NAME="Pop!_OS 22.04 LTS"
VERSION_ID="22.04"
HOME_URL="https://pop.system76.com"
SUPPORT_URL="https://support.system76.com"
BUG_REPORT_URL="https://github.com/pop-os/pop/issues"
PRIVACY_POLICY_URL="https://system76.com/privacy"
VERSION_CODENAME=jammy
UBUNTU_CODENAME=jammy
LOGO=distributor-logo-pop-os

I decided to try to build it from source (after uninstalling either .snap or .deb) utilizing:

And after tossing some Logger.log messages inside the src.scripts.discord.ts I was noticing that it seems to be resolving itself mostly!

The one thing is that I guess on launch, if a music file was already present in tidal hifi, like picking up where the user was previously listening to the track, I was catching a lot of logs w/ my custom log entry of media is paused... but the media wasn't paused it was playing.

In the discord.ts I had just modified this chunk to see some log data:


const getActivity = (): Presence => {
  const presence: Presence = { ...defaultPresence };

  if (mediaInfo.status === MediaStatus.paused) {
    Logger.log("media is paused...")
    presence.details =
      settingsStore.get<string, string>(settings.discord.idleText) ?? "Browsing Tidal";
  } else {
    const showSong = settingsStore.get<string, boolean>(settings.discord.showSong) ?? false;
    Logger.log("show song is?: " + showSong);
    if (showSong) {
      Logger.log("show song is present --- : " + showSong)
      const { includeTimestamps, detailsPrefix, buttonText } = getFromStore();
      includeTimeStamps(includeTimestamps);
      setPresenceFromMediaInfo(detailsPrefix, buttonText);
    } else {
      presence.details =
        settingsStore.get<string, string>(settings.discord.usingText) ?? "Playing media on TIDAL";
      Logger.log("showing the details of the precense but no song? ...");
    }
  }
  return presence;

  function getFromStore() {
    const includeTimestamps =
      settingsStore.get<string, boolean>(settings.discord.includeTimestamps) ?? true;
    const detailsPrefix =
      settingsStore.get<string, string>(settings.discord.detailsPrefix) ?? "Listening to ";
    const buttonText =
      settingsStore.get<string, string>(settings.discord.buttonText) ?? "Play on TIDAL";

    Logger.log("detailsPrefix is: " + detailsPrefix);
    return { includeTimestamps, detailsPrefix, buttonText };
  }

  function setPresenceFromMediaInfo(detailsPrefix: any, buttonText: any) {
    if (mediaInfo.url) {
      Logger.log("Here is mediaInfo.url: " + mediaInfo.url);
      presence.details = `${detailsPrefix}${mediaInfo.title}`;
      presence.state = mediaInfo.artists ? mediaInfo.artists : "unknown artist(s)";
      presence.largeImageKey = mediaInfo.image;
      if (mediaInfo.album) {
        presence.largeImageText = mediaInfo.album;
      }
      presence.buttons = [{ label: buttonText, url: mediaInfo.url }];
    } else {
      presence.details = `Watching ${mediaInfo.title}`;
      presence.state = mediaInfo.artists;
    }
  }

  function includeTimeStamps(includeTimestamps: any) {
    if (includeTimestamps) {
      const currentSeconds = timeToSeconds(mediaInfo.current.split(":"));
      const durationSeconds = timeToSeconds(mediaInfo.duration.split(":"));
      const date = new Date();
      const now = (date.getTime() / 1000) | 0;
      const remaining = date.setSeconds(date.getSeconds() + (durationSeconds - currentSeconds));
      presence.startTimestamp = now;
      presence.endTimestamp = remaining;
    }
  }
};

/**
 * Set up the discord rpc and listen on globalEvents.updateInfo
 */
export const initRPC = () => {
  rpc = new Client({ transport: "ipc" });
  rpc.login({ clientId }).then(
    () => {
      rpc.on("ready", () => {
        Logger.log("rpc is ready...")
        rpc.setActivity(getActivity());
      });
      ipcMain.on(globalEvents.updateInfo, observer);
    },
    () => {
      Logger.log("Can't connect to Discord, is it running?");
    }
  );
};

Workaround

Even if it is displaying media is paused when the media is playing at first launch, the moment you switch to another track it resolves itself to actually show that track information in Discord accurately.

Questions

Additional context, Discord is installed on my little rig via just the package manager.

I think for now, just doing an npm compile & npm run start alleviates Discord from not seeing updates - wondering though if it's possible to debug the .deb package to see what differences might be in the loadout.

Screenshot from 2024-02-19 13-20-27

Mastermindzh commented 4 months ago

Will have to look at the startup sequence & variable instantiation 😄. Thanks for the solid issue! Luckily it doesn't affect anything after the first song.

I guess I'm curious as to what the difference is between running this from source off the master branch in comparison to running this off a .deb / .snap package. As with running from an installed .deb package in my system, for some reason, Discord isn't really getting any updates from Tidal HiFi, it just seems to hang at "Browsing Tidal".

Snaps are sandboxed by default and need to be allowed to talk to system interfaces.