demo / network-computer
Public
Copy of github.com/askrobots/network-computer (MIT): a desk you reach from anything. The original lives on GitHub.
GitStable demo
README: say this is a copy and where the original is
cde82c2
13 hours, 39 minutes ago
History
hub.go · 3.3 KB · 130 lines
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "log" |
| 6 | "sync" |
| 7 | |
| 8 | "github.com/pion/webrtc/v4/pkg/media" |
| 9 | ) |
| 10 | |
| 11 | // A hub runs one capture and encoder and gives every connected device a copy |
| 12 | // of its samples. The desk has one screen and one sound output: five devices |
| 13 | // used to mean five x11grab+x264 encoders (about 14% of a core each), which |
| 14 | // together with anything else pinned a 2-core desk (2026-09-25). |
| 15 | // |
| 16 | // Video: a device that joins mid-stream starts at the next keyframe (the GOP |
| 17 | // is 2 s), since frames before it cannot be decoded. A new size (a device |
| 18 | // asked the screen to change) restarts the one encoder for everyone, as the |
| 19 | // screen changed for everyone. The encoder stops when the last device leaves. |
| 20 | type hub struct { |
| 21 | name string |
| 22 | gated bool // wait for a keyframe before a new subscriber gets samples (video) |
| 23 | run func(ctx context.Context, opts captureOpts, w sampleWriter, onKeyframe func()) |
| 24 | |
| 25 | mu sync.Mutex |
| 26 | subs map[*hubSub]struct{} |
| 27 | opts captureOpts |
| 28 | cancel context.CancelFunc |
| 29 | done chan struct{} |
| 30 | nextKey bool |
| 31 | } |
| 32 | |
| 33 | type hubSub struct { |
| 34 | w sampleWriter |
| 35 | waiting bool |
| 36 | } |
| 37 | |
| 38 | func newHub(name string, gated bool, run func(ctx context.Context, opts captureOpts, w sampleWriter, onKeyframe func())) *hub { |
| 39 | return &hub{name: name, gated: gated, run: run, subs: map[*hubSub]struct{}{}} |
| 40 | } |
| 41 | |
| 42 | // join adds a device's track; the encoder starts with the first one. The |
| 43 | // returned function removes it again. |
| 44 | func (h *hub) join(w sampleWriter, opts captureOpts) (leave func()) { |
| 45 | s := &hubSub{w: w, waiting: h.gated} |
| 46 | h.mu.Lock() |
| 47 | h.subs[s] = struct{}{} |
| 48 | if h.cancel == nil { |
| 49 | h.startLocked(opts) |
| 50 | } |
| 51 | n := len(h.subs) |
| 52 | h.mu.Unlock() |
| 53 | log.Printf("%s: %d device(s) on one encoder", h.name, n) |
| 54 | return func() { |
| 55 | h.mu.Lock() |
| 56 | delete(h.subs, s) |
| 57 | empty := len(h.subs) == 0 |
| 58 | h.mu.Unlock() |
| 59 | if empty { |
| 60 | h.stop() |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // restart runs the encoder with new options (a new screen size) for everyone. |
| 66 | func (h *hub) restart(opts captureOpts) { |
| 67 | h.stop() |
| 68 | h.mu.Lock() |
| 69 | for h.cancel != nil { // a device joined meanwhile and started one: replace it |
| 70 | h.mu.Unlock() |
| 71 | h.stop() |
| 72 | h.mu.Lock() |
| 73 | } |
| 74 | defer h.mu.Unlock() |
| 75 | if len(h.subs) == 0 { |
| 76 | return |
| 77 | } |
| 78 | for s := range h.subs { |
| 79 | s.waiting = h.gated |
| 80 | } |
| 81 | log.Printf("%s: restarting at %dx%d, %s", h.name, opts.Width, opts.Height, opts.Bitrate) |
| 82 | h.startLocked(opts) |
| 83 | } |
| 84 | |
| 85 | func (h *hub) startLocked(opts captureOpts) { |
| 86 | ctx, cancel := context.WithCancel(context.Background()) |
| 87 | done := make(chan struct{}) |
| 88 | h.opts, h.cancel, h.done = opts, cancel, done |
| 89 | go func() { |
| 90 | h.run(ctx, opts, h, h.keyframe) |
| 91 | close(done) |
| 92 | }() |
| 93 | } |
| 94 | |
| 95 | func (h *hub) stop() { |
| 96 | h.mu.Lock() |
| 97 | cancel, done := h.cancel, h.done |
| 98 | h.cancel, h.done = nil, nil |
| 99 | h.mu.Unlock() |
| 100 | if cancel != nil { |
| 101 | cancel() |
| 102 | <-done |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // keyframe: the next sample holds a keyframe (the capture loop calls this |
| 107 | // while it assembles that access unit, before writing it). |
| 108 | func (h *hub) keyframe() { |
| 109 | h.mu.Lock() |
| 110 | h.nextKey = true |
| 111 | h.mu.Unlock() |
| 112 | } |
| 113 | |
| 114 | // WriteSample gives one sample to every device that can decode it. |
| 115 | func (h *hub) WriteSample(s media.Sample) error { |
| 116 | h.mu.Lock() |
| 117 | defer h.mu.Unlock() |
| 118 | key := h.nextKey |
| 119 | h.nextKey = false |
| 120 | for sub := range h.subs { |
| 121 | if sub.waiting { |
| 122 | if !key { |
| 123 | continue |
| 124 | } |
| 125 | sub.waiting = false |
| 126 | } |
| 127 | sub.w.WriteSample(s) // one slow or closed device must not stop the rest |
| 128 | } |
| 129 | return nil |
| 130 | } |