Multitrack publish and play with AMS
WebRTC multitrack lets us stream multiple audio/video tracks through a single WebRTC connection. In order to use Multitrack feature, the SDP semantic should be set as Unified Plan which is also set by default in AMS v2.4.3 and above.
With multitrack streams, you can play different groups of streams with a single broadcast ID. Then, you can start playing those groups of streams with one play request and, most importantly through a single WebRTC connection and this decreases resource usage as well.
Terminologies related to multitrack
Main track: Stream Id of a group is referred as main track.
Sub track: The streams in a group with different stream Ids are referred as subtracks.
To combine the broadcasts into a single broadcast (main track), publish the streams as shown below.
- When calling the publish method of the webrtcAdaptor in the SDK, pass the group ID as
mainTrack. The URL for the WebRTC sample page will be as follows:
- You must use the following URL for RTMP streams:
Publishing multitrack streams
Let's publish a stream to the sample WebRTCAppEE application with streamId=video and group ID (mainTrack) as main.
Now, as needed, publish streams with different audio subtracks.
Assume you have two audio tracks with the stream IDs audio1 and audio2.
Playing multitrack streams
Multitrack streams can be played with the sample page, multitrackplayer.html
- Write the group ID in the text box
- Request the sub-tracks by clicking the
- Select the tracks you want to play and click the
- If a new subtrack is added to the group, it will be played automatically
You can enable or disable video/audio feed for a sub-track with
enableTrack(mainTrackId, trackId, enabled) methods in the webrtc-adaptor SDK.
multitrack-conference.html is a new sample conference page that is compatible with multitrack playback.
mainTrack (group ID) will be the same as the room Id. For example, in the above-described case, the mainTrack is main, so it will be the room Id.
You can also join the room in
playOnly mode using the following URL.
The play request for the room Id is only called once in a Multitrack conference.
By default, there is no limit on audio and video tracks, so each participant can play other participants videos, which increases CPU load on the server side. In order to optimize the performance, you can limit the audio and video tracks by adding below settings to the /usr/local/antmedia/webapps/App-Name/WEB-INF/red5-web.properties file.
Please change the values as per your requirements and restart the server after making changes. In version 2.6.2 and above, all settings can be changed from dashboard itself. Please check here.
Add and Delete subtracks via Rest API
You can use the add SubTrack Rest API to add the various subtracks to the main track. Here's the CURL sample:
curl -X 'POST' 'https://AMS-domain:5443/WebRTCAppEE/rest/v2/broadcasts/mainTrackId/subtrack?id=stream1' -H 'accept: application/json'
You can use the delete SubTrack Rest API to delete the various subtracks from the main track. Here's the CURL sample:
curl -X 'DELETE' 'https://AMS-domain:5443/WebRTCAppEE/rest/v2/broadcasts/mainTrackId/subtrack?id=stream1' -H 'accept: application/json'