mediascale-sdk 1.0.3 → 1.0.4

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.
@@ -2,7 +2,7 @@
2
2
 
3
3
  One address talks to Mediascale. The default is `http://localhost:8080`.
4
4
 
5
- A person joins a session, turns on camera and microphone, hears the others, and leaves. If `recording` is true and that person is the host, recording starts by itself. Anyone else can call `startRecording()` if the host leaves and the recording should continue.
5
+ A person joins a session, turns on camera and microphone, hears the others, and leaves. Recording is **off by default**. Pass `recording: true` only when the host should start armed on `connect()`, or call `startRecording()` / `stopRecording()` any time during the session. Each start/stop opens a new segment; `end()` combines every segment into one file.
6
6
 
7
7
  The secret that signs tokens stays on your server. The browser only receives a short-lived token.
8
8
 
@@ -51,7 +51,7 @@ export async function POST(request) {
51
51
  }
52
52
  ```
53
53
 
54
- `role` is `host` or `audience`. Both can publish. Only the host records automatically.
54
+ `role` is `host` or `audience`. Both can publish. Only the host should drive recording in the UI.
55
55
 
56
56
  ## 2. Copy the room page
57
57
 
@@ -61,10 +61,10 @@ Copy `sdk/react-sdk/sample/app/room.js` into your app. The comments in that file
61
61
  2. **Camera** calls `startCamera()` or `stopCamera()`.
62
62
  3. **Mic** calls `startMicrophone()` or `stopMicrophone()`.
63
63
  4. **Screen** calls `startScreenShare()` or `stopScreenShare()`.
64
- 5. **Start recording** is optional. Use it when the host has left and someone else should continue.
65
- 6. **End** leaves the session.
64
+ 5. **Record** (host) calls `startRecording()` or `stopRecording()` from the toolbar button. You can toggle any time; the final file includes every segment.
65
+ 6. **End** leaves the session and saves the recording if this person was recording.
66
66
 
67
- Host is unchecked. Tick it only for the person who should record. When it is ticked, `recording` is true and recording starts on `connect()`.
67
+ `recording` defaults to `false`. Set `recording: true` only when the host should already be armed when they join (the sample Record button then starts in the on state). Chunks still wait until camera, mic, or screen is live.
68
68
 
69
69
  The room must run in the browser. `app/page.js`
70
70
 
@@ -104,12 +104,23 @@ await client.startMicrophone({ micId: "device-id" });
104
104
 
105
105
  `resolution` can be `"480p"`, `"720p"`, `"1080p"`, or `{ width: 1280, height: 720 }`.
106
106
 
107
+ The SFU origin caps each publisher’s uplink at **2500 Kbps** (`MAX_PUBLISH_BITRATE_BPS`) via `setMaxIncomingBitrate` on the send transport.
108
+
109
+ Toggle recording during the session:
110
+
111
+ ```js
112
+ await client.startRecording();
113
+ await client.stopRecording(); // pauses; does not save yet
114
+ await client.startRecording(); // new segment, same recording id
115
+ await client.end(); // combines every segment into one file
116
+ ```
117
+
107
118
  ## Functions
108
119
 
109
120
  | Function | What it does |
110
121
  | --- | --- |
111
- | `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. |
112
- | `connect()` | Checks the token and joins the session. If `recording` is true and the token role is `host`, recording starts. |
122
+ | `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**. |
123
+ | `connect()` | Checks the token and joins the session. If `recording` is true and the token role is `host`, recording is armed (same as calling `startRecording()`). |
113
124
  | `subscribe()` | Starts playing the other people. Call this once after `connect()`. |
114
125
  | `startCamera({ cameraId, resolution })` | Publishes the camera. Both arguments are optional. |
115
126
  | `stopCamera()` | Stops the camera. The person stays in the session. |
@@ -119,8 +130,9 @@ await client.startMicrophone({ micId: "device-id" });
119
130
  | `stopScreenShare()` | Stops the screen share. |
120
131
  | `publish({ cameraId, micId, resolution })` | Turns on the camera and the microphone together. |
121
132
  | `stop()` | Turns off the camera and the microphone. The screen share stays on. |
122
- | `startRecording()` | Starts recording for this person. Call it when the host has left and someone else should keep the same recording going. |
123
- | `end()` | Leaves the session. If this person is the one recording, the recording is saved. |
133
+ | `startRecording()` | Arms recording for this person (or resumes after `stopRecording()`). Chunks upload only while webcam, mic, or screen is live. Also used when the host left and a cohost should continue the same file. |
134
+ | `stopRecording()` | Pauses recording without saving. Call `startRecording()` again to continue; `end()` still combines all segments into one file. |
135
+ | `end()` | Leaves the session. If this person is the one recording, every segment is combined and saved. |
124
136
  | `listDevices()` | Returns `{ cameras, microphones }`. Each item is `{ id, label }`. |
125
137
 
126
138
  Call `connect`, then `subscribe`. Call `startCamera`, `startMicrophone`, or `startScreenShare` only when that button is pressed. Call the matching stop function to turn that input off. Call `end` when the person leaves.
@@ -135,13 +147,49 @@ Pass these to `createClient`.
135
147
  | `onTrackEnded({ producerId })` | That media stopped. Remove the element. |
136
148
  | `onStatus(text)` | `Joining`, `Ready`, `Live`, `Network unstable`, `Ended`, or an error message. |
137
149
  | `onError(error)` | A call failed. `error.message` is the reason. The same call also throws, so `await` can use `try/catch`. |
138
- | `onRecording(state)` | `recording` when this browser is recording. `stopped` when it stops or hands the recording to someone else. |
150
+ | `onRecording(state)` | `armed` when recording is on but waiting for live media. `recording` while chunks are uploading. `stopped` after `stopRecording()`, handoff, or `end()`. |
151
+ | `onActiveSpeaker({ peerId, displayName, producerId, local })` | Fires when the loudest mic in the room changes. `peerId` is `null` when nobody is speaking. `local` is true when it is this browser. Requires `ACTIVE_SPEAKER_DETECTION=true` on the origin SFU and portal. |
152
+
153
+ ## Active speaker
154
+
155
+ Enable on the media stack (origin + portal):
156
+
157
+ ```bash
158
+ ACTIVE_SPEAKER_DETECTION=true
159
+ ```
160
+
161
+ Optional tuning on the origin SFU:
162
+
163
+ ```bash
164
+ ACTIVE_SPEAKER_THRESHOLD=-45
165
+ ACTIVE_SPEAKER_INTERVAL_MS=500
166
+ ```
167
+
168
+ When the flag is on, the origin watches mic producers with mediasoup `AudioLevelObserver` and the portal emits `activeSpeaker` to every peer in the session. The SDK surfaces that as `onActiveSpeaker`.
169
+
170
+ ```js
171
+ const client = createClient({
172
+ endpoint,
173
+ sessionId,
174
+ token,
175
+ onActiveSpeaker: ({ peerId, displayName, local }) => {
176
+ // peerId null → clear the speaking highlight
177
+ // local true → this person is the active speaker
178
+ highlightSpeaker({ peerId, displayName, local });
179
+ },
180
+ });
181
+ ```
182
+
183
+ The sample room highlights the speaking camera tile (and strip card) with a cyan border. Screen-share tiles are not marked as speaking.
139
184
 
140
185
  ## Recording
141
186
 
142
- - Set `recording: true` only for the host. `connect()` then records without an extra call. The red `[ rec ]` label shows while that recording is active. If recording does not start, the label stays hidden and the person can still publish.
143
- - The file keeps going when another person calls `startRecording()`. Ask them to start before the host calls `end()`.
144
- - `end()` saves the file only for the person who is recording at that moment.
187
+ - `recording` defaults to **false**. No recording starts unless you pass `recording: true` or call `startRecording()`.
188
+ - The sample host toolbar has a Record button. If the host joined with `recording: true`, that button starts in the on state. The host can turn it off and on any time.
189
+ - `stopRecording()` only pauses. It does not upload a finished file. `startRecording()` afterward opens a new segment under the same recording id.
190
+ - `end()` (or idle finalize when everybody has left) combines every segment into **one** playable file.
191
+ - Chunks upload only while webcam, mic, or screen share is live. The red `REC` pill means chunks are flowing; an armed-but-idle session keeps the Record button on without the pill.
192
+ - The file keeps going when another person calls `startRecording()` after a handoff. Ask them to start before the previous recorder calls `end()`.
145
193
  - If everybody leaves, the recording is still saved after a few minutes of silence.
146
194
 
147
195
  ## Run the sample
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mediascale-sdk",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
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
@@ -62,6 +62,7 @@ export function createClient(options = {}) {
62
62
  const onStatus = options.onStatus || (() => {});
63
63
  const onError = options.onError || (() => {});
64
64
  const onRecording = options.onRecording || (() => {});
65
+ const onActiveSpeaker = options.onActiveSpeaker || (() => {});
65
66
 
66
67
  if (!sessionId) throw new Error("sessionId is required");
67
68
  if (!token) throw new Error("token is required");
@@ -78,6 +79,7 @@ export function createClient(options = {}) {
78
79
  let subscribed = false;
79
80
  let recorder = null;
80
81
  let startingRecorder = null;
82
+ let selfPeerId = null;
81
83
  const locals = new Map();
82
84
  const tiles = new Map();
83
85
  const pending = [];
@@ -229,6 +231,16 @@ export function createClient(options = {}) {
229
231
  .then(() => onRecording("stopped"))
230
232
  .catch((error) => onError(error));
231
233
  });
234
+ socket.on("activeSpeaker", (payload = {}) => {
235
+ if (closed) return;
236
+ const peerId = payload.peerId || null;
237
+ onActiveSpeaker({
238
+ peerId,
239
+ displayName: payload.displayName || null,
240
+ producerId: payload.producerId || null,
241
+ local: Boolean(peerId && selfPeerId && peerId === selfPeerId),
242
+ });
243
+ });
232
244
  socket.on("connect", async () => {
233
245
  try {
234
246
  if (closed) return;
@@ -239,6 +251,7 @@ export function createClient(options = {}) {
239
251
  displayName,
240
252
  ...clientHints(),
241
253
  });
254
+ selfPeerId = joined.peerId || null;
242
255
  iceServers = iceOverride || joined.iceServers || [];
243
256
  device = new Device();
244
257
  await device.load({ routerRtpCapabilities: joined.rtpCapabilities });
@@ -403,10 +416,24 @@ export function createClient(options = {}) {
403
416
  await flush();
404
417
  }
405
418
 
419
+ function notifyRecording(next) {
420
+ if (!next?.wanted) {
421
+ onRecording("stopped");
422
+ return;
423
+ }
424
+ // "recording" while chunks flow; "armed" while enabled but waiting for live A/V/screen.
425
+ onRecording(next.capturing ? "recording" : "armed");
426
+ }
427
+
406
428
  async function startRecording() {
407
429
  if (closed) throw new Error("The session has ended");
408
430
  if (!sendTransport) await connect();
409
- if (closed || recorder) return;
431
+ if (closed) return;
432
+ if (recorder) {
433
+ await recorder.setWanted(true);
434
+ notifyRecording(recorder);
435
+ return;
436
+ }
410
437
  if (startingRecorder) return startingRecorder;
411
438
  startingRecorder = (async () => {
412
439
  await emit(socket, "claimRecording", {});
@@ -418,9 +445,10 @@ export function createClient(options = {}) {
418
445
  endpoint,
419
446
  token,
420
447
  // Chunks only flow while webcam / mic / screenshare is live.
421
- onCaptureChange: (active) => onRecording(active ? "recording" : "stopped"),
448
+ onCaptureChange: () => notifyRecording(next),
422
449
  });
423
450
  recorder = next;
451
+ next.wanted = true;
424
452
  next.sync(tracksFromTiles([...tiles.values()]));
425
453
  try {
426
454
  await next.start();
@@ -430,8 +458,7 @@ export function createClient(options = {}) {
430
458
  return;
431
459
  }
432
460
  if (recorder !== next) return;
433
- // Keep "stopped" until the first live track; sync() will arm capture then.
434
- if (!tracksFromTiles([...tiles.values()]).length) onRecording("stopped");
461
+ notifyRecording(next);
435
462
  })();
436
463
  try {
437
464
  await startingRecorder;
@@ -440,6 +467,12 @@ export function createClient(options = {}) {
440
467
  }
441
468
  }
442
469
 
470
+ async function stopRecording() {
471
+ if (closed || !recorder) return;
472
+ await recorder.setWanted(false);
473
+ onRecording("stopped");
474
+ }
475
+
443
476
  async function end() {
444
477
  if (closed) return;
445
478
  closed = true;
@@ -501,6 +534,7 @@ export function createClient(options = {}) {
501
534
  stopScreenShare: guard(stopScreenShare),
502
535
  subscribe: guard(subscribe),
503
536
  startRecording: guard(startRecording),
537
+ stopRecording: guard(stopRecording),
504
538
  end: guard(end),
505
539
  listDevices: guard(listDevices),
506
540
  };
package/src/record.js CHANGED
@@ -104,6 +104,8 @@ export class CanvasRecorder {
104
104
  this.finishing = false;
105
105
  this.prepared = false;
106
106
  this.capturing = false;
107
+ /** Host/app wants chunks; false pauses without finalizing so start can open a new segment. */
108
+ this.wanted = true;
107
109
  this.captureGate = Promise.resolve();
108
110
  this.untilStop = new Promise((resolve) => {
109
111
  this.release = resolve;
@@ -164,16 +166,23 @@ export class CanvasRecorder {
164
166
  const run = this.captureGate.then(async () => {
165
167
  if (this.finishing || !this.prepared) return;
166
168
  const live = hasActiveMedia(this.tracks);
167
- if (live && !this.capturing) await this.beginCapture();
168
- else if (!live && this.capturing) await this.pauseCapture();
169
+ if (this.wanted && live && !this.capturing) await this.beginCapture();
170
+ else if ((!this.wanted || !live) && this.capturing) await this.pauseCapture();
169
171
  });
170
172
  this.captureGate = run.catch(() => undefined);
171
173
  return run;
172
174
  }
173
175
 
176
+ /** Pause or resume capture without finalizing. New segments are combined on finish(). */
177
+ async setWanted(wanted) {
178
+ this.wanted = Boolean(wanted);
179
+ await this.updateCapture();
180
+ }
181
+
174
182
  async finish(options = {}) {
175
183
  if (this.finishing) return;
176
184
  this.finishing = true;
185
+ this.wanted = false;
177
186
  await this.captureGate.catch(() => undefined);
178
187
  const didCapture = Boolean(this.startedAt);
179
188
  if (this.capturing) {