Skip to content

Commit 18c3b07

Browse files
author
smoghe-bw
committed
feat: harden audio stream availability signaling and fallback events
1 parent 166837e commit 18c3b07

2 files changed

Lines changed: 17 additions & 10 deletions

File tree

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export interface RtcOptions {
4646

4747
export interface RtcStream {
4848
mediaTypes: MediaType[];
49-
mediaStream?: MediaStream;
49+
mediaStream: MediaStream;
5050
callId?: string;
5151
autoAccepted?: boolean;
5252
}

src/v1/bandwidthRtc.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ export class BandwidthRtc {
131131
this.signaling.on("init", this.init.bind(this));
132132
this.signaling.on("streamAvailable", ({ callId, autoAccepted }: { callId: string; autoAccepted: boolean }) => {
133133
if (this.pendingSubscribeStream) {
134+
// ontrack already fired at connect time — fire the combined event now.
134135
this.callIsActive = true;
135136
this.streamAvailableHandler?.({
136137
mediaTypes: [MediaType.AUDIO],
@@ -139,7 +140,7 @@ export class BandwidthRtc {
139140
autoAccepted,
140141
});
141142
} else {
142-
// ontrack hasn't fired yet — store and fire once it does
143+
// ontrack hasn't fired yet — store and fire once it does.
143144
this.pendingCallInfo = { callId, autoAccepted };
144145
}
145146
});
@@ -151,7 +152,7 @@ export class BandwidthRtc {
151152
this.pendingCallInfo = undefined;
152153
this.streamUnavailableHandler?.({
153154
mediaTypes: [MediaType.AUDIO],
154-
mediaStream: this.pendingSubscribeStream,
155+
mediaStream: this.pendingSubscribeStream!,
155156
callId,
156157
});
157158
});
@@ -527,6 +528,17 @@ export class BandwidthRtc {
527528

528529
track.onunmute = (event) => {
529530
logger.debug("onunmute", event.target);
531+
// Fallback for gateways that don't send WS streamAvailable: fire
532+
// onStreamAvailable when audio actually starts flowing. No-op on new
533+
// gateways because callIsActive is already true from the WS event.
534+
if (!this.callIsActive && this.pendingSubscribeStream) {
535+
this.callIsActive = true;
536+
logger.debug("onStreamAvailable (onunmute fallback)", this.pendingSubscribeStream.id);
537+
this.streamAvailableHandler?.({
538+
mediaTypes: [MediaType.AUDIO],
539+
mediaStream: this.pendingSubscribeStream,
540+
});
541+
}
530542
};
531543

532544
track.onended = (event) => {
@@ -567,24 +579,19 @@ export class BandwidthRtc {
567579
}
568580
};
569581

570-
// Store the subscribe stream. We fire onStreamAvailable only when the WS
571-
// streamAvailable notification arrives (which carries callId/autoAccepted), so
572-
// that customers always receive a single event with mediaStream set. If the WS
573-
// notification already arrived before ontrack, fire the combined event now.
574582
this.pendingSubscribeStream = stream;
575583
if (this.pendingCallInfo) {
584+
// WS arrived before ontrack — fire the combined event now.
576585
const { callId, autoAccepted } = this.pendingCallInfo;
577586
this.pendingCallInfo = undefined;
578587
this.callIsActive = true;
579-
logger.debug("onStreamAvailable (deferred)", stream.id);
588+
logger.debug("onStreamAvailable (WS-enriched)", stream.id);
580589
this.streamAvailableHandler?.({
581590
mediaTypes: [MediaType.AUDIO],
582591
mediaStream: stream,
583592
callId,
584593
autoAccepted,
585594
});
586-
} else {
587-
logger.debug("ontrack: stored subscribe stream, waiting for WS streamAvailable", stream.id);
588595
}
589596
}
590597
};

0 commit comments

Comments
 (0)