WebRTC RTCSessionDescription swift

时间:2017-05-20 20:16:10

标签: swift webrtc

我正在尝试使用swift中的WebRTC框架编写本机iOS应用程序。我遇到了一个问题,因为看起来RTCSessionDescriptonDelegate已被弃用,我不能再真正遵循这个tutorial

我想我已经走得很远但是阻碍我的主要原因是我不明白如何创建一个sdp参数。 RTCSessionDescription(类型:.offer,sdp:)

如果有人可以提供建议我会非常感激。

1 个答案:

答案 0 :(得分:0)

Swift 3.0

这是我创建SDP的解决方案。

    let constraints: RTCMediaConstraints? = defaultPeerConnectionConstraints()
    peerConnection: RTCPeerCo = factory?.peerConnection(withICEServers: iceServers, constraints: constraints, delegate: self)
    let localStream: RTCMediaStream? = createLocalMediaStream() //get Media stream(video/audio)
    peerConnection?.add(localStream)

    //create offer for other opponent        

    if isInitiator {
        peerConnection?.createOffer(with: self, constraints: defaultOfferConstraints())
        print("create Offer")
    }
在ICE服务器上

func defaultICEServer() -> [RTCICEServer] {

  var iceServers = [RTCICEServer]()
  let defaultSTUNServerURL = URL(string: Config.defaultSTUNServerUrl)
  let defaultTURNServerURL = URL(string: Config.defaultTURNServerUrl)
  let stunServ = RTCICEServer(uri: defaultSTUNServerURL, username: "", password: "")
  let turnServ = RTCICEServer(uri: defaultTURNServerURL, username: "admin", password: "admin")
  iceServers.append(stunServ!)
  iceServers.append(turnServ!)

  return iceServers
}

 func defaultPeerConnectionConstraints() -> RTCMediaConstraints {
    let optionalConstraints: [Any] = [RTCPair(key: "DtlsSrtpKeyAgreement", value: "true")]
    let constraints = RTCMediaConstraints(mandatoryConstraints: nil, optionalConstraints: optionalConstraints)
    return constraints!
}

然后是呼叫代表

func peerConnection(_ peerConnection: RTCPeerConnection, didCreateSessionDescription sdp: RTCSessionDescription, error: Error?) {}

您可以使用sdp发送其他用户。您应该将此sdp设置为localDescription以获得正确的工作。

peerConnection?.setLocalDescriptionWith(self, sessionDescription: sdp)

我希望,我可以帮助