-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_sources.go
91 lines (73 loc) · 2.25 KB
/
node_sources.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
// WebRTC sources management
package main
// Checks if the node has a WebRTC source
// for the specified Stream Id (sid)
func (node *WebRTC_CDN_Node) resolveSource(sid string) bool {
node.mutexStatus.Lock()
defer node.mutexStatus.Unlock()
return node.sources[sid] != nil
}
// Registers a WebRTC source
func (node *WebRTC_CDN_Node) registerSource(source *WRTC_Source) {
node.mutexStatus.Lock()
defer node.mutexStatus.Unlock()
if node.sources[source.sid] != nil {
// Close the old source
s := node.sources[source.sid]
s.close(true, false)
}
node.sources[source.sid] = source
// Remove any relays for that source
if node.relays[source.sid] != nil {
node.relays[source.sid].close()
delete(node.relays, source.sid)
}
// Remove all the senders
if node.senders[source.sid] != nil {
for _, sender := range node.senders[source.sid] {
sender.close()
}
delete(node.senders, source.sid)
}
// Announce to other nodes
node.sendInfoMessage(REDIS_BROADCAST_CHANNEL, source.sid)
}
// Called when a WebRTC source is ready
// This means the tracks can be played
func (node *WebRTC_CDN_Node) onSourceReady(source *WRTC_Source) {
node.mutexStatus.Lock()
defer node.mutexStatus.Unlock()
source.ready = true
// Notify sinks
if node.sinks[source.sid] != nil {
for _, sink := range node.sinks[source.sid] {
sink.onTracksReady(source.localTrackVideo, source.localTrackAudio)
}
}
// Notify senders
if node.senders[source.sid] != nil {
for _, sender := range node.senders[source.sid] {
sender.onTracksReady(source.localTrackVideo, source.localTrackAudio)
}
}
}
// Called when the WebRTC source closes by itself (by the client, a disconnection, etc)
// Do not call from another status methods
func (node *WebRTC_CDN_Node) onSourceClosed(source *WRTC_Source) {
node.mutexStatus.Lock()
defer node.mutexStatus.Unlock()
delete(node.sources, source.sid)
// Close and remove all the senders
if node.senders[source.sid] != nil {
for _, sender := range node.senders[source.sid] {
sender.close()
}
delete(node.senders, source.sid)
}
// Any sinks waiting, tell them the tracks are closed
if node.sinks[source.sid] != nil {
for _, sink := range node.sinks[source.sid] {
sink.onTracksClosed(source.localTrackVideo, source.localTrackAudio)
}
}
}