forked from aliyun/aliyun-oss-go-sdk
/
livechannel.go
445 lines (367 loc) · 12.2 KB
/
livechannel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package sample
import (
"fmt"
"io/ioutil"
"time"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
// CreateLiveChannelSample Samples for create a live-channel
func CreateLiveChannelSample() {
channelName := "create-livechannel"
//create bucket
bucket, err := GetTestBucket(bucketName)
if err != nil {
HandleError(err)
}
// Case 1 - Create live-channel with Completely configure
config := oss.LiveChannelConfiguration{
Description: "sample-for-livechannel", //description information, up to 128 bytes
Status: "enabled", //enabled or disabled
Target: oss.LiveChannelTarget{
Type: "HLS", //the type of object, only supports HLS, required
FragDuration: 10, //the length of each ts object (in seconds), in the range [1,100], default: 5
FragCount: 4, //the number of ts objects in the m3u8 object, in the range of [1,100], default: 3
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
},
}
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)
// Case 2 - Create live-channel only specified type of target which is required
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 Set the status of the live-channel 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", //the type of object, only supports HLS, required
},
}
_, err = bucket.CreateLiveChannel(channelName, config)
if err != nil {
HandleError(err)
}
// Case 1 - Set the status of live-channel to disabled
err = bucket.PutLiveChannelStatus(channelName, "disabled")
if err != nil {
HandleError(err)
}
// Case 2 - Set the status of live-channel to enabled
err = bucket.PutLiveChannelStatus(channelName, "enabled")
if err != nil {
HandleError(err)
}
err = DeleteTestBucketAndLiveChannel(bucketName)
if err != nil {
HandleError(err)
}
fmt.Println("PutLiveChannelStatusSample completed")
}
// PostVodPlayListSample Sample for generate playlist
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", //the type of object, only supports HLS, required
PlaylistName: "playlist.m3u8",
},
}
_, err = bucket.CreateLiveChannel(channelName, config)
if err != nil {
HandleError(err)
}
//This stage you can push live stream, and after that you could generator playlist
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")
}
// GetVodPlayListSample Sample for generate playlist and return the content of the playlist
func GetVodPlayListSample() {
channelName := "get-vod-playlist"
bucket, err := GetTestBucket(bucketName)
if err != nil {
HandleError(err)
}
config := oss.LiveChannelConfiguration{
Target: oss.LiveChannelTarget{
Type: "HLS", //the type of object, only supports HLS, required
PlaylistName: "playlist.m3u8",
},
}
_, err = bucket.CreateLiveChannel(channelName, config)
if err != nil {
HandleError(err)
}
//This stage you can push live stream, and after that you could generator playlist
endTime := time.Now().Add(-1 * time.Minute)
startTime := endTime.Add(-60 * time.Minute)
body, err := bucket.GetVodPlaylist(channelName, startTime, endTime)
if err != nil {
HandleError(err)
}
defer body.Close()
data, err := ioutil.ReadAll(body)
if err != nil {
HandleError(err)
}
fmt.Printf("content of playlist is:%v\n", string(data))
err = DeleteTestBucketAndLiveChannel(bucketName)
if err != nil {
HandleError(err)
}
fmt.Println("PostVodPlayListSampleSample completed")
}
// GetLiveChannelStatSample Sample for get the state of live-channel
func GetLiveChannelStatSample() {
channelName := "get-livechannel-stat"
bucket, err := GetTestBucket(bucketName)
if err != nil {
HandleError(err)
}
config := oss.LiveChannelConfiguration{
Target: oss.LiveChannelTarget{
Type: "HLS", //the type of object, only supports HLS, required
},
}
_, 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 for get the configuration infomation of live-channel
func GetLiveChannelInfoSample() {
channelName := "get-livechannel-info"
bucket, err := GetTestBucket(bucketName)
if err != nil {
HandleError(err)
}
config := oss.LiveChannelConfiguration{
Target: oss.LiveChannelTarget{
Type: "HLS", //the type of object, only supports HLS, required
},
}
_, 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 for get push records of live-channel
func GetLiveChannelHistorySample() {
channelName := "get-livechannel-info"
bucket, err := GetTestBucket(bucketName)
if err != nil {
HandleError(err)
}
config := oss.LiveChannelConfiguration{
Target: oss.LiveChannelTarget{
Type: "HLS", //the type of object, only supports HLS, required
},
}
_, err = bucket.CreateLiveChannel(channelName, config)
if err != nil {
HandleError(err)
}
//at most return up to lastest 10 push records
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 Samples for list live-channels with specified bucket name
func ListLiveChannelSample() {
channelName := "list-livechannel"
bucket, err := GetTestBucket(bucketName)
if err != nil {
HandleError(err)
}
config := oss.LiveChannelConfiguration{
Target: oss.LiveChannelTarget{
Type: "HLS", //the type of object, only supports HLS, required
},
}
_, err = bucket.CreateLiveChannel(channelName, config)
if err != nil {
HandleError(err)
}
// Case 1: list all the live-channels
marker := ""
for {
// Set the marker value, the first time is "", the value of NextMarker that returned should as the marker in the next time
// At most return up to lastest 100 live-channels if "max-keys" is not specified
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
}
}
// Case 2: Use the parameter "max-keys" to specify the maximum number of records returned, the value of max-keys cannot exceed 1000
// if "max-keys" the default value is 100
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])
}
// Case 3: Only list the live-channels with the value of parameter "prefix" as prefix
// max-keys, prefix, maker parameters can be combined
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 for delete live-channel
func DeleteLiveChannelSample() {
channelName := "delete-livechannel"
bucket, err := GetTestBucket(bucketName)
if err != nil {
HandleError(err)
}
config := oss.LiveChannelConfiguration{
Target: oss.LiveChannelTarget{
Type: "HLS", //the type of object, only supports HLS, required
},
}
_, 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 for generate a RTMP push-stream signature URL for the trusted user to push the RTMP stream to the live channel.
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", //the type of object, only supports HLS, required
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("SignRtmpURLSample completed")
}