Skip to content

player/main: implement single instance mode - #18932

Open
guidocella wants to merge 10 commits into
mpv-player:masterfrom
guidocella:single-instance
Open

guidocella wants to merge 10 commits into
mpv-player:masterfrom
guidocella:single-instance

Conversation

@guidocella

Copy link
Copy Markdown
Contributor

If another instance is listening on the --input-ipc-server path, send it the files with loadfile append-play and exit instead of playing them.

Add mp_ipc_send() to connect to an IPC server and send it commands. It waits until the server has processed them before disconnecting, because the Unix server stops processing commands when it can't write their replies.

Fixes #18897.

Written by Claude Opus 5.5. Not tested on Windows.

@kasper93 kasper93 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

umpv should be rewrite to call mpv with appropriate options and remove the comunication part that lives in mpv now.

Comment thread DOCS/man/options.rst Outdated

``--single-instance=<yes|no>``
If another mpv instance is listening on the ``--input-ipc-server`` path,
append the files to its playlist with ``loadfile <file> append-play`` and

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has to be configurable, one may want to append-play other replace and so on.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Funny cause I had all loadfile options but removed them because they didn't seem necessary 😂 But do you want to settle #17458 first so this won't have to be changed later as well?

Comment thread DOCS/man/options.rst Outdated

Put this in ``mpv.conf`` to play every file in the same window::

input-ipc-server=/tmp/mpv-socket

@kasper93 kasper93 Dec 2, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't steal the ipc socket. It should be dedicated socket. Most importantly, we shouldn't make every mpv instance run unrestricted ipc socket. It should be either dedicated secure and restricted ipc or some other solution.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if I want a single instance for videos and one for music that use different sockets?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then you configure different "single instance" socket.

@guidocella

Copy link
Copy Markdown
Contributor Author

umpv should be rewrite to call mpv with appropriate options and remove the comunication part that lives in mpv now.

Why is umpv still needed?

@kasper93

kasper93 commented Dec 2, 2026

Copy link
Copy Markdown
Member

umpv should be rewrite to call mpv with appropriate options and remove the comunication part that lives in mpv now.

Why is umpv still needed?

For backward compatibility for users who want to use umpv.

@guidocella

Copy link
Copy Markdown
Contributor Author

For backward compatibility for users who want to use umpv.

That would break it for Linux users who download it without using git master though.

@kasper93

kasper93 commented Dec 2, 2026

Copy link
Copy Markdown
Member

For backward compatibility for users who want to use umpv.

That would break it for Linux users who download it without using git master though.

Why does this matter?

@kasper93 kasper93 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of generating loadfile commands, how about reusing the drag and drop loader? Split handle_dnd() in input/dnd.c into the --drag-and-drop handling and a loader that takes files and an action. The second instance sends {"action": "append", "files": [...]}, and the single-instance calls the code with that.

This will cut a lot of duplication.

Comment thread DOCS/man/options.rst Outdated
the FD value is the same (but the string is different e.g. due to
whitespace). This is not a bug.

``--single-instance=<no|replace|append|append-play|insert-next|insert-next-play>``

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think options could be split into --instance-name=<...> and --on-instance-action or some better name, not sure what works. Then we could have multiple multiple instances for music/images.

Move loading files with an action into mp_load_files(), so that it can be
reused for files received from other instances.
@guidocella guidocella changed the title player/main: add --single-instance player/main: implement single instance mode Dec 2, 2026
@kasper93

kasper93 commented Dec 2, 2026

Copy link
Copy Markdown
Member

You also need to split the commits into separate changes. There is a lot different files and functions changes in "add" commit.

Like node_map_add_string(), but for arrays.
Create and bind the socket in mp_init_ipc() instead of in the IPC
thread, so that the caller knows whether it is listening. Factor
building the socket address and creating the listening socket out of
the thread, so that they can be reused for other sockets.
Create the first pipe instance in mp_init_ipc() instead of in the IPC
thread, so that the caller knows whether it is listening. Since creating
the first instance with FILE_FLAG_FIRST_PIPE_INSTANCE is atomic, this
can also be used to determine which of several processes listens.

Factor out building the pipe path, creating pipe instances and starting
the server, so that they can be reused for other pipes.
This will be used to check the user of the process at the other end of
a pipe.
Load the files in a message received from another instance using the
drag and drop loader.
If another instance with the same name is running, send it a message
with files to load. Otherwise, listen for messages from instances started
later.

The socket is created in $XDG_RUNTIME_DIR, or in the cache directory if
it is not set. Both ends verify that the peer runs as the same user. A
lock file serializes startup, so that of multiple instances started at
the same time, e.g. by a file manager opening several files, only one
listens and the others send their files to it.

Nothing but a list of files to load is accepted over the socket.
Use a named pipe unique to the user and session, with the existing
restricted security descriptor. Creating its first instance is atomic,
so of multiple instances started at the same time only one listens. The
client verifies that the server runs as the same user, and doesn't
allow it to impersonate the client.
If another instance with the same --instance-name is running, send it
the files and exit instead of playing them. Otherwise, accept files from
instances with the same name started later, and load them like dropped
files according to --instance-action. Different names allow e.g.
separate instances for videos and music.

This doesn't need --input-ipc-server, so it works without further
configuration and can be enabled by default.
mpv can now send files to an existing instance by itself, so just run it
with --instance-name=umpv. This also fixes files being played in separate
instances when umpv is run multiple times at once.

UMPV_SOCKET_DIR is no longer used.
@Akemi

Akemi commented Dec 2, 2026

Copy link
Copy Markdown
Member

Instead of generating loadfile commands, how about reusing the drag and drop loader? Split handle_dnd() in input/dnd.c into the --drag-and-drop handling and a loader that takes files and an action. The second instance sends {"action": "append", "files": [...]}, and the single-instance calls the code with that.

This will cut a lot of duplication.

on #18225 (and #17921) we argued about not dissecting DnD for loading files. did the opinion on that change now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mpv windows single instance mode

3 participants