Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MTC SessionsChanged and CurrentSessionChanged (FocusedSession) not triggering #6

Open
Aviv-Frenkel opened this issue Oct 15, 2022 · 15 comments
Labels
bug Something isn't working waiting on third party Completion is being held by a third party

Comments

@Aviv-Frenkel
Copy link

Aviv-Frenkel commented Oct 15, 2022

I tried the example project in the repository, I am using Windows 10 Pro 21H2 build 19044.2006.

  • OnAnySessionOpened event is raised only when you launch the program, any new media source can not be detected while the program is running.
  • When you close media source (spotify or chrome for example) the OnAnySessionClosed event is not raised and instead the OnAnyPlaybackStateChanged event is being called multiple times:
    image
    If you try to reopen the closed media source, it is not detected neither.
@DubyaDude
Copy link
Owner

DubyaDude commented Oct 15, 2022

Just to clarify:

  • Spotify doing multiple pauses is how the media handles its shutdown/startup process. I wish I could tell ya why, but I don't really know.

Currently on Windows 10 Pro 21H2 build 19044.2130
This is currently what I get (added in some lines to show what action me/the user took)

=> Spotify already opened, opened WMC-Sample
[15:41:07.639] -- New Source: Spotify.exe
[15:41:07.647] Spotify.exe is now playing Shake Up Christmas by Train
=> Clicked Play
[15:41:58.742] Spotify.exe is now Playing
=> Clicked Pause
[15:42:11.990] Spotify.exe is now Paused
=> Closed Spotify App (Not playing music) (gotta love this app)
[15:42:25.396] Spotify.exe is now Paused
[15:42:25.397] Spotify.exe is now Paused
[15:42:25.397] Spotify.exe is now Paused
[15:42:25.397] Spotify.exe is now Paused
[15:42:25.398] Spotify.exe is now Paused
[15:42:25.398] Spotify.exe is now Paused
[15:42:25.398] Spotify.exe is now Paused
[15:42:25.399] Spotify.exe is now Paused
[15:42:25.400] Spotify.exe is now playing
[15:42:25.400] Spotify.exe is now Paused
[15:42:25.440] Spotify.exe is now Paused
[15:42:25.440] Spotify.exe is now Paused
[15:42:25.441] Spotify.exe is now Paused
[15:42:25.467] -- Removed Source: Spotify.exe
=> Opened Spotify App (Not playing music) (gotta love this app)
[15:43:00.502] -- New Source: Spotify.exe
[15:43:00.504] Spotify.exe is now playing
[15:43:00.778] Spotify.exe is now playing
[15:43:00.778] Spotify.exe is now Paused
[15:43:00.778] Spotify.exe is now Paused
[15:43:00.779] Spotify.exe is now Paused
[15:43:00.789] Spotify.exe is now playing
[15:43:00.836] Spotify.exe is now playing
[15:43:00.847] Spotify.exe is now playing
[15:43:00.854] Spotify.exe is now Paused
[15:43:00.854] Spotify.exe is now Paused
[15:43:00.854] Spotify.exe is now Paused
[15:43:00.855] Spotify.exe is now Paused
[15:43:00.855] Spotify.exe is now Paused
[15:43:00.855] Spotify.exe is now Paused
[15:43:00.856] Spotify.exe is now Paused
[15:43:00.857] Spotify.exe is now Paused
[15:43:00.862] Spotify.exe is now Paused
[15:43:00.908] Spotify.exe is now Paused
[15:43:00.967] Spotify.exe is now playing Shake Up Christmas by Train
[15:43:00.972] Spotify.exe is now playing Shake Up Christmas by Train

Seems to be working fine, try rebooting/run any pending updates?
If that doesn't fix it, I'd be happy to help dig further.

@Aviv-Frenkel
Copy link
Author

As I mentioned above this behavior also occurs for Chrome and other processes.
Tomorrow I'll dig into this more to check what is happening.
Just want to point out that I tried to use the existing DLL file from the example solution, rebuild the project locally and use the new compiled DLL file and also tried to use the package from the Nuget installer - same behavior with all of these.

@Aviv-Frenkel
Copy link
Author

Aviv-Frenkel commented Oct 16, 2022

I found out that _WindowsSessionManager.SessionsChanged += SessionsChanged; is never triggered.
I also noticed this:
image
I am using DotNet 4.8, am I missing something?

@DubyaDude
Copy link
Owner

Hmmm, can you try changing that line and making it an asyc start?

@DubyaDude
Copy link
Owner

DubyaDude commented Oct 16, 2022

Actually digging more into it, the compiler seemed to be smart enough to replace GetAwaiter().GetResult() to its counterpart

WindowsRuntimeSystemExtensions.GetAwaiter(GlobalSystemMediaTransportControlsSessionManager.RequestAsync()).GetResult()

So generally, that could be ignored.

Rereading this, if SessionsChanged is the only event not being triggered, this may be something that has to do more with the machine itself.

How does the UI sample look when doing these tests?

@Aviv-Frenkel
Copy link
Author

Aviv-Frenkel commented Oct 16, 2022

I googled the issue and found this thread: Bug: Media Sessions are not updating in Windows 10 2004
It seems that this is a known bug from a while ago and still present in later Windows versions.
Maybe this repository can also help: ADeltaX/NPSMLib.
I don't know how the SessionsChanged event triggers on your machine and and not on mine.
I'll try to dig in the above repositories and look for their solutions.

@Aytackydln
Copy link

Something I've noticed while I was integrating this library to Artemis and Aurora is: If you don't unsubscribe to the events and close your program the events won't trigger again. Maybe this library needs to use weak events or needs to unsubscribe with dispose methods

@DubyaDude
Copy link
Owner

Something I've noticed while I was integrating this library to Artemis and Aurora is: If you don't unsubscribe to the events and close your program the events won't trigger again. Maybe this library needs to use weak events or needs to unsubscribe with dispose methods

Hmmm, I see, I started running into it now as well, I'll need to test with some reboots to confirm.
I'm not too familiar with weak events but sounds interesting. The tool currently does unsubscribe itself via its dispose method here

@DubyaDude
Copy link
Owner

After some testing, The issue goes away for me after logout+login, I haven't been able to purposely trigger it breaking again.
I tried creating ~2k instances of media manager and forcefully closing with the dispose method removed. Can't get the issue to recreate itself on demand.

@DubyaDude DubyaDude changed the title OnAnySessionOpened & OnAnySessionClosed events issues GlobalSystemMediaTransportControlsSessionManager.SessionsChanged not triggering Oct 17, 2022
@DubyaDude DubyaDude added the bug Something isn't working label Oct 17, 2022
@DubyaDude
Copy link
Owner

Unless you guys think otherwise, I think this is more or less a Windows bug. Not quite sure where/how to report/debug it further.

After some thinking though, a somewhat jank way around this is to have a background task to check if sessions changed by grabbing sessions and comparing them with the local dictionary. Would that be something you'd be interested in?

@Aytackydln
Copy link

Neither me or any of Aurora's and Artemis plugin users had this issue (reportedly).

I think is is more of a Spotify bug. I've used this library for Chrome, Opera GX a lot and a little for Spotify to test library. Even Chrome and opera GX uses media sessions very differently, Spotify might use media sessions very different too.

If it would help I can share how I use the library:
https://github.com/Aytackydln/Artemis.MediaInfo/blob/master/Artemis.MediaInfo/MediaInfoModule.cs

@DubyaDude
Copy link
Owner

I think is is more of a Spotify bug. I've used this library for Chrome, Opera GX a lot and a little for Spotify to test library. Even Chrome and opera GX uses media sessions very differently, Spotify might use media sessions very different too.

If it would help I can share how I use the library: https://github.com/Aytackydln/Artemis.MediaInfo/blob/master/Artemis.MediaInfo/MediaInfoModule.cs

It seems implausible to me that Spotify alone 'breaks' that event entirely.
From what I could tell, it happens after a bit of PC uptime. What specific action for me causes it, I'm not sure, but it's been fairly consistent, even on days I don't use Spotify.

Needs some more research in general and I just need to throw more time into figuring out what event actually causes it to break.

@DubyaDude
Copy link
Owner

Also after some more digging, this is a reported issue in Windows Feedback Hub that is marked 'Looking into it'

You need to have the 'Feedback Hub' app to be able to see this (which is slightly annoying).
https://aka.ms/AAbz7hd

Screenshot for those that don't want to bother with the feedback hub:
image

@DubyaDude
Copy link
Owner

Version 2.3.0 adds a method called ForceUpdate().
This will allow you to do a workaround if desired.

Until Microsoft fixes the issue, this is the best I can really do.

@DubyaDude DubyaDude added the waiting on third party Completion is being held by a third party label Nov 24, 2022
@DubyaDude DubyaDude changed the title GlobalSystemMediaTransportControlsSessionManager.SessionsChanged not triggering MTC SessionsChanged and CurrentSessionChanged not triggering Nov 25, 2022
@DubyaDude
Copy link
Owner

Confirmed issue also affects CurrentSessionChanged event (what I named FocusedSession)

@DubyaDude DubyaDude changed the title MTC SessionsChanged and CurrentSessionChanged not triggering MTC SessionsChanged and CurrentSessionChanged (FocusedSession) not triggering Nov 25, 2022
@DubyaDude DubyaDude pinned this issue Apr 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working waiting on third party Completion is being held by a third party
Projects
None yet
Development

No branches or pull requests

3 participants