Skip to content

Commit

Permalink
Merge branch 'dev' into fix/WEB-2914-audio-not-switching-to-bluetooth…
Browse files Browse the repository at this point in the history
…-on-rejoin
  • Loading branch information
raviteja83 authored Aug 5, 2024
2 parents efcdcf3 + 03e7e8e commit 092b29d
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 30 deletions.
6 changes: 6 additions & 0 deletions packages/hms-video-store/src/connection/HMSConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export default abstract class HMSConnection {
* - [HMSSubscribeConnection] clears this list as soon as we call [addIceCandidate]
*/
readonly candidates = new Array<RTCIceCandidateInit>();
// @ts-ignore
private sfuNodeId?: string;

selectedCandidatePair?: RTCIceCandidatePair;

Expand All @@ -48,6 +50,10 @@ export default abstract class HMSConnection {
return this.role === HMSConnectionRole.Publish ? HMSAction.PUBLISH : HMSAction.SUBSCRIBE;
}

setSfuNodeId(nodeId?: string) {
this.sfuNodeId = nodeId;
}

addTransceiver(track: MediaStreamTrack, init: RTCRtpTransceiverInit): RTCRtpTransceiver {
return this.nativeConnection.addTransceiver(track, init);
}
Expand Down
4 changes: 0 additions & 4 deletions packages/hms-video-store/src/media/streams/HMSLocalStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ export class HMSLocalStream extends HMSMediaStream {
this.connection = connection;
}

clone() {
return new HMSLocalStream(this.nativeStream.clone());
}

addTransceiver(track: HMSLocalTrack, simulcastLayers: SimulcastLayer[]) {
const transceiver = this.connection!.addTransceiver(track.getTrackBeingSent(), {
streams: [this.nativeStream],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ export class HMSLocalAudioTrack extends HMSAudioTrack {
}
}

clone(stream?: HMSLocalStream) {
clone(stream: HMSLocalStream) {
const track = new HMSLocalAudioTrack(
stream || (this.stream as HMSLocalStream).clone(),
stream,
this.nativeTrack.clone(),
this.source!,
this.eventBus,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ export class HMSLocalVideoTrack extends HMSVideoTrack {
}
}

clone(stream?: HMSLocalStream) {
clone(stream: HMSLocalStream) {
const track = new HMSLocalVideoTrack(
stream || (this.stream as HMSLocalStream).clone(),
stream,
this.nativeTrack.clone(),
this.source!,
this.eventBus,
Expand Down
25 changes: 15 additions & 10 deletions packages/hms-video-store/src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,16 +429,9 @@ export class HMSSdk implements HMSInterface {
resolve();
};

const errorHandler = (ex?: HMSException) => {
this.analyticsTimer.end(TimedEvent.PREVIEW);
ex && this.errorListener?.onError(ex);
this.sendPreviewAnalyticsEvent(ex);
this.sdkState.isPreviewInProgress = false;
reject(ex as HMSException);
};

this.eventBus.policyChange.subscribeOnce(policyHandler);
this.eventBus.leave.subscribeOnce(errorHandler);
this.eventBus.leave.subscribeOnce(this.handlePreviewError);
this.eventBus.leave.subscribeOnce(ex => reject(ex as HMSException));

this.transport
.preview(
Expand All @@ -458,10 +451,20 @@ export class HMSSdk implements HMSInterface {
});
}
})
.catch(errorHandler);
.catch(ex => {
this.handlePreviewError(ex);
reject(ex);
});
});
}

private handlePreviewError = (ex?: HMSException) => {
this.analyticsTimer.end(TimedEvent.PREVIEW);
ex && this.errorListener?.onError(ex);
this.sendPreviewAnalyticsEvent(ex);
this.sdkState.isPreviewInProgress = false;
};

private async midCallPreview(asRole?: string, settings?: InitialSettings): Promise<void> {
if (!this.localPeer || this.transportState !== TransportState.Joined) {
throw ErrorFactory.GenericErrors.NotConnected(HMSAction.VALIDATION, 'Not connected - midCallPreview');
Expand Down Expand Up @@ -539,6 +542,8 @@ export class HMSSdk implements HMSInterface {
throw ErrorFactory.GenericErrors.NotReady(HMSAction.JOIN, "Preview is in progress, can't join");
}

// remove terminal error handling from preview(do not send preview.failed after join on disconnect)
this.eventBus.leave.unsubscribe(this.handlePreviewError);
this.analyticsTimer.start(TimedEvent.JOIN);
this.sdkState.isJoinInProgress = true;

Expand Down
28 changes: 16 additions & 12 deletions packages/hms-video-store/src/transport/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,15 @@ export default class HMSTransport {
};

private signalObserver: ISignalEventsObserver = {
onOffer: async (jsep: RTCSessionDescriptionInit) => {
onOffer: async (jsep: RTCSessionDescriptionInit & { sfu_node_id: string }) => {
try {
if (!this.subscribeConnection) {
return;
}
if (this.sfuNodeId !== jsep.sfu_node_id) {
HMSLogger.d(TAG, 'ignoring old offer');
return;
}
await this.subscribeConnection.setRemoteDescription(jsep);
HMSLogger.d(
TAG,
Expand Down Expand Up @@ -419,10 +423,12 @@ export default class HMSTransport {
}
}

setSFUNodeId(id: string) {
setSFUNodeId(id?: string) {
this.signal.setSfuNodeId(id);
if (!this.sfuNodeId) {
this.sfuNodeId = id;
this.publishConnection?.setSfuNodeId(id);
this.subscribeConnection?.setSfuNodeId(id);
} else if (this.sfuNodeId !== id) {
this.sfuNodeId = id;
this.handleSFUMigration();
Expand Down Expand Up @@ -456,9 +462,9 @@ export default class HMSTransport {
if (localPeer.audioTrack) {
const stream = localPeer.audioTrack.stream as HMSLocalStream;
if (!streamMap.get(stream.id)) {
streamMap.set(stream.id, stream.clone());
streamMap.set(stream.id, new HMSLocalStream(new MediaStream()));
}
const newTrack = localPeer.audioTrack.clone(streamMap.get(stream.id));
const newTrack = localPeer.audioTrack.clone(streamMap.get(stream.id)!);
this.store.removeTrack(localPeer.audioTrack);
localPeer.audioTrack.cleanup();
await this.publishTrack(newTrack);
Expand All @@ -468,10 +474,10 @@ export default class HMSTransport {
if (localPeer.videoTrack) {
const stream = localPeer.videoTrack.stream as HMSLocalStream;
if (!streamMap.get(stream.id)) {
streamMap.set(stream.id, stream.clone());
streamMap.set(stream.id, new HMSLocalStream(new MediaStream()));
}
this.store.removeTrack(localPeer.videoTrack);
const newTrack = localPeer.videoTrack.clone(streamMap.get(stream.id));
const newTrack = localPeer.videoTrack.clone(streamMap.get(stream.id)!);
localPeer.videoTrack.cleanup();
await this.publishTrack(newTrack);
localPeer.videoTrack = newTrack;
Expand All @@ -483,10 +489,10 @@ export default class HMSTransport {
if (track) {
const stream = track.stream as HMSLocalStream;
if (!streamMap.get(stream.id)) {
streamMap.set(stream.id, stream.clone());
streamMap.set(stream.id, new HMSLocalStream(new MediaStream()));
}
this.store.removeTrack(track);
const newTrack = track.clone(streamMap.get(stream.id));
const newTrack = track.clone(streamMap.get(stream.id)!);
if (newTrack.type === 'video' && newTrack.source === 'screen') {
newTrack.nativeTrack.addEventListener('ended', this.onScreenshareStop);
}
Expand Down Expand Up @@ -913,8 +919,7 @@ export default class HMSTransport {
onDemandTracks,
offer,
);
this.sfuNodeId = answer?.sfu_node_id;
this.signal.setSfuNodeId(this.sfuNodeId);
this.setSFUNodeId(answer?.sfu_node_id);
await this.publishConnection.setRemoteDescription(answer);
for (const candidate of this.publishConnection.candidates) {
await this.publishConnection.addIceCandidate(candidate);
Expand All @@ -937,8 +942,7 @@ export default class HMSTransport {
simulcast,
onDemandTracks,
);
this.sfuNodeId = response?.sfu_node_id;
this.signal.setSfuNodeId(this.sfuNodeId);
this.setSFUNodeId(response?.sfu_node_id);
return !!response;
}

Expand Down

0 comments on commit 092b29d

Please sign in to comment.