this post was submitted on 26 Jul 2024
0 points (NaN% liked)

Rust Programming

8127 readers
1 users here now

founded 5 years ago
MODERATORS
 

Hi all,

mpv communities seem to be tiny in lemmy, so I'm sharing it here.

This is a program I made for music control from local network.

You can run it in a computer with some local media files, or youtube links or any other links yt-dlp supports. And then with the server, you can control the media player and the playlist from any devices in your local network. So that you can just show a QR code or something to house guests for parties, or have it bookmarked within family to control the music.

I wanted to make something similar to how youtube app let's you play in TV and such, but my skills were not enough to do that. So I tried a simple alternative that works with computers. In an ideal world, I could make "Play with local mpv server" option come while on other android apps, but I have zero experience in android app development and it looks complicated.

I know some other programs also give option to control media, but I wanted to give it a go with a simple implementation. Making the web-server was a tricky part. Only tutorial from the rust book was useful here as every other web server developement in rust seems to be async ones using libraries so I would have to make a complicated system to communicate with the mpv. Using the simple Tcp connection let me make a thread with mpv instance in the scope. I do need to support https and file uploads and other things, but I haven't had any luck finding a solution that works with simple Tcp connection like in the tutorial. Let me know if you know anything.

Github: https://github.com/Atreyagaurav/local-mpv

top 1 comments
sorted by: hot top controversial new old
[–] [email protected] 0 points 3 months ago* (last edited 3 months ago)

The problem with making a custom web server is that you take responsibility for re-solving all the non-obvious security vulnerabilities. I always try to delegate as much network-facing code as possible to a mature implementation someone else wrote for that reason.

Here's how I'd implement it, based on stuff I've done before:

  1. Start with either Actix Web or Axum for the server itself.
  2. Use std::thread to bring up mpv in a separate thread.
  3. Use an async-capable channel implementation like flume as a bridge between the async and sync worlds.
  4. If the async side needs to wait on the sync side, include the sending side of a tokio::sync::oneshot in the "job order" object your async code drops into the channel and then have the async task await the receiving side. That way, you can have the async task block on the some kind of completion signal from the sync thread without blocking the thread(s) underlying the task executor.