mirror of
https://github.com/AlexxIT/go2rtc.git
synced 2025-10-08 17:50:15 +08:00
256 lines
5.7 KiB
Go
256 lines
5.7 KiB
Go
package webrtc
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"github.com/AlexxIT/go2rtc/cmd/api"
|
|
"github.com/AlexxIT/go2rtc/cmd/app"
|
|
"github.com/AlexxIT/go2rtc/cmd/streams"
|
|
"github.com/AlexxIT/go2rtc/pkg/webrtc"
|
|
pion "github.com/pion/webrtc/v3"
|
|
"github.com/rs/zerolog"
|
|
"io"
|
|
"mime"
|
|
"net"
|
|
"net/http"
|
|
)
|
|
|
|
func Init() {
|
|
var cfg struct {
|
|
Mod struct {
|
|
Listen string `yaml:"listen"`
|
|
Candidates []string `yaml:"candidates"`
|
|
IceServers []pion.ICEServer `yaml:"ice_servers"`
|
|
} `yaml:"webrtc"`
|
|
}
|
|
|
|
cfg.Mod.Listen = ":8555"
|
|
cfg.Mod.IceServers = []pion.ICEServer{
|
|
{URLs: []string{"stun:stun.l.google.com:19302"}},
|
|
}
|
|
|
|
app.LoadConfig(&cfg)
|
|
|
|
log = app.GetLogger("webrtc")
|
|
|
|
address := cfg.Mod.Listen
|
|
pionAPI, err := webrtc.NewAPI(address)
|
|
if pionAPI == nil {
|
|
log.Error().Err(err).Caller().Msg("webrtc.NewAPI")
|
|
return
|
|
}
|
|
|
|
if err != nil {
|
|
log.Warn().Err(err).Msg("[webrtc] listen")
|
|
} else if address != "" {
|
|
log.Info().Str("addr", address).Msg("[webrtc] listen")
|
|
_, Port, _ = net.SplitHostPort(address)
|
|
}
|
|
|
|
pionConf := pion.Configuration{
|
|
ICEServers: cfg.Mod.IceServers,
|
|
SDPSemantics: pion.SDPSemanticsUnifiedPlanWithFallback,
|
|
}
|
|
|
|
NewPConn = func() (*pion.PeerConnection, error) {
|
|
return pionAPI.NewPeerConnection(pionConf)
|
|
}
|
|
|
|
candidates = cfg.Mod.Candidates
|
|
|
|
api.HandleWS("webrtc/offer", asyncHandler)
|
|
api.HandleWS("webrtc/candidate", candidateHandler)
|
|
|
|
api.HandleFunc("api/webrtc", syncHandler)
|
|
}
|
|
|
|
var Port string
|
|
var log zerolog.Logger
|
|
|
|
var NewPConn func() (*pion.PeerConnection, error)
|
|
|
|
func asyncHandler(tr *api.Transport, msg *api.Message) error {
|
|
src := tr.Request.URL.Query().Get("src")
|
|
stream := streams.GetOrNew(src)
|
|
if stream == nil {
|
|
return errors.New(api.StreamNotFound)
|
|
}
|
|
|
|
log.Debug().Str("url", src).Msg("[webrtc] new consumer")
|
|
|
|
var err error
|
|
|
|
// create new webrtc instance
|
|
conn := new(webrtc.Conn)
|
|
conn.Conn, err = NewPConn()
|
|
if err != nil {
|
|
log.Error().Err(err).Caller().Send()
|
|
return err
|
|
}
|
|
|
|
conn.UserAgent = tr.Request.UserAgent()
|
|
conn.Listen(func(msg interface{}) {
|
|
switch msg := msg.(type) {
|
|
case pion.PeerConnectionState:
|
|
if msg == pion.PeerConnectionStateClosed {
|
|
stream.RemoveConsumer(conn)
|
|
}
|
|
case *pion.ICECandidate:
|
|
if msg != nil {
|
|
s := msg.ToJSON().Candidate
|
|
log.Trace().Str("candidate", s).Msg("[webrtc] local")
|
|
tr.Write(&api.Message{Type: "webrtc/candidate", Value: s})
|
|
}
|
|
}
|
|
})
|
|
|
|
// 1. SetOffer, so we can get remote client codecs
|
|
offer := msg.Value.(string)
|
|
log.Trace().Msgf("[webrtc] offer:\n%s", offer)
|
|
|
|
if err = conn.SetOffer(offer); err != nil {
|
|
log.Warn().Err(err).Caller().Send()
|
|
return err
|
|
}
|
|
|
|
// 2. AddConsumer, so we get new tracks
|
|
if err = stream.AddConsumer(conn); err != nil {
|
|
log.Debug().Err(err).Msg("[webrtc] add consumer")
|
|
_ = conn.Conn.Close()
|
|
return err
|
|
}
|
|
|
|
conn.Init()
|
|
|
|
// 3. Exchange SDP without waiting all candidates
|
|
answer, err := conn.GetAnswer()
|
|
log.Trace().Msgf("[webrtc] answer\n%s", answer)
|
|
|
|
if err != nil {
|
|
log.Error().Err(err).Caller().Send()
|
|
return err
|
|
}
|
|
|
|
tr.Consumer = conn
|
|
|
|
tr.Write(&api.Message{Type: "webrtc/answer", Value: answer})
|
|
|
|
asyncCandidates(tr)
|
|
|
|
return nil
|
|
}
|
|
|
|
type SDP struct {
|
|
Type string `json:"type"`
|
|
Sdp string `json:"sdp"`
|
|
}
|
|
|
|
// syncHandler
|
|
func syncHandler(w http.ResponseWriter, r *http.Request) {
|
|
url := r.URL.Query().Get("src")
|
|
stream := streams.Get(url)
|
|
if stream == nil {
|
|
return
|
|
}
|
|
|
|
var offer string
|
|
|
|
ct := r.Header.Get("Content-Type")
|
|
if ct != "" {
|
|
ct, _, _ = mime.ParseMediaType(ct)
|
|
}
|
|
|
|
if ct == "application/json" {
|
|
var v SDP
|
|
if err := json.NewDecoder(r.Body).Decode(&v); err != nil {
|
|
log.Error().Err(err).Caller().Send()
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
offer = v.Sdp
|
|
} else {
|
|
body, err := io.ReadAll(r.Body)
|
|
if err != nil {
|
|
log.Error().Err(err).Caller().Send()
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
offer = string(body)
|
|
}
|
|
|
|
answer, err := ExchangeSDP(stream, offer, r.UserAgent())
|
|
if err != nil {
|
|
log.Error().Err(err).Caller().Send()
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// send SDP to client
|
|
if ct == "application/json" {
|
|
w.Header().Set("Content-Type", ct)
|
|
|
|
v := SDP{Sdp: answer, Type: "answer"}
|
|
if err = json.NewEncoder(w).Encode(v); err != nil {
|
|
log.Error().Err(err).Caller().Send()
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
} else {
|
|
if _, err = w.Write([]byte(answer)); err != nil {
|
|
log.Error().Err(err).Caller().Send()
|
|
}
|
|
}
|
|
}
|
|
|
|
func ExchangeSDP(
|
|
stream *streams.Stream, offer string, userAgent string,
|
|
) (answer string, err error) {
|
|
// create new webrtc instance
|
|
conn := new(webrtc.Conn)
|
|
conn.Conn, err = NewPConn()
|
|
if err != nil {
|
|
log.Error().Err(err).Caller().Msg("NewPConn")
|
|
return
|
|
}
|
|
|
|
conn.UserAgent = userAgent
|
|
conn.Listen(func(msg interface{}) {
|
|
switch msg := msg.(type) {
|
|
case pion.PeerConnectionState:
|
|
if msg == pion.PeerConnectionStateClosed {
|
|
stream.RemoveConsumer(conn)
|
|
}
|
|
}
|
|
})
|
|
|
|
// 1. SetOffer, so we can get remote client codecs
|
|
log.Trace().Msgf("[webrtc] offer:\n%s", offer)
|
|
|
|
if err = conn.SetOffer(offer); err != nil {
|
|
log.Warn().Err(err).Caller().Msg("conn.SetOffer")
|
|
return
|
|
}
|
|
|
|
// 2. AddConsumer, so we get new tracks
|
|
if err = stream.AddConsumer(conn); err != nil {
|
|
log.Warn().Err(err).Caller().Msg("stream.AddConsumer")
|
|
_ = conn.Conn.Close()
|
|
return
|
|
}
|
|
|
|
conn.Init()
|
|
|
|
// exchange sdp without waiting all candidates
|
|
//answer, err := conn.ExchangeSDP(offer, false)
|
|
answer, err = conn.GetCompleteAnswer()
|
|
if err == nil {
|
|
answer, err = syncCanditates(answer)
|
|
}
|
|
log.Trace().Msgf("[webrtc] answer\n%s", answer)
|
|
|
|
if err != nil {
|
|
log.Error().Err(err).Caller().Msg("conn.GetCompleteAnswer")
|
|
}
|
|
|
|
return
|
|
}
|