This topic describes common operations on LiveChannels, such as creating, listing, and deleting LiveChannels.
Create a LiveChannel
Before you upload audio and video data over the Real-Time Messaging Protocol (RTMP), you must call the PutLiveChannel operation to create a LiveChannel. This operation returns an RTMP ingest URL and the corresponding playback URL.
You can use the returned URLs for stream ingest and playback. You can also use the LiveChannel name to perform related operations, such as querying the stream ingest status, querying stream ingest records, and disabling stream ingest.
The following code shows how to create a 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)
}List LiveChannels
The following code shows how to list specified LiveChannels.
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)
}Set the status of a LiveChannel
The following code shows how to set the status of a 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)
}
}Get a signed URL for a LiveChannel
The following code shows how to obtain a signed URL for a 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)
}Get the status information of a LiveChannel
The following code shows how to retrieve the stream ingest status of a specified 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 status information of the LiveChannel.
result,err := bucket.GetLiveChannelStat(channelName)
if err != nil {
HandleError(err)
}
fmt.Println(result)
}Get the configuration of a LiveChannel
The following code shows how to retrieve the configuration of a specified 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 configuration of the LiveChannel.
result,err := bucket.GetLiveChannelInfo(channelName)
if err != nil {
HandleError(err)
}
fmt.Println(result)
}Generate a playlist for a LiveChannel
The PostVodPlaylist operation generates a video-on-demand (VOD) playlist for a specified LiveChannel. OSS queries for the .ts files generated from stream ingest to the LiveChannel within a specified time range. OSS then combines them into an .m3u8 playlist.
The following code shows how to generate a playlist for a 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)
}
}View the playlist of a LiveChannel
The following code shows how to view the playlist generated from stream ingest for a specified LiveChannel within a specified time period.
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)
}Get stream ingest records of a LiveChannel
The GetLiveChannelHistory operation returns the last 10 stream ingest records for a specified LiveChannel.
The following code shows how to retrieve the stream ingest records of a 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)
}Delete a LiveChannel
If a client is ingesting a stream to the LiveChannel, the delete request fails.
The DeleteLiveChannel operation deletes only the LiveChannel. It does not delete the files generated from stream ingest.
The following code shows how to delete a specified 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)
}
// Delete the LiveChannel.
err := bucket.DeleteLiveChannel(channelName)
if err != nil {
HandleError(err)
}
}References
For the complete sample code for LiveChannel, see the GitHub sample.
For details about the PutLiveChannel API operation, see PutLiveChannel.
For details about the ListLiveChannel API operation, see ListLiveChannel.
For details about the PutLiveChannelStatus API operation, see PutLiveChannelStatus.
For details about the GetLiveChannelStat API operation, see GetLiveChannelStat.
For details about the GetLiveChannelInfo API operation, see GetLiveChannelInfo.
For details about the PostVodPlaylist API operation, see PostVodPlaylist.
For details about the GetVodPlaylist API operation, see GetVodPlaylist.
For details about the GetLiveChannelHistory API operation, see GetLiveChannelHistory.
For details about the DeleteLiveChannel API operation, see DeleteLiveChannel.