Este tópico descreve operações comuns em LiveChannels, como criação, listagem e exclusão.
Crie um LiveChannel
Antes de enviar 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 ingestão e reprodução de streams. Você também pode usar o nome do LiveChannel para executar operações relacionadas, como consultar o status da ingestão de stream, consultar registros de ingestão e desativar a ingestão.
O 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 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)
}
Defina o status de um LiveChannel
O 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 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 informações de status de um LiveChannel
O código a seguir mostra como recuperar o status de ingestão de stream 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 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 especificado. O OSS consulta os arquivos .ts gerados pela ingestão de stream no LiveChannel dentro de um intervalo de tempo definido e os combina em uma playlist .m3u8.
O 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)
}
}
Visualize a playlist de um LiveChannel
O código a seguir mostra como visualizar a playlist gerada pela ingestão de stream de um LiveChannel específico em um período 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 stream de um LiveChannel especificado.
O código a seguir mostra como recuperar os registros de ingestão de stream 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)
}
Exclua um LiveChannel
Se um cliente estiver enviando uma stream para o LiveChannel, a solicitação de exclusão falhará.
A operação DeleteLiveChannel exclui apenas o LiveChannel. Ela não exclui os arquivos gerados pela ingestão de stream.
O 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 obter o código de exemplo completo para LiveChannel, consulte o exemplo no GitHub.
Para obter detalhes sobre a operação de API PutLiveChannel, consulte PutLiveChannel.
Para obter detalhes sobre a operação de API ListLiveChannel, consulte ListLiveChannel.
Para obter detalhes sobre a operação de API PutLiveChannelStatus, consulte PutLiveChannelStatus.
Para obter detalhes sobre a operação de API GetLiveChannelStat, consulte GetLiveChannelStat.
Para obter detalhes sobre a operação de API GetLiveChannelInfo, consulte GetLiveChannelInfo.
Para obter detalhes sobre a operação de API PostVodPlaylist, consulte PostVodPlaylist.
Para obter detalhes sobre a operação de API GetVodPlaylist, consulte GetVodPlaylist.
Para obter detalhes sobre a operação de API GetLiveChannelHistory, consulte GetLiveChannelHistory.
Para obter detalhes sobre a operação de API DeleteLiveChannel, consulte DeleteLiveChannel.