126 lines
2.8 KiB
Go
126 lines
2.8 KiB
Go
package handlers
|
|
|
|
import (
|
|
"fmt"
|
|
w "moxitech_fullstream/pkg/webrtc"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/gofiber/websocket/v2"
|
|
)
|
|
|
|
// Stream - обработчик маршрута для стрима.
|
|
// @Summary Обработчик маршрута для стрима
|
|
// @Description Обрабатывает GET-запрос на маршрут /:suuid/stream.
|
|
// @Param suuid path "UUID стрима"
|
|
// @Success 200 {object} map[string]interface{}
|
|
// @Failure 400 {string} string
|
|
// @Router /:suuid/stream [get]
|
|
func Stream(c *fiber.Ctx) error {
|
|
suuid := c.Params("suuid")
|
|
if suuid == "" {
|
|
c.Status(400)
|
|
return nil
|
|
}
|
|
|
|
ws := "ws"
|
|
if os.Getenv("ENVIRONMENT") == "PRODUCTION" {
|
|
ws = "wss"
|
|
}
|
|
|
|
w.RoomsLock.Lock()
|
|
if _, ok := w.Streams[suuid]; ok {
|
|
w.RoomsLock.Unlock()
|
|
return c.Render("stream", fiber.Map{
|
|
"StreamWebsocketAddr": fmt.Sprintf("%s://%s/stream/%s/websocket", ws, c.Hostname(), suuid),
|
|
"ChatWebsocketAddr": fmt.Sprintf("%s://%s/stream/%s/chat/websocket", ws, c.Hostname(), suuid),
|
|
"ViewerWebsocketAddr": fmt.Sprintf("%s://%s/stream/%s/viewer/websocket", ws, c.Hostname(), suuid),
|
|
"Type": "stream",
|
|
}, "layouts/main")
|
|
}
|
|
w.RoomsLock.Unlock()
|
|
|
|
return c.Render("stream", fiber.Map{
|
|
"NoStream": "true",
|
|
"Leave": "true",
|
|
}, "layouts/main")
|
|
}
|
|
|
|
func ApiStream(c *fiber.Ctx) error {
|
|
suuid := c.Params("suuid")
|
|
if suuid == "" {
|
|
c.Status(400)
|
|
return nil
|
|
}
|
|
|
|
ws := "ws"
|
|
if os.Getenv("ENVIRONMENT") == "PRODUCTION" {
|
|
ws = "wss"
|
|
}
|
|
|
|
w.RoomsLock.Lock()
|
|
if _, ok := w.Streams[suuid]; ok {
|
|
w.RoomsLock.Unlock()
|
|
return c.JSON(fiber.Map{
|
|
"StreamWebsocketAddr": fmt.Sprintf("%s://%s/stream/%s/websocket", ws, c.Hostname(), suuid),
|
|
"ChatWebsocketAddr": fmt.Sprintf("%s://%s/stream/%s/chat/websocket", ws, c.Hostname(), suuid),
|
|
"ViewerWebsocketAddr": fmt.Sprintf("%s://%s/stream/%s/viewer/websocket", ws, c.Hostname(), suuid),
|
|
"Type": "stream",
|
|
})
|
|
}
|
|
w.RoomsLock.Unlock()
|
|
|
|
return c.JSON(fiber.Map{
|
|
"NoStream": "true",
|
|
"Leave": "true",
|
|
})
|
|
}
|
|
|
|
func StreamWebsocket(c *websocket.Conn) {
|
|
suuid := c.Params("suuid")
|
|
if suuid == "" {
|
|
return
|
|
}
|
|
|
|
w.RoomsLock.Lock()
|
|
if stream, ok := w.Streams[suuid]; ok {
|
|
w.RoomsLock.Unlock()
|
|
w.StreamConn(c, stream.Peers)
|
|
return
|
|
}
|
|
w.RoomsLock.Unlock()
|
|
}
|
|
|
|
func StreamViewerWebsocket(c *websocket.Conn) {
|
|
suuid := c.Params("suuid")
|
|
if suuid == "" {
|
|
return
|
|
}
|
|
|
|
w.RoomsLock.Lock()
|
|
if stream, ok := w.Streams[suuid]; ok {
|
|
w.RoomsLock.Unlock()
|
|
viewerConn(c, stream.Peers)
|
|
return
|
|
}
|
|
w.RoomsLock.Unlock()
|
|
}
|
|
|
|
func viewerConn(c *websocket.Conn, p *w.Peers) {
|
|
ticker := time.NewTicker(1 * time.Second)
|
|
defer ticker.Stop()
|
|
defer c.Close()
|
|
|
|
for {
|
|
select {
|
|
case <-ticker.C:
|
|
w, err := c.Conn.NextWriter(websocket.TextMessage)
|
|
if err != nil {
|
|
return
|
|
}
|
|
w.Write([]byte(fmt.Sprintf("%d", len(p.Connections))))
|
|
}
|
|
}
|
|
}
|