livechannel.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. package sample
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  6. )
  7. // CreateLiveChannelSample - 创建livechannel的sample
  8. func CreateLiveChannelSample() {
  9. channelName := "create-livechannel"
  10. //创建bucket
  11. bucket, err := GetTestBucket(bucketName)
  12. if err != nil {
  13. HandleError(err)
  14. }
  15. // 场景1 - 完整配置livechannel创建
  16. config := oss.LiveChannelConfiguration{
  17. Description: "sample-for-livechannel", //livechannel的描述信息,最长128字节,非必须
  18. Status: "enabled", //指定livechannel的状态,非必须(默认值:"enabled", 值只能是"enabled", "disabled")
  19. Target: oss.LiveChannelTarget{
  20. Type: "HLS", //指定转储的类型,只支持HLS, 必须
  21. FragDuration: 10, //指定每个ts文件的时长(单位:秒),取值范围为【1,100】的整数, 非必须(默认值:5)
  22. FragCount: 4, //指定m3u8文件中包含ts文件的个数,取值范围为【1,100】的整数,非必须(默认值:3)
  23. PlaylistName: "test-get-channel-status.m3u8", //当Type为HLS时,指定生成m3u8文件的名称,必须以“.m3u8”结尾,长度范围为【6,128】,非必须(默认值:playlist.m3u8)
  24. },
  25. }
  26. result, err := bucket.CreateLiveChannel(channelName, config)
  27. if err != nil {
  28. HandleError(err)
  29. }
  30. playURL := result.PlayUrls[0]
  31. publishURL := result.PublishUrls[0]
  32. fmt.Printf("create livechannel:%s with config respones: playURL:%s, publishURL: %s\n", channelName, playURL, publishURL)
  33. // 场景2 - 简单配置livechannel创建
  34. simpleCfg := oss.LiveChannelConfiguration{
  35. Target: oss.LiveChannelTarget{
  36. Type: "HLS",
  37. },
  38. }
  39. result, err = bucket.CreateLiveChannel(channelName, simpleCfg)
  40. if err != nil {
  41. HandleError(err)
  42. }
  43. playURL = result.PlayUrls[0]
  44. publishURL = result.PublishUrls[0]
  45. fmt.Printf("create livechannel:%s with simple config respones: playURL:%s, publishURL: %s\n", channelName, playURL, publishURL)
  46. err = DeleteTestBucketAndLiveChannel(bucketName)
  47. if err != nil {
  48. HandleError(err)
  49. }
  50. fmt.Println("PutObjectSample completed")
  51. }
  52. // PutLiveChannelStatusSample - 设置直播频道的状态的sample,有两种状态可选:enabled和disabled
  53. func PutLiveChannelStatusSample() {
  54. channelName := "put-livechannel-status"
  55. bucket, err := GetTestBucket(bucketName)
  56. if err != nil {
  57. HandleError(err)
  58. }
  59. config := oss.LiveChannelConfiguration{
  60. Target: oss.LiveChannelTarget{
  61. Type: "HLS", //指定转储的类型,只支持HLS, 必须
  62. },
  63. }
  64. _, err = bucket.CreateLiveChannel(channelName, config)
  65. if err != nil {
  66. HandleError(err)
  67. }
  68. // 场景1 - 设置livechannel的状态为disabled
  69. err = bucket.PutLiveChannelStatus(channelName, "disabled")
  70. if err != nil {
  71. HandleError(err)
  72. }
  73. // 场景2 - 设置livechannel的状态为enalbed
  74. err = bucket.PutLiveChannelStatus(channelName, "enabled")
  75. if err != nil {
  76. HandleError(err)
  77. }
  78. err = DeleteTestBucketAndLiveChannel(bucketName)
  79. if err != nil {
  80. HandleError(err)
  81. }
  82. fmt.Println("PutLiveChannelStatusSample completed")
  83. }
  84. // PostVodPlayListSample - 生成点播列表的sample
  85. func PostVodPlayListSample() {
  86. channelName := "post-vod-playlist"
  87. playlistName := "playlist.m3u8"
  88. bucket, err := GetTestBucket(bucketName)
  89. if err != nil {
  90. HandleError(err)
  91. }
  92. config := oss.LiveChannelConfiguration{
  93. Target: oss.LiveChannelTarget{
  94. Type: "HLS", //指定转储的类型,只支持HLS, 必须
  95. PlaylistName: "playlist.m3u8",
  96. },
  97. }
  98. _, err = bucket.CreateLiveChannel(channelName, config)
  99. if err != nil {
  100. HandleError(err)
  101. }
  102. //这个阶段可以推流了...
  103. endTime := time.Now().Add(-1 * time.Minute)
  104. startTime := endTime.Add(-60 * time.Minute)
  105. err = bucket.PostVodPlaylist(channelName, playlistName, startTime, endTime)
  106. if err != nil {
  107. HandleError(err)
  108. }
  109. err = DeleteTestBucketAndLiveChannel(bucketName)
  110. if err != nil {
  111. HandleError(err)
  112. }
  113. fmt.Println("PostVodPlayListSampleSample completed")
  114. }
  115. // GetLiveChannelStatSample - 获取指定直播流频道当前推流的状态的sample
  116. func GetLiveChannelStatSample() {
  117. channelName := "get-livechannel-stat"
  118. bucket, err := GetTestBucket(bucketName)
  119. if err != nil {
  120. HandleError(err)
  121. }
  122. config := oss.LiveChannelConfiguration{
  123. Target: oss.LiveChannelTarget{
  124. Type: "HLS", //指定转储的类型,只支持HLS, 必须
  125. },
  126. }
  127. _, err = bucket.CreateLiveChannel(channelName, config)
  128. if err != nil {
  129. HandleError(err)
  130. }
  131. stat, err := bucket.GetLiveChannelStat(channelName)
  132. if err != nil {
  133. HandleError(err)
  134. }
  135. status := stat.Status
  136. connectedTime := stat.ConnectedTime
  137. remoteAddr := stat.RemoteAddr
  138. audioBW := stat.Audio.Bandwidth
  139. audioCodec := stat.Audio.Codec
  140. audioSampleRate := stat.Audio.SampleRate
  141. videoBW := stat.Video.Bandwidth
  142. videoFrameRate := stat.Video.FrameRate
  143. videoHeight := stat.Video.Height
  144. videoWidth := stat.Video.Width
  145. 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)
  146. err = DeleteTestBucketAndLiveChannel(bucketName)
  147. if err != nil {
  148. HandleError(err)
  149. }
  150. fmt.Println("GetLiveChannelStatSample completed")
  151. }
  152. // GetLiveChannelInfoSample - 获取直播流频道的配置信息的sample
  153. func GetLiveChannelInfoSample() {
  154. channelName := "get-livechannel-info"
  155. bucket, err := GetTestBucket(bucketName)
  156. if err != nil {
  157. HandleError(err)
  158. }
  159. config := oss.LiveChannelConfiguration{
  160. Target: oss.LiveChannelTarget{
  161. Type: "HLS", //指定转储的类型,只支持HLS, 必须
  162. },
  163. }
  164. _, err = bucket.CreateLiveChannel(channelName, config)
  165. if err != nil {
  166. HandleError(err)
  167. }
  168. info, err := bucket.GetLiveChannelInfo(channelName)
  169. if err != nil {
  170. HandleError(err)
  171. }
  172. desc := info.Description
  173. status := info.Status
  174. fragCount := info.Target.FragCount
  175. fragDuation := info.Target.FragDuration
  176. playlistName := info.Target.PlaylistName
  177. targetType := info.Target.Type
  178. fmt.Printf("get channel stat:(%v,%v, %v), target(%v, %v, %v, %v)\n", channelName, desc, status, fragCount, fragDuation, playlistName, targetType)
  179. err = DeleteTestBucketAndLiveChannel(bucketName)
  180. if err != nil {
  181. HandleError(err)
  182. }
  183. fmt.Println("GetLiveChannelInfoSample completed")
  184. }
  185. // GetLiveChannelHistorySample - 获取直播流频道的历史推流记录的sample
  186. func GetLiveChannelHistorySample() {
  187. channelName := "get-livechannel-info"
  188. bucket, err := GetTestBucket(bucketName)
  189. if err != nil {
  190. HandleError(err)
  191. }
  192. config := oss.LiveChannelConfiguration{
  193. Target: oss.LiveChannelTarget{
  194. Type: "HLS", //指定转储的类型,只支持HLS, 必须
  195. },
  196. }
  197. _, err = bucket.CreateLiveChannel(channelName, config)
  198. if err != nil {
  199. HandleError(err)
  200. }
  201. //目前最多会返回指定LiveChannel最近的10次推流记录
  202. history, err := bucket.GetLiveChannelHistory(channelName)
  203. for _, record := range history.Record {
  204. remoteAddr := record.RemoteAddr
  205. startTime := record.StartTime
  206. endTime := record.EndTime
  207. fmt.Printf("get channel:%s history:(%v, %v, %v)\n", channelName, remoteAddr, startTime, endTime)
  208. }
  209. err = DeleteTestBucketAndLiveChannel(bucketName)
  210. if err != nil {
  211. HandleError(err)
  212. }
  213. fmt.Println("GetLiveChannelHistorySample completed")
  214. }
  215. // ListLiveChannelSample - 获取当前bucket下直播流频道的信息列表的sample
  216. func ListLiveChannelSample() {
  217. channelName := "list-livechannel"
  218. bucket, err := GetTestBucket(bucketName)
  219. if err != nil {
  220. HandleError(err)
  221. }
  222. config := oss.LiveChannelConfiguration{
  223. Target: oss.LiveChannelTarget{
  224. Type: "HLS", //指定转储的类型,只支持HLS, 必须
  225. },
  226. }
  227. _, err = bucket.CreateLiveChannel(channelName, config)
  228. if err != nil {
  229. HandleError(err)
  230. }
  231. // 场景1:列出当前bucket下所有的livechannel
  232. marker := ""
  233. for {
  234. //设定marker值,第一次执行时为”“,后续执行时以返回结果的nextmarker的值作为marker的值
  235. result, err := bucket.ListLiveChannel(oss.Marker(marker))
  236. if err != nil {
  237. HandleError(err)
  238. }
  239. for _, channel := range result.LiveChannel {
  240. 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])
  241. }
  242. if result.IsTruncated {
  243. marker = result.NextMarker
  244. } else {
  245. break
  246. }
  247. }
  248. // 场景2:使用参数”max-keys“指定返回记录的最大个数, 但max-keys的值不能超过1000
  249. result, err := bucket.ListLiveChannel(oss.MaxKeys(10))
  250. if err != nil {
  251. HandleError(err)
  252. }
  253. for _, channel := range result.LiveChannel {
  254. 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])
  255. }
  256. // 场景3;使用参数”prefix“过滤只列出包含”prefix“的值作为前缀的livechannel
  257. // max-keys, prefix, maker参数可以组合使用
  258. result, err = bucket.ListLiveChannel(oss.MaxKeys(10), oss.Prefix("list-"))
  259. if err != nil {
  260. HandleError(err)
  261. }
  262. for _, channel := range result.LiveChannel {
  263. 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])
  264. }
  265. err = DeleteTestBucketAndLiveChannel(bucketName)
  266. if err != nil {
  267. HandleError(err)
  268. }
  269. fmt.Println("ListLiveChannelSample completed")
  270. }
  271. // DeleteLiveChannelSample - 删除直播频道的信息列表的sample
  272. func DeleteLiveChannelSample() {
  273. channelName := "delete-livechannel"
  274. bucket, err := GetTestBucket(bucketName)
  275. if err != nil {
  276. HandleError(err)
  277. }
  278. config := oss.LiveChannelConfiguration{
  279. Target: oss.LiveChannelTarget{
  280. Type: "HLS", //指定转储的类型,只支持HLS, 必须
  281. },
  282. }
  283. _, err = bucket.CreateLiveChannel(channelName, config)
  284. if err != nil {
  285. HandleError(err)
  286. }
  287. err = bucket.DeleteLiveChannel(channelName)
  288. if err != nil {
  289. HandleError(err)
  290. }
  291. err = DeleteTestBucketAndLiveChannel(bucketName)
  292. if err != nil {
  293. HandleError(err)
  294. }
  295. fmt.Println("DeleteLiveChannelSample completed")
  296. }
  297. // SignRtmpURLSample - 创建签名推流地址的sample
  298. func SignRtmpURLSample() {
  299. channelName := "sign-rtmp-url"
  300. playlistName := "playlist.m3u8"
  301. bucket, err := GetTestBucket(bucketName)
  302. if err != nil {
  303. HandleError(err)
  304. }
  305. config := oss.LiveChannelConfiguration{
  306. Target: oss.LiveChannelTarget{
  307. Type: "HLS", //指定转储的类型,只支持HLS, 必须
  308. PlaylistName: "playlist.m3u8",
  309. },
  310. }
  311. result, err := bucket.CreateLiveChannel(channelName, config)
  312. if err != nil {
  313. HandleError(err)
  314. }
  315. playURL := result.PlayUrls[0]
  316. publishURL := result.PublishUrls[0]
  317. fmt.Printf("livechannel:%s, playURL:%s, publishURL: %s\n", channelName, playURL, publishURL)
  318. signedRtmpURL, err := bucket.SignRtmpURL(channelName, playlistName, 3600)
  319. if err != nil {
  320. HandleError(err)
  321. }
  322. fmt.Printf("livechannel:%s, sinedRtmpURL: %s\n", channelName, signedRtmpURL)
  323. err = DeleteTestBucketAndLiveChannel(bucketName)
  324. if err != nil {
  325. HandleError(err)
  326. }
  327. fmt.Println("PutObjectSample completed")
  328. }