Update App version to 0.1.1

Signed-off-by: luckfox-eng29 <eng29@luckfox.com>
This commit is contained in:
luckfox-eng29
2026-02-05 11:28:14 +08:00
parent 5e17c52afc
commit 9a4e604c61
289 changed files with 23076 additions and 12473 deletions

59
stream_broadcaster.go Normal file
View File

@@ -0,0 +1,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
}
}
}