neko/internal/api/room/screen.go

77 lines
1.8 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"
"github.com/go-chi/render"
2020-10-31 07:56:22 +13:00
"demodesk/neko/internal/api/utils"
)
2020-10-31 06:16:21 +13:00
2020-10-31 09:28:07 +13:00
type ScreenConfiguration struct {
2020-10-31 06:16:21 +13:00
Width int `json:"width"`
Height int `json:"height"`
Rate int `json:"rate"`
}
2020-10-31 09:28:07 +13:00
func (a *ScreenConfiguration) Bind(r *http.Request) error {
2020-10-31 10:20:23 +13:00
// Bind will run after the unmarshalling is complete, its a
// good time to focus some post-processing after a decoding.
return nil
}
func (a *ScreenConfiguration) Render(w http.ResponseWriter, r *http.Request) error {
// Pre-processing before a response is marshalled and sent
// across the wire
2020-10-31 07:56:22 +13:00
return nil
}
2020-10-31 09:28:07 +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-01 09:58:57 +13:00
_ = render.Render(w, r, utils.ErrMessage(500, "Unable to get screen configuration."))
2020-10-31 06:16:21 +13:00
return
}
2020-10-31 09:28:07 +13:00
render.JSON(w, r, ScreenConfiguration{
2020-10-31 06:16:21 +13:00
Width: size.Width,
Height: size.Height,
Rate: int(size.Rate),
})
}
2020-10-31 09:28:07 +13:00
func (h *RoomHandler) ScreenConfigurationChange(w http.ResponseWriter, r *http.Request) {
data := &ScreenConfiguration{}
2020-10-31 07:56:22 +13:00
if err := render.Bind(r, data); err != nil {
2020-11-01 09:58:57 +13:00
_ = render.Render(w, r, utils.ErrBadRequest(err))
2020-10-31 07:56:22 +13:00
return
}
2020-11-02 04:54:06 +13:00
if err := h.capture.ChangeResolution(data.Width, data.Height, data.Rate); err != nil {
2020-11-01 09:58:57 +13:00
_ = render.Render(w, r, utils.ErrUnprocessableEntity(err))
2020-10-31 07:56:22 +13:00
return
}
2020-11-02 04:54:06 +13:00
// TODO: Broadcast change to all sessions.
2020-10-31 07:56:22 +13:00
render.JSON(w, r, data)
2020-10-31 06:16:21 +13:00
}
2020-10-31 09:28:07 +13:00
func (h *RoomHandler) ScreenConfigurationsList(w http.ResponseWriter, r *http.Request) {
2020-10-31 10:20:23 +13:00
list := []render.Renderer{}
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 {
list = append(list, &ScreenConfiguration{
Width: size.Width,
Height: size.Height,
Rate: int(fps),
})
}
}
2020-11-01 09:58:57 +13:00
_ = render.RenderList(w, r, list)
2020-10-31 06:16:21 +13:00
}