123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- package oss
- import (
- "bytes"
- "encoding/xml"
- "fmt"
- "io"
- "net/http"
- "strconv"
- "time"
- )
- //
- // CreateLiveChannel create a live-channel
- //
- // channelName the name of the channel
- // config configuration of the channel
- //
- // CreateLiveChannelResult the result of create live-channel
- // error nil if success, otherwise error
- //
- func (bucket Bucket) CreateLiveChannel(channelName string, config LiveChannelConfiguration) (CreateLiveChannelResult, error) {
- var out CreateLiveChannelResult
- bs, err := xml.Marshal(config)
- if err != nil {
- return out, err
- }
- buffer := new(bytes.Buffer)
- buffer.Write(bs)
- params := map[string]interface{}{}
- params["live"] = nil
- resp, err := bucket.do("PUT", channelName, params, nil, buffer, nil)
- if err != nil {
- return out, err
- }
- defer resp.Body.Close()
- err = xmlUnmarshal(resp.Body, &out)
- return out, err
- }
- //
- // PutLiveChannelStatus Set the status of the live-channel: enabled/disabled
- //
- // channelName the name of the channel
- // status enabled/disabled
- //
- // error nil if success, otherwise error
- //
- func (bucket Bucket) PutLiveChannelStatus(channelName, status string) error {
- params := map[string]interface{}{}
- params["live"] = nil
- params["status"] = status
- resp, err := bucket.do("PUT", channelName, params, nil, nil, nil)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
- }
- // PostVodPlaylist create an playlist based on the specified playlist name, startTime and endTime
- //
- // channelName the name of the channel
- // playlistName the name of the playlist, must end with ".m3u8"
- // startTime the start time of the playlist
- // endTime the endtime of the playlist
- //
- // error nil if success, otherwise error
- //
- func (bucket Bucket) PostVodPlaylist(channelName, playlistName string, startTime, endTime time.Time) error {
- params := map[string]interface{}{}
- params["vod"] = nil
- params["startTime"] = strconv.FormatInt(startTime.Unix(), 10)
- params["endTime"] = strconv.FormatInt(endTime.Unix(), 10)
- key := fmt.Sprintf("%s/%s", channelName, playlistName)
- resp, err := bucket.do("POST", key, params, nil, nil, nil)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
- }
- // GetVodPlaylist get the playlist based on the specified channelName, startTime and endTime
- //
- // channelName the name of the channel
- // startTime the start time of the playlist
- // endTime the endtime of the playlist
- //
- // io.ReadCloser reader instance for reading data from response. It must be called close() after the usage and only valid when error is nil.
- // error nil if success, otherwise error
- //
- func (bucket Bucket) GetVodPlaylist(channelName string, startTime, endTime time.Time) (io.ReadCloser, error) {
- params := map[string]interface{}{}
- params["vod"] = nil
- params["startTime"] = strconv.FormatInt(startTime.Unix(), 10)
- params["endTime"] = strconv.FormatInt(endTime.Unix(), 10)
- resp, err := bucket.do("GET", channelName, params, nil, nil, nil)
- if err != nil {
- return nil, err
- }
- return resp.Body, nil
- }
- //
- // GetLiveChannelStat Get the state of the live-channel
- //
- // channelName the name of the channel
- //
- // LiveChannelStat the state of the live-channel
- // error nil if success, otherwise error
- //
- func (bucket Bucket) GetLiveChannelStat(channelName string) (LiveChannelStat, error) {
- var out LiveChannelStat
- params := map[string]interface{}{}
- params["live"] = nil
- params["comp"] = "stat"
- resp, err := bucket.do("GET", channelName, params, nil, nil, nil)
- if err != nil {
- return out, err
- }
- defer resp.Body.Close()
- err = xmlUnmarshal(resp.Body, &out)
- return out, err
- }
- //
- // GetLiveChannelInfo Get the configuration info of the live-channel
- //
- // channelName the name of the channel
- //
- // LiveChannelConfiguration the configuration info of the live-channel
- // error nil if success, otherwise error
- //
- func (bucket Bucket) GetLiveChannelInfo(channelName string) (LiveChannelConfiguration, error) {
- var out LiveChannelConfiguration
- params := map[string]interface{}{}
- params["live"] = nil
- resp, err := bucket.do("GET", channelName, params, nil, nil, nil)
- if err != nil {
- return out, err
- }
- defer resp.Body.Close()
- err = xmlUnmarshal(resp.Body, &out)
- return out, err
- }
- //
- // GetLiveChannelHistory Get push records of live-channel
- //
- // channelName the name of the channel
- //
- // LiveChannelHistory push records
- // error nil if success, otherwise error
- //
- func (bucket Bucket) GetLiveChannelHistory(channelName string) (LiveChannelHistory, error) {
- var out LiveChannelHistory
- params := map[string]interface{}{}
- params["live"] = nil
- params["comp"] = "history"
- resp, err := bucket.do("GET", channelName, params, nil, nil, nil)
- if err != nil {
- return out, err
- }
- defer resp.Body.Close()
- err = xmlUnmarshal(resp.Body, &out)
- return out, err
- }
- //
- // ListLiveChannel list the live-channels
- //
- // options Prefix: filter by the name start with the value of "Prefix"
- // MaxKeys: the maximum count returned
- // Marker: cursor from which starting list
- //
- // ListLiveChannelResult live-channel list
- // error nil if success, otherwise error
- //
- func (bucket Bucket) ListLiveChannel(options ...Option) (ListLiveChannelResult, error) {
- var out ListLiveChannelResult
- params, err := GetRawParams(options)
- if err != nil {
- return out, err
- }
- params["live"] = nil
- resp, err := bucket.do("GET", "", params, nil, nil, nil)
- if err != nil {
- return out, err
- }
- defer resp.Body.Close()
- err = xmlUnmarshal(resp.Body, &out)
- return out, err
- }
- //
- // 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.
- //
- // channelName the name of the channel
- //
- // error nil if success, otherwise error
- //
- func (bucket Bucket) DeleteLiveChannel(channelName string) error {
- params := map[string]interface{}{}
- params["live"] = nil
- if channelName == "" {
- return fmt.Errorf("invalid argument: channel name is empty")
- }
- resp, err := bucket.do("DELETE", channelName, params, nil, nil, nil)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent})
- }
- //
- // SignRtmpURL Generate a RTMP push-stream signature URL for the trusted user to push the RTMP stream to the live-channel.
- //
- // channelName the name of the channel
- // playlistName the name of the playlist, must end with ".m3u8"
- // expires expiration (in seconds)
- //
- // string singed rtmp push stream url
- // error nil if success, otherwise error
- //
- func (bucket Bucket) SignRtmpURL(channelName, playlistName string, expires int64) (string, error) {
- if expires <= 0 {
- return "", fmt.Errorf("invalid argument: %d, expires must greater than 0", expires)
- }
- expiration := time.Now().Unix() + expires
- return bucket.Client.Conn.signRtmpURL(bucket.BucketName, channelName, playlistName, expiration), nil
- }
|