我正在尝试使用 WebRTC 实现音频通话。我拥有的豆荚是:
pod 'Starscream', '~> 2.0.4'
pod 'libjingle_peerconnection'
当我尝试发送收到的 SDP 作为答案时,它失败了。我在这里提供选定的代码行,因为它太长了。
连接插座
self.socket?.connect()
随着套接字获得连接状态,初始化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() }
这是我的配置
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 }
使用 WebRTC 创建报价
self.peerConnection?.createOffer(with: self, constraints: constraint)
将数据写入套接字
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)
成功后,我收到了来自套接字的 sip 消息。
[“sdpAnswer”:v=0
o=- 544328641767753251 2 IN IP4 104.248.181.233
s=影音室2623889018315199
t=0 0
a=组:捆绑音频
a=msid 语义:WMS janus
m=音频 9 UDP/TLS/RTP/SAVPF 111
c=IN IP4 104.248.181.233
a=recvonly
a=中:音频
a=rtcp 多路复用器
a=冰-ufrag:SJ8U
a=ice-pwd:72CaTpbKHUzwWj7rX64cGn
a=冰选项:涓流
a=指纹: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=设置:活动
a=rtpmap:111 作品/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 标签:janusa0
a=候选人:1 1 udp 2013266431 104.248.181.233 57307 类型主机
a=候选:2 1 udp 2013266430 10.46.0.9 56250 类型主机
a=候选人结束
, "id": joinRoomPresenterResponse, "response": 接受]
我发送相同的 sdp 作为答案
self.peerConnection?.setRemoteDescriptionWith(self, sessionDescription: rtcSessionDesc!)
我将 iceGathering 状态更改为 2(正在收集)。但几秒钟后,iceConnection 状态变为 4(失败)
我哪里错了?