Skip to content

Commit

Permalink
Fix: hot registration/unregistration of players
Browse files Browse the repository at this point in the history
  • Loading branch information
rmoalic committed Aug 24, 2021
1 parent e704c24 commit 63510dc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,4 @@ $ cp obs_media_plugin.so /usr/share/obs/obs-plugins/

## Note

* If using Firefox make sure `media.hardwaremediakeys.enabled` is set to `true` (default)
* On Windows, the player must be started before obs starts. Otherwise the infos will not be available. This is because of a bug introduced in windows 20H1, with prevent the feature to be implemented the 'correct' way. Workarounds are possible but none are implemented at this time.
* If using Firefox make sure `media.hardwaremediakeys.enabled` is set to `true` (default)
50 changes: 35 additions & 15 deletions player_windows_uwp_get_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#pragma comment(lib, "windowsapp")

#include <iostream>
#include <set>
#include <algorithm>

#include "player_info_get.h"
#include "track_info.h"
Expand All @@ -25,11 +27,13 @@ using namespace Windows::Foundation::Collections;

using namespace std;

static void handle_session_change(GlobalSystemMediaTransportControlsSessionManager session_manager, SessionsChangedEventArgs const& args) {
// TODO: handle player hot connect/disconnect, when microsoft fixes the bug that makes this function not being called
cout << "HERE §§§" << endl;
void update_players_registration();

__debugbreak();
static GlobalSystemMediaTransportControlsSessionManager session_manager { nullptr };
set<string> registered_players;

static void handle_session_change(GlobalSystemMediaTransportControlsSessionManager session_manager_l, SessionsChangedEventArgs const& args) {
update_players_registration();
}

static void handle_media_property_change(GlobalSystemMediaTransportControlsSession session, MediaPropertiesChangedEventArgs const& arg) {
Expand Down Expand Up @@ -124,33 +128,49 @@ static void handle_media_playback_info_change(GlobalSystemMediaTransportControls
track_info_print_players();
}

void register_players() {
GlobalSystemMediaTransportControlsSessionManager session_manager = GlobalSystemMediaTransportControlsSessionManager::RequestAsync().get();
session_manager.SessionsChanged(handle_session_change);

void update_players_registration() {
set<string> players_seen;
auto sessions = session_manager.GetSessions();
winrt::hstring AUMI;

for (auto& session : sessions) {
session.MediaPropertiesChanged(handle_media_property_change);
session.PlaybackInfoChanged(handle_media_playback_info_change);

AUMI = session.SourceAppUserModelId();
std::string s = winrt::to_string(AUMI);

track_info_register_player(s.c_str(), s.c_str());
players_seen.insert(s);
if (registered_players.find(s) == registered_players.end()) { // not found
session.MediaPropertiesChanged(handle_media_property_change);
session.PlaybackInfoChanged(handle_media_playback_info_change);

registered_players.insert(s);
track_info_register_player(s.c_str(), s.c_str());

handle_media_property_change(session, NULL);
handle_media_playback_info_change(session, NULL);
handle_media_property_change(session, NULL);
handle_media_playback_info_change(session, NULL);
}
}
set<string> players_not_seen;
set_difference(registered_players.begin(), registered_players.end(), players_seen.begin(), players_seen.end(), std::inserter(players_not_seen, players_not_seen.end()));

for (string player_name: players_not_seen) {
track_info_unregister_player(player_name.c_str());
registered_players.erase(player_name);
}
}

extern "C" void player_info_init() {
log_info("Initialising");
track_info_init();

register_players();
try {
session_manager = GlobalSystemMediaTransportControlsSessionManager::RequestAsync().get();
} catch (...) {
log_error("an error occured while getting the GlobalSystemMediaTransportControlsSessionManager");
return;
}
session_manager.SessionsChanged(handle_session_change);

update_players_registration();
}

extern "C" int player_info_process() {
Expand Down

0 comments on commit 63510dc

Please sign in to comment.