1

我正在构建一个通过 websocket 和 webrtc 与 janus 网关一起使用的应用程序。一切正常,我可以成功发送和接收语音呼叫,但 insertDtmf 方法不会将我的 dtmf 发送给其他对等方。android中相同的帐户和相同的代码可以正常工作。这是我准备webrtc的地方

private func prepareWebRtc( callbacks:PluginHandleWebRTCCallbacksDelegate) {

    if (pc != nil) {

        if (callbacks.getJsep() == nil) {
          
            createSdpInternal(callbacks: callbacks, isOffer: isOffer)
        } else {
         

            let jsep = callbacks.getJsep()!
                let sdpString:String = jsep["sdp"] as! String
            let type:RTCSdpType = RTCSessionDescription.type(for: jsep["type"] as! String)
            let sdp:RTCSessionDescription =  RTCSessionDescription.init(type: type, sdp: sdpString)
     
            pc.setRemoteDescription(sdp) { (err) in}



            }
    } else {

        trickle = callbacks.getTrickle() != nil ? callbacks.getTrickle()! : false

        streamsDone(webRTCCallbacks: callbacks)


    }
}


private func streamsDone(webRTCCallbacks:PluginHandleWebRTCCallbacksDelegate) {


    let rtcConfig =  RTCConfiguration.init()
    rtcConfig.iceServers = server.iceServers
    rtcConfig.bundlePolicy = RTCBundlePolicy.maxBundle
    rtcConfig.rtcpMuxPolicy = RTCRtcpMuxPolicy.require
    rtcConfig.continualGatheringPolicy = RTCContinualGatheringPolicy.gatherContinually
    rtcConfig.sdpSemantics = .planB


    let source :RTCAudioSource = sessionFactory.audioSource(with: audioConstraints)
    
    let audioTrack:RTCAudioTrack? = sessionFactory.audioTrack(with: source, trackId: AUDIO_TRACK_ID)

    let stream:RTCMediaStream?  = sessionFactory.mediaStream(withStreamId: LOCAL_MEDIA_ID)
    
    if (audioTrack != nil){
        stream!.addAudioTrack(audioTrack!)
    myStream = stream
    }
           
    if (stream != nil){
        onLocalStream(stream: stream!)
    }
            

       // pc.addTrack(audioTrack, mediaStreamLabels);


    pc = sessionFactory.peerConnection(with: rtcConfig, constraints: audioConstraints, delegate: nil)

    if (myStream != nil){
        pc.add(myStream)
    }
    
    
    if  let obj:[String:Any] = webRTCCallbacks.getJsep(){
        
        let sdp:String = obj["sdp"] as! String
        
        let type:RTCSdpType = RTCSessionDescription.type(for: obj["type"] as! String)
        
        let sessionDescription:RTCSessionDescription =  RTCSessionDescription(type: type, sdp: sdp)

            print("  STREAMS DONE  JSEP NULL  DEĞİL")
        
         //   pc.setRemoteDescription(WebRtcObserver(webRTCCallbacks), sessionDescription);
        
         pc.setRemoteDescription(sessionDescription) { (err) in

        }
    }else{
        createSdpInternal(callbacks: webRTCCallbacks, isOffer: isOffer)
        print("  STREAMS DONE  JSEP NULL ");
    }
    

      
       /* } catch (Exception ex) {
            webRTCCallbacks.onCallbackError(ex.getMessage());
        }*/
  
}

在这里我尝试发送 dtmf

public func insertDTMF(_ tone:String){
    if(pc != nil){
      

        if let dtmfSender = pc.senders.first?.dtmfSender{
                dtmfSender.insertDtmf(tone, duration: 200, interToneGap: 70)
            }
           //Here the timers are in ms
     
    }
}
4

1 回答 1

1

就我而言,这就是我处理插入 DTMF 功能的方式。

a - 首先过滤掉音频 RTCRtpSender 轨道:

var audioSender: RTCRtpSender?

for rtpSender in pc.senders {   
  if rtpSender.track?.kind == "audio" {    
    audioSender = rtpSender   
  } 
}

b - 然后使用相同的过滤后的 audioSender 对象使用 OperationQueue 插入音调

if let audioSender = audioSender {
   let queue = OperationQueue()
   queue.addOperation({
     audioSender.dtmfSender?.insertDtmf(dtmfTone, duration: TimeInterval(0.1),interToneGap: TimeInterval(0.5))
   })
}

注意:您可以根据需要修改持续时间和 interToneGap。

希望此解决方案也适用于您。

原始答案可以在这里找到:https ://stackoverflow.com/a/60148372/4515269

于 2021-07-05T20:32:50.240 回答