WebRtc之HTML推流控制码率范围

  1. 首先获取摄像头的流,请在 localhost 或者 https 环境下获取
    navigator.mediaDevices.getUserMedia({video: true, audio: true}).then(stream => {
    
    })
  2. 创建RTCPeerConnection,并将获取到的流音频和视频轨道添加进去
    const pc = new RTCPeerConnection();
    stream.getTracks().forEach(track => {
         pc.addTrack(track)
    });
  3. 交换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('推流成功')
                        })
                    })
                })
            })
  4. 关闭推送
    pc.close()
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片快捷回复

    请登录后查看评论内容