- 首先获取摄像头的流,请在 localhost 或者 https 环境下获取
navigator.mediaDevices.getUserMedia({video: true, audio: true}).then(stream => { })
- 创建RTCPeerConnection,并将获取到的流音频和视频轨道添加进去
const pc = new RTCPeerConnection(); stream.getTracks().forEach(track => { pc.addTrack(track) });
- 交换SDP,最重的一步
pc.createOffer().then(offer => { if (offer?.sdp) { let arr: string[] = offer?.sdp?.split('\r\n'); arr.forEach((str: any, i: any) => { if (/^a=fmtp:\d*/.test(str)) { // 这里面就控制码率 arr[i] = str + ';x-google-max-bitrate=15000;x-google-min-bitrate=5000;x-google-start-bitrate=5000'; } }); offer.sdp = arr?.join('\r\n') } pc.setLocalDescription(offer).then(() => { request.post("服务器接口/getSdp", { 其他参数, sdp: offer.sdp}).then((remoteInfo: any) => { pc.setRemoteDescription({sdp: remoteInfo.sdp, type: 'answer'}).then(() => { console.log('推流成功') }) }) }) })
- 关闭推送
pc.close()
© 版权声明
THE END
请登录后查看评论内容