livechannel.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. package sample
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "time"
  6. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  7. )
  8. // CreateLiveChannelSample Samples for create a live-channel
  9. func CreateLiveChannelSample() {
  10. channelName := "create-livechannel"
  11. //create bucket
  12. bucket, err := GetTestBucket(bucketName)
  13. if err != nil {
  14. HandleError(err)
  15. }
  16. // Case 1 - Create live-channel with Completely configure
  17. config := oss.LiveChannelConfiguration{
  18. Description: "sample-for-livechannel", //description information, up to 128 bytes
  19. Status: "enabled", //enabled or disabled
  20. Target: oss.LiveChannelTarget{
  21. Type: "HLS", //the type of object, only supports HLS, required
  22. FragDuration: 10, //the length of each ts object (in seconds), in the range [1,100], default: 5
  23. FragCount: 4, //the number of ts objects in the m3u8 object, in the range of [1,100], default: 3
  24. 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
  25. },
  26. }
  27. result, err := bucket.CreateLiveChannel(channelName, config)
  28. if err != nil {
  29. HandleError(err)
  30. }
  31. playURL := result.PlayUrls[0]
  32. publishURL := result.PublishUrls[0]
  33. fmt.Printf("create livechannel:%s with config respones: playURL:%s, publishURL: %s\n", channelName, playURL, publishURL)
  34. // Case 2 - Create live-channel only specified type of target which is required
  35. simpleCfg := oss.LiveChannelConfiguration{
  36. Target: oss.LiveChannelTarget{
  37. Type: "HLS",
  38. },
  39. }
  40. result, err = bucket.CreateLiveChannel(channelName, simpleCfg)
  41. if err != nil {
  42. HandleError(err)
  43. }
  44. playURL = result.PlayUrls[0]
  45. publishURL = result.PublishUrls[0]
  46. fmt.Printf("create livechannel:%s with simple config respones: playURL:%s, publishURL: %s\n", channelName, playURL, publishURL)
  47. err = DeleteTestBucketAndLiveChannel(bucketName)
  48. if err != nil {
  49. HandleError(err)
  50. }
  51. fmt.Println("PutObjectSample completed")
  52. }
  53. // PutLiveChannelStatusSample Set the status of the live-channel sample: enabled/disabled
  54. func PutLiveChannelStatusSample() {
  55. channelName := "put-livechannel-status"
  56. bucket, err := GetTestBucket(bucketName)
  57. if err != nil {
  58. HandleError(err)
  59. }
  60. config := oss.LiveChannelConfiguration{
  61. Target: oss.LiveChannelTarget{
  62. Type: "HLS", //the type of object, only supports HLS, required
  63. },
  64. }
  65. _, err = bucket.CreateLiveChannel(channelName, config)
  66. if err != nil {
  67. HandleError(err)
  68. }
  69. // Case 1 - Set the status of live-channel to disabled
  70. err = bucket.PutLiveChannelStatus(channelName, "disabled")
  71. if err != nil {
  72. HandleError(err)
  73. }
  74. // Case 2 - Set the status of live-channel to enabled
  75. err = bucket.PutLiveChannelStatus(channelName, "enabled")
  76. if err != nil {
  77. HandleError(err)
  78. }
  79. err = DeleteTestBucketAndLiveChannel(bucketName)
  80. if err != nil {
  81. HandleError(err)
  82. }
  83. fmt.Println("PutLiveChannelStatusSample completed")
  84. }
  85. // PostVodPlayListSample Sample for generate playlist
  86. func PostVodPlayListSample() {
  87. channelName := "post-vod-playlist"
  88. playlistName := "playlist.m3u8"
  89. bucket, err := GetTestBucket(bucketName)
  90. if err != nil {
  91. HandleError(err)
  92. }
  93. config := oss.LiveChannelConfiguration{
  94. Target: oss.LiveChannelTarget{
  95. Type: "HLS", //the type of object, only supports HLS, required
  96. PlaylistName: "playlist.m3u8",
  97. },
  98. }
  99. _, err = bucket.CreateLiveChannel(channelName, config)
  100. if err != nil {
  101. HandleError(err)
  102. }
  103. //This stage you can push live stream, and after that you could generator playlist
  104. endTime := time.Now().Add(-1 * time.Minute)
  105. startTime := endTime.Add(-60 * time.Minute)
  106. err = bucket.PostVodPlaylist(channelName, playlistName, startTime, endTime)
  107. if err != nil {
  108. HandleError(err)
  109. }
  110. err = DeleteTestBucketAndLiveChannel(bucketName)
  111. if err != nil {
  112. HandleError(err)
  113. }
  114. fmt.Println("PostVodPlayListSampleSample completed")
  115. }
  116. // GetVodPlayListSample Sample for generate playlist and return the content of the playlist
  117. func GetVodPlayListSample() {
  118. channelName := "get-vod-playlist"
  119. bucket, err := GetTestBucket(bucketName)
  120. if err != nil {
  121. HandleError(err)
  122. }
  123. config := oss.LiveChannelConfiguration{
  124. Target: oss.LiveChannelTarget{
  125. Type: "HLS", //the type of object, only supports HLS, required
  126. PlaylistName: "playlist.m3u8",
  127. },
  128. }
  129. _, err = bucket.CreateLiveChannel(channelName, config)
  130. if err != nil {
  131. HandleError(err)
  132. }
  133. //This stage you can push live stream, and after that you could generator playlist
  134. endTime := time.Now().Add(-1 * time.Minute)
  135. startTime := endTime.Add(-60 * time.Minute)
  136. body, err := bucket.GetVodPlaylist(channelName, startTime, endTime)
  137. if err != nil {
  138. HandleError(err)
  139. }
  140. defer body.Close()
  141. data, err := ioutil.ReadAll(body)
  142. if err != nil {
  143. HandleError(err)
  144. }
  145. fmt.Printf("content of playlist is:%v\n", string(data))
  146. err = DeleteTestBucketAndLiveChannel(bucketName)
  147. if err != nil {
  148. HandleError(err)
  149. }
  150. fmt.Println("PostVodPlayListSampleSample completed")
  151. }
  152. // GetLiveChannelStatSample Sample for get the state of live-channel
  153. func GetLiveChannelStatSample() {
  154. channelName := "get-livechannel-stat"
  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. stat, err := bucket.GetLiveChannelStat(channelName)
  169. if err != nil {
  170. HandleError(err)
  171. }
  172. status := stat.Status
  173. connectedTime := stat.ConnectedTime
  174. remoteAddr := stat.RemoteAddr
  175. audioBW := stat.Audio.Bandwidth
  176. audioCodec := stat.Audio.Codec
  177. audioSampleRate := stat.Audio.SampleRate
  178. videoBW := stat.Video.Bandwidth
  179. videoFrameRate := stat.Video.FrameRate
  180. videoHeight := stat.Video.Height
  181. videoWidth := stat.Video.Width
  182. 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)
  183. err = DeleteTestBucketAndLiveChannel(bucketName)
  184. if err != nil {
  185. HandleError(err)
  186. }
  187. fmt.Println("GetLiveChannelStatSample completed")
  188. }
  189. // GetLiveChannelInfoSample Sample for get the configuration infomation of live-channel
  190. func GetLiveChannelInfoSample() {
  191. channelName := "get-livechannel-info"
  192. bucket, err := GetTestBucket(bucketName)
  193. if err != nil {
  194. HandleError(err)
  195. }
  196. config := oss.LiveChannelConfiguration{
  197. Target: oss.LiveChannelTarget{
  198. Type: "HLS", //the type of object, only supports HLS, required
  199. },
  200. }
  201. _, err = bucket.CreateLiveChannel(channelName, config)
  202. if err != nil {
  203. HandleError(err)
  204. }
  205. info, err := bucket.GetLiveChannelInfo(channelName)
  206. if err != nil {
  207. HandleError(err)
  208. }
  209. desc := info.Description
  210. status := info.Status
  211. fragCount := info.Target.FragCount
  212. fragDuation := info.Target.FragDuration
  213. playlistName := info.Target.PlaylistName
  214. targetType := info.Target.Type
  215. fmt.Printf("get channel stat:(%v,%v, %v), target(%v, %v, %v, %v)\n", channelName, desc, status, fragCount, fragDuation, playlistName, targetType)
  216. err = DeleteTestBucketAndLiveChannel(bucketName)
  217. if err != nil {
  218. HandleError(err)
  219. }
  220. fmt.Println("GetLiveChannelInfoSample completed")
  221. }
  222. // GetLiveChannelHistorySample Sample for get push records of live-channel
  223. func GetLiveChannelHistorySample() {
  224. channelName := "get-livechannel-info"
  225. bucket, err := GetTestBucket(bucketName)
  226. if err != nil {
  227. HandleError(err)
  228. }
  229. config := oss.LiveChannelConfiguration{
  230. Target: oss.LiveChannelTarget{
  231. Type: "HLS", //the type of object, only supports HLS, required
  232. },
  233. }
  234. _, err = bucket.CreateLiveChannel(channelName, config)
  235. if err != nil {
  236. HandleError(err)
  237. }
  238. //at most return up to lastest 10 push records
  239. history, err := bucket.GetLiveChannelHistory(channelName)
  240. for _, record := range history.Record {
  241. remoteAddr := record.RemoteAddr
  242. startTime := record.StartTime
  243. endTime := record.EndTime
  244. fmt.Printf("get channel:%s history:(%v, %v, %v)\n", channelName, remoteAddr, startTime, endTime)
  245. }
  246. err = DeleteTestBucketAndLiveChannel(bucketName)
  247. if err != nil {
  248. HandleError(err)
  249. }
  250. fmt.Println("GetLiveChannelHistorySample completed")
  251. }
  252. // ListLiveChannelSample Samples for list live-channels with specified bucket name
  253. func ListLiveChannelSample() {
  254. channelName := "list-livechannel"
  255. bucket, err := GetTestBucket(bucketName)
  256. if err != nil {
  257. HandleError(err)
  258. }
  259. config := oss.LiveChannelConfiguration{
  260. Target: oss.LiveChannelTarget{
  261. Type: "HLS", //the type of object, only supports HLS, required
  262. },
  263. }
  264. _, err = bucket.CreateLiveChannel(channelName, config)
  265. if err != nil {
  266. HandleError(err)
  267. }
  268. // Case 1: list all the live-channels
  269. marker := ""
  270. for {
  271. // Set the marker value, the first time is "", the value of NextMarker that returned should as the marker in the next time
  272. // At most return up to lastest 100 live-channels if "max-keys" is not specified
  273. result, err := bucket.ListLiveChannel(oss.Marker(marker))
  274. if err != nil {
  275. HandleError(err)
  276. }
  277. for _, channel := range result.LiveChannel {
  278. 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])
  279. }
  280. if result.IsTruncated {
  281. marker = result.NextMarker
  282. } else {
  283. break
  284. }
  285. }
  286. // Case 2: Use the parameter "max-keys" to specify the maximum number of records returned, the value of max-keys cannot exceed 1000
  287. // if "max-keys" the default value is 100
  288. result, err := bucket.ListLiveChannel(oss.MaxKeys(10))
  289. if err != nil {
  290. HandleError(err)
  291. }
  292. for _, channel := range result.LiveChannel {
  293. 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])
  294. }
  295. // Case 3: Only list the live-channels with the value of parameter "prefix" as prefix
  296. // max-keys, prefix, maker parameters can be combined
  297. result, err = bucket.ListLiveChannel(oss.MaxKeys(10), oss.Prefix("list-"))
  298. if err != nil {
  299. HandleError(err)
  300. }
  301. for _, channel := range result.LiveChannel {
  302. 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])
  303. }
  304. err = DeleteTestBucketAndLiveChannel(bucketName)
  305. if err != nil {
  306. HandleError(err)
  307. }
  308. fmt.Println("ListLiveChannelSample completed")
  309. }
  310. // DeleteLiveChannelSample Sample for delete live-channel
  311. func DeleteLiveChannelSample() {
  312. channelName := "delete-livechannel"
  313. bucket, err := GetTestBucket(bucketName)
  314. if err != nil {
  315. HandleError(err)
  316. }
  317. config := oss.LiveChannelConfiguration{
  318. Target: oss.LiveChannelTarget{
  319. Type: "HLS", //the type of object, only supports HLS, required
  320. },
  321. }
  322. _, err = bucket.CreateLiveChannel(channelName, config)
  323. if err != nil {
  324. HandleError(err)
  325. }
  326. err = bucket.DeleteLiveChannel(channelName)
  327. if err != nil {
  328. HandleError(err)
  329. }
  330. err = DeleteTestBucketAndLiveChannel(bucketName)
  331. if err != nil {
  332. HandleError(err)
  333. }
  334. fmt.Println("DeleteLiveChannelSample completed")
  335. }
  336. // SignRtmpURLSample Sample for generate a RTMP push-stream signature URL for the trusted user to push the RTMP stream to the live channel.
  337. func SignRtmpURLSample() {
  338. channelName := "sign-rtmp-url"
  339. playlistName := "playlist.m3u8"
  340. bucket, err := GetTestBucket(bucketName)
  341. if err != nil {
  342. HandleError(err)
  343. }
  344. config := oss.LiveChannelConfiguration{
  345. Target: oss.LiveChannelTarget{
  346. Type: "HLS", //the type of object, only supports HLS, required
  347. PlaylistName: "playlist.m3u8",
  348. },
  349. }
  350. result, err := bucket.CreateLiveChannel(channelName, config)
  351. if err != nil {
  352. HandleError(err)
  353. }
  354. playURL := result.PlayUrls[0]
  355. publishURL := result.PublishUrls[0]
  356. fmt.Printf("livechannel:%s, playURL:%s, publishURL: %s\n", channelName, playURL, publishURL)
  357. signedRtmpURL, err := bucket.SignRtmpURL(channelName, playlistName, 3600)
  358. if err != nil {
  359. HandleError(err)
  360. }
  361. fmt.Printf("livechannel:%s, sinedRtmpURL: %s\n", channelName, signedRtmpURL)
  362. err = DeleteTestBucketAndLiveChannel(bucketName)
  363. if err != nil {
  364. HandleError(err)
  365. }
  366. fmt.Println("SignRtmpURLSample completed")
  367. }