neko/internal/api/room/screen.go
2021-09-17 00:58:50 +02:00

111 lines
2.7 KiB
Go

package room
import (
"net/http"
"strconv"
"demodesk/neko/internal/types"
"demodesk/neko/internal/types/event"
"demodesk/neko/internal/types/message"
"demodesk/neko/internal/utils"
)
type ScreenConfigurationPayload struct {
Width int `json:"width"`
Height int `json:"height"`
Rate int16 `json:"rate"`
}
func (h *RoomHandler) screenConfiguration(w http.ResponseWriter, r *http.Request) error {
size := h.desktop.GetScreenSize()
if size == nil {
return utils.HttpInternalServerError().WithInternalMsg("unable to get screen configuration")
}
return utils.HttpSuccess(w, ScreenConfigurationPayload{
Width: size.Width,
Height: size.Height,
Rate: size.Rate,
})
}
func (h *RoomHandler) screenConfigurationChange(w http.ResponseWriter, r *http.Request) error {
data := &ScreenConfigurationPayload{}
if err := utils.HttpJsonRequest(w, r, data); err != nil {
return err
}
if err := h.desktop.SetScreenSize(types.ScreenSize{
Width: data.Width,
Height: data.Height,
Rate: data.Rate,
}); err != nil {
return utils.HttpUnprocessableEntity("cannot set screen size").WithInternalErr(err)
}
h.sessions.Broadcast(
event.SCREEN_UPDATED,
message.ScreenSize{
Width: data.Width,
Height: data.Height,
Rate: data.Rate,
}, nil)
return utils.HttpSuccess(w, data)
}
func (h *RoomHandler) screenConfigurationsList(w http.ResponseWriter, r *http.Request) error {
list := []ScreenConfigurationPayload{}
ScreenConfigurations := h.desktop.ScreenConfigurations()
for _, size := range ScreenConfigurations {
for _, fps := range size.Rates {
list = append(list, ScreenConfigurationPayload{
Width: size.Width,
Height: size.Height,
Rate: fps,
})
}
}
return utils.HttpSuccess(w, list)
}
func (h *RoomHandler) screenShotGet(w http.ResponseWriter, r *http.Request) error {
quality, err := strconv.Atoi(r.URL.Query().Get("quality"))
if err != nil {
quality = 90
}
img := h.desktop.GetScreenshotImage()
bytes, err := utils.CreateJPGImage(img, quality)
if err != nil {
return utils.HttpInternalServerError().WithInternalErr(err)
}
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Content-Type", "image/jpeg")
_, err = w.Write(bytes)
return err
}
func (h *RoomHandler) screenCastGet(w http.ResponseWriter, r *http.Request) error {
screencast := h.capture.Screencast()
if !screencast.Enabled() {
return utils.HttpBadRequest("screencast pipeline is not enabled")
}
bytes, err := screencast.Image()
if err != nil {
return utils.HttpInternalServerError().WithInternalErr(err)
}
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Content-Type", "image/jpeg")
_, err = w.Write(bytes)
return err
}