IOS Swift WebRtc insertDtmf 问题

IOS Swift WebRtc insertDtmf issue

我正在构建一个通过 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
     
    }
}

就我而言,这就是我处理插入 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))
   })
}

注意:duration和interToneGap可以根据自己的需要修改。

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

原始答案可以在这里找到: