| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- package sample
- import (
- "fmt"
- "time"
- "github.com/aliyun/aliyun-oss-go-sdk/oss"
- )
- // CreateLiveChannelSample - 创建livechannel的sample
- func CreateLiveChannelSample() {
- channelName := "create-livechannel"
- //创建bucket
- bucket, err := GetTestBucket(bucketName)
- if err != nil {
- HandleError(err)
- }
- // 场景1 - 完整配置livechannel创建
- config := oss.LiveChannelConfiguration{
- Description: "sample-for-livechannel", //livechannel的描述信息,最长128字节,非必须
- Status: "enabled", //指定livechannel的状态,非必须(默认值:"enabled", 值只能是"enabled", "disabled")
- Target: oss.LiveChannelTarget{
- Type: "HLS", //指定转储的类型,只支持HLS, 必须
- FragDuration: 10, //指定每个ts文件的时长(单位:秒),取值范围为【1,100】的整数, 非必须(默认值:5)
- FragCount: 4, //指定m3u8文件中包含ts文件的个数,取值范围为【1,100】的整数,非必须(默认值:3)
- PlaylistName: "test-get-channel-status.m3u8", //当Type为HLS时,指定生成m3u8文件的名称,必须以“.m3u8”结尾,长度范围为【6,128】,非必须(默认值:playlist.m3u8)
- },
- }
- result, err := bucket.CreateLiveChannel(channelName, config)
- if err != nil {
- HandleError(err)
- }
- playURL := result.PlayUrls[0]
- publishURL := result.PublishUrls[0]
- fmt.Printf("create livechannel:%s with config respones: playURL:%s, publishURL: %s\n", channelName, playURL, publishURL)
- // 场景2 - 简单配置livechannel创建
- simpleCfg := oss.LiveChannelConfiguration{
- Target: oss.LiveChannelTarget{
- Type: "HLS",
- },
- }
- result, err = bucket.CreateLiveChannel(channelName, simpleCfg)
- if err != nil {
- HandleError(err)
- }
- playURL = result.PlayUrls[0]
- publishURL = result.PublishUrls[0]
- fmt.Printf("create livechannel:%s with simple config respones: playURL:%s, publishURL: %s\n", channelName, playURL, publishURL)
- err = DeleteTestBucketAndLiveChannel(bucketName)
- if err != nil {
- HandleError(err)
- }
- fmt.Println("PutObjectSample completed")
- }
- // PutLiveChannelStatusSample - 设置直播频道的状态的sample,有两种状态可选:enabled和disabled
- func PutLiveChannelStatusSample() {
- channelName := "put-livechannel-status"
- bucket, err := GetTestBucket(bucketName)
- if err != nil {
- HandleError(err)
- }
- config := oss.LiveChannelConfiguration{
- Target: oss.LiveChannelTarget{
- Type: "HLS", //指定转储的类型,只支持HLS, 必须
- },
- }
- _, err = bucket.CreateLiveChannel(channelName, config)
- if err != nil {
- HandleError(err)
- }
- // 场景1 - 设置livechannel的状态为disabled
- err = bucket.PutLiveChannelStatus(channelName, "disabled")
- if err != nil {
- HandleError(err)
- }
- // 场景2 - 设置livechannel的状态为enalbed
- err = bucket.PutLiveChannelStatus(channelName, "enabled")
- if err != nil {
- HandleError(err)
- }
- err = DeleteTestBucketAndLiveChannel(bucketName)
- if err != nil {
- HandleError(err)
- }
- fmt.Println("PutLiveChannelStatusSample completed")
- }
- // PostVodPlayListSample - 生成点播列表的sample
- func PostVodPlayListSample() {
- channelName := "post-vod-playlist"
- playlistName := "playlist.m3u8"
- bucket, err := GetTestBucket(bucketName)
- if err != nil {
- HandleError(err)
- }
- config := oss.LiveChannelConfiguration{
- Target: oss.LiveChannelTarget{
- Type: "HLS", //指定转储的类型,只支持HLS, 必须
- PlaylistName: "playlist.m3u8",
- },
- }
- _, err = bucket.CreateLiveChannel(channelName, config)
- if err != nil {
- HandleError(err)
- }
- //这个阶段可以推流了...
- endTime := time.Now().Add(-1 * time.Minute)
- startTime := endTime.Add(-60 * time.Minute)
- err = bucket.PostVodPlaylist(channelName, playlistName, startTime, endTime)
- if err != nil {
- HandleError(err)
- }
- err = DeleteTestBucketAndLiveChannel(bucketName)
- if err != nil {
- HandleError(err)
- }
- fmt.Println("PostVodPlayListSampleSample completed")
- }
- // GetLiveChannelStatSample - 获取指定直播流频道当前推流的状态的sample
- func GetLiveChannelStatSample() {
- channelName := "get-livechannel-stat"
- bucket, err := GetTestBucket(bucketName)
- if err != nil {
- HandleError(err)
- }
- config := oss.LiveChannelConfiguration{
- Target: oss.LiveChannelTarget{
- Type: "HLS", //指定转储的类型,只支持HLS, 必须
- },
- }
- _, err = bucket.CreateLiveChannel(channelName, config)
- if err != nil {
- HandleError(err)
- }
- stat, err := bucket.GetLiveChannelStat(channelName)
- if err != nil {
- HandleError(err)
- }
- status := stat.Status
- connectedTime := stat.ConnectedTime
- remoteAddr := stat.RemoteAddr
- audioBW := stat.Audio.Bandwidth
- audioCodec := stat.Audio.Codec
- audioSampleRate := stat.Audio.SampleRate
- videoBW := stat.Video.Bandwidth
- videoFrameRate := stat.Video.FrameRate
- videoHeight := stat.Video.Height
- videoWidth := stat.Video.Width
- fmt.Printf("get channel stat:(%v, %v,%v, %v), audio(%v, %v, %v), video(%v, %v, %v, %v)\n", channelName, status, connectedTime, remoteAddr, audioBW, audioCodec, audioSampleRate, videoBW, videoFrameRate, videoHeight, videoWidth)
- err = DeleteTestBucketAndLiveChannel(bucketName)
- if err != nil {
- HandleError(err)
- }
- fmt.Println("GetLiveChannelStatSample completed")
- }
- // GetLiveChannelInfoSample - 获取直播流频道的配置信息的sample
- func GetLiveChannelInfoSample() {
- channelName := "get-livechannel-info"
- bucket, err := GetTestBucket(bucketName)
- if err != nil {
- HandleError(err)
- }
- config := oss.LiveChannelConfiguration{
- Target: oss.LiveChannelTarget{
- Type: "HLS", //指定转储的类型,只支持HLS, 必须
- },
- }
- _, err = bucket.CreateLiveChannel(channelName, config)
- if err != nil {
- HandleError(err)
- }
- info, err := bucket.GetLiveChannelInfo(channelName)
- if err != nil {
- HandleError(err)
- }
- desc := info.Description
- status := info.Status
- fragCount := info.Target.FragCount
- fragDuation := info.Target.FragDuration
- playlistName := info.Target.PlaylistName
- targetType := info.Target.Type
- fmt.Printf("get channel stat:(%v,%v, %v), target(%v, %v, %v, %v)\n", channelName, desc, status, fragCount, fragDuation, playlistName, targetType)
- err = DeleteTestBucketAndLiveChannel(bucketName)
- if err != nil {
- HandleError(err)
- }
- fmt.Println("GetLiveChannelInfoSample completed")
- }
- // GetLiveChannelHistorySample - 获取直播流频道的历史推流记录的sample
- func GetLiveChannelHistorySample() {
- channelName := "get-livechannel-info"
- bucket, err := GetTestBucket(bucketName)
- if err != nil {
- HandleError(err)
- }
- config := oss.LiveChannelConfiguration{
- Target: oss.LiveChannelTarget{
- Type: "HLS", //指定转储的类型,只支持HLS, 必须
- },
- }
- _, err = bucket.CreateLiveChannel(channelName, config)
- if err != nil {
- HandleError(err)
- }
- //目前最多会返回指定LiveChannel最近的10次推流记录
- history, err := bucket.GetLiveChannelHistory(channelName)
- for _, record := range history.Record {
- remoteAddr := record.RemoteAddr
- startTime := record.StartTime
- endTime := record.EndTime
- fmt.Printf("get channel:%s history:(%v, %v, %v)\n", channelName, remoteAddr, startTime, endTime)
- }
- err = DeleteTestBucketAndLiveChannel(bucketName)
- if err != nil {
- HandleError(err)
- }
- fmt.Println("GetLiveChannelHistorySample completed")
- }
- // ListLiveChannelSample - 获取当前bucket下直播流频道的信息列表的sample
- func ListLiveChannelSample() {
- channelName := "list-livechannel"
- bucket, err := GetTestBucket(bucketName)
- if err != nil {
- HandleError(err)
- }
- config := oss.LiveChannelConfiguration{
- Target: oss.LiveChannelTarget{
- Type: "HLS", //指定转储的类型,只支持HLS, 必须
- },
- }
- _, err = bucket.CreateLiveChannel(channelName, config)
- if err != nil {
- HandleError(err)
- }
- // 场景1:列出当前bucket下所有的livechannel
- marker := ""
- for {
- //设定marker值,第一次执行时为”“,后续执行时以返回结果的nextmarker的值作为marker的值
- result, err := bucket.ListLiveChannel(oss.Marker(marker))
- if err != nil {
- HandleError(err)
- }
- for _, channel := range result.LiveChannel {
- fmt.Printf("list livechannel: (%v, %v, %v, %v, %v, %v)\n", channel.Name, channel.Status, channel.Description, channel.LastModified, channel.PlayUrls[0], channel.PublishUrls[0])
- }
- if result.IsTruncated {
- marker = result.NextMarker
- } else {
- break
- }
- }
- // 场景2:使用参数”max-keys“指定返回记录的最大个数, 但max-keys的值不能超过1000
- result, err := bucket.ListLiveChannel(oss.MaxKeys(10))
- if err != nil {
- HandleError(err)
- }
- for _, channel := range result.LiveChannel {
- fmt.Printf("list livechannel: (%v, %v, %v, %v, %v, %v)\n", channel.Name, channel.Status, channel.Description, channel.LastModified, channel.PlayUrls[0], channel.PublishUrls[0])
- }
- // 场景3;使用参数”prefix“过滤只列出包含”prefix“的值作为前缀的livechannel
- // max-keys, prefix, maker参数可以组合使用
- result, err = bucket.ListLiveChannel(oss.MaxKeys(10), oss.Prefix("list-"))
- if err != nil {
- HandleError(err)
- }
- for _, channel := range result.LiveChannel {
- fmt.Printf("list livechannel: (%v, %v, %v, %v, %v, %v)\n", channel.Name, channel.Status, channel.Description, channel.LastModified, channel.PlayUrls[0], channel.PublishUrls[0])
- }
- err = DeleteTestBucketAndLiveChannel(bucketName)
- if err != nil {
- HandleError(err)
- }
- fmt.Println("ListLiveChannelSample completed")
- }
- // DeleteLiveChannelSample - 删除直播频道的信息列表的sample
- func DeleteLiveChannelSample() {
- channelName := "delete-livechannel"
- bucket, err := GetTestBucket(bucketName)
- if err != nil {
- HandleError(err)
- }
- config := oss.LiveChannelConfiguration{
- Target: oss.LiveChannelTarget{
- Type: "HLS", //指定转储的类型,只支持HLS, 必须
- },
- }
- _, err = bucket.CreateLiveChannel(channelName, config)
- if err != nil {
- HandleError(err)
- }
- err = bucket.DeleteLiveChannel(channelName)
- if err != nil {
- HandleError(err)
- }
- err = DeleteTestBucketAndLiveChannel(bucketName)
- if err != nil {
- HandleError(err)
- }
- fmt.Println("DeleteLiveChannelSample completed")
- }
- // SignRtmpURLSample - 创建签名推流地址的sample
- func SignRtmpURLSample() {
- channelName := "sign-rtmp-url"
- playlistName := "playlist.m3u8"
- bucket, err := GetTestBucket(bucketName)
- if err != nil {
- HandleError(err)
- }
- config := oss.LiveChannelConfiguration{
- Target: oss.LiveChannelTarget{
- Type: "HLS", //指定转储的类型,只支持HLS, 必须
- PlaylistName: "playlist.m3u8",
- },
- }
- result, err := bucket.CreateLiveChannel(channelName, config)
- if err != nil {
- HandleError(err)
- }
- playURL := result.PlayUrls[0]
- publishURL := result.PublishUrls[0]
- fmt.Printf("livechannel:%s, playURL:%s, publishURL: %s\n", channelName, playURL, publishURL)
- signedRtmpURL, err := bucket.SignRtmpURL(channelName, playlistName, 3600)
- if err != nil {
- HandleError(err)
- }
- fmt.Printf("livechannel:%s, sinedRtmpURL: %s\n", channelName, signedRtmpURL)
- err = DeleteTestBucketAndLiveChannel(bucketName)
- if err != nil {
- HandleError(err)
- }
- fmt.Println("PutObjectSample completed")
- }
|