mpv-player/mpv — pull request #693 by deuiore (2014-04-02 19:09:02+00:00)
Title: IPC and slave mode.

Since there weren't much developments in the last few months I decided to take it on myself to experiment a bit.
I don't know how this will be received, given how wm4 regarded my idea to use TCP sockets the other day on IRC. But well, I did more than that.
Also I probably messed up a lot of your coding conventions; so, well, sorry about that.

Anyway, right to the point.

The `osdep/ipc*` files implement a generic interface to communicate to TCP sockets, Unix domain sockets (on *nix) and named pipes (on Windows).
A client (`mpv_handle`) is assigned to each interface and a listener thread which waits for connections is created. Then a new thread is created for each new connection.

This is used by `player/slave*` to build a slave protocol upon it. It basically reflects most of the functions in `libmpv/client.h`, stripped of "mpv_" and only using the "_string" variants.

The protocol is as simple as it can be: the client issues commands in this format (args are in percent-encoding):
```command arg1 arg2**CRLF**```
The server simply replies with strings followed by CRLFs.
Sending an empty string followed by a CRLF tells the server to close the connection.
And invalid command returns `ERR invalid command**CRLF**`.
If a function returns a NULL value, the server returns `NULL**CRLF**`.

The slave mode is enabled by the option `-slave`.
With `-slave-opts` the user can specify the `protocol` (`local` or `tcp`, defaults to `local`), the `host` (defaults to "localhost"), the `port` (defaults to "17771"), the `auth` for the TCP mode or the `path` (defaults to ".mpv_slave") for the Unix domain socket/named pipe mode.

The TCP authentication mechanism is pretty simple (and naive if you want). The server just expects the client to first send the `auth` token specified from the command line followed by a NULL character as soon as it connects, and closes the connection otherwise.

I tried my best to implement the `libmpv/client.h` functions, but I left alone the "event" ones. I mean, would they even be used by the slave protocol? Honestly I'm having a hard time figuring out how they could be implemented with this kind of scheme, or if they would even be of use in this particular case.

Tested on Linux 3.12.13, Windows 7 SP1 and Mac OS X 10.9.2.