All Products
Search
Document Center

Object Storage Service:Common operations on LiveChannels

Last Updated:Oct 19, 2023

This topic describes the common operations that you can perform on LiveChannels, such as creating, listing, and deleting LiveChannels.

Create a LiveChannel

Before you can upload audio and video data by using Real-Time Messaging Protocol (RTMP), you must call the PutLiveChannel operation to create a LiveChannel. The response to the PutLiveChannel request includes the URL that is used to ingest streams to the LiveChannel and the URL that is used to play the ingested streams.

The following code provides an example on how to create a LiveChannel:

const OSS = require('ali-oss');

const store = new OSS({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourRegion',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // Specify the name of the bucket. 
  bucket: 'examplebucket',
})

// Specify the name of the LiveChannel that you want to create. The name cannot contain forward slashes (/). Example: mychannel. 
const cid = 'mychannel';
const conf = {
  // Specify the description of the LiveChannel. The description can be up to 128 bytes in length. 
  Description: 'this is my channel',
  // Specify the status of the LiveChannel. In this example, the Status parameter is set to enabled, which indicates that the LiveChannel is enabled. If you want to disable the LiveChannel, set the Status parameter to disabled. 
  Status: 'enabled',
  Target: {
    // Specify the format in which the LiveChannel stores the uploaded data. Only the HTTP Live Streaming (HLS) format is supported. 
    Type: 'HLS',
    // Specify the duration of each TS segment. Unit: seconds. 
    FragDuration: '10',
    // If you set Type to HLS, you must specify the number of TS segments that are included in the M3U8 file. 
    FragCount: '5',
    // If you set Type to HLS, you must specify the name of the generated M3U8 file. The name must be 6 to 128 bytes in length and end with .m3u8. 
    PlaylistName: 'playlist.m3u8'
  }
};

// Create the LiveChannel. 
store.putChannel(cid, conf).then(r=>console.log(r))

Query the configurations of a LiveChannel

The following code provides an example on how to query the configurations of a LiveChannel:

const OSS = require('ali-oss');

const store = new OSS({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourRegion',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // Specify the name of the bucket. 
  bucket: 'examplebucket',
})

// Specify the name of the LiveChannel.
const cid = 'mychannel'; 

// Query the configurations of the LiveChannel. 
store.getChannel(cid).then(r=>console.log(r));

Configure the status of a LiveChannel

The following code provides an example on how to configure the status of a LiveChannel:

const OSS = require('ali-oss');

const store = new OSS({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourRegion',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // Specify the name of the bucket. 
  bucket: 'examplebucket',
})

// Specify the name of the LiveChannel. 
const cid = 'mychannel';

// A LiveChannel can be in one of the following states: enabled or disabled. 
// If a LiveChannel is in the disabled state, you cannot ingest streams to the LiveChannel. If you ingest a stream to a LiveChannel that is in the disabled state, your client is disconnected from the LiveChannel after approximately 10 seconds. 
store.putChannelStatus(cid, 'disabled').then(r=>console.log(r));

Query the status of a LiveChannel

The following code provides an example on how to query the stream ingest status of a LiveChannel:

const OSS = require('ali-oss');

const store = new OSS({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourRegion',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // Specify the name of the bucket. 
  bucket: 'examplebucket',
})

// Specify the name of the LiveChannel. 
const cid = 'mychannel';
// Query the status of the LiveChannel. 
store.getChannelStatus(cid).then(r=>console.log(r))

Generate a playlist for a LiveChannel

You can call the PostVodPlaylist operation to generate a VOD playlist for a specified LiveChannel. OSS queries the TS segments that are generated by the streams ingested to the specified LiveChannel within a specific time period and converges the segments into an M3U8 playlist.

The following code provides an example on how to generate a playlist for a specified LiveChannel:

const OSS = require('ali-oss');

const store = new OSS({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourRegion',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // Specify the name of the bucket. 
  bucket: 'examplebucket',
})

// Specify the name of the LiveChannel. 
const cid = 'mychannel';

const r = await this.store.createVod(cid, 're-play', {
  // Specify the beginning of the time range to query the TS segments that are generated. The value is a Unix timestamp. 
  startTime: 1460464870,
  // Specify the end of the time range to query the TS segments that are generated. The value is a Unix timestamp. 
  endTime: 1460465877
  // The value of EndTime must be greater than the value of StartTime. The time range between EndTime and StartTime must be less than or equal to one day. 
}).then(r=>console.log(r))

List specified LiveChannels

The following code provides an example on how to list specified LiveChannels:

const OSS = require('ali-oss');

const store = new OSS({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourRegion',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // Specify the name of the bucket. 
  bucket: 'examplebucket',
})

const r = await this.store.listChannels({
  // List LiveChannels whose names contain the 'my' prefix. 
  prefix: 'my',
  // Set the maximum number of LiveChannels to return to 3. 
  'max-keys': 3
}).then(r=>console.log(r))

Query the stream ingest records of a LiveChannel

You can call the GetLiveChannelHistory operation to query the stream ingest records of a specified LiveChannel. The response to a GetLiveChannelHistory request includes up to the 10 most recent stream ingest records of the specified LiveChannel.

The following code provides an example on how to query the stream ingest records of a LiveChannel:

const OSS = require('ali-oss');

const store = new OSS({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourRegion',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // Specify the name of the bucket. 
  bucket: 'examplebucket',
})

// Specify the name of the LiveChannel. 
const cid = 'mychannel';
// Query the stream ingest records of the LiveChannel. 
store.getChannelHistory(cid).then(r=>console.log(r))

Delete a LiveChannel

Important
  • If you send a DeleteLiveChannel request to delete a LiveChannel to which a client is ingesting streams, the request fails.

  • The DeleteLiveChannel operation deletes only the LiveChannel and does not delete the files that are generated by the streams ingested to the LiveChannel.

The following code provides an example on how to delete a LiveChannel:

const OSS = require('ali-oss');

const store = new OSS({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourRegion',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // Specify the name of the bucket. 
  bucket: 'examplebucket',
})

// Specify the name of the LiveChannel. 
const cid = 'mychannel'; 

// Delete the LiveChannel. 
store.deleteChannel(cid).then(r=>console.log(r))

References

  • For more information about the API operation that you can call to create a LiveChannel, see PutLiveChannel.

  • For more information about the API operation that you can call to query the configurations of a LiveChannel, see GetLiveChannelInfo.

  • For more information about the API operation that you can call to configure the status of a LiveChannel, see PutLiveChannelStatus.

  • For more information about the API operation that you can call to query the stream ingest status of a LiveChannel, see GetLiveChannelStat.

  • For more information about the API operation that you can call to list specified LiveChannels, see ListLiveChannel.

  • For more information about the API operation that you can call to generate a playlist for a LiveChannel, see PostVodPlaylist.

  • For more information about the API operation that you can call to query the stream ingest records of a LiveChannel, see GetLiveChannelHistory.

  • For more information about the API operation that you can call to delete a LiveChannel, see DeleteLiveChannel.