WebRTC приложение для видеоконференций "многие ко многим" - PullRequest
0 голосов
/ 22 октября 2018

Я интегрирую функцию видеоконференции в свое приложение iOS с помощью WebRTC.Ниже приведен код, который писал:

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
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...