mediascale-sdk 1.0.4 → 1.0.5
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/docs/integration.md +51 -4
- package/package.json +1 -1
- package/src/client.js +47 -0
- package/src/record.js +14 -7
package/docs/integration.md
CHANGED
|
@@ -61,8 +61,9 @@ 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. **
|
|
65
|
-
6. **
|
|
64
|
+
5. **Custom stream** (optional) — see [Custom streams](#custom-streams) (`startCustomStream` / `stopCustomStream`).
|
|
65
|
+
6. **Record** (host) calls `startRecording()` or `stopRecording()` from the toolbar button. You can toggle any time; the final file includes every segment.
|
|
66
|
+
7. **End** leaves the session and saves the recording if this person was recording.
|
|
66
67
|
|
|
67
68
|
`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
69
|
|
|
@@ -128,6 +129,8 @@ await client.end(); // combines every segment into one file
|
|
|
128
129
|
| `stopMicrophone()` | Stops the microphone. |
|
|
129
130
|
| `startScreenShare()` | Shares the screen. Anyone in the session can call this. |
|
|
130
131
|
| `stopScreenShare()` | Stops the screen share. |
|
|
132
|
+
| `startCustomStream(identifier, track, type)` | Publishes a custom `MediaStreamTrack`. `type` is `"audio"` or `"video"`. Reserved ids: `webcam`, `mic`, `screen`. |
|
|
133
|
+
| `stopCustomStream(identifier)` | Stops that custom stream. |
|
|
131
134
|
| `publish({ cameraId, micId, resolution })` | Turns on the camera and the microphone together. |
|
|
132
135
|
| `stop()` | Turns off the camera and the microphone. The screen share stays on. |
|
|
133
136
|
| `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. |
|
|
@@ -135,7 +138,7 @@ await client.end(); // combines every segment into one file
|
|
|
135
138
|
| `end()` | Leaves the session. If this person is the one recording, every segment is combined and saved. |
|
|
136
139
|
| `listDevices()` | Returns `{ cameras, microphones }`. Each item is `{ id, label }`. |
|
|
137
140
|
|
|
138
|
-
Call `connect`, then `subscribe`. Call `startCamera`, `startMicrophone`, or `
|
|
141
|
+
Call `connect`, then `subscribe`. Call `startCamera`, `startMicrophone`, `startScreenShare`, or `startCustomStream` only when that button is pressed. Call the matching stop function to turn that input off. Call `end` when the person leaves.
|
|
139
142
|
|
|
140
143
|
## Callbacks
|
|
141
144
|
|
|
@@ -143,7 +146,7 @@ Pass these to `createClient`.
|
|
|
143
146
|
|
|
144
147
|
| Callback | When it runs |
|
|
145
148
|
| --- | --- |
|
|
146
|
-
| `onTrack({ producerId, peerId, source, kind, displayName, stream })` |
|
|
149
|
+
| `onTrack({ producerId, peerId, source, kind, displayName, stream })` | Remote media ready. `source` is `webcam`, `mic`, `screen`, or a custom id (e.g. `whiteboard`). |
|
|
147
150
|
| `onTrackEnded({ producerId })` | That media stopped. Remove the element. |
|
|
148
151
|
| `onStatus(text)` | `Joining`, `Ready`, `Live`, `Network unstable`, `Ended`, or an error message. |
|
|
149
152
|
| `onError(error)` | A call failed. `error.message` is the reason. The same call also throws, so `await` can use `try/catch`. |
|
|
@@ -182,6 +185,50 @@ const client = createClient({
|
|
|
182
185
|
|
|
183
186
|
The sample room highlights the speaking camera tile (and strip card) with a cyan border. Screen-share tiles are not marked as speaking.
|
|
184
187
|
|
|
188
|
+
## Custom streams
|
|
189
|
+
|
|
190
|
+
Publish any `MediaStreamTrack` under your own id. Subscribers get that same id as `onTrack.source`.
|
|
191
|
+
|
|
192
|
+
Reserved ids (use the dedicated APIs instead): `webcam`, `mic`, `screen`.
|
|
193
|
+
|
|
194
|
+
### Whiteboard example
|
|
195
|
+
|
|
196
|
+
**Publish** — show your whiteboard UI, capture this tab, send the video track:
|
|
197
|
+
|
|
198
|
+
```js
|
|
199
|
+
const capture = await navigator.mediaDevices.getDisplayMedia({
|
|
200
|
+
video: true,
|
|
201
|
+
audio: false,
|
|
202
|
+
preferCurrentTab: true,
|
|
203
|
+
selfBrowserSurface: "include",
|
|
204
|
+
});
|
|
205
|
+
const track = capture.getVideoTracks()[0];
|
|
206
|
+
await client.startCustomStream("whiteboard", track, "video");
|
|
207
|
+
|
|
208
|
+
// later
|
|
209
|
+
await client.stopCustomStream("whiteboard");
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
**Receive** — treat `source === "whiteboard"` like a screen-share tile:
|
|
213
|
+
|
|
214
|
+
```js
|
|
215
|
+
const client = createClient({
|
|
216
|
+
endpoint,
|
|
217
|
+
sessionId,
|
|
218
|
+
token,
|
|
219
|
+
onTrack: ({ source, kind, stream }) => {
|
|
220
|
+
if (source === "whiteboard" && kind === "video") {
|
|
221
|
+
whiteboardVideo.srcObject = stream;
|
|
222
|
+
}
|
|
223
|
+
},
|
|
224
|
+
onTrackEnded: ({ producerId }) => {
|
|
225
|
+
// remove that video element
|
|
226
|
+
},
|
|
227
|
+
});
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
Optional: broadcast the publisher’s whiteboard DOM bounds (normalized `x,y,w,h` vs the viewport) over your own channel so viewers can crop the full-tab capture to only the whiteboard area. See the sample app for that pattern.
|
|
231
|
+
|
|
185
232
|
## Recording
|
|
186
233
|
|
|
187
234
|
- `recording` defaults to **false**. No recording starts unless you pass `recording: true` or call `startRecording()`.
|
package/package.json
CHANGED
package/src/client.js
CHANGED
|
@@ -8,6 +8,9 @@ const SIZES = {
|
|
|
8
8
|
"1080p": { width: 1920, height: 1080 },
|
|
9
9
|
};
|
|
10
10
|
|
|
11
|
+
/** Built-in sources that have dedicated start/stop helpers. */
|
|
12
|
+
const RESERVED_SOURCES = new Set(["webcam", "mic", "screen", "screen-audio"]);
|
|
13
|
+
|
|
11
14
|
function readToken(token) {
|
|
12
15
|
const body = String(token || "").split(".")[0];
|
|
13
16
|
if (!body) throw new Error("token is required");
|
|
@@ -410,6 +413,48 @@ export function createClient(options = {}) {
|
|
|
410
413
|
}
|
|
411
414
|
}
|
|
412
415
|
|
|
416
|
+
/**
|
|
417
|
+
* Publish any caller-owned MediaStreamTrack under a custom stream identifier.
|
|
418
|
+
* Subscribers receive it via onTrack with `source` set to that identifier and
|
|
419
|
+
* `kind` set to audio or video.
|
|
420
|
+
*
|
|
421
|
+
* @param {string} identifier e.g. "whiteboard"
|
|
422
|
+
* @param {MediaStreamTrack} track
|
|
423
|
+
* @param {"audio"|"video"} type must match track.kind
|
|
424
|
+
*/
|
|
425
|
+
async function startCustomStream(identifier, track, type) {
|
|
426
|
+
await connect();
|
|
427
|
+
if (!sendTransport) throw new Error("The session is not ready");
|
|
428
|
+
const source = String(identifier || "").trim();
|
|
429
|
+
if (!source) throw new Error("stream identifier is required");
|
|
430
|
+
if (RESERVED_SOURCES.has(source)) {
|
|
431
|
+
throw new Error(`"${source}" is reserved; use the dedicated camera, mic, or screen APIs`);
|
|
432
|
+
}
|
|
433
|
+
if (!track || typeof track !== "object" || typeof track.kind !== "string") {
|
|
434
|
+
throw new Error("track is required");
|
|
435
|
+
}
|
|
436
|
+
const kind = String(type || track.kind || "").toLowerCase();
|
|
437
|
+
if (kind !== "audio" && kind !== "video") throw new Error("type must be audio or video");
|
|
438
|
+
if (track.kind !== kind) throw new Error(`track kind is ${track.kind}, expected ${kind}`);
|
|
439
|
+
|
|
440
|
+
// Replace an existing publish of the same identifier + kind.
|
|
441
|
+
for (const [id, entry] of [...locals]) {
|
|
442
|
+
if (entry.source === source && entry.track.kind === kind) await unpublish(id);
|
|
443
|
+
}
|
|
444
|
+
return produce(source, track);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
async function stopCustomStream(identifier) {
|
|
448
|
+
const source = String(identifier || "").trim();
|
|
449
|
+
if (!source) return;
|
|
450
|
+
if (RESERVED_SOURCES.has(source)) {
|
|
451
|
+
throw new Error(`"${source}" is reserved; use the dedicated stop API`);
|
|
452
|
+
}
|
|
453
|
+
for (const [id, entry] of [...locals]) {
|
|
454
|
+
if (entry.source === source) await unpublish(id);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
413
458
|
async function subscribe() {
|
|
414
459
|
await connect();
|
|
415
460
|
subscribed = true;
|
|
@@ -532,6 +577,8 @@ export function createClient(options = {}) {
|
|
|
532
577
|
stopMicrophone: guard(stopMicrophone),
|
|
533
578
|
startScreenShare: guard(startScreenShare),
|
|
534
579
|
stopScreenShare: guard(stopScreenShare),
|
|
580
|
+
startCustomStream: guard(startCustomStream),
|
|
581
|
+
stopCustomStream: guard(stopCustomStream),
|
|
535
582
|
subscribe: guard(subscribe),
|
|
536
583
|
startRecording: guard(startRecording),
|
|
537
584
|
stopRecording: guard(stopRecording),
|
package/src/record.js
CHANGED
|
@@ -66,15 +66,21 @@ export function tracksFromTiles(tiles) {
|
|
|
66
66
|
return out;
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
/**
|
|
69
|
+
/** Screen share and custom video (e.g. whiteboard) use the large featured slot. */
|
|
70
|
+
function isFeaturedVideoSource(source) {
|
|
71
|
+
return source === "screen" || (source && source !== "webcam");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** True when at least one live webcam / mic / screenshare / custom track is present. */
|
|
70
75
|
export function hasActiveMedia(tracks) {
|
|
71
76
|
return (tracks || []).some((item) => {
|
|
72
77
|
const track = item?.track;
|
|
73
78
|
if (!track || track.readyState === "ended" || track.enabled === false) return false;
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
79
|
+
if (item.source === "webcam" || item.source === "mic" || item.source === "screen" || item.source === "screen-audio") {
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
// Custom streams (whiteboard, etc.) also keep recording alive.
|
|
83
|
+
return Boolean(item.source);
|
|
78
84
|
});
|
|
79
85
|
}
|
|
80
86
|
|
|
@@ -378,9 +384,10 @@ export class CanvasRecorder {
|
|
|
378
384
|
continue;
|
|
379
385
|
}
|
|
380
386
|
if (item.kind !== "video") continue;
|
|
381
|
-
if (item.source !== "webcam" && item.source !== "screen") continue;
|
|
382
387
|
if (disabled) continue;
|
|
383
|
-
const
|
|
388
|
+
const featured = isFeaturedVideoSource(item.source);
|
|
389
|
+
if (!featured && item.source !== "webcam") continue;
|
|
390
|
+
const kind = featured ? "screen" : "camera";
|
|
384
391
|
const key = `${item.participantKey}|${kind}`;
|
|
385
392
|
const slot = {
|
|
386
393
|
key,
|