Skip to main content

Publish Track

Overview

Using the publish track api, you can publish or transmit a new track to the Metered Global SFU. To publish a track to the Metered Global SFU, you will add the track to the peerConnection, and then generate the new SDP.

After generating the SDP you will send the SDP to the Global SFU using the publish track API.

Authentication

You must provide bearer token in the Authorization header. The bearer token in the secret of the SFU App that you have created in the dashboard. When you create an SFU app, you will get the SFU App Id and the Secret.

  • Header: Authorization: Bearer {secret}

HTTP Request

POST
https://global.sfu.metered.ca/api/sfu/:sfu_app_id/session/:session_id/track/publish

URL Parameters

Parameter NameTypeDescription
sfu_app_idstringThe unique identifier of your SFU app. This parameter is required to specify which SFU app the session should be created for. You can find the SFU app ID in the dashboard after creating the SFU app.
session_idstringThe unique identifier of the session. This parameter is required to specify which session the track should be published to. You can find the session ID in the response of the session creation API.

Headers

HeaderValueDescription
AuthorizationBearer {secret}The bearer token which is the secret of the SFU App created in the dashboard.
Content-Typeapplication/jsonIndicates that the request body format is JSON.

Request Body

{
tracks: [{
"trackId": "...",
"mid": "...",
"customTrackName": "custom track name"
}],
sessionDescription: {..sdp..}
}
FieldTypeRequiredDescription
tracksarrayYesAn array of track objects to be published. Each object should contain trackId, mid, and customTrackName.
sessionDescriptionobjectYesThe SDP (Session Description Protocol) information from the peerConnection.

Response

Success Response

The session response contains the unique sessionId representing the peerConnection, and the remote sdp from the SFU that you would have to add as remote session descriptor in the peerConnection.

{
"sessionDescription": {
"type": "answer",
"sdp": "v=0\r\no=- 7170991192596339755 1723210567..."
},
"sessionId": "session-101-03d73157-9df4-4bfc-91c9-01647bcfe80c"
}

Code Example

Here is the complete code example on how to publish the track.

const stream = await navigator.mediaDevices.getUserMedia({
video: true,
});

const transceiverA = peerConnection.addTransceiver(stream.getVideoTracks()[0], {
direction: 'sendonly'
});
const offer = await peerConnection.createOffer();
await peerConnection.setLocalDescription(offer);
console.log("Track ID:", transceiverA.sender.track.id);
console.log("MID:", transceiverA.mid);
const response = await fetch(
`${host}/api/sfu/${sfuAppId}/session/${sessionIdA}/track/publish`, { // Corrected HOST and SESSION_ID
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${secret}`
},
body: JSON.stringify({
tracks: [{
"trackId": transceiverA.sender.track.id,
"mid": transceiverA.mid,
"customTrackName": "userA"
}],
sessionDescription: offer
})
});
const json = await response.json();
console.log("Publish track response", json);
await peerConnection.setRemoteDescription(new RTCSessionDescription(json.sessionDescription));