Guide to Interactive streaming

This guides explains you on how to build interactive live streaming apps via conferencing SDK.

Generate an API Key for your project

  1. Generate a Bearer Token

  2. Creating a Conference Room

  3. Alternative to Creating a Conference Room

  4. Start Live Streaming

  5. Stop Live Streaming

  6. Streaming to Multiple Outlets

Generate API Key

You can generate your API key for your project by visiting console.sariska.io and signing up.

The full instruction on how to do it can be found here: Link

Generate a Bearer Token

We generate these authentication tokens on our own servers. You can use the same token for all the services offered by Sariska.

To generate a token, follow the instruction on the page here

“user” data in the above curl command is optional and the bearer token is generated regardless of it.

Creating a Conference Room for Your Live Stream

In order to start a multi host live stream, all the hosts need to join a common room. The name for this common room can be anything the hosts choose to. This part details on how to create a simple conference for your hosts to live stream.

Installation:

The first step is to install the sariska-media-transport library by running the following command in your project directory using NPM.

npm i sariska-media-transport

Initialise SDK:

After installing the SDK, perform initial setup tasks by running initialize().

import SariskaMediaTransport from "sariska-media-transport";

SariskaMediaTransport.initialize();

Create Connection:

To establish a connection, you would need to generate a token. You’d also need to specify a room name, this is the room your hosts will join to live stream together.

const token = {your-bearer-token};

const connection = new SariskaMediaTransport.new SariskaMediaTransport.JitsiConnection(token, "{room-name}", isNightly);

connection.addEventListener(SariskaMediaTransport.events.connection.CONNECTION_ESTABLISHED, () => {
  console.log('connection successful!!!');
});

connection.addEventListener(SariskaMediaTransport.events.connection.CONNECTION_FAILED, (error) => {
  if (error  === SariskaMediaTransport.events.connection.PASSWORD_REQUIRED) { // token expired set again
      connection.setToken(token) // set a new token
      console.log('connection disconnect!!!', error);
  }
});

connection.addEventListener(SariskaMediaTransport.events.connection.CONNECTION_DISCONNECTED, (error) => {
  console.log('connection disconnect!!!', error);
});

connection.connect();

Create Conference:

Once you have your connection established, the next step is to create a conference.

const conference = connection.initJitsiConference(options);
conference.join();

//Additional options initJitsiConference accepts to enable more features

// startAudioMuted: true  // to join with  audio muted
// startVideoMuted: true // to join with video muted
// startSilent : true  // to join with startSilent no audio receive/send
// rtcstatsServer:  “”  // to send enable rtcstats tracking
// callStatsID: “”    // callstatsid
// callStatsSecret:  “”   // callstatssecet
// channelLastN: 10  // last n speakers

Now, the conferenceobject will have all events and methods that you would possibly need for any feature that you wish to supplement your application with.

Capture local streams:

A MediaStream consists of zero or more MediaStreamTrack objects, representing various audio or video tracks. Each MediaStreamTrack may have one or more channels. The channel represents the smallest unit of a media stream, such as an audio signal associated with a given speaker, like left or right in a stereo audio track. Here we mostly talk about track.

const options = {
  devices: ["audio", "video"],
  resolution: 240, // 180,  240,  360, vga, 480, qhd, 540, hd, 720, fullhd, 1080, 4k, 2160
}

const localTracks = await SariskaMediaTransport.createLocalTracks(options);

Play Local stream:

const audioTrack = localTracks.find(track=>track.getType()==="audio");
const videoTrack = localTracks.find(track=>track.getType()==="video");

// to play audio 
audioTrack.attach(document.getElementById("audioElement"))

// to play video
videoTrack.attach(document.getElementById("videoElement"))

User Joined:

The moderator of the meeting controls and gate keeps the participants. The moderator has exclusive control of the meeting.

conference.addEventListener(SariskaMediaTransport.events.conference.USER_JOINED, function(id, participant) {
  console.log("user joined!!!", id, participant);
});

Publish your stream to other peers:

localTracks.forEach(track => conference.addTrack(track));

Playing remote peers streams:

const remoteTracks = [];

conference.addEventListener(SariskaMediaTransport.events.conference.TRACK_ADDED, function(track) {
  remoteTracks.push(track);
});

remoteTracks.forEach(track => {
    if (track.getType() === "audio") {
        RemoteContainer.append(track.attach(document.getElementById("remoteAudioElemId")));
    }
    
    if (track.getType() === "video") {
        RemoteContainer.append(track.attach(document.getElementById("remoteVideoElemId")));
    }
});

Alternative to Creating a Conference Room

If you’d like to quickly test the live streams without having to create a conference, you can use Sariska’s meet application.

let’s say the room name for our case is “streamtest”. Since we are using Sariska’s meet app to run this demo, you can go to meet.sariska.io/streamtest

Start Live Streaming

With Sariska, starting a live stream is just a simple API call away. Generate another bearer token and then follow the curl command.

For example, the curl command will look something like this for a room “streamtest”.

curl --location --request GET '<https://streaming.sariska.io/user/startRecording?room_name=streamtest>' \\
		 --header 'Authorization: Bearer {your-bearer-token}' \
		 --data-raw '{        
        	      "room_name": String,  
                      "is_low_latency": true         	              
		 }'

The sample response for such a request will be

{
    "started": true,
    "stream_name": "hvfypdujkrpa1gtl",
    "pod_name": "gstreamer-pipeline-bf569469d-v7wjb",
    "hls_url": "https://edge.sariska.io/play/hls/hvfypdujkrpa1gtl/jcmfew6oxbxtwqtg.m3u8",
    "hls_master_url": "https://edge.sariska.io/hvfypdujkrpa1gtl/jcmfew6oxbxtwqtg/master.m3u8",
    "low_latency_hls_url": "https://low-latency-edge.sariska.io/hvfypdujkrpa1gtl/jcmfew6oxbxtwqtg/original/playlist.m3u8",
    "rtmp_url": "rtmp://streaming-edge-nlb-tcp-d2d9b7821f042c80.elb.ap-south-1.amazonaws.com:1935/hvfypdujkrpa1gtl/jcmfew6oxbxtwqtg?domain=ll_latency_h264",
    "flv_url": "http://streaming-edge-nlb-tcp-d2d9b7821f042c80.elb.ap-south-1.amazonaws.com:8080/hvfypdujkrpa1gtl/jcmfew6oxbxtwqtg.flv?domain=ll_latency_h264",
    "vod_url": "https://vod.sariska.io/d4b041b4ac2e43a6940aa290bbd93c8b/index.m3u8"
}

Stop Live Streaming

curl --location --request GET '<https://streaming.sariska.io/user/stopRecording?room_name=streamtest>' \\
--header 'Authorization: Bearer {your-bearer-token}' 

Sample Response:

{
	"started": false
}

Streaming To Multiple Outlets

If you’d like to stream to platforms such as Youtube, Facebook, or Twitch. You can send a request like shown below.

curl --location --request GET '<https://streaming.sariska.io/user/startRecording?room_name=streamtest>' \\
		 --header 'Authorization: Bearer {your-bearer-token}'
		 --data '{"streamKeys": [
		{"streamKey": "youtube", "streamValue": "youtube-stream-key"}, 
		{"streamKey": "facebook", "streamValue": "facebook-stream-key"}, 
		{"streamKey": "twitch", "streamValue": "twitch-stream-key"}, 
		{"streamKey": "vimeo", "streamValue": "vimeo-stream-key"}, 
		{"streamKey": "periscope", "streamValue": "periscope-stream-key"}, 
		{"streamKey": "instagram", "streamValue": "instagram-stream-key"}
						],
			"streamUrls":["rtmp://test-rtmp-url-1", "rtmp://test-rtmp-url-2"],
			"is_recording":false,
			"is_vod":false,
			"audio_only":false
}'
  • streamKeys (optional): Streaming keys for all the platforms you want to stream at

  • streamUrls (optional): RTMP Url Endpoints

  • is_recording (optional): Flag to state if you’d like to record the meeting

  • is_vod (optional): Flag to state if you’d like to provide this stream as a VOD

  • audio_only (optional): Flag true if you’d like it to be an audio only stream

For detailed information, please refer to the Swagger documentation.

Last updated