发送一个dtmf,搞了两天,我有什么办法,我也很绝望啊,sip与webrtc不可兼得
在建立PeerConnection
时,会创建一系列的RtpSender
,可通过RtpSender.track().kind()
方法来确认我们所需的RtpSender,dtmf的发送需要kind为audio的RtpSender,拿到后再通过RtpSender.dtmf()
获取到我们需要的DtmfSender
就可以了
public DtmfSender getDtmfSender() {
for (RtpSender sender : peerConnection.getSenders()) {//只有kind为audio的RtpSender存在dtmf,所以这里随便判断一下就好
if (sender.track() != null && sender.dtmf() != null) {
return sender.dtmf();
}
}
return null;
}
拿到后的操作就更简单了
DtmfSender dtmfSender = getDtmfSender();//一个信令对应一个sender
if (dtmfSender != null && dtmfSender.canInsertDtmf()) {
dtmfSender.insertDtmf(digits, 200, 50);//参数为信令、信令持续时间、音调间隙
}
信令发送就这么简单,需要注意的是每发送一次dtmf信号,RtpSender都是会变化的,也就是说DtmfSender需要新获取
贴一下源码中的注释
/**
* Queues a task that sends the provided DTMF tones.
* <p>
* If insertDtmf is called on the same object while an existing task for this
* object to generate DTMF is still running, the previous task is canceled.
*
* @param tones This parameter is treated as a series of characters. The characters 0
* through 9, A through D, #, and * generate the associated DTMF tones. The
* characters a to d are equivalent to A to D. The character ',' indicates a
* delay of 2 seconds before processing the next character in the tones
* parameter. Unrecognized characters are ignored.
* @param duration Indicates the duration in ms to use for each character passed in the tones
* parameter. The duration cannot be more than 6000 or less than 70.
* @param interToneGap Indicates the gap between tones in ms. Must be at least 50 ms but should be
* as short as possible.
* @return true on success and false on failure.
*/
public boolean insertDtmf(String tones, int duration, int interToneGap) {
checkDtmfSenderExists();
return nativeInsertDtmf(nativeDtmfSender, tones, duration, interToneGap);
}
这是我们用到的核心方法,注释中写的很清楚,就不多做解释了
参考 webRTC android 官方文档
虽然webRTC已经出现很多年了,但资料不多,web还好,android这一块主要还是靠官方文档活着