mediascale-sdk 1.0.8 → 1.0.9

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.
@@ -180,7 +180,27 @@ await client.replaceMicrophoneTrack(nextMicTrack);
180
180
 
181
181
  `replaceCameraTrack` / `replaceMicrophoneTrack` swap media on the **existing producer** (no unpublish). Use these for smooth device switching while live.
182
182
 
183
- The SFU origin caps each publisher’s uplink at **2500 Kbps** (`MAX_PUBLISH_BITRATE_BPS`) via `setMaxIncomingBitrate` on the send transport.
183
+ ### Publish bitrate caps (camera / screen)
184
+
185
+ The SDK limits **outgoing** video encode bitrate via WebRTC `encodings.maxBitrate`:
186
+
187
+ | Stream | Default | Env (portal) |
188
+ | --- | --- | --- |
189
+ | Camera (`webcam`) | **1 Mbps** | `CAMERA_MAX_BITRATE_BPS` |
190
+ | Screen share, whiteboard, PDF, other custom video | **1.5 Mbps** | `SCREEN_MAX_BITRATE_BPS` |
191
+
192
+ Flow: portal reads those env vars → includes `publishLimits: { cameraBps, screenBps }` on the `join` ack → SDK applies them on `produce`. Set a value to `0` to disable that SDK-side cap. Optional override on the client:
193
+
194
+ ```js
195
+ createClient({
196
+ // …
197
+ publishLimits: { cameraBps: 1_000_000, screenBps: 1_500_000 },
198
+ });
199
+ ```
200
+
201
+ `createClient({ publishLimits })` wins over the portal values. Mic / screen-audio are unchanged.
202
+
203
+ Separately, the SFU origin still caps each publisher’s uplink at **2500 Kbps** (`MAX_PUBLISH_BITRATE_BPS`) via `setMaxIncomingBitrate` on the send transport.
184
204
 
185
205
  Toggle recording during the session:
186
206
 
@@ -195,7 +215,7 @@ await client.end(); // combines every segment into one file
195
215
 
196
216
  | Function | What it does |
197
217
  | --- | --- |
198
- | `createClient({ endpoint, sessionId, token, recording, iceServers, ...callbacks })` | Prepares the client. `sessionId` and `token` are required. `endpoint` defaults to `http://localhost:8080`. `recording` defaults to **false**. If you omit `recording` (or pass anything other than `false`) and the host token has `recording: true`, auto-start is still enabled. Pass `recording: false` to force off. |
218
+ | `createClient({ endpoint, sessionId, token, recording, iceServers, publishLimits, ...callbacks })` | Prepares the client. `sessionId` and `token` are required. `endpoint` defaults to `http://localhost:8080`. `recording` defaults to **false**. Optional `publishLimits: { cameraBps, screenBps }` overrides portal join caps. |
199
219
  | `connect()` | Checks the token and joins the session. If recording is enabled for a **host**, recording starts immediately in the background (same as `startRecording()`). Join does not wait for capture to finish preparing. |
200
220
  | `subscribe()` | Starts playing the other people. Call this once after `connect()`. |
201
221
  | `startCamera({ cameraId, resolution, track })` | Publishes the camera. Pass `track` to use your own live video track (SDK clones it). |
@@ -215,6 +235,7 @@ await client.end(); // combines every segment into one file
215
235
  | `setRecordingCrop(source, crop)` | Crops a video source in the recording layout. `crop` is `{ x, y, w, h }` in 0..1 (same as subscriber whiteboard crop). Pass `null` to clear. |
216
236
  | `setRecordingStage(people)` | **Required for stage-accurate recording.** Who appears on the recording stage: `[{ id, name, local? }]`. Avatars until that peer’s webcam arrives. See [Recording must match the live stage](#recording-must-match-the-live-stage). |
217
237
  | `getPeerId()` | Socket peer id for this browser after `connect()`. |
238
+ | `getPublishLimits()` | Current `{ cameraBps, screenBps }` used for outgoing video encode caps. |
218
239
  | `getUserList()` / `updateSelf(...)` / `stageAction(...)` | Optional portal roster helpers used by the sample meet UI. Skip these if your app already manages users and stage on its own. |
219
240
  | `end()` | Leaves the session. If this person is the one recording, every segment is combined and saved. |
220
241
  | `listDevices()` | Returns `{ cameras, microphones }`. Each item is `{ id, label }`. |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mediascale-sdk",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "Mediascale React/browser SDK for live sessions, WebRTC publish/subscribe, and recording",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
package/src/client.js CHANGED
@@ -11,6 +11,24 @@ const SIZES = {
11
11
  /** Built-in sources that have dedicated start/stop helpers. */
12
12
  const RESERVED_SOURCES = new Set(["webcam", "mic", "screen", "screen-audio"]);
13
13
 
14
+ /** Default SDK publish caps (bps). Portal join / createClient can override. */
15
+ const DEFAULT_CAMERA_MAX_BITRATE_BPS = 1_000_000;
16
+ const DEFAULT_SCREEN_MAX_BITRATE_BPS = 1_500_000;
17
+
18
+ function parseBitrateBps(raw, fallback) {
19
+ if (raw == null || raw === "") return fallback;
20
+ const n = Number(raw);
21
+ if (!Number.isFinite(n)) return fallback;
22
+ return Math.max(0, Math.floor(n));
23
+ }
24
+
25
+ function normalizePublishLimits(raw = {}) {
26
+ return {
27
+ cameraBps: parseBitrateBps(raw.cameraBps ?? raw.camera, DEFAULT_CAMERA_MAX_BITRATE_BPS),
28
+ screenBps: parseBitrateBps(raw.screenBps ?? raw.screen, DEFAULT_SCREEN_MAX_BITRATE_BPS),
29
+ };
30
+ }
31
+
14
32
  function readToken(token) {
15
33
  const body = String(token || "").split(".")[0];
16
34
  if (!body) throw new Error("token is required");
@@ -81,6 +99,11 @@ export function createClient(options = {}) {
81
99
  || (options.recording !== false && claims.recording === true && claims.role === "host");
82
100
 
83
101
  const displayName = String(claims.name || "Guest").slice(0, 40);
102
+ // createClient({ publishLimits }) wins over portal join values when set.
103
+ const publishLimitsOverride = options.publishLimits != null
104
+ ? normalizePublishLimits(options.publishLimits)
105
+ : null;
106
+ let publishLimits = publishLimitsOverride || normalizePublishLimits({});
84
107
  let socket = null;
85
108
  let device = null;
86
109
  let sendTransport = null;
@@ -311,11 +334,36 @@ export function createClient(options = {}) {
311
334
  if (socket?.connected) await emit(socket, "closeProducer", { producerId }).catch(() => {});
312
335
  }
313
336
 
337
+ function maxBitrateFor(source, kind) {
338
+ if (kind !== "video") return 0;
339
+ if (source === "webcam") return publishLimits.cameraBps;
340
+ // screen + custom video (whiteboard, pdf, …)
341
+ return publishLimits.screenBps;
342
+ }
343
+
344
+ function applyPublishLimitsFromJoin(raw) {
345
+ if (publishLimitsOverride) return;
346
+ if (!raw || typeof raw !== "object") return;
347
+ publishLimits = normalizePublishLimits({
348
+ cameraBps: raw.cameraBps ?? raw.camera,
349
+ screenBps: raw.screenBps ?? raw.screen,
350
+ });
351
+ }
352
+
314
353
  // Camera inputs use an SDK-owned clone. Custom inputs keep upstream direct ownership.
315
354
  async function produce(source, track, original = track) {
316
355
  let producer;
317
356
  try {
318
- producer = await sendTransport.produce({ track, stopTracks: false, appData: { source } });
357
+ const options = { track, stopTracks: false, appData: { source } };
358
+ const maxBitrate = maxBitrateFor(source, track.kind);
359
+ if (maxBitrate > 0) {
360
+ options.encodings = [{ maxBitrate }];
361
+ // Chrome start bitrate is kbps; keep a modest ramp under the cap.
362
+ options.codecOptions = {
363
+ videoGoogleStartBitrate: Math.max(100, Math.min(1000, Math.round(maxBitrate / 2000))),
364
+ };
365
+ }
366
+ producer = await sendTransport.produce(options);
319
367
  if (closed) throw new Error("The session has ended");
320
368
  if (track.readyState === "ended" || original.readyState === "ended") throw new Error("The source track ended");
321
369
  } catch (error) {
@@ -436,6 +484,7 @@ export function createClient(options = {}) {
436
484
  });
437
485
  selfPeerId = joined.peerId || null;
438
486
  iceServers = iceOverride || joined.iceServers || [];
487
+ applyPublishLimitsFromJoin(joined.publishLimits);
439
488
  if (Array.isArray(joined.users)) {
440
489
  ingestRosterUsers(joined.users);
441
490
  onUserList(joined.users);
@@ -868,6 +917,7 @@ export function createClient(options = {}) {
868
917
  updateSelf: guard(updateSelf),
869
918
  stageAction: guard(stageAction),
870
919
  getPeerId: () => selfPeerId,
920
+ getPublishLimits: () => ({ ...publishLimits }),
871
921
  end: guard(end),
872
922
  listDevices: guard(listDevices),
873
923
  };