pion实现sfu之ice配置
在实现sfu服务器时,由于sfu服务通常运行在监听公网地址的机器上,并不需要实现完整的ice candidate收集流程(host, relay, srvflx candidate)。因为公网的host candidate是客户端可以直接访问的,在ice握手过程中,也可以简单的作为controlled方,响应客户端的bind请求即可,这种模式成为ice-lite,可以大大简化sfu服务的ice实现,并且可以减少ice握手的交互流程,加快链接建立的时间。
关于ice-lite的介绍:
ice-lite
Last updated: 06 September 2019ice-lite is a minimal version of the ICE specification, intended for servers running on a public IP address.
ice-lite is easy to implement, requiring the media server to only answer incoming STUN binding requests and acting as a controlled entity in the ICE process itself. This simplicity makes it quite popular among implementations of SFUs and other media servers.
Support for ice-lite is announced in the SDP as a=ice-lite.
https://webrtcglossary.com/ice-lite/
在pion/webrtc中,我们可以这样设置sfu的ice配置:
se = webrtc.SettingEngine{} //启用ice-lite,设置为ice-lite模式后,不可以再添加ICEServer配置 se.SetLite(true) if len(cfg.ICEServers) > 0 { log.Warn("ice servers will be ignored when icelite enabled") cfg.ICEServers = []ICEServer{} } // 过滤docker的candidate,如果还有其他interface不希望暴露,也可以过滤掉 se.SetInterfaceFilter(func(ifs string) bool { if strings.Contains(ifs, "docker") { return false } return true }) //配置nat ip映射, if len(cfg.NAT1to1Ips) > 0 { se.SetNAT1To1IPs(cfg.NAT1to1Ips, webrtc.ICECandidateTypeHost) } api = webrtc.NewAPI(webrtc.WithMediaEngine(me), webrtc.WithSettingEngine(se))
需要注意的是NAT1To1Ips这个配置,因为当sfu服务运行在云服务器上时(aws,阿里云,腾讯云等),云主机是在nat后面的,本机收集到的candidate是内网地址,需要映射为公网地址,客户端才能访问。
例如服务器外网ip是2.2.2.2,内网ip是192.168.0.1;使用se.SetNAT1To1Ips([]{“2.2.2.2”}, webrtc.ICECandidateTypeHost)来进行映射。
经过上述配置之后,我们可以看到服务器收集到的ice candidate非常精简,仅有两个host类型的candidate(rtp rtcp),简化了ice握手的流程和信令传输过程。