neko/internal/api/room/broadcast.go

71 lines
1.6 KiB
Go
Raw Normal View History

2020-10-30 10:23:30 +13:00
package room
import (
"net/http"
"demodesk/neko/internal/types/event"
"demodesk/neko/internal/types/message"
2021-02-15 02:40:17 +13:00
"demodesk/neko/internal/utils"
)
type BroadcastStatusPayload struct {
2021-02-15 02:40:17 +13:00
URL string `json:"url,omitempty"`
IsActive bool `json:"is_active"`
}
2021-09-17 10:58:50 +12:00
func (h *RoomHandler) broadcastStatus(w http.ResponseWriter, r *http.Request) error {
broadcast := h.capture.Broadcast()
2021-09-17 10:58:50 +12:00
return utils.HttpSuccess(w, BroadcastStatusPayload{
2021-02-06 02:03:53 +13:00
IsActive: broadcast.Started(),
URL: broadcast.Url(),
})
}
2021-09-17 10:58:50 +12:00
func (h *RoomHandler) boradcastStart(w http.ResponseWriter, r *http.Request) error {
data := &BroadcastStatusPayload{}
2021-09-17 10:58:50 +12:00
if err := utils.HttpJsonRequest(w, r, data); err != nil {
return err
}
if data.URL == "" {
2021-09-17 10:58:50 +12:00
return utils.HttpBadRequest("missing broadcast URL")
}
broadcast := h.capture.Broadcast()
2021-02-06 02:03:53 +13:00
if broadcast.Started() {
2021-09-17 10:58:50 +12:00
return utils.HttpUnprocessableEntity("server is already broadcasting")
}
if err := broadcast.Start(data.URL); err != nil {
2021-09-17 10:58:50 +12:00
return utils.HttpInternalServerError().WithInternalErr(err)
}
h.sessions.AdminBroadcast(
2021-09-02 07:58:39 +12:00
event.BORADCAST_STATUS,
message.BroadcastStatus{
2021-02-06 02:03:53 +13:00
IsActive: broadcast.Started(),
URL: broadcast.Url(),
}, nil)
2021-09-17 10:58:50 +12:00
return utils.HttpSuccess(w)
}
2021-09-17 10:58:50 +12:00
func (h *RoomHandler) boradcastStop(w http.ResponseWriter, r *http.Request) error {
broadcast := h.capture.Broadcast()
2021-02-06 02:03:53 +13:00
if !broadcast.Started() {
2021-09-17 10:58:50 +12:00
return utils.HttpUnprocessableEntity("server is not broadcasting")
}
2021-02-03 06:28:32 +13:00
broadcast.Stop()
h.sessions.AdminBroadcast(
2021-09-02 07:58:39 +12:00
event.BORADCAST_STATUS,
message.BroadcastStatus{
2021-02-06 02:03:53 +13:00
IsActive: broadcast.Started(),
URL: broadcast.Url(),
}, nil)
2021-09-17 10:58:50 +12:00
return utils.HttpSuccess(w)
}