Skip to content

Commit 4aa6267

Browse files
committed
Add support for MediaMTX auth
1 parent 736b5b5 commit 4aa6267

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,16 @@ You should be able to find the details in your [BELABOX cloud](https://cloud.bel
402402
```JSON
403403
"streamServer": {
404404
"type": "Mediamtx",
405-
"statsUrl": "http://localhost:9997/v3/paths/get/{name}"
405+
"statsUrl": "http://localhost:9997/v3/paths/get/{name}",
406+
"auth": {
407+
"username": "user",
408+
"password": "pass"
409+
}
406410
},
407411
```
408412

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

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

src/stream_servers/mediamtx.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,20 @@ pub struct Stats {
101101
pub srt: Option<SrtStats>,
102102
}
103103

104+
#[derive(Serialize, Deserialize, Debug)]
105+
pub struct Auth {
106+
username: String,
107+
password: String,
108+
}
109+
104110
#[derive(Serialize, Deserialize)]
105111
#[serde(rename_all = "camelCase")]
106112
pub struct Mediamtx {
107113
/// URL to MediaMTX stats page (ex; http://localhost:9997/v3/paths/get/mystream )
108114
pub stats_url: String,
109115

116+
pub auth: Option<Auth>,
117+
110118
/// Client to make HTTP requests with
111119
#[serde(skip, default = "default_reqwest_client")]
112120
pub client: reqwest::Client,
@@ -138,7 +146,13 @@ impl Default for Cache {
138146

139147
impl Mediamtx {
140148
pub async fn get_stats(&self) -> Option<Stats> {
141-
let res = match self.client.get(&self.stats_url).send().await {
149+
let mut request = self.client.get(&self.stats_url);
150+
151+
if let Some(auth) = &self.auth {
152+
request = request.basic_auth(&auth.username, Some(&auth.password));
153+
}
154+
155+
let res = match request.send().await {
142156
Ok(res) => res,
143157
Err(_) => {
144158
error!("Stats page ({}) is unreachable", self.stats_url);
@@ -197,7 +211,13 @@ impl Mediamtx {
197211
let stats_url: Vec<&str> = self.stats_url.split("/v3").collect();
198212
let stats_url = format!("{}/v3/srtconns/get/{id}", stats_url.first()?);
199213

200-
let res = match self.client.get(stats_url.clone()).send().await {
214+
let mut request = self.client.get(stats_url.clone());
215+
216+
if let Some(auth) = &self.auth {
217+
request = request.basic_auth(&auth.username, Some(&auth.password));
218+
}
219+
220+
let res = match request.send().await {
201221
Ok(res) => res,
202222
Err(_) => {
203223
error!("Stats page ({}) is unreachable", stats_url);

0 commit comments

Comments
 (0)