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_test.go · 2.4 KB · 90 lines
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "sync" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/pion/webrtc/v4/pkg/media" |
| 10 | ) |
| 11 | |
| 12 | type countWriter struct { |
| 13 | mu sync.Mutex |
| 14 | n int |
| 15 | } |
| 16 | |
| 17 | func (c *countWriter) WriteSample(media.Sample) error { c.mu.Lock(); c.n++; c.mu.Unlock(); return nil } |
| 18 | func (c *countWriter) count() int { c.mu.Lock(); defer c.mu.Unlock(); return c.n } |
| 19 | |
| 20 | // A fake encoder: a keyframe every 10th sample, until cancelled. |
| 21 | func fakeEncoder(starts *int, mu *sync.Mutex) func(context.Context, captureOpts, sampleWriter, func()) { |
| 22 | return func(ctx context.Context, _ captureOpts, w sampleWriter, key func()) { |
| 23 | mu.Lock() |
| 24 | *starts++ |
| 25 | mu.Unlock() |
| 26 | for i := 0; ctx.Err() == nil; i++ { |
| 27 | if i%10 == 0 { |
| 28 | key() |
| 29 | } |
| 30 | w.WriteSample(media.Sample{Data: []byte{byte(i)}}) |
| 31 | time.Sleep(time.Millisecond) |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func TestHubSharesOneEncoder(t *testing.T) { |
| 37 | var starts int |
| 38 | var mu sync.Mutex |
| 39 | h := newHub("video", true, fakeEncoder(&starts, &mu)) |
| 40 | n := func() int { mu.Lock(); defer mu.Unlock(); return starts } |
| 41 | a, b := &countWriter{}, &countWriter{} |
| 42 | leaveA := h.join(a, captureOpts{}) |
| 43 | time.Sleep(30 * time.Millisecond) |
| 44 | leaveB := h.join(b, captureOpts{}) |
| 45 | time.Sleep(60 * time.Millisecond) |
| 46 | if n() != 1 { |
| 47 | t.Fatalf("two devices started %d encoders, want 1", n()) |
| 48 | } |
| 49 | if a.count() == 0 || b.count() == 0 { |
| 50 | t.Fatalf("both devices get samples: a=%d b=%d", a.count(), b.count()) |
| 51 | } |
| 52 | h.restart(captureOpts{Width: 800}) |
| 53 | time.Sleep(30 * time.Millisecond) |
| 54 | if n() != 2 { |
| 55 | t.Fatalf("restart: %d starts, want 2", n()) |
| 56 | } |
| 57 | leaveA() |
| 58 | leaveB() |
| 59 | h.mu.Lock() |
| 60 | running := h.cancel != nil |
| 61 | h.mu.Unlock() |
| 62 | if running { |
| 63 | t.Fatal("the encoder keeps running with nobody left") |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func TestHubLateJoinerStartsAtKeyframe(t *testing.T) { |
| 68 | var starts int |
| 69 | var mu sync.Mutex |
| 70 | got := []byte{} |
| 71 | var gmu sync.Mutex |
| 72 | h := newHub("video", true, fakeEncoder(&starts, &mu)) |
| 73 | first := &countWriter{} |
| 74 | leave := h.join(first, captureOpts{}) |
| 75 | defer leave() |
| 76 | time.Sleep(15 * time.Millisecond) // mid-GOP |
| 77 | late := sampleFunc(func(s media.Sample) { gmu.Lock(); got = append(got, s.Data[0]); gmu.Unlock() }) |
| 78 | leaveLate := h.join(late, captureOpts{}) |
| 79 | time.Sleep(40 * time.Millisecond) |
| 80 | leaveLate() |
| 81 | gmu.Lock() |
| 82 | defer gmu.Unlock() |
| 83 | if len(got) == 0 || got[0]%10 != 0 { |
| 84 | t.Fatalf("a late device's first sample is %v, want a keyframe (multiple of 10)", got) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | type sampleFunc func(media.Sample) |
| 89 | |
| 90 | func (f sampleFunc) WriteSample(s media.Sample) error { f(s); return nil } |