2017-06-15 31 views
0

iOS(10)アプリでWebRTCデータチャンネルでデータ転送機能を実装しようとしています。WebRTC DataChannelがiOSネイティブアプリで開けない

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") 
    } 
} 

これは出力を生成します:

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 
これはスウィフトの私のテストコードです

... 現在 (https://github.com/webrtc/samples/blob/gh-pages/src/content/datachannel/datatransfer/js/main.js)ローカル転送にoficialサンプルを再実装しようとするが、私のデータチャネルが開かれたことはありません

私はOne to many webrtcからコードを試してみましたが、シグナリングのためのWebSocketサーバーを使っていましたが、datacannelも開けませんでした。 どうしたのですか?

答えて

0

提供されたコードに従って、Offerを作成した後にデータチャネルを作成しています。

提供元/返信SDPにデータチャネルの詳細が含まれるように、データチャネルをpeerConnectionに追加した後で提供者を作成します。

RtpDataChannelsは推奨されていないため、制約の中で指定する必要はありません。

+0

ご協力いただきありがとうございます。しかし、もし私がピア接続を作成した後にdatacannelを作成すると、私は "shouldNegotiate"デリゲートコールしか持っていません。 "作成されたローカル接続 shouldNegotiate 作成されたリモート接続' – ZhuchoG

+0

データチャンネルの後にオファーを作成する必要があります。 – Ajay

+0

こんにちは、iOS 9でチャンネルを開くことはできますが、10でまだチャンネルを開くことはできません。 iOS 10のサポートに関する記事はありますか?データ通信が10で全く機能しないようですね? – ZhuchoG

関連する問題