2
votes

I'm trying to achieve audio calling with WebRTC. The pods I have are:

pod 'Starscream', '~> 2.0.4'
pod 'libjingle_peerconnection'

When I try to send SDP received as an answer, it got failed. I'm here providing selected lines of code as it's too lengthy.

  1. Connect Socket

    self.socket?.connect()
    
  2. As socket get connected status, initialize WebRTC

    func initalizeWebRTC() -> Void {
    print("----------------initalizeWebRTC----------------")
    
    RTCPeerConnectionFactory.initializeSSL()
    self.webRtcClient  = RTCPeerConnectionFactory.init()
    let stunServer = self.defaultStunServer()
    let defaultConstraint = self.createDefaultConstraint()
    self.peerConnection = self.webRtcClient?.peerConnection(withICEServers: [stunServer], constraints: defaultConstraint, delegate: self)
    
    self.localVideoView.delegate = self
    self.remoteVideoView.delegate = self
    // webrtc initalized local rendering of video on
    self.addLocalMediaStrem()
    
    }
    
  3. Here are my configurations

    func defaultStunServer() -> RTCICEServer {
    print("----------------defaultStunServer----------------")
    let url = URL.init(string: stunServer);
    let iceServer = RTCICEServer.init(uri: url, username: "", password: "")
    return iceServer!
    }
    
    func createAudioVideoConstraints() -> RTCMediaConstraints{
    print("----------------createAudioVideoConstraints----------------")
    let audioOffer : RTCPair = RTCPair(key: "OfferToReceiveAudio", value: "true")
    let videoOffer : RTCPair = RTCPair(key: "OfferToReceiveVideo", value: "false")
    let dtlsSrtpKeyAgreement : RTCPair = RTCPair(key: "DtlsSrtpKeyAgreement", value: "true")
    
    let connectConstraints : RTCMediaConstraints = RTCMediaConstraints.init(mandatoryConstraints: [audioOffer,videoOffer], optionalConstraints: [dtlsSrtpKeyAgreement])
    
    return connectConstraints
    }
    
    func createDefaultConstraint() -> RTCMediaConstraints {
    print("----------------createDefaultConstraint----------------")
      let dtlsSrtpKeyAgreement : RTCPair = RTCPair(key: "DtlsSrtpKeyAgreement", value: "true")
    let connectConstraints : RTCMediaConstraints = RTCMediaConstraints.init(mandatoryConstraints: nil, optionalConstraints: [dtlsSrtpKeyAgreement])
    
    return connectConstraints
    }
    
  4. Create offer using WebRTC

    self.peerConnection?.createOffer(with: self, constraints: constraint)
    
  5. Write data to socket

    let offerDict =  ["id":"joinRoomPresenter","name":fileName,"roomName":fileName,"isFrontCamera":"false","isMicroPhone":"false","isPhoneAudio":"false","isChat":"false","isOneToOneSession":"false","accessToken":"9289010e-d2d5-42e8-a95c-212f06aa9238","userId":"3459","fileName":fileName,"portalId":voiceCallID,"personName":fileName,"photoURL":"","sdpOffer":sdp.description] as [String : Any]
    socket?.write(string: offerDict.json)
    
  6. On success, I received sip as a message from socket.

["sdpAnswer": v=0

o=- 544328641767753251 2 IN IP4 104.248.181.233

s=VideoRoom 2623889018315199

t=0 0

a=group:BUNDLE audio

a=msid-semantic: WMS janus

m=audio 9 UDP/TLS/RTP/SAVPF 111

c=IN IP4 104.248.181.233

a=recvonly

a=mid:audio

a=rtcp-mux

a=ice-ufrag:SJ8U

a=ice-pwd:72CaTpbKHUzwWj7rX64cGn

a=ice-options:trickle

a=fingerprint:sha-256 13:2A:57:AA:FC:AE:2B:44:86:1A:FD:FD:77:4A:11:BD:78:60:A3:4E:D1:98:3C:43:1C:71:82:C7:88:EE:01:CC

a=setup:active

a=rtpmap:111 opus/48000/2

a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level

a=msid:janus janusa0

a=ssrc:1319411670 cname:janus

a=ssrc:1319411670 msid:janus janusa0

a=ssrc:1319411670 mslabel:janus

a=ssrc:1319411670 label:janusa0

a=candidate:1 1 udp 2013266431 104.248.181.233 57307 typ host

a=candidate:2 1 udp 2013266430 10.46.0.9 56250 typ host

a=end-of-candidates

, "id": joinRoomPresenterResponse, "response": accepted]

  1. I send the same sdp as an answer

    self.peerConnection?.setRemoteDescriptionWith(self, sessionDescription: rtcSessionDesc!)
    
  2. I got the iceGathering state Changed to 2 (gathering). but after some seconds, iceConnection state Changed to 4 (failed)

Where I'm wrong?

1
Did you used the new library GoogleWebRTC pod or still working with the pod 'libjingle_peerconnection' ?? and were you successful in sending the DataMuhammad Faizan

1 Answers

3
votes

For starters, libjingle is pretty old (2016). I would try the GoogleWebRTC pod, it is constantly updated from their repo.

In the socket send you have "isMicroPhone":"false","isPhoneAudio":"false" If it's a voice shouldn't those be true?

I would try Googles main example app and go from there. Getting all the steps correct with a webrtc connection is difficult sometimes.