Skip to content

Commit 9d7ec58

Browse files
committed
fix bug: dont start sending after gUM
1 parent 70e66a1 commit 9d7ec58

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

js/examples/call/src/media/callMediaController.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { VideoJitterConfig, AudioJitterConfig } from '../types/jitterBuffer
55
import type { VideoEncodingSettings } from '../types/videoEncoding'
66
import type { AudioEncodingSettings } from '../types/audioEncoding'
77
import type { AudioCaptureConstraints, CameraCaptureConstraints } from '../types/captureConstraints'
8+
import type { SubscribeMessage } from '../../../../pkg/moqt_client_wasm'
89

910
export interface MediaHandlers {
1011
onLocalVideoStream?: (stream: MediaStream | null) => void
@@ -30,10 +31,12 @@ export class CallMediaController {
3031
private handlers: MediaHandlers = {}
3132
private readonly publisher: MediaPublisher
3233
private readonly subscriber: MediaSubscriber
34+
private readonly trackNamespace: string[]
3335

3436
constructor(client: MoqtClientWrapper, trackNamespace: string[]) {
3537
this.publisher = new MediaPublisher(client, trackNamespace)
3638
this.subscriber = new MediaSubscriber(client)
39+
this.trackNamespace = trackNamespace
3740

3841
this.publisher.setHandlers({
3942
onLocalVideoStream: (stream) => this.handlers.onLocalVideoStream?.(stream),
@@ -57,6 +60,26 @@ export class CallMediaController {
5760
onRemoteAudioRenderingLatency: (userId, ms) => this.handlers.onRemoteAudioRenderingLatency?.(userId, ms),
5861
onRemoteVideoConfig: (userId, config) => this.handlers.onRemoteVideoConfig?.(userId, config)
5962
})
63+
64+
client.setOnIncomingSubscribeHandler(async ({ subscribe, isSuccess, code, respondOk, respondError }) => {
65+
if (!isSuccess) {
66+
await respondError(BigInt(code), 'Subscription validation failed')
67+
return
68+
}
69+
await respondOk(0n, 'secret', 'subgroup')
70+
if (!this.isLocalTrack(subscribe)) {
71+
return
72+
}
73+
try {
74+
if (subscribe.trackName === 'video') {
75+
await this.publisher.restartVideoForNewSubscriber()
76+
} else if (subscribe.trackName === 'audio') {
77+
await this.publisher.restartAudioForNewSubscriber()
78+
}
79+
} catch (err) {
80+
console.error('Failed to kick media pipeline for new subscriber', err)
81+
}
82+
})
6083
}
6184

6285
setHandlers(handlers: MediaHandlers): void {
@@ -122,4 +145,11 @@ export class CallMediaController {
122145
setAudioCaptureConstraints(constraints: AudioCaptureConstraints): void {
123146
this.publisher.setAudioCaptureConstraints(constraints)
124147
}
148+
149+
private isLocalTrack(subscribe: SubscribeMessage): boolean {
150+
if (subscribe.trackNamespace.length !== this.trackNamespace.length) {
151+
return false
152+
}
153+
return subscribe.trackNamespace.every((value, index) => value === this.trackNamespace[index])
154+
}
125155
}

js/examples/call/src/media/mediaPublisher.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,20 @@ export class MediaPublisher {
435435
await this.startVideo(deviceId)
436436
}
437437

438+
async restartVideoForNewSubscriber(): Promise<void> {
439+
if (!this.videoWorker) {
440+
return
441+
}
442+
await this.forceKeyframeAndRestart(this.currentVideoDeviceId ?? undefined)
443+
}
444+
445+
async restartAudioForNewSubscriber(): Promise<void> {
446+
if (!this.audioWorker) {
447+
return
448+
}
449+
await this.setAudioEncodingSettings(this.audioEncodingSettings, true)
450+
}
451+
438452
private collectAliasesSafe(trackName: 'video' | 'audio'): bigint[] {
439453
const rawClient = this.client.getRawClient()
440454
if (!rawClient) {

0 commit comments

Comments
 (0)