-
Notifications
You must be signed in to change notification settings - Fork 18
/
ParticipantView.tsx
181 lines (165 loc) · 5.37 KB
/
ParticipantView.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import {
ComponentType,
forwardRef,
ReactElement,
useMemo,
useState,
} from 'react';
import clsx from 'clsx';
import {
hasAudio,
hasScreenShareAudio,
hasVideo,
StreamVideoParticipant,
VideoTrackType,
} from '@stream-io/video-client';
import { Audio } from '../Audio';
import { Video, VideoProps } from '../Video';
import { useTrackElementVisibility } from '../../hooks';
import { DefaultParticipantViewUI } from './DefaultParticipantViewUI';
import { applyElementToRef, isComponentType } from '../../../utilities';
import { ParticipantViewContext } from './ParticipantViewContext';
export type ParticipantViewProps = {
/**
* The participant whose video/audio stream we want to play.
*/
participant: StreamVideoParticipant;
/**
* Override the default UI for rendering participant information/actions.
* pass `null` if you wish to not render anything
* @default DefaultParticipantViewUI
*/
ParticipantViewUI?: ComponentType | ReactElement | null;
/**
* The kind of video stream to play for the given participant.
* The default value is `videoTrack`.
* You can use `none` if you're building an audio-only call.
*/
trackType?: VideoTrackType | 'none';
/**
* This prop is only useful for advanced use-cases (for example, building your own layout).
* When set to `true` it will mute the give participant's audio stream on the client side.
* The local participant is always muted.
*/
muteAudio?: boolean;
/**
* An object with set functions meant for exposing the video
* and video placeholder elements to the integrators.
* It's useful when you want to attach custom event handlers to these elements.
* - `refs.setVideoElement`
* - `refs.setVideoPlaceholderElement`
*/
refs?: VideoProps['refs'];
/**
* Custom class applied to the root DOM element.
*/
className?: string;
} & Pick<VideoProps, 'VideoPlaceholder'>;
export const ParticipantView = forwardRef<HTMLDivElement, ParticipantViewProps>(
function ParticipantView(
{
participant,
trackType = 'videoTrack',
muteAudio,
refs: { setVideoElement, setVideoPlaceholderElement } = {},
className,
VideoPlaceholder,
ParticipantViewUI = DefaultParticipantViewUI as ComponentType,
},
ref,
) {
const { isLocalParticipant, isSpeaking, isDominantSpeaker, sessionId } =
participant;
const hasAudioTrack = hasAudio(participant);
const hasVideoTrack = hasVideo(participant);
const hasScreenShareAudioTrack = hasScreenShareAudio(participant);
const [trackedElement, setTrackedElement] = useState<HTMLDivElement | null>(
null,
);
const [contextVideoElement, setContextVideoElement] =
useState<HTMLVideoElement | null>(null);
const [contextVideoPlaceholderElement, setContextVideoPlaceholderElement] =
useState<HTMLDivElement | null>(null);
// TODO: allow to pass custom ViewportTracker instance from props
useTrackElementVisibility({
sessionId,
trackedElement,
trackType,
});
const participantViewContextValue = useMemo(
() => ({
participant,
participantViewElement: trackedElement,
videoElement: contextVideoElement,
videoPlaceholderElement: contextVideoPlaceholderElement,
trackType,
}),
[
contextVideoElement,
contextVideoPlaceholderElement,
participant,
trackedElement,
trackType,
],
);
const videoRefs: VideoProps['refs'] = useMemo(
() => ({
setVideoElement: (element) => {
setVideoElement?.(element);
setContextVideoElement(element);
},
setVideoPlaceholderElement: (element) => {
setVideoPlaceholderElement?.(element);
setContextVideoPlaceholderElement(element);
},
}),
[setVideoElement, setVideoPlaceholderElement],
);
return (
<div
data-testid="participant-view"
ref={(element) => {
applyElementToRef(ref, element);
setTrackedElement(element);
}}
className={clsx(
'str-video__participant-view',
isDominantSpeaker && 'str-video__participant-view--dominant-speaker',
isSpeaking && 'str-video__participant-view--speaking',
!hasVideoTrack && 'str-video__participant-view--no-video',
!hasAudioTrack && 'str-video__participant-view--no-audio',
className,
)}
>
<ParticipantViewContext.Provider value={participantViewContextValue}>
{/* mute the local participant, as we don't want to hear ourselves */}
{!isLocalParticipant && !muteAudio && (
<>
{hasAudioTrack && (
<Audio participant={participant} trackType="audioTrack" />
)}
{hasScreenShareAudioTrack && (
<Audio
participant={participant}
trackType="screenShareAudioTrack"
/>
)}
</>
)}
<Video
VideoPlaceholder={VideoPlaceholder}
participant={participant}
trackType={trackType}
refs={videoRefs}
autoPlay
/>
{isComponentType(ParticipantViewUI) ? (
<ParticipantViewUI />
) : (
ParticipantViewUI
)}
</ParticipantViewContext.Provider>
</div>
);
},
);