mirror of
https://github.com/AlexxIT/go2rtc.git
synced 2025-10-05 16:26:50 +08:00
Code refactoring for webrtc candidates
This commit is contained in:
@@ -29,7 +29,7 @@ func asyncCandidates(ctx *api.Context) {
|
||||
|
||||
log.Trace().Str("candidate", cand).Msg("[webrtc] config")
|
||||
|
||||
ctx.Write(&streamer.Message{Type: webrtc.MsgTypeCandidate, Value: cand})
|
||||
ctx.Write(&streamer.Message{Type: "webrtc/candidate", Value: cand})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,7 +84,8 @@ func candidateHandler(ctx *api.Context, msg *streamer.Message) {
|
||||
return
|
||||
}
|
||||
if conn := ctx.Consumer.(*webrtc.Conn); conn != nil {
|
||||
log.Trace().Str("candidate", msg.Value.(string)).Msg("[webrtc] remote")
|
||||
conn.Push(msg)
|
||||
s := msg.Value.(string)
|
||||
log.Trace().Str("candidate", s).Msg("[webrtc] remote")
|
||||
conn.AddCandidate(s)
|
||||
}
|
||||
}
|
||||
|
@@ -55,8 +55,8 @@ func Init() {
|
||||
|
||||
candidates = cfg.Mod.Candidates
|
||||
|
||||
api.HandleWS(webrtc.MsgTypeOffer, asyncHandler)
|
||||
api.HandleWS(webrtc.MsgTypeCandidate, candidateHandler)
|
||||
api.HandleWS("webrtc/offer", asyncHandler)
|
||||
api.HandleWS("webrtc/candidate", candidateHandler)
|
||||
|
||||
api.HandleFunc("api/webrtc", syncHandler)
|
||||
}
|
||||
@@ -92,10 +92,12 @@ func asyncHandler(ctx *api.Context, msg *streamer.Message) {
|
||||
if msg == pion.PeerConnectionStateClosed {
|
||||
stream.RemoveConsumer(conn)
|
||||
}
|
||||
case *streamer.Message:
|
||||
// subscribe on webrtc server candidates
|
||||
log.Trace().Str("candidate", msg.Value.(string)).Msg("[webrtc] local")
|
||||
ctx.Write(msg)
|
||||
case *pion.ICECandidate:
|
||||
if msg != nil {
|
||||
s := msg.ToJSON().Candidate
|
||||
log.Trace().Str("candidate", s).Msg("[webrtc] local")
|
||||
ctx.Write(&streamer.Message{Type: "webrtc/candidate", Value: s})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -119,7 +121,7 @@ func asyncHandler(ctx *api.Context, msg *streamer.Message) {
|
||||
|
||||
conn.Init()
|
||||
|
||||
// exchange sdp without waiting all candidates
|
||||
// 3. Exchange SDP without waiting all candidates
|
||||
answer, err := conn.GetAnswer()
|
||||
log.Trace().Msgf("[webrtc] answer\n%s", answer)
|
||||
|
||||
@@ -131,7 +133,7 @@ func asyncHandler(ctx *api.Context, msg *streamer.Message) {
|
||||
|
||||
ctx.Consumer = conn
|
||||
|
||||
ctx.Write(&streamer.Message{Type: webrtc.MsgTypeAnswer, Value: answer})
|
||||
ctx.Write(&streamer.Message{Type: "webrtc/answer", Value: answer})
|
||||
|
||||
asyncCandidates(ctx)
|
||||
}
|
||||
|
@@ -5,13 +5,6 @@ import (
|
||||
"github.com/pion/webrtc/v3"
|
||||
)
|
||||
|
||||
const (
|
||||
MsgTypeOffer = "webrtc/offer"
|
||||
MsgTypeOfferComplete = "webrtc/offer-complete"
|
||||
MsgTypeAnswer = "webrtc/answer"
|
||||
MsgTypeCandidate = "webrtc/candidate"
|
||||
)
|
||||
|
||||
type Conn struct {
|
||||
streamer.Element
|
||||
|
||||
@@ -28,11 +21,7 @@ type Conn struct {
|
||||
|
||||
func (c *Conn) Init() {
|
||||
c.Conn.OnICECandidate(func(candidate *webrtc.ICECandidate) {
|
||||
if candidate != nil {
|
||||
c.Fire(&streamer.Message{
|
||||
Type: MsgTypeCandidate, Value: candidate.ToJSON().Candidate,
|
||||
})
|
||||
}
|
||||
c.Fire(candidate)
|
||||
})
|
||||
|
||||
c.Conn.OnTrack(func(remote *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
|
||||
|
@@ -108,14 +108,8 @@ func (c *Conn) AddTrack(media *streamer.Media, track *streamer.Track) *streamer.
|
||||
|
||||
//
|
||||
|
||||
func (c *Conn) Push(msg interface{}) {
|
||||
if msg := msg.(*streamer.Message); msg != nil {
|
||||
if msg.Type == MsgTypeCandidate {
|
||||
_ = c.Conn.AddICECandidate(webrtc.ICECandidateInit{
|
||||
Candidate: msg.Value.(string),
|
||||
})
|
||||
}
|
||||
}
|
||||
func (c *Conn) AddCandidate(candidate string) {
|
||||
_ = c.Conn.AddICECandidate(webrtc.ICECandidateInit{Candidate: candidate})
|
||||
}
|
||||
|
||||
func (c *Conn) MarshalJSON() ([]byte, error) {
|
||||
|
Reference in New Issue
Block a user