Skip to content

Commit

Permalink
Add support for MediaMTX auth
Browse files Browse the repository at this point in the history
  • Loading branch information
715209 committed May 31, 2024
1 parent 736b5b5 commit 4aa6267
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,11 +402,16 @@ You should be able to find the details in your [BELABOX cloud](https://cloud.bel
```JSON
"streamServer": {
"type": "Mediamtx",
"statsUrl": "http://localhost:9997/v3/paths/get/{name}"
"statsUrl": "http://localhost:9997/v3/paths/get/{name}",
"auth": {
"username": "user",
"password": "pass"
}
},
```

- `statsUrl`: The API stats page. Replace `{name}` with the name of your stream.
- `auth`: Optional field

For more details, refer to the [MediaMTX documentation](https://github.com/bluenviron/mediamtx#table-of-contents).

Expand Down
24 changes: 22 additions & 2 deletions src/stream_servers/mediamtx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,20 @@ pub struct Stats {
pub srt: Option<SrtStats>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Auth {
username: String,
password: String,
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Mediamtx {
/// URL to MediaMTX stats page (ex; http://localhost:9997/v3/paths/get/mystream )
pub stats_url: String,

pub auth: Option<Auth>,

/// Client to make HTTP requests with
#[serde(skip, default = "default_reqwest_client")]
pub client: reqwest::Client,
Expand Down Expand Up @@ -138,7 +146,13 @@ impl Default for Cache {

impl Mediamtx {
pub async fn get_stats(&self) -> Option<Stats> {
let res = match self.client.get(&self.stats_url).send().await {
let mut request = self.client.get(&self.stats_url);

if let Some(auth) = &self.auth {
request = request.basic_auth(&auth.username, Some(&auth.password));
}

let res = match request.send().await {
Ok(res) => res,
Err(_) => {
error!("Stats page ({}) is unreachable", self.stats_url);
Expand Down Expand Up @@ -197,7 +211,13 @@ impl Mediamtx {
let stats_url: Vec<&str> = self.stats_url.split("/v3").collect();
let stats_url = format!("{}/v3/srtconns/get/{id}", stats_url.first()?);

let res = match self.client.get(stats_url.clone()).send().await {
let mut request = self.client.get(stats_url.clone());

if let Some(auth) = &self.auth {
request = request.basic_auth(&auth.username, Some(&auth.password));
}

let res = match request.send().await {
Ok(res) => res,
Err(_) => {
error!("Stats page ({}) is unreachable", stats_url);
Expand Down

0 comments on commit 4aa6267

Please sign in to comment.