-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_encoder.go
More file actions
57 lines (50 loc) · 1.81 KB
/
api_encoder.go
File metadata and controls
57 lines (50 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package control
import (
"encoding/json"
"net/http"
"github.com/zsiec/switchframe/server/codec"
"github.com/zsiec/switchframe/server/control/httperr"
)
// encoderResponse is the JSON response for GET /api/encoder.
type encoderResponse struct {
Current string `json:"current"`
Available []codec.EncoderInfo `json:"available"`
}
// encoderRequest is the JSON body for PUT /api/encoder.
type encoderRequest struct {
Encoder string `json:"encoder"`
}
// registerEncoderRoutes registers encoder-related API routes on the given mux.
func (a *API) registerEncoderRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /api/encoder", a.handleGetEncoder)
mux.HandleFunc("PUT /api/encoder", timedHandler(a.cmdQueue, "encoder_set", a.seqTracker, a.handleSetEncoderInner))
}
// handleGetEncoder returns the current encoder and available encoder options.
func (a *API) handleGetEncoder(w http.ResponseWriter, _ *http.Request) {
resp := encoderResponse{
Current: a.switcher.EncoderName(),
Available: a.switcher.AvailableEncoders(),
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(resp)
}
// handleSetEncoderInner switches the video encoder at runtime.
// body is the pre-read request body provided by timedHandler.
func (a *API) handleSetEncoderInner(w http.ResponseWriter, r *http.Request, body []byte) {
a.setLastOperator(r)
var req encoderRequest
if err := json.Unmarshal(body, &req); err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid json")
return
}
if req.Encoder == "" {
httperr.Write(w, http.StatusBadRequest, "encoder name required")
return
}
if err := a.switcher.SetEncoder(req.Encoder); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(a.enrichedState())
}