@lavitre/apps 1.63.0 → 1.65.0

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.
@@ -0,0 +1,121 @@
1
+ /// Sending end of a frame → host shared stream carried over a local
2
+ /// `RTCPeerConnection` (`Frame.shareStream`'s fallback). Track readables only
3
+ /// reach the Vitre page when the frame is on the same site: from another site
4
+ /// the first `VideoFrame` chunk fails with `DataCloneError` and the share dies.
5
+ /// Browsers without `MediaStreamTrackProcessor` cannot transfer them at all.
6
+ /// Re-encoded once, on the same machine.
7
+ ///
8
+ /// Signaling runs over the `MessagePort` whose twin travels in the
9
+ /// `shareStream` message (`ShareStreamPeerTransport`). Mirror of
10
+ /// `SharedStreamPeer.ts`, roles swapped:
11
+ /// frame → host { type: 'offer', sdp } | { type: 'candidate', candidate } | { type: 'close' }
12
+ /// host → frame { type: 'answer', sdp } | { type: 'candidate', candidate } | { type: 'close' }
13
+ /** Same ceiling as the host's reverse direction: the encoder and the decoder
14
+ * run on the same machine, the link is local. */
15
+ const MAX_BITRATE = 8000000;
16
+ const tuneSenders = async (pc) => {
17
+ for (const sender of pc.getSenders()) {
18
+ if (sender.track?.kind !== 'video')
19
+ continue;
20
+ const params = sender.getParameters();
21
+ const encoding = params.encodings?.[0];
22
+ if (!encoding)
23
+ continue;
24
+ encoding.maxBitrate = MAX_BITRATE;
25
+ encoding.maxFramerate = 30;
26
+ params.degradationPreference = 'maintain-resolution';
27
+ await sender.setParameters(params).catch(() => {
28
+ // A browser refusing a field keeps its defaults.
29
+ });
30
+ }
31
+ };
32
+ export function offerStreamToHost(port, stream, onClosed) {
33
+ const pc = new RTCPeerConnection();
34
+ let closed = false;
35
+ const close = (notifyHost) => {
36
+ if (closed)
37
+ return;
38
+ closed = true;
39
+ if (notifyHost) {
40
+ try {
41
+ port.postMessage({ type: 'close' });
42
+ }
43
+ catch {
44
+ // Port already gone.
45
+ }
46
+ }
47
+ port.close();
48
+ pc.close();
49
+ onClosed();
50
+ };
51
+ for (const track of stream.getTracks()) {
52
+ pc.addTransceiver(track, { direction: 'sendonly', streams: [stream] });
53
+ }
54
+ pc.onicecandidate = (e) => {
55
+ if (e.candidate && !closed) {
56
+ port.postMessage({
57
+ type: 'candidate',
58
+ candidate: e.candidate.toJSON(),
59
+ });
60
+ }
61
+ };
62
+ pc.onconnectionstatechange = () => {
63
+ if (pc.connectionState === 'failed')
64
+ close(true);
65
+ };
66
+ // Candidates must not overtake the answer they belong to: handle in order.
67
+ let chain = Promise.resolve();
68
+ port.onmessage = (e) => {
69
+ const signal = e.data;
70
+ chain = chain
71
+ .then(async () => {
72
+ if (closed || signal == null)
73
+ return;
74
+ switch (signal.type) {
75
+ case 'answer':
76
+ await pc.setRemoteDescription({ type: 'answer', sdp: signal.sdp });
77
+ break;
78
+ case 'candidate':
79
+ await pc.addIceCandidate(signal.candidate);
80
+ break;
81
+ case 'close':
82
+ close(false);
83
+ break;
84
+ }
85
+ })
86
+ .catch(() => close(true));
87
+ };
88
+ void (async () => {
89
+ try {
90
+ await pc.setLocalDescription(await pc.createOffer());
91
+ await tuneSenders(pc);
92
+ if (!closed) {
93
+ port.postMessage({
94
+ type: 'offer',
95
+ sdp: pc.localDescription?.sdp ?? '',
96
+ });
97
+ }
98
+ }
99
+ catch {
100
+ close(true);
101
+ }
102
+ })();
103
+ return {
104
+ replaceTracks: async (next) => {
105
+ if (closed)
106
+ throw new Error('replaceShareStream: the share is closed');
107
+ for (const track of next.getTracks()) {
108
+ const sender = pc
109
+ .getTransceivers()
110
+ .find((t) => t.receiver.track.kind === track.kind)?.sender;
111
+ if (!sender) {
112
+ throw new Error(`replaceShareStream: this share carries no ${track.kind} track`);
113
+ }
114
+ await sender.replaceTrack(track);
115
+ }
116
+ await tuneSenders(pc);
117
+ },
118
+ close: () => close(true),
119
+ };
120
+ }
121
+ //# sourceMappingURL=OutgoingStreamPeer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"OutgoingStreamPeer.js","sour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A;gBAC5D,IAAI,CAAC,MAAM,EAAE,CAAC;oBACZ,MAAM,IAAI,KAAK,CACb,6CAA6C,KAAK,CAAC,IAAI,QAAQ,CAChE,CAAA;gBACH,CAAC;gBACD,MAAM,MAAM,CAAC,YAAY,CAAC,KAAK,CAAC,CAAA;YAClC,CAAC;YACD,MAAM,WAAW,CAAC,EAAE,CAAC,CAAA;QACvB,CAAC;QACD,KAAK,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC;KACzB,CAAA;AACH,CAAC","sourcesContent":["/// Sending end of a frame → host shared stream carried over a local\n/// `RTCPeerConnection` (`Frame.shareStream`'s fallback). Track readables only\n/// reach the Vitre page when the frame is on the same site: from another site\n/// the first `VideoFrame` chunk fails with `DataCloneError` and the share dies.\n/// Browsers without `MediaStreamTrackProcessor` cannot transfer them at all.\n/// Re-encoded once, on the same machine.\n///\n/// Signaling runs over the `MessagePort` whose twin travels in the\n/// `shareStream` message (`ShareStreamPeerTransport`). Mirror of\n/// `SharedStreamPeer.ts`, roles swapped:\n/// frame → host { type: 'offer', sdp } | { type: 'candidate', candidate } | { type: 'close' }\n/// host → frame { type: 'answer', sdp } | { type: 'candidate', candidate } | { type: 'close' }\n\nimport type { SharedStreamSignal } from './SharedStreamPeer.js'\n\nexport type OutgoingStreamPeer = {\n /** Swap the tracks sent without renegotiating (`replaceTrack`). Only kinds the\n * share already carries: a new kind would need a new negotiation. */\n replaceTracks: (stream: MediaStream) => Promise<void>\n /** Close from this side, telling the host. */\n close: () => void\n}\n\n/** Same ceiling as the host's reverse direction: the encoder and the decoder\n * run on the same machine, the link is local. */\nconst MAX_BITRATE = 8_000_000\n\nconst tuneSenders = async (pc: RTCPeerConnection) => {\n for (const sender of pc.getSenders()) {\n if (sender.track?.kind !== 'video') continue\n const params = sender.getParameters()\n const encoding = params.encodings?.[0]\n if (!encoding) continue\n encoding.maxBitrate = MAX_BITRATE\n encoding.maxFramerate = 30\n ;(\n params as RTCRtpSendParameters & { degradationPreference?: string }\n ).degradationPreference = 'maintain-resolution'\n await sender.setParameters(params).catch(() => {\n // A browser refusing a field keeps its defaults.\n })\n }\n}\n\nexport function offerStreamToHost(\n port: MessagePort,\n stream: MediaStream,\n onClosed: () => void,\n): OutgoingStreamPeer {\n const pc = new RTCPeerConnection()\n let closed = false\n const close = (notifyHost: boolean) => {\n if (closed) return\n closed = true\n if (notifyHost) {\n try {\n port.postMessage({ type: 'close' } satisfies SharedStreamSignal)\n } catch {\n // Port already gone.\n }\n }\n port.close()\n pc.close()\n onClosed()\n }\n\n for (const track of stream.getTracks()) {\n pc.addTransceiver(track, { direction: 'sendonly', streams: [stream] })\n }\n pc.onicecandidate = (e) => {\n if (e.candidate && !closed) {\n port.postMessage({\n type: 'candidate',\n candidate: e.candidate.toJSON(),\n } satisfies SharedStreamSignal)\n }\n }\n pc.onconnectionstatechange = () => {\n if (pc.connectionState === 'failed') close(true)\n }\n\n // Candidates must not overtake the answer they belong to: handle in order.\n let chain = Promise.resolve()\n port.onmessage = (e: MessageEvent<SharedStreamSignal | null>) => {\n const signal = e.data\n chain = chain\n .then(async () => {\n if (closed || signal == null) return\n switch (signal.type) {\n case 'answer':\n await pc.setRemoteDescription({ type: 'answer', sdp: signal.sdp })\n break\n case 'candidate':\n await pc.addIceCandidate(signal.candidate)\n break\n case 'close':\n close(false)\n break\n }\n })\n .catch(() => close(true))\n }\n\n void (async () => {\n try {\n await pc.setLocalDescription(await pc.createOffer())\n await tuneSenders(pc)\n if (!closed) {\n port.postMessage({\n type: 'offer',\n sdp: pc.localDescription?.sdp ?? '',\n } satisfies SharedStreamSignal)\n }\n } catch {\n close(true)\n }\n })()\n\n return {\n replaceTracks: async (next: MediaStream) => {\n if (closed) throw new Error('replaceShareStream: the share is closed')\n for (const track of next.getTracks()) {\n const sender = pc\n .getTransceivers()\n .find((t) => t.receiver.track.kind === track.kind)?.sender\n if (!sender) {\n throw new Error(\n `replaceShareStream: this share carries no ${track.kind} track`,\n )\n }\n await sender.replaceTrack(track)\n }\n await tuneSenders(pc)\n },\n close: () => close(true),\n }\n}\n"]}
package/dist/Version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const apiVersion = "1.63.0";
1
+ export declare const apiVersion = "1.65.0";
package/dist/Version.js CHANGED
@@ -1,2 +1,2 @@
1
- export const apiVersion = '1.63.0';
1
+ export const apiVersion = '1.65.0';
2
2
  //# sourceMappingURL=Version.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"Version.js","sourceRoot":"","sources":["../Version.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,UAAU,GAAG,QAAQ,CAAA","sourcesContent":["export const apiVersion = '1.63.0'"]}
1
+ {"version":3,"file":"Version.js","sourceRoot":"","sources":["../Version.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,UAAU,GAAG,QAAQ,CAAA","sourcesContent":["export const apiVersion = '1.65.0'"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lavitre/apps",
3
- "version": "1.63.0",
3
+ "version": "1.65.0",
4
4
  "description": "TypeScript SDK for building la Vitre apps (connector, frames, companion, CRDT sync)",
5
5
  "license": "MIT",
6
6
  "type": "module",