Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
video-player.test.ts proves stop() closes the socket and the decoder and that no frame is drawn after it - test only #427
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/426-video-player-test-ts-proves-stop-closes → main 1 commit fork
2 files
changed +34
−3
Software/so101/src/ui/video-player.js +1
| @@ -77,6 +77,7 @@ export class VideoPlayer { | ||
| 77 | 77 | } |
| 78 | 78 | |
| 79 | 79 | status(state) { |
| 80 | if (state === "none") this.stop(); // giving up releases the socket and the decoder | |
| 80 | 81 | this.o.onStatus?.({ state, age: this.age, rendition: this.rendition, ladder: this.ladder }); |
| 81 | 82 | } |
| 82 | 83 |
Software/so101/src/ui/video-player.test.ts +33 −3
| @@ -30,7 +30,10 @@ class FakeSocket { | ||
| 30 | 30 | send(s: string) { |
| 31 | 31 | this.sent.push(s); |
| 32 | 32 | } |
| 33 | close() {} | |
| 33 | closes = 0; | |
| 34 | close() { | |
| 35 | this.closes++; | |
| 36 | } | |
| 34 | 37 | } |
| 35 | 38 | |
| 36 | 39 | function setup() { |
| @@ -39,9 +42,12 @@ function setup() { | ||
| 39 | 42 | let configured: unknown; |
| 40 | 43 | let output: (f: unknown) => void = () => {}; |
| 41 | 44 | const drawn: unknown[] = []; |
| 45 | const decoders: FakeDecoder[] = []; | |
| 42 | 46 | class FakeDecoder { |
| 43 | 47 | decodeQueueSize = 0; |
| 48 | closes = 0; | |
| 44 | 49 | constructor(init: { output: (f: unknown) => void }) { |
| 50 | decoders.push(this); | |
| 45 | 51 | output = init.output; |
| 46 | 52 | } |
| 47 | 53 | configure(c: unknown) { |
| @@ -51,14 +57,16 @@ function setup() { | ||
| 51 | 57 | decoded.push({ type: chunk.type, timestamp: chunk.timestamp, bytes: chunk.data.length }); |
| 52 | 58 | output({ displayWidth: 1280, displayHeight: 720, close() {} }); |
| 53 | 59 | } |
| 54 | close() {} | |
| 60 | close() { | |
| 61 | this.closes++; | |
| 62 | } | |
| 55 | 63 | } |
| 56 | 64 | const canvas = { width: 0, height: 0, getContext: () => ({ drawImage: (f: unknown) => drawn.push(f) }) }; |
| 57 | 65 | let now = 10_000; |
| 58 | 66 | const player = new VideoPlayer({ url: "ws://x/video", canvas, onStatus: (s) => states.push(s.state), deps: { WebSocket: FakeSocket, VideoDecoder: FakeDecoder, EncodedVideoChunk: class { constructor(init: object) { Object.assign(this, init); } }, now: () => now } }); |
| 59 | 67 | player.start(); |
| 60 | 68 | const ws = FakeSocket.last; |
| 61 | return { player, ws, decoded, states, drawn, canvas, get configured() { return configured; }, tick: (ms: number) => (now += ms), set: (n: number) => (now = n), decoder: () => player.decoder }; | |
| 69 | return { player, ws, decoders, decoded, states, drawn, canvas, get configured() { return configured; }, tick: (ms: number) => (now += ms), set: (n: number) => (now = n), decoder: () => player.decoder }; | |
| 62 | 70 | } |
| 63 | 71 | |
| 64 | 72 | describe("video player", () => { |
| @@ -162,4 +170,26 @@ describe("video player", () => { | ||
| 162 | 170 | expect(t.states).toEqual(["none"]); |
| 163 | 171 | t.player.stop(); |
| 164 | 172 | }); |
| 173 | ||
| 174 | test("stop() closes the socket and the decoder once, ignores later pictures, and can be called twice", () => { | |
| 175 | const t = setup(); | |
| 176 | t.ws.onmessage!({ data: frame(true, 0, 10_000, pics[0]!) }); | |
| 177 | expect(t.decoders).toHaveLength(1); | |
| 178 | t.player.stop(); | |
| 179 | expect(t.ws.closes).toBe(1); | |
| 180 | expect(t.decoders[0]!.closes).toBe(1); | |
| 181 | t.ws.onmessage!({ data: frame(true, 0, 10_000, pics[5]!) }); | |
| 182 | expect(t.decoded).toHaveLength(1); | |
| 183 | expect(t.drawn).toHaveLength(1); | |
| 184 | expect(() => t.player.stop()).not.toThrow(); | |
| 185 | }); | |
| 186 | ||
| 187 | test("giving up with none releases the decoder it made", () => { | |
| 188 | const t = setup(); | |
| 189 | t.ws.onmessage!({ data: frame(true, 0, 10_000, pics[0]!) }); | |
| 190 | t.ws.onclose!(); | |
| 191 | expect(t.states.at(-1)).toBe("none"); | |
| 192 | expect(t.decoders).toHaveLength(1); | |
| 193 | expect(t.decoders[0]!.closes).toBe(1); | |
| 194 | }); | |
| 165 | 195 | }); |
new above
offline