Use the OSS Node.js SDK to create and manage LiveChannels for Real-Time Messaging Protocol (RTMP) stream ingest and HTTP Live Streaming (HLS) playback.
How it works
A LiveChannel is an RTMP stream ingest endpoint that OSS manages. When you create a LiveChannel, OSS returns an RTMP ingest URL and a playback URL. Audio and video data pushed to the ingest URL is split into .ts segments and assembled into an .m3u8 playlist for HLS playback.
The typical workflow:
Create a LiveChannel to get the ingest URL and playback URL.
Enable the channel (default status) to accept incoming streams.
Check status to confirm the channel is receiving a stream.
Generate a VOD playlist from recorded
.tssegments when needed.Review history to see past stream ingest sessions.
Delete the channel when it is no longer needed.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket
The
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables set with valid access credentials
All examples in this topic use the following client initialization:
const OSS = require('ali-oss');
const store = new OSS({
// Region where the bucket is located, for example: oss-cn-hangzhou
region: 'yourRegion',
// Read access credentials from environment variables
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
bucket: 'examplebucket',
});Create a LiveChannel
Call putChannel() to create a LiveChannel. The operation returns an RTMP ingest URL and a playback URL.
const cid = 'mychannel';
const conf = {
Description: 'this is my channel',
Status: 'enabled',
Target: {
Type: 'HLS',
FragDuration: '10',
FragCount: '5',
PlaylistName: 'playlist.m3u8',
},
};
store.putChannel(cid, conf).then(r => console.log(r));Parameters:
| Parameter | Description | Constraints |
|---|---|---|
cid | LiveChannel name | No forward slashes (/) allowed |
Description | Channel description | Up to 128 bytes |
Status | Initial status | enabled or disabled |
Type | Dump format | Only HLS is supported |
FragDuration | Duration of each .ts segment, in seconds | Example: 10 |
FragCount | Number of .ts segments included in the .m3u8 playlist | Example: 5 |
PlaylistName | Name of the generated .m3u8 file | Must end with .m3u8; 6–128 bytes in length |
Get LiveChannel information
Call getChannel() to retrieve the configuration of a LiveChannel.
const cid = 'mychannel';
store.getChannel(cid).then(r => console.log(r));Set the LiveChannel status
Call putChannelStatus() to enable or disable a LiveChannel.
const cid = 'mychannel';
store.putChannelStatus(cid, 'disabled').then(r => console.log(r));When you disable a LiveChannel, OSS blocks all incoming stream ingest. If a client is currently streaming to the channel, it is disconnected after approximately 10 seconds.
Get LiveChannel status information
Call getChannelStatus() to retrieve the current stream ingest status of a LiveChannel.
const cid = 'mychannel';
store.getChannelStatus(cid).then(r => console.log(r));Generate a LiveChannel playlist
Call createVod() to generate a video-on-demand (VOD) playlist for a LiveChannel. OSS queries the .ts segments recorded within the specified time range and combines them into an .m3u8 playlist file.
const cid = 'mychannel';
store.createVod(cid, 're-play', {
startTime: 1460464870, // UNIX timestamp
endTime: 1460465877, // UNIX timestamp
}).then(r => console.log(r));Parameters:
| Parameter | Description | Constraints |
|---|---|---|
startTime | Start of the time range to query for .ts segments | UNIX timestamp; must be earlier than endTime |
endTime | End of the time range to query for .ts segments | UNIX timestamp; the time span cannot exceed one day |
List specific LiveChannels
Call listChannels() to list LiveChannels in a bucket, with optional prefix filtering and result limits.
store.listChannels({
prefix: 'my',
'max-keys': 3,
}).then(r => console.log(r));Parameters:
| Parameter | Description | Example |
|---|---|---|
prefix | Filter channels by name prefix | 'my' returns channels whose names start with my |
max-keys | Maximum number of channels to return | 3 |
Get LiveChannel stream ingest records
Call getChannelHistory() to retrieve past stream ingest records for a LiveChannel. This operation returns the last 10 stream ingest records.
const cid = 'mychannel';
store.getChannelHistory(cid).then(r => console.log(r));Delete a LiveChannel
Call deleteChannel() to delete a LiveChannel.
You cannot delete a LiveChannel while a client is actively ingesting a stream to it. The delete request fails if a stream ingest session is in progress. Deleting a LiveChannel removes only the channel itself—the .ts segments and .m3u8 files generated from previous stream ingest sessions are not deleted.
const cid = 'mychannel';
store.deleteChannel(cid).then(r => console.log(r));What's next
For the full API specification of each operation used in this topic, see: