Search code examples
swiftwebrtc

Setting an RTCPeerConnections answer SessionDescription gives EXC_BAD_ACCESS on iOS


I am building a native webRTC app on iOS using Googles WebRTC pod in Swift. I am using a custom web socket to an OpenVidu server to send and receive information needed to establish an RTC video chat

However, upon trying to set a session description answer for a peer connection, the application throws an EXC_BAD_ACCESS signal which crashes it.

I first call a function to create and set the offer as a local description:

func createOfferForPublishing(constraints:RTCMediaConstraints){
        localParticipant?.peerConnection?.offer(for: constraints, completionHandler: {
            sdp, error in
            if let error = error {
                print(error)
                return
            }
            self.localParticipant?.peerConnection?.setLocalDescription(sdp!, completionHandler:     {
                error in
                if let error=error{
                    print(error)
                    return
                }
                self.openViduSocket?.publishVideo(sessionDescription: sdp!)
            })
        })
    }

which then gets passed to the socket and receives a json response with an answer

if let connection = session.localParticipant?.peerConnection{
                    let remoteSdpAnswer = RTCSessionDescription(type: .answer, sdp: params.sdpAnswer)
                    connection.setRemoteDescription(remoteSdpAnswer)
                }

I then create a session description from a string and set it as the remote description, at which point the app crashes with an EXC_BAD_ACCESS

I have found this bug report but in the linked fix RTCSessionDescription initialiser should return nil if the sdp string is invalid, which it doesn't do, but rather returns a normal answer

My questions are:

  • Is there any way work around this crash in swift?
  • Is there a way to fix this crash by changing the framework from a manual download of the SDK?

Solution

  • After looking through someone else's demo app, I figured out that these methods require completion handlers despite the arguments being optional.

    The call should instead look something like this:

    connection.setRemoteDescription(remoteSdpAnswer, completionHandler: {
                            error in
                            if let error = error {
                                print(error)
                                return
                            }
                        })