-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrtc_source_sender.go
279 lines (215 loc) · 6.39 KB
/
wrtc_source_sender.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// WebRTC source sender.
// Sends data to other nodes.
package main
import (
"encoding/json"
"sync"
"github.com/pion/webrtc/v3"
)
// WRTC_Source_Sender - This data structure contains the status data
// of an inter-node OUTPUT connection
// Sends the tracks of a WRTC_Source to other node
type WRTC_Source_Sender struct {
sid string // WebRTC stream ID
remoteId string // ID of the remote node
node *WebRTC_CDN_Node // Reference to the node
closed bool // True when the source is no longer active
peerConnection *webrtc.PeerConnection // WebRTC Peer Connection
statusMutex *sync.Mutex // Mutex to control access to the struct
hasAudio bool
localTrackAudio *webrtc.TrackLocalStaticRTP // Audio track
hasVideo bool
localTrackVideo *webrtc.TrackLocalStaticRTP // Video track
}
// Initialize
func (sender *WRTC_Source_Sender) init() {
sender.statusMutex = &sync.Mutex{}
sender.closed = false
}
// Receive the tracks from local source
func (sender *WRTC_Source_Sender) onTracksReady(localTrackVideo *webrtc.TrackLocalStaticRTP, localTrackAudio *webrtc.TrackLocalStaticRTP) {
sender.statusMutex.Lock()
defer sender.statusMutex.Unlock()
// Set video track
sender.localTrackVideo = localTrackVideo
sender.hasVideo = localTrackVideo != nil
// Set audio track
sender.localTrackAudio = localTrackAudio
sender.hasAudio = localTrackAudio != nil
// If there is an existing connection, close it
if sender.peerConnection != nil {
sender.peerConnection.OnICECandidate(nil)
sender.peerConnection.OnConnectionStateChange(nil)
sender.peerConnection.Close()
}
sender.peerConnection = nil
// Run the connection process
go sender.runAfterTracksReady()
}
// Starts the peer connection, generates the offer and sets up the event handlers
func (sender *WRTC_Source_Sender) runAfterTracksReady() {
sender.statusMutex.Lock()
defer sender.statusMutex.Unlock()
if !sender.hasVideo && !sender.hasAudio {
return // Nothing to do
}
peerConnectionConfig := loadWebRTCConfig() // Load config
// Create a new PeerConnection
peerConnection, err := webrtc.NewPeerConnection(peerConnectionConfig)
if err != nil {
LogError(err)
return
}
sender.peerConnection = peerConnection
// ICE candidate handler
peerConnection.OnICECandidate(func(i *webrtc.ICECandidate) {
sender.statusMutex.Lock()
defer sender.statusMutex.Unlock()
if i != nil {
b, e := json.Marshal(i.ToJSON())
if e != nil {
LogError(e)
} else {
sender.sendICECandidate(string(b)) // Send candidate to the remote node
}
} else {
sender.sendICECandidate("") // End of candidates
}
})
// Connection status handler
peerConnection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
if state == webrtc.PeerConnectionStateClosed || state == webrtc.PeerConnectionStateFailed {
LogDebug("Source Sender Disconnected | RemoteNode: " + sender.remoteId + " | SreamID: " + sender.sid)
sender.onClose() // If the connection fails, close the sender
} else if state == webrtc.PeerConnectionStateConnected {
LogDebug("Source Sender Connected | RemoteNode: " + sender.remoteId + " | SreamID: " + sender.sid)
}
})
// Include the audio track
if sender.hasAudio {
audioSender, err := peerConnection.AddTrack(sender.localTrackAudio)
if err != nil {
LogError(err)
return
}
go readPacketsFromRTPSender(audioSender)
}
// Include the video track
if sender.hasVideo {
videoSender, err := peerConnection.AddTrack(sender.localTrackVideo)
if err != nil {
LogError(err)
return
}
go readPacketsFromRTPSender(videoSender)
}
// Generate offer
offer, err := peerConnection.CreateOffer(nil)
if err != nil {
LogError(err)
return
}
// Sets the LocalDescription, and starts our UDP listeners
err = peerConnection.SetLocalDescription(offer)
if err != nil {
LogError(err)
return
}
// Send OFFER to the remote node
offerJSON, e := json.Marshal(offer)
if e != nil {
LogError(e)
return
}
sender.sendOffer(string(offerJSON))
}
// Called if the peer connection is disconnected
func (sender *WRTC_Source_Sender) onClose() {
sender.statusMutex.Lock()
defer sender.statusMutex.Unlock()
sender.peerConnection = nil
// Remove the sender from the node
sender.node.onSenderClosed(sender)
}
// SEND
// Send offer SDP message to the remote node
func (sender *WRTC_Source_Sender) sendOffer(offerJSON string) {
mp := make(map[string]string)
mp["type"] = "OFFER"
mp["src"] = sender.node.id
mp["dst"] = sender.remoteId
mp["sid"] = sender.sid
if sender.hasVideo {
mp["video"] = "true"
}
if sender.hasAudio {
mp["audio"] = "true"
}
mp["data"] = offerJSON
sender.node.sendRedisMessage(sender.remoteId, &mp)
}
// Send candidate message to the remote node
func (sender *WRTC_Source_Sender) sendICECandidate(candidate string) {
mp := make(map[string]string)
mp["type"] = "CANDIDATE"
mp["src"] = sender.node.id
mp["dst"] = sender.remoteId
mp["sid"] = sender.sid
mp["data"] = candidate
sender.node.sendRedisMessage(sender.remoteId, &mp)
}
// RECEIVE
// Call when an ICE Candidate message is received from the remote node
func (sender *WRTC_Source_Sender) onICECandidate(candidateJSON string) {
sender.statusMutex.Lock()
defer sender.statusMutex.Unlock()
if sender.peerConnection == nil {
return
}
if candidateJSON == "" {
return
}
candidate := webrtc.ICECandidateInit{}
err := json.Unmarshal([]byte(candidateJSON), &candidate)
if err != nil {
LogError(err)
}
err = sender.peerConnection.AddICECandidate(candidate)
if err != nil {
LogError(err)
}
}
// Call when the ANSWER is received from the remote node
func (sender *WRTC_Source_Sender) onAnswer(answerJSON string) {
sender.statusMutex.Lock()
defer sender.statusMutex.Unlock()
if sender.peerConnection == nil {
return
}
sd := webrtc.SessionDescription{}
err := json.Unmarshal([]byte(answerJSON), &sd)
if err != nil {
LogError(err)
}
// Set the remote SessionDescription
err = sender.peerConnection.SetRemoteDescription(sd)
if err != nil {
LogError(err)
}
}
// Close the sender
func (sender *WRTC_Source_Sender) close() {
sender.statusMutex.Lock()
defer sender.statusMutex.Unlock()
sender.closed = true
if sender.peerConnection != nil {
sender.peerConnection.OnICECandidate(nil)
sender.peerConnection.OnConnectionStateChange(nil)
sender.peerConnection.Close()
}
sender.peerConnection = nil
sender.hasAudio = false
sender.hasVideo = false
sender.localTrackAudio = nil
sender.localTrackVideo = nil
}