@cero-base/cero 3.6.0 → 3.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cero-base/cero",
3
- "version": "3.6.0",
3
+ "version": "3.6.1",
4
4
  "description": "The ideal p2p API — everything is a handle, handles contain refs, refs contain rows.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -122,7 +122,7 @@
122
122
  "test:node": "find test -name '*.test.js' | sort | xargs -P4 -n1 brittle-node"
123
123
  },
124
124
  "dependencies": {
125
- "@cero-base/core": "^3.6.0",
125
+ "@cero-base/core": "^3.6.1",
126
126
  "b4a": "^1.9.0",
127
127
  "bare-abort-controller": "^1.1.2",
128
128
  "bare-crypto": "^1.16.1",
@@ -9,9 +9,9 @@ import { Call } from '@cero-base/core/call'
9
9
 
10
10
  import { t } from '../lib/spec.js'
11
11
  import { Ref } from '../lib/refs.js'
12
- import { LANES, wire } from '../lib/line.js'
12
+ import { LANES, muted, wire } from '../lib/line.js'
13
13
  import { Media, bindings } from '../lib/media.js'
14
- import { after, before, call, close, del, get, open, put } from '../lib/operators.js'
14
+ import { after, before, call, close, del, get, open, put, supports } from '../lib/operators.js'
15
15
 
16
16
  /**
17
17
  * How often a device on a call beats, how long one stays silent before it is a ghost, how long a
@@ -50,8 +50,8 @@ const refs = {
50
50
  key: t.string,
51
51
  screen: t.bool
52
52
  }),
53
- callStart: t.action({ to: t.json, over: t.string, key: t.string }),
54
- callJoin: t.action({ call: t.string, to: t.json, key: t.string }),
53
+ callStart: t.action({ to: t.json, over: t.string, key: t.string, off: t.json }),
54
+ callJoin: t.action({ call: t.string, to: t.json, key: t.string, off: t.json }),
55
55
  callLeave: t.action({}),
56
56
  callEnd: t.action({ call: t.string }),
57
57
  callDeny: t.action({ call: t.string, reason: t.string }),
@@ -137,7 +137,7 @@ async function start(ctx) {
137
137
  const rung = ringing({}, named, memberId, ts)
138
138
  const fresh = { by: memberId, rung, denied: {}, ended: 0, endedBy: null, answered: false }
139
139
  const { data: started } = await put('calls', fresh)
140
- await put('participants', seat(device, started.id, memberId, ts, row.key))
140
+ await put('participants', seat(device, started.id, memberId, ts, row.key, down(row.off)))
141
141
  }
142
142
 
143
143
  async function join(ctx) {
@@ -169,9 +169,20 @@ async function enter({ row, memberId, device, ts, get, put, set }, id, to) {
169
169
  if (to?.length || answered !== going.answered) {
170
170
  await set('calls', { id, rung, denied, answered })
171
171
  }
172
- // back on the call it is on, a device keeps its flags
173
- if (mine?.call === id) await set('participants', { id: device, at: ts })
174
- else await put('participants', seat(device, id, memberId, ts, row.key))
172
+ const off = down(row.off)
173
+ // back on the call it is on, a device keeps its flags: a lane its stream has off goes off, none on
174
+ const dark = Object.fromEntries(off.map((lane) => [lane, false]))
175
+ if (mine?.call === id) await set('participants', { id: device, at: ts, ...dark })
176
+ else await put('participants', seat(device, id, memberId, ts, row.key, off))
177
+ }
178
+
179
+ // the lanes a device starts with off
180
+ function down(off) {
181
+ if (off == null) return []
182
+ if (!Array.isArray(off) || !off.every((lane) => LANES.includes(lane))) {
183
+ throw CeroError.INVALID(`off takes ${LANES.join(' or ')}`)
184
+ }
185
+ return off
175
186
  }
176
187
 
177
188
  async function leave({ memberId, device, ts, get, set, del }) {
@@ -258,8 +269,8 @@ function ringing(rung, members, by, at) {
258
269
  return out
259
270
  }
260
271
 
261
- function seat(device, call, member, ts, key) {
262
- const lanes = { audio: true, video: true, screen: false }
272
+ function seat(device, call, member, ts, key, off) {
273
+ const lanes = { audio: !off.includes('audio'), video: !off.includes('video'), screen: false }
263
274
  return { id: device, call, member, ...lanes, at: ts, key: key || null }
264
275
  }
265
276
 
@@ -351,7 +362,7 @@ export class Calls {
351
362
  * @param {{ to?: string[] | null, stream?: MediaStream | null }} [opts]
352
363
  * @returns {Promise<Line>}
353
364
  */
354
- async dial(room, { to = null, stream = null } = {}) {
365
+ async dial(room, { to = null, stream = null, off = muted(stream) } = {}) {
355
366
  if (!room.parent || !this.types.has(room.type)) {
356
367
  throw CeroError.INVALID('dial takes a handle of a type with calls')
357
368
  }
@@ -369,8 +380,8 @@ export class Calls {
369
380
  if (to) await line._add(to)
370
381
  return line
371
382
  }
372
- if (this.lines.get(room.id) !== was) return this.dial(room, { to, stream })
373
- const opening = this._open(room, to, stream, line)
383
+ if (this.lines.get(room.id) !== was) return this.dial(room, { to, stream, off })
384
+ const opening = this._open(room, to, stream, line, off)
374
385
  this.lines.set(room.id, opening)
375
386
  opening.catch(() => this.lines.get(room.id) === opening && this.lines.delete(room.id))
376
387
  return opening
@@ -418,7 +429,7 @@ export class Calls {
418
429
 
419
430
  // decided on this device's clock: a call someone is heard on is joined, any other is started anew
420
431
  /** @private */
421
- async _open(room, to, stream, last) {
432
+ async _open(room, to, stream, last, off) {
422
433
  await last?._closing?.catch(() => {})
423
434
  // a key pair holds one transport at a time
424
435
  await this._hangups.get(room.id)
@@ -438,8 +449,13 @@ export class Calls {
438
449
  await call(room.callLeave)
439
450
  ;({ rows, live, mine } = await read())
440
451
  }
441
- if (!live.length) await call(room.callStart, { to, over: rows[0]?.call ?? null, key })
442
- else await call(room.callJoin, { call: live[0].call, to, key })
452
+ // what starts off rides with the start or the join where the room has that field, and follows
453
+ // it where it does not yet
454
+ const rides = off.length > 0 && (await supports(room.callStart, 'off'))
455
+ const dark = rides ? { off } : {}
456
+ if (!live.length) await call(room.callStart, { to, over: rows[0]?.call ?? null, key, ...dark })
457
+ else await call(room.callJoin, { call: live[0].call, to, key, ...dark })
458
+ if (!rides) for (const lane of off) await call(room.callLane, { lane, on: false })
443
459
  // back on its seat with a new line, which shares no screen: one its row still shows is taken down
444
460
  if (mine?.screen) await call(room.callLane, { lane: 'screen', on: false })
445
461
  return new Line(this, room, device, stream, keyPair)
@@ -839,6 +855,8 @@ export class Line {
839
855
  const run = async () => {
840
856
  if (this.closed) return
841
857
  const { mine, others } = await this._rows()
858
+ // closed while its rows were read: no timer is armed past its close
859
+ if (this.closed) return
842
860
  if (!mine) return this._drop()
843
861
  const now = this._calls.now()
844
862
  // a key two seats claim, or this device's own, is held for neither: whose it is cannot be told
package/src/lib/line.js CHANGED
@@ -1,6 +1,19 @@
1
1
  /** The lanes a mute names. */
2
2
  export const LANES = ['audio', 'video']
3
3
 
4
+ /**
5
+ * The lanes a stream starts off: a kind it has no track of turned on. With no stream, none: what
6
+ * a device with no WebRTC says of its lanes is its own.
7
+ *
8
+ * @param {MediaStream | null} stream
9
+ * @returns {string[]}
10
+ */
11
+ export function muted(stream) {
12
+ if (!stream) return []
13
+ const tracks = { audio: stream.getAudioTracks(), video: stream.getVideoTracks() }
14
+ return LANES.filter((lane) => !tracks[lane].some((track) => track.enabled))
15
+ }
16
+
4
17
  const REFS = {
5
18
  peers: { kind: 'collection', type: 'line-peer' },
6
19
  _lanes: { kind: 'single', type: 'line-lanes' },
package/src/lib/media.js CHANGED
@@ -8,6 +8,12 @@ const CONFIG = { iceServers: [], rtcpMuxPolicy: 'require', bundlePolicy: 'max-bu
8
8
  const LANES = ['audio', 'video', 'screen']
9
9
  const KINDS = ['audio', 'video', 'video']
10
10
 
11
+ /**
12
+ * How long a connection may stay unconnected before the side that offers starts it over: from its
13
+ * offer, and from a drop. A lost offer or answer heals at the first.
14
+ */
15
+ export const durations = { deadline: 5000, grace: 2000 }
16
+
11
17
  /**
12
18
  * @typedef {object} Bindings The WebRTC a line's media runs on: `react-native-webrtc`, or a window's own.
13
19
  * @property {typeof RTCPeerConnection} RTCPeerConnection
@@ -29,6 +35,7 @@ const KINDS = ['audio', 'video', 'video']
29
35
  * @property {{ stream: MediaStream, screen: MediaStream } | null} streams
30
36
  * @property {boolean} closed
31
37
  * @property {Promise<void> | null} turn
38
+ * @property {ReturnType<typeof setTimeout> | null} timer When the side that offers starts it over, unless connected by then.
32
39
  */
33
40
 
34
41
  /**
@@ -165,7 +172,8 @@ export class Media {
165
172
  heard: false,
166
173
  streams: null,
167
174
  closed: false,
168
- turn: null
175
+ turn: null,
176
+ timer: null
169
177
  }
170
178
  pc.addEventListener('connectionstatechange', () => this._state(conn))
171
179
  this._conns.set(id, conn)
@@ -173,6 +181,7 @@ export class Media {
173
181
  conn.lanes = KINDS.map((kind) => pc.addTransceiver(kind, { direction: 'sendrecv' }))
174
182
  this._attach(conn, [0, 1, 2])
175
183
  this._turn(conn, () => this._describe(conn, () => pc.createOffer()))
184
+ this._due(conn, durations.deadline)
176
185
  }
177
186
  this._onchange()
178
187
  return conn
@@ -227,13 +236,27 @@ export class Media {
227
236
  return this.muted[lane] ? null : (tracks(this.camera, lane)[0] ?? null)
228
237
  }
229
238
 
239
+ // not connected by then, the side that offers starts over, and the other rebuilds on its offer
240
+ /** @private */
241
+ _due(conn, ms) {
242
+ clearTimeout(conn.timer)
243
+ conn.timer = setTimeout(() => {
244
+ if (!conn.closed && conn.pc.connectionState !== 'connected') this._open(conn.id, conn.port)
245
+ }, ms)
246
+ }
247
+
230
248
  /** @private */
231
249
  _state(conn) {
250
+ const state = conn.pc.connectionState
232
251
  // lost on a port that did not move: the side that offers starts over, the other rebuilds on its offer
233
- if (conn.pc.connectionState === 'failed' && conn.offerer && !conn.closed) {
252
+ if (state === 'failed' && conn.offerer && !conn.closed) {
234
253
  this._open(conn.id, conn.port)
235
254
  return
236
255
  }
256
+ if (state === 'connected') clearTimeout(conn.timer)
257
+ if (state === 'disconnected' && conn.offerer && !conn.closed) {
258
+ this._due(conn, durations.grace)
259
+ }
237
260
  if (conn.pc.connectionState === 'connected' && !conn.streams) {
238
261
  const [audio, video, screen] = conn.lanes.map((transceiver) => transceiver.receiver.track)
239
262
  const { MediaStream } = this.webrtc
@@ -251,6 +274,7 @@ export class Media {
251
274
  /** @private */
252
275
  _drop(conn) {
253
276
  conn.closed = true
277
+ clearTimeout(conn.timer)
254
278
  if (this._conns.get(conn.id) === conn) this._conns.delete(conn.id)
255
279
  conn.pc.close()
256
280
  this._onchange()
package/src/rpc/client.js CHANGED
@@ -9,7 +9,7 @@ import z32 from 'z32'
9
9
  import { decodeId, servedType } from '@cero-base/core/blobs/codec'
10
10
 
11
11
  import { Ref } from '../lib/refs.js'
12
- import { wire } from '../lib/line.js'
12
+ import { muted, wire } from '../lib/line.js'
13
13
  import { Media, bindings } from '../lib/media.js'
14
14
  import { follow } from './push.js'
15
15
  import * as verbs from '../lib/operators.js'
@@ -438,7 +438,8 @@ const operators = {
438
438
  // the worker joins the call; the stream stays with the UI, which holds the media
439
439
  /** @private */
440
440
  async _dial({ stream = null, ...opts }) {
441
- const { id } = await this._verb('dial', opts)
441
+ // the stream stays here: what it starts with off is all the worker hears of it
442
+ const { id } = await this._verb('dial', { ...opts, off: muted(stream) })
442
443
  const root = this.parent || this
443
444
  // the worker answers with the room's open line: this side keeps the one it has, and its media
444
445
  const open = root._lines.get(id)
@@ -60,7 +60,7 @@ export declare class Calls {
60
60
  * @param {{ to?: string[] | null, stream?: MediaStream | null }} [opts]
61
61
  * @returns {Promise<Line>}
62
62
  */
63
- dial(room: import('../handle/index.js').Context, { to, stream }?: {
63
+ dial(room: import('../handle/index.js').Context, { to, stream, off }?: {
64
64
  to?: string[] | null;
65
65
  stream?: MediaStream | null;
66
66
  }): Promise<Line>;
@@ -1,5 +1,13 @@
1
1
  /** The lanes a mute names. */
2
2
  export declare const LANES: string[];
3
+ /**
4
+ * The lanes a stream starts off: a kind it has no track of turned on. With no stream, none: what
5
+ * a device with no WebRTC says of its lanes is its own.
6
+ *
7
+ * @param {MediaStream | null} stream
8
+ * @returns {string[]}
9
+ */
10
+ export declare function muted(stream: MediaStream | null): string[];
3
11
  export type Spec = import('./spec.js').Spec;
4
12
  export type Wire = Pick<Spec, 'schema' | 'codec'> & {
5
13
  meta: {
@@ -1,3 +1,11 @@
1
+ /**
2
+ * How long a connection may stay unconnected before the side that offers starts it over: from its
3
+ * offer, and from a drop. A lost offer or answer heals at the first.
4
+ */
5
+ export declare const durations: {
6
+ deadline: number;
7
+ grace: number;
8
+ };
1
9
  export type Bindings = {
2
10
  RTCPeerConnection: typeof RTCPeerConnection;
3
11
  MediaStream: typeof MediaStream;
@@ -37,6 +45,10 @@ export type Conn = {
37
45
  } | null;
38
46
  closed: boolean;
39
47
  turn: Promise<void> | null;
48
+ /**
49
+ * When the side that offers starts it over, unless connected by then.
50
+ */
51
+ timer: ReturnType<typeof setTimeout> | null;
40
52
  };
41
53
  /**
42
54
  * @typedef {object} Bindings The WebRTC a line's media runs on: `react-native-webrtc`, or a window's own.
@@ -59,6 +71,7 @@ export type Conn = {
59
71
  * @property {{ stream: MediaStream, screen: MediaStream } | null} streams
60
72
  * @property {boolean} closed
61
73
  * @property {Promise<void> | null} turn
74
+ * @property {ReturnType<typeof setTimeout> | null} timer When the side that offers starts it over, unless connected by then.
62
75
  */
63
76
  /**
64
77
  * The bindings to run on: the ones given, else the window's.
@@ -148,6 +161,8 @@ export declare class Media {
148
161
  /** @private */
149
162
  private _track;
150
163
  /** @private */
164
+ private _due;
165
+ /** @private */
151
166
  private _state;
152
167
  /** @private */
153
168
  private _turn;