I am unable to obtain the mediaStreamTracks
for local and remote participants, even after the roomState
changes to connected. However, I noticed that they become available after reconnecting. Please advice how I could access the mediaStreamTracks
for both local and remote participants after connecting to the room.
To reproduce the bug try the following code in Room component of this repo.
// Below function is from room component of this repo
import useRoomState from '../../hooks/useRoomState/useRoomState';
import useVideoContext from '../useVideoContext/useVideoContext';
export default function Room() {
const { room } = useVideoContext();
const {
room: { localParticipant },
} = useVideoContext();
const [participants, setParticipants] = useState(Array.from(room.participants.values()));
const roomState = useRoomState();
useEffect(() => {
if(roomState === 'connected'){
localParticipant.tracks.forEach(track => {
if (track.kind === 'data' ) {
return;
}
console.log(track.mediaStreamTrack) // Getting undefined!!
}
participants.forEach(participant => {
participant.tracks.forEach(track => {
if (track.kind === 'data') {
return;
}
console.log(track.mediaStreamTrack) // Getting undefined!!
}
}
}, [roomState]);
}
Twilio developer evangelist here.
When you call
tracks
on aParticipant
object you don't actually get theTrack
objects. Instead you get an array ofTrackPublication
objects. ATrackPublication
represents a track that has been published to a room, however it is not theTrack
object (which has the reference to theMediaStreamTrack
that you're looking for) and, for remote participants, aRemoteTrack
object may not yet exist until thelocalParticipant
has subscribed to the track.To handle this, you need to check whether the
TrackPublication
has the track available and if it does, use it, if it doesn't, listen for the "subscribed" event.For example, with the remote participants:
Let me know if that helps.