1
0
mirror of https://gitlab.crans.org/nounous/ghostream.git synced 2025-07-04 06:02:11 +02:00

Handle websocket

This commit is contained in:
Alexandre Iooss
2020-10-20 19:12:15 +02:00
parent ac2f87e936
commit 01efba3e3f
5 changed files with 78 additions and 86 deletions

66
web/websocket_handler.go Normal file
View File

@ -0,0 +1,66 @@
package web
import (
"log"
"net/http"
"github.com/gorilla/websocket"
"gitlab.crans.org/nounous/ghostream/stream/webrtc"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
// clientDescription is sent by new client
type clientDescription struct {
webRtcSdp webrtc.SessionDescription
stream string
quality string
}
// websocketHandler exchanges WebRTC SDP and viewer count
func websocketHandler(w http.ResponseWriter, r *http.Request) {
// Upgrade client connection to WebSocket
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Printf("Failed to upgrade client to websocket: %s", err)
return
}
for {
// Get client description
c := &clientDescription{}
err = conn.ReadJSON(c)
if err != nil {
log.Printf("Failed to receive client description: %s", err)
return
}
// Get requested stream
stream, err := streams.Get(c.stream)
if err != nil {
log.Printf("Stream not found: %s", c.stream)
return
}
// Get requested quality
q, err := stream.GetQuality(c.quality)
if err != nil {
log.Printf("Quality not found: %s", c.quality)
return
}
// Exchange session descriptions with WebRTC stream server
// FIXME: Add trickle ICE support
q.WebRtcRemoteSdp <- c.webRtcSdp
localDescription := <-q.WebRtcLocalSdp
// Send new local description
if err := conn.WriteJSON(localDescription); err != nil {
log.Println(err)
return
}
}
}