Ability to control the MPRIS Service Name

181 views
Skip to first unread message

Damien Simonin Feugas

unread,
Dec 9, 2020, 3:38:29 AM12/9/20
to media-dev

Hello!

As a developer of an Electron-based Music application, I'm relying on the MediaSession API to support OS media keys and controls in a portable way. As you can guess, media keys are critical to the app.

This works pretty well, and I'm very grateful for it.

However, I've encounter a serious blocker while publishing my application as a Snap.
As you know, Snap is the prefered way to distribute desktop applications in the Ubuntu ecosystem (and broader).

Snap has a safe and strict containment model which impose to declare plugs and slot, kind of the way Android platform does. Developer must provide a yaml descriptor indicating the desired MPRIS name.

In Chromium source code, the MPRIS name is hardcoded. It can be changed from "chromium" to "chrome" when building the browser. But since I'm using Electron, I don't built it myself, and it's set to chromium.

The issue is: Snap moderators are rejecting my application as they would like me to use an MPRIS name in line with my app name (melodie).

Would it be reasonable to allow Chromium user set their MPRIS name with some flag?

I'm no good at C/C++, but I'm keen to give it a try, if you think this could be a valid solution.

Dale Curtis

unread,
Dec 9, 2020, 1:11:32 PM12/9/20
to Damien Simonin Feugas, Tommy Steimel, media-dev
+Tommy Steimel who is familiar with this code.

- dale

--
You received this message because you are subscribed to the Google Groups "media-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to media-dev+...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/media-dev/f3487449-dbbe-4764-8451-6d692d1af971n%40chromium.org.

Tommy Steimel

unread,
Dec 16, 2020, 12:18:27 PM12/16/20
to Dale Curtis, Damien Simonin Feugas, media-dev
Hi Damien,

Sorry for the delay. I'm just getting around to looking into this this morning, and I think I have a relatively easy fix for this (that will use the app name you give Electron), so I'll try to get that in today and I'll ping this thread again when that happens. Not sure how long it'll be before it's released and Electron updates to use that version though.

Thanks for pointing out this issue!

- Tommy

Damien Simonin Feugas

unread,
Dec 19, 2020, 3:19:37 AM12/19/20
to media-dev, Tommy Steimel, Damien Simonin Feugas, media-dev, Dale Curtis
Hi Tommy.

That is great to ear!
In the meantime, I did a (half) successful experiment.

On Electron side, there is a way to set flags for Chromium.
On Chromium side, here is a draft:

--- a/chrome/common/chrome_switches.cc
+++ b/chrome/common/chrome_switches.cc
@@ -355,6 +355,10 @@ const char kMakeDefaultBrowser[]            = "make-default-browser";
 // messages. Useful when running against a non-prod management server.
 const char kMonitoringDestinationID[]       = "monitoring-destination-id";
 
+// Allows setting a different MPRIS name when connecting to DBUS for Linux
+// Media intefration. Mostly used in Electron apps.
+const char kMprisName[]                     = "mpris-name";
+
 // Requests a native messaging connection be established between the native
 // messaging host named by this switch and the extension with ID specified by
 // kNativeMessagingConnectExtension.
diff --git a/chrome/common/chrome_switches.h b/chrome/common/chrome_switches.h
index 7dfdfc348a..682442413c 100644
--- a/chrome/common/chrome_switches.h
+++ b/chrome/common/chrome_switches.h
@@ -120,6 +120,7 @@ extern const char kLaunchSimpleBrowserSwitch[];
 extern const char kLoadMediaRouterComponentExtension[];
 extern const char kMakeDefaultBrowser[];
 extern const char kMonitoringDestinationID[];
+extern const char kMprisName[];
 extern const char kNativeMessagingConnectHost[];
 extern const char kNativeMessagingConnectExtension[];
 extern const char kNativeMessagingConnectId[];
diff --git a/components/system_media_controls/linux/system_media_controls_linux.cc b/components/system_media_controls/linux/system_media_controls_linux.cc
index 0d475be5e7..af07dc5433 100644
--- a/components/system_media_controls/linux/system_media_controls_linux.cc
+++ b/components/system_media_controls/linux/system_media_controls_linux.cc
@@ -10,11 +10,13 @@
 
 #include "base/bind.h"
 #include "base/callback_helpers.h"
+#include "base/command_line.h"
 #include "base/memory/singleton.h"
 #include "base/process/process.h"
 #include "base/strings/string_number_conversions.h"
 #include "base/strings/utf_string_conversions.h"
 #include "build/branding_buildflags.h"
+#include "chrome/common/chrome_switches.h"
 #include "components/dbus/properties/dbus_properties.h"
 #include "components/dbus/properties/success_barrier_callback.h"
 #include "components/dbus/thread_linux/dbus_thread_linux.h"
@@ -41,15 +43,21 @@ namespace {
 
 constexpr int kNumMethodsToExport = 11;
 
+// Returns the Mpris Service Name ID base on current process pid and flags..
+std::string GetMPrisServiceNameID() {
+  #if BUILDFLAG(GOOGLE_CHROME_BRANDING)
+  std::string serviceName = "chrome"
+  #else
+  std::string serviceName = "chromium";
+  #endif
+  if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kMprisName)) {
+    serviceName = base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(switches::kMprisName);
+  }
+  return std::string(kMprisAPIInterfaceName) + "." + serviceName + ".instance" + base::NumberToString(base::Process::Current().Pid());
+}
+
 }  // namespace
 
-#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
-const char kMprisAPIServiceNamePrefix[] =
-    "org.mpris.MediaPlayer2.chrome.instance";
-#else
-const char kMprisAPIServiceNamePrefix[] =
-    "org.mpris.MediaPlayer2.chromium.instance";
-#endif
 const char kMprisAPIObjectPath[] = "/org/mpris/MediaPlayer2";
 const char kMprisAPIInterfaceName[] = "org.mpris.MediaPlayer2";
 const char kMprisAPIPlayerInterfaceName[] = "org.mpris.MediaPlayer2.Player";
@@ -60,8 +68,7 @@ SystemMediaControlsLinux* SystemMediaControlsLinux::GetInstance() {
 }
 
 SystemMediaControlsLinux::SystemMediaControlsLinux()
-    : service_name_(std::string(kMprisAPIServiceNamePrefix) +
-                    base::NumberToString(base::Process::Current().Pid())) {}
+    : service_name_(GetMPrisServiceNameID()) {}
 
 SystemMediaControlsLinux::~SystemMediaControlsLinux() {
   if (bus_) {


I hope it'll help!

Damien Simonin Feugas

unread,
Jan 19, 2021, 2:31:53 PM1/19/21
to media-dev, Damien Simonin Feugas, Tommy Steimel, media-dev, Dale Curtis
Hello Tommy, happy new year!

Any progress on this?
Is there anything I could do to help?

Cheers!

Tommy Steimel

unread,
Jan 19, 2021, 3:09:01 PM1/19/21
to Damien Simonin Feugas, media-dev, Dale Curtis
Hi Damien,

I have two CLs out for review and when they land it will fix this. I'll ping the reviewers today since it's been a while

Thanks,
Tommy

Tommy Steimel

unread,
Jan 26, 2021, 4:19:47 PM1/26/21
to Damien Simonin Feugas, media-dev, Dale Curtis
They're on 90.0.4397.0. Note that stable is currently at 88 and 90 won't be stable until April

Damien Simonin Feugas

unread,
Jan 28, 2021, 12:36:03 PM1/28/21
to media-dev, Tommy Steimel, media-dev, Dale Curtis, Damien Simonin Feugas
Good evening Tommy.
That's a great news! Thanks a million.

I'll now keep this topic live on the electron side.
Message has been deleted

Tommy Steimel

unread,
Sep 28, 2021, 3:53:02 PM9/28/21
to Damien Simonin Feugas, media-dev, Dale Curtis, Tom Anderson
Hi Damien,

It's been a while so I'm trying to remember how this was intended to work. As best as I can tell, I think I had assumed that Electron was doing things with Chrome strings that it doesn't seem like they are actually doing (since this fix doesn't seem to be working). That's my fault for trying to solve this without knowing a lot about Electron. I'm trying to figure out if there's something that Electron is doing that we can take advantage of but I don't see anything.

That said, I think that given that we know you can change command line arguments for Electron, it makes sense to me that we could try your original suggestion that we add a command line argument to affect MPRIS name. I want to be certain we get this right this time though given how long you've already had to wait.

Adding thomasanderson@ to cc as the other owner of Chromium MPRIS: do you think it makes sense to add a command line flag for MPRIS name, or are there problems I'm not seeing? Alternatively, do you have any other ideas on how we could solve this?

Thanks,
Tommy

On Thu, Sep 23, 2021 at 1:04 PM Damien Simonin Feugas <damien...@gmail.com> wrote:
Hello Tommy!

Time has passed, and now Electron has latest version of chromium embedded. I'm not trying to change the mpris-name of my application from chromium to melodie.
Unfortunately, I can't find how to do such thing!

If I'm not mistaken, the Mpris name sent to DBus derives from SystemMediaControls::Create()
The only place where it is initialized is here,

>   system_media_controls_ = system_media_controls::SystemMediaControls::Create(
      media::AudioManager::GetGlobalAppName());


And it seems this GlobalAppName is set from translations:

>  media::AudioManager::SetGlobalAppName(
      l10n_util::GetStringUTF8(IDS_SHORT_PRODUCT_NAME));

How can I set this when starting chromium? Or maybe there's another way?

Tom Anderson

unread,
Sep 28, 2021, 3:53:08 PM9/28/21
to Tommy Steimel, Damien Simonin Feugas, media-dev, Dale Curtis
I'm wondering if there's already some way to get the name of the app (chromium/chrome/melodie/etc).  If there's not, then I'm okay with making this controllable with a command line switch.  We may want to make the switch generic enough for it to be used in other contexts too.

Damien Feugas

unread,
Sep 28, 2021, 3:53:12 PM9/28/21
to Tom Anderson, Tommy Steimel, media-dev, Dale Curtis
Good evening

Indeed, as an electron user, there's nothing I can tweak in V8 but command line switches. When deploying an electron app, Chromium is pre-built, so there's nothing I could configure that would involve building from the sources.
--
-- Damien Simonin Feugas

Tommy Steimel

unread,
Oct 5, 2021, 11:17:01 AM10/5/21
to Damien Feugas, Tom Anderson, media-dev, Dale Curtis, Olga Sharonova
Re: already some way to get the name of the app: I was hoping that'd be media::AudioManager::GetGlobalAppName(), but it seems Electron isn't doing anything to make that return the given app name there.

That said, should this potential command line switch control what Chrome sets in media::AudioManager::SetGlobalAppName()? Or is that too strong? +olka@ to cc as an audio owner. IIUC the only other thing this would affect besides MPRIS is //media/audio/pulse.


Reply all
Reply to author
Forward
0 new messages