Skip to content

Commit fc030d1

Browse files
committed
add genStaticDomainUrl interface and update DeviceManagerTest
1 parent adaa9a6 commit fc030d1

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public Response listChannels(String namespaceId, String gbId, String prefix) thr
9797
/*
9898
* 启动设备拉流
9999
*/
100-
public Response startDevice(String namespaceId, String gbId, String channels) throws QiniuException {
100+
public Response startDevice(String namespaceId, String gbId, String[] channels) throws QiniuException {
101101
String url = String.format("%s/v1/namespaces/%s/devices/%s/start", apiServer, namespaceId, gbId);
102102
StringMap params = new StringMap().put("channels", channels);
103103
return QvsResponse.post(url, params, client, auth);
@@ -106,7 +106,7 @@ public Response startDevice(String namespaceId, String gbId, String channels) th
106106
/*
107107
* 停止设备拉流
108108
*/
109-
public Response stopDevice(String namespaceId, String gbId, String channels) throws QiniuException {
109+
public Response stopDevice(String namespaceId, String gbId, String[] channels) throws QiniuException {
110110
String url = String.format("%s/v1/namespaces/%s/devices/%s/stop", apiServer, namespaceId, gbId);
111111
StringMap params = new StringMap().put("channels", channels);
112112
return QvsResponse.post(url, params, client, auth);

src/main/java/com/qiniu/qvs/model/StaticLiveRoute.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.qiniu.qvs.model;
22

3+
import com.qiniu.common.Constants;
4+
import com.qiniu.util.Md5;
5+
import com.qiniu.util.UrlSafeBase64;
6+
37
public class StaticLiveRoute {
48

59
private String domain; // 域名
@@ -40,4 +44,35 @@ public int getUrlExpireSec() {
4044
public void setUrlExpireSec(int urlExpireSec) {
4145
this.urlExpireSec = urlExpireSec;
4246
}
47+
48+
public String genStaticHLSFLVDomain(String nsId, String streamId, String key, boolean useHttps) {
49+
String path = "/" + nsId + "/" + streamId;
50+
String scheme = useHttps ? "https" : "http";
51+
String host = "";
52+
if ("liveHls".equals(domainType)) {
53+
host = domain + ":1370";
54+
path += ".m3u8";
55+
} else {
56+
host = domain + ":1360";
57+
path += ".flv";
58+
}
59+
long expireTime = System.currentTimeMillis() + urlExpireSec * 1000;
60+
String token = signToken(key, path, expireTime);
61+
return String.format("%s://%s%s?e=%d&token=%s", scheme, host, path, expireTime, token);
62+
}
63+
64+
public String genStaticRtmpDomain(String nsId, String streamId, String key) {
65+
String path = "/" + nsId + "/" + streamId;
66+
String scheme = "rtmp";
67+
String host = domain + ":2045";
68+
long expireTime = System.currentTimeMillis() + urlExpireSec * 1000;
69+
String token = signToken(key, path, expireTime);
70+
return String.format("%s://%s%s?e=%d&token=%s", scheme, host, path, expireTime, token);
71+
}
72+
73+
private String signToken(String key, String path, long expireTime) {
74+
String encode_path = UrlSafeBase64.encodeToString(path);
75+
String tempS = key + encode_path + Long.toHexString(expireTime);
76+
return Md5.md5(tempS.getBytes(Constants.UTF_8));
77+
}
4378
}

src/test/java/test/com/qiniu/qvs/DeviceManagerTest.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.qiniu.qvs.model.Device;
77
import com.qiniu.qvs.model.PatchOperation;
88
import com.qiniu.util.Auth;
9+
import org.junit.Assert;
910
import org.junit.Before;
1011
import org.junit.Test;
1112
import test.com.qiniu.TestConfig;
@@ -14,8 +15,9 @@ public class DeviceManagerTest {
1415
Auth auth = TestConfig.testAuth;
1516
private DeviceManager deviceManager;
1617
private Response res = null;
17-
private String namespaceId = "3nm4x0v0h6vjr";
18-
private String gbId = "31011500991320000056";
18+
private String namespaceId = "2xenzw3lpzpdz";
19+
private String gbId = "31011500991320000126";
20+
private String[] channels = {"31011500991320000126"};
1921

2022

2123
@Before
@@ -30,8 +32,10 @@ public void testCreateDevice() {
3032
device.setPassword("QQQNNN111");
3133
try {
3234
res = deviceManager.createDevice(namespaceId, device);
35+
Assert.assertNotNull(res);
3336
System.out.println(res.bodyString());
3437
} catch (QiniuException e) {
38+
Assert.assertEquals("401", res.statusCode);
3539
e.printStackTrace();
3640
} finally {
3741
if (res != null) {
@@ -44,8 +48,10 @@ public void testCreateDevice() {
4448
public void testQueryDevice() {
4549
try {
4650
res = deviceManager.queryDevice(namespaceId, gbId);
51+
Assert.assertNotNull(res);
4752
System.out.println(res.bodyString());
4853
} catch (QiniuException e) {
54+
Assert.assertEquals("401", res.statusCode);
4955
e.printStackTrace();
5056
} finally {
5157
if (res != null) {
@@ -59,8 +65,10 @@ public void testUpdateDevice() {
5965
PatchOperation[] patchOperation = {new PatchOperation("replace", "name", "GBTEST")};
6066
try {
6167
res = deviceManager.updateDevice(namespaceId, gbId, patchOperation);
68+
Assert.assertNotNull(res);
6269
System.out.println(res.bodyString());
6370
} catch (QiniuException e) {
71+
Assert.assertEquals("401", res.statusCode);
6472
e.printStackTrace();
6573
} finally {
6674
if (res != null) {
@@ -78,8 +86,10 @@ public void testListDevice() {
7886
String state = "notReg";
7987
try {
8088
res = deviceManager.listDevice(namespaceId, offset, line, prefix, state, qtype);
89+
Assert.assertNotNull(res);
8190
System.out.println(res.bodyString());
8291
} catch (QiniuException e) {
92+
Assert.assertEquals("401", res.statusCode);
8393
e.printStackTrace();
8494
} finally {
8595
if (res != null) {
@@ -93,6 +103,7 @@ public void testListChannels() {
93103
String prefix = "310";
94104
try {
95105
res = deviceManager.listChannels(namespaceId, gbId, prefix);
106+
Assert.assertNotNull(res);
96107
System.out.println(res.bodyString());
97108
} catch (QiniuException e) {
98109
e.printStackTrace();
@@ -106,7 +117,8 @@ public void testListChannels() {
106117
@Test
107118
public void testStartDevice() {
108119
try {
109-
res = deviceManager.startDevice(namespaceId, gbId, "31011500991320000056");
120+
res = deviceManager.startDevice(namespaceId, gbId, channels);
121+
Assert.assertNotNull(res);
110122
System.out.println(res.bodyString());
111123
} catch (QiniuException e) {
112124
e.printStackTrace();
@@ -120,7 +132,8 @@ public void testStartDevice() {
120132
@Test
121133
public void testStopDevice() {
122134
try {
123-
res = deviceManager.stopDevice(namespaceId, gbId, "31011500991320000056");
135+
res = deviceManager.stopDevice(namespaceId, gbId, channels);
136+
Assert.assertNotNull(res);
124137
System.out.println(res.bodyString());
125138
} catch (QiniuException e) {
126139
e.printStackTrace();
@@ -135,8 +148,10 @@ public void testStopDevice() {
135148
public void testDeleteDevice() {
136149
try {
137150
res = deviceManager.deleteDevice(namespaceId, gbId);
151+
Assert.assertNotNull(res);
138152
System.out.println(res.bodyString());
139153
} catch (QiniuException e) {
154+
Assert.assertEquals("401", res.statusCode);
140155
e.printStackTrace();
141156
} finally {
142157
if (res != null) {

0 commit comments

Comments
 (0)