github.com/qiniu/go-sdk/v7@v7.27.0/linking/vod.go (about)

     1  package linking
     2  
     3  import (
     4  	"context"
     5  	"encoding/base64"
     6  	"encoding/json"
     7  	"fmt"
     8  	"net/url"
     9  )
    10  
    11  type Segment struct {
    12  	From  int    `json:"from"`
    13  	To    int    `json:"to"`
    14  	Frame string `json:"frame"`
    15  }
    16  
    17  // 视频片段查询
    18  func (manager *Manager) Segments(appid, device string, start, end int, marker string, limit int) ([]Segment, string, error) {
    19  	ret := struct {
    20  		Items  []Segment `json:"items"`
    21  		Marker string    `json:"marker"`
    22  	}{}
    23  
    24  	query := url.Values{}
    25  	if limit > 0 {
    26  		setQuery(query, "limit", limit)
    27  	}
    28  	if marker != "" {
    29  		setQuery(query, "marker", marker)
    30  	}
    31  	if start > 0 {
    32  		setQuery(query, "start", start)
    33  	}
    34  	if end > 0 {
    35  		setQuery(query, "end", end)
    36  	}
    37  
    38  	device = base64.URLEncoding.EncodeToString([]byte(device))
    39  	err := manager.client.Call(context.Background(), &ret, "GET", manager.url("/apps/%s/devices/%s/vod/segments?%v", appid, device, query.Encode()), nil)
    40  	if err != nil {
    41  		return nil, "", err
    42  	}
    43  	return ret.Items, ret.Marker, nil
    44  }
    45  
    46  // -----------------------------------------------------------------------------
    47  // mqtt rpc
    48  type RpcRequest struct {
    49  	Action   int             `json:"action"`
    50  	Params   json.RawMessage `json:"params,omitempty"`
    51  	Timeout  int             `json:"timeout,omitempty"`
    52  	Response bool            `json:"response,omitempty"`
    53  }
    54  type DevResponse struct {
    55  	ErrorCode int             `json:"errorCode,omitempty"`
    56  	Error     string          `json:"error,omitempty"`
    57  	Value     json.RawMessage `json:"value,omitempty"`
    58  }
    59  type RpcResponse struct {
    60  	Id   string      `json:"id"`
    61  	Resp DevResponse `json:"response"`
    62  }
    63  
    64  func (manager *Manager) RPC(appid, device string, req *RpcRequest) (*RpcResponse, error) {
    65  	var ret RpcResponse
    66  	device = base64.URLEncoding.EncodeToString([]byte(device))
    67  	err := manager.client.CallWithJson(context.Background(), &ret, "POST", manager.url("/apps/%s/devices/%s/rpc", appid, device), nil, req)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  	return &ret, nil
    72  }
    73  
    74  type SaveasReply struct {
    75  	Fname       string `json:"fname"`
    76  	PersistenId string `json:"persistentId,omitempty"`
    77  	Duration    int    `json:"duration"` // ms
    78  }
    79  
    80  // 指定的视频片段进行收藏,保存在云存储上
    81  func (manager *Manager) Saveas(appid, device string, start, end int, fname, format string) (*SaveasReply, error) {
    82  	device = base64.URLEncoding.EncodeToString([]byte(device))
    83  
    84  	req := M{
    85  		"start": start,
    86  		"end":   end,
    87  	}
    88  	if fname != "" {
    89  		req["fname"] = fname
    90  	}
    91  	if format != "" {
    92  		req["format"] = format
    93  	}
    94  
    95  	var reply SaveasReply
    96  
    97  	err := manager.client.CallWithJson(context.Background(), &reply, "POST", manager.url("/apps/%s/devices/%s/vod/saveas", appid, device), nil, req)
    98  	return &reply, err
    99  }
   100  
   101  type LiveRequest struct {
   102  	Appid      string `json:"appid"`
   103  	DeviceName string `json:"deviceName"`
   104  	PublishIP  string `json:"publishIP"`
   105  	PlayIP     string `json:"playIP"`
   106  }
   107  
   108  type PlayUrls struct {
   109  	Rtmp string `json:"rtmp"`
   110  	Hls  string `json:"hls"`
   111  	Flv  string `json:"flv"`
   112  }
   113  
   114  type LiveResponse struct {
   115  	PublishUrl string   `json:"publishUrl"`
   116  	PlayUrls   PlayUrls `json:"playUrls"`
   117  }
   118  
   119  // 指定的视频片段进行收藏,保存在云存储上
   120  func (manager *Manager) StartLive(req *LiveRequest) (*LiveResponse, error) {
   121  	var reply LiveResponse
   122  
   123  	err := manager.client.CallWithJson(context.Background(), &reply, "POST", manager.url("/startlive"), nil, req)
   124  	return &reply, err
   125  }
   126  
   127  // 查询活跃设备信息
   128  type StatReq struct {
   129  	Start  int    `form:"start"`
   130  	End    int    `form:"end"`
   131  	Group  string `form:"g"`
   132  	Select string `form:"select"`
   133  }
   134  
   135  func (manager *Manager) Stat(req *StatReq) ([]M, error) {
   136  	var ret []M
   137  	query := url.Values{}
   138  	setQuery(query, "start", req.Start)
   139  	setQuery(query, "end", req.End)
   140  	setQuery(query, "g", req.Group)
   141  	setQuery(query, "select", req.Select)
   142  
   143  	err := manager.client.Call(context.Background(), &ret, "GET", fmt.Sprintf("http://linking.qiniuapi.com/statd/device?%v", query.Encode()), nil)
   144  	return ret, err
   145  }