1000 FAQs, 500 tutorials and explanatory videos. Here, there are only solutions!
Secure an MP3/AAC or HLS stream
This guide explains how to maintain control over your MP3/AAC or HLS radio streaming streams, for example, to decide if a listener can listen to your radio by activating protection with a unique key (token).
Protecting an Audio Stream with a Unique Key
The principle is simple: with each connection, you will make a request to the Infomaniak API, which will return a unique token with a limited and configurable lifespan. This token will authorize anyone who possesses it to consume the stream during that period.
You can protect an MP3/AAC or HLS stream independently (the same goes for geolocation).
To do this, simply go to the restriction settings and activate token protection on the stream you want to secure:
- Log in to the Infomaniak Manager (manager.infomaniak.com) from a web browser like Brave or Edge
- Click on the icon at the top right of the interface (or navigate using the left sidebar menu, for example)
- Choose Streaming radio (under Streaming)
- Click on the name of the relevant object in the displayed table
- Click on Restrictions in the left sidebar menu
- Select HLS if necessary
- Click on the action menu â‹® to the right of the relevant stream in the displayed table
- Click on Token restriction
Then activate the protection.
Attention: when you activate this option, access to the stream will be instantly blocked for new connections. Adjust your players to take into account the restriction, as illustrated in the example below:
Creating a Radio API Token
To access the Radio API, you must first authenticate yourself with an application token. This step only needs to be done once. To create this application token, read this guide.The scope is radio, and the lifespan is unlimited to avoid having to regenerate a code regularly. Once the token is generated, copy it to paste it into the example below.
Example of Use in PHP Language
For MP3/AAC or HLS, the code can be essentially the same, with only the URL called in POST changing in its form.
/**
* Generic function to execute cURL requests
*
* @param string $method HTTP method (GET, POST, PUT, etc...)
* @param string $url URL of the API to request
* @param array $headers List of HTTP headers (the authorization must be passed here with a ['Authorization: Bearer
* @param array $payload An array containing data to create a token
* @return mixed
*/
- For an MP3 / AAC stream
- For an HLS stream
POST https://api.infomaniak.com/1/radios/acl/streams/<b>mountpoint.mp3</b>/token
Example to protect https://newradiotest.ice.infomaniak.ch/newradiotest-128.aac
the route will be: https://api.infomaniak.com/1/radios/acl/streams/newradiotest-128.aac/token
POST https://api.infomaniak.com/1/radios/acl/hls_streams/<stream>/token
Example to protect https://myradiostream.radiohls.infomaniak.com/myradiostream/manifest.m3u8
the route will be: https://api.infomaniak.com/1/radios/acl/hls_streams/myradiostream/token
Example in the case of MP3 / AAC, make sure to adjust
'POST',
'https://api.infomaniak.com/1/radios/acl/streams/newradiotest-128.aac/token',
// authorization header
[
'Authorization: Bearer ' . API_TOKEN,
'Content-Type: application/json',
],
/**
* payload to create the token, you can pass the following values
* window | 300 | optional | token validity duration (default: 5 minutes)
*/
[
'window' => 3600, // 1h validity
]
It is important to note that if this code is generated at the time of page loading, the listener will have "window" seconds to start playing the stream. After this time, the token will expire, and the stream will no longer be able to be played unless the page is reloaded. Depending on your needs and use case, it will be necessary to adjust this delay in the best possible way.
You will also need to replace below the playback URL of your stream instead of the one indicated while keeping the $token
parameter at the end. And finally, we display the player (here a simple html5 tag, but you can of course add any overlay afterwards, the token being passed in the $_GET
parameters of the URL.
$streamUrl = "https://newradiotest.ice.infomaniak.ch/newradiotest-128.aac?$token";
echo "<audio controls=""><source src="$streamUrl"></audio>";