Skip to main content
Version: 5.x.x

RTMP Livestream - Flutter

RTMP is a popular protocol for live streaming video content from a VideoSDK to platforms such as YouTube, Twitch, Facebook, and others.

By providing the platform-specific stream key and stream URL, the VideoSDK can connect to the platform's RTMP server and transmit the live video stream.

VideoSDK allows you to livestream your room to platform which support RTMP ingestion just by providing the platform-specific stream key and stream URL, we can connect to the platform's RTMP server and transmit the live video stream.

VideoSDK also allows you to configure the livestream layouts in numerous ways like by simply setting different prebuilt layouts in the configuration or by providing your own custom template to do the livestream according to your layout choice.

This guide will provide an overview of how to implement start and stop RTMP Livestreaming.

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<LivestreamOutput>. Each LivestreamOutput 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 should look. Every field is optional.

    final 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. On Android, a call made while the room reconnects reports 3065 on 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 RoomScreen extends StatefulWidget {
...
}

class _RoomScreenState extends State<RoomScreen> {
late Room _room;

@override
void initState() {
...
}

@override
Widget build(BuildContext context) {
return Column(
children:[
ElevatedButton(
onPressed:() async {
final 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 room livestream state changes, then onLivestreamStateChanged event will trigger.
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:videosdk/videosdk.dart';

class RoomScreen extends StatefulWidget {
...
}

class _RoomScreenState extends State<RoomScreen> {
late Room room;

@override
void initState() {
...

setupRoomEventListener();
}

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

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