Skip to content

Commit

Permalink
input/ipc-win: support fd:// for --input-ipc-client
Browse files Browse the repository at this point in the history
This makes --input-ipc-client work on Windows.

To use this feature, a parent process needs to create a connected named pipe,
wrap the server handle in a CRT fd, and then spawn mpv as a child process
with the fd as the --input-ipc-client parameter.
The process can then communicate through the client handle.

The named pipe must be created duplex with overlapped IO and inheritable
handles.
  • Loading branch information
na-na-hi authored and kasper93 committed Jul 29, 2024
1 parent 87686a8 commit bb0932a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
6 changes: 5 additions & 1 deletion DOCS/man/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4243,7 +4243,11 @@ Input

.. note::

Does not and will not work on Windows.
To use this option on Windows, the fd must refer to a wrapped
(created by ``_open_osfhandle``) named pipe server handle with a client
already connected. The named pipe must be created duplex with overlapped
IO and inheritable handles. The program communicates with mpv through
the client handle.

.. warning::

Expand Down
19 changes: 19 additions & 0 deletions input/ipc-win.c
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,25 @@ struct mp_ipc_ctx *mp_init_ipc(struct mp_client_api *client_api,
.client_api = client_api,
};

if (opts->ipc_client && opts->ipc_client[0]) {
int fd = -1;
if (strncmp(opts->ipc_client, "fd://", 5) == 0) {
char *end;
unsigned long l = strtoul(opts->ipc_client + 5, &end, 0);
if (!end[0] && l <= INT_MAX)
fd = l;
}
if (fd < 0) {
MP_ERR(arg, "Invalid IPC client argument: '%s'\n", opts->ipc_client);
} else {
HANDLE h = (HANDLE)_get_osfhandle(fd);
if (h && h != INVALID_HANDLE_VALUE && (intptr_t)h != -2)
ipc_start_client_json(arg, -1, h);
else
MP_ERR(arg, "Invalid IPC client fd: '%d'\n", fd);
}
}

if (!opts->ipc_path || !*opts->ipc_path)
goto out;

Expand Down
2 changes: 0 additions & 2 deletions options/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -881,9 +881,7 @@ static const m_option_t mp_opts[] = {
{"input-terminal", OPT_BOOL(consolecontrols), .flags = UPDATE_TERM},

{"input-ipc-server", OPT_STRING(ipc_path), .flags = M_OPT_FILE},
#if HAVE_POSIX
{"input-ipc-client", OPT_STRING(ipc_client)},
#endif

{"screenshot", OPT_SUBSTRUCT(screenshot_image_opts, screenshot_conf)},
{"screenshot-template", OPT_STRING(screenshot_template)},
Expand Down

0 comments on commit bb0932a

Please sign in to comment.