How to handle audio stream in JsSIP?

。_饼干妹妹 提交于 2020-01-01 03:44:07

问题


I'm creating React application that use JsSIP library to answer calls made via VoIP SIP provider.

I've already created a page that have two buttons (Accept and Reject). It successfully register SIP client on SIP-server. It also successfully receive call and I can answer it. But I don't hear anything while answering call.

Registering JsSIP client (in willReceiveProps because I have information for connection after props changing):

const socketHost = 'wss://' + contactCenter.host + ':' + contactCenter.port
const socket = new JsSIP.WebSocketInterface(socketHost)
const configuration = {
    sockets: [socket],
    uri: 'sip:' + contactCenter.login + '@' + contactCenter.host,
    password: contactCenter.password,
    socketHost: socketHost,
}

const coolPhone = new JsSIP.UA(configuration)

coolPhone.on('connected', (e: any) => {
    const messages = ServiceContainer.get<MessageManagerInterface>(ServiceTypes.Messages)
    messages.addSuccess('SIP connected')
})

coolPhone.on('newRTCSession', (e: any) => {
    const messages = ServiceContainer.get<MessageManagerInterface>(ServiceTypes.Messages)
    messages.addAlert('New call')

    const session = e.session

    session.on('failed', this.resetLocalState)
    session.on('ended', this.resetLocalState)

    const numberRegexp = /\"(\d+)\"/
    const fromNumber = (numberRegexp.exec(e.request.headers.From[0].raw))[1]
    const toNumber = (numberRegexp.exec(e.request.headers.Contact[0].raw))[1].slice(1)

    this.setState({
        callReceived: true,
        callSession: session,
        fromNumber: fromNumber,
        toNumber: toNumber,
    })
})

coolPhone.start()

Method that handles answer button click:

private answerCall = () => {
    const messages = ServiceContainer.get<MessageManagerInterface>(ServiceTypes.Messages)
    messages.addSuccess('Call answered')

    const callOptions = {
        mediaConstraints: {
            audio: true, // only audio calls
            video: false
        },
        pcConfig: {
            iceServers: [
                { urls: ["stun:stun.l.google.com:19302"] }
            ],
            iceTransportPolicy: "all",
            rtcpMuxPolicy: "negotiate"
        }
    }

    this.state.callSession.answer(callOptions)

    this.state.callSession.connection.addEventListener('addstream', (event: any) => {
        console.log(event)
        this.audioElement.srcObject = event.stream
    })

    this.audioElement.play()

    this.setState({
        callAnswered: true,
        callReceived: false,
    })
}

What did I do wrong?


回答1:


I solved the problem.

The problem was in the position of this.audioElement.play() line.

I moved it to the callback on addstream event:

this.state.callSession.connection.addEventListener('addstream', (event: any) => {
    console.log(event)
    this.audioElement.srcObject = event.stream
    this.audioElement.play()
})

Now it works fine. Hope you also find it useful.




回答2:


You can use react-sip npm library which simplifies usage of jssip inside React apps: https://www.npmjs.com/package/react-sip

You will just need to pass your connection settings as props to <SipProvider/>, which will be somewhere near the top of your react tree. This will allow you to perform basic start/stop/answer operations and watch the status of your call in the context!



来源:https://stackoverflow.com/questions/44183387/how-to-handle-audio-stream-in-jssip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!