Skip to content

Commit 99ef7e6

Browse files
authored
Merge pull request #479 from qiniu/hugo
[qvs]add stopStream and delete check argument
2 parents 3be435d + fc38f91 commit 99ef7e6

File tree

8 files changed

+497
-20
lines changed

8 files changed

+497
-20
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package com.qiniu.qvs;
2+
3+
import com.qiniu.common.QiniuException;
4+
import com.qiniu.qvs.model.Device;
5+
import com.qiniu.qvs.model.PatchOperation;
6+
import com.qiniu.http.Client;
7+
import com.qiniu.http.Response;
8+
import com.qiniu.util.Auth;
9+
import com.qiniu.util.StringMap;
10+
import com.qiniu.util.UrlUtils;
11+
12+
public class DeviceManager {
13+
14+
private final String apiServer;
15+
private final Client client;
16+
private final Auth auth;
17+
18+
public DeviceManager(Auth auth) {
19+
this(auth, "http://qvs.qiniuapi.com");
20+
}
21+
22+
public DeviceManager(Auth auth, String apiServer) {
23+
this(auth, apiServer, new Client());
24+
}
25+
26+
public DeviceManager(Auth auth, String apiServer, Client client) {
27+
this.auth = auth;
28+
this.apiServer = apiServer;
29+
this.client = client;
30+
}
31+
32+
33+
/*
34+
* 创建设备
35+
*/
36+
public Response createDevice(String namespaceId, Device device) throws QiniuException {
37+
StringMap params = new StringMap();
38+
params.put("name", device.getName());
39+
params.put("gbId", device.getGbId());
40+
params.putNotNull("username", device.getUsername());
41+
params.putNotNull("password", device.getPassword());
42+
params.put("pullIfRegister", device.isPullIfRegister());
43+
params.put("desc", device.getDesc());
44+
45+
String url = String.format("%s/v1/namespaces/%s/devices", apiServer, namespaceId);
46+
return QvsResponse.post(url, params, client, auth);
47+
}
48+
49+
/*
50+
* 删除设备
51+
*/
52+
public Response deleteDevice(String namespaceId, String gbId) throws QiniuException {
53+
String url = String.format("%s/v1/namespaces/%s/devices/%s", apiServer, namespaceId, gbId);
54+
return QvsResponse.delete(url, client, auth);
55+
}
56+
57+
/*
58+
* 更新设备
59+
*/
60+
public Response updateDevice(String namespaceId, String gbId, PatchOperation[] patchOperation)
61+
throws QiniuException {
62+
StringMap params = new StringMap().put("operations", patchOperation);
63+
String url = String.format("%s/v1/namespaces/%s/devices/%s", apiServer, namespaceId, gbId);
64+
return QvsResponse.patch(url, params, client, auth);
65+
}
66+
67+
/*
68+
* 查询设备
69+
*/
70+
public Response queryDevice(String namespaceId, String gbId) throws QiniuException {
71+
String url = String.format("%s/v1/namespaces/%s/devices/%s", apiServer, namespaceId, gbId);
72+
return QvsResponse.get(url, client, auth);
73+
}
74+
75+
/*
76+
* 获取设备列表
77+
*/
78+
public Response listDevice(String namespaceId, int offset, int line, String prefix, String state, int qtype)
79+
throws QiniuException {
80+
String requestUrl = String.format("%s/v1/namespaces/%s/devices", apiServer, namespaceId);
81+
StringMap map = new StringMap().put("offset", offset).put("line", line).put("qtype", qtype)
82+
.put("prefix", prefix).put("state", state);
83+
requestUrl = UrlUtils.composeUrlWithQueries(requestUrl, map);
84+
return QvsResponse.get(requestUrl, client, auth);
85+
}
86+
87+
/*
88+
* 获取通道列表
89+
*/
90+
public Response listChannels(String namespaceId, String gbId, String prefix) throws QiniuException {
91+
String requestUrl = String.format("%s/v1/namespaces/%s/devices/%s/channels", apiServer, namespaceId, gbId);
92+
StringMap map = new StringMap().put("prefix", prefix);
93+
requestUrl = UrlUtils.composeUrlWithQueries(requestUrl, map);
94+
return QvsResponse.get(requestUrl, client, auth);
95+
}
96+
97+
/*
98+
* 启动设备拉流
99+
*/
100+
public Response startDevice(String namespaceId, String gbId, String[] channels) throws QiniuException {
101+
String url = String.format("%s/v1/namespaces/%s/devices/%s/start", apiServer, namespaceId, gbId);
102+
StringMap params = new StringMap().put("channels", channels);
103+
return QvsResponse.post(url, params, client, auth);
104+
}
105+
106+
/*
107+
* 停止设备拉流
108+
*/
109+
public Response stopDevice(String namespaceId, String gbId, String[] channels) throws QiniuException {
110+
String url = String.format("%s/v1/namespaces/%s/devices/%s/stop", apiServer, namespaceId, gbId);
111+
StringMap params = new StringMap().put("channels", channels);
112+
return QvsResponse.post(url, params, client, auth);
113+
}
114+
115+
}

src/main/java/com/qiniu/qvs/NameSpaceManager.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public Response createNameSpace(NameSpace nameSpace) throws QiniuException {
4949
params.putNotNull("snapshotTemplateId", nameSpace.getSnapShotTemplateId());
5050
params.putNotNull("recordTemplateApplyAll", nameSpace.isRecordTemplateApplyAll());
5151
params.putNotNull("snapshotTemplateApplyAll", nameSpace.isSnapTemplateApplyAll());
52+
params.putNotNull("urlMode", nameSpace.getUrlMode());
53+
params.putNotNull("zone", nameSpace.getZone());
54+
params.putNotNull("hlsLowLatency", nameSpace.isHlsLowLatency());
5255

5356
String url = String.format("%s/v1/namespaces", apiServer);
5457
return QvsResponse.post(url, params, client, auth);
@@ -96,15 +99,15 @@ public Response listNameSpace(int offset, int line, String sortBy) throws QiniuE
9699
*/
97100
public Response disableNameSpace(String namespaceId) throws QiniuException {
98101
String url = String.format("%s/v1/namespaces/%s/disabled", apiServer, namespaceId);
99-
return QvsResponse.post(url, null, client, auth);
102+
return QvsResponse.post(url, new StringMap(), client, auth);
100103
}
101104

102105
/*
103106
启用空间API
104107
*/
105108
public Response enableNameSpace(String namespaceId) throws QiniuException {
106109
String url = String.format("%s/v1/namespaces/%s/enabled", apiServer, namespaceId);
107-
return QvsResponse.post(url, null, client, auth);
110+
return QvsResponse.post(url, new StringMap(), client, auth);
108111
}
109112

110113

src/main/java/com/qiniu/qvs/README.md

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@
2424
- [x] 启用流: enableStream(String namespaceId, String streamId)
2525
- [x] 查询推流记录: queryStreamPubHistories(String namespaceId, String streamId, int start, int end, int offset, int line)
2626

27+
- 设备管理
28+
- [x] 创建设备: createDevice(String namespaceId, Device device)
29+
- [x] 删除设备: deleteDevice(String namespaceId, String gbId)
30+
- [x] 更新设备: updateDevice(String namespaceId, String gbId, PatchOperation[] patchOperation)
31+
- [x] 查询设备信息: queryDevice(String namespaceId, String gbId)
32+
- [x] 获取设备列表: listDevice(String namespaceId, int offset, int line, String prefix, String state, int qtype)
33+
- [x] 获取通道列表: listChannels(String namespaceId, String gbId, String prefix)
34+
- [x] 启动设备拉流: startDevice(String namespaceId, String gbId, String channels)
35+
- [x] 停止设备拉流: stopDevice(String namespaceId, String gbId, String channels)
36+
2737
- 模板管理
2838
- [x] 创建模板: createTemplate(Template template)
2939
- [x] 删除模板: deleteTemplate(String templateId)
@@ -66,20 +76,31 @@
6676
* [启用流](#启用流)
6777
* [删除流](#删除流)
6878

79+
- [设备管理](#设备管理)
80+
81+
- [创建设备](#创建设备)
82+
- [删除设备](#删除设备)
83+
- [查询设备](#查询设备)
84+
- [更新设备](#更新设备)
85+
- [获取设备列表](#获取设备列表)
86+
- [获取通道列表](#获取通道列表)
87+
- [启动设备拉流](#启动设备拉流)
88+
- [停止设备拉流](#停止设备拉流)
89+
6990
- [模板管理](#模板管理)
70-
91+
7192
* [创建模板](#创建模板)
7293
* [查询模板](#查询模板)
7394
* [更新模板](#更新模板)
7495
* [获取模板列表](#获取模板列表)
7596
* [删除模板](#删除模板)
76-
97+
7798
- [录制管理](#录制管理)
78-
99+
79100
- [查询录制记录](#查询录制记录)
80101
- [查询流封面](#查询流封面)
81102
- [获取截图列表](#获取截图列表)
82-
103+
83104

84105

85106
## Usage
@@ -249,6 +270,79 @@ streamManager.enableStream(namespaceId, stream.getStreamID());
249270
streamManager.deleteStream(namespaceId, stream.getStreamID());
250271
```
251272

273+
### 设备管理
274+
275+
#### 创建设备
276+
277+
```
278+
// 创建设备
279+
DeviceManager deviceManager = new DeviceManager(auth);
280+
Device device = new Device();
281+
device.setUsername("admin");
282+
device.setPassword("QQQNNN111");
283+
284+
String namespaceId = "3nm4x0v0h6vjr";
285+
deviceManager.createDevice(namespaceId, device);
286+
```
287+
288+
#### 删除设备
289+
290+
```
291+
deviceManager.deleteDevice(namespaceId, device.getGbId());
292+
```
293+
294+
#### 查询设备
295+
296+
```
297+
// 查询设备
298+
device.setGbId("31011500991320000056");
299+
300+
deviceManager.queryDevice(namespaceId, device.getGbId());
301+
```
302+
303+
#### 更新设备
304+
305+
```
306+
// 更新设备
307+
PatchOperation[] patchOperation = {new PatchOperation("replace", "name", "GBTEST")};
308+
309+
deviceManager.updateDevice(namespaceId, device.getGbId(), patchOperation);
310+
```
311+
312+
#### 获取设备列表
313+
314+
```
315+
// 获取设备列表
316+
int offset = 0;
317+
int line = 3;
318+
int qtype = 0;
319+
String prefix = "310";
320+
String state = "notReg";
321+
322+
deviceManager.listDevice(namespaceId, offset, line, prefix, state, qtype);
323+
```
324+
325+
#### 获取通道列表
326+
327+
```
328+
// 禁用空间
329+
deviceManager.listChannels(namespaceId, device.getGbId(), prefix);
330+
```
331+
332+
#### 启动设备拉流
333+
334+
```
335+
// 启动设备拉流
336+
deviceManager.startDevice(namespaceId, device.getGbId(), "31011500991320000056");
337+
```
338+
339+
#### 停止设备拉流
340+
341+
```
342+
// 停止设备拉流
343+
deviceManager.stopDevice(namespaceId, device.getGbId(), "31011500991320000056");
344+
```
345+
252346

253347

254348
### 模板管理

src/main/java/com/qiniu/qvs/StreamManager.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,36 +116,30 @@ public Response dynamicPublishPlayURL(String namespaceId, String streamId, Dynam
116116
*/
117117
public Response disableStream(String namespaceId, String streamId) throws QiniuException {
118118
String url = String.format("%s/v1/namespaces/%s/streams/%s/disabled", apiServer, namespaceId, streamId);
119-
return QvsResponse.post(url, null, client, auth);
119+
return QvsResponse.post(url, new StringMap(), client, auth);
120120
}
121121

122122
/*
123123
启用流API
124124
*/
125125
public Response enableStream(String namespaceId, String streamId) throws QiniuException {
126126
String url = String.format("%s/v1/namespaces/%s/streams/%s/enabled", apiServer, namespaceId, streamId);
127-
return QvsResponse.post(url, null, client, auth);
127+
return QvsResponse.post(url, new StringMap(), client, auth);
128128
}
129129

130130
// 查询推流历史记录
131131
public Response queryStreamPubHistories(String namespaceId, String streamId, int start, int end, int offset, int line) throws QiniuException {
132-
if (start <= 0 || end < 0 || (start >= end && end != 0)) {
133-
throw new QiniuException(new IllegalArgumentException("bad argument" + start + "," + end));
134-
}
135132
String requestUrl = String.format("%s/v1/namespaces/%s/streams/%s/pubhistories", apiServer, namespaceId, streamId);
136-
StringMap map = new StringMap().put("start", start).put("end", end).
133+
StringMap map = new StringMap().putNotNull("start", start).putNotNull("end", end).
137134
putNotNull("offset", offset).putNotNull("line", line);
138135
requestUrl = UrlUtils.composeUrlWithQueries(requestUrl, map);
139136
return QvsResponse.get(requestUrl, client, auth);
140137
}
141138

142139
// 查询录制记录
143140
public Response queryStreamRecordHistories(String namespaceId, String streamId, int start, int end, int line, String marker) throws QiniuException {
144-
if (start <= 0 || end < 0 || (start >= end && end != 0)) {
145-
throw new QiniuException(new IllegalArgumentException("bad argument" + start + "," + end));
146-
}
147141
String requestUrl = String.format("%s/v1/namespaces/%s/streams/%s/recordhistories", apiServer, namespaceId, streamId);
148-
StringMap map = new StringMap().put("start", start).put("end", end)
142+
StringMap map = new StringMap().putNotNull("start", start).putNotNull("end", end)
149143
.putNotNull("line", line).putNotEmpty("marker", marker);
150144
requestUrl = UrlUtils.composeUrlWithQueries(requestUrl, map);
151145
return QvsResponse.get(requestUrl, client, auth);
@@ -159,13 +153,16 @@ public Response queryStreamCover(String namespaceId, String streamId) throws Qin
159153

160154
// 获取截图列表
161155
public Response streamsSnapshots(String namespaceId, String streamId, int start, int end, int type, int line, String marker) throws QiniuException {
162-
if (start <= 0 || end < 0 || (start >= end && end != 0)) {
163-
throw new QiniuException(new IllegalArgumentException("bad argument" + start + "," + end));
164-
}
165156
String requestUrl = String.format("%s/v1/namespaces/%s/streams/%s/snapshots", apiServer, namespaceId, streamId);
166-
StringMap map = new StringMap().put("start", start).put("end", end)
157+
StringMap map = new StringMap().putNotNull("start", start).putNotNull("end", end)
167158
.putNotNull("line", line).putNotEmpty("marker", marker).put("type", type);
168159
requestUrl = UrlUtils.composeUrlWithQueries(requestUrl, map);
169160
return QvsResponse.get(requestUrl, client, auth);
170161
}
162+
163+
// 停用流
164+
public Response stopStream(String namespaceId, String streamId) throws QiniuException {
165+
String url = String.format("%s/v1/namespaces/%s/streams/%s/stop", apiServer, namespaceId, streamId);
166+
return QvsResponse.post(url, new StringMap(), client, auth);
167+
}
171168
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.qiniu.qvs.model;
2+
3+
public class Device {
4+
private String name;// 设备名称(可包含 字母、数字、中划线、下划线;1 ~ 100 个字符长)
5+
private String gbId; // 设备国标ID
6+
private String username; // 用户名, 4~40位,可包含大写字母、小写字母、数字、中划线,建议与设备国标ID一致
7+
private String password; // 密码, 4~40位,可包含大写字母、小写字母、数字、中划线
8+
private boolean pullIfRegister; // 注册成功后启动拉流, 默认关闭
9+
private String desc;// 关于设备的描述信息
10+
11+
public String getName() {
12+
return name;
13+
}
14+
15+
public void setName(String name) {
16+
this.name = name;
17+
}
18+
19+
public String getGbId() {
20+
return gbId;
21+
}
22+
23+
public void setGbId(String gbId) {
24+
this.gbId = gbId;
25+
}
26+
27+
public String getUsername() {
28+
return username;
29+
}
30+
31+
public void setUsername(String username) {
32+
this.username = username;
33+
}
34+
35+
public String getPassword() {
36+
return password;
37+
}
38+
39+
public void setPassword(String password) {
40+
this.password = password;
41+
}
42+
43+
public boolean isPullIfRegister() {
44+
return pullIfRegister;
45+
}
46+
47+
public void setPullIfRegister(boolean pullIfRegister) {
48+
this.pullIfRegister = pullIfRegister;
49+
}
50+
51+
public String getDesc() {
52+
return desc;
53+
}
54+
55+
public void setDesc(String desc) {
56+
this.desc = desc;
57+
}
58+
}

0 commit comments

Comments
 (0)