我正在开发WebRtc原生Android应用程序。我也在编译io.pristine库。只有当两个设备都连接到wifi时,我才能建立通话。如果其中一个设备连接到移动网络,则无法建立电话。我已经阅读了所有可能的论坛,看起来我需要TURN服务器。我已经运行了自己的TURN服务器,但我不知道如何强制应用程序使用此服务器。任何帮助都欢迎。谢谢!
我正在开发WebRtc原生Android应用程序。我也在编译io.pristine库。只有当两个设备都连接到wifi时,我才能建立通话。如果其中一个设备连接到移动网络,则无法建立电话。我已经阅读了所有可能的论坛,看起来我需要TURN服务器。我已经运行了自己的TURN服务器,但我不知道如何强制应用程序使用此服务器。任何帮助都欢迎。谢谢!
WebRTC弃用旧API以创建ICE服务器。(使用旧API的答案)
要创建ICE服务器,您需要使用IceServer构建器模式。
PeerConnection.IceServer stun = PeerConnection.IceServer.builder("stun:stun.l.google.com:19302").createIceServer();
PeerConnection.IceServer turn = PeerConnection.IceServer.builder("turn:numb.viagenie.ca").setUsername("webrtc@live.com").setPassword("muazkh").createIceServer();
创建PeerConnection时需要设置TURN服务器。
大致如下:
// Set ICE servers
List<PeerConnection.IceServer> iceServers = new ArrayList<>();
iceServers.add(new org.webrtc.PeerConnection.IceServer("stun:xxx.xxx.xxx.xxx"));
iceServers.add(new org.webrtc.PeerConnection.IceServer("turn:xxx.xxx.xxx.xxx:3478", "username", "credential"));
// Create peer connection
final PeerConnectionFactory.Options options = new PeerConnectionFactory.Options();
PeerConnectionFactory factory = new PeerConnectionFactory(new PeerConnectionFactory.Options());
MediaConstraints constraints = new MediaConstraints();
PeerConnection peerConnection = factory.createPeerConnection(iceServers, constraints, new YourPeerConnectionObserver());
我没有运行过这段代码,但你应该能够理解它的含义。