2020-10-30 10:23:30 +13:00
|
|
|
package room
|
2020-10-31 06:16:21 +13:00
|
|
|
|
|
|
|
import (
|
2021-01-22 08:44:09 +13:00
|
|
|
"bytes"
|
|
|
|
"image/jpeg"
|
|
|
|
"strconv"
|
2020-10-31 06:16:21 +13:00
|
|
|
"net/http"
|
|
|
|
|
2021-01-16 05:30:19 +13:00
|
|
|
"demodesk/neko/internal/types"
|
2020-11-17 09:57:53 +13:00
|
|
|
"demodesk/neko/internal/types/event"
|
|
|
|
"demodesk/neko/internal/types/message"
|
2020-11-15 05:51:18 +13:00
|
|
|
"demodesk/neko/internal/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
|
|
|
}
|
|
|
|
|
2020-11-19 10:35:50 +13:00
|
|
|
func (h *RoomHandler) screenConfiguration(w http.ResponseWriter, r *http.Request) {
|
2020-11-02 04:54:06 +13:00
|
|
|
size := h.desktop.GetScreenSize()
|
2020-10-31 06:16:21 +13:00
|
|
|
|
|
|
|
if size == nil {
|
2020-11-29 03:00:21 +13:00
|
|
|
utils.HttpInternalServerError(w, "Unable to get screen configuration.")
|
2020-10-31 06:16:21 +13:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-19 09:56:42 +13:00
|
|
|
utils.HttpSuccess(w, ScreenConfigurationPayload{
|
2020-10-31 06:16:21 +13:00
|
|
|
Width: size.Width,
|
|
|
|
Height: size.Height,
|
2021-01-16 05:30:19 +13:00
|
|
|
Rate: size.Rate,
|
2020-10-31 06:16:21 +13:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-19 10:35:50 +13:00
|
|
|
func (h *RoomHandler) screenConfigurationChange(w http.ResponseWriter, r *http.Request) {
|
2020-11-19 09:56:42 +13:00
|
|
|
data := &ScreenConfigurationPayload{}
|
2020-11-15 05:51:18 +13:00
|
|
|
if !utils.HttpJsonRequest(w, r, data) {
|
2020-10-31 07:56:22 +13:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-16 05:30:19 +13:00
|
|
|
if err := h.desktop.SetScreenSize(types.ScreenSize{
|
|
|
|
Width: data.Width,
|
|
|
|
Height: data.Height,
|
|
|
|
Rate: data.Rate,
|
|
|
|
}); err != nil {
|
2020-11-15 05:51:18 +13:00
|
|
|
utils.HttpUnprocessableEntity(w, err)
|
2020-10-31 07:56:22 +13:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-19 08:30:33 +13:00
|
|
|
h.sessions.Broadcast(
|
2020-12-01 05:53:05 +13:00
|
|
|
message.ScreenSize{
|
|
|
|
Event: event.SCREEN_UPDATED,
|
2020-11-17 09:57:53 +13:00
|
|
|
Width: data.Width,
|
|
|
|
Height: data.Height,
|
|
|
|
Rate: data.Rate,
|
2020-11-19 08:30:33 +13:00
|
|
|
}, nil)
|
2020-10-31 07:56:22 +13:00
|
|
|
|
2020-11-15 05:51:18 +13:00
|
|
|
utils.HttpSuccess(w, data)
|
2020-10-31 06:16:21 +13:00
|
|
|
}
|
|
|
|
|
2020-11-19 10:35:50 +13:00
|
|
|
func (h *RoomHandler) screenConfigurationsList(w http.ResponseWriter, r *http.Request) {
|
2020-11-19 09:56:42 +13:00
|
|
|
list := []ScreenConfigurationPayload{}
|
2020-10-31 10:20:23 +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
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-15 05:51:18 +13:00
|
|
|
utils.HttpSuccess(w, list)
|
2020-10-31 06:16:21 +13:00
|
|
|
}
|
2021-01-22 08:44:09 +13:00
|
|
|
|
2021-01-24 03:23:10 +13:00
|
|
|
func (h *RoomHandler) screenShotGet(w http.ResponseWriter, r *http.Request) {
|
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()
|
|
|
|
out := new(bytes.Buffer)
|
2021-01-26 05:31:24 +13:00
|
|
|
|
|
|
|
if err := jpeg.Encode(out, img, &jpeg.Options{
|
|
|
|
Quality: quality,
|
|
|
|
}); err != nil {
|
2021-01-22 08:44:09 +13:00
|
|
|
utils.HttpInternalServerError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
w.Write(out.Bytes())
|
|
|
|
}
|
2021-01-23 06:13:32 +13:00
|
|
|
|
|
|
|
func (h *RoomHandler) screenCastGet(w http.ResponseWriter, r *http.Request) {
|
|
|
|
screencast := h.capture.Screencast()
|
|
|
|
if !screencast.Enabled() {
|
|
|
|
utils.HttpBadRequest(w, "Screencast pipeline is not enabled.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-24 03:17:52 +13:00
|
|
|
bytes, err := screencast.Image()
|
|
|
|
if err != nil {
|
|
|
|
utils.HttpInternalServerError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
w.Write(bytes)
|
|
|
|
}
|