generated from hatchways/express-starter
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#57 video-chat functionality first iteration
- Loading branch information
Showing
8 changed files
with
399 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import React, { useState, useEffect, useRef } from "react"; | ||
import { makeStyles } from "@material-ui/core/styles"; | ||
|
||
const useStyles = makeStyles((theme) => ({ | ||
screen: { | ||
maxWidth: "200px", | ||
maxHeight: "200px" | ||
} | ||
})); | ||
|
||
const ParticipantVideo = ({ participant }) => { | ||
const classes = useStyles(); | ||
const [videoTracks, setVideoTracks] = useState([]); | ||
const [audioTracks, setAudioTracks] = useState([]); | ||
|
||
const videoRef = useRef(); | ||
const audioRef = useRef(); | ||
|
||
const trackpubsToTracks = (trackMap) => | ||
Array.from(trackMap.values()) | ||
.map((publication) => publication.track) | ||
.filter((track) => track !== null); | ||
|
||
useEffect(() => { | ||
setVideoTracks(trackpubsToTracks(participant.videoTracks)); | ||
setAudioTracks(trackpubsToTracks(participant.audioTracks)); | ||
|
||
const trackSubscribed = (track) => { | ||
if (track.kind === "video") { | ||
setVideoTracks((videoTracks) => [...videoTracks, track]); | ||
} else if (track.kind === "audio") { | ||
setAudioTracks((audioTracks) => [...audioTracks, track]); | ||
} | ||
}; | ||
|
||
const trackUnsubscribed = (track) => { | ||
if (track.kind === "video") { | ||
setVideoTracks((videoTracks) => videoTracks.filter((v) => v !== track)); | ||
} else if (track.kind === "audio") { | ||
setAudioTracks((audioTracks) => audioTracks.filter((a) => a !== track)); | ||
} | ||
}; | ||
|
||
participant.on("trackSubscribed", trackSubscribed); | ||
participant.on("trackUnsubscribed", trackUnsubscribed); | ||
|
||
return () => { | ||
setVideoTracks([]); | ||
setAudioTracks([]); | ||
participant.removeAllListeners(); | ||
}; | ||
}, [participant]); | ||
|
||
useEffect(() => { | ||
const videoTrack = videoTracks[0]; | ||
if (videoTrack) { | ||
videoTrack.attach(videoRef.current); | ||
return () => { | ||
videoTrack.detach(); | ||
}; | ||
} | ||
}, [videoTracks]); | ||
|
||
useEffect(() => { | ||
const audioTrack = audioTracks[0]; | ||
if (audioTrack) { | ||
audioTrack.attach(audioRef.current); | ||
return () => { | ||
audioTrack.detach(); | ||
}; | ||
} | ||
}, [audioTracks]); | ||
|
||
return ( | ||
<div className={classes.screen}> | ||
<h4>{participant.identity}</h4> | ||
<video ref={videoRef} autoPlay={true} /> | ||
<audio ref={audioRef} autoPlay={true} muted={true} /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ParticipantVideo; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.