Skip to content
Permalink
Browse files
[GLIB] MediaSessionGlib shows multiple players for a single video
https://bugs.webkit.org/show_bug.cgi?id=241857

Patch by Philippe Normand <philn@igalia.com> on 2022-06-23
Reviewed by Carlos Garcia Campos.

The MediaSession is now registered on DBus lazily, only if any of its properties changes.

* Source/WebCore/platform/audio/glib/MediaSessionGLib.cpp:
(WebCore::MediaSessionGLib::MediaSessionGLib):
(WebCore::MediaSessionGLib::~MediaSessionGLib):
(WebCore::MediaSessionGLib::ensureMprisSessionRegistered):
(WebCore::MediaSessionGLib::emitPositionChanged):
(WebCore::MediaSessionGLib::emitPropertiesChanged):
* Source/WebCore/platform/audio/glib/MediaSessionGLib.h:

Canonical link: https://commits.webkit.org/251779@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@295774 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
philn authored and webkit-commit-queue committed Jun 23, 2022
1 parent bae248d commit 32442d73fb68bbcb955c334ce459c481ec345e63
Showing 2 changed files with 23 additions and 14 deletions.
@@ -200,7 +200,23 @@ MediaSessionGLib::MediaSessionGLib(MediaSessionManagerGLib& manager, GRefPtr<GDB
, m_manager(manager)
, m_connection(WTFMove(connection))
{
if (!m_connection)
}

MediaSessionGLib::~MediaSessionGLib()
{
if (m_connection) {
if (m_rootRegistrationId && !g_dbus_connection_unregister_object(m_connection.get(), m_rootRegistrationId))
g_warning("Unable to unregister MPRIS D-Bus object.");
if (m_playerRegistrationId && !g_dbus_connection_unregister_object(m_connection.get(), m_playerRegistrationId))
g_warning("Unable to unregister MPRIS D-Bus player object.");
}
if (m_ownerId)
g_bus_unown_name(m_ownerId);
}

void MediaSessionGLib::ensureMprisSessionRegistered()
{
if (m_ownerId || !m_connection)
return;

const auto& mprisInterface = m_manager.mprisInterface();
@@ -222,28 +238,18 @@ MediaSessionGLib::MediaSessionGLib(MediaSessionManagerGLib& manager, GRefPtr<GDB
}

const auto& applicationID = getApplicationID();
m_instanceId = applicationID.isEmpty() ? makeString("org.mpris.MediaPlayer2.webkit.instance", getpid(), "-", identifier.toUInt64()) : makeString("org.mpris.MediaPlayer2.", applicationID.ascii().data(), ".instance-", identifier.toUInt64());
m_instanceId = applicationID.isEmpty() ? makeString("org.mpris.MediaPlayer2.webkit.instance", getpid(), "-", m_identifier.toUInt64()) : makeString("org.mpris.MediaPlayer2.", applicationID.ascii().data(), ".instance-", m_identifier.toUInt64());

m_ownerId = g_bus_own_name_on_connection(m_connection.get(), m_instanceId.ascii().data(), G_BUS_NAME_OWNER_FLAGS_NONE, nullptr, nullptr, this, nullptr);
}

MediaSessionGLib::~MediaSessionGLib()
{
if (m_connection) {
if (m_rootRegistrationId && !g_dbus_connection_unregister_object(m_connection.get(), m_rootRegistrationId))
g_warning("Unable to unregister MPRIS D-Bus object.");
if (m_playerRegistrationId && !g_dbus_connection_unregister_object(m_connection.get(), m_playerRegistrationId))
g_warning("Unable to unregister MPRIS D-Bus player object.");
}
if (m_ownerId)
g_bus_unown_name(m_ownerId);
}

void MediaSessionGLib::emitPositionChanged(double time)
{
if (!m_connection)
return;

ensureMprisSessionRegistered();

GUniqueOutPtr<GError> error;
int64_t position = time * 1000000;
if (!g_dbus_connection_emit_signal(m_connection.get(), nullptr, DBUS_MPRIS_OBJECT_PATH, DBUS_MPRIS_PLAYER_INTERFACE, "Seeked", g_variant_new("(x)", position), &error.outPtr()))
@@ -320,6 +326,8 @@ void MediaSessionGLib::emitPropertiesChanged(GVariant* parameters)
if (!m_connection)
return;

ensureMprisSessionRegistered();

GUniqueOutPtr<GError> error;
if (!g_dbus_connection_emit_signal(m_connection.get(), nullptr, DBUS_MPRIS_OBJECT_PATH, "org.freedesktop.DBus.Properties", "PropertiesChanged", parameters, &error.outPtr()))
g_warning("Failed to emit MPRIS properties changed: %s", error->message);
@@ -51,6 +51,7 @@ class MediaSessionGLib {
private:
void emitPropertiesChanged(GVariant*);
std::optional<NowPlayingInfo> nowPlayingInfo();
void ensureMprisSessionRegistered();

MediaSessionIdentifier m_identifier;
MediaSessionManagerGLib& m_manager;

0 comments on commit 32442d7

Please sign in to comment.