askrobots / network-computer
Public
Your phone is the terminal. Stream a Linux/Mac desktop to an iPhone over WebRTC through NAT, with audio and input. Open source, single binaries.
Dan Quellhorst
Firefox policy: no WebRTC mDNS (a page using WebRTC made Firefox listen on UDP 5353)
a1d92f3
1 month, 3 weeks ago
History
oggopus_test.go · 2.2 KB · 69 lines
| 1 | package oggopus |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "io" |
| 6 | "testing" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | // page builds an Ogg page from lacing values and data (no checksum: the |
| 11 | // reader does not verify it). |
| 12 | func page(lacing []byte, data []byte) []byte { |
| 13 | h := make([]byte, 27) |
| 14 | copy(h, "OggS") |
| 15 | h[26] = byte(len(lacing)) |
| 16 | return append(append(h, lacing...), data...) |
| 17 | } |
| 18 | |
| 19 | func TestSplitsPagesIntoPackets(t *testing.T) { |
| 20 | a := append([]byte{0xFC}, bytes.Repeat([]byte{1}, 99)...) // 100 bytes |
| 21 | b := append([]byte{0xFC}, bytes.Repeat([]byte{2}, 119)...) // 120 bytes |
| 22 | long := append([]byte{0xFC}, bytes.Repeat([]byte{3}, 299)...) // 300 bytes: spans two pages |
| 23 | var stream []byte |
| 24 | stream = append(stream, page([]byte{19}, []byte("OpusHead........... "[:19]))...) |
| 25 | stream = append(stream, page([]byte{8}, []byte("OpusTags"))...) |
| 26 | stream = append(stream, page([]byte{100, 120}, append(append([]byte{}, a...), b...))...) // two packets on one page |
| 27 | stream = append(stream, page([]byte{255}, long[:255])...) // continues... |
| 28 | stream = append(stream, page([]byte{45}, long[255:])...) // ...and ends here |
| 29 | r := NewReader(bytes.NewReader(stream)) |
| 30 | var got [][]byte |
| 31 | for { |
| 32 | p, err := r.Next() |
| 33 | if err == io.EOF { |
| 34 | break |
| 35 | } |
| 36 | if err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | got = append(got, p) |
| 40 | } |
| 41 | want := [][]byte{a, b, long} |
| 42 | if len(got) != len(want) { |
| 43 | t.Fatalf("got %d packets, want %d (headers must be skipped, pages split)", len(got), len(want)) |
| 44 | } |
| 45 | for i := range want { |
| 46 | if !bytes.Equal(got[i], want[i]) { |
| 47 | t.Errorf("packet %d differs (len %d, want %d)", i, len(got[i]), len(want[i])) |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestDuration(t *testing.T) { |
| 53 | cases := []struct { |
| 54 | p []byte |
| 55 | want time.Duration |
| 56 | }{ |
| 57 | {[]byte{31<<3 | 0}, 20 * time.Millisecond}, // CELT 20 ms, one frame |
| 58 | {[]byte{31<<3 | 1}, 40 * time.Millisecond}, // two frames |
| 59 | {[]byte{31<<3 | 3, 3}, 60 * time.Millisecond}, // code 3, three frames |
| 60 | {[]byte{1<<3 | 0}, 20 * time.Millisecond}, // SILK 20 ms |
| 61 | {[]byte{16<<3 | 0}, 2500 * time.Microsecond}, // CELT 2.5 ms |
| 62 | {nil, 0}, |
| 63 | } |
| 64 | for _, c := range cases { |
| 65 | if got := Duration(c.p); got != c.want { |
| 66 | t.Errorf("Duration(%x) = %v, want %v", c.p, got, c.want) |
| 67 | } |
| 68 | } |
| 69 | } |