livechannel.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. package sample
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  6. )
  7. // CreateLiveChannelSample Samples for create a live-channel
  8. func CreateLiveChannelSample() {
  9. channelName := "create-livechannel"
  10. //create bucket
  11. bucket, err := GetTestBucket(bucketName)
  12. if err != nil {
  13. HandleError(err)
  14. }
  15. // Case 1 - Create live-channel with Completely configure
  16. config := oss.LiveChannelConfiguration{
  17. Description: "sample-for-livechannel", //description information, up to 128 bytes
  18. Status: "enabled", //enabled or disabled
  19. Target: oss.LiveChannelTarget{
  20. Type: "HLS", //the type of object, only supports HLS, required
  21. FragDuration: 10, //the length of each ts object (in seconds), in the range [1,100], default: 5
  22. FragCount: 4, //the number of ts objects in the m3u8 object, in the range of [1,100], default: 3
  23. 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
  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. // Case 2 - Create live-channel only specified type of target which is required
  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 Set the status of the live-channel 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", //the type of object, only supports HLS, required
  62. },
  63. }
  64. _, err = bucket.CreateLiveChannel(channelName, config)
  65. if err != nil {
  66. HandleError(err)
  67. }
  68. // Case 1 - Set the status of live-channel to disabled
  69. err = bucket.PutLiveChannelStatus(channelName, "disabled")
  70. if err != nil {
  71. HandleError(err)
  72. }
  73. // Case 2 - Set the status of live-channel to enabled
  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 for generate playlist
  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", //the type of object, only supports HLS, required
  95. PlaylistName: "playlist.m3u8",
  96. },
  97. }
  98. _, err = bucket.CreateLiveChannel(channelName, config)
  99. if err != nil {
  100. HandleError(err)
  101. }
  102. //This stage you can push live stream, and after that you could generator playlist
  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 for get the state of live-channel
  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", //the type of object, only supports HLS, required
  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 for get the configuration infomation of live-channel
  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", //the type of object, only supports HLS, required
  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 for get push records of live-channel
  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", //the type of object, only supports HLS, required
  195. },
  196. }
  197. _, err = bucket.CreateLiveChannel(channelName, config)
  198. if err != nil {
  199. HandleError(err)
  200. }
  201. //at most return up to lastest 10 push records
  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 Samples for list live-channels with specified bucket name
  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", //the type of object, only supports HLS, required
  225. },
  226. }
  227. _, err = bucket.CreateLiveChannel(channelName, config)
  228. if err != nil {
  229. HandleError(err)
  230. }
  231. // Case 1: list all the live-channels
  232. marker := ""
  233. for {
  234. // Set the marker value, the first time is "", the value of NextMarker that returned should as the marker in the next time
  235. // At most return up to lastest 100 live-channels if "max-keys" is not specified
  236. result, err := bucket.ListLiveChannel(oss.Marker(marker))
  237. if err != nil {
  238. HandleError(err)
  239. }
  240. for _, channel := range result.LiveChannel {
  241. 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])
  242. }
  243. if result.IsTruncated {
  244. marker = result.NextMarker
  245. } else {
  246. break
  247. }
  248. }
  249. // Case 2: Use the parameter "max-keys" to specify the maximum number of records returned, the value of max-keys cannot exceed 1000
  250. // if "max-keys" the default value is 100
  251. result, err := bucket.ListLiveChannel(oss.MaxKeys(10))
  252. if err != nil {
  253. HandleError(err)
  254. }
  255. for _, channel := range result.LiveChannel {
  256. 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])
  257. }
  258. // Case 3: Only list the live-channels with the value of parameter "prefix" as prefix
  259. // max-keys, prefix, maker parameters can be combined
  260. result, err = bucket.ListLiveChannel(oss.MaxKeys(10), oss.Prefix("list-"))
  261. if err != nil {
  262. HandleError(err)
  263. }
  264. for _, channel := range result.LiveChannel {
  265. 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])
  266. }
  267. err = DeleteTestBucketAndLiveChannel(bucketName)
  268. if err != nil {
  269. HandleError(err)
  270. }
  271. fmt.Println("ListLiveChannelSample completed")
  272. }
  273. // DeleteLiveChannelSample Sample for delete live-channel
  274. func DeleteLiveChannelSample() {
  275. channelName := "delete-livechannel"
  276. bucket, err := GetTestBucket(bucketName)
  277. if err != nil {
  278. HandleError(err)
  279. }
  280. config := oss.LiveChannelConfiguration{
  281. Target: oss.LiveChannelTarget{
  282. Type: "HLS", //the type of object, only supports HLS, required
  283. },
  284. }
  285. _, err = bucket.CreateLiveChannel(channelName, config)
  286. if err != nil {
  287. HandleError(err)
  288. }
  289. err = bucket.DeleteLiveChannel(channelName)
  290. if err != nil {
  291. HandleError(err)
  292. }
  293. err = DeleteTestBucketAndLiveChannel(bucketName)
  294. if err != nil {
  295. HandleError(err)
  296. }
  297. fmt.Println("DeleteLiveChannelSample completed")
  298. }
  299. // SignRtmpURLSample Sample for generate a RTMP push-stream signature URL for the trusted user to push the RTMP stream to the live channel.
  300. func SignRtmpURLSample() {
  301. channelName := "sign-rtmp-url"
  302. playlistName := "playlist.m3u8"
  303. bucket, err := GetTestBucket(bucketName)
  304. if err != nil {
  305. HandleError(err)
  306. }
  307. config := oss.LiveChannelConfiguration{
  308. Target: oss.LiveChannelTarget{
  309. Type: "HLS", //the type of object, only supports HLS, required
  310. PlaylistName: "playlist.m3u8",
  311. },
  312. }
  313. result, err := bucket.CreateLiveChannel(channelName, config)
  314. if err != nil {
  315. HandleError(err)
  316. }
  317. playURL := result.PlayUrls[0]
  318. publishURL := result.PublishUrls[0]
  319. fmt.Printf("livechannel:%s, playURL:%s, publishURL: %s\n", channelName, playURL, publishURL)
  320. signedRtmpURL, err := bucket.SignRtmpURL(channelName, playlistName, 3600)
  321. if err != nil {
  322. HandleError(err)
  323. }
  324. fmt.Printf("livechannel:%s, sinedRtmpURL: %s\n", channelName, signedRtmpURL)
  325. err = DeleteTestBucketAndLiveChannel(bucketName)
  326. if err != nil {
  327. HandleError(err)
  328. }
  329. fmt.Println("PutObjectSample completed")
  330. }