livechannel.go 6.2 KB

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