~jojo/jucast

efa77d73b6125cfdc5a87b69ce676b64398bc01b — JoJo a month ago 6bdfbc2
Player in own thread, receiving messages to Stop & Play new song
5 files changed, 271 insertions(+), 17 deletions(-)

M Cargo.lock
M Cargo.toml
A README.md
A TODO.org
M src/main.rs
M Cargo.lock => Cargo.lock +220 -3
@@ 8,6 8,12 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"

[[package]]
name = "bytes"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38"

[[package]]
name = "bytes"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad1f8e949d755f9d79112b5bb46938e0ef9d3804a0b16dfab13aafcaa5f0fa72"


@@ 25,6 31,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"

[[package]]
name = "fnv"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"

[[package]]
name = "futures"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 112,7 124,7 @@ dependencies = [
 "futures-sink",
 "futures-task",
 "memchr",
 "pin-project",
 "pin-project 1.0.2",
 "pin-utils",
 "proc-macro-hack",
 "proc-macro-nested",


@@ 120,6 132,32 @@ dependencies = [
]

[[package]]
name = "h2"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b67e66362108efccd8ac053abafc8b7a8d86a37e6e48fc4f6f7485eb5e9e6a5"
dependencies = [
 "bytes 1.0.0",
 "fnv",
 "futures-core",
 "futures-sink",
 "futures-util",
 "http",
 "indexmap",
 "slab",
 "tokio",
 "tokio-util",
 "tracing",
 "tracing-futures",
]

[[package]]
name = "hashbrown"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04"

[[package]]
name = "hermit-abi"
version = "0.1.17"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 129,14 167,94 @@ dependencies = [
]

[[package]]
name = "http"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84129d298a6d57d246960ff8eb831ca4af3f96d29e2e28848dae275408658e26"
dependencies = [
 "bytes 0.5.6",
 "fnv",
 "itoa",
]

[[package]]
name = "http-body"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2861bd27ee074e5ee891e8b539837a9430012e249d7f0ca2d795650f579c1994"
dependencies = [
 "bytes 1.0.0",
 "http",
]

[[package]]
name = "httparse"
version = "1.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9"

[[package]]
name = "httpdate"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47"

[[package]]
name = "hyper"
version = "0.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12219dc884514cb4a6a03737f4413c0e01c23a1b059b0156004b23f1e19dccbe"
dependencies = [
 "bytes 1.0.0",
 "futures-channel",
 "futures-core",
 "futures-util",
 "h2",
 "http",
 "http-body",
 "httparse",
 "httpdate",
 "itoa",
 "pin-project 1.0.2",
 "socket2",
 "tokio",
 "tower-service",
 "tracing",
 "want",
]

[[package]]
name = "indexmap"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fb1fa934250de4de8aef298d81c729a7d33d8c239daa3a7575e6b92bfc7313b"
dependencies = [
 "autocfg",
 "hashbrown",
]

[[package]]
name = "itoa"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736"

[[package]]
name = "jukebox"
version = "0.1.0"
dependencies = [
 "futures",
 "hyper",
 "tokio",
]

[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"

[[package]]
name = "libc"
version = "0.2.81"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 207,11 325,31 @@ checksum = "13bd41f508810a131401606d54ac32a467c97172d74ba7662562ebba5ad07fa0"

[[package]]
name = "pin-project"
version = "0.4.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ffbc8e94b38ea3d2d8ba92aea2983b503cd75d0888d75b86bb37970b5698e15"
dependencies = [
 "pin-project-internal 0.4.27",
]

[[package]]
name = "pin-project"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ccc2237c2c489783abd8c4c80e5450fc0e98644555b1364da68cc29aa151ca7"
dependencies = [
 "pin-project-internal",
 "pin-project-internal 1.0.2",
]

[[package]]
name = "pin-project-internal"
version = "0.4.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65ad2ae56b6abe3a1ee25f15ee605bacadb9a764edaba9c2bf4103800d4a1895"
dependencies = [
 "proc-macro2",
 "quote",
 "syn",
]

[[package]]


@@ 311,8 449,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d258221f566b6c803c7b4714abadc080172b272090cdc5e244a6d4dd13c3a6bd"
dependencies = [
 "autocfg",
 "bytes",
 "bytes 1.0.0",
 "libc",
 "memchr",
 "mio",
 "num_cpus",
 "once_cell",


@@ 334,12 473,90 @@ dependencies = [
]

[[package]]
name = "tokio-stream"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e4cdeb73537e63f98adcd73138af75e3f368ccaecffaa29d7eb61b9f5a440457"
dependencies = [
 "futures-core",
 "pin-project-lite",
 "tokio",
]

[[package]]
name = "tokio-util"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36135b7e7da911f5f8b9331209f7fab4cc13498f3fff52f72a710c78187e3148"
dependencies = [
 "bytes 1.0.0",
 "futures-core",
 "futures-sink",
 "log",
 "pin-project-lite",
 "tokio",
 "tokio-stream",
]

[[package]]
name = "tower-service"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e987b6bf443f4b5b3b6f38704195592cca41c5bb7aedd3c3693c7081f8289860"

[[package]]
name = "tracing"
version = "0.1.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f47026cdc4080c07e49b37087de021820269d996f581aac150ef9e5583eefe3"
dependencies = [
 "cfg-if 1.0.0",
 "pin-project-lite",
 "tracing-core",
]

[[package]]
name = "tracing-core"
version = "0.1.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f50de3927f93d202783f4513cda820ab47ef17f624b03c096e86ef00c67e6b5f"
dependencies = [
 "lazy_static",
]

[[package]]
name = "tracing-futures"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab7bb6f14721aa00656086e9335d363c5c8747bae02ebe32ea2c7dece5689b4c"
dependencies = [
 "pin-project 0.4.27",
 "tracing",
]

[[package]]
name = "try-lock"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642"

[[package]]
name = "unicode-xid"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"

[[package]]
name = "want"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0"
dependencies = [
 "log",
 "try-lock",
]

[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"

M Cargo.toml => Cargo.toml +2 -1
@@ 8,4 8,5 @@ edition = "2018"

[dependencies]
tokio = { version = "1.0", features = ["rt-multi-thread", "process", "time", "macros"] }
futures = "0.3"
\ No newline at end of file
futures = "0.3"
hyper = { version = "0.14", features = ["full"] }
\ No newline at end of file

A README.md => README.md +3 -0
@@ 0,0 1,3 @@
See https://github.com/MayaPosch/NymphCast/ for something similar to
what I want, only mine should not require an app to be used from
guests phones.

A TODO.org => TODO.org +9 -0
@@ 0,0 1,9 @@
* TODO Put on sr.ht or smth
* TODO Upload files to play
  Check out Hyper,
  https://docs.rs/hyper/0.14.2/hyper/struct.Request.html,
  https://stackoverflow.com/questions/40684307/how-can-i-receive-an-uploaded-file-using-a-golang-net-http-server,
  https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file,
  https://wiki.debian.org/DebianMultimedia/PlayerSupport,
  https://github.com/hyperium/hyper/issues/1901,
  https://stackoverflow.com/questions/913626/what-should-a-multipart-http-request-with-multiple-files-look-like

M src/main.rs => src/main.rs +37 -13
@@ 1,27 1,51 @@
#![feature(async_closure)]

// use futures::prelude::*;
use futures::{channel::mpsc, future::Either::*, pin_mut, prelude::*};
use std::{process::Stdio, time::Duration};
use tokio::{process::Command, time::timeout};
use tokio::process::Command;

enum Msg {
    Stop,
    Play(String),
}

#[tokio::main]
async fn main() {
    let play = async move |x| {
        Command::new("mpv")
            .arg(x)
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .kill_on_drop(true)
            .status()
            .await
            .unwrap()
    };
    let (mut tx, mut rx) = mpsc::unbounded();

    // Player thread
    tokio::spawn(async move {
        // TODO: if playing image, then --keep-open and require skip
        let player = future::pending().boxed();
        pin_mut!(player);
        loop {
            match future::select(&mut player, rx.next()).await {
                Left((_exited, _)) => player.set(future::pending().boxed()),
                Right((msg, _)) => match msg.unwrap() {
                    Msg::Stop => player.set(future::pending().boxed()),
                    Msg::Play(url) => player.set(
                        Command::new("mpv")
                            .arg(url)
                            .stdout(Stdio::null())
                            .stderr(Stdio::null())
                            .kill_on_drop(true)
                            .status()
                            .map(Result::unwrap)
                            .boxed(),
                    ),
                },
            }
        }
    });
    let vs = [
        "https://www.youtube.com/watch?v=a8c5wmeOL9o",
        "/home/jojo/Music/Lion/Transformers The Movie/Transformers Theme.mp3",
        "https://www.youtube.com/watch?v=8jZUETfIfNo",
    ];
    for v in &vs {
        timeout(Duration::from_secs(12), play(v)).await.ok();
        tx.send(Msg::Play(v.to_string())).await.unwrap();
        std::thread::sleep(Duration::from_secs(4));
        tx.send(Msg::Stop).await.unwrap();
        std::thread::sleep(Duration::from_secs(4));
    }
}