@voltkit-rtc/client 0.4.0 → 0.5.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/CHANGELOG.md +15 -0
- package/README.md +90 -29
- package/dist/cjs/index.d.ts +63 -8
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +326 -26
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/music.d.ts +1 -1
- package/dist/cjs/music.d.ts.map +1 -1
- package/dist/cjs/quality.d.ts +19 -21
- package/dist/cjs/quality.d.ts.map +1 -1
- package/dist/cjs/quality.js +46 -28
- package/dist/cjs/quality.js.map +1 -1
- package/dist/esm/index.d.ts +63 -8
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +327 -27
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/music.d.ts +1 -1
- package/dist/esm/music.d.ts.map +1 -1
- package/dist/esm/quality.d.ts +19 -21
- package/dist/esm/quality.d.ts.map +1 -1
- package/dist/esm/quality.js +43 -28
- package/dist/esm/quality.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +308 -27
- package/src/music.ts +1 -1
- package/src/quality.ts +58 -29
package/src/quality.ts
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Video quality tiers and the simulcast layers sent for each.
|
|
3
|
-
*
|
|
4
|
-
* A project can publish at 720p or 480p on any plan. 1080p uses more
|
|
5
|
-
* bandwidth, so the server allows it only for projects that have it enabled,
|
|
6
|
-
* and for the first project during its free trial; the join result says which
|
|
7
|
-
* (`video.maxQuality`), and the server caps each publisher's incoming bitrate
|
|
8
|
-
* to match.
|
|
9
|
-
*/
|
|
1
|
+
/** All video qualities are available on every plan; per-project safety restrictions still apply. */
|
|
10
2
|
|
|
11
3
|
export type VideoQuality = '1080p' | '720p' | '480p';
|
|
12
4
|
|
|
@@ -17,13 +9,14 @@ export type LayerCount = 1 | 2 | 3;
|
|
|
17
9
|
|
|
18
10
|
/** What the server allows this project to publish, from the join result. */
|
|
19
11
|
export interface VideoPolicy {
|
|
20
|
-
maxQuality:
|
|
12
|
+
maxQuality: VideoQuality;
|
|
13
|
+
maxFrameRate?: 30 | 60;
|
|
21
14
|
/** The server's cap on one publisher's incoming bitrate, in bits per second. */
|
|
22
15
|
maxBitrate?: number;
|
|
23
16
|
}
|
|
24
17
|
|
|
25
18
|
/** Servers from before quality tiers send no policy; they get the default tier. */
|
|
26
|
-
export const DEFAULT_VIDEO_POLICY: VideoPolicy = { maxQuality: '
|
|
19
|
+
export const DEFAULT_VIDEO_POLICY: VideoPolicy = { maxQuality: '1080p', maxFrameRate: 60 };
|
|
27
20
|
|
|
28
21
|
/** Each tier's 16:9 frame. */
|
|
29
22
|
const TIER_SIZE: Record<VideoQuality, { width: number; height: number }> = {
|
|
@@ -36,27 +29,18 @@ const RANK: Record<VideoQuality, number> = { '480p': 0, '720p': 1, '1080p': 2 };
|
|
|
36
29
|
|
|
37
30
|
/** The lower of what was asked for and what the project may use. */
|
|
38
31
|
export function allowedQuality(requested: VideoQuality | undefined, policy: VideoPolicy = DEFAULT_VIDEO_POLICY): VideoQuality {
|
|
39
|
-
const max = policy.maxQuality
|
|
32
|
+
const max = policy.maxQuality in RANK ? policy.maxQuality : '1080p';
|
|
40
33
|
const wanted = requested && requested in RANK ? requested : max;
|
|
41
34
|
return RANK[wanted] <= RANK[max] ? wanted : max;
|
|
42
35
|
}
|
|
43
36
|
|
|
44
|
-
/**
|
|
45
|
-
* The bitrate a layer of this many pixels gets, at up to 30 fps.
|
|
46
|
-
* About 1080p 4 Mbps, 720p 2.5 Mbps, 540p 1.5 Mbps, 480p 1 Mbps,
|
|
47
|
-
* 360p 700 kbps, 240p 400 kbps, smaller 200 kbps.
|
|
48
|
-
*
|
|
49
|
-
* Measured on a 720p camera picture: 2.5 Mbps brought VP8's quantiser down
|
|
50
|
-
* from about 64 (at 1.8 Mbps) to about 55, which is finer detail, most of all
|
|
51
|
-
* in motion. The server's per-project caps (4 Mbps up to 720p, 7 Mbps with
|
|
52
|
-
* 1080p) carry these ladders with their audio.
|
|
53
|
-
*/
|
|
37
|
+
/** The authoritative 30 fps bitrate for a layer of this many pixels. */
|
|
54
38
|
export function layerBitrate(pixels: number): number {
|
|
55
|
-
if (pixels >= 1_800_000) return
|
|
56
|
-
if (pixels >= 800_000) return
|
|
57
|
-
if (pixels >= 450_000) return
|
|
58
|
-
if (pixels >= 360_000) return
|
|
59
|
-
if (pixels >= 200_000) return
|
|
39
|
+
if (pixels >= 1_800_000) return 6_000_000;
|
|
40
|
+
if (pixels >= 800_000) return 3_000_000;
|
|
41
|
+
if (pixels >= 450_000) return 2_000_000;
|
|
42
|
+
if (pixels >= 360_000) return 1_200_000;
|
|
43
|
+
if (pixels >= 200_000) return 800_000;
|
|
60
44
|
if (pixels >= 100_000) return 400_000;
|
|
61
45
|
return 200_000;
|
|
62
46
|
}
|
|
@@ -66,6 +50,7 @@ export interface VideoEncoding {
|
|
|
66
50
|
scaleResolutionDownBy: number;
|
|
67
51
|
maxBitrate: number;
|
|
68
52
|
scalabilityMode: 'L1T3';
|
|
53
|
+
maxFramerate: number;
|
|
69
54
|
}
|
|
70
55
|
|
|
71
56
|
/**
|
|
@@ -98,7 +83,7 @@ export function topLayerSize(width: number, height: number, quality: VideoQualit
|
|
|
98
83
|
* browser shrinks the picture and then sends fewer layers: from two it keeps
|
|
99
84
|
* the full one, from three it lost it.
|
|
100
85
|
*/
|
|
101
|
-
export function videoEncodings(width: number, height: number, quality: VideoQuality, maxLayers: LayerCount = 3): VideoEncoding[] {
|
|
86
|
+
export function videoEncodings(width: number, height: number, quality: VideoQuality, maxLayers: LayerCount = 3, frameRate: 30 | 60 = 30): VideoEncoding[] {
|
|
102
87
|
const { width: w, height: h, scale } = topLayerSize(width, height, quality);
|
|
103
88
|
const top = w * h;
|
|
104
89
|
const possible = top >= 960 * 540 ? 3 : top >= 480 * 270 ? 2 : 1;
|
|
@@ -108,7 +93,8 @@ export function videoEncodings(width: number, height: number, quality: VideoQual
|
|
|
108
93
|
const layer = (divisor: number, rid?: string): VideoEncoding => ({
|
|
109
94
|
...(rid ? { rid } : {}),
|
|
110
95
|
scaleResolutionDownBy: round(scale * divisor),
|
|
111
|
-
maxBitrate: layerBitrate(top / (divisor * divisor)),
|
|
96
|
+
maxBitrate: layerBitrate(top / (divisor * divisor)) * (divisor === 1 && frameRate === 60 ? 1.5 : 1),
|
|
97
|
+
maxFramerate: divisor === 1 ? frameRate : 30,
|
|
112
98
|
scalabilityMode: 'L1T3',
|
|
113
99
|
});
|
|
114
100
|
if (layers === 3) return [layer(4, 'q'), layer(2, 'h'), layer(1, 'f')];
|
|
@@ -124,3 +110,46 @@ export function videoEncodings(width: number, height: number, quality: VideoQual
|
|
|
124
110
|
export function sameLayout(a: readonly VideoEncoding[], b: readonly VideoEncoding[]): boolean {
|
|
125
111
|
return a.length === b.length && a.every((encoding, i) => encoding.scaleResolutionDownBy === b[i].scaleResolutionDownBy);
|
|
126
112
|
}
|
|
113
|
+
|
|
114
|
+
/** Actual producer layer dimensions, smallest first; includes portrait and screen video. */
|
|
115
|
+
export interface VideoLayer {
|
|
116
|
+
spatialLayer: number;
|
|
117
|
+
width: number;
|
|
118
|
+
height: number;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export type ReceiveQuality = 'auto' | VideoQuality;
|
|
122
|
+
export type ScreenMode = 'detail' | 'motion';
|
|
123
|
+
|
|
124
|
+
/** Never invent layers when an older producer does not describe them. */
|
|
125
|
+
export function receiveSpatialLayer(layers: readonly VideoLayer[], quality: VideoQuality): number {
|
|
126
|
+
const valid = layers.filter((l) => Number.isInteger(l.spatialLayer) && l.spatialLayer >= 0 &&
|
|
127
|
+
Number.isFinite(l.width) && l.width > 0 && Number.isFinite(l.height) && l.height > 0)
|
|
128
|
+
.sort((a, b) => a.width * a.height - b.width * b.height);
|
|
129
|
+
if (!valid.length) throw new Error('producer has no video layer metadata');
|
|
130
|
+
const tier = TIER_SIZE[quality];
|
|
131
|
+
const fitting = valid.filter((l) => Math.max(l.width, l.height) <= tier.width + 1 && Math.min(l.width, l.height) <= tier.height + 1);
|
|
132
|
+
return (fitting.at(-1) ?? valid[0]).spatialLayer;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function describeVideoLayers(width: number, height: number, encodings: readonly VideoEncoding[], quality: VideoQuality = '1080p'): VideoLayer[] {
|
|
136
|
+
const fallback = TIER_SIZE[quality];
|
|
137
|
+
const w = width > 0 ? width : fallback.width;
|
|
138
|
+
const h = height > 0 ? height : fallback.height;
|
|
139
|
+
return encodings.map((encoding, spatialLayer) => ({ spatialLayer, width: Math.round(w / encoding.scaleResolutionDownBy), height: Math.round(h / encoding.scaleResolutionDownBy) }));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Screen frames are capped to native 1080p, never enlarged, with two adaptive copies. */
|
|
143
|
+
export function screenEncodings(width: number, height: number, mode: ScreenMode = 'detail'): VideoEncoding[] {
|
|
144
|
+
const nativeWidth = width > 0 ? width : 1920;
|
|
145
|
+
const nativeHeight = height > 0 ? height : 1080;
|
|
146
|
+
const scale = Math.max(1, Math.max(nativeWidth, nativeHeight) / 1920, Math.min(nativeWidth, nativeHeight) / 1080);
|
|
147
|
+
const w = nativeWidth / scale;
|
|
148
|
+
const h = nativeHeight / scale;
|
|
149
|
+
const layer = (divisor: number, rid?: string): VideoEncoding => ({
|
|
150
|
+
...(rid ? { rid } : {}), scaleResolutionDownBy: scale * divisor,
|
|
151
|
+
maxBitrate: divisor === 1 ? (mode === 'motion' ? 6_000_000 : 2_500_000) : 600_000,
|
|
152
|
+
maxFramerate: mode === 'motion' ? 30 : 15, scalabilityMode: 'L1T3',
|
|
153
|
+
});
|
|
154
|
+
return w * h >= 480 * 270 ? [layer(2, 'h'), layer(1, 'f')] : [layer(1)];
|
|
155
|
+
}
|