diff --git a/internal/webrtc/whep.go b/internal/webrtc/whep.go index 628ec626..a45a7161 100644 --- a/internal/webrtc/whep.go +++ b/internal/webrtc/whep.go @@ -67,7 +67,10 @@ func WHEPChangeLayer(whepSessionId, layer string) error { if _, ok := streamMap[streamKey].whepSessions[whepSessionId]; ok { streamMap[streamKey].whepSessions[whepSessionId].currentLayer.Store(layer) streamMap[streamKey].whepSessions[whepSessionId].waitingForKeyframe.Store(true) - streamMap[streamKey].pliChan <- true + select { + case streamMap[streamKey].pliChan <- true: + default: + } } } diff --git a/main.go b/main.go index 255e4158..a4db8f10 100644 --- a/main.go +++ b/main.go @@ -129,7 +129,7 @@ func whipHandler(res http.ResponseWriter, r *http.Request) { } func whepHandler(res http.ResponseWriter, req *http.Request) { - if req.Method != "POST" && req.Method != "PATCH" { + if req.Method != http.MethodPost { return } @@ -145,11 +145,6 @@ func whepHandler(res http.ResponseWriter, req *http.Request) { return } - if req.Method == "PATCH" { - patchHandler(res, req, "TODO", string(body), true) - return - } - answer, whepSessionId, err := webrtc.WHEP(string(body), streamKey) if err != nil { logHTTPError(res, err.Error(), http.StatusBadRequest) @@ -167,6 +162,26 @@ func whepHandler(res http.ResponseWriter, req *http.Request) { } } +func whepSessionPatchHandler(res http.ResponseWriter, req *http.Request) { + if req.Method != http.MethodPatch { + return + } + + whepSessionId := strings.TrimPrefix(req.URL.Path, "/api/whep/") + if whepSessionId == "" { + logHTTPError(res, "missing WHEP session id", http.StatusBadRequest) + return + } + + body, err := io.ReadAll(req.Body) + if err != nil { + logHTTPError(res, err.Error(), http.StatusBadRequest) + return + } + + patchHandler(res, req, whepSessionId, string(body), false) +} + func whepServerSentEventsHandler(res http.ResponseWriter, req *http.Request) { res.Header().Set("Content-Type", "text/event-stream") res.Header().Set("Cache-Control", "no-cache") @@ -320,6 +335,7 @@ func main() { } mux.HandleFunc("/api/whip", corsHandler(whipHandler)) mux.HandleFunc("/api/whep", corsHandler(whepHandler)) + mux.HandleFunc("/api/whep/", corsHandler(whepSessionPatchHandler)) mux.HandleFunc("/api/sse/", corsHandler(whepServerSentEventsHandler)) mux.HandleFunc("/api/layer/", corsHandler(whepLayerHandler)) mux.HandleFunc("/api/status", corsHandler(statusHandler))