远程流
最后更新于:2022-04-02 03:29:59
[TOC]
## 入门
一旦将`RTCPeerConnection`连接到远程对等方,便可以在它们之间流式传输音频和视频
```
const localStream = await getUserMedia({vide: true, audio: true});
const peerConnection = new RTCPeerConnection(iceConfig);
localStream.getTracks().forEach(track => {
peerConnection.addTrack(track, localStream);
});
```
## 添加远程轨道
为了接收其他对等方添加的远程轨道,我们在本地`RTCPeerConnection`上注册了一个侦听器,以监听`track`事件。
```
const remoteStream = MediaStream();
const remoteVideo = document.querySelector('#remoteVideo');
remoteVideo.srcObject = remoteStream;
peerConnection.addEventListener('track', async (event) => {
remoteStream.addTrack(event.track, remoteStream);
});
```
';