-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpeerConnectionState.go
99 lines (91 loc) · 2.61 KB
/
peerConnectionState.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
package main
import (
"log"
"github.com/pion/webrtc/v3"
)
type PeerConnectionState struct {
peerConnection *webrtc.PeerConnection
client *SSEClient
channels *WebRTCDataChannelStates
candidateFound chan *webrtc.ICECandidate
changeConnectionState chan webrtc.PeerConnectionState
addTrack chan *webrtc.TrackRemote
heartbeat chan int
}
func (ps *PeerConnectionState) Close() {
for i := 0; i < len(ps.heartbeat); i++ {
<-ps.heartbeat
}
close(ps.heartbeat)
close(ps.candidateFound)
close(ps.changeConnectionState)
close(ps.addTrack)
if ps.peerConnection.ConnectionState() != webrtc.PeerConnectionStateClosed {
ps.peerConnection.Close()
}
}
func NewPeerConnection() (*webrtc.PeerConnection, error) {
peerConnection, err := webrtc.NewPeerConnection(webrtc.Configuration{
ICEServers: []webrtc.ICEServer{
{
URLs: []string{
"stun:stun.l.google.com:19302",
},
},
},
})
if err != nil {
return nil, err
}
for _, typ := range []webrtc.RTPCodecType{webrtc.RTPCodecTypeVideo, webrtc.RTPCodecTypeAudio} {
if _, err := peerConnection.AddTransceiverFromKind(typ, webrtc.RTPTransceiverInit{
Direction: webrtc.RTPTransceiverDirectionRecvonly,
}); err != nil {
return nil, err
}
}
return peerConnection, nil
}
func NewPeerConnectionState(client *SSEClient, peerConnection *webrtc.PeerConnection,
channels *WebRTCDataChannelStates) (*PeerConnectionState, error) {
heartbeat := make(chan int, 1)
candidateFound := make(chan *webrtc.ICECandidate)
changeConnectionState := make(chan webrtc.PeerConnectionState)
addTrack := make(chan *webrtc.TrackRemote)
heartbeat <- 1
peerConnection.OnICECandidate(func(i *webrtc.ICECandidate) {
if i == nil {
return
}
_, ok := <-heartbeat
if ok {
candidateFound <- i
heartbeat <- 1
}
})
peerConnection.OnConnectionStateChange(func(p webrtc.PeerConnectionState) {
_, ok := <-heartbeat
if ok {
changeConnectionState <- p
heartbeat <- 1
}
log.Printf("State: %s", p.String())
})
peerConnection.OnTrack(func(t *webrtc.TrackRemote, _ *webrtc.RTPReceiver) {
log.Printf("OnTrack ID:%s Kind:%s MSID:%s Codec:%s SSRC:%d StreamID:%s", t.ID(), t.Kind(), t.Msid(), t.Codec().MimeType, t.SSRC(), t.StreamID())
_, ok := <-heartbeat
if ok {
addTrack <- t
heartbeat <- 1
}
})
return &PeerConnectionState{
peerConnection: peerConnection,
client: client,
channels: channels,
candidateFound: candidateFound,
changeConnectionState: changeConnectionState,
addTrack: addTrack,
heartbeat: heartbeat,
}, nil
}