Skip to content

Commit

Permalink
feat: add voice actvity indicator on the participant video (#208)
Browse files Browse the repository at this point in the history
Co-authored-by: Yohan Totting <[email protected]>
  • Loading branch information
tyohan and Yohan Totting authored Apr 24, 2024
1 parent b62585f commit 036f91f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 17 deletions.
14 changes: 14 additions & 0 deletions app/_features/room/components/conference-screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -460,13 +460,27 @@ function VideoScreen({
const videoContainerRef = useRef<HTMLDivElement>(null);

useEffect(() => {
const callbackVoiceActivity = () => {
if (stream.audioLevel > 0) {
stream.videoElement.style.borderColor = 'green';
stream.videoElement.style.borderWidth = '5px';
stream.videoElement.style.borderStyle = 'solid';
} else {
stream.videoElement.style.borderColor = 'transparent';
stream.videoElement.style.borderWidth = '0';
stream.videoElement.style.borderStyle = 'none';
}
};

if (stream.origin === 'remote') {
peer?.observeVideo(stream.videoElement);
stream.addEventListener('voiceactivity', callbackVoiceActivity);
}

return () => {
if (stream.origin === 'remote') {
peer?.unobserveVideo(stream.videoElement);
stream.removeEventListener('voiceactivity', callbackVoiceActivity);
}
};
}, [peer, stream]);
Expand Down
6 changes: 5 additions & 1 deletion app/_features/room/contexts/datachannel-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ export const DataChannelProvider = ({
});
};

peerConnection.addEventListener('datachannel', onPeerDataChannelAdded);
peerConnection.addEventListener('datachannel', (e) => {
if (e.channel.label !== 'internal') {
onPeerDataChannelAdded(e);
}
});

return () => {
peerConnection.removeEventListener('datachannel', onPeerDataChannelAdded);
Expand Down
34 changes: 23 additions & 11 deletions app/_features/room/contexts/participant-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,26 @@ export type ParticipantVideo = {
readonly source: 'media' | 'screen';
readonly mediaStream: MediaStream;
readonly videoElement: HTMLVideoElement;
audioLevel: number;
readonly replaceTrack: (newTrack: MediaStreamTrack) => void;
readonly addEventListener: (
type: string,
listener: (event: CustomEvent) => void
) => void;
readonly removeEventListener: (
type: string,
listener: (event: CustomEvent) => void
) => void;
};

export type ParticipantStream = Omit<ParticipantVideo, 'videoElement'>;

const createParticipantVideo = (
stream: ParticipantStream
): ParticipantVideo => {
const participantVideo: ParticipantVideo = {
...stream,
videoElement: document.createElement('video'),
};
const createParticipantVideo = (stream: any): ParticipantVideo => {
stream.videoElement = document.createElement('video');

participantVideo.videoElement.srcObject = stream.mediaStream;
stream.videoElement.srcObject = stream.mediaStream;

return participantVideo;
return stream;
};

const defaultValue = {
Expand All @@ -50,7 +54,7 @@ export function ParticipantProvider({
const [localStream, setLocalStream] = useState<MediaStream | null>(null);

useEffect(() => {
const onMediaInputTurnedOn = ((event: CustomEvent) => {
const onMediaInputTurnedOn = ((event: CustomEventInit) => {
const detail = event.detail || {};
const mediaInput = detail.mediaInput;

Expand All @@ -68,8 +72,16 @@ export function ParticipantProvider({

useEffect(() => {
clientSDK.on(RoomEvent.STREAM_AVAILABLE, (data) => {
const video = createParticipantVideo(data.stream);
data.stream.addEventListener('voiceactivity', (e: CustomEventInit) => {
// reordering the streams based on voice activity
const audioLevel = e.detail.audioLevel;

// call setStreams with the new streams order
});

setStreams((prevState) => {
return [...prevState, createParticipantVideo(data.stream)];
return [...prevState, video];
});
});

Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"db:studio": "npx drizzle-kit studio"
},
"dependencies": {
"@inlivedev/inlive-js-sdk": "^0.17.4",
"@inlivedev/inlive-js-sdk": "^0.17.5",
"@nextui-org/react": "^2.2.9",
"@react-email/components": "0.0.16",
"@sentry/nextjs": "^7.70.0",
Expand Down

0 comments on commit 036f91f

Please sign in to comment.