forked from livekit/server-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport.go
142 lines (119 loc) · 3.45 KB
/
transport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package lksdk
import (
"sync"
"time"
"github.com/bep/debounce"
"github.com/pion/interceptor"
"github.com/pion/sdp/v3"
"github.com/pion/webrtc/v3"
)
const (
negotiationFrequency = 150 * time.Millisecond
)
// PCTransport is a wrapper around PeerConnection, with some helper methods
type PCTransport struct {
pc *webrtc.PeerConnection
lock sync.Mutex
pendingCandidates []webrtc.ICECandidateInit
debouncedNegotiate func(func())
renegotiate bool
OnOffer func(description webrtc.SessionDescription)
}
func NewPCTransport(iceServers []webrtc.ICEServer) (*PCTransport, error) {
m := &webrtc.MediaEngine{}
if err := m.RegisterDefaultCodecs(); err != nil {
return nil, err
}
audioLevelExtension := webrtc.RTPHeaderExtensionCapability{URI: sdp.AudioLevelURI}
if err := m.RegisterHeaderExtension(audioLevelExtension, webrtc.RTPCodecTypeAudio); err != nil {
return nil, err
}
sdesMidExtension := webrtc.RTPHeaderExtensionCapability{URI: sdp.SDESMidURI}
if err := m.RegisterHeaderExtension(sdesMidExtension, webrtc.RTPCodecTypeVideo); err != nil {
return nil, err
}
sdesRtpStreamIdExtension := webrtc.RTPHeaderExtensionCapability{URI: sdp.SDESRTPStreamIDURI}
if err := m.RegisterHeaderExtension(sdesRtpStreamIdExtension, webrtc.RTPCodecTypeVideo); err != nil {
return nil, err
}
i := &interceptor.Registry{}
if err := webrtc.RegisterDefaultInterceptors(m, i); err != nil {
return nil, err
}
api := webrtc.NewAPI(webrtc.WithMediaEngine(m), webrtc.WithInterceptorRegistry(i))
pc, err := api.NewPeerConnection(webrtc.Configuration{ICEServers: iceServers})
if err != nil {
return nil, err
}
t := &PCTransport{
pc: pc,
debouncedNegotiate: debounce.New(negotiationFrequency),
}
return t, nil
}
func (t *PCTransport) AddICECandidate(candidate webrtc.ICECandidateInit) error {
if t.pc.RemoteDescription() == nil {
t.lock.Lock()
t.pendingCandidates = append(t.pendingCandidates, candidate)
t.lock.Unlock()
return nil
}
return t.pc.AddICECandidate(candidate)
}
func (t *PCTransport) PeerConnection() *webrtc.PeerConnection {
return t.pc
}
func (t *PCTransport) IsConnected() bool {
return t.pc.ICEConnectionState() == webrtc.ICEConnectionStateConnected
}
func (t *PCTransport) Close() error {
return t.pc.Close()
}
func (t *PCTransport) SetRemoteDescription(sd webrtc.SessionDescription) error {
if err := t.pc.SetRemoteDescription(sd); err != nil {
return err
}
t.lock.Lock()
defer t.lock.Unlock()
for _, c := range t.pendingCandidates {
if err := t.pc.AddICECandidate(c); err != nil {
return err
}
}
t.pendingCandidates = nil
if t.renegotiate {
t.renegotiate = false
go t.createAndSendOffer(nil)
}
return nil
}
func (t *PCTransport) Negotiate() {
t.debouncedNegotiate(func() {
t.createAndSendOffer(nil)
})
}
func (t *PCTransport) createAndSendOffer(options *webrtc.OfferOptions) {
if t.OnOffer == nil {
return
}
t.lock.Lock()
defer t.lock.Unlock()
// TODO: does not support ice restart yet
//if options.ICERestart {
// logger.V(1).Info("restarting ICE")
//}
if t.pc.SignalingState() == webrtc.SignalingStateHaveLocalOffer {
t.renegotiate = true
return
}
logger.V(1).Info("starting to negotiate")
offer, err := t.pc.CreateOffer(options)
logger.V(1).Info("create offer", "offer", offer.SDP)
if err != nil {
logger.Error(err, "could not negotiate")
}
if err := t.pc.SetLocalDescription(offer); err != nil {
logger.Error(err, "could not set local description")
}
t.OnOffer(offer)
}