Twilio可编程视频 - 在LocalVideoTrack上调用.disable()不会阻止它被传输

问题描述:

我试图让我的用户减少带宽使用,如果他们的呼叫通过禁用视频波涛汹涌。该文件说:Twilio可编程视频 - 在LocalVideoTrack上调用.disable()不会阻止它被传输

“静音或暂停单一媒体轨道

要控制静音/取消静音或LocalVideoTrack单LocalAudioTrack的暂停/取消暂停状态,您可以使用LocalTrack#启用和LocalTrack#禁用方法 “

但是,当我使用这个,本地媒体元素变黑(即它停止渲染),但远程流(打开在不同的窗口)仍然收到视频。我正在使用的代码如下。

createLocalVideoTrack().then(track => { 
      var localMediaContainer = document.getElementById(self.local_vid_id); 
      var title = document.createElement('span') 
      title.innerText = "Me"; 
      localMediaContainer.appendChild(title); 
      var videoIcon = document.createElement('span') 
      videoIcon.className = 'glyphicon glyphicon-facetime-video'; 
      videoIcon.title = 'Disable Video'; 
      videoIcon.videoTrack = track; 
      videoIcon.onclick = (event) => { 
       if (event.target.videoTrack.isEnabled) { 
        event.target.videoTrack.disable(); 
        event.target.title = 'Enable Video'; 
       } else { 
        event.target.videoTrack.enable(); 
        event.target.title = 'Disable Video'; 
       } 
      } 
      localMediaContainer.appendChild(videoIcon); 
      localMediaContainer.appendChild(track.attach()); 
     }); 

有没有其他人遇到过这个问题,有没有简单的解决方法?

+0

您好创建的轨道,你不应该依然能够看到遥控器上的流视频。您能否与[Twilio支持](https://www.twilio.com/help/contact)联系并描述此问题,以便视频小组可以对其进行调查?谢谢。 – philnash

+0

@philnash我设法找到记录的方式来做到这一点是通过调用'removeTrack'。没有任何方法可以创建新的视频轨道(重新启用它),而无需重新连接到房间,所以我认为这可能是与团队讨论的问题。 – thequickbrownfox

+0

嘿,是的,删除轨道完全从连接中删除轨道,并需要完全重新连接才能重新开始。禁用它不应该导致这种成本,但如果它不工作,这是没有用的!我让团队意识到了这个问题。谢谢 – philnash

在这里回答我自己的问题,但希望其他人会发现它有用。

您需要移除videoTrack以停止发送。 我使用的代码的最终版本是

videoIcon.onclick = function(event) { 
    if (event.target.videoTrack){ 
    self.room.localParticipant.videoTracks.forEach((track) => { 
     self.room.localParticipant.removeTrack(track,true); 
    }) 
    trackRemoved(event.target.videoTrack); 
    event.target.videoTrack.stop(); 
    event.target.title = 'Enable Video'; 
    event.target.videoTrack = undefined; 
    } else { 
    // Totally reconnect to the room 
    self.stop(); 
    self.reconnect(); 
    self.startPreview(); 
    } 
} 

的潜在的问题是最有可能调用connect(token, options)功能,当你不使用新创建的本地轨道。因此,当您在options中未指定tracks时,它将使用不同的ID创建新的本地曲目。因此,您通过createLocalVideoTrack()createLocalTracks()功能创建的本地视频轨道看到本地视频,并通过在connect()功能期间创建的完全不同的本地视频轨道将视频数据发送给远程参与者。

因此,为了克服这个问题,您应该在选项中指定创建的曲目,以便使用相同的曲目或获取从connect()函数创建的曲目。之后,如果您拨打disable()功能,它将在通话的双方静音。

选项1 - 指定曲目。

const { connect, createLocalTracks } = require('twilio-video'); 

createLocalTracks({ 
    audio: true, 
    video: true 
}).then(localTracks => { 
    return connect('$TOKEN', { 
    name: 'my-room-name', 
    tracks: localTracks 
    }); 
}).then(room => { 
    console.log('Connected to Room:', room.name); 
    localTracks.forEach((track)=>{ 
    // hide camera after 5 seconds 
    if(track.kind === 'video'){ 
     setTimeout(()=>{ 
     track.disable(); 
     } ,5000) 
    } 
    }) 
}); 

选项1 - 使用连接

Twilio.Video.connect('$TOKEN', {name:'my-new-room'}).then(function(room) { 
    console.log('Successfully joined a Room: ', room); 
    room.localParticipant.tracks.forEach((track)=>{ 
    // hide camera after 5 seconds 
    if(track.kind === 'video'){ 
     setTimeout(()=>{ 
     track.disable(); 
     } ,5000) 
    } 
    }); 
    room.on('participantConnected', function(participant) { 
    console.log('A remote Participant connected: ', participant); 
    }) 
}, function(error) { 
    console.error('Unable to connect to Room: ' + error.message); 
});