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