@@ -5,6 +5,7 @@ import type { VideoJitterConfig, AudioJitterConfig } from '../types/jitterBuffer
55import type { VideoEncodingSettings } from '../types/videoEncoding'
66import type { AudioEncodingSettings } from '../types/audioEncoding'
77import type { AudioCaptureConstraints , CameraCaptureConstraints } from '../types/captureConstraints'
8+ import type { SubscribeMessage } from '../../../../pkg/moqt_client_wasm'
89
910export 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}
0 commit comments