99 ConnectionState ,
1010 type E2EEManagerOptions ,
1111 ExternalE2EEKeyProvider ,
12- type LocalVideoTrack ,
12+ LocalVideoTrack ,
1313 Room ,
1414 type RoomOptions ,
1515 Track ,
@@ -18,6 +18,8 @@ import { useEffect, useMemo, useRef } from "react";
1818import E2EEWorker from "livekit-client/e2ee-worker?worker" ;
1919import { logger } from "matrix-js-sdk/lib/logger" ;
2020import { type MatrixRTCSession } from "matrix-js-sdk/lib/matrixrtc" ;
21+ import { useObservable , useObservableEagerState } from "observable-hooks" ;
22+ import { map } from "rxjs" ;
2123
2224import { defaultLiveKitOptions } from "./options" ;
2325import { type SFUConfig } from "./openIDSFU" ;
@@ -39,6 +41,7 @@ import {
3941 useTrackProcessorSync ,
4042} from "./TrackProcessorContext" ;
4143import { useInitial } from "../useInitial" ;
44+ import { observeTrackReference$ } from "../state/MediaViewModel" ;
4245
4346interface UseLivekitResult {
4447 livekitRoom ?: Room ;
@@ -132,21 +135,25 @@ export function useLiveKit(
132135 return r ;
133136 } , [ roomOptions , e2eeSystem ] ) ;
134137
135- const videoTrack = useMemo (
136- ( ) =>
137- Array . from ( room . localParticipant . videoTrackPublications . values ( ) ) . find (
138- ( v ) => v . source === Track . Source . Camera ,
139- ) ?. track as LocalVideoTrack | null ,
140- [
141- room . localParticipant . videoTrackPublications ,
142- // We need to update on map changes
143- // eslint-disable-next-line react-hooks/exhaustive-deps
144- room . localParticipant . videoTrackPublications . keys ( ) ,
145- ] ,
138+ // Sync the requested track processors with LiveKit
139+ useTrackProcessorSync (
140+ useObservableEagerState (
141+ useObservable (
142+ ( room$ ) =>
143+ observeTrackReference$ (
144+ room$ . pipe ( map ( ( [ room ] ) => room . localParticipant ) ) ,
145+ Track . Source . Camera ,
146+ ) . pipe (
147+ map ( ( trackRef ) => {
148+ const track = trackRef ?. publication ?. track ;
149+ return track instanceof LocalVideoTrack ? track : null ;
150+ } ) ,
151+ ) ,
152+ [ room ] ,
153+ ) ,
154+ ) ,
146155 ) ;
147156
148- useTrackProcessorSync ( videoTrack ) ;
149-
150157 const connectionState = useECConnectionState (
151158 {
152159 deviceId : initialDevices . current . audioInput . selectedId ,
0 commit comments