mirror of
https://github.com/m1k1o/neko.git
synced 2025-05-20 12:37:06 +02:00
add ice restarts.
This commit is contained in:
parent
d575d88daf
commit
09fdea1b11
6 changed files with 75 additions and 41 deletions
|
@ -1,6 +1,14 @@
|
|||
package webrtc
|
||||
|
||||
import "github.com/pion/webrtc/v3"
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/pion/webrtc/v3"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// how long is can take between sending offer and connecting
|
||||
const offerTimeout = 10 * time.Second
|
||||
|
||||
type WebRTCPeerCtx struct {
|
||||
api *webrtc.API
|
||||
|
@ -9,6 +17,48 @@ type WebRTCPeerCtx struct {
|
|||
changeVideo func(videoID string) error
|
||||
}
|
||||
|
||||
func (peer *WebRTCPeerCtx) CreateOffer(ICETrickle bool, ICERestart bool) (*webrtc.SessionDescription, error) {
|
||||
// offer timeout
|
||||
go func() {
|
||||
time.Sleep(offerTimeout)
|
||||
|
||||
// already disconnected
|
||||
if peer.connection.ConnectionState() == webrtc.PeerConnectionStateClosed {
|
||||
return
|
||||
}
|
||||
|
||||
// not connected
|
||||
if peer.connection.ConnectionState() != webrtc.PeerConnectionStateConnected {
|
||||
log.Warn().Msg("connection timeouted, closing")
|
||||
peer.connection.Close()
|
||||
}
|
||||
}()
|
||||
|
||||
offer, err := peer.connection.CreateOffer(&webrtc.OfferOptions{
|
||||
ICERestart: ICERestart,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !ICETrickle {
|
||||
// Create channel that is blocked until ICE Gathering is complete
|
||||
gatherComplete := webrtc.GatheringCompletePromise(peer.connection)
|
||||
|
||||
if err := peer.connection.SetLocalDescription(offer); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
<-gatherComplete
|
||||
} else {
|
||||
if err := peer.connection.SetLocalDescription(offer); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return peer.connection.LocalDescription(), nil
|
||||
}
|
||||
|
||||
func (peer *WebRTCPeerCtx) SignalAnswer(sdp string) error {
|
||||
return peer.connection.SetRemoteDescription(webrtc.SessionDescription{
|
||||
SDP: sdp,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue