neko/internal/api/room/screen.go

81 lines
2.0 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 09:29:51 +13:00
"demodesk/neko/internal/websocket/broadcast"
2020-10-31 07:56:22 +13:00
)
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-10-31 06:16:21 +13:00
size := h.remote.GetScreenSize()
if size == nil {
2020-10-31 07:56:22 +13:00
render.Render(w, r, utils.ErrMessage(500, "Not implmented."))
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 {
render.Render(w, r, utils.ErrInvalidRequest(err))
return
}
if err := h.remote.ChangeResolution(data.Width, data.Height, data.Rate); err != nil {
render.Render(w, r, utils.ErrInvalidRequest(err))
return
}
2020-10-31 09:29:51 +13:00
if err := broadcast.ScreenConfiguration(h.sessions, "-todo-session-id-", data.Width, data.Height, data.Rate); err != nil {
render.Render(w, r, utils.ErrInvalidRequest(err))
return
}
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{}
ScreenConfigurations := h.remote.ScreenConfigurations()
for _, size := range ScreenConfigurations {
for _, fps := range size.Rates {
list = append(list, &ScreenConfiguration{
Width: size.Width,
Height: size.Height,
Rate: int(fps),
})
}
}
render.RenderList(w, r, list)
2020-10-31 06:16:21 +13:00
}