livechannel.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package oss
  2. import (
  3. "bytes"
  4. "encoding/xml"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "strconv"
  9. "time"
  10. )
  11. //
  12. // CreateLiveChannel create a live-channel
  13. //
  14. // channelName the name of the channel
  15. // config configuration of the channel
  16. //
  17. // CreateLiveChannelResult the result of create live-channel
  18. // error nil if success, otherwise error
  19. //
  20. func (bucket Bucket) CreateLiveChannel(channelName string, config LiveChannelConfiguration) (CreateLiveChannelResult, error) {
  21. var out CreateLiveChannelResult
  22. bs, err := xml.Marshal(config)
  23. if err != nil {
  24. return out, err
  25. }
  26. buffer := new(bytes.Buffer)
  27. buffer.Write(bs)
  28. params := map[string]interface{}{}
  29. params["live"] = nil
  30. resp, err := bucket.do("PUT", channelName, params, nil, buffer, nil)
  31. if err != nil {
  32. return out, err
  33. }
  34. defer resp.Body.Close()
  35. err = xmlUnmarshal(resp.Body, &out)
  36. return out, err
  37. }
  38. //
  39. // PutLiveChannelStatus Set the status of the live-channel: enabled/disabled
  40. //
  41. // channelName the name of the channel
  42. // status enabled/disabled
  43. //
  44. // error nil if success, otherwise error
  45. //
  46. func (bucket Bucket) PutLiveChannelStatus(channelName, status string) error {
  47. params := map[string]interface{}{}
  48. params["live"] = nil
  49. params["status"] = status
  50. resp, err := bucket.do("PUT", channelName, params, nil, nil, nil)
  51. if err != nil {
  52. return err
  53. }
  54. defer resp.Body.Close()
  55. return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
  56. }
  57. // PostVodPlaylist create an playlist based on the specified playlist name, startTime and endTime
  58. //
  59. // channelName the name of the channel
  60. // playlistName the name of the playlist, must end with ".m3u8"
  61. // startTime the start time of the playlist
  62. // endTime the endtime of the playlist
  63. //
  64. // error nil if success, otherwise error
  65. //
  66. func (bucket Bucket) PostVodPlaylist(channelName, playlistName string, startTime, endTime time.Time) error {
  67. params := map[string]interface{}{}
  68. params["vod"] = nil
  69. params["startTime"] = strconv.FormatInt(startTime.Unix(), 10)
  70. params["endTime"] = strconv.FormatInt(endTime.Unix(), 10)
  71. key := fmt.Sprintf("%s/%s", channelName, playlistName)
  72. resp, err := bucket.do("POST", key, params, nil, nil, nil)
  73. if err != nil {
  74. return err
  75. }
  76. defer resp.Body.Close()
  77. return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
  78. }
  79. // GetVodPlaylist get the playlist based on the specified channelName, startTime and endTime
  80. //
  81. // channelName the name of the channel
  82. // startTime the start time of the playlist
  83. // endTime the endtime of the playlist
  84. //
  85. // io.ReadCloser reader instance for reading data from response. It must be called close() after the usage and only valid when error is nil.
  86. // error nil if success, otherwise error
  87. //
  88. func (bucket Bucket) GetVodPlaylist(channelName string, startTime, endTime time.Time) (io.ReadCloser, error) {
  89. params := map[string]interface{}{}
  90. params["vod"] = nil
  91. params["startTime"] = strconv.FormatInt(startTime.Unix(), 10)
  92. params["endTime"] = strconv.FormatInt(endTime.Unix(), 10)
  93. resp, err := bucket.do("GET", channelName, params, nil, nil, nil)
  94. if err != nil {
  95. return nil, err
  96. }
  97. return resp.Body, nil
  98. }
  99. //
  100. // GetLiveChannelStat Get the state of the live-channel
  101. //
  102. // channelName the name of the channel
  103. //
  104. // LiveChannelStat the state of the live-channel
  105. // error nil if success, otherwise error
  106. //
  107. func (bucket Bucket) GetLiveChannelStat(channelName string) (LiveChannelStat, error) {
  108. var out LiveChannelStat
  109. params := map[string]interface{}{}
  110. params["live"] = nil
  111. params["comp"] = "stat"
  112. resp, err := bucket.do("GET", channelName, params, nil, nil, nil)
  113. if err != nil {
  114. return out, err
  115. }
  116. defer resp.Body.Close()
  117. err = xmlUnmarshal(resp.Body, &out)
  118. return out, err
  119. }
  120. //
  121. // GetLiveChannelInfo Get the configuration info of the live-channel
  122. //
  123. // channelName the name of the channel
  124. //
  125. // LiveChannelConfiguration the configuration info of the live-channel
  126. // error nil if success, otherwise error
  127. //
  128. func (bucket Bucket) GetLiveChannelInfo(channelName string) (LiveChannelConfiguration, error) {
  129. var out LiveChannelConfiguration
  130. params := map[string]interface{}{}
  131. params["live"] = nil
  132. resp, err := bucket.do("GET", channelName, params, nil, nil, nil)
  133. if err != nil {
  134. return out, err
  135. }
  136. defer resp.Body.Close()
  137. err = xmlUnmarshal(resp.Body, &out)
  138. return out, err
  139. }
  140. //
  141. // GetLiveChannelHistory Get push records of live-channel
  142. //
  143. // channelName the name of the channel
  144. //
  145. // LiveChannelHistory push records
  146. // error nil if success, otherwise error
  147. //
  148. func (bucket Bucket) GetLiveChannelHistory(channelName string) (LiveChannelHistory, error) {
  149. var out LiveChannelHistory
  150. params := map[string]interface{}{}
  151. params["live"] = nil
  152. params["comp"] = "history"
  153. resp, err := bucket.do("GET", channelName, params, nil, nil, nil)
  154. if err != nil {
  155. return out, err
  156. }
  157. defer resp.Body.Close()
  158. err = xmlUnmarshal(resp.Body, &out)
  159. return out, err
  160. }
  161. //
  162. // ListLiveChannel list the live-channels
  163. //
  164. // options Prefix: filter by the name start with the value of "Prefix"
  165. // MaxKeys: the maximum count returned
  166. // Marker: cursor from which starting list
  167. //
  168. // ListLiveChannelResult live-channel list
  169. // error nil if success, otherwise error
  170. //
  171. func (bucket Bucket) ListLiveChannel(options ...Option) (ListLiveChannelResult, error) {
  172. var out ListLiveChannelResult
  173. params, err := GetRawParams(options)
  174. if err != nil {
  175. return out, err
  176. }
  177. params["live"] = nil
  178. resp, err := bucket.do("GET", "", params, nil, nil, nil)
  179. if err != nil {
  180. return out, err
  181. }
  182. defer resp.Body.Close()
  183. err = xmlUnmarshal(resp.Body, &out)
  184. return out, err
  185. }
  186. //
  187. // DeleteLiveChannel Delete the live-channel. When a client trying to stream the live-channel, the operation will fail. it will only delete the live-channel itself and the object generated by the live-channel will not be deleted.
  188. //
  189. // channelName the name of the channel
  190. //
  191. // error nil if success, otherwise error
  192. //
  193. func (bucket Bucket) DeleteLiveChannel(channelName string) error {
  194. params := map[string]interface{}{}
  195. params["live"] = nil
  196. if channelName == "" {
  197. return fmt.Errorf("invalid argument: channel name is empty")
  198. }
  199. resp, err := bucket.do("DELETE", channelName, params, nil, nil, nil)
  200. if err != nil {
  201. return err
  202. }
  203. defer resp.Body.Close()
  204. return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent})
  205. }
  206. //
  207. // SignRtmpURL Generate a RTMP push-stream signature URL for the trusted user to push the RTMP stream to the live-channel.
  208. //
  209. // channelName the name of the channel
  210. // playlistName the name of the playlist, must end with ".m3u8"
  211. // expires expiration (in seconds)
  212. //
  213. // string singed rtmp push stream url
  214. // error nil if success, otherwise error
  215. //
  216. func (bucket Bucket) SignRtmpURL(channelName, playlistName string, expires int64) (string, error) {
  217. if expires <= 0 {
  218. return "", fmt.Errorf("invalid argument: %d, expires must greater than 0", expires)
  219. }
  220. expiration := time.Now().Unix() + expires
  221. return bucket.Client.Conn.signRtmpURL(bucket.BucketName, channelName, playlistName, expiration), nil
  222. }