Skip to content

Commit 9b86c07

Browse files
committed
Expand README
1 parent 8bbb957 commit 9b86c07

File tree

1 file changed

+85
-11
lines changed

1 file changed

+85
-11
lines changed

README.md

+85-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
# mpv-web-remote
22

3-
Simple web remote control for the [mpv media player](https://mpv.io/), using the [JSON IPC interface](https://mpv.io/manual/stable/#json-ipc).
3+
Simple zero-dependency web remote for the [mpv media player](https://mpv.io/), using the [JSON IPC interface](https://mpv.io/manual/stable/#json-ipc).
44

55
![Screenshot](screenshot.png)
66

7-
A single binary on the host machine connects to the mpv unix socket and exposes a remote control via a web server, that can be accessed from any device on the same network (e.g. a smartphone).
8-
9-
The web app announces itself via the [media session API](https://developer.mozilla.org/en-US/docs/Web/API/Media_Session_API), allowing control even when the app is in the background, or from connected devices (e.g. a smartwatch).
7+
A single binary on the host machine connects to the mpv [unix socket](https://en.wikipedia.org/wiki/Unix_domain_socket) and exposes a remote control via a web server, which can be accessed from any device on the same network (e.g. a smartphone). The web app announces itself via the [media session API](https://developer.mozilla.org/en-US/docs/Web/API/Media_Session_API), allowing control even when the app is in the background, or from connected devices (e.g. a smartwatch).
108

119
![Screenshot](mediasession.png)
1210

1311
The client code can also be used separately to interface with mpv from Rust in other use cases.
1412

15-
See https://c.pgdm.ch/eps-projects/mpv-web-remote
13+
## Features
14+
15+
For now, only the following basic functions are supported:
16+
17+
- Play/pause
18+
- Toggle full-screen
19+
- Rewind 10 seconds (can be changed with the `--rewind-s` option)
20+
- Visualize progress (current time, total time, percentage)
21+
- Preview (updated every 3 seconds)
22+
- Seek (by clicking on the progress bar)
1623

1724
## Installation
1825

@@ -37,7 +44,8 @@ The web server will then be enabled shortly after a new instance of mpv is start
3744

3845
With the default parameters, the control interface will be accessible on <http://[ip]:3000>.
3946

40-
Warning: there is currently no authentication.
47+
> [!WARNING]
48+
> There is currently no authentication.
4149
4250
### Alternative: Manual installation
4351

@@ -66,9 +74,75 @@ Options:
6674
-h, --help Print help
6775
```
6876

69-
## Other projects
77+
## Implementation details
78+
79+
There are two components:
80+
81+
- the interface with mpv
82+
- the web server
83+
84+
The web server is straightforward and is implemented with [axum](https://docs.rs/axum/latest/axum/). For page interactions, we simply use [jQuery](https://jquery.com/).
85+
86+
The mpv interface takes the form
87+
88+
```rust
89+
impl Mpv {
90+
pub fn connect(socket: impl AsRef<Path>) -> Result<Self, Error>;
91+
pub fn send(&mut self, mut request: Request) -> Result<Response, Error>;
92+
pub fn wait_event(&self, filter: impl Fn(&Event) -> bool);
93+
}
94+
```
95+
96+
The `connect` constructor connects to the socket and creates a thread responsible for reading replies and events from the servers into a buffer. In particular, we are robust against changes to this implementation detail:
97+
98+
> "Currently, the mpv-side IPC implementation does not service the socket while a command is executed and the reply is written. It is for example not possible that other events, that happened during the execution of the command, are written to the socket before the reply is written.
99+
>
100+
> This might change in the future. The only guarantee is that replies to IPC messages are sent in sequence."
101+
102+
The `send` method writes a request to the socket, serialized using [`serde_json`](https://docs.rs/serde_json/latest/serde_json/). It then blocks until the server responds to that request.
103+
104+
A selection of [commands](https://mpv.io/manual/stable/#list-of-input-commands) is implemented:
105+
106+
```rust
107+
impl Request {
108+
pub fn playback_time() -> Self;
109+
pub fn get_property(property: &str) -> Self;
110+
pub fn seek(target: f32, flags: &str) -> Self;
111+
pub fn set_property<T: Into<serde_json::Value>>(property: &str, value: T) -> Self;
112+
pub fn show_text(text: &str) -> Self;
113+
pub fn observe_property(id: i64, property: &str) -> Self;
114+
pub fn screenshot<P: AsRef<Path> + ?Sized>(filename: &P) -> Self;
115+
}
116+
```
117+
118+
A generic response takes the form
119+
120+
```rust
121+
#[derive(Deserialize, Debug)]
122+
pub struct Response {
123+
pub request_id: i64,
124+
pub data: Option<serde_json::Value>,
125+
pub error: String,
126+
}
127+
```
128+
129+
and can be downcast to the expected data type
130+
131+
```rust
132+
impl Response {
133+
pub fn into_inner<T: DeserializeOwned>(self) -> Result<T, Error>;
134+
}
135+
```
136+
137+
This could also be encapsulated into higher-level methods (e.g. `get_playback_time() -> f64`).
138+
139+
Finally, the `wait_event` method simply blocks until an event occurs. This is for example useful to only trigger a screenshot at the new position after a seek has finished.
140+
141+
## Existing solutions
142+
143+
A quick search will reveal two "mpv remote" Android apps using the aforementioned mpv control interface. Naturally, they also require an additional component to run on the host machine to expose the API to the app.
144+
145+
- <https://github.com/husudosu/mpv-remote-app>, where a Node.JS server (<https://github.com/husudosu/mpv-remote-node>) runs as an mpv plugin and provides an API over HTTP.
146+
- <https://github.com/mcastorina/mpv-remote-app>, where a separate Python server exposes an API over a network socket.
70147

71-
- Android apps:
72-
- <https://github.com/husudosu/mpv-remote-app> with a Node.JS server.
73-
- <https://github.com/mcastorina/mpv-remote-app> with a Python server.
74-
- https://crates.io/crates/mpvipc: fairly complete Rust interface for the mpv control interface.
148+
The [mpvipc](https://crates.io/crates/mpvipc) crate provides a similar and more complete interface, but the above was an opportunity to experiment with a different implementation, under a more permissive license.

0 commit comments

Comments
 (0)