import { useEffect } from 'react'; import { Platform } from 'react-native'; import { Call, CallingState, DebounceType, hasScreenShare, hasVideo, SfuModels, type StreamVideoParticipant, type VideoTrackType, } from '@stream-io/video-client'; import { BehaviorSubject, combineLatest, distinctUntilChanged, distinctUntilKeyChanged, filter, map, of, } from 'rxjs'; import { getIosPipTrack$, setIosPipTrack, } from '../../../../utils/internal/iosPipTrack'; type TrackSubscriberProps = { participantSessionId: string; call: Call; trackType: VideoTrackType; isVisible: boolean; /** * The dimensions of the view rendering the track, owned by the parent so that * the last reported layout survives a remount of this component. */ dimensions$: BehaviorSubject; /** * Set by the native iOS Picture in Picture window, whose bounds this * subscriber requests. While it does, the inline views of the same track * stop requesting their own, hidden layout. Only rendered on iOS; on other * platforms the gate is always open. */ isPipWriter?: boolean; }; /** * This component is used to subscribe to the video + audio track of the participant in the following cases: * 1. When the participant starts publishing the video track * 2. When the participant changes the video track dimensions * 3. When the participant becomes visible * 4. On joined callingState, this handles reconnection * This component is used to unsubscribe to video track and subscribe only to the audio track of the participant (by passing undefined dimensions) in the following cases: * 1. When the participant stops publishing the video track * 2. When the participant becomes invisible * * The same component serves the native iOS Picture in Picture window, which * renders one of the tracks too. Its bounds take precedence over the inline * views of that track for as long as it is on screen. */ const TrackSubscriber = (props: TrackSubscriberProps) => { const { call, participantSessionId, trackType, isVisible, dimensions$, isPipWriter, } = props; useEffect(() => { const requestTrackWithDimensions = ( debounceType: DebounceType, dimension: SfuModels.VideoDimension | undefined, ) => { if (dimension && (dimension.width === 0 || dimension.height === 0)) { // ignore 0x0 dimensions. this can happen when the video element // is not visible (e.g., has display: none). // we treat this as "unsubscription" as we don't want to keep // consuming bandwidth for a video that is not visible on the screen. dimension = undefined; } call.state.updateParticipantTracks(trackType, { [participantSessionId]: { dimension }, }); call.trackSubscriptionManager.apply(debounceType); }; const isPublishingTrack$ = call.state.participants$.pipe( map((ps) => ps.find((p) => p.sessionId === participantSessionId)), filter((p): p is StreamVideoParticipant => !!p), distinctUntilKeyChanged('publishedTracks'), map((p) => trackType === 'videoTrack' ? hasVideo(p) : hasScreenShare(p), ), distinctUntilChanged(), ); const isJoinedState$ = call.state.callingState$.pipe( map((callingState) => callingState === CallingState.JOINED), ); const trackKey = { sessionId: participantSessionId, trackType }; const pipTrack$ = Platform.OS === 'ios' ? getIosPipTrack$(call) : undefined; if (isPipWriter && pipTrack$) setIosPipTrack(call, trackKey); const canWrite$ = pipTrack$ ? pipTrack$.pipe( map((track) => { const sameTrack = track?.sessionId === participantSessionId && track?.trackType === trackType; return isPipWriter ? sameTrack : !sameTrack; }), distinctUntilChanged(), ) : of(true); const subscription = combineLatest([ dimensions$, isPublishingTrack$, isJoinedState$, canWrite$, ]).subscribe(([dimension, isPublishing, isJoined, canWrite]) => { if (isJoined && canWrite) { if (!isVisible || !isPublishing) { requestTrackWithDimensions(DebounceType.MEDIUM, undefined); } else if (dimension) { requestTrackWithDimensions(DebounceType.IMMEDIATE, dimension); } } }); return () => { subscription.unsubscribe(); if (isPipWriter && pipTrack$) { // Let sibling inline views finish unmounting before handing back demand. queueMicrotask(() => { if (pipTrack$.getValue() === trackKey) setIosPipTrack(call, undefined); }); } }; }, [ call, participantSessionId, trackType, isVisible, dimensions$, isPipWriter, ]); return null; }; TrackSubscriber.displayName = 'TrackSubscriber'; export default TrackSubscriber;