Este tópico descreve operações comuns com LiveChannels, como criar, listar e excluir LiveChannels.
Criar um LiveChannel
Antes de carregar dados de áudio e vídeo pelo Real-Time Messaging Protocol (RTMP), chame a operação PutLiveChannel para criar um LiveChannel. Essa operação retorna uma URL de ingestão RTMP e a URL de reprodução correspondente.
Use as URLs retornadas para a ingestão e a reprodução de streams. Use também o nome do LiveChannel para operações relacionadas, como consultar o status e os registros de ingestão de streams, além de desativar a ingestão.
O exemplo de código a seguir mostra como criar um LiveChannel:
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
)
func HandleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
func main() {
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set the Endpoint to the one that corresponds to your bucket. For example, if your bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
HandleError(err)
}
// Specify the name of the bucket that stores the LiveChannel.
bucketName := "srcexamplebucket"
bucket,err := client.Bucket(bucketName)
if err != nil {
HandleError(err)
}
// Specify the LiveChannel name. The name cannot contain forward slashes (/).
channelName := "mychannel"
// 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"
// Specify the name of the bucket that stores high-frequency snapshots.
destBucketName := "destexamplebucket"
// Specify the topic name of the lightweight MSMQ. This topic is used to notify users of the results of high-frequency snapshot operations.
notify := "exampletopic"
target := oss.LiveChannelTarget{
PlaylistName: playlistName,
// Specify the dump type. Only HLS is supported.
Type: "HLS",
// If Type is set to HLS, specify the number of .ts files in the .m3u8 file.
FragCount: 3,
// Specify the duration of each .ts file in seconds.
FragDuration: 5,
}
snapshot := oss.LiveChannelSnapshot{
// The name of the role used for high-frequency snapshot operations. The role must have write permissions on DestBucket and permissions to send messages to NotifyTopic.
RoleName: "examplerole",
// The interval for high-frequency snapshots in seconds. The value must be between 1 and 100.
Interval: 10,
DestBucket: destBucketName,
NotifyTopic: notify,
}
config := oss.LiveChannelConfiguration{
// Specify the description of the LiveChannel. The maximum length is 128 bytes.
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: target,
Snapshot: &snapshot,
}
// Create the LiveChannel.
data, err := bucket.CreateLiveChannel(channelName, config)
if err != nil {
HandleError(err)
}
fmt.Println(data)
}
Listar LiveChannels
O exemplo de código a seguir mostra como listar LiveChannels específicos:
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
)
func HandleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
func main() {
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set the Endpoint to the one that corresponds to your bucket. For example, if your bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
HandleError(err)
}
// Specify the bucket name.
bucketName := "yourBucketName"
bucket,err := client.Bucket(bucketName)
// List all LiveChannels that have the "test" prefix.
prefix := "test"
if err != nil {
HandleError(err)
}
// List the LiveChannels.
result, err := bucket.ListLiveChannel(oss.Prefix(prefix))
if err != nil {
HandleError(err)
}
fmt.Println(result)
}
Definir o status de um LiveChannel
O exemplo de código a seguir mostra como definir o status de um LiveChannel:
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
)
func HandleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
func main() {
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set the Endpoint to the one that corresponds to your bucket. For example, if your bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
HandleError(err)
}
// Specify the bucket name.
bucketName := "yourBucketName"
bucket,err := client.Bucket(bucketName)
// Specify the LiveChannel name.
channelName := "mychannel"
if err != nil {
HandleError(err)
}
// A LiveChannel can be enabled or disabled.
// If a LiveChannel is disabled, OSS denies stream ingest requests to this LiveChannel. If a client is ingesting a stream to this LiveChannel, the client is disconnected. This may take about 10 seconds.
err = bucket.PutLiveChannelStatus(channelName,"disabled")
if err != nil {
HandleError(err)
}
}
Obter uma URL assinada para um LiveChannel
O exemplo de código a seguir mostra como obter uma URL assinada para um LiveChannel:
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
)
func main() {
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set the Endpoint to the one that corresponds to your bucket. For example, if your bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Specify the bucket name, for example, examplebucket.
bucket, err := client.Bucket("examplebucket")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Specify the LiveChannel name.
channelName := "test-sign-rtmp-url"
// Specify the playlist name.
playlistName := "playlist.m3u8"
// The expiration time in a UNIX timestamp. This example sets the expiration time to one hour from now.
expiration := time.Now().Unix() + 3600
result, err := bucket.SignRtmpURL(channelName,playlistName,expiration)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Println("Signed RTMP URL:" + result)
}
Obter as informações de status de um LiveChannel
O exemplo de código a seguir mostra como recuperar o status de ingestão de streams de um LiveChannel específico:
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
)
func HandleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
func main() {
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set the Endpoint to the one that corresponds to your bucket. For example, if your bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
HandleError(err)
}
// Specify the bucket name.
bucketName := "yourBucketName"
bucket,err := client.Bucket(bucketName)
// Specify the LiveChannel name.
channelName := "mychannel"
if err != nil {
HandleError(err)
}
// Get the status information of the LiveChannel.
result,err := bucket.GetLiveChannelStat(channelName)
if err != nil {
HandleError(err)
}
fmt.Println(result)
}
Obter a configuração de um LiveChannel
O exemplo de código a seguir mostra como recuperar a configuração de um LiveChannel específico:
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
)
func HandleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
func main() {
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set the Endpoint to the one that corresponds to your bucket. For example, if your bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
HandleError(err)
}
// Specify the bucket name.
bucketName := "yourBucketName"
bucket,err := client.Bucket(bucketName)
// Specify the LiveChannel name.
channelName := "mychannel"
if err != nil {
HandleError(err)
}
// Get the configuration of the LiveChannel.
result,err := bucket.GetLiveChannelInfo(channelName)
if err != nil {
HandleError(err)
}
fmt.Println(result)
}
Gerar uma playlist para um LiveChannel
A operação PostVodPlaylist gera uma playlist de vídeo sob demanda (VOD) para um LiveChannel específico. O OSS consulta os arquivos .ts gerados a partir da ingestão de streams para o LiveChannel em um intervalo de tempo especificado e, em seguida, combina-os em uma playlist .m3u8.
O exemplo de código a seguir mostra como gerar uma playlist para um LiveChannel:
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
)
func HandleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
func main() {
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set the Endpoint to the one that corresponds to your bucket. For example, if your bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
HandleError(err)
}
// Specify the bucket name.
bucketName := "yourBucketName"
bucket,err := client.Bucket(bucketName)
// Specify the LiveChannel name.
channelName := "mychannel"
playlistName := "playlist.m3u8"
// Specify the end time to query for .ts files. The default value is the current time.
endTime := time.Now().Add(time.Minute)
// Specify the start time to query for .ts files. The default value is 60 minutes before the current time.
startTime := endTime.Add(-60 * time.Minute)
if err != nil {
HandleError(err)
}
// Generate the playlist for the LiveChannel.
err = bucket.PostVodPlaylist(channelName,playlistName,startTime,endTime)
if err != nil {
HandleError(err)
}
}
Visualizar a playlist de um LiveChannel
O exemplo de código a seguir mostra como visualizar a playlist gerada a partir da ingestão de streams para um LiveChannel específico em um período de tempo determinado:
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
)
func HandleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
func main() {
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set the Endpoint to the one that corresponds to your bucket. For example, if your bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
HandleError(err)
}
// Specify the bucket name.
bucketName := "yourBucketName"
bucket,err := client.Bucket(bucketName)
// Specify the LiveChannel name.
channelName := "mychannel"
// Specify the end time to query for .ts files. The default value is the current time.
endTime := time.Now().Add(time.Minute)
// Specify the start time to query for .ts files. The default value is 60 minutes before the current time.
startTime := endTime.Add(-60 * time.Minute)
if err != nil {
HandleError(err)
}
// View the playlist of the LiveChannel.
result,err := bucket.GetVodPlaylist(channelName,startTime,endTime)
if err != nil {
HandleError(err)
}
fmt.Println(result)
}
Obter registros de ingestão de stream de um LiveChannel
A operação GetLiveChannelHistory retorna os últimos 10 registros de ingestão de streams para um LiveChannel específico.
O exemplo de código a seguir mostra como recuperar os registros de ingestão de streams de um LiveChannel:
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
)
func HandleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
func main() {
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set the Endpoint to the one that corresponds to your bucket. For example, if your bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
HandleError(err)
}
// Specify the bucket name.
bucketName := "yourBucketName"
bucket,err := client.Bucket(bucketName)
// Specify the LiveChannel name.
channelName := "mychannel"
if err != nil {
HandleError(err)
}
// Get the stream ingest records of the LiveChannel.
result,err := bucket.GetLiveChannelHistory(channelName)
if err != nil {
HandleError(err)
}
fmt.Println(result)
}
Excluir um LiveChannel
Se um cliente estiver realizando a ingestão de um stream no LiveChannel, a solicitação de exclusão falhará.
A operação DeleteLiveChannel exclui apenas o LiveChannel. Ela não exclui os arquivos gerados a partir da ingestão de streams.
O exemplo de código a seguir mostra como excluir um LiveChannel específico:
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
)
func HandleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
func main() {
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set the Endpoint to the one that corresponds to your bucket. For example, if your bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
HandleError(err)
}
// Specify the bucket name.
bucketName := "yourBucketName"
bucket,err := client.Bucket(bucketName)
// Specify the LiveChannel name.
channelName := "mychannel"
if err != nil {
HandleError(err)
}
// Delete the LiveChannel.
err := bucket.DeleteLiveChannel(channelName)
if err != nil {
HandleError(err)
}
}
Referências
Para o código de exemplo completo do LiveChannel, consulte a amostra do GitHub.
Para obter detalhes sobre a operação da API PutLiveChannel, consulte PutLiveChannel.
Consulte ListLiveChannel para mais informações sobre a operação da API ListLiveChannel.
A documentação da operação da API PutLiveChannelStatus está disponível em PutLiveChannelStatus.
Para detalhes técnicos da operação da API GetLiveChannelStat, acesse GetLiveChannelStat.
Encontre informações completas sobre a operação da API GetLiveChannelInfo em GetLiveChannelInfo.
Para entender melhor a operação da API PostVodPlaylist, consulte PostVodPlaylist.
Veja GetVodPlaylist para obter detalhes sobre a operação da API GetVodPlaylist.
A referência da operação da API GetLiveChannelHistory está em GetLiveChannelHistory.
Para saber mais sobre a operação da API DeleteLiveChannel, consulte DeleteLiveChannel.