Skip to content
Snippets Groups Projects
Commit a6d8ce55 authored by Patrick Auernig's avatar Patrick Auernig :coffee:
Browse files

Add small JSON API for querying active streams

parent 24b09494
No related branches found
No related tags found
No related merge requests found
......@@ -479,6 +479,8 @@ dependencies = [
"native-tls 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rml_rtmp 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.34 (registry+https://github.com/rust-lang/crates.io-index)",
"simplelog 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"tempfile 3.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)",
......
......@@ -47,11 +47,19 @@ version = "3.0"
optional = true
version = "0.1"
[dependencies.serde_json]
optional = true
version = "^1.0"
[dependencies.serde]
optional = true
version = "^1.0"
[features]
default = ["tls", "hls", "web"]
tls = ["native-tls", "tokio-tls"]
hls = ["mpeg2ts", "m3u8-rs", "tempfile"]
web = ["warp"]
web = ["warp", "serde", "serde_json"]
[profile.release]
opt-level = 3
......
......@@ -2,6 +2,7 @@ use std::{
thread,
net::SocketAddr,
};
use serde_json::json;
use warp::{
self,
Filter,
......@@ -36,5 +37,34 @@ fn server(shared: Shared) {
let hls_files = warp::path("hls").and(warp::fs::dir(hls_root));
warp::serve(hls_files).run(addr);
let streams_api = warp::path("api").and(warp::path::param())
.map(move |resource: String| {
let json = match resource.as_str() {
"active_streams" => {
json!({ "streams": active_streams(&shared) })
}
_ => {
json!({ "error": "Unknown resource" })
}
};
warp::reply::json(&json)
});
let routes = hls_files.or(streams_api);
warp::serve(routes).run(addr);
}
fn active_streams(shared: &Shared) -> Vec<String> {
let streams = shared.streams.read();
streams.iter()
.filter_map(|(k, v)| {
if v.has_publisher() {
Some(k.clone())
} else {
None
}
})
.collect()
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment