@@ -34,6 +34,9 @@ function SrsRtcWhipWhepAsync() {
3434 self . displayStream = null ;
3535 self . userStream = null ;
3636
37+ // Store the WHIP session resource URL from Location header for cleanup.
38+ self . resourceUrl = null ;
39+
3740 // See https://datatracker.ietf.org/doc/draft-ietf-wish-whip/
3841 // @url The WebRTC url to publish with, for example:
3942 // http://localhost:1985/rtc/v1/whip/?app=live&stream=livestream
@@ -111,6 +114,14 @@ function SrsRtcWhipWhepAsync() {
111114 if ( xhr . status !== 200 && xhr . status !== 201 ) return reject ( xhr ) ;
112115 const data = xhr . responseText ;
113116 console . log ( "Got answer: " , data ) ;
117+
118+ // Extract Location header for WHIP session resource URL.
119+ const location = xhr . getResponseHeader ( 'Location' ) ;
120+ if ( location ) {
121+ self . resourceUrl = new URL ( location , url ) . href ;
122+ console . log ( `WHIP session resource URL: ${ self . resourceUrl } ` ) ;
123+ }
124+
114125 return data . code ? reject ( xhr ) : resolve ( data ) ;
115126 }
116127 xhr . open ( 'POST' , url , true ) ;
@@ -159,6 +170,14 @@ function SrsRtcWhipWhepAsync() {
159170 if ( xhr . status !== 200 && xhr . status !== 201 ) return reject ( xhr ) ;
160171 const data = xhr . responseText ;
161172 console . log ( "Got answer: " , data ) ;
173+
174+ // Extract Location header for WHEP session resource URL.
175+ const location = xhr . getResponseHeader ( 'Location' ) ;
176+ if ( location ) {
177+ self . resourceUrl = new URL ( location , url ) . href ;
178+ console . log ( `WHEP session resource URL: ${ self . resourceUrl } ` ) ;
179+ }
180+
162181 return data . code ? reject ( xhr ) : resolve ( data ) ;
163182 }
164183 xhr . open ( 'POST' , url , true ) ;
@@ -190,6 +209,25 @@ function SrsRtcWhipWhepAsync() {
190209 } ) ;
191210 self . userStream = null ;
192211 }
212+
213+ // Send DELETE request to WHIP session resource URL to cleanup server resources.
214+ if ( self . resourceUrl ) {
215+ const xhr = new XMLHttpRequest ( ) ;
216+ xhr . open ( 'DELETE' , self . resourceUrl , true ) ;
217+ xhr . onload = function ( ) {
218+ if ( xhr . readyState !== xhr . DONE ) return ;
219+ if ( xhr . status === 200 ) {
220+ console . log ( `WHIP session deleted: ${ self . resourceUrl } ` ) ;
221+ } else {
222+ console . warn ( `Failed to delete WHIP session: ${ self . resourceUrl } , status: ${ xhr . status } ` ) ;
223+ }
224+ } ;
225+ xhr . onerror = function ( ) {
226+ console . warn ( `Error deleting WHIP session: ${ self . resourceUrl } ` ) ;
227+ } ;
228+ xhr . send ( ) ;
229+ self . resourceUrl = null ;
230+ }
193231 } ;
194232
195233 // The callback when got local stream.
0 commit comments