美文网首页
二十四、CreatOffer参数实例

二十四、CreatOffer参数实例

作者: ThemisHoo | 来源:发表于2019-05-17 10:16 被阅读0次

    基本格式
    promis = myPeerConnection.createOffer([options])

    Options 可选
    • iceRestart:该选项会重启ICE,重新进行Candidate收集
    • voiceActivityDetection:是否开启静音检测,默认开启
    • 接收远端音频
    • 接收远端视频
    
    // let videoPlayer = document.querySelector('video#player');
    let creatOffer = document.querySelector('button#createOffer')
    
    let pc = new RTCPeerConnection()
    let pc2 = new RTCPeerConnection()
    
    let gotMediaStream = (stream) => {
        stream.getTracks().forEach(track => {
            pc.addTrack(track)
        });
    
        let options = {
            offerToReceiveAudio: 0,
            offerToReceiveVideo: 1,
            iceRestart: true
        }
        // 媒体协商
        pc.createOffer(options)
            .then((desc) => {
                console.log('offer', desc.sdp)
                pc.setLocalDescription(desc)
                pc2.setRemoteDescription(desc)
    
                pc2.createAnswer().then((desc) => {
                    console.log('answer', desc.sdp)
                    pc2.setLocalDescription(desc)
                    pc.setRemoteDescription(desc)
                }).catch((err) => {
                    console.log(err)
                })
            })
            .catch(() => {
                console.log(err)
            })
    }
    let handleError = (err) => {
        alert(`getUserMedia errpr:${err}`)
        console.log(`getUserMedia errpr:${err}`)
    }
    
    let getStream = () => {
        let constrants = {
            video: {
                width: 640,
                height: 480,
                frameRate: 60,
                facingMode: 'environment',
                resizeMode: 'none'
            },
            audio: false
        }
        navigator.mediaDevices.getUserMedia(constrants)
        .then(gotMediaStream)
        .catch(handleError)
    }
    
    creatOffer.onclick = () => {
        if (!pc) {
            console.log('pc is null')
        }
        getStream()
        return
    }
    
    
    if(!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
        alert('getUserMedia is not supported!')
        console.log('getUserMedia is not supported!');
    }
    

    当iceRestart为true时,进行两次连接时,会重新收集candidate,a=ice-ufrag会改变

    相关文章

      网友评论

          本文标题:二十四、CreatOffer参数实例

          本文链接:https://www.haomeiwen.com/subject/cpinnqtx.html