Cette rubrique décrit les opérations courantes sur les LiveChannels, telles que la création, la liste et la suppression de LiveChannels.
Créer un LiveChannel
Avant de télécharger des données audio et vidéo via le protocole Real-Time Messaging Protocol (RTMP), vous devez appeler l'opération PutLiveChannel pour créer un LiveChannel. Cette opération renvoie une URL d'ingestion RTMP et l'URL de lecture correspondante.
Vous pouvez utiliser les URL renvoyées pour l'ingestion et la lecture de flux. Vous pouvez également utiliser le nom du LiveChannel pour effectuer des opérations connexes, telles que la consultation du statut d'ingestion, la consultation des historiques d'ingestion et la désactivation de l'ingestion de flux.
Le code suivant montre comment créer un 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)
}
Lister les LiveChannels
Le code suivant montre comment lister des LiveChannels spécifiques.
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)
}
Définir le statut d'un LiveChannel
Le code suivant montre comment définir le statut d'un 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)
}
}
Obtenir une URL signée pour un LiveChannel
Le code suivant montre comment obtenir une URL signée pour un 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)
}
Obtenir les informations de statut d'un LiveChannel
Le code suivant montre comment récupérer le statut d'ingestion d'un LiveChannel spécifié.
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)
}
Obtenir la configuration d'un LiveChannel
Le code suivant montre comment récupérer la configuration d'un LiveChannel spécifié.
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)
}
Générer une playlist pour un LiveChannel
L'opération PostVodPlaylist génère une playlist vidéo à la demande (VOD) pour un LiveChannel spécifié. OSS recherche les fichiers .ts générés à partir de l'ingestion de flux vers le LiveChannel dans une plage de temps donnée. OSS les combine ensuite en une playlist .m3u8.
Le code suivant montre comment générer une playlist pour un 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)
}
}
Consulter la playlist d'un LiveChannel
Le code suivant montre comment consulter la playlist générée à partir de l'ingestion de flux pour un LiveChannel spécifié sur une période donnée.
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)
}
Obtenir les historiques d'ingestion d'un LiveChannel
L'opération GetLiveChannelHistory renvoie les 10 derniers historiques d'ingestion pour un LiveChannel spécifié.
Le code suivant montre comment récupérer les historiques d'ingestion d'un 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)
}
Supprimer un LiveChannel
Si un client ingère un flux vers le LiveChannel, la demande de suppression échoue.
L'opération DeleteLiveChannel supprime uniquement le LiveChannel. Elle ne supprime pas les fichiers générés à partir de l'ingestion de flux.
Le code suivant montre comment supprimer un LiveChannel spécifié.
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)
}
}
Références
Pour l'exemple de code complet relatif aux LiveChannels, consultez l'exemple GitHub.
Pour plus de détails sur l'opération API PutLiveChannel, consultez PutLiveChannel.
Pour plus de détails sur l'opération API ListLiveChannel, consultez ListLiveChannel.
Pour plus de détails sur l'opération API PutLiveChannelStatus, consultez PutLiveChannelStatus.
Pour plus de détails sur l'opération API GetLiveChannelStat, consultez GetLiveChannelStat.
Pour plus de détails sur l'opération API GetLiveChannelInfo, consultez GetLiveChannelInfo.
Pour plus de détails sur l'opération API PostVodPlaylist, consultez PostVodPlaylist.
Pour plus de détails sur l'opération API GetVodPlaylist, consultez GetVodPlaylist.
Pour plus de détails sur l'opération API GetLiveChannelHistory, consultez GetLiveChannelHistory.
Pour plus de détails sur l'opération API DeleteLiveChannel, consultez DeleteLiveChannel.