Este tópico descreve operações comuns para LiveChannels, como criar, listar e excluir.
Criar um LiveChannel
Antes de enviar dados de áudio e vídeo por meio do Real-Time Messaging Protocol (RTMP), chame esta operação de API para criar um LiveChannel. A operação de API PutLiveChannel retorna uma URL de ingestão RTMP e a URL de reprodução correspondente.
O código a seguir demonstra como criar um LiveChannel:
const OSS = require('ali-oss');
const store = new OSS({
// Replace yourRegion with the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to oss-cn-hangzhou.
region: 'yourRegion',
// Obtain access credentials from environment variables. Before running the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'examplebucket',
})
// Specify the LiveChannel name. The name cannot contain forward slashes (/). For 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 is set to enabled. To disable the LiveChannel, set this parameter to disabled.
Status: 'enabled',
Target: {
// Specify the dump type. Only HLS is supported.
Type: 'HLS',
// Specify the duration of each .ts file in seconds.
FragDuration: '10',
// If Type is set to HLS, specify the number of .ts files in the .m3u8 file.
FragCount: '5',
// If Type is set to HLS, specify the name of the generated .m3u8 file. The name must end with .m3u8 and be 6 to 128 bytes in length.
PlaylistName: 'playlist.m3u8'
}
};
// Create a LiveChannel.
store.putChannel(cid, conf).then(r=>console.log(r))
Obter informações do LiveChannel
O exemplo a seguir recupera as informações de um LiveChannel específico:
const OSS = require('ali-oss');
const store = new OSS({
// Replace yourRegion with the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to oss-cn-hangzhou.
region: 'yourRegion',
// Obtain access credentials from environment variables. Before running the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'examplebucket',
})
// Specify the LiveChannel name.
const cid = 'mychannel';
// Get information about the LiveChannel.
store.getChannel(cid).then(r=>console.log(r));
Definir o status do LiveChannel
Use o código a seguir para definir o status de um LiveChannel:
const OSS = require('ali-oss');
const store = new OSS({
// Replace yourRegion with the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to oss-cn-hangzhou.
region: 'yourRegion',
// Obtain access credentials from environment variables. Before running the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'examplebucket',
})
// Specify the LiveChannel name.
const cid = 'mychannel';
// A LiveChannel can be enabled or disabled.
// If a LiveChannel is disabled, OSS blocks stream ingest to the channel. If a client is ingesting a stream to the channel, the client is disconnected after a delay of about 10 seconds.
store.putChannelStatus(cid, 'disabled').then(r=>console.log(r));
Obter informações de status do LiveChannel
O código abaixo ilustra como obter o status de ingestão de stream de um LiveChannel específico:
const OSS = require('ali-oss');
const store = new OSS({
// Replace yourRegion with the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to oss-cn-hangzhou.
region: 'yourRegion',
// Obtain access credentials from environment variables. Before running the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'examplebucket',
})
// Specify the LiveChannel name.
const cid = 'mychannel';
// Get the status information of the LiveChannel.
store.getChannelStatus(cid).then(r=>console.log(r))
Gerar uma playlist do LiveChannel
A operação de API PostVodPlaylist gera uma playlist de vídeo sob demanda (VOD) para um LiveChannel específico. Essa operação consulta o OSS em busca dos arquivos .ts gerados pela ingestão de stream no LiveChannel durante um intervalo de tempo especificado e os combina em uma playlist .m3u8.
Confira o código a seguir para gerar uma playlist do LiveChannel:
const OSS = require('ali-oss');
const store = new OSS({
// Replace yourRegion with the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to oss-cn-hangzhou.
region: 'yourRegion',
// Obtain access credentials from environment variables. Before running the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'examplebucket',
})
// Specify the LiveChannel name.
const cid = 'mychannel';
const r = await this.store.createVod(cid, 're-play', {
// Specify the start time to query for .ts files. The time must be a UNIX timestamp.
startTime: 1460464870,
// Specify the end time to query for .ts files. The time must be a UNIX timestamp.
endTime: 1460465877
// The end time must be later than the start time. The time span cannot exceed one day.
}).then(r=>console.log(r))
Listar LiveChannels específicos
O trecho de código a seguir lista LiveChannels específicos:
const OSS = require('ali-oss');
const store = new OSS({
// Replace yourRegion with the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to oss-cn-hangzhou.
region: 'yourRegion',
// Obtain access credentials from environment variables. Before running the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'examplebucket',
})
const r = await this.store.listChannels({
// List LiveChannels with the prefix 'my'.
prefix: 'my',
// Specify that a maximum of three LiveChannels can be returned.
'max-keys': 3
}).then(r=>console.log(r))
Obter registros de ingestão de stream do LiveChannel
A operação de API GetLiveChannelHistory recupera os registros de ingestão de stream de um LiveChannel específico. Ela retorna os 10 últimos registros de ingestão do canal especificado.
Execute o código a seguir para recuperar os registros de ingestão de stream de um LiveChannel:
const OSS = require('ali-oss');
const store = new OSS({
// Replace yourRegion with the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to oss-cn-hangzhou.
region: 'yourRegion',
// Obtain access credentials from environment variables. Before running the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'examplebucket',
})
// Specify the LiveChannel name.
const cid = 'mychannel';
// Get the stream ingest records of the LiveChannel.
store.getChannelHistory(cid).then(r=>console.log(r))
Excluir um LiveChannel
Se um cliente estiver realizando a ingestão de stream no LiveChannel, a solicitação de exclusão falha.
A operação de API DeleteLiveChannel exclui apenas o LiveChannel, mas não exclui os arquivos gerados pela ingestão de stream.
Utilize o código a seguir para excluir um LiveChannel específico:
const OSS = require('ali-oss');
const store = new OSS({
// Replace yourRegion with the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to oss-cn-hangzhou.
region: 'yourRegion',
// Obtain access credentials from environment variables. Before running the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'examplebucket',
})
// Specify the LiveChannel name.
const cid = 'mychannel';
// Delete the LiveChannel.
store.deleteChannel(cid).then(r=>console.log(r))
Referências
Para obter detalhes sobre a operação de API PutLiveChannel, consulte PutLiveChannel.
Para mais informações sobre a operação de API GetLiveChannelInfo, consulte GetLiveChannelInfo.
Para detalhes da operação de API PutLiveChannelStatus, consulte PutLiveChannelStatus.
Para informações sobre a operação de API GetLiveChannelStat, consulte GetLiveChannelStat.
Para obter detalhes sobre a operação de API que lista LiveChannels específicos, consulte ListLiveChannel.
Para informações detalhadas sobre a operação de API PostVodPlaylist, consulte PostVodPlaylist.
Para mais detalhes sobre a operação de API GetLiveChannelHistory, consulte GetLiveChannelHistory.
Para informações sobre a operação de API DeleteLiveChannel, consulte DeleteLiveChannel.