Commit Graph

42 Commits

Author SHA1 Message Date
cvzi f5eb7ea1a9 json: unify json_parse depth to MAX_JSON_DEPTH=50 2023-07-08 11:36:15 +02:00
Thomas Weißschuh 9efce6d4ae various: drop unused #include "config.h"
Most sources don't need config.h.
The inclusion only leads to lots of unneeded recompilation if the
configuration is changed.
2023-02-20 14:21:18 +00:00
wm4 88194d05d3 ipc: fix recently added memory leak
Sucks.
2020-03-27 00:07:03 +01:00
wm4 218d6643e9 client API, lua, ipc: unify event struct return
Both Lua and the JSON IPC code need to convert the mpv_event struct (and
everything it points to) to Lua tables or JSON.

I was getting sick of having to make the same changes to Lua and IPC. Do
what has been done everywhere else, and let the core handle this by
going through mpv_node (which is supposed to serve both Lua tables and
JSON, and potentially other scripting language backends). Expose it as
new libmpv API function.

The new API is still a bit "rough" and support for other event types
might be added in the future.

This silently adds support for the playlist_entry_id fields to both Lua
and JSON IPC.

There is a small API change for Lua; I don't think this matters, so I
didn't care about compatibility. The new code in client.c is mashed up
from the Lua and the IPC code. The manpage additions are moved from the
Lua docs, and made slightly more "general".

Some danger for unintended regressions both in Lua and IPC. Also damn
these node functions suck, expect crashes due to UB.

Not sure why this became more code instead of less compared to before
(according to the diff stat), even though some code duplication across
Lua and IPC was removed. Software development sucks.
2020-03-21 19:33:48 +01:00
wm4 c418aa3807 ipc: allow sending commands with named arguments
This has been part of the libmpv for a while, so the implementation in
the IPC code is quite simple: just pass the mpv_node representing the
value of the "command" field without further checks to
mpv_command_node().

The only problem are the IPC-specific commands, which essentially have
their own dispatch mechanism. They expect an array. I'm not going to
rewrite the dispatch mechanism, so these still work only with an array.
I decided make the other case explicit with cmd==NULL. (I could also
have set cmd=="", which would have avoided changing each if condition
since "" matches no existing command, but that felt dirty.)
2020-02-24 00:31:46 +01:00
wm4 b05550fe55 ipc: implement asynchronous commands
I decided to make this explicit. The alternative would have been making
all commands asynchronous always, like a small note in the manpage
threatened. I think that could have caused compatibility issues.

As a design decision, this does not send a reply if an async command
started. This could be a good or bad idea, but in any case, it will make
async command look almost like synchronous ones, except they don't block
the IPC protocol.
2020-02-24 00:14:54 +01:00
wm4 fc574ee563 ipc: some user-visible changes to prepare for making all commands async
I wanted to put all commands through mpv_command_node_async() instead of
mpv_command_node(). Using synchronous commands over a synchronous
transport doesn't make sense anyway.

This would have used the request_id field in IPC requests as reply ID
for the async commands. But the latter need to be [u]int64, while the
former can be any type. To avoid that we need an extra lookup table for
mapping reply IDs to request_id values, we now require that request_id
fields are integers.

Since this would be an incompatible change, just deprecate non-integers
for now, and plan the change for a later time.
2018-05-24 19:56:34 +02:00
wm4 11c74068b2 ipc: cosmetic: switch a negated if/else 2018-05-24 19:56:34 +02:00
wm4 b44ea70209 ipc: alias set_property_string to set_property
The only effective difference is that the former explicitly checks
whether the JSON value type is string, and errors out if not. The rest
is exactly the same (mpv_set_property_string is mpv_set_property with
MPV_FORMAT_STRING).

It seems silly to keep this, so just remove it.
2018-05-24 19:56:34 +02:00
wm4 1157f07c5b node: move a mpv_node helper from ipc.c to shared code
This particular one is needed in a following commit.
2018-05-24 19:56:34 +02:00
wm4 9f268d7613 ipc: raise json nesting limit
Fixes the issue pointed out in #4394.
2017-05-03 20:47:11 +02:00
wm4 f30c5d09f4 client API: turn mpv_suspend() and mpv_resume() into stubs
As threatened by the API changes document.

This commit also removes or stubs equivalent calls in IPC and Lua
scripting.

The stubs are left to maintain ABI compatibility. The semantics of the
API functions have been close enough to doing nothing that this probably
won't even break existing API users. Probably.
2016-11-22 15:54:44 +01:00
James Ross-Gowan 5bf473d5ca ipc: add Windows implementation with named pipes
This implements the JSON IPC protocol with named pipes, which are
probably the closest Windows equivalent to Unix domain sockets in terms
of functionality. Like with Unix sockets, this will allow mpv to listen
for IPC connections and handle multiple IPC clients at once. A few cross
platform libraries and frameworks (Qt, node.js) use named pipes for IPC
on Windows and Unix sockets on Linux and Unix, so hopefully this will
ease the creation of portable JSON IPC clients.

Unlike the Unix implementation, this doesn't share code with
--input-file, meaning --input-file on Windows won't understand JSON
commands (yet.) Sharing code and removing the separate implementation in
pipe-win32.c is definitely a possible future improvement.
2016-03-23 23:15:20 +11:00
wm4 f9f3175487 ipc: fix uninitialized field
The sockaddr_un.sun_len field was not initialized. It seems our API use
is correct by simply making sure it's 0.

Fixes CID 1350075.
2016-02-12 16:11:05 +01:00
wm4 f352669157 Change 3 more files to LGPL 2016-01-20 15:43:56 +01:00
wm4 a56a7f3e8c ipc: fix undefined behavior in some error cases
goto jumping over an initialization.
2015-07-06 00:08:29 +02:00
Preston Hunt 029da5abce ipc: add request_id to json
If the request contains a "request_id", copy it back into the
response. There is no interpretation of the request_id value by mpv; the
only purpose is to make it easier on the requester by providing an
ability to match up responses with requests.

Because the IPC mechanism sends events continously, it's possible for
the response to a request to arrive several events after the request was
made. This can make it very difficult on the requester to determine
which response goes to which request.
2015-07-03 22:26:54 +02:00
wm4 47d69f366b ipc: avoid SIGPIPE
Until now, we just blocked SIGPIPE globally. Fix it properly to get away
from it.

MSG_NOSIGNAL should be widely available and is part of the POSIX.1-2008
standard. But it's not available on OSX, because Apple is both evil and
retarded. Thus we continue to ignore the problem on such shitty systems.
2015-05-12 22:54:11 +02:00
wm4 19ab5f7943 ipc: silence some common info messages
They are not really interesting. At least one user complained about the
noise resulting from use with shell scripts, which connect and
disconnect immediately.
2015-05-05 01:11:16 +02:00
wm4 6e73b4dac7 input: handle closed pipe correctly 2015-02-26 22:09:01 +01:00
wm4 f47beb1f07 input: if FD is not writable, just don't write to the FD
This is for the case if the FD is a uni-directional pipe.
2015-02-26 22:09:00 +01:00
wm4 7b02c79a23 input: allow passing FDs to --input-file 2015-02-26 22:09:00 +01:00
wm4 a22de99544 input: avoid creating world-writeable file with --input-unix-socket
This requires fchmod(), which is not necessarily available everywhere.
It also might not work at all. (It does work on Linux.)
2015-02-26 21:44:35 +01:00
wm4 32b56c56ba ipc: put playback core to sleep while dequeuing commands
Happens to fix #1581 due to an unfortunate interaction with the way the
VO does not react to commands for a while if a video frame is queued.
Slightly improves other situations as well, if the client spams mpv with
commands during playback.
2015-02-13 21:25:09 +01:00
wm4 51abca8afd ipc: add enable_event and disable_event commands
This was requested.
2014-12-24 14:32:02 +01:00
wm4 98a80884da ipc: report some user errors better
Using the IPC with a program, it's not often obvious that a newline must
be sent to terminate a command. Print a warning if the connection is
closed while there is still uninterpreted data in the buffer.

Print the OS reported error if reading/writing the socket fails. Print
an erro if JSON parsing fails.

I considered silencing write errors if the write end is closed (EPIPE),
because a client might send a bunch of commands, and then close the
socket without wanting to read the reply. But then, mpv disconnects
without reading further commands that might still be buffered, so it's
probably a good idea to always print the error.
2014-12-24 13:18:00 +01:00
wm4 756adee999 client API: be more lenient about mpv_suspend/resume mismatches
Before this commit, this was defined to trigger undefined behavior. This
was nice because it required less code; but on the other hand, Lua as
well as IPC support had to check these things manually. Do it directly
in the API to avoid code duplication, and to make the API more robust.
(The total code size still grows, though...)

Since all of the failure cases were originally meant to ruin things
forever, there is no way to return error codes. So just print the
errors.
2014-12-15 14:44:47 +01:00
wm4 2a017734a5 lua, ipc: remove leftovers
MPV_EVENT_SCRIPT_INPUT_DISPATCH is now unused/deprecated.

Also remove a debug-print from defaults.lua.
2014-11-24 10:33:55 +01:00
wm4 4704fab82c ipc: fix confusion of write() return value and errno
Found by Coverity.
2014-11-21 05:18:05 +01:00
wm4 c920a3920e ipc: make sure --input-file=/dev/stdin always works
It's not necessarily available on Unix systems other than Linux (sigh).
2014-11-07 09:50:29 +01:00
wm4 dbc41ea3bb ipc: make it possible to receive log messages
The receiving part was implemented, but since no messages are enabled
by default, it couldn't be used.
2014-11-01 15:45:41 +01:00
wm4 de59b87609 ipc: add a command to retrieve API version 2014-11-01 15:45:41 +01:00
wm4 a1e7daf942 ipc: verify resume/suspend commands
Calling mpv_resume() too often is considered an API usage violation,
and will trigger an internal assertion somewhere.
2014-11-01 15:45:40 +01:00
wm4 b330f16fed input: resolve ~ and similar for --input-file
Because why not.
2014-10-24 21:28:07 +02:00
wm4 9ba6641879 Set thread name for debugging
Especially with other components (libavcodec, OSX stuff), the thread
list can get quite populated. Setting the thread name helps when
debugging.

Since this is not portable, we check the OS variants in waf configure.
old-configure just gets a special-case for glibc, since doing a full
check here would probably be a waste of effort.
2014-10-19 23:48:40 +02:00
wm4 d089772b69 ipc: skip empty and commented lines 2014-10-19 22:34:37 +02:00
wm4 cf627fd3de ipc: accept both JSON and "old" commands
Minimizes the differences between --input-file and --input-unix-socket.
2014-10-19 22:34:37 +02:00
wm4 f8f0098560 ipc: fix minor error cleanup issues
The ipc_thread can exit any time, and will free the mp_ipc_ctx when
doing this, leaving a dangling pointer. This was somewhat handled in the
original commit by setting mpctx->ipc_ctx to NULL when the thread
exited, but that was still a race condition.

Handle it by freeing most things after joining the ipc_thread. This
means some resources will not be freed until player exit, but that
should be ok (it's an exceptional error situation).

Also, actually close the pipe FDs in mp_init_ipc() on another error
path.
2014-10-19 21:04:38 +02:00
wm4 f4c589418c ipc: decouple from MPContext
Just a minor refactor to keep unneeded dependencies on the core low.
2014-10-19 20:44:29 +02:00
wm4 70cc42655d ipc: fix a small memory leak 2014-10-17 22:31:14 +02:00
Alessandro Ghedini 3deb6c3d4f input: implement --input-file on unix using the IPC support 2014-10-17 20:47:43 +02:00
Alessandro Ghedini 13039414f5 input: implement JSON-based IPC protocol 2014-10-17 20:46:31 +02:00