Skip to content

Commit db90e73

Browse files
committed
Add HTTP DELETE handler for WHIP
Faster session tear down for OBS
1 parent 913ddc3 commit db90e73

4 files changed

Lines changed: 70 additions & 4 deletions

File tree

internal/server/handlers/whip/whip.go

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
)
1818

1919
func WhipHandler(responseWriter http.ResponseWriter, request *http.Request) {
20-
if request.Method != "POST" && request.Method != "PATCH" {
20+
if request.Method != "POST" && request.Method != "PATCH" && request.Method != "DELETE" {
2121
return
2222
}
2323

@@ -35,6 +35,24 @@ func WhipHandler(responseWriter http.ResponseWriter, request *http.Request) {
3535
return
3636
}
3737

38+
if request.Method == "DELETE" {
39+
sessionId := getSessionIdFromWhipPath(request.URL.Path)
40+
41+
if sessionId == "" {
42+
log.Println("API.WHIP.Delete Error: Missing session id")
43+
helpers.LogHttpError(responseWriter, "Missing session id", http.StatusBadRequest)
44+
return
45+
}
46+
47+
log.Println("API.WHIP.Delete: Removing session", sessionId)
48+
if err := deleteHandler(responseWriter, sessionId); err != nil {
49+
log.Println("API.WHIP.Delete Error:", err)
50+
helpers.LogHttpError(responseWriter, err.Error(), http.StatusBadRequest)
51+
}
52+
53+
return
54+
}
55+
3856
offer, err := io.ReadAll(request.Body)
3957
if err != nil || string(offer) == "" {
4058
log.Println("Error reading offer")
@@ -106,9 +124,7 @@ func WhipHandler(responseWriter http.ResponseWriter, request *http.Request) {
106124
return
107125
}
108126

109-
path := strings.Replace(request.URL.Path, "/api/whip", "", 1)
110-
segments := strings.Split(path, "/")
111-
sessionId := strings.TrimSpace(segments[len(segments)-1])
127+
sessionId := getSessionIdFromWhipPath(request.URL.Path)
112128

113129
if sessionId == "" {
114130
log.Println("API.WHIP.Patch Error: Missing session id")
@@ -159,3 +175,19 @@ func patchHandler(res http.ResponseWriter, r *http.Request, sessionId, body stri
159175

160176
return nil
161177
}
178+
179+
func deleteHandler(res http.ResponseWriter, sessionId string) error {
180+
if err := webrtc.HandleWhipDelete(sessionId); err != nil {
181+
return err
182+
}
183+
184+
res.WriteHeader(http.StatusNoContent)
185+
186+
return nil
187+
}
188+
189+
func getSessionIdFromWhipPath(path string) string {
190+
path = strings.Replace(path, "/api/whip", "", 1)
191+
segments := strings.Split(path, "/")
192+
return strings.TrimSpace(segments[len(segments)-1])
193+
}

internal/webrtc/sessions/manager/manager.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,21 @@ func (manager *SessionManager) GetHostSessionById(sessionId string) (host *whip.
212212

213213
return nil, false
214214
}
215+
216+
func (manager *SessionManager) GetSessionByHostSessionId(sessionId string) (session *session.Session, foundSession bool) {
217+
manager.sessionsLock.RLock()
218+
defer manager.sessionsLock.RUnlock()
219+
220+
for _, session := range manager.sessions {
221+
host := session.Host.Load()
222+
if host == nil {
223+
continue
224+
}
225+
226+
if sessionId == host.Id {
227+
return session, true
228+
}
229+
}
230+
231+
return nil, false
232+
}

internal/webrtc/sessions/session/session.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ func (session *Session) close() {
174174
session.ActiveContextCancel()
175175
}
176176

177+
func (session *Session) Close() {
178+
log.Println("Session.Close", session.StreamKey)
179+
session.close()
180+
}
181+
177182
// Returns true is no WHIP tracks are present, and no WHEP sessions are waiting for incoming streams
178183
func (session *Session) isEmpty() bool {
179184
if session.hasWhepSessions() {

internal/webrtc/webrtc.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ func HandleWhipPatch(sessionId, body string) error {
8383
return nil
8484
}
8585

86+
func HandleWhipDelete(sessionId string) error {
87+
session, isFound := manager.SessionsManager.GetSessionByHostSessionId(sessionId)
88+
89+
if !isFound {
90+
return errors.New("no session found")
91+
}
92+
93+
session.Close()
94+
return nil
95+
}
96+
8697
func patchPeerConnection(peerConnection *webrtc.PeerConnection, body string) error {
8798
oldUfrag := getSdpKeyValue(peerConnection.CurrentRemoteDescription().SDP, "ice-ufrag")
8899
oldPwd := getSdpKeyValue(peerConnection.CurrentRemoteDescription().SDP, "ice-pwd")

0 commit comments

Comments
 (0)