Files
donut/internal/web/media.go
Leandro Moreira e8188cbfc5 split components
2024-01-26 19:13:23 -03:00

50 lines
1.0 KiB
Go

package handlers
import (
"encoding/json"
"log"
"net/http"
astisrt "github.com/asticode/go-astisrt/pkg"
"github.com/flavioribeiro/donut/internal/entity"
)
type MediaHandler struct{}
func NewMediaHandler() *MediaHandler {
return &MediaHandler{}
}
func (m *MediaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
SetCORS(w, r)
if r.Method != http.MethodGet {
ErrorToHTTP(w, entity.ErrHTTPGetOnly)
return
}
offer := entity.ParamsOffer{}
if err := json.NewDecoder(r.Body).Decode(&offer); err != nil {
ErrorToHTTP(w, err)
return
}
log.Println("Connecting to SRT ", offer)
_, err := astisrt.Dial(astisrt.DialOptions{
ConnectionOptions: []astisrt.ConnectionOption{
astisrt.WithLatency(300),
astisrt.WithStreamid(offer.SRTStreamID),
},
// Callback when the connection is disconnected
OnDisconnect: func(c *astisrt.Connection, err error) { log.Fatal("Disconnected from SRT") },
Host: offer.SRTHost,
Port: offer.SRTPort,
})
if err != nil {
ErrorToHTTP(w, err)
return
}
log.Println("Connected to SRT")
}