8 "github.com/aliyun/aliyun-oss-go-sdk/oss"
11 // CreateLiveChannelSample Samples for create a live-channel
12 func CreateLiveChannelSample() {
13 channelName := "create-livechannel"
15 bucket, err := GetTestBucket(bucketName)
20 // Case 1 - Create live-channel with Completely configure
21 config := oss.LiveChannelConfiguration{
22 Description: "sample-for-livechannel", //description information, up to 128 bytes
23 Status: "enabled", //enabled or disabled
24 Target: oss.LiveChannelTarget{
25 Type: "HLS", //the type of object, only supports HLS, required
26 FragDuration: 10, //the length of each ts object (in seconds), in the range [1,100], default: 5
27 FragCount: 4, //the number of ts objects in the m3u8 object, in the range of [1,100], default: 3
28 PlaylistName: "test-get-channel-status.m3u8", //the name of m3u8 object, which must end with ".m3u8" and the length range is [6,128],default: playlist.m3u8
32 result, err := bucket.CreateLiveChannel(channelName, config)
37 playURL := result.PlayUrls[0]
38 publishURL := result.PublishUrls[0]
39 fmt.Printf("create livechannel:%s with config respones: playURL:%s, publishURL: %s\n", channelName, playURL, publishURL)
41 // Case 2 - Create live-channel only specified type of target which is required
42 simpleCfg := oss.LiveChannelConfiguration{
43 Target: oss.LiveChannelTarget{
47 result, err = bucket.CreateLiveChannel(channelName, simpleCfg)
51 playURL = result.PlayUrls[0]
52 publishURL = result.PublishUrls[0]
53 fmt.Printf("create livechannel:%s with simple config respones: playURL:%s, publishURL: %s\n", channelName, playURL, publishURL)
55 err = DeleteTestBucketAndLiveChannel(bucketName)
60 fmt.Println("PutObjectSample completed")
63 // PutLiveChannelStatusSample Set the status of the live-channel sample: enabled/disabled
64 func PutLiveChannelStatusSample() {
65 channelName := "put-livechannel-status"
66 bucket, err := GetTestBucket(bucketName)
71 config := oss.LiveChannelConfiguration{
72 Target: oss.LiveChannelTarget{
73 Type: "HLS", //the type of object, only supports HLS, required
77 _, err = bucket.CreateLiveChannel(channelName, config)
82 // Case 1 - Set the status of live-channel to disabled
83 err = bucket.PutLiveChannelStatus(channelName, "disabled")
88 // Case 2 - Set the status of live-channel to enabled
89 err = bucket.PutLiveChannelStatus(channelName, "enabled")
94 err = DeleteTestBucketAndLiveChannel(bucketName)
99 fmt.Println("PutLiveChannelStatusSample completed")
102 // PostVodPlayListSample Sample for generate playlist
103 func PostVodPlayListSample() {
104 channelName := "post-vod-playlist"
105 playlistName := "playlist.m3u8"
106 bucket, err := GetTestBucket(bucketName)
111 config := oss.LiveChannelConfiguration{
112 Target: oss.LiveChannelTarget{
113 Type: "HLS", //the type of object, only supports HLS, required
114 PlaylistName: "playlist.m3u8",
118 _, err = bucket.CreateLiveChannel(channelName, config)
123 //This stage you can push live stream, and after that you could generator playlist
125 endTime := time.Now().Add(-1 * time.Minute)
126 startTime := endTime.Add(-60 * time.Minute)
127 err = bucket.PostVodPlaylist(channelName, playlistName, startTime, endTime)
132 err = DeleteTestBucketAndLiveChannel(bucketName)
137 fmt.Println("PostVodPlayListSampleSample completed")
140 // GetVodPlayListSample Sample for generate playlist and return the content of the playlist
141 func GetVodPlayListSample() {
142 channelName := "get-vod-playlist"
143 bucket, err := GetTestBucket(bucketName)
148 config := oss.LiveChannelConfiguration{
149 Target: oss.LiveChannelTarget{
150 Type: "HLS", //the type of object, only supports HLS, required
151 PlaylistName: "playlist.m3u8",
155 _, err = bucket.CreateLiveChannel(channelName, config)
160 //This stage you can push live stream, and after that you could generator playlist
162 endTime := time.Now().Add(-1 * time.Minute)
163 startTime := endTime.Add(-60 * time.Minute)
164 body, err := bucket.GetVodPlaylist(channelName, startTime, endTime)
170 data, err := ioutil.ReadAll(body)
174 fmt.Printf("content of playlist is:%v\n", string(data))
176 err = DeleteTestBucketAndLiveChannel(bucketName)
181 fmt.Println("PostVodPlayListSampleSample completed")
184 // GetLiveChannelStatSample Sample for get the state of live-channel
185 func GetLiveChannelStatSample() {
186 channelName := "get-livechannel-stat"
187 bucket, err := GetTestBucket(bucketName)
192 config := oss.LiveChannelConfiguration{
193 Target: oss.LiveChannelTarget{
194 Type: "HLS", //the type of object, only supports HLS, required
198 _, err = bucket.CreateLiveChannel(channelName, config)
203 stat, err := bucket.GetLiveChannelStat(channelName)
208 status := stat.Status
209 connectedTime := stat.ConnectedTime
210 remoteAddr := stat.RemoteAddr
212 audioBW := stat.Audio.Bandwidth
213 audioCodec := stat.Audio.Codec
214 audioSampleRate := stat.Audio.SampleRate
216 videoBW := stat.Video.Bandwidth
217 videoFrameRate := stat.Video.FrameRate
218 videoHeight := stat.Video.Height
219 videoWidth := stat.Video.Width
221 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)
223 err = DeleteTestBucketAndLiveChannel(bucketName)
228 fmt.Println("GetLiveChannelStatSample completed")
231 // GetLiveChannelInfoSample Sample for get the configuration infomation of live-channel
232 func GetLiveChannelInfoSample() {
233 channelName := "get-livechannel-info"
234 bucket, err := GetTestBucket(bucketName)
239 config := oss.LiveChannelConfiguration{
240 Target: oss.LiveChannelTarget{
241 Type: "HLS", //the type of object, only supports HLS, required
245 _, err = bucket.CreateLiveChannel(channelName, config)
250 info, err := bucket.GetLiveChannelInfo(channelName)
255 desc := info.Description
256 status := info.Status
257 fragCount := info.Target.FragCount
258 fragDuation := info.Target.FragDuration
259 playlistName := info.Target.PlaylistName
260 targetType := info.Target.Type
262 fmt.Printf("get channel stat:(%v,%v, %v), target(%v, %v, %v, %v)\n", channelName, desc, status, fragCount, fragDuation, playlistName, targetType)
264 err = DeleteTestBucketAndLiveChannel(bucketName)
269 fmt.Println("GetLiveChannelInfoSample completed")
272 // GetLiveChannelHistorySample Sample for get push records of live-channel
273 func GetLiveChannelHistorySample() {
274 channelName := "get-livechannel-info"
275 bucket, err := GetTestBucket(bucketName)
280 config := oss.LiveChannelConfiguration{
281 Target: oss.LiveChannelTarget{
282 Type: "HLS", //the type of object, only supports HLS, required
286 _, err = bucket.CreateLiveChannel(channelName, config)
291 //at most return up to lastest 10 push records
292 history, err := bucket.GetLiveChannelHistory(channelName)
293 for _, record := range history.Record {
294 remoteAddr := record.RemoteAddr
295 startTime := record.StartTime
296 endTime := record.EndTime
297 fmt.Printf("get channel:%s history:(%v, %v, %v)\n", channelName, remoteAddr, startTime, endTime)
300 err = DeleteTestBucketAndLiveChannel(bucketName)
305 fmt.Println("GetLiveChannelHistorySample completed")
308 // ListLiveChannelSample Samples for list live-channels with specified bucket name
309 func ListLiveChannelSample() {
310 channelName := "list-livechannel"
311 bucket, err := GetTestBucket(bucketName)
316 config := oss.LiveChannelConfiguration{
317 Target: oss.LiveChannelTarget{
318 Type: "HLS", //the type of object, only supports HLS, required
322 _, err = bucket.CreateLiveChannel(channelName, config)
327 // Case 1: list all the live-channels
330 // Set the marker value, the first time is "", the value of NextMarker that returned should as the marker in the next time
331 // At most return up to lastest 100 live-channels if "max-keys" is not specified
332 result, err := bucket.ListLiveChannel(oss.Marker(marker))
337 for _, channel := range result.LiveChannel {
338 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])
341 if result.IsTruncated {
342 marker = result.NextMarker
348 // Case 2: Use the parameter "max-keys" to specify the maximum number of records returned, the value of max-keys cannot exceed 1000
349 // if "max-keys" the default value is 100
350 result, err := bucket.ListLiveChannel(oss.MaxKeys(10))
354 for _, channel := range result.LiveChannel {
355 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])
358 // Case 3: Only list the live-channels with the value of parameter "prefix" as prefix
359 // max-keys, prefix, maker parameters can be combined
360 result, err = bucket.ListLiveChannel(oss.MaxKeys(10), oss.Prefix("list-"))
364 for _, channel := range result.LiveChannel {
365 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])
368 err = DeleteTestBucketAndLiveChannel(bucketName)
373 fmt.Println("ListLiveChannelSample completed")
376 // DeleteLiveChannelSample Sample for delete live-channel
377 func DeleteLiveChannelSample() {
378 channelName := "delete-livechannel"
379 bucket, err := GetTestBucket(bucketName)
384 config := oss.LiveChannelConfiguration{
385 Target: oss.LiveChannelTarget{
386 Type: "HLS", //the type of object, only supports HLS, required
390 _, err = bucket.CreateLiveChannel(channelName, config)
395 err = bucket.DeleteLiveChannel(channelName)
400 err = DeleteTestBucketAndLiveChannel(bucketName)
405 fmt.Println("DeleteLiveChannelSample completed")
408 // SignRtmpURLSample Sample for generate a RTMP push-stream signature URL for the trusted user to push the RTMP stream to the live channel.
409 func SignRtmpURLSample() {
410 channelName := "sign-rtmp-url"
411 playlistName := "playlist.m3u8"
412 bucket, err := GetTestBucket(bucketName)
417 config := oss.LiveChannelConfiguration{
418 Target: oss.LiveChannelTarget{
419 Type: "HLS", //the type of object, only supports HLS, required
420 PlaylistName: "playlist.m3u8",
424 result, err := bucket.CreateLiveChannel(channelName, config)
429 playURL := result.PlayUrls[0]
430 publishURL := result.PublishUrls[0]
431 fmt.Printf("livechannel:%s, playURL:%s, publishURL: %s\n", channelName, playURL, publishURL)
433 signedRtmpURL, err := bucket.SignRtmpURL(channelName, playlistName, 3600)
437 fmt.Printf("livechannel:%s, sinedRtmpURL: %s\n", channelName, signedRtmpURL)
439 err = DeleteTestBucketAndLiveChannel(bucketName)
444 fmt.Println("SignRtmpURLSample completed")