Configure STUN/TURN Server
Custom STUN servers for AWS Wavelength Zones
In most cases, the default Google STUN server is sufficient for WebRTC connectivity, so you don’t need to change the default settings.
However, in AWS Wavelength Zones, there are limitations when obtaining ICE candidates. To address this, Ant Media provides freely accessible STUN servers specifically designed for Wavelength Zones.
You can use them via: stun.wavelength.antmedia.cloud where multiple instances are actively running.
Custom STUN Server Configuration in Ant Media Server
-
Open the Ant Media Server Web Panel and navigate to
Application→Advanced Settings. -
Change the following property:
"stunServerURI"="stun:stun1.l.google.com:19302"
to
"stunServerURI"="stun:stun.wavelength.antmedia.cloud"
- Save the application settings to apply the changes.
Configure Custom STUN Server on the Client Side
If you configure STUN/TURN on the server, you don’t need to configure it again on the client.
If not, you must add your custom STUN/TURN server as an ICE server on the client side.
Javascript SDK
For a custom STUN/TURN server to work, you need to pass it to the WebRTCAdaptor object as an element in the iceServers list.
WebRTCAdaptor has a peerconnection_config field that accepts iceServers array.
- Example:
var pc_config = {
'iceServers' : [ {
'urls' : 'stun:stun1.l.google.com:19302'
} ]
};
var webRTCAdaptor = new WebRTCAdaptor({
websocket_url: websocketURL,
mediaConstraints: mediaConstraints,
peerconnection_config: pc_config,
//goes on...
To use a custom STUN server, replace the default Google STUN with your STUN server.
- Example:
var pc_config = {
'iceServers' : [ {
'urls' : 'stun:stun.wavelength.antmedia.cloud'
} ]
};
And pass it to peerconnection_config field of WebRTCAdaptor, as specified above.
- If you are utilizing sample pages for publishing or playing, you can:
-
Open the html files under
/usr/local/antmedia/webapps/live -
Find the lines below
var pc_config = {
'iceServers' : [ {
'urls' : 'stun:stun1.l.google.com:19302'
} ]
};
Replace them with the following:
var pc_config = {
'iceServers' : [ {
'urls' : 'stun:stun.wavelength.antmedia.cloud'
} ]
};
- Save the files. You don't need to restart the Ant Media Server.
Custom TURN Server
If WebRTC connectivity fails even with all ports open, you may need to configure a TURN server. To learn more about TURN server , check out this Guide
TURN is an extension of STUN, so configuration is very similar.
Custom TURN server Configuration in Ant Media Server
You can configure the TURN server directly through the Ant Media Server application settings. This way, you won't need to configure a TURN server separately in each client SDK.
But in case configuring on the server side does not work, then you must add your custom STUN/TURN server as an ICE server on the client side.
-
Go to your Ant Media Server Dashboard →
Application→Settings→Advanced Settings. -
Set the below settings as follows:
"stunServerURI"="turn:TYPE_YOUR_TURN_SERVER_URL",
"turnServerUsername"="TYPE_YOUR_TURN_SERVER_USERNAME",
"turnServerCredential"="TYPE_YOUR_TURN_SERVER_PASSWORD",
- Save the settings. Ant Media Server will automatically use your TURN server.
Configure Custom TURN server in the JavaScript SDK.
var pc_config =
{
'iceServers': [
{
'urls': 'stun:stun1.l.google.com:19302'
},
{
'urls': 'TURN_IP:3478',
'username': 'username',
'credential': 'passsword'
}]
}
var webRTCAdaptor = new WebRTCAdaptor({
peerconnection_config: pc_config,
// other options
})
Configure custom TURN server in the Embedded Player.
- If you are using Ant Media Server Embedded Web Player, which is found in this Github Repo to play your streams, you need to pass
iceServersas a string to the WebPlayer constructor.
Example:
new WebPlayer({
streamId: "teststream",
httpBaseURL: "http://localhost:5080/live/",
iceServers: '[
{ "urls": "stun:stun1.l.google.com:19302" },
{
'urls': 'turn:TURN_IP:3478',
'username': 'username',
'credential': 'password'
}
]',
videoHTMLContent: '<video id="video-player" class="video-js vjs-default-skin vjs-big-play-centered" controls playsinline style="width:100%;height:100%"></video>',
playOrder: playOrderLocal
}, videoRef.current, placeHolderRef.current);
-
If you are utilizing the Ant Media Server sample
play.htmlpage, please remember that it is also based on an embedded web player. -
You can find
embedded-player.jsin/usr/local/antmedia/webapps/live/webapps/js/embedded-player.jslocation and directly edit the ICE server list.
this.iceServers = '[ { "urls": "stun:stun1.l.google.com:19302" } ]';
To
this.iceServers = '[ { "urls": "turn:TURN_IP:3478", "username": "username", "credential": "password" } ]';
- For more information about embedded web player, check out this Guide
Configure custom TURN server in Android SDK
- You can set a TURN server by using
setTurnServer()method of WebRTCClient Builder in the Android SDK.
Example:
webRTCClient = IWebRTCClient.builder()
.setLocalVideoRenderer(fullScreenRenderer)
.setServerUrl(serverUrl)
.setTurnServer("turn:YOUR_SERVER", "username", "password")
.setActivity(this)
.setWebRTCListener(createWebRTCListener())
.setDataChannelObserver(createDatachannelObserver())
.build();
-
This will add your turn server to ICE server lists. If default Google STUN fails, it will automatically utilize your TURN server.
-
If you are using the WebRTC Android SDK as a module in your project, since it is open source, you can also directly add a TURN server to the ICE server list.
-
Open
WebRTCClient.javafile and go to theinit()function. There is a line that adds stunServerUri to ice servers.
iceServers.add(new PeerConnection.IceServer(stunServerUri));
Replace this line with:
iceServers.add(PeerConnection.IceServer.builder("turn:YOUR_SERVER")
.setUsername("username")
.setPassword("credential")
.createIceServer());
Configure custom TURN server in the iOS SDK.
- Open the
Config.swiftfile, go to thecreateConfiguration()function. There is a line that adds a stunServerUri to ice servers.
let configuration = Config.createConfiguration(server: stunServer)
Replace this function with:
static func createConfiguration(server: RTCIceServer) ->` RTCConfiguration {
let config = RTCConfiguration.init()
let iceServerNew = RTCIceServer.init(urlStrings: [your_server], username: "your_username", credential: "your_password")
config.iceServers = [server, iceServerNew]
return config
}
Configure custom TURN server in the Flutter SDK.
List<Map<String, String>> iceServers = [
{'url': 'stun:stun.l.google.com:19302'},
{
'urls': 'turn:TURN_IP:3478',
'username': 'username',
'credential': 'password'
}
];
AntMediaFlutter.connect(
//other options,
widget.iceServers
);
STUN & TURN for you
You’ve swapped out the default STUN server, added your custom Ant Media STUN for Wavelength Zones, and configured a TURN server for those tough NAT/firewall cases.
Tada, you now have rock-solid WebRTC connectivity with custom STUN/TURN servers in Ant Media Server, ready for smooth, reliable streaming everywhere.