philnash/mediadevices-camera-selection

Could not get stream error when unpublish existed tracks

shyamady opened this issue · 3 comments

This time, I tried to switch local video track to screen sharing. It seems to be the same logic as this repository. But I could not unpublish existed tracks due not to get stream error.

Could not get stream: TypeError: track must be a LocalAudioTrack, LocalVideoTrack, LocalDataTrack, or MediaStreamTrack

This is React code:

const screenTracks = returnedStream.getVideoTracks()[0];

const tracks = Array.from(localParticipant.videoTracks.values());
console.log("tracks", tracks);
localParticipant.unpublishTracks(tracks);
// Error occurs here

activeRoom.localParticipant.publishTrack(screenTracks);
screenVideo.srcObject = returnedStream;
self.setState({ turnScreen: true, screenTracks: returnedStream });

Tracks fetched successfully like this:

tracks 
 [LocalVideoTrackPublication]
    0: LocalVideoTrackPublication
      isTrackEnabled:(...)
      priority: (...)
      trackName: "XXXXXXXXXXXXX"
      trackSid: "XXXXXXXXXXXX"
      kind: "video"
      track: LocalVideoTrack
          isStarted: (...)
          isEnabled: (...)
          isStopped: (...)
          kind: "video"
          name: "XXXXXXXXXXXXXXXXXX"
          mediaStreamTrack: MediaStreamTrack
...

Oh, I haven't checked this, but I think that is probably because you are using Twilio Video JS version 2 and localParticipant.videoTracks.values() actually returns a list of LocalTrackPublications not any of the objects it needs.

Try something like:

const tracks = Array.from(localParticipant.videoTracks.values()).map(trackPublication => trackPublication.track);

Let me know if that helps.

Thank you so much! I have built it completely done )

Great to hear!