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-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
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.remote.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-10-31 09:29:51 +13:00
|
|
|
if err := broadcast.ScreenConfiguration(h.sessions, "-todo-session-id-", data.Width, data.Height, data.Rate); err != nil {
|
2020-11-01 09:58:57 +13:00
|
|
|
_ = render.Render(w, r, utils.ErrInternalServer(err))
|
2020-10-31 09:29:51 +13:00
|
|
|
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),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-01 09:58:57 +13:00
|
|
|
_ = render.RenderList(w, r, list)
|
2020-10-31 06:16:21 +13:00
|
|
|
}
|