Skip to main content
Version: 5.x.x

RTMP Livestream - Flutter

RTMP is a widely used protocol for live streaming video content from VideoSDK to platforms like YouTube, Twitch, Facebook, and others.

To initiate live streaming from VideoSDK to platforms supporting RTMP ingestion, you simply need to provide the platform-specific stream key and stream URL. This enables VideoSDK to connect to the platform's RTMP server and transmit the live video stream.

Furthermore, VideoSDK offers flexibility in configuring livestream layouts. You can achieve this by either selecting different prebuilt layouts in the configuration or by providing your custom template for livestreaming, catering to your specific layout preferences.

This guide will provide an overview of how to implement starting and stopping RTMP livestreaming with VideoSDK.

startLivestream()​

startLivestream() can be used to start a RTMP livestream of the room which can be accessed from the Room object. This method accepts two parameters:

  • 1. outputs: A list of LivestreamOutput. Each one holds the RTMP url and streamKey of a platform you want to start the livestream on.

  • 2. config (optional): A LivestreamConfig that defines how the livestream layout looks. Every field is optional.

const outputs = [
LivestreamOutput(
url: "rtmp://a.rtmp.youtube.com/live2",
streamKey: "<STREAM_KEY>",
),
LivestreamOutput(
url: "rtmps://",
streamKey: "<STREAM_KEY>",
),
];
const config = LivestreamConfig(
// Layout Configuration
layout: ConfigLayout(
type: ConfigLayoutGrid.GRID, // SPOTLIGHT | SIDEBAR, Default: GRID
priority: ConfigLayoutPriority.SPEAKER, // PIN, Default: SPEAKER
gridSize: 4, // MAX: 4
),

// Theme of livestream
theme: ConfigTheme.DARK, // LIGHT | DEFAULT
);

try {
await room.startLivestream(outputs, config: config);
} on VideoSDKError catch (error) {
debugPrint("startLivestream failed: ${error.code} ${error.name}");
}

startLivestream() returns a Future that throws a VideoSDKError when the SDK refuses the call, so await it inside try/catch. Some failures reach Events.error instead of throwing, so keep an Events.error handler too.

The Future completes before the livestream starts, so wait for Events.onLivestreamStateChanged to report LIVESTREAM_STARTED.

stopLivestream()​

  • stopLivestream() is used to stop the room livestream which can be accessed from the Room object. Like startLivestream(), it returns a Future that can throw a VideoSDKError.

Example​

import 'package:flutter/material.dart';
import 'package:videosdk/videosdk.dart';

class LivestreamScreen extends StatefulWidget {
...
}

class _LivestreamScreenState extends State<LivestreamScreen> {
late Room _room;

@override
void initState() {
...
}

@override
Widget build(BuildContext context) {
return Column(
children:[
ElevatedButton(
onPressed:() async {
const outputs = [
LivestreamOutput(
url: "rtmp://a.rtmp.youtube.com/live2",
streamKey: "<STREAM_KEY>",
),
LivestreamOutput(
url: "rtmps://",
streamKey: "<STREAM_KEY>",
),
];
const liveStreamConfig = LivestreamConfig(
layout: ConfigLayout(
type: ConfigLayoutGrid.GRID,
priority: ConfigLayoutPriority.SPEAKER,
gridSize: 4,
),
theme: ConfigTheme.LIGHT,
);
try {
await _room.startLivestream(outputs, config: liveStreamConfig);
} on VideoSDKError catch (error) {
debugPrint("startLivestream failed: ${error.code} ${error.name}");
}
},
child: const Text("Start Livestream"),
),
ElevatedButton(
onPressed:() async {
try {
await _room.stopLivestream();
} on VideoSDKError catch (error) {
debugPrint("stopLivestream failed: ${error.code} ${error.name}");
}
},
child: const Text("Stop Livestream"),
),
]
);
}
}

Event associated with Livestream​

  • onLivestreamStateChanged - Whenever the room livestream state changes, the onLivestreamStateChanged event is triggered. room.liveStreamState holds the latest state.
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:videosdk/videosdk.dart';

class LivestreamScreen extends StatefulWidget {
...
}

class _LivestreamScreenState extends State<LivestreamScreen> {
late Room room;

@override
void initState() {
...

setupRoomEventListener();
}

@override
Widget build(BuildContext context) {
return YourLivestreamScreenWidget();
}

void setupRoomEventListener() {
room.on(Events.onLivestreamStateChanged, (String status) {
//Status can be :: LIVESTREAM_STARTING
//Status can be :: LIVESTREAM_STARTED
//Status can be :: LIVESTREAM_STOPPING
//Status can be :: LIVESTREAM_STOPPED
log("Room Livestream status : $status");
});
}
}

API Reference​

The API references for all the methods utilized in this guide are provided below.

Got a Question? Ask us on discord