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")
|
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
|
return
|
||||||
}
|
}
|
||||||
if conn := ctx.Consumer.(*webrtc.Conn); conn != nil {
|
if conn := ctx.Consumer.(*webrtc.Conn); conn != nil {
|
||||||
log.Trace().Str("candidate", msg.Value.(string)).Msg("[webrtc] remote")
|
s := msg.Value.(string)
|
||||||
conn.Push(msg)
|
log.Trace().Str("candidate", s).Msg("[webrtc] remote")
|
||||||
|
conn.AddCandidate(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -55,8 +55,8 @@ func Init() {
|
|||||||
|
|
||||||
candidates = cfg.Mod.Candidates
|
candidates = cfg.Mod.Candidates
|
||||||
|
|
||||||
api.HandleWS(webrtc.MsgTypeOffer, asyncHandler)
|
api.HandleWS("webrtc/offer", asyncHandler)
|
||||||
api.HandleWS(webrtc.MsgTypeCandidate, candidateHandler)
|
api.HandleWS("webrtc/candidate", candidateHandler)
|
||||||
|
|
||||||
api.HandleFunc("api/webrtc", syncHandler)
|
api.HandleFunc("api/webrtc", syncHandler)
|
||||||
}
|
}
|
||||||
@@ -92,10 +92,12 @@ func asyncHandler(ctx *api.Context, msg *streamer.Message) {
|
|||||||
if msg == pion.PeerConnectionStateClosed {
|
if msg == pion.PeerConnectionStateClosed {
|
||||||
stream.RemoveConsumer(conn)
|
stream.RemoveConsumer(conn)
|
||||||
}
|
}
|
||||||
case *streamer.Message:
|
case *pion.ICECandidate:
|
||||||
// subscribe on webrtc server candidates
|
if msg != nil {
|
||||||
log.Trace().Str("candidate", msg.Value.(string)).Msg("[webrtc] local")
|
s := msg.ToJSON().Candidate
|
||||||
ctx.Write(msg)
|
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()
|
conn.Init()
|
||||||
|
|
||||||
// exchange sdp without waiting all candidates
|
// 3. Exchange SDP without waiting all candidates
|
||||||
answer, err := conn.GetAnswer()
|
answer, err := conn.GetAnswer()
|
||||||
log.Trace().Msgf("[webrtc] answer\n%s", answer)
|
log.Trace().Msgf("[webrtc] answer\n%s", answer)
|
||||||
|
|
||||||
@@ -131,7 +133,7 @@ func asyncHandler(ctx *api.Context, msg *streamer.Message) {
|
|||||||
|
|
||||||
ctx.Consumer = conn
|
ctx.Consumer = conn
|
||||||
|
|
||||||
ctx.Write(&streamer.Message{Type: webrtc.MsgTypeAnswer, Value: answer})
|
ctx.Write(&streamer.Message{Type: "webrtc/answer", Value: answer})
|
||||||
|
|
||||||
asyncCandidates(ctx)
|
asyncCandidates(ctx)
|
||||||
}
|
}
|
||||||
|
@@ -5,13 +5,6 @@ import (
|
|||||||
"github.com/pion/webrtc/v3"
|
"github.com/pion/webrtc/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
MsgTypeOffer = "webrtc/offer"
|
|
||||||
MsgTypeOfferComplete = "webrtc/offer-complete"
|
|
||||||
MsgTypeAnswer = "webrtc/answer"
|
|
||||||
MsgTypeCandidate = "webrtc/candidate"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Conn struct {
|
type Conn struct {
|
||||||
streamer.Element
|
streamer.Element
|
||||||
|
|
||||||
@@ -28,11 +21,7 @@ type Conn struct {
|
|||||||
|
|
||||||
func (c *Conn) Init() {
|
func (c *Conn) Init() {
|
||||||
c.Conn.OnICECandidate(func(candidate *webrtc.ICECandidate) {
|
c.Conn.OnICECandidate(func(candidate *webrtc.ICECandidate) {
|
||||||
if candidate != nil {
|
c.Fire(candidate)
|
||||||
c.Fire(&streamer.Message{
|
|
||||||
Type: MsgTypeCandidate, Value: candidate.ToJSON().Candidate,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
c.Conn.OnTrack(func(remote *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
|
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{}) {
|
func (c *Conn) AddCandidate(candidate string) {
|
||||||
if msg := msg.(*streamer.Message); msg != nil {
|
_ = c.Conn.AddICECandidate(webrtc.ICECandidateInit{Candidate: candidate})
|
||||||
if msg.Type == MsgTypeCandidate {
|
|
||||||
_ = c.Conn.AddICECandidate(webrtc.ICECandidateInit{
|
|
||||||
Candidate: msg.Value.(string),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Conn) MarshalJSON() ([]byte, error) {
|
func (c *Conn) MarshalJSON() ([]byte, error) {
|
||||||
|
Reference in New Issue
Block a user