CDN selector
This feature is experimental, starting from version 1.8.16 on Android and 1.3.8 on Web.
The CDN selector allows to balance the client HTTP requests between several CDN. The CDN selector uses QoS/QoE metrics to select the most appropriate server.
Install
- Web
- Android
No additional package is required. The CDN selector is included in all QUANTEEC web player plugins starting from version 1.3.8.
In your Gradle file, make sure you are using a version above 1.8.16 of the QUANTEEC plugin and the QUANTEEC Media3 Exoplayer adapter. The recommended one is 1.9.3.
repositories {
dependencies {
...
implementation "com.quanteec:quanteecMedia3Exoplayer:1.9.3"
implementation "com.quanteec:quanteecPlugin:1.9.3"
}
}
Get started
Setting URLs via configuration
The CDN URLs can be set directly in the QUANTEEC configuration object:
- Web
- Android
The servers parameter accepts an array of strings (base URLs) or objects with name, url, and useFirst properties:
// Simple string array
var quanteecConfig = {
quanteecKey: "<your-quanteec-key>",
videoID: "<enter-your-custom-videoID>",
servers: [
"https://server1.example.com",
"https://server2.example.com"
]
};
// Or with named CDN objects
var quanteecConfig = {
quanteecKey: "<your-quanteec-key>",
videoID: "<enter-your-custom-videoID>",
servers: [
{ name: "cdn1", url: "https://server1.example.com", useFirst: true },
{ name: "cdn2", url: "https://server2.example.com" }
]
};
- Kotlin
- Java
val urls = ArrayList<CDNUrl>()
urls.add(CDNUrl("cdn1","https://server1.example.com"))
urls.add(CDNUrl("cdn2","https://server2.example.com"))
val quanteecConfig = QuanteecConfig.Builder("<your-quanteec-key>")
.setVideoID("<enter-your-custom-videoID>")
.setCDNUrls(urls)
.build()
List<CDNUrl> listUrls = new ArrayList<CDNUrl>();
listUrls.add(new CDNUrl("cdn1","https://server1.example.com"));
listUrls.add(new CDNUrl("cdn2","https://server2.example.com"));
QuanteecConfig quanteecConfig = new QuanteecConfig.Builder("<your-quanteec-key>")
.setVideoID("<enter-your-custom-videoID>")
.setCDNUrls(listUrls)
.build();
All of the URL of the CDNs must be provided to Quanteec, not only the alternative ones. Quanteec will modify the base URL of the requests only if the request is made to a URL that matches one of the provided CDN URLs.
Accessing the CDN selector instance
- Web
- Android
var cdnSelector = myQuanteec.getCDNSelector();
- Kotlin
- Java
val cdnSelector = quanteecCore.cdnSelector
CDNSelector cdnSelector = quanteecCore.getCDNSelector();
Setting URLs at runtime
The CDN URLs can also be set or updated at runtime via the CDN selector instance:
- Web
- Android
var cdnSelector = myQuanteec.getCDNSelector();
// Using string URLs (names are derived from hostnames)
cdnSelector.setUrls([
"https://server1.example.com",
"https://server2.example.com"
]);
// Or using named CDN objects
cdnSelector.setUrls([
{ name: "cdn1", url: "https://server1.example.com", useFirst: true },
{ name: "cdn2", url: "https://server2.example.com" }
]);
- Kotlin
- Java
val urls = ArrayList<CDNUrl>()
urls.add(CDNUrl("cdn1","https://server1.example.com"))
urls.add(CDNUrl("cdn2","https://server2.example.com"))
cdnSelector.urls = urls
List<CDNUrl> listUrls = new ArrayList<CDNUrl>();
listUrls.add(new CDNUrl("cdn1","https://server1.example.com"));
listUrls.add(new CDNUrl("cdn2","https://server2.example.com"));
cdnSelector.setUrls(listUrls);
Behavior
The CDN selector uses QoS/QoE metrics to select the appropriate server. Every time a HTTP request is started, the CDN selector monitors and saves the QoS/QoE metrics: observed bandwidth, time-to-first-byte, and failures over a rolling time window. The CDN selector is activated automatically when two or more CDN server URLs are configured.
The CDN selector can switch to another server because a server fails to deliver requests or because another server with better metrics has been found.
Starting from Android SDK 1.9.2, each HTTP request can be retried across every configured CDN. The selector tries each CDN up to maxRetry times before returning the failure to the player. The retry limit defaults to 2; set it to -1 to retry indefinitely. Retry counters are scoped to a single request, so a CDN that failed for one URL remains available for subsequent URLs.
To compute the CDN with the best stat:
factorBandwidth × cdnStat.average_bandwidth + factorTtfb × cdnStat.average_ttfb
Where factorBandwidth and factorTtfb are configurable weighting factors (default values are 1 and -10000 respectively).
A CDN is only promoted over the current selection if it has both a bandwidth and a TTFB measurement. A CDN also wins unconditionally if the currently selected CDN has a recent failure and the candidate does not (or its most recent failure is older).
Statistics are kept in a rolling window of up to 10 entries per metric, with entries older than 5 minutes automatically discarded.
On Android, the CDN selector also performs bounded background probing. When a manifest request provides an opportunity and the probe cadence is due, it sends a small range request to one non-selected CDN at a time, rotating between them. This keeps inactive CDN statistics up to date without blocking playback.
API
- Web
- Android
CDN Selector
The CDN selector is accessed via getCDNSelector() on the QUANTEEC instance:
var cdnSelector = myQuanteec.getCDNSelector();
Methods
URL Management
| Method | Return Type | Description |
|---|---|---|
getUrls() | Array | Returns the list of configured CDN URLs (as {name, url, useFirst} objects) |
setUrls(cdnUrls) | void | Sets the CDN URLs. Accepts an array of strings or {name, url, useFirst} objects |
getSelectedUrl() | `Object | null` |
forceUrl(name) | boolean | Forces the use of a specific CDN by name. Returns false if the name is not found |
getAllPossibleUrl(url) | Array | Returns an array of URLs with the base CDN prefix replaced by each configured CDN |
isActive() | boolean | Returns true if the CDN selector is active (2+ CDN URLs configured) |
reset() | void | Clears all CDN URLs, statistics, and listeners. Called automatically when the plugin is destroyed |
Statistics
| Method | Return Type | Description |
|---|---|---|
getStats() | Array | Returns CDN statistics. Each entry has name, averageBandwidth, averageTtfb, mostRecentFail |
Listener Management
| Method | Description |
|---|---|
addCDNStatListener(callback) | Registers a callback function(stats) called when CDN statistics are updated |
removeCDNStatListener(callback) | Unregisters a CDN statistics callback |
addCDNUrlListener(callback) | Registers a callback function(oldCdnUrl, newCdnUrl) called when the selected CDN changes |
removeCDNUrlListener(callback) | Unregisters a CDN URL callback |
Configuration Factors
| Method | Return Type | Description |
|---|---|---|
getFactorBandwidth() | number | Returns the bandwidth weighting factor (Default: 1) |
setFactorBandwidth(value) | void | Sets the bandwidth weighting factor |
getFactorTtfb() | number | Returns the TTFB weighting factor (Default: -10000) |
setFactorTtfb(value) | void | Sets the TTFB weighting factor |
Usage Example
var cdnSelector = myQuanteec.getCDNSelector();
// Check if CDN selector is active
console.log("Active:", cdnSelector.isActive());
console.log("Selected:", cdnSelector.getSelectedUrl()?.name);
// Listen for CDN switches
cdnSelector.addCDNUrlListener(function(oldCdn, newCdn) {
console.log("CDN switch: " + (oldCdn ? oldCdn.name : "none") + " → " + newCdn.name);
});
// Listen for stat updates
cdnSelector.addCDNStatListener(function(stats) {
stats.forEach(function(s) {
console.log(s.name + ": BW=" + s.averageBandwidth + ", TTFB=" + s.averageTtfb);
});
});
// Force a specific CDN
cdnSelector.forceUrl("cdn1");
// Adjust scoring factors
cdnSelector.setFactorBandwidth(1);
cdnSelector.setFactorTtfb(-10000);
CDNSelector
CDNSelector - Documentation
CDNSelector is a class that controls CDN selection.
Package
com.quanteec.plugin.core.models
Class Structure
Constructor
public CDNSelector(CDNSelectorInternal internal)
Creates an instance with an internal implementation.
Parameters:
internal: The internal CDN selector implementation
Methods
URL Management
| Method | Return Type | Description |
|---|---|---|
getUrls() | List<CDNUrl> | Retrieves the list of available CDN URLs |
setUrls(List<CDNUrl> cdnUrls) | void | Sets the list of CDN URLs |
forceUrl(String name) | boolean | Forces the use of a specific CDN by name |
Statistics
| Method | Return Type | Description |
|---|---|---|
getStats() | List<CDNStat> | Retrieves CDN statistics |
Listener Management
| Method | Parameters | Description |
|---|---|---|
addCDNStatListener(CDNStatListener listener) | listener : statistics listener | Registers a listener for CDN statistics updates |
removeCDNStatListener(CDNStatListener listener) | listener : statistics listener | Unregisters a CDN statistics listener |
addCDNUrlListener(CDNUrlListener listener) | listener : URL listener | Registers a listener for CDN URL changes |
removeCDNUrlListener(CDNUrlListener listener) | listener : URL listener | Unregisters a CDN URL listener |
Configuration Factors
| Method | Return Type | Description |
|---|---|---|
getFactorBandwidth() | double | Retrieves the bandwidth weighting factor |
setFactorBandwidth(double factorBandwidth) | void | Sets the bandwidth weighting factor (Default: 1) |
getFactorTtfb() | double | Retrieves the TTFB (Time To First Byte) weighting factor (Default: -10000) |
setFactorTtfb(double factorTtfb) | void | Sets the TTFB weighting factor |
getMaxRetry() | int | Retrieves the maximum number of attempts per CDN and per HTTP request (Default: 2) |
setMaxRetry(int maxRetry) | void | Sets the maximum number of attempts per CDN and per HTTP request. Accepts a positive value or -1 for unlimited retries |