Skip to content

Commit 807b5f0

Browse files
committed
Remove ready() from end user and use it internally
1 parent 05def1d commit 807b5f0

File tree

5 files changed

+39
-18
lines changed

5 files changed

+39
-18
lines changed

packages/webrtc-sdk/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,12 @@ Open `docs/index.html` in a browser.
140140
- `MediaManager`: handles local media (getUserMedia, device switching, screen share).
141141

142142
For most applications, call methods on `WebRTCClient` only. It exposes the common
143-
operations you need: `ready()`, `join()`, `publish()`, `play()`, `stop()`, `listDevices()`,
143+
operations you need: `join()`, `publish()`, `play()`, `stop()`, `listDevices()`,
144144
`selectVideoInput()`, `selectAudioInput()`, `startScreenShare()`, `stopScreenShare()`,
145145
`sendData()`, `enableStats()/disableStats()`, room/multitrack helpers, and emits typed events.
146146

147+
Note: Methods internally wait for signaling readiness; you don't need to call `ready()` yourself.
148+
147149
Only use `WebSocketAdaptor` or `MediaManager` directly if you have advanced
148150
customization needs (e.g., custom signaling transport or bespoke media capture).
149151
Otherwise, prefer the higher-level `WebRTCClient` methods.

packages/webrtc-sdk/examples/play.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ <h2>Play Sample (TS v2)</h2>
117117
const id = streamIdEl.value.trim();
118118
if (!id) { log('enter stream id'); return; }
119119
try {
120-
await adaptor.ready();
121120
await adaptor.join({ role: 'viewer', streamId: id, token: tokenEl.value.trim() || undefined, timeoutMs: 20000 });
122121
currentId = id;
123122
} catch (e) {

packages/webrtc-sdk/examples/publish.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ <h2>Publish Sample (TS v2)</h2>
176176
startBtn.onclick = async () => {
177177
const id = streamIdEl.value || 'stream_' + generateRandomString(6);
178178
try {
179-
await adaptor.ready();
180179
await adaptor.join({ role: 'publisher', streamId: id, token: tokenEl.value.trim() || undefined, timeoutMs: 20000 });
181180
currentId = id;
182181
startShareBtn.disabled = false;

packages/webrtc-sdk/src/core/media-manager.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ export class MediaManager extends Emitter<EventMap> {
102102
return grouped;
103103
}
104104

105-
/** Re-acquire default devices and replace local tracks; emits device_hotswapped. */
105+
/**
106+
* Re-acquire default input devices when `devicechange` fires and replace local tracks in-place.
107+
* Emits `device_hotswapped` with the swapped kind. Does nothing if user explicitly selected devices.
108+
*/
106109
private async handleDeviceHotSwap(): Promise<void> {
107110
try {
108111
const prevVideoId = this.selectedVideoInputId;

packages/webrtc-sdk/src/core/webrtc-client.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,10 @@ export class WebRTCClient extends Emitter<EventMap> {
762762
return this.media.listDevices();
763763
}
764764

765-
/** Set audio output device for a media element (or local preview by default). */
765+
/**
766+
* Set audio output device (sinkId) for a media element (or local preview by default).
767+
* If the browser does not support sinkId, an `error` event with code `set_sink_id_unsupported` is emitted.
768+
*/
766769
async setAudioOutput(deviceId: string, element?: HTMLMediaElement | null): Promise<void> {
767770
await this.media.setAudioOutput(deviceId, element);
768771
}
@@ -860,7 +863,8 @@ export class WebRTCClient extends Emitter<EventMap> {
860863
* sdk.enableTrack('mainStreamId', 'camera_user3', true);
861864
* ```
862865
*/
863-
enableTrack(mainTrackId: string, trackId: string, enabled: boolean): void {
866+
async enableTrack(mainTrackId: string, trackId: string, enabled: boolean): Promise<void> {
867+
await this.ready();
864868
if (!this.ws) return;
865869
const jsCmd = {
866870
command: "enableTrack",
@@ -879,7 +883,8 @@ export class WebRTCClient extends Emitter<EventMap> {
879883
* sdk.forceStreamQuality('mainStreamId', 720); // or 'auto'
880884
* ```
881885
*/
882-
forceStreamQuality(streamId: string, height: number | "auto"): void {
886+
async forceStreamQuality(streamId: string, height: number | "auto"): Promise<void> {
887+
await this.ready();
883888
if (!this.ws) return;
884889
const jsCmd = {
885890
command: "forceStreamQuality",
@@ -1296,49 +1301,57 @@ export class WebRTCClient extends Emitter<EventMap> {
12961301
}
12971302

12981303
/** Request stream info; listen on 'notification:streamInformation' or stream_information. */
1299-
getStreamInfo(streamId: string): void {
1304+
async getStreamInfo(streamId: string): Promise<void> {
1305+
await this.ready();
13001306
if (!this.ws) return;
13011307
this.ws.send(JSON.stringify({ command: "getStreamInfo", streamId }));
13021308
}
13031309

13041310
/** Request broadcast object; listen on 'notification:broadcastObject' or broadcast_object. */
1305-
getBroadcastObject(streamId: string): void {
1311+
async getBroadcastObject(streamId: string): Promise<void> {
1312+
await this.ready();
13061313
if (!this.ws) return;
13071314
this.ws.send(JSON.stringify({ command: "getBroadcastObject", streamId }));
13081315
}
13091316

13101317
/** Request room info of roomId; optionally include streamId for context. */
1311-
getRoomInfo(roomId: string, streamId = ""): void {
1318+
async getRoomInfo(roomId: string, streamId = ""): Promise<void> {
1319+
await this.ready();
13121320
if (!this.ws) return;
13131321
this.ws.send(JSON.stringify({ command: "getRoomInfo", room: roomId, streamId }));
13141322
}
13151323

13161324
/** Request track list under a main stream. */
1317-
getTracks(streamId: string, token = ""): void {
1325+
async getTracks(streamId: string, token = ""): Promise<void> {
1326+
await this.ready();
13181327
if (!this.ws) return;
13191328
this.ws.send(JSON.stringify({ command: "getTrackList", streamId, token }));
13201329
}
13211330

13221331
/** Request subtracks for a main stream with optional paging and role filter. */
1323-
getSubtracks(streamId: string, role = "", offset = 0, size = 50): void {
1332+
async getSubtracks(streamId: string, role = "", offset = 0, size = 50): Promise<void> {
1333+
await this.ready();
13241334
if (!this.ws) return;
13251335
this.ws.send(JSON.stringify({ command: "getSubtracks", streamId, role, offset, size }));
13261336
}
13271337

13281338
/** Request subtrack count for a main stream with optional role/status. */
1329-
getSubtrackCount(streamId: string, role = "", status = ""): void {
1339+
async getSubtrackCount(streamId: string, role = "", status = ""): Promise<void> {
1340+
await this.ready();
13301341
if (!this.ws) return;
13311342
this.ws.send(JSON.stringify({ command: "getSubtracksCount", streamId, role, status }));
13321343
}
13331344

13341345
/** Request current subscriber count; listen on subscriber_count. */
1335-
getSubscriberCount(streamId: string): void {
1346+
async getSubscriberCount(streamId: string): Promise<void> {
1347+
await this.ready();
13361348
if (!this.ws) return;
13371349
this.ws.send(JSON.stringify({ command: "getSubscriberCount", streamId }));
13381350
}
13391351

13401352
/** Request current subscriber list; listen on subscriber_list. */
1341-
getSubscriberList(streamId: string, offset = 0, size = 50): void {
1353+
async getSubscriberList(streamId: string, offset = 0, size = 50): Promise<void> {
1354+
await this.ready();
13421355
if (!this.ws) return;
13431356
this.ws.send(JSON.stringify({ command: "getSubscribers", streamId, offset, size }));
13441357
}
@@ -1416,7 +1429,8 @@ export class WebRTCClient extends Emitter<EventMap> {
14161429
}
14171430

14181431
/** Request video track assignments list for a main stream. */
1419-
requestVideoTrackAssignments(streamId: string): void {
1432+
async requestVideoTrackAssignments(streamId: string): Promise<void> {
1433+
await this.ready();
14201434
if (!this.ws) return;
14211435
this.ws.send(JSON.stringify({ command: "getVideoTrackAssignmentsCommand", streamId }));
14221436
}
@@ -1453,7 +1467,10 @@ export class WebRTCClient extends Emitter<EventMap> {
14531467
* sdk.updateVideoTrackAssignments({ streamId: 'main', offset: 20, size: 10 });
14541468
* ```
14551469
*/
1456-
updateVideoTrackAssignments(opts: import("./types.js").UpdateVideoTrackAssignmentsOptions): void {
1470+
async updateVideoTrackAssignments(
1471+
opts: import("./types.js").UpdateVideoTrackAssignmentsOptions
1472+
): Promise<void> {
1473+
await this.ready();
14571474
if (!this.ws) return;
14581475
this.ws.send(
14591476
JSON.stringify({
@@ -1473,7 +1490,8 @@ export class WebRTCClient extends Emitter<EventMap> {
14731490
* sdk.setMaxVideoTrackCount('mainStreamId', 9);
14741491
* ```
14751492
*/
1476-
setMaxVideoTrackCount(streamId: string, maxTrackCount: number): void {
1493+
async setMaxVideoTrackCount(streamId: string, maxTrackCount: number): Promise<void> {
1494+
await this.ready();
14771495
if (!this.ws) return;
14781496
this.ws.send(
14791497
JSON.stringify({

0 commit comments

Comments
 (0)