Initialize Live Stream - Flutter
To configure livestreaming with VideoSDK you require two things, first the token
which will be used for Authentication purpose and a streamId
which will be used to specify where the hosts and audience will join.
Generating Token​
You can generate a token
in two ways:
-
Temporary Token
: You can visit Dashboard's API Key section and generate a temporary token from there. -
Server
: You can setup JWT in backend and make an API call to get the token from your server.
To learn more about Authentication and token in detail you can follow this guide.
// With Temporary Token
const getToken = async () => {
// Update the token here from the VideoSDK dashboard
let token = "YOUR_TOKEN";
};
// Server
const getToken = async () => {
const response = await fetch(`http://localhost:3000/get-token`, {
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
});
const { token } = await response.json();
return token;
};
Generating Stream Id​
With the token ready, you can now get the streamId
from the VideoSDK's rooms API.
// API call to create livestream
Future<String> createStreamId(String token) async {
final http.Response httpResponse = await http.post(
Uri.parse("https://api.videosdk.live/v2/rooms"),
headers: {'Authorization': token},
);
//Destructuring the roomId from the response and store it in stremId variable
String streamId = json.decode(httpResponse.body)['roomId'];
return streamId;
}
Initialization of Live Stream​
You can initialize the live stream using the VideoSdk Class
from the Flutter SDK. The VideoSdk Class
is responsible for initializing the live stream with the provided configuration, which includes the token
, livestreamId
, participantId
and many more.
VideoSdk Class​
VideoSDK Class is a core class of SDK that provides methods to create and configure VideoSdk Room
.
You have to pass the initialization configuration for the live stream and the token in the VideoSdk Class
.
VideoSDK.createRoom(
roomId: "<streamId>",
mode: "SEND_AND_RECV", // "SEND_AND_RECV" for hosts/co-hosts, "RECV_ONLY" for audience
token: "Generated-Token",
displayName: "<Participant Name>",
micEnabled: true, //Ignore for audience
camEnabled: true, //Ignore for audience
participantId: "<Unique Participant ID>", // Optional
multiStrem : true, // //Ignore for audience
);
-
roomId
:- This is a unique identifier that allows participants to join a specific live stream.
- It will be in the format of
xxx-yyy-zzz
and will be generated using the VideoSDK's Room API.
-
mode
:- This paramter defines the mode the participant will enter the live stream with.
SEND_AND_RECV
: For hosts/co-hosts who can send and receive media.RECV_ONLY
: For audience members who can only receive media.​
-
name
:- This represents the name of the participant in the live streams.
- It will accept
String
type value.
-
micEnabled
: -
This is a
boolean
flag, indicating whether a participant's microphone will be automatically enabled when they join the live stream. This property will be completely ignored for Audience participants, as they are not allowed to publish their audio. -
wcamEnabled
: -
This is a
boolean
flag, indicating whether a participant's camera will be automatically enabled when they join the live stream. This property will be completely ignored for Audience participants, as they are not allowed to publish their video. -
metaData
:- If you want to provide additional details about the participants joining the live stream, such as their profile image, you can pass that information in this parameter.
- It has to be of
Object
type. - This is an
OPTIONAL
parameter.
-
participantId
:-
This is a unique identifier for the participant's inside the live stream.
- It can be used to specify the unique identifier which can be linked with your own database service.
- It has to be of
String
type. - This is an
OPTIONAL
parameter. By default VideoSDK will generate unique id for each participant.
-
-
multistream
:- This is a
boolean
flag, indicating whether the host's media stream should send multiple resolution layers or a single resolution layer.
- This is a
import 'package:flutter/material.dart';
import 'package:videosdk/videosdk.dart';
class LiveStreamScreen extends StatefulWidget {
const LiveStreamScreen({super.key});
@override
State<LiveStreamScreen> createState() => _LiveStreamScreenState();
}
class _LiveStreamScreenState extends State<LiveStreamScreen> {
late Room _room;
@override
void initState() {
String token = getToken();
String liveStreamId = createStreamId();
//Creating a new Room based on the passed LivestreamId and token from the Joining Screen
// create room
_room = VideoSDK.createRoom(
roomId: liveStreamId,
mode: "SEND_AND_RECV", // "SEND_AND_RECV" for hosts/co-hosts, "RECV_ONLY" for audience
token: token,
displayName: "<Participant Name>",
micEnabled: true, //Ignore for audience
camEnabled: true, //Ignore for audience
participantId: "<Unique Participant ID>", // Optional
multiStream : true, // //Ignore for audience
);
super.initState();
}
@override
Widget build(BuildContext context) {
return YourLivestreamWidget();
}
}
API Reference​
The API references for all the methods utilized in this guide are provided below.
Got a Question? Ask us on discord