const peerConfig = {
  peerHost: '101.200.151.183',
  peerPort: 9000,
  peerPath: '/',
  stunServer: 'stun:101.200.151.183:3478',
  turnServer: 'turn:101.200.151.183:3478',
  turnUserName: 'aaaaa',
  turnPassword: 'bbbbb'
}

const initPeer = () => {
    return new Peer({
        //peer server 是用此域名的证书启动的，所以这里用域名
        host: peerConfig.peerHost,
        port: peerConfig.peerPort,
        path: peerConfig.peerPath,
        secure: false,
        config: {
            iceServers: [
                { urls: peerConfig.stunServer },
                {
                    urls: peerConfig.turnServer,
                    username: peerConfig.turnUserName,
                    credential: peerConfig.turnPassword
                }
            ]
        }
    })
}

const PeerCmd = {
    /**
     * 请求视频通话
     */
    request: 1,
    /**
     * 拒绝视频通话
     */
    reject: 2,
    /**
     * 接受视频通话
     */
    accept: 3,
    /**
     * 取消视频通话
     */
    cancel: 4,
    /**
     * 正常挂断
     */
    ringOff: 5,
    /**
     * 忙碌挂断
     */
    busy: 6
}

const SendVideoCode = {
    //视频通话
    VIDEO: '8',
    //关闭视频
    CLOSE_ALL: '9'
}


const getLocalUserMedia = (constrains) => {
    if (window.navigator.mediaDevices.getUserMedia) {
        return window.navigator.mediaDevices.getUserMedia(constrains)
    }
    throw new Error('unable to get user media')
}

const getQueryVariable = (variable) => {
    let query = window.location.search.substring(1);
    let vars = query.split("&");
    for (let i = 0; i < vars.length; i++) {
        let pair = vars[i].split("=");
        if (pair[0] === variable) {
            return pair[1];
        }
    }
    return "";
}
