OSDN Git Service

Create ossClient.go (#574)
[bytom/vapor.git] / vendor / github.com / aliyun / aliyun-oss-go-sdk / sample / livechannel.go
1 package sample
2
3 import (
4         "fmt"
5         "io/ioutil"
6         "time"
7
8         "github.com/aliyun/aliyun-oss-go-sdk/oss"
9 )
10
11 // CreateLiveChannelSample Samples for create a live-channel
12 func CreateLiveChannelSample() {
13         channelName := "create-livechannel"
14         //create bucket
15         bucket, err := GetTestBucket(bucketName)
16         if err != nil {
17                 HandleError(err)
18         }
19
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
29                 },
30         }
31
32         result, err := bucket.CreateLiveChannel(channelName, config)
33         if err != nil {
34                 HandleError(err)
35         }
36
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)
40
41         // Case 2 - Create live-channel only specified type of target which is required
42         simpleCfg := oss.LiveChannelConfiguration{
43                 Target: oss.LiveChannelTarget{
44                         Type: "HLS",
45                 },
46         }
47         result, err = bucket.CreateLiveChannel(channelName, simpleCfg)
48         if err != nil {
49                 HandleError(err)
50         }
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)
54
55         err = DeleteTestBucketAndLiveChannel(bucketName)
56         if err != nil {
57                 HandleError(err)
58         }
59
60         fmt.Println("PutObjectSample completed")
61 }
62
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)
67         if err != nil {
68                 HandleError(err)
69         }
70
71         config := oss.LiveChannelConfiguration{
72                 Target: oss.LiveChannelTarget{
73                         Type: "HLS", //the type of object, only supports HLS, required
74                 },
75         }
76
77         _, err = bucket.CreateLiveChannel(channelName, config)
78         if err != nil {
79                 HandleError(err)
80         }
81
82         // Case 1 - Set the status of live-channel to disabled
83         err = bucket.PutLiveChannelStatus(channelName, "disabled")
84         if err != nil {
85                 HandleError(err)
86         }
87
88         // Case 2 - Set the status of live-channel to enabled
89         err = bucket.PutLiveChannelStatus(channelName, "enabled")
90         if err != nil {
91                 HandleError(err)
92         }
93
94         err = DeleteTestBucketAndLiveChannel(bucketName)
95         if err != nil {
96                 HandleError(err)
97         }
98
99         fmt.Println("PutLiveChannelStatusSample completed")
100 }
101
102 // PostVodPlayListSample Sample for generate playlist
103 func PostVodPlayListSample() {
104         channelName := "post-vod-playlist"
105         playlistName := "playlist.m3u8"
106         bucket, err := GetTestBucket(bucketName)
107         if err != nil {
108                 HandleError(err)
109         }
110
111         config := oss.LiveChannelConfiguration{
112                 Target: oss.LiveChannelTarget{
113                         Type:         "HLS", //the type of object, only supports HLS, required
114                         PlaylistName: "playlist.m3u8",
115                 },
116         }
117
118         _, err = bucket.CreateLiveChannel(channelName, config)
119         if err != nil {
120                 HandleError(err)
121         }
122
123         //This stage you can push live stream, and after that you could generator playlist
124
125         endTime := time.Now().Add(-1 * time.Minute)
126         startTime := endTime.Add(-60 * time.Minute)
127         err = bucket.PostVodPlaylist(channelName, playlistName, startTime, endTime)
128         if err != nil {
129                 HandleError(err)
130         }
131
132         err = DeleteTestBucketAndLiveChannel(bucketName)
133         if err != nil {
134                 HandleError(err)
135         }
136
137         fmt.Println("PostVodPlayListSampleSample completed")
138 }
139
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)
144         if err != nil {
145                 HandleError(err)
146         }
147
148         config := oss.LiveChannelConfiguration{
149                 Target: oss.LiveChannelTarget{
150                         Type:         "HLS", //the type of object, only supports HLS, required
151                         PlaylistName: "playlist.m3u8",
152                 },
153         }
154
155         _, err = bucket.CreateLiveChannel(channelName, config)
156         if err != nil {
157                 HandleError(err)
158         }
159
160         //This stage you can push live stream, and after that you could generator playlist
161
162         endTime := time.Now().Add(-1 * time.Minute)
163         startTime := endTime.Add(-60 * time.Minute)
164         body, err := bucket.GetVodPlaylist(channelName, startTime, endTime)
165         if err != nil {
166                 HandleError(err)
167         }
168         defer body.Close()
169
170         data, err := ioutil.ReadAll(body)
171         if err != nil {
172                 HandleError(err)
173         }
174         fmt.Printf("content of playlist is:%v\n", string(data))
175
176         err = DeleteTestBucketAndLiveChannel(bucketName)
177         if err != nil {
178                 HandleError(err)
179         }
180
181         fmt.Println("PostVodPlayListSampleSample completed")
182 }
183
184 // GetLiveChannelStatSample Sample for get the state of live-channel
185 func GetLiveChannelStatSample() {
186         channelName := "get-livechannel-stat"
187         bucket, err := GetTestBucket(bucketName)
188         if err != nil {
189                 HandleError(err)
190         }
191
192         config := oss.LiveChannelConfiguration{
193                 Target: oss.LiveChannelTarget{
194                         Type: "HLS", //the type of object, only supports HLS, required
195                 },
196         }
197
198         _, err = bucket.CreateLiveChannel(channelName, config)
199         if err != nil {
200                 HandleError(err)
201         }
202
203         stat, err := bucket.GetLiveChannelStat(channelName)
204         if err != nil {
205                 HandleError(err)
206         }
207
208         status := stat.Status
209         connectedTime := stat.ConnectedTime
210         remoteAddr := stat.RemoteAddr
211
212         audioBW := stat.Audio.Bandwidth
213         audioCodec := stat.Audio.Codec
214         audioSampleRate := stat.Audio.SampleRate
215
216         videoBW := stat.Video.Bandwidth
217         videoFrameRate := stat.Video.FrameRate
218         videoHeight := stat.Video.Height
219         videoWidth := stat.Video.Width
220
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)
222
223         err = DeleteTestBucketAndLiveChannel(bucketName)
224         if err != nil {
225                 HandleError(err)
226         }
227
228         fmt.Println("GetLiveChannelStatSample completed")
229 }
230
231 // GetLiveChannelInfoSample Sample for get the configuration infomation of live-channel
232 func GetLiveChannelInfoSample() {
233         channelName := "get-livechannel-info"
234         bucket, err := GetTestBucket(bucketName)
235         if err != nil {
236                 HandleError(err)
237         }
238
239         config := oss.LiveChannelConfiguration{
240                 Target: oss.LiveChannelTarget{
241                         Type: "HLS", //the type of object, only supports HLS, required
242                 },
243         }
244
245         _, err = bucket.CreateLiveChannel(channelName, config)
246         if err != nil {
247                 HandleError(err)
248         }
249
250         info, err := bucket.GetLiveChannelInfo(channelName)
251         if err != nil {
252                 HandleError(err)
253         }
254
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
261
262         fmt.Printf("get channel stat:(%v,%v, %v), target(%v, %v, %v, %v)\n", channelName, desc, status, fragCount, fragDuation, playlistName, targetType)
263
264         err = DeleteTestBucketAndLiveChannel(bucketName)
265         if err != nil {
266                 HandleError(err)
267         }
268
269         fmt.Println("GetLiveChannelInfoSample completed")
270 }
271
272 // GetLiveChannelHistorySample  Sample for get push records of live-channel
273 func GetLiveChannelHistorySample() {
274         channelName := "get-livechannel-info"
275         bucket, err := GetTestBucket(bucketName)
276         if err != nil {
277                 HandleError(err)
278         }
279
280         config := oss.LiveChannelConfiguration{
281                 Target: oss.LiveChannelTarget{
282                         Type: "HLS", //the type of object, only supports HLS, required
283                 },
284         }
285
286         _, err = bucket.CreateLiveChannel(channelName, config)
287         if err != nil {
288                 HandleError(err)
289         }
290
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)
298         }
299
300         err = DeleteTestBucketAndLiveChannel(bucketName)
301         if err != nil {
302                 HandleError(err)
303         }
304
305         fmt.Println("GetLiveChannelHistorySample completed")
306 }
307
308 // ListLiveChannelSample Samples for list live-channels with specified bucket name
309 func ListLiveChannelSample() {
310         channelName := "list-livechannel"
311         bucket, err := GetTestBucket(bucketName)
312         if err != nil {
313                 HandleError(err)
314         }
315
316         config := oss.LiveChannelConfiguration{
317                 Target: oss.LiveChannelTarget{
318                         Type: "HLS", //the type of object, only supports HLS, required
319                 },
320         }
321
322         _, err = bucket.CreateLiveChannel(channelName, config)
323         if err != nil {
324                 HandleError(err)
325         }
326
327         // Case 1: list all the live-channels
328         marker := ""
329         for {
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))
333                 if err != nil {
334                         HandleError(err)
335                 }
336
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])
339                 }
340
341                 if result.IsTruncated {
342                         marker = result.NextMarker
343                 } else {
344                         break
345                 }
346         }
347
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))
351         if err != nil {
352                 HandleError(err)
353         }
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])
356         }
357
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-"))
361         if err != nil {
362                 HandleError(err)
363         }
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])
366         }
367
368         err = DeleteTestBucketAndLiveChannel(bucketName)
369         if err != nil {
370                 HandleError(err)
371         }
372
373         fmt.Println("ListLiveChannelSample completed")
374 }
375
376 // DeleteLiveChannelSample Sample for delete live-channel
377 func DeleteLiveChannelSample() {
378         channelName := "delete-livechannel"
379         bucket, err := GetTestBucket(bucketName)
380         if err != nil {
381                 HandleError(err)
382         }
383
384         config := oss.LiveChannelConfiguration{
385                 Target: oss.LiveChannelTarget{
386                         Type: "HLS", //the type of object, only supports HLS, required
387                 },
388         }
389
390         _, err = bucket.CreateLiveChannel(channelName, config)
391         if err != nil {
392                 HandleError(err)
393         }
394
395         err = bucket.DeleteLiveChannel(channelName)
396         if err != nil {
397                 HandleError(err)
398         }
399
400         err = DeleteTestBucketAndLiveChannel(bucketName)
401         if err != nil {
402                 HandleError(err)
403         }
404
405         fmt.Println("DeleteLiveChannelSample completed")
406 }
407
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)
413         if err != nil {
414                 HandleError(err)
415         }
416
417         config := oss.LiveChannelConfiguration{
418                 Target: oss.LiveChannelTarget{
419                         Type:         "HLS", //the type of object, only supports HLS, required
420                         PlaylistName: "playlist.m3u8",
421                 },
422         }
423
424         result, err := bucket.CreateLiveChannel(channelName, config)
425         if err != nil {
426                 HandleError(err)
427         }
428
429         playURL := result.PlayUrls[0]
430         publishURL := result.PublishUrls[0]
431         fmt.Printf("livechannel:%s, playURL:%s, publishURL: %s\n", channelName, playURL, publishURL)
432
433         signedRtmpURL, err := bucket.SignRtmpURL(channelName, playlistName, 3600)
434         if err != nil {
435                 HandleError(err)
436         }
437         fmt.Printf("livechannel:%s, sinedRtmpURL: %s\n", channelName, signedRtmpURL)
438
439         err = DeleteTestBucketAndLiveChannel(bucketName)
440         if err != nil {
441                 HandleError(err)
442         }
443
444         fmt.Println("SignRtmpURLSample completed")
445 }