char/char-rtc-obs

git clone https://git.t4t.associates/char/char-rtc-obs

videogame hackerInitial commiteeaf4aa

main
2.6 KiB93 linesraw
1use anyhow::Result;
2use log::{info, warn};
3use reqwest::{
4    header::{HeaderValue, AUTHORIZATION, CONTENT_TYPE, LOCATION, USER_AGENT},
5    Url,
6};
7use webrtc::peer_connection::sdp::session_description::RTCSessionDescription;
8
9use crate::obs_log;
10
11const OBS_VERSION: &str = env!("OBS_VERSION");
12
13pub async fn offer(
14    url: &str,
15    bearer_token: Option<&str>,
16    local_desc: RTCSessionDescription,
17) -> Result<(RTCSessionDescription, Url)> {
18    let client = reqwest::Client::new();
19
20    let mut headers = reqwest::header::HeaderMap::new();
21    headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/sdp"));
22    headers.insert(
23        USER_AGENT,
24        HeaderValue::from_str(&format!("libobs/{OBS_VERSION}"))?,
25    );
26
27    if let Some(bearer_token) = bearer_token {
28        if !bearer_token.is_empty() {
29            headers.insert(
30                AUTHORIZATION,
31                HeaderValue::from_str(&format!("Bearer {bearer_token}"))?,
32            );
33        }
34    }
35
36    if obs_log::debug_whip() {
37        info!(
38            "[WHIP DEBUG | CAUTION SENSITIVE INFO] Sending offer to {url}: {}",
39            local_desc.sdp
40        );
41    }
42
43    let request = client.post(url).headers(headers).body(local_desc.sdp);
44
45    if obs_log::debug_whip() {
46        info!("[WHIP DEBUG | CAUTION SENSITIVE INFO] Offer request {request:#?}");
47    }
48
49    let response = request.send().await?;
50
51    if obs_log::debug_whip() {
52        info!("[WHIP DEBUG | CAUTION SENSITIVE INFO] Offer response: {response:#?}");
53    }
54
55    let mut url = response.url().to_owned();
56    if let Some(location) = response.headers().get(LOCATION) {
57        url.set_path(location.to_str()?);
58    }
59
60    let body = response.text().await?;
61    let sdp = RTCSessionDescription::answer(body)?;
62
63    if obs_log::debug_whip() {
64        info!("[WHIP DEBUG | CAUTION SENSITIVE INFO] Answer SDP: {sdp:#?}");
65    }
66
67    Ok((sdp, url))
68}
69
70pub async fn delete(url: &Url) -> Result<()> {
71    let client = reqwest::Client::new();
72
73    let request = client.delete(url.to_owned()).header(
74        USER_AGENT,
75        HeaderValue::from_str(&format!("libobs/{OBS_VERSION}"))?,
76    );
77
78    if obs_log::debug_whip() {
79        info!("[WHIP DEBUG | CAUTION SENSITIVE INFO] Delete request {request:#?}");
80    }
81
82    let response = request.send().await?;
83
84    if obs_log::debug_whip() {
85        info!("[WHIP DEBUG | CAUTION SENSITIVE INFO] Delete response {response:#?}");
86    }
87
88    if !response.status().is_success() {
89        warn!("Failed DELETE of whip resource: {}", response.status())
90    }
91
92    Ok(())
93}