OSDN Git Service

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