This topic describes common operations on LiveChannels, such as create, list, and delete LiveChannels.

Create a LiveChannel

Before you can upload audio and video data by using the Real-Time Messaging Protocol (RTMP), you must call the PutLiveChannel operation to create a LiveChannel. The response to the PutLiveChannel request includes the URL that is used to ingest streams to the LiveChannel and the URL that is used to play the ingested streams.

Important You can use the returned URLs to ingest and play streams. You can also perform operations based on the returned LiveChannel name, such as query the stream ingestion status, query stream ingestion records, and disable stream ingestion.

The following code provides an example on 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() {
    // Create an OSSClient instance. 
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
    if err != nil {
        HandleError(err)
    }

    // Specify the name of the bucket that is used to store the LiveChannel. 
    bucketName := "srcexamplebucket"
    bucket,err := client.Bucket(bucketName)

    if err != nil {
        HandleError(err)
    }

    // Specify the name of the LiveChannel that you want to create. The name cannot contain forward slashes (/). 
    channelName := "mychannel"
    // If you set Type to HLS, you must specify the name of the generated m3u8 file. The name must be 6 to 128 bytes in length and must end with .m3u8. 
    playlistName := "playlist.m3u8"
    // Specify the name of the bucket that stores the results of high-frequency snapshot operations. 
    destBucketName := "destexamplebucket"
    // Specify the name of the MNS topic that is used to notify users of the results of high-frequency screenshot operations. 
    notify := "exampletopic"
    target := oss.LiveChannelTarget{
        PlaylistName: playlistName,
        // Specify the format in which the LiveChannel stores the uploaded data. Only the HLS format is supported. 
        Type:         "HLS",
        // If you set Type to HLS, you must specify the number of TS files that are included in the m3u8 file. 
        FragCount:   3,
        // Specify the duration of each TS file. Unit: seconds. 
        FragDuration:   5,
    }
   
    snapshot := oss.LiveChannelSnapshot{
        // Specify the name of the role that is used to perform high-frequency snapshot operations. The role must have the write permissions on DestBucket and the permission to send messages to NotifyTopic. 
        RoleName:    "examplerole",
        // Specify the interval of high-frequency snapshot operations. Unit: seconds. Valid values: 1 to 100. 
        Interval:    10,        
        DestBucket:  destBucketName,
        NotifyTopic: notify,
    }
    config := oss.LiveChannelConfiguration{
        // 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 parameter is set to enabled, which indicates that the LiveChannel is enabled. If you want to disable the LiveChannel, set the Status 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 provides an example on how to list specific 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() {
    // Create an OSSClient instance. 
    // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. 
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
    client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
    if err != nil {
        HandleError(err)
    }

    // Specify the name of the bucket. 
    bucketName := "yourBucketName"
    bucket,err := client.Bucket(bucketName)
    // Specify that the LiveChannels whose names contain the test prefix are listed. 
    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)
}

Configure the LiveChannel status

The following code provides an example on how to configure 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() {
    // Create an OSSClient instance. 
    // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. 
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
    client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
    if err != nil {
        HandleError(err)
    }

    // Specify the name of the bucket. 
    bucketName := "yourBucketName"
    bucket,err := client.Bucket(bucketName)
    // Specify the name of the LiveChannel. 
    channelName := "mychannel"
   
    if err != nil {
         HandleError(err)
    }

    // A LiveChannel can be in one of the following states: enabled or disabled. 
    // If a LiveChannel is in the disabled state, OSS prohibits you from ingesting streams to the LiveChannel. If you are ingesting a stream to the LiveChannel that is in the disabled state, your client is disconnected from the LiveChannel after approximately 10 seconds. 
    err = bucket.PutLiveChannelStatus(channelName,"disabled")
    if err != nil {
        HandleError(err)
     }
}

Query the signed URL of a LiveChannel

The following code provides an example on how to query the signed URL of a LiveChannel:

package main

import (
    "fmt"
    "github.com/aliyun/aliyun-oss-go-sdk/oss"
    "os"
)

func main() {

    // Create an OSSClient instance. 
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Specify the name of the bucket. Example: examplebucket. 
    bucket, err := client.Bucket("examplebucket")
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    // Specify the name of the LiveChannel. 
    channelName := "test-sign-rtmp-url"
    // Specify the name of the playlist. 
    playlistName := "playlist.m3u8"
    // The expiration parameter indicates the validity period of the signed URL and is a timestamp that follows the UNIX time format. In this example, the validity period is set to 1 hour. 
    expiration := time.Now().Unix() + 3600
    result, err := bucket.SignRtmpURL(channelName,playlistName,expiration)
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    fmt.Println("Sign Rtmp url:"+result)
}

Query the stream ingestion status of a LiveChannel

The following code provides an example on how to query the stream ingestion status of the 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() {
    // Create an OSSClient instance. 
    // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. 
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
    client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
    if err != nil {
        HandleError(err)
    }

    // Specify the name of the bucket. 
    bucketName := "yourBucketName"
    bucket,err := client.Bucket(bucketName)
    // Specify the name of the LiveChannel. 
    channelName := "mychannel"

    if err != nil {
        HandleError(err)
    }

    // Query the stream ingestion status of the LiveChannel. 
    result,err := bucket.GetLiveChannelStat(channelName)
    if err != nil {
        HandleError(err)
    }
    fmt.Println(result)
}

Query the configurations of a LiveChannel

The following code provides an example on how to query the configurations 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() {
    // Create an OSSClient instance. 
    // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. 
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
    client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
    if err != nil {
        HandleError(err)
    }

    // Specify the name of the bucket. 
    bucketName := "yourBucketName"
    bucket,err := client.Bucket(bucketName)
    // Specify the name of the LiveChannel. 
    channelName := "mychannel"

    if err != nil {
        HandleError(err)
    }

    // Query the configurations of the LiveChannel. 
    result,err := bucket.GetLiveChannelInfo(channelName)
    if err != nil {
        HandleError(err)
    }
    fmt.Println(result)
}

Generate a playlist for a LiveChannel

You can call the PostVodPlaylist operation to generate a VOD playlist for the specified LiveChannel. OSS queries the TS files that are generated by the streams ingested to the specified LiveChannel within the specified time period and converges the files into an m3u8 playlist.

The following code provides an example on 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() {
    // Create an OSSClient instance. 
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
    if err != nil {
        HandleError(err)
    }

    // Specify the name of the bucket. 
    bucketName := "yourBucketName"
    bucket,err := client.Bucket(bucketName)
    // Specify the name of the LiveChannel. 
    channelName := "mychannel"
    playlistName := "playlist.m3u8"
    // Specify the end time of the time range during which the TS files that you want to query are generated. By default, the end time is the same as the current time. 
    endTime := time.Now().Add(time.Minute)
    // Specify the start time of the time range during which the TS files that you want to query are generated. By default, the start time is 60 minutes earlier than the current time. 
    startTime := endTime.Add(-60 * time.Minute)

    if err != nil {
        HandleError(err)
    }

    // Generate a playlist for the LiveChannel. 
    err = bucket.PostVodPlaylist(channelName,playlistName,startTime,endTime)
    if err != nil {
        HandleError(err)
    }
}

Query the playlist of a LiveChannel

The following code provides an example on how to query the playlist that is generated by the streams ingested to a specific LiveChannel within the specified time range:

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() {
    // Create an OSSClient instance. 
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
    if err != nil {
        HandleError(err)
    }

    // Specify the name of the bucket. 
    bucketName := "yourBucketName"
    bucket,err := client.Bucket(bucketName)
    // Specify the name of the LiveChannel. 
    channelName := "mychannel"
    
    // Specify the end time of the time range during which the TS files that you want to query are generated. By default, the end time is the same as the current time. 
    endTime := time.Now().Add(time.Minute)
    // Specify the start time of the time range during which the TS files that you want to query are generated. By default, the start time is 60 minutes earlier than the current time. 
    startTime := endTime.Add(-60 * time.Minute)

    if err != nil {
        HandleError(err)
    }

    // Query the playlist of the LiveChannel. 
    result,err := bucket.GetVodPlaylist(channelName,startTime,endTime)
    if err != nil {
        HandleError(err)
    }
    fmt.Println(result)
}

Query the stream ingestion records of a LiveChannel

You can call the GetLiveChannelHistory operation to query up to the 10 most recent stream ingestion records of the specified LiveChannel.

The following code provides an example on how to query the stream ingestion 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() {
    // Create an OSSClient instance. 
    // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. 
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
    client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
    if err != nil {
        HandleError(err)
    }

    // Specify the name of the bucket. 
    bucketName := "yourBucketName"
    bucket,err := client.Bucket(bucketName)
    // Specify the name of the LiveChannel. 
    channelName := "mychannel"

    if err != nil {
        HandleError(err)
    }

    // Query the stream ingestion records of the LiveChannel. 
    result,err := bucket.GetLiveChannelHistory(channelName)
    if err != nil {
        HandleError(err)
    }
    fmt.Println(result)
}

Delete a LiveChannel

Important
  • If you send a DeleteLiveChannel request to delete a LiveChannel to which a client is ingesting streams, the request fails.
  • The DeleteLiveChannel operation deletes only the LiveChannel and does not delete the files that are generated by the streams ingested to the LiveChannel.

The following code provides an example on how to delete 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() {
    // Create an OSSClient instance. 
    // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. 
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
    client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
    if err != nil {
        HandleError(err)
    }

    // Specify the name of the bucket. 
    bucketName := "yourBucketName"
    bucket,err := client.Bucket(bucketName)
    // Specify the name of the LiveChannel. 
    channelName := "mychannel"

    if err != nil {
        HandleError(err)
    }

    // Delete the LiveChannel. 
    err := bucket.DeleteLiveChannel(channelName)
    if err != nil {
        HandleError(err)
    }
 }

References

  • For more information about the complete sample code of LiveChannels, visit GitHub.
  • For more information about the API operation that you can call to create a LiveChannel, see PutLiveChannel.
  • For more information about the API operation that you can call to list LiveChannels, see ListLiveChannel.
  • For more information about the API operation that you can call to configure the LiveChannel status, see PutLiveChannelStatus.
  • For more information about the API operation that you can call to query the stream ingestion status of a LiveChannel, see GetLiveChannelStat.
  • For more information about the API operation that you can call to query the configurations of a LiveChannel, see GetLiveChannelInfo.
  • For more information about the API operation that you can call to generate a playlist for a LiveChannel, see PostVodPlaylist.
  • For more information about the API operation that you can call to query the playlist of a LiveChannel, see GetVodPlaylist.
  • For more information about the API operation that you can call to query the stream ingestion records of a LiveChannel, see GetLiveChannelHistory.
  • For more information about the API operation that you can call to delete a LiveChannel, see DeleteLiveChannel.