2017-06-15 2 views
0

Ich versuche, Datenübertragung Funktionalität mit WebRTC Datachannels in iOS (10) App zu implementieren. versuchen Derzeit oficial Probe mit lokalem Transfer (https://github.com/webrtc/samples/blob/gh-pages/src/content/datachannel/datatransfer/js/main.js) aber mein Datenkanal nicht geöffnet ...WebRTC DataChannel nicht auf iOS native App öffnen

Dies ist mein Test-Code in Swift neu zu implementieren:

import Foundation 
import WebRTC 
import RealmSwift 

class StreamingAPIWrapper { 
    internal static let sharedInstance = StreamingAPIWrapper() 

    var localConnection: RTCPeerConnection? 
    var remoteConnection: RTCPeerConnection? 
    var sendChannel: RTCDataChannel? 
    var reciveChannel: RTCDataChannel? 

    let dataChannelDelegate = MyRTCDataChannelDelegate() 
    let factory = RTCPeerConnectionFactory() 


    class func initStreamingAPI() { 
     RTCInitializeSSL() 
    } 

    class func deinitStreamingAPI() { 
     RTCCleanupSSL() 
    } 

    func connect() { 

     let configuration = RTCConfiguration() 
     configuration.bundlePolicy = .balanced 
     let server = RTCIceServer(urlStrings: ["stun:stun.l.google.com:19302"]) 
     configuration.iceServers = [server] 

     let mandatoryConstraints = ["OfferToReceiveAudio": "true", "OfferToReceiveVideo": "true"] 
     let optionalConstraints = [ "DtlsSrtpKeyAgreement": "true", "RtpDataChannels" : "true", "internalSctpDataChannels" : "true"] 
     let constraints = RTCMediaConstraints(mandatoryConstraints: mandatoryConstraints, optionalConstraints: optionalConstraints) 

     let localConnectionDelegate = MyRTCPeerConnectionDelegate() 
     localConnectionDelegate.onGenerateCandidate = { candidate in 
      self.remoteConnection!.add(candidate) 
     } 
     localConnection = factory.peerConnection(with: configuration, 
               constraints: constraints, 
               delegate: localConnectionDelegate) 
     print("Created local connection") 

     let remoteConnectionDelegate = MyRTCPeerConnectionDelegate() 
     remoteConnectionDelegate.onGenerateCandidate = { candidate in 
      self.localConnection!.add(candidate) 
     } 
     remoteConnection = factory.peerConnection(with: configuration, 
                constraints: constraints, 
                delegate: remoteConnectionDelegate) 
     print("Created remote connection") 

     localConnection?.offer(for: constraints, completionHandler: { (description1, _) in 
      self.localConnection?.setLocalDescription(description1!, completionHandler: { _ in }) 
      self.remoteConnection?.setRemoteDescription(description1!, completionHandler: { _ in }) 
      self.remoteConnection?.answer(for: constraints, completionHandler: { (description2, error) in 
       self.localConnection?.setRemoteDescription(description2!, completionHandler: { _ in }) 
       self.remoteConnection?.setLocalDescription(description2!, completionHandler: { _ in }) 
      }) 
     }) 

     let dataChannelConfiguration = RTCDataChannelConfiguration() 
     dataChannelConfiguration.isOrdered = true 
     sendChannel = localConnection?.dataChannel(forLabel: "test", configuration: dataChannelConfiguration) 
    } 
} 

class MyRTCPeerConnectionDelegate: NSObject, RTCPeerConnectionDelegate { 

    var onConnect: (() ->())? 
    var onGenerateCandidate: ((RTCIceCandidate) ->())? 
    var onOffer: (() ->())? 

    func peerConnection(_ peerConnection: RTCPeerConnection, didAdd stream: RTCMediaStream) { 
     print("didAddStream") 
    } 
    func peerConnection(_ peerConnection: RTCPeerConnection, didRemove stream: RTCMediaStream) { 
     print("didRemoveStream") 
    } 
    func peerConnection(_ peerConnection: RTCPeerConnection, didOpen dataChannel: RTCDataChannel) { 
     print("didOpenDataChannel") 
    } 
    func peerConnection(_ peerConnection: RTCPeerConnection, didGenerate candidate: RTCIceCandidate) { 

     onGenerateCandidate?(candidate) 

     print("didGenerateCandidate") 
    } 
    func peerConnection(_ peerConnection: RTCPeerConnection, didRemove candidates: [RTCIceCandidate]) { 
     print("didRemoveCandidates") 
    } 
    func peerConnection(_ peerConnection: RTCPeerConnection, didChange newState: RTCIceGatheringState) { 
     print("didChangeIceGatheringState") 
    } 
    func peerConnection(_ peerConnection: RTCPeerConnection, didChange newState: RTCIceConnectionState) { 
     print("didChangeIceConnectionState \(newState.rawValue)") 
    } 
    func peerConnection(_ peerConnection: RTCPeerConnection, didChange stateChanged: RTCSignalingState) { 
     print("didChangeSignalingState \(stateChanged.rawValue)") 
    } 
    func peerConnectionShouldNegotiate(_ peerConnection: RTCPeerConnection) { 
     print("shouldNegotiate") 
    } 
} 

class MyRTCDataChannelDelegate: NSObject, RTCDataChannelDelegate { 
    func dataChannel(_ dataChannel: RTCDataChannel, didReceiveMessageWith buffer: RTCDataBuffer) { 
     print("didReciveMessage") 
    } 
    func dataChannel(_ dataChannel: RTCDataChannel, didChangeBufferedAmount amount: UInt64) { 
     print("didChangeAmount") 
    } 
    func dataChannelDidChangeState(_ dataChannel: RTCDataChannel) { 
     print("didChangeDataChannel") 
    } 
} 

Diesen erzeugen Ausgabe:

Created local connection 
Created remote connection 
didChangeSignalingState 1 
didChangeSignalingState 3 
shouldNegotiate 
didChangeIceGatheringState 
didGenerateCandidate 
didGenerateCandidate 
didGenerateCandidate 
didGenerateCandidate 
didChangeSignalingState 0 
didChangeIceConnectionState 1 
didChangeSignalingState 0 
didChangeIceConnectionState 1 
didChangeIceGatheringState 

Ich versuche auch Code von One to many webrtc, mit WebSockets Server für die Signalisierung, aber auch Datachannel nicht geöffnet. Was habe ich falsch gemacht?

Antwort

0

Gemäß dem bereitgestellten Code erstellen Sie nach dem Erstellen von Offer einen Datenkanal.

Erstellen Sie das Angebot nach dem Hinzufügen von dataChannel zu peerConnection,
, so dass Angebot/Antwort SDP die Datenkanaldetails enthalten wird.

RtpDataChannels sind veraltet, daher müssen Sie dies nicht in Constraints angeben.

+0

Vielen Dank für Ihre Unterstützung. Aber, wenn ich die Erstellung von Datachannel nach dem Erstellen von Peerconnection verschieben, dann habe ich nur "shouldNegotiate" Delegat Anruf 'Created local connection shouldNegotiate Erstellte Remote-Verbindung ' – ZhuchoG

+0

Sie müssen das Angebot, gefolgt von dem Datenkanal erstellen. – Ajay

+0

Hallo, ich kann Kanal auf iOS 9 öffnen, aber auf 10 kann immer noch nicht. Gibt es Papiere über iOS 10-Unterstützung? Scheint Datakanäle auf 10 überhaupt nicht zu arbeiten? – ZhuchoG