Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions examples/RTCExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

import java.util.Date;

public class RTCExample {
public static void main(String[] args) {

//init
String accessKey = "DWQOcIddmtTCrnPp1ogwgdasdaAHBdIK1mIFrnmtnXb-66-";
String secretKey = "cJFhYsuaaq7Vo35e1XDFUasdasG8Rm8C2VjkpmXO0aGkJGM";
Client cli = new Client(accessKey, secretKey);
Meeting meeting = cli.newMeeting();

//create room
try {
String res = meeting.createRoom("123");
System.out.println(res);
} catch (PiliException e) {
e.printStackTrace();
}

try {
String res = meeting.createRoom("1234", "test");
System.out.println(res);
} catch (PiliException e) {
e.printStackTrace();
}

try {
String res = meeting.createRoom("12345", "testtest", 5);
System.out.println(res);
} catch (PiliException e) {
e.printStackTrace();
}

//get room info
try {
Meeting.Room room = meeting.getRoom("testtest");
System.out.println(room.name);
System.out.println(room.status);
System.out.println(room.ownerId);
System.out.println(room.userMax);

} catch (PiliException e) {
e.printStackTrace();
}

//delete room
try {
meeting.deleteRoom("testtest");
} catch (PiliException e) {
e.printStackTrace();
}

//get roomToken
try {
String token = meeting.roomToken("test", "123456", "admin", new Date(System.currentTimeMillis() + 3600));
System.out.println(token);
} catch (Exception e) {
e.printStackTrace();
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/qiniu/pili/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public String SnapshotPlayURL(String domain, String hub, String streamKey) {
public Hub newHub(String hub) {
return new Hub(this.cli, hub);
}

public Meeting newMeeting() {
return new Meeting(this.cli);
}
}


Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/qiniu/pili/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ public final class Config {
public static String VERSION = "2.1.0";
static final String APIUserAgent = String.format("pili-sdk-java/%s %s %s/%s", VERSION, System.getProperty("java.version"), System.getProperty("os.name"), System.getProperty("os.arch"));
public static String APIHost = "pili.qiniuapi.com";
public static String RTCAPIHost = "rtc.qiniuapi.com";
}
8 changes: 8 additions & 0 deletions src/main/java/com/qiniu/pili/Mac.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,12 @@ private boolean incBody(byte[] body, String contentType) {
return typeOK && lengthOK;
}

public String signRoomToken(String roomAccess) throws Exception {
String encodedRoomAcc = UrlSafeBase64.encodeToString(roomAccess);
byte[] sign = HMac.HmacSHA1Encrypt(encodedRoomAcc, this.secretKey);
String encodedSign = UrlSafeBase64.encodeToString(sign);
return this.accessKey + ":" + encodedSign+":"+encodedRoomAcc;
}


}
147 changes: 147 additions & 0 deletions src/main/java/com/qiniu/pili/Meeting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package com.qiniu.pili;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;

import java.util.Date;

public class Meeting {
private String baseUrl;
private Gson gson;
private RPC cli;

Meeting(RPC cli ) {
this.baseUrl = Config.APIHTTPScheme + Config.RTCAPIHost + "/v1";
this.cli = cli;
this.gson = new Gson();
}

public String createRoom(String ownerId, String roomName, int userMax) throws PiliException {
CreateArgs args = new CreateArgs(ownerId, roomName, userMax);
return createRoom(args);
}

public String createRoom(String ownerId ,String roomName) throws PiliException {
CreateArgs args = new CreateArgs(ownerId, roomName);
return createRoom(args);
}

public String createRoom(String ownerId) throws PiliException {
CreateArgs args = new CreateArgs(ownerId);
return createRoom(args);
}

private String createRoom(CreateArgs args) throws PiliException {
String path = this.baseUrl + "/rooms";
String json = gson.toJson(args);

try {
String resp = cli.callWithJson(path, json);
RoomName ret = gson.fromJson(resp, RoomName.class);
return ret.roomName;
}catch (PiliException e){
throw e;
}catch (Exception e) {
e.printStackTrace();
throw new PiliException(e);
}
}

public Room getRoom(String roomName) throws PiliException {
String path = this.baseUrl+ "/rooms/" + roomName;
try {
String resp = cli.callWithGet(path);
Room room = gson.fromJson(resp, Room.class);
return room;
}catch (PiliException e){
throw e;
}catch (Exception e) {
e.printStackTrace();
throw new PiliException(e);
}
}

public void deleteRoom(String room) throws PiliException {
String path = this.baseUrl + "/rooms/"+room;
try {
cli.callWithDelete(path);
}catch (PiliException e){
throw e;
}catch (Exception e) {
e.printStackTrace();
throw new PiliException(e);
}
}

public String roomToken(String roomName, String userId, String perm, Date expireAt) throws Exception {
RoomAccess access = new RoomAccess(roomName, userId, perm, expireAt);
String json = gson.toJson(access);
return this.cli.getMac().signRoomToken(json);
}

private class RoomAccess{
@SerializedName("room_name")
String roomName;
@SerializedName("user_id")
String userId;
@SerializedName("perm")
String perm;
@SerializedName("expire_at")
long expireAt;

RoomAccess(String roomName, String userId, String perm, Date expireAt){
this.roomName = roomName;
this.userId = userId;
this.perm = perm;
this.expireAt = expireAt.getTime() / 1000;// seconds
}
}

/**
*
*/
public enum Status {
@SerializedName("0")
NEW,
@SerializedName("1")
MEETING,
@SerializedName("2")
FINISHED,
}

public class Room {
@SerializedName("room_name") public String name;
@SerializedName("room_status") public Status status;
@SerializedName("owner_id") public String ownerId;
@SerializedName("user_max") public int userMax;
}

/**
*
*/
private class RoomName {
@SerializedName("room_name") String roomName;
}

private class CreateArgs {
@SerializedName("owner_id") String ownerId;
@SerializedName("room_name") String room;
@SerializedName("user_max") int user_max;

public CreateArgs(String ownerId, String room, int user_max) {
this.ownerId = ownerId;
this.room = room;
this.user_max = user_max;
}

public CreateArgs(String ownerId, String room) {
this.ownerId = ownerId;
this.room = room;
}

public CreateArgs(String ownerId){
this.ownerId = ownerId;
}
}

}
18 changes: 18 additions & 0 deletions src/main/java/com/qiniu/pili/RPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,22 @@ public String callWithGet(String urlStr) throws Exception {
throw new PiliException(response);
}
}

public String callWithDelete(String urlStr) throws Exception{
URL url = new URL(urlStr);
String macToken = mac.signRequest(url, "DELETE", null,null);
Request request = new Request.Builder()
.url(url)
.delete()
.header("User-Agent", Config.APIUserAgent)
.addHeader("Authorization", "Qiniu " + macToken)
.build();

Response response = okHttpClient.newCall(request).execute();
if (response.isSuccessful()) {
return response.body().string();
} else {
throw new PiliException(response);
}
}
}
133 changes: 133 additions & 0 deletions src/test/java/com/qiniu/pili/MeetingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package com.qiniu.pili;

import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

import java.util.Date;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class MeetingTest {
String accessKey;
String secretKey;
Client cli;
Meeting meeting;



@Before
public void prepare() {
// local test environment
// Config.APIHost = "10.200.20.28:7778";

accessKey = "7O7hf7Ld1RrC_fpZdFvU8aCgOPuhw2K4eapYOdII";
secretKey = "6Rq7rMSUHHqOgo0DJjh15tHsGUBEH9QhWqqyj4ka";

cli = new Client(accessKey, secretKey);
meeting = cli.newMeeting();
}

private boolean skip() {
return Config.RTCAPIHost != "rtc.qiniuapi.com";
}

@Test
public void testRoom() {
Assume.assumeTrue(skip());

String roomName = String.valueOf(new Date().getTime());
//get un-existed room
try {
meeting.getRoom(roomName);
fail("should not exist");
} catch (PiliException e) {
assertTrue(e.isNotFound());
}

// create room with name
try {
String r1 = meeting.createRoom("123",roomName);
assertEquals(roomName,r1);

Meeting.Room room = meeting.getRoom(roomName);
assertEquals(roomName, room.name);
assertEquals("123",room.ownerId);
assertEquals(Meeting.Status.NEW,room.status);

} catch (PiliException e){
fail();
}

// recreate room with the same name
try {
String r1 = meeting.createRoom("123",roomName);
fail();
}catch (PiliException e){
assertEquals(611,e.code());
}

// create room without name
try {
String r2 = meeting.createRoom("123");

Meeting.Room room = meeting.getRoom(r2);
assertEquals(r2, room.name);
assertEquals("123",room.ownerId);
assertEquals(Meeting.Status.NEW,room.status);
}catch (PiliException e){
fail();
}

//delete room
try{
meeting.deleteRoom(roomName);
}catch (PiliException e){
e.printStackTrace();
fail();
}
try {
meeting.getRoom(roomName);
fail("should not exist");
} catch (PiliException e) {
assertTrue(e.isNotFound());
}

// delete un-existed room
try {
meeting.getRoom(roomName);
fail("should not exist");
} catch (PiliException e) {
assertTrue(e.isNotFound());
}

}

@Test
public void testRoomToken(){
// String roomName = "room1";
// create room with name
// try {
// String r1 = meeting.createRoom("123",roomName);
// assertEquals(roomName,r1);
//
// Meeting.Room room = meeting.getRoom(roomName);
// assertEquals(roomName, room.name);
// assertEquals("123",room.ownerId);
// assertEquals(Meeting.Status.NEW,room.status);
//
// } catch (PiliException e){
// fail();
// }

try {
String token = meeting.roomToken("room1", "123", "admin", new Date(1785600000000L));
assertEquals("7O7hf7Ld1RrC_fpZdFvU8aCgOPuhw2K4eapYOdII:jltpX6P42j2fH3ErOp7Zj7RyaeE=:eyJyb29tX25hbWUiOiJyb29tMSIsInVzZXJfaWQiOiIxMjMiLCJwZXJtIjoiYWRtaW4iLCJleHBpcmVfYXQiOjE3ODU2MDAwMDB9",
token);
} catch (Exception e) {
fail();
}
}
}