@atmo-dev/kit 0.20.0 → 0.22.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.
- package/README.md +1 -1
- package/dist/components/RichText.svelte +17 -9
- package/dist/components/RichTextEditor.svelte +67 -11
- package/dist/components/RichTextEditor.svelte.d.ts +0 -2
- package/dist/components/StreamplacePicker.svelte +2 -2
- package/dist/components/StreamplacePicker.svelte.d.ts +1 -1
- package/dist/components/composer/EmojiPanel.svelte +55 -0
- package/dist/components/composer/EmojiPanel.svelte.d.ts +6 -0
- package/dist/components/composer/EmojiPicker.svelte +2 -38
- package/dist/components/data/DataLabel.svelte +120 -0
- package/dist/components/data/DataLabel.svelte.d.ts +10 -0
- package/dist/components/data/issues.d.ts +5 -0
- package/dist/components/data/issues.js +52 -0
- package/dist/components/data/types.d.ts +28 -0
- package/dist/components/data/types.js +1 -0
- package/dist/components/data/visibility.d.ts +7 -0
- package/dist/components/data/visibility.js +15 -0
- package/dist/components/legal/links.js +1 -0
- package/dist/components/legal/types.d.ts +1 -1
- package/dist/components/notifications/NotificationList.svelte +137 -0
- package/dist/components/notifications/NotificationList.svelte.d.ts +19 -0
- package/dist/components/notifications/NotificationPushToggle.svelte +76 -0
- package/dist/components/notifications/NotificationPushToggle.svelte.d.ts +19 -0
- package/dist/components/notifications/NotificationSettings.svelte +125 -0
- package/dist/components/notifications/NotificationSettings.svelte.d.ts +16 -0
- package/dist/components/notifications/types.d.ts +37 -0
- package/dist/components/notifications/types.js +1 -0
- package/dist/editor/ImageDialog.svelte +87 -0
- package/dist/editor/ImageDialog.svelte.d.ts +16 -0
- package/dist/editor/image-node.d.ts +6 -2
- package/dist/editor/image-node.js +36 -37
- package/dist/events/EventCard.svelte +6 -1
- package/dist/events/VodPlayer.svelte +1 -1
- package/dist/events/atmo-rsvp.d.ts +6 -0
- package/dist/events/atmo-rsvp.js +34 -0
- package/dist/events/atproto-helpers.d.ts +1 -0
- package/dist/events/atproto-helpers.js +3 -0
- package/dist/events/event-link-embed.d.ts +7 -3
- package/dist/events/event-link-embed.js +11 -5
- package/dist/events/event-view/StreamPlacePlayer.svelte +14 -108
- package/dist/events/linked-records.d.ts +1 -1
- package/dist/events/linked-records.js +2 -2
- package/dist/events-card.d.ts +9 -0
- package/dist/events-card.js +7 -0
- package/dist/events-data.d.ts +1 -0
- package/dist/events-data.js +1 -0
- package/dist/gestures.d.ts +5 -1
- package/dist/gestures.js +6 -3
- package/dist/grain/xrpc.js +11 -4
- package/dist/index.d.ts +13 -1
- package/dist/index.js +7 -1
- package/dist/legal.d.ts +3 -0
- package/dist/legal.js +2 -0
- package/dist/richtext/serialize.js +13 -8
- package/dist/streamplace/StreamCard.svelte +184 -0
- package/dist/streamplace/StreamCard.svelte.d.ts +24 -0
- package/dist/streamplace/StreamLinkEmbed.svelte +59 -0
- package/dist/streamplace/StreamLinkEmbed.svelte.d.ts +5 -0
- package/dist/streamplace/StreamPlayer.svelte +539 -0
- package/dist/streamplace/StreamPlayer.svelte.d.ts +42 -0
- package/dist/streamplace/VideoCard.svelte +159 -0
- package/dist/streamplace/VideoCard.svelte.d.ts +17 -0
- package/dist/streamplace/api.d.ts +74 -0
- package/dist/streamplace/api.js +182 -0
- package/dist/streamplace/broadcast.d.ts +3 -0
- package/dist/streamplace/broadcast.js +110 -0
- package/dist/streamplace/channel.svelte.d.ts +74 -0
- package/dist/streamplace/channel.svelte.js +515 -0
- package/dist/streamplace/chat/Composer.svelte +232 -0
- package/dist/streamplace/chat/Composer.svelte.d.ts +15 -0
- package/dist/streamplace/chat/Embeds.svelte +135 -0
- package/dist/streamplace/chat/Embeds.svelte.d.ts +13 -0
- package/dist/streamplace/chat/Message.svelte +303 -0
- package/dist/streamplace/chat/Message.svelte.d.ts +25 -0
- package/dist/streamplace/chat/StreamChat.svelte +463 -0
- package/dist/streamplace/chat/StreamChat.svelte.d.ts +33 -0
- package/dist/streamplace/chat/StreamChatPopups.svelte +151 -0
- package/dist/streamplace/chat/StreamChatPopups.svelte.d.ts +15 -0
- package/dist/streamplace/chat/UserCard.svelte +76 -0
- package/dist/streamplace/chat/UserCard.svelte.d.ts +12 -0
- package/dist/streamplace/chat/color.d.ts +7 -0
- package/dist/streamplace/chat/color.js +33 -0
- package/dist/streamplace/chat/links.d.ts +10 -0
- package/dist/streamplace/chat/links.js +29 -0
- package/dist/streamplace/facets.d.ts +15 -0
- package/dist/streamplace/facets.js +112 -0
- package/dist/streamplace/link-embed.d.ts +13 -0
- package/dist/streamplace/link-embed.js +16 -0
- package/dist/streamplace/links.d.ts +6 -0
- package/dist/streamplace/links.js +1 -0
- package/dist/streamplace/reactions.d.ts +21 -0
- package/dist/streamplace/reactions.js +46 -0
- package/dist/streamplace/types.d.ts +200 -0
- package/dist/streamplace/types.js +1 -0
- package/dist/streamplace-data.d.ts +11 -0
- package/dist/streamplace-data.js +10 -0
- package/dist/streamplace.d.ts +14 -6
- package/dist/streamplace.js +11 -1
- package/dist/{events/event-view → video}/Player.svelte +69 -21
- package/dist/video/Player.svelte.d.ts +38 -0
- package/dist/video/engines.d.ts +59 -0
- package/dist/video/engines.js +647 -0
- package/package.json +11 -1
- package/dist/events/event-view/Player.svelte.d.ts +0 -24
|
@@ -0,0 +1,647 @@
|
|
|
1
|
+
/** How long the node gets to answer one playback request. */
|
|
2
|
+
const ANSWER_DEADLINE = 8000;
|
|
3
|
+
/** A peer that negotiated but never delivered media is not worth many more tries. */
|
|
4
|
+
const SILENT_ATTEMPTS = 2;
|
|
5
|
+
/** The node's refusal of a playback request, with whether to treat it as offline. */
|
|
6
|
+
class Refused extends Error {
|
|
7
|
+
reason;
|
|
8
|
+
constructor(reason) {
|
|
9
|
+
super(reason);
|
|
10
|
+
this.reason = reason;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
const peers = new Map();
|
|
14
|
+
/** Unused sessions kept open at most, newest first. */
|
|
15
|
+
const MAX_IDLE_PEERS = 4;
|
|
16
|
+
function openPeer(url) {
|
|
17
|
+
const pc = new RTCPeerConnection({ bundlePolicy: 'max-bundle' });
|
|
18
|
+
pc.addTransceiver('video', { direction: 'recvonly' });
|
|
19
|
+
pc.addTransceiver('audio', { direction: 'recvonly' });
|
|
20
|
+
const peer = {
|
|
21
|
+
url,
|
|
22
|
+
pc,
|
|
23
|
+
stream: new MediaStream(),
|
|
24
|
+
answered: Promise.resolve(),
|
|
25
|
+
request: new AbortController(),
|
|
26
|
+
users: 0,
|
|
27
|
+
idleSince: 0,
|
|
28
|
+
closed: false,
|
|
29
|
+
listeners: new Set()
|
|
30
|
+
};
|
|
31
|
+
pc.ontrack = (event) => {
|
|
32
|
+
peer.stream.addTrack(event.track);
|
|
33
|
+
for (const listener of peer.listeners)
|
|
34
|
+
listener.track();
|
|
35
|
+
};
|
|
36
|
+
pc.onconnectionstatechange = () => {
|
|
37
|
+
for (const listener of peer.listeners)
|
|
38
|
+
listener.state();
|
|
39
|
+
};
|
|
40
|
+
peer.answered = negotiate(peer);
|
|
41
|
+
// Close a failed session without notifying listeners via `state()`: active users already
|
|
42
|
+
// await `peer.answered` and need the original `Refused` reason before reacting.
|
|
43
|
+
peer.answered.catch(() => closePeer(peer, false));
|
|
44
|
+
peers.set(url, peer);
|
|
45
|
+
return peer;
|
|
46
|
+
}
|
|
47
|
+
async function negotiate(peer) {
|
|
48
|
+
const { pc } = peer;
|
|
49
|
+
await pc.setLocalDescription(await pc.createOffer());
|
|
50
|
+
await new Promise((resolve) => {
|
|
51
|
+
if (pc.iceGatheringState === 'complete')
|
|
52
|
+
return resolve();
|
|
53
|
+
const timer = setTimeout(resolve, 800);
|
|
54
|
+
pc.addEventListener('icegatheringstatechange', () => {
|
|
55
|
+
if (pc.iceGatheringState === 'complete') {
|
|
56
|
+
clearTimeout(timer);
|
|
57
|
+
resolve();
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
// Closed while gathering: nothing is requested.
|
|
62
|
+
if (peer.closed)
|
|
63
|
+
throw new Refused('network');
|
|
64
|
+
const deadline = setTimeout(() => peer.request.abort(), ANSWER_DEADLINE);
|
|
65
|
+
let response;
|
|
66
|
+
let answer;
|
|
67
|
+
try {
|
|
68
|
+
response = await fetch(peer.url, {
|
|
69
|
+
method: 'POST',
|
|
70
|
+
headers: { 'content-type': 'application/sdp' },
|
|
71
|
+
body: pc.localDescription?.sdp,
|
|
72
|
+
signal: peer.request.signal
|
|
73
|
+
});
|
|
74
|
+
// The deadline covers the body too.
|
|
75
|
+
answer = await response.text();
|
|
76
|
+
}
|
|
77
|
+
finally {
|
|
78
|
+
clearTimeout(deadline);
|
|
79
|
+
}
|
|
80
|
+
if (peer.closed)
|
|
81
|
+
throw new Refused('network');
|
|
82
|
+
if (!response.ok)
|
|
83
|
+
throw new Refused(response.status >= 500 ? 'offline' : 'network');
|
|
84
|
+
await pc.setRemoteDescription({ type: 'answer', sdp: answer });
|
|
85
|
+
}
|
|
86
|
+
function usable(peer) {
|
|
87
|
+
const state = peer?.pc.connectionState;
|
|
88
|
+
return !!peer && !peer.closed && state !== 'failed' && state !== 'closed';
|
|
89
|
+
}
|
|
90
|
+
function acquirePeer(url) {
|
|
91
|
+
const existing = peers.get(url);
|
|
92
|
+
const peer = usable(existing) ? existing : openPeer(url);
|
|
93
|
+
clearTimeout(peer.idle);
|
|
94
|
+
peer.idle = undefined;
|
|
95
|
+
peer.users += 1;
|
|
96
|
+
return peer;
|
|
97
|
+
}
|
|
98
|
+
function releasePeer(peer, keep) {
|
|
99
|
+
peer.users -= 1;
|
|
100
|
+
if (peer.users > 0 || peer.closed)
|
|
101
|
+
return;
|
|
102
|
+
if (keep <= 0)
|
|
103
|
+
return closePeer(peer);
|
|
104
|
+
peer.idleSince = Date.now();
|
|
105
|
+
peer.idle = setTimeout(() => closePeer(peer), keep);
|
|
106
|
+
const idle = [...peers.values()]
|
|
107
|
+
.filter((other) => !other.users && !other.closed)
|
|
108
|
+
.sort((a, b) => b.idleSince - a.idleSince);
|
|
109
|
+
for (const old of idle.slice(MAX_IDLE_PEERS))
|
|
110
|
+
closePeer(old);
|
|
111
|
+
}
|
|
112
|
+
function closePeer(peer, notify = true) {
|
|
113
|
+
if (peer.closed)
|
|
114
|
+
return;
|
|
115
|
+
peer.closed = true;
|
|
116
|
+
clearTimeout(peer.idle);
|
|
117
|
+
peer.request.abort();
|
|
118
|
+
peer.pc.close();
|
|
119
|
+
if (peers.get(peer.url) === peer)
|
|
120
|
+
peers.delete(peer.url);
|
|
121
|
+
// `close()` fires no state change; anyone still playing it has to hear.
|
|
122
|
+
if (notify)
|
|
123
|
+
for (const listener of peer.listeners)
|
|
124
|
+
listener.state();
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Starts a WHEP session nobody watches yet, kept open for `keep` milliseconds, so that
|
|
128
|
+
* `startWebRTC` for the same URL begins with frames already arriving. Calling it again
|
|
129
|
+
* while the session is open only extends it.
|
|
130
|
+
*/
|
|
131
|
+
export function warmWebRTC(url, keep = 15_000) {
|
|
132
|
+
if (typeof RTCPeerConnection === 'undefined')
|
|
133
|
+
return;
|
|
134
|
+
releasePeer(acquirePeer(url), keep);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Sub-second playback over WHEP. Reconnects by itself and gives up after `attempts` failures
|
|
138
|
+
* in a row (4 by default; 1 never retries), or sooner when peers connect and no media ever
|
|
139
|
+
* arrives, as on networks that block it.
|
|
140
|
+
*
|
|
141
|
+
* Sessions are shared by URL: one opened by `warmWebRTC`, or by another engine (a hover
|
|
142
|
+
* preview), is taken over instead of negotiating again. `keep` leaves the session open that
|
|
143
|
+
* long after `destroy()`, for the next engine to take over.
|
|
144
|
+
*/
|
|
145
|
+
export function startWebRTC(video, url, events, options = {}) {
|
|
146
|
+
const attempts = options.attempts ?? 4;
|
|
147
|
+
let destroyed = false;
|
|
148
|
+
let peer;
|
|
149
|
+
let listener = { track() { }, state() { } };
|
|
150
|
+
let retry;
|
|
151
|
+
let watchdog;
|
|
152
|
+
let failures = 0;
|
|
153
|
+
let silent = 0;
|
|
154
|
+
let lastBytes = 0;
|
|
155
|
+
let lastBytesAt = 0;
|
|
156
|
+
let previous;
|
|
157
|
+
/** Lets go of the session; `drop` closes it for everyone, as after a failure. */
|
|
158
|
+
const detach = (drop) => {
|
|
159
|
+
clearInterval(watchdog);
|
|
160
|
+
const current = peer;
|
|
161
|
+
peer = undefined;
|
|
162
|
+
if (!current)
|
|
163
|
+
return;
|
|
164
|
+
current.listeners.delete(listener);
|
|
165
|
+
if (drop)
|
|
166
|
+
closePeer(current);
|
|
167
|
+
releasePeer(current, drop ? 0 : (options.keep ?? 0));
|
|
168
|
+
};
|
|
169
|
+
const failed = (reason) => {
|
|
170
|
+
if (destroyed)
|
|
171
|
+
return;
|
|
172
|
+
clearTimeout(retry);
|
|
173
|
+
detach(true);
|
|
174
|
+
failures += 1;
|
|
175
|
+
if (failures >= attempts)
|
|
176
|
+
return events.onfail?.();
|
|
177
|
+
events.onwaiting?.(reason);
|
|
178
|
+
retry = setTimeout(connect, Math.min(4000, 600 * 2 ** failures));
|
|
179
|
+
};
|
|
180
|
+
async function connect() {
|
|
181
|
+
if (destroyed)
|
|
182
|
+
return;
|
|
183
|
+
detach(false);
|
|
184
|
+
const current = acquirePeer(url);
|
|
185
|
+
peer = current;
|
|
186
|
+
const showTracks = () => {
|
|
187
|
+
if (peer !== current || !current.stream.getTracks().length)
|
|
188
|
+
return;
|
|
189
|
+
if (video.srcObject !== current.stream)
|
|
190
|
+
video.srcObject = current.stream;
|
|
191
|
+
events.onready?.();
|
|
192
|
+
};
|
|
193
|
+
listener = {
|
|
194
|
+
track: showTracks,
|
|
195
|
+
state: () => {
|
|
196
|
+
if (peer !== current)
|
|
197
|
+
return;
|
|
198
|
+
const state = current.pc.connectionState;
|
|
199
|
+
if (current.closed || state === 'failed' || state === 'closed')
|
|
200
|
+
failed('network');
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
current.listeners.add(listener);
|
|
204
|
+
// A session taken over may be playing already.
|
|
205
|
+
showTracks();
|
|
206
|
+
try {
|
|
207
|
+
await current.answered;
|
|
208
|
+
}
|
|
209
|
+
catch (cause) {
|
|
210
|
+
// A superseded attempt must not count as a failure of the current one.
|
|
211
|
+
if (peer !== current)
|
|
212
|
+
return;
|
|
213
|
+
return failed(cause instanceof Refused ? cause.reason : 'network');
|
|
214
|
+
}
|
|
215
|
+
// A connection can stay "connected" while no media arrives, e.g. when the stream ends.
|
|
216
|
+
if (peer !== current)
|
|
217
|
+
return;
|
|
218
|
+
const connection = current.pc;
|
|
219
|
+
lastBytes = 0;
|
|
220
|
+
lastBytesAt = Date.now();
|
|
221
|
+
watchdog = setInterval(async () => {
|
|
222
|
+
if (peer !== current)
|
|
223
|
+
return;
|
|
224
|
+
const reports = await connection.getStats().catch(() => undefined);
|
|
225
|
+
if (peer !== current)
|
|
226
|
+
return;
|
|
227
|
+
let bytes = 0;
|
|
228
|
+
reports?.forEach((report) => {
|
|
229
|
+
if (report.type === 'inbound-rtp')
|
|
230
|
+
bytes += report.bytesReceived ?? 0;
|
|
231
|
+
});
|
|
232
|
+
if (bytes > lastBytes) {
|
|
233
|
+
// A negotiated peer alone does not mean any media arrived.
|
|
234
|
+
failures = 0;
|
|
235
|
+
silent = 0;
|
|
236
|
+
lastBytes = bytes;
|
|
237
|
+
lastBytesAt = Date.now();
|
|
238
|
+
}
|
|
239
|
+
else if (Date.now() - lastBytesAt > 6000) {
|
|
240
|
+
// Never any media on this peer, again: stop early so the caller can fall back.
|
|
241
|
+
if (!lastBytes && ++silent >= SILENT_ATTEMPTS)
|
|
242
|
+
failures = attempts;
|
|
243
|
+
failed('offline');
|
|
244
|
+
}
|
|
245
|
+
}, 2000);
|
|
246
|
+
}
|
|
247
|
+
void connect();
|
|
248
|
+
return {
|
|
249
|
+
destroy() {
|
|
250
|
+
destroyed = true;
|
|
251
|
+
clearTimeout(retry);
|
|
252
|
+
detach(false);
|
|
253
|
+
video.srcObject = null;
|
|
254
|
+
},
|
|
255
|
+
latency: () => undefined,
|
|
256
|
+
async stats() {
|
|
257
|
+
if (!peer)
|
|
258
|
+
return {};
|
|
259
|
+
let bytes = 0;
|
|
260
|
+
let dropped;
|
|
261
|
+
(await peer.pc.getStats()).forEach((report) => {
|
|
262
|
+
if (report.type !== 'inbound-rtp')
|
|
263
|
+
return;
|
|
264
|
+
bytes += report.bytesReceived ?? 0;
|
|
265
|
+
if (report.kind === 'video')
|
|
266
|
+
dropped = report.framesDropped;
|
|
267
|
+
});
|
|
268
|
+
const now = performance.now();
|
|
269
|
+
const bitrate = previous
|
|
270
|
+
? ((bytes - previous.bytes) * 8) / ((now - previous.at) / 1000)
|
|
271
|
+
: undefined;
|
|
272
|
+
previous = { bytes, at: now };
|
|
273
|
+
return {
|
|
274
|
+
bitrate: bitrate && bitrate > 0 ? bitrate : undefined,
|
|
275
|
+
dropped,
|
|
276
|
+
resolution: video.videoWidth ? `${video.videoWidth}×${video.videoHeight}` : undefined
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
const prefetched = new Map();
|
|
282
|
+
const PREFETCH_TTL = 4 * 60_000;
|
|
283
|
+
const MAX_PREFETCH_ENTRIES = 120;
|
|
284
|
+
const MAX_PREFETCH_BYTES = 16 * 1024 * 1024;
|
|
285
|
+
let prefetchedBytes = 0;
|
|
286
|
+
let sweepTimer;
|
|
287
|
+
const cacheKey = (url, start = 0, end = 0) => (end ? `${url}#${start}-${end}` : url);
|
|
288
|
+
function deletePrefetched(key) {
|
|
289
|
+
const entry = prefetched.get(key);
|
|
290
|
+
if (!entry)
|
|
291
|
+
return;
|
|
292
|
+
prefetchedBytes -= entry.bytes;
|
|
293
|
+
prefetched.delete(key);
|
|
294
|
+
}
|
|
295
|
+
function scheduleSweep() {
|
|
296
|
+
if (sweepTimer || !prefetched.size)
|
|
297
|
+
return;
|
|
298
|
+
let oldest = Infinity;
|
|
299
|
+
for (const entry of prefetched.values())
|
|
300
|
+
if (entry.at < oldest)
|
|
301
|
+
oldest = entry.at;
|
|
302
|
+
const delay = Math.max(1000, oldest + PREFETCH_TTL - Date.now());
|
|
303
|
+
sweepTimer = setTimeout(() => {
|
|
304
|
+
sweepTimer = undefined;
|
|
305
|
+
sweep();
|
|
306
|
+
}, delay);
|
|
307
|
+
}
|
|
308
|
+
function cached(key) {
|
|
309
|
+
const entry = prefetched.get(key);
|
|
310
|
+
if (entry && Date.now() - entry.at < PREFETCH_TTL)
|
|
311
|
+
return entry.data;
|
|
312
|
+
deletePrefetched(key);
|
|
313
|
+
}
|
|
314
|
+
function sweep() {
|
|
315
|
+
const now = Date.now();
|
|
316
|
+
for (const [key, entry] of prefetched) {
|
|
317
|
+
if (now - entry.at >= PREFETCH_TTL)
|
|
318
|
+
deletePrefetched(key);
|
|
319
|
+
}
|
|
320
|
+
if (!prefetched.size) {
|
|
321
|
+
clearTimeout(sweepTimer);
|
|
322
|
+
sweepTimer = undefined;
|
|
323
|
+
}
|
|
324
|
+
else {
|
|
325
|
+
scheduleSweep();
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
function trimPrefetched() {
|
|
329
|
+
for (const key of prefetched.keys()) {
|
|
330
|
+
if (prefetched.size <= MAX_PREFETCH_ENTRIES && prefetchedBytes <= MAX_PREFETCH_BYTES)
|
|
331
|
+
break;
|
|
332
|
+
deletePrefetched(key);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
function prefetchOnce(url, kind, range) {
|
|
336
|
+
const key = cacheKey(url, range?.[0], range?.[1]);
|
|
337
|
+
const hit = cached(key);
|
|
338
|
+
if (hit)
|
|
339
|
+
return hit;
|
|
340
|
+
sweep();
|
|
341
|
+
const start = performance.now();
|
|
342
|
+
const data = fetch(url, range ? { headers: { range: `bytes=${range[0]}-${range[1] - 1}` } } : {})
|
|
343
|
+
.then(async (response) => {
|
|
344
|
+
if (!response.ok)
|
|
345
|
+
throw new Error(`${response.status}`);
|
|
346
|
+
const body = kind === 'text' ? await response.text() : await response.arrayBuffer();
|
|
347
|
+
const size = typeof body === 'string' ? body.length * 2 : body.byteLength;
|
|
348
|
+
const entry = prefetched.get(key);
|
|
349
|
+
if (entry?.data === data) {
|
|
350
|
+
prefetchedBytes += size - entry.bytes;
|
|
351
|
+
entry.bytes = size;
|
|
352
|
+
trimPrefetched();
|
|
353
|
+
}
|
|
354
|
+
return { body, url: response.url || url, start, end: performance.now() };
|
|
355
|
+
})
|
|
356
|
+
.catch((cause) => {
|
|
357
|
+
if (prefetched.get(key)?.data === data)
|
|
358
|
+
deletePrefetched(key);
|
|
359
|
+
throw cause;
|
|
360
|
+
});
|
|
361
|
+
prefetched.set(key, { at: Date.now(), bytes: 0, data });
|
|
362
|
+
trimPrefetched();
|
|
363
|
+
scheduleSweep();
|
|
364
|
+
// Nobody may ever use it.
|
|
365
|
+
data.catch(() => { });
|
|
366
|
+
return data;
|
|
367
|
+
}
|
|
368
|
+
const tagValue = (line, name) => line
|
|
369
|
+
.match(new RegExp(`${name}=(?:"([^"]*)"|([^,]*))`))
|
|
370
|
+
?.slice(1)
|
|
371
|
+
.find((v) => v !== undefined);
|
|
372
|
+
/** Fetches the first `seconds` of a media playlist, the same requests hls.js will make. */
|
|
373
|
+
async function prefetchMedia(url, seconds) {
|
|
374
|
+
const playlist = await prefetchOnce(url, 'text');
|
|
375
|
+
const base = playlist.url;
|
|
376
|
+
const text = String(playlist.body);
|
|
377
|
+
const loads = [];
|
|
378
|
+
let duration = 0;
|
|
379
|
+
let range;
|
|
380
|
+
let previousEnd = 0;
|
|
381
|
+
for (const raw of text.split('\n')) {
|
|
382
|
+
const line = raw.trim();
|
|
383
|
+
if (!line)
|
|
384
|
+
continue;
|
|
385
|
+
if (line.startsWith('#EXT-X-MAP:')) {
|
|
386
|
+
const uri = tagValue(line, 'URI');
|
|
387
|
+
const [length, offset] = (tagValue(line, 'BYTERANGE') ?? '').split('@').map(Number);
|
|
388
|
+
if (uri) {
|
|
389
|
+
const mapRange = length
|
|
390
|
+
? [offset || 0, (offset || 0) + length]
|
|
391
|
+
: undefined;
|
|
392
|
+
loads.push(prefetchOnce(new URL(uri, base).href, 'bytes', mapRange));
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
else if (line.startsWith('#EXTINF:')) {
|
|
396
|
+
if (duration >= seconds)
|
|
397
|
+
break;
|
|
398
|
+
duration += parseFloat(line.slice(8)) || 0;
|
|
399
|
+
}
|
|
400
|
+
else if (line.startsWith('#EXT-X-BYTERANGE:')) {
|
|
401
|
+
const [length, offset] = line.slice(17).split('@').map(Number);
|
|
402
|
+
const start = Number.isFinite(offset) ? offset : previousEnd;
|
|
403
|
+
range = [start, start + length];
|
|
404
|
+
}
|
|
405
|
+
else if (!line.startsWith('#')) {
|
|
406
|
+
loads.push(prefetchOnce(new URL(line, base).href, 'bytes', range));
|
|
407
|
+
previousEnd = range?.[1] ?? 0;
|
|
408
|
+
range = undefined;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
await Promise.all(loads);
|
|
412
|
+
}
|
|
413
|
+
let hlsModule;
|
|
414
|
+
function loadHls() {
|
|
415
|
+
return (hlsModule ??= import('hls.js').catch((cause) => {
|
|
416
|
+
hlsModule = undefined;
|
|
417
|
+
throw cause;
|
|
418
|
+
}));
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Loads a recording's playlists and its first `seconds` of media, so `startHls` for the same
|
|
422
|
+
* URL soon after starts from memory. With `seconds` at 0 only the playlists (and the small
|
|
423
|
+
* init segments) are loaded: cheap enough for every video in view, leaving the media for the
|
|
424
|
+
* moment a pointer reaches one. What is already loaded is not loaded again. Never throws.
|
|
425
|
+
*/
|
|
426
|
+
export async function prefetchHls(url, seconds = 2) {
|
|
427
|
+
if (typeof window === 'undefined')
|
|
428
|
+
return;
|
|
429
|
+
void loadHls().catch(() => { });
|
|
430
|
+
try {
|
|
431
|
+
const master = await prefetchOnce(url, 'text');
|
|
432
|
+
const text = String(master.body);
|
|
433
|
+
if (!text.includes('#EXT-X-STREAM-INF'))
|
|
434
|
+
return await prefetchMedia(url, seconds);
|
|
435
|
+
const lines = text
|
|
436
|
+
.split('\n')
|
|
437
|
+
.map((line) => line.trim())
|
|
438
|
+
.filter(Boolean);
|
|
439
|
+
// hls.js starts on the first variant listed and the default audio rendition.
|
|
440
|
+
const variant = lines.find((_, i) => i && lines[i - 1].startsWith('#EXT-X-STREAM-INF'));
|
|
441
|
+
const audio = lines.filter((line) => line.startsWith('#EXT-X-MEDIA:') && /TYPE=AUDIO/.test(line));
|
|
442
|
+
const audioUri = tagValue(audio.find((line) => /DEFAULT=YES/.test(line)) ?? audio[0] ?? '', 'URI');
|
|
443
|
+
await Promise.all([variant, audioUri]
|
|
444
|
+
.filter((uri) => !!uri)
|
|
445
|
+
.map((uri) => prefetchMedia(new URL(uri, master.url).href, seconds)));
|
|
446
|
+
}
|
|
447
|
+
catch {
|
|
448
|
+
// Playback loads it again.
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
/** hls.js's own loader, answering from what `prefetchHls` already has. */
|
|
452
|
+
function prefetchingLoader(Base) {
|
|
453
|
+
return class {
|
|
454
|
+
#inner;
|
|
455
|
+
#done = false;
|
|
456
|
+
#pending;
|
|
457
|
+
context = null;
|
|
458
|
+
stats;
|
|
459
|
+
constructor(config) {
|
|
460
|
+
this.#inner = new Base(config);
|
|
461
|
+
this.stats = this.#inner.stats;
|
|
462
|
+
}
|
|
463
|
+
load(context, config, callbacks) {
|
|
464
|
+
const hit = cached(cacheKey(context.url, context.rangeStart, context.rangeEnd));
|
|
465
|
+
const fallback = () => {
|
|
466
|
+
this.#pending = undefined;
|
|
467
|
+
this.#inner.load(context, config, callbacks);
|
|
468
|
+
this.context = this.#inner.context;
|
|
469
|
+
};
|
|
470
|
+
if (!hit)
|
|
471
|
+
return fallback();
|
|
472
|
+
this.context = context;
|
|
473
|
+
this.#pending = callbacks;
|
|
474
|
+
hit.then(({ body, url, start, end }) => {
|
|
475
|
+
if (this.#done)
|
|
476
|
+
return;
|
|
477
|
+
this.#pending = undefined;
|
|
478
|
+
const wantsText = context.responseType === 'text';
|
|
479
|
+
if (wantsText !== (typeof body === 'string'))
|
|
480
|
+
return fallback();
|
|
481
|
+
// The real timings, so bandwidth estimates stay honest. A copy, because hls.js
|
|
482
|
+
// may hand the buffer to a worker.
|
|
483
|
+
const data = typeof body === 'string' ? body : body.slice(0);
|
|
484
|
+
const size = typeof data === 'string' ? data.length : data.byteLength;
|
|
485
|
+
const { stats } = this;
|
|
486
|
+
stats.loading.start = start;
|
|
487
|
+
stats.loading.first = start + Math.min(50, end - start);
|
|
488
|
+
stats.loading.end = end;
|
|
489
|
+
stats.loaded = stats.total = size;
|
|
490
|
+
callbacks.onSuccess({ url: url || context.url, data, code: 200 }, stats, context, null);
|
|
491
|
+
}, () => !this.#done && fallback());
|
|
492
|
+
}
|
|
493
|
+
abort() {
|
|
494
|
+
this.#done = true;
|
|
495
|
+
if (this.#pending && this.context) {
|
|
496
|
+
const callbacks = this.#pending;
|
|
497
|
+
this.#pending = undefined;
|
|
498
|
+
this.stats.aborted = true;
|
|
499
|
+
callbacks.onAbort?.(this.stats, this.context, null);
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
this.#inner.abort();
|
|
503
|
+
}
|
|
504
|
+
destroy() {
|
|
505
|
+
this.#done = true;
|
|
506
|
+
this.#pending = undefined;
|
|
507
|
+
this.#inner.destroy();
|
|
508
|
+
}
|
|
509
|
+
getCacheAge() {
|
|
510
|
+
return this.#inner.getCacheAge?.() ?? null;
|
|
511
|
+
}
|
|
512
|
+
getResponseHeader(name) {
|
|
513
|
+
return this.#inner.getResponseHeader?.(name) ?? null;
|
|
514
|
+
}
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
/** Buffered playback over HLS: a few seconds behind live, with quality levels. */
|
|
518
|
+
export function startHls(video, url, events, options) {
|
|
519
|
+
let destroyed = false;
|
|
520
|
+
let hls;
|
|
521
|
+
let retry;
|
|
522
|
+
let misses = 0;
|
|
523
|
+
let recoveries = 0;
|
|
524
|
+
const retryLoad = (reason, reload) => {
|
|
525
|
+
clearTimeout(retry);
|
|
526
|
+
misses += 1;
|
|
527
|
+
if (!options.live && misses > 3)
|
|
528
|
+
return events.onfail?.();
|
|
529
|
+
events.onwaiting?.(reason);
|
|
530
|
+
retry = setTimeout(() => !destroyed && reload(), Math.min(8000, 1500 * misses));
|
|
531
|
+
};
|
|
532
|
+
const listeners = new AbortController();
|
|
533
|
+
const native = () => {
|
|
534
|
+
let sought = false;
|
|
535
|
+
const reload = () => {
|
|
536
|
+
video.src = url;
|
|
537
|
+
};
|
|
538
|
+
const listener = { signal: listeners.signal };
|
|
539
|
+
video.addEventListener('loadedmetadata', () => {
|
|
540
|
+
if (options.startAt && !sought) {
|
|
541
|
+
sought = true;
|
|
542
|
+
video.currentTime = options.startAt;
|
|
543
|
+
}
|
|
544
|
+
events.onready?.();
|
|
545
|
+
}, listener);
|
|
546
|
+
video.addEventListener('playing', () => (misses = 0), listener);
|
|
547
|
+
video.addEventListener('error', () => retryLoad('offline', reload), listener);
|
|
548
|
+
reload();
|
|
549
|
+
};
|
|
550
|
+
async function load() {
|
|
551
|
+
let HlsClass;
|
|
552
|
+
try {
|
|
553
|
+
({ default: HlsClass } = await loadHls());
|
|
554
|
+
}
|
|
555
|
+
catch {
|
|
556
|
+
if (!destroyed)
|
|
557
|
+
events.onfail?.();
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
if (destroyed)
|
|
561
|
+
return;
|
|
562
|
+
if (!HlsClass.isSupported()) {
|
|
563
|
+
if (video.canPlayType('application/vnd.apple.mpegurl'))
|
|
564
|
+
return native();
|
|
565
|
+
return events.onfail?.();
|
|
566
|
+
}
|
|
567
|
+
const instance = new HlsClass({
|
|
568
|
+
...(!options.live && HlsClass.DefaultConfig?.loader
|
|
569
|
+
? { loader: prefetchingLoader(HlsClass.DefaultConfig.loader) }
|
|
570
|
+
: {}),
|
|
571
|
+
lowLatencyMode: options.live,
|
|
572
|
+
backBufferLength: options.live ? 20 : 60,
|
|
573
|
+
startPosition: options.startAt ?? -1,
|
|
574
|
+
...(options.live
|
|
575
|
+
? {
|
|
576
|
+
liveSyncDurationCount: 2,
|
|
577
|
+
liveMaxLatencyDurationCount: 8,
|
|
578
|
+
maxLiveSyncPlaybackRate: 1.25
|
|
579
|
+
}
|
|
580
|
+
: {})
|
|
581
|
+
});
|
|
582
|
+
hls = instance;
|
|
583
|
+
instance.on(HlsClass.Events.FRAG_BUFFERED, () => (misses = 0));
|
|
584
|
+
// Recovered for real once playback moves on, not merely once a fragment is buffered.
|
|
585
|
+
const progressed = () => (recoveries = 0);
|
|
586
|
+
video.addEventListener('timeupdate', progressed, { signal: listeners.signal });
|
|
587
|
+
instance.on(HlsClass.Events.MANIFEST_PARSED, () => {
|
|
588
|
+
events.onready?.();
|
|
589
|
+
const levels = instance.levels
|
|
590
|
+
.map((level, index) => ({
|
|
591
|
+
index,
|
|
592
|
+
label: level.height ? `${level.height}p` : level.name || `${index}`
|
|
593
|
+
}))
|
|
594
|
+
.reverse();
|
|
595
|
+
events.onlevels?.(levels.length > 1 ? levels : []);
|
|
596
|
+
});
|
|
597
|
+
instance.on(HlsClass.Events.ERROR, (_event, data) => {
|
|
598
|
+
if (!data.fatal || destroyed)
|
|
599
|
+
return;
|
|
600
|
+
if (data.type === HlsClass.ErrorTypes.MEDIA_ERROR) {
|
|
601
|
+
// Media the browser cannot decode fails the same way every time.
|
|
602
|
+
if (++recoveries > 3) {
|
|
603
|
+
instance.stopLoad();
|
|
604
|
+
return events.onfail?.();
|
|
605
|
+
}
|
|
606
|
+
return instance.recoverMediaError();
|
|
607
|
+
}
|
|
608
|
+
// A missing playlist means the stream is not live on this node (yet).
|
|
609
|
+
retryLoad(data.response?.code && data.response.code < 500 ? 'offline' : 'network', () => {
|
|
610
|
+
instance.loadSource(url);
|
|
611
|
+
instance.startLoad();
|
|
612
|
+
});
|
|
613
|
+
});
|
|
614
|
+
instance.loadSource(url);
|
|
615
|
+
instance.attachMedia(video);
|
|
616
|
+
}
|
|
617
|
+
void load();
|
|
618
|
+
return {
|
|
619
|
+
destroy() {
|
|
620
|
+
destroyed = true;
|
|
621
|
+
clearTimeout(retry);
|
|
622
|
+
listeners.abort();
|
|
623
|
+
hls?.destroy();
|
|
624
|
+
video.removeAttribute('src');
|
|
625
|
+
video.load();
|
|
626
|
+
},
|
|
627
|
+
setLevel(index) {
|
|
628
|
+
if (hls)
|
|
629
|
+
hls.currentLevel = index;
|
|
630
|
+
},
|
|
631
|
+
latency: () => (options.live ? (hls?.latency ?? undefined) : undefined),
|
|
632
|
+
toLive() {
|
|
633
|
+
const position = hls?.liveSyncPosition;
|
|
634
|
+
if (position)
|
|
635
|
+
video.currentTime = position;
|
|
636
|
+
},
|
|
637
|
+
async stats() {
|
|
638
|
+
const level = hls?.levels[hls.currentLevel];
|
|
639
|
+
const quality = video.getVideoPlaybackQuality?.();
|
|
640
|
+
return {
|
|
641
|
+
bitrate: level?.bitrate,
|
|
642
|
+
resolution: video.videoWidth ? `${video.videoWidth}×${video.videoHeight}` : undefined,
|
|
643
|
+
dropped: quality?.droppedVideoFrames
|
|
644
|
+
};
|
|
645
|
+
}
|
|
646
|
+
};
|
|
647
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atmo-dev/kit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.22.0",
|
|
4
4
|
"description": "The shared UI foundation for Atmo apps.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -40,6 +40,11 @@
|
|
|
40
40
|
"types": "./dist/events-data.d.ts",
|
|
41
41
|
"default": "./dist/events-data.js"
|
|
42
42
|
},
|
|
43
|
+
"./events/card": {
|
|
44
|
+
"types": "./dist/events-card.d.ts",
|
|
45
|
+
"svelte": "./dist/events-card.js",
|
|
46
|
+
"default": "./dist/events-card.js"
|
|
47
|
+
},
|
|
43
48
|
"./events/editor": {
|
|
44
49
|
"types": "./dist/events-editor.d.ts",
|
|
45
50
|
"default": "./dist/events-editor.js"
|
|
@@ -54,8 +59,13 @@
|
|
|
54
59
|
},
|
|
55
60
|
"./streamplace": {
|
|
56
61
|
"types": "./dist/streamplace.d.ts",
|
|
62
|
+
"svelte": "./dist/streamplace.js",
|
|
57
63
|
"default": "./dist/streamplace.js"
|
|
58
64
|
},
|
|
65
|
+
"./streamplace-data": {
|
|
66
|
+
"types": "./dist/streamplace-data.d.ts",
|
|
67
|
+
"default": "./dist/streamplace-data.js"
|
|
68
|
+
},
|
|
59
69
|
"./media": {
|
|
60
70
|
"types": "./dist/media.d.ts",
|
|
61
71
|
"default": "./dist/media.js"
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import type { Snippet } from 'svelte';
|
|
2
|
-
type $$ComponentProps = {
|
|
3
|
-
title: string;
|
|
4
|
-
poster?: string;
|
|
5
|
-
/** A live stream has no timeline to scrub and no playback speed. */
|
|
6
|
-
live?: boolean;
|
|
7
|
-
/** Start playing, muted, as browsers require. */
|
|
8
|
-
autoplay?: boolean;
|
|
9
|
-
/** WebVTT captions, shown from the start. */
|
|
10
|
-
captionsUrl?: string;
|
|
11
|
-
video?: HTMLVideoElement;
|
|
12
|
-
currentTime?: number;
|
|
13
|
-
/** Whether the controls are on screen, for overlays that come and go with them. */
|
|
14
|
-
controlsVisible?: boolean;
|
|
15
|
-
onplay?: () => void;
|
|
16
|
-
/** Overlay content, positioned by the caller inside the frame. */
|
|
17
|
-
children?: Snippet;
|
|
18
|
-
};
|
|
19
|
-
declare const Player: import("svelte").Component<$$ComponentProps, {
|
|
20
|
-
seek: (time: number) => void;
|
|
21
|
-
pause: () => void;
|
|
22
|
-
}, "video" | "currentTime" | "controlsVisible">;
|
|
23
|
-
type Player = ReturnType<typeof Player>;
|
|
24
|
-
export default Player;
|