forked from jetkvm/kvm
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathstream_broadcaster.go
More file actions
59 lines (52 loc) · 1.29 KB
/
stream_broadcaster.go
File metadata and controls
59 lines (52 loc) · 1.29 KB
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
package kvm
import (
"sync"
"github.com/google/uuid"
)
type VideoBroadcaster struct {
subscribers map[string]chan []byte
lock sync.RWMutex
onFirstSubscribe func()
onLastUnsubscribe func()
}
var videoBroadcaster = &VideoBroadcaster{
subscribers: make(map[string]chan []byte),
}
func (b *VideoBroadcaster) Subscribe() (string, chan []byte) {
b.lock.Lock()
defer b.lock.Unlock()
id := uuid.New().String()
// Buffer a bit to avoid dropping frames too easily,
// but not too much to avoid latency build-up
ch := make(chan []byte, 200)
wasEmpty := len(b.subscribers) == 0
b.subscribers[id] = ch
if wasEmpty && b.onFirstSubscribe != nil {
b.onFirstSubscribe()
}
return id, ch
}
func (b *VideoBroadcaster) Unsubscribe(id string) {
b.lock.Lock()
defer b.lock.Unlock()
if ch, ok := b.subscribers[id]; ok {
close(ch)
delete(b.subscribers, id)
if len(b.subscribers) == 0 && b.onLastUnsubscribe != nil {
b.onLastUnsubscribe()
}
}
}
func (b *VideoBroadcaster) Broadcast(data []byte) {
b.lock.RLock()
defer b.lock.RUnlock()
for _, ch := range b.subscribers {
// Non-blocking send
select {
case ch <- data:
default:
// Drop frame if channel is full to avoid blocking other subscribers
// Ideally we should have a ring buffer or similar, but this is simple
}
}
}