@yaks/rtc 0.0.0-stage → 0.2.36

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/loop.ts ADDED
@@ -0,0 +1,73 @@
1
+ // A page's own sound, played so the browser's echo canceller hears it.
2
+ //
3
+ // Chrome cancels what a media element plays from the microphone, and not what
4
+ // Web Audio plays: a page that places voices in space with Web Audio sends
5
+ // each listener's loudspeakers back into their microphone, and every speaker
6
+ // hears themselves a moment later. Played through an <audio> element it is
7
+ // cancelled, but an element fed straight from a MediaStreamDestination is not
8
+ // either; one fed from a remote WebRTC track is. So the loop is two
9
+ // RTCPeerConnections inside the page: Web Audio's mix goes into one, comes out
10
+ // of the other as a remote track, and plays through an <audio>.
11
+ //
12
+ // The mix stays stereo, since a voice placed in space is nothing without its
13
+ // left and right: Opus is asked for two channels at a bitrate for music, not
14
+ // for a voice. The loop adds some 40 ms (a 16 ms jitter buffer and a 20 ms
15
+ // Opus frame, measured in Chrome 150), so a page routes through it only while
16
+ // a microphone is open.
17
+
18
+ /** A loop: connect sound `into` it and it plays through an <audio> element
19
+ * once `ready`; `stop` ends it. */
20
+ export type Loop = { into: AudioNode; ready: Promise<void>; stop: () => void }
21
+
22
+ // Opus in both directions as two channels, at up to 256 kbit/s.
23
+ let stereo = (d: RTCSessionDescriptionInit): RTCSessionDescriptionInit => {
24
+ let sdp = d.sdp ?? ''
25
+ let pt = /a=rtpmap:(\d+) opus\/48000\/2/i.exec(sdp)?.[1]
26
+ if (!pt) return d
27
+ return {
28
+ type: d.type,
29
+ sdp: sdp.replace(
30
+ new RegExp(`a=fmtp:${pt} (.*)`),
31
+ (_, params) =>
32
+ `a=fmtp:${pt} ${params};stereo=1;sprop-stereo=1;maxaveragebitrate=256000`,
33
+ ),
34
+ }
35
+ }
36
+
37
+ /** A loop for Web Audio on `ctx` that the echo canceller hears. It is
38
+ * `ready` once the two ends have met and the element plays, a moment after it
39
+ * is made; until then, and if it never is, sound connected into it is not
40
+ * heard, so a page moves its mix over once it is. */
41
+ export let loopback = (ctx: AudioContext): Loop => {
42
+ let into = ctx.createMediaStreamDestination()
43
+ let send = new RTCPeerConnection()
44
+ let hear = new RTCPeerConnection()
45
+ let out = new Audio()
46
+ send.onicecandidate = (e) => e.candidate && hear.addIceCandidate(e.candidate)
47
+ hear.onicecandidate = (e) => e.candidate && send.addIceCandidate(e.candidate)
48
+ let track = new Promise<MediaStreamTrack>((ok) =>
49
+ hear.ontrack = (e) => ok(e.track)
50
+ )
51
+ send.addTrack(into.stream.getAudioTracks()[0], into.stream)
52
+ let ready = (async () => {
53
+ let offer = await send.createOffer()
54
+ await send.setLocalDescription(offer)
55
+ await hear.setRemoteDescription(stereo(offer))
56
+ let answer = stereo(await hear.createAnswer())
57
+ await hear.setLocalDescription(answer)
58
+ await send.setRemoteDescription(answer)
59
+ out.srcObject = new MediaStream([await track])
60
+ await out.play()
61
+ })()
62
+ return {
63
+ into,
64
+ ready,
65
+ stop: () => {
66
+ out.pause()
67
+ out.srcObject = null
68
+ send.close()
69
+ hear.close()
70
+ into.disconnect()
71
+ },
72
+ }
73
+ }
package/mod.js ADDED
@@ -0,0 +1,30 @@
1
+ /**
2
+ * @yaks/rtc — voice between pages over Cloudflare Realtime.
3
+ *
4
+ * A page {@link join}s a call through its app's Realtime door, publishes a
5
+ * microphone, and hears the tracks it asks for. What it says is named on the
6
+ * entity it speaks as, in the relayed `rtc` component, so a listener finds a
7
+ * voice where it finds everything else about that entity:
8
+ *
9
+ * ```ts ignore
10
+ * import { join, microphone } from '@yaks/rtc'
11
+ *
12
+ * let call = await join({ entity: hero, write: (b) => client.mutate(b) })
13
+ * await call.publish(await microphone())
14
+ * // a peer wearing rtc{session, tracks: ['voice']}:
15
+ * let voice = await call.hear(peer.rtc.session, 'voice')
16
+ * audio.createMediaStreamSource(voice!.stream).connect(panner)
17
+ * ```
18
+ *
19
+ * A page that places voices in space with Web Audio plays its mix through
20
+ * {@link loopback} while a microphone is open, so the browser's echo canceller
21
+ * hears it, which in Chrome it does not otherwise.
22
+ *
23
+ * The host half, which keeps the account's secret, the sessions and the
24
+ * meter, is `@yaks/rtc/door`; the components are `@yaks/rtc/vocab`.
25
+ *
26
+ * @module
27
+ */ export { BITRATE, join, microphone, Refused, RENEW, VOICE, voiced } from "./join.js";
28
+ export { loopback } from "./loop.js";
29
+ export { docs, rtcDoc } from "./vocab.js";
30
+ //# sourceMappingURL=mod.js.map
package/mod.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.js","sources":["./mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;CA0BC,GAED,SACE,OAAO,EAGP,IAAI,EACJ,UAAU,EAIV,OAAO,EACP,KAAK,EAEL,KAAK,EACL,MAAM,oBACU;AAClB,SAAoB,QAAQ,oBAAmB;AAC/C,SAAS,IAAI,EAAE,MAAM,qBAAoB"}
package/mod.ts ADDED
@@ -0,0 +1,45 @@
1
+ /**
2
+ * @yaks/rtc — voice between pages over Cloudflare Realtime.
3
+ *
4
+ * A page {@link join}s a call through its app's Realtime door, publishes a
5
+ * microphone, and hears the tracks it asks for. What it says is named on the
6
+ * entity it speaks as, in the relayed `rtc` component, so a listener finds a
7
+ * voice where it finds everything else about that entity:
8
+ *
9
+ * ```ts ignore
10
+ * import { join, microphone } from '@yaks/rtc'
11
+ *
12
+ * let call = await join({ entity: hero, write: (b) => client.mutate(b) })
13
+ * await call.publish(await microphone())
14
+ * // a peer wearing rtc{session, tracks: ['voice']}:
15
+ * let voice = await call.hear(peer.rtc.session, 'voice')
16
+ * audio.createMediaStreamSource(voice!.stream).connect(panner)
17
+ * ```
18
+ *
19
+ * A page that places voices in space with Web Audio plays its mix through
20
+ * {@link loopback} while a microphone is open, so the browser's echo canceller
21
+ * hears it, which in Chrome it does not otherwise.
22
+ *
23
+ * The host half, which keeps the account's secret, the sessions and the
24
+ * meter, is `@yaks/rtc/door`; the components are `@yaks/rtc/vocab`.
25
+ *
26
+ * @module
27
+ */
28
+
29
+ export {
30
+ BITRATE,
31
+ type Call,
32
+ type Heard,
33
+ join,
34
+ microphone,
35
+ type MicSignal,
36
+ type Opts,
37
+ type Published,
38
+ Refused,
39
+ RENEW,
40
+ type State,
41
+ VOICE,
42
+ voiced,
43
+ } from './join.js'
44
+ export { type Loop, loopback } from './loop.js'
45
+ export { docs, rtcDoc } from './vocab.js'
package/package.json CHANGED
@@ -1,6 +1,32 @@
1
1
  {
2
2
  "name": "@yaks/rtc",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.36",
4
+ "homepage": "https://jsr.io/@yaks/rtc",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./_dist/mod.d.ts",
9
+ "default": "./mod.js"
10
+ },
11
+ "./door": {
12
+ "types": "./_dist/door.d.ts",
13
+ "default": "./door.js"
14
+ },
15
+ "./effects": {
16
+ "types": "./_dist/effects.d.ts",
17
+ "default": "./effects.js"
18
+ },
19
+ "./vocab": {
20
+ "types": "./_dist/vocab.d.ts",
21
+ "default": "./vocab.js"
22
+ }
23
+ },
24
+ "license": "Apache-2.0",
25
+ "dependencies": {
26
+ "@yaks/effects": "0.2.36",
27
+ "@yaks/graph": "0.2.36",
28
+ "@yaks/vocab": "0.2.36"
29
+ },
30
+ "description": "Voice between pages over Cloudflare Realtime: a page joins a session, publishes its microphone and hears the tracks it asks for, and a host answers the door that keeps the secret, the sessions and the meter.",
31
+ "repository": "https://github.com/yak-sh/yak.git"
32
+ }
package/vocab.js ADDED
@@ -0,0 +1,16 @@
1
+ // The component declarations, and nothing else: what a host or a page loads at
2
+ // `@yaks/rtc/vocab`. `rtc` is what a speaking entity wears, relayed to its
3
+ // listeners; `sfu` is the row a door keeps for each session it opened.
4
+ import doc from './vocab.json' with {
5
+ type: 'json'
6
+ };
7
+ import manifest from './package.json' with {
8
+ type: 'json'
9
+ };
10
+ /** The `rtc` and `sfu` components. */ export let rtcDoc = doc;
11
+ /** What this package is: its manifest's description, the one place it is
12
+ * written, which a host stamps on each document here. */ export let description = manifest.description;
13
+ /** Every document this package declares. */ export let docs = [
14
+ rtcDoc
15
+ ];
16
+ //# sourceMappingURL=vocab.js.map
package/vocab.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vocab.js","sources":["./vocab.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,2EAA2E;AAC3E,uEAAuE;AAGvE,OAAO,SAAS,oBAAoB;EAAE,MAAM;AAAO,EAAC;AACpD,OAAO,cAAc,sBAAsB;EAAE,MAAM;AAAO,EAAC;AAE3D,oCAAoC,GACpC,OAAO,IAAI,SAAmB,IAAe;AAE7C;uDACuD,GACvD,OAAO,IAAI,cAAsB,SAAS,WAAW,CAAA;AAErD,0CAA0C,GAC1C,OAAO,IAAI,OAAmB;EAAC;CAAO,CAAA"}
package/vocab.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "$id": "urn:uuid:c44d6568-b6c0-4af6-bead-8d67bb438d85",
3
+ "$vocabulary": {
4
+ "https://yak.sh/vocab/core": true
5
+ },
6
+ "title": "rtc",
7
+ "$defs": {
8
+ "rtc_lapse": {
9
+ "effect": true,
10
+ "created": ["fired"],
11
+ "active": ".sfu",
12
+ "description": "Close the tracks of a Realtime session whose lease fired, then drop its row."
13
+ },
14
+ "rtc": {
15
+ "$id": "urn:uuid:48ff9518-622e-4147-9588-af21f41038c9",
16
+ "component": true,
17
+ "type": "object",
18
+ "sync": "peers",
19
+ "durable": "connection",
20
+ "pace": "200ms",
21
+ "description": "What this entity is saying out loud this moment: the Realtime session it publishes from, the tracks a listener asks for, and whether it is muted or talking. Relayed and kept by nobody, so it goes with the tab that wrote it, and a listener stops hearing it then.",
22
+ "properties": {
23
+ "session": {
24
+ "$id": "urn:uuid:1c8a0d52-bed4-47f6-a124-8327d8cb02c8",
25
+ "type": "string",
26
+ "description": "the Realtime session the tracks are published from"
27
+ },
28
+ "tracks": {
29
+ "$id": "urn:uuid:e5159060-3c2e-43b2-925f-5a1f444f8852",
30
+ "type": "array",
31
+ "items": { "type": "string" },
32
+ "description": "the names of the tracks it publishes, each asked for as a remote track of the session"
33
+ },
34
+ "muted": {
35
+ "$id": "urn:uuid:e278296b-1af1-4243-9c05-83b3a1412f73",
36
+ "type": "boolean",
37
+ "description": "its microphone is off"
38
+ },
39
+ "talking": {
40
+ "$id": "urn:uuid:3d95492b-8c4e-47f9-981b-0685ff135f14",
41
+ "type": "boolean",
42
+ "description": "its microphone hears a voice this moment"
43
+ }
44
+ }
45
+ },
46
+ "sfu": {
47
+ "$id": "urn:uuid:5c35d67a-deec-4d04-89c8-534cbd8eca4d",
48
+ "component": true,
49
+ "journal": false,
50
+ "type": "object",
51
+ "wire": false,
52
+ "description": "A Realtime session this app's door opened, while its lease holds: the wake beside it is when the lease ends, and the session's tracks close when it does.",
53
+ "properties": {
54
+ "session": {
55
+ "$id": "urn:uuid:944b1083-e409-40e2-af75-7a797a583333",
56
+ "type": "string",
57
+ "unique": true,
58
+ "description": "the session's id at Cloudflare Realtime"
59
+ },
60
+ "key": {
61
+ "$id": "urn:uuid:0e384019-d0dc-4a3d-93ce-4e78ca760486",
62
+ "type": "string",
63
+ "description": "the SHA-256 of the key the door handed whoever opened it, which every change to the session carries"
64
+ }
65
+ }
66
+ }
67
+ }
68
+ }
package/vocab.ts ADDED
@@ -0,0 +1,17 @@
1
+ // The component declarations, and nothing else: what a host or a page loads at
2
+ // `@yaks/rtc/vocab`. `rtc` is what a speaking entity wears, relayed to its
3
+ // listeners; `sfu` is the row a door keeps for each session it opened.
4
+
5
+ import type { VocabDoc } from 'jsr:@yaks/vocab@0.2.36'
6
+ import doc from './vocab.json' with { type: 'json' }
7
+ import manifest from './package.json' with { type: 'json' }
8
+
9
+ /** The `rtc` and `sfu` components. */
10
+ export let rtcDoc: VocabDoc = doc as VocabDoc
11
+
12
+ /** What this package is: its manifest's description, the one place it is
13
+ * written, which a host stamps on each document here. */
14
+ export let description: string = manifest.description
15
+
16
+ /** Every document this package declares. */
17
+ export let docs: VocabDoc[] = [rtcDoc]