twilio / twilio-video.js

Twilio’s Programmable Video JavaScript SDK
https://www.twilio.com/docs/video/javascript
Other
571 stars 217 forks source link

Retrieve remote participants mediaStreams #34

Closed iaptsiauri closed 7 years ago

iaptsiauri commented 8 years ago

Hello, As an initial setup I took the Ruby getting started application which uses the twillio video 1.0.0-beta2 version, as the remote participant connects to the room, the media is attached properly and is visible on the webpage, but what I want to achieve is to iterate other the participant.media.mediaStreams which appears to be empty.

  videoClient = new Twilio.Video.Client(data.token);
  videoClient.connect({ to: roomName, localMedia: previewMedia}).then(roomJoined,
      function(error) { 
          log('Could not connect to Twilio: ' + error.message);
  });
  previewMedia = new Twilio.Video.LocalMedia();
  Twilio.Video.getUserMedia().then(
  function (mediaStream) {
    previewMedia.addStream(mediaStream);
    previewMedia.attach('#local-media');
  },
  function (error) {
    console.error('Unable to access local media', error);
    log('Unable to access Camera and Microphone');
  });

   room.on('participantConnected', function (participant) {
    var participantMedia = participant.media;

    participantMedia.attach('#remote-media');
    participantMedia.mediaStreams.forEach(function(stream) {
      log('Logging stream');
    })
  });
markandrus commented 8 years ago

Hi @iaptsiauri,

We'll look into this to see if it is a bug or not. You should be able to iterate over participant.media.mediaStreams; however, you must iterate once Tracks have been added. Initially, the media may be "empty", i.e. contain no Tracks, which would imply there are no MediaStreams either.

One thing you can try is to gate your code on the "trackAdded" event, e.g.

participant.media.once('trackAdded', () => {
  console.log(participant.media.mediaStreams.size);
  // => should be at least 1 now
});

or, similarly,

participant.media.once('trackAdded', track => {
  // Log stream
  console.log(track.mediaStream);
});

Best, Mark

iaptsiauri commented 8 years ago

Hi @markandrus Thanks, for the suggestion. Will use that approach at the moment and looking forward for the updates on this matter 👍

markandrus commented 7 years ago

Hi @iaptsiauri,

We do not believe this is a bug—it is possible that a Participant initially connects and your application has not yet received any Tracks from them. Instead, they must be listened for via the "trackAdded" event.