neko/internal/api/room/screen.go

114 lines
3.1 KiB
Go
Raw Normal View History

2020-10-30 10:23:30 +13:00
package room
2020-10-31 06:16:21 +13:00
import (
"net/http"
2021-02-15 02:40:17 +13:00
"strconv"
2020-10-31 06:16:21 +13:00
"github.com/demodesk/neko/pkg/auth"
"github.com/demodesk/neko/pkg/types"
"github.com/demodesk/neko/pkg/types/event"
"github.com/demodesk/neko/pkg/types/message"
"github.com/demodesk/neko/pkg/utils"
2020-10-31 07:56:22 +13:00
)
2020-10-31 06:16:21 +13:00
2020-11-19 09:56:42 +13:00
type ScreenConfigurationPayload struct {
2021-01-16 05:30:19 +13:00
Width int `json:"width"`
Height int `json:"height"`
Rate int16 `json:"rate"`
2020-10-31 06:16:21 +13:00
}
2021-09-17 10:58:50 +12:00
func (h *RoomHandler) screenConfiguration(w http.ResponseWriter, r *http.Request) error {
2020-11-02 04:54:06 +13:00
size := h.desktop.GetScreenSize()
2020-10-31 06:16:21 +13:00
if size == nil {
2021-09-17 10:58:50 +12:00
return utils.HttpInternalServerError().WithInternalMsg("unable to get screen configuration")
2020-10-31 06:16:21 +13:00
}
2021-09-21 04:28:13 +12:00
payload := ScreenConfigurationPayload(*size)
return utils.HttpSuccess(w, payload)
2020-10-31 06:16:21 +13:00
}
2021-09-17 10:58:50 +12:00
func (h *RoomHandler) screenConfigurationChange(w http.ResponseWriter, r *http.Request) error {
2020-11-19 09:56:42 +13:00
data := &ScreenConfigurationPayload{}
2021-09-17 10:58:50 +12:00
if err := utils.HttpJsonRequest(w, r, data); err != nil {
return err
2020-10-31 07:56:22 +13:00
}
2021-09-21 04:28:13 +12:00
size := types.ScreenSize(*data)
if err := h.desktop.SetScreenSize(size); err != nil {
2021-09-17 10:58:50 +12:00
return utils.HttpUnprocessableEntity("cannot set screen size").WithInternalErr(err)
2020-10-31 07:56:22 +13:00
}
2021-09-21 04:28:13 +12:00
payload := message.ScreenSize(*data)
h.sessions.Broadcast(event.SCREEN_UPDATED, payload, nil)
2020-10-31 07:56:22 +13:00
2021-09-17 10:58:50 +12:00
return utils.HttpSuccess(w, data)
2020-10-31 06:16:21 +13:00
}
2021-09-17 10:58:50 +12:00
func (h *RoomHandler) screenConfigurationsList(w http.ResponseWriter, r *http.Request) error {
2020-11-19 09:56:42 +13:00
list := []ScreenConfigurationPayload{}
2021-02-03 06:28:32 +13:00
2020-11-02 04:54:06 +13:00
ScreenConfigurations := h.desktop.ScreenConfigurations()
2020-10-31 10:20:23 +13:00
for _, size := range ScreenConfigurations {
for _, fps := range size.Rates {
2020-11-19 09:56:42 +13:00
list = append(list, ScreenConfigurationPayload{
2020-10-31 10:20:23 +13:00
Width: size.Width,
Height: size.Height,
2021-01-16 05:30:19 +13:00
Rate: fps,
2020-10-31 10:20:23 +13:00
})
}
}
2021-09-17 10:58:50 +12:00
return utils.HttpSuccess(w, list)
2020-10-31 06:16:21 +13:00
}
2021-01-22 08:44:09 +13:00
2021-09-17 10:58:50 +12:00
func (h *RoomHandler) screenShotGet(w http.ResponseWriter, r *http.Request) error {
2021-01-26 05:31:24 +13:00
quality, err := strconv.Atoi(r.URL.Query().Get("quality"))
if err != nil {
quality = 90
2021-01-22 08:44:09 +13:00
}
img := h.desktop.GetScreenshotImage()
bytes, err := utils.CreateJPGImage(img, quality)
if err != nil {
2021-09-17 10:58:50 +12:00
return utils.HttpInternalServerError().WithInternalErr(err)
2021-01-22 08:44:09 +13:00
}
2021-01-24 03:23:10 +13:00
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
2021-01-22 08:44:09 +13:00
w.Header().Set("Content-Type", "image/jpeg")
2021-09-17 10:58:50 +12:00
_, err = w.Write(bytes)
return err
2021-01-22 08:44:09 +13:00
}
2021-01-23 06:13:32 +13:00
2021-09-17 10:58:50 +12:00
func (h *RoomHandler) screenCastGet(w http.ResponseWriter, r *http.Request) error {
2022-03-27 11:20:38 +13:00
// display fallback image when private mode is enabled even if screencast is not
if session, ok := auth.GetSession(r); ok && session.PrivateModeEnabled() {
if h.privateModeImage != nil {
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Content-Type", "image/jpeg")
_, err := w.Write(h.privateModeImage)
return err
}
return utils.HttpBadRequest("private mode is enabled but no fallback image available")
}
2021-01-23 06:13:32 +13:00
screencast := h.capture.Screencast()
if !screencast.Enabled() {
2021-09-17 10:58:50 +12:00
return utils.HttpBadRequest("screencast pipeline is not enabled")
2021-01-23 06:13:32 +13:00
}
2021-01-24 03:17:52 +13:00
bytes, err := screencast.Image()
if err != nil {
2021-09-17 10:58:50 +12:00
return utils.HttpInternalServerError().WithInternalErr(err)
2021-01-24 03:17:52 +13:00
}
2021-01-23 06:13:32 +13:00
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Content-Type", "image/jpeg")
2021-09-17 10:58:50 +12:00
_, err = w.Write(bytes)
return err
2021-01-23 06:13:32 +13:00
}