我正在使用WebRTC在我的iOS应用中集成视频会议功能。下面是编写的代码:
var connections = [String : ClientChannel?]()
ClientChannel
是一个包含对等连接和相应远程视图的类
这是我创建和接收要约/答案的功能。
extension WebRTC {
func createOffer(`for` channelId: String) {
_createOffer(for: channelId)
}
func receiveOffer(`for` channelId: String, remoteSdp: NSDictionary) {
_receiveOffer(for: channelId, remoteSdp: remoteSdp)
}
func receiveAnswer(`for` channelId: String, remoteSdp: NSDictionary) {
_receiveAnswer(for: channelId, remoteSdp: remoteSdp)
}
func receiveCandidate(`for` channelId: String, candidate: NSDictionary) {
guard let candidate = candidate as? [AnyHashable:Any]
, let rtcCandidate = RTCIceCandidate(fromJSONDictionary: candidate) else {
print("invalid candiate")
return
}
if let channer = self.connections[channelId] {
channer?.peerConnection?.add(rtcCandidate)
}
}
fileprivate func _createOffer(`for` channelId: String) {
if let channer = self.connections[channelId] {
channer?.peerConnection?.offer(for: WebRTCUtil.mediaStreamConstraints(), completionHandler: { [weak channer] (description, error) in
guard let description = description else{
print("----- no description ----")
return;
}
//
channer?.peerConnection?.setLocalDescription(description, completionHandler: { (error) in
})
guard let callback = self.onCreatedLocalSdp, let localDescription = WebRTCUtil.jsonFromDescription(description: channer?.peerConnection?.localDescription) else {
print("no localDescription")
return
}
callback(localDescription)
self.onCreatedLocalSdp = nil
})
}
}
fileprivate func _receiveOffer(`for` channelId: String, remoteSdp: NSDictionary) {
guard let sdpContents = remoteSdp.object(forKey: "sdp") as? String else{
print("noSDp")
return;
}
let remoteSdp = RTCSessionDescription(type: .offer, sdp: sdpContents)
if let channer = self.connections[channelId] {
channer?.peerConnection?.setRemoteDescription(remoteSdp, completionHandler: { [weak channer] (error) in
channer?.peerConnection?.answer(for: WebRTCUtil.answerConstraints(), completionHandler: { [weak channer] (sdp, error) in
guard let sdp = sdp else {
print("can not create sdp")
return;
}
channer?.peerConnection?.setLocalDescription(sdp, completionHandler: { (error) in
guard let callback = self.onCreatedLocalSdp, let localDescription = WebRTCUtil.jsonFromDescription(description: channer?.peerConnection?.localDescription) else{
print("no localDescription")
return
}
callback(localDescription)
self.onCreatedLocalSdp = nil
})
})
})
}
}
fileprivate func _receiveAnswer(`for` channelId: String, remoteSdp: NSDictionary) {
guard let sdpContents = remoteSdp.object(forKey: "sdp") as? String else {
print("noSDp")
return;
}
let sdp = RTCSessionDescription(type: .answer, sdp: sdpContents)
if let channer = self.connections[channelId] {
channer?.peerConnection?.setRemoteDescription(sdp, completionHandler: { (error) in
print(error?.localizedDescription ?? "NA")
})
}
}
}
一对一工作正常,但是添加新用户后,它将停止工作。
任何人都可以帮助我。
创建对等连接:
func connect(with id: String, iceServerUrlList:[String], onCreatedLocalSdp:@escaping ((_ localSdp:NSDictionary)->()),didGenerateCandidate:@escaping ((_ localSdp:NSDictionary)->()), didReceiveRemoteStream:@escaping ((String)->())){
if connections[id] != nil {
print("#ALREADY EXIST#")
} else {
self.onCreatedLocalSdp = onCreatedLocalSdp
let configuration = RTCConfiguration()
configuration.iceServers = [RTCIceServer(urlStrings: iceServerUrlList)]
let channel = ClientChannel.init(with: id, didGenerateCandidate: didGenerateCandidate, didReceiveRemoteStream: didReceiveRemoteStream)
channel.peerConnection = factory.peerConnection(with: configuration, constraints: WebRTCUtil.peerConnectionConstraints(), delegate: channel)
channel.peerConnection?.add(localStream!)
connections[id] = channel
}
}