mirror of https://github.com/OpenVidu/openvidu.git
openvidu-java-client updated to recording
parent
4a1b4617b7
commit
d7c9a30258
|
@ -0,0 +1,8 @@
|
|||
package io.openvidu.java.client;
|
||||
|
||||
public enum ArchiveLayout {
|
||||
BEST_FIT,
|
||||
PICTURE_IN_PICTURE,
|
||||
VERTICAL_PRESENTATION,
|
||||
HORIZONTAL_PRESENTATION
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package io.openvidu.java.client;
|
||||
|
||||
public enum ArchiveMode {
|
||||
ALWAYS, // The session is archived automatically (as soon as there are clients publishing streams to the session)
|
||||
MANUAL; // The session is not archived automatically. To archive the session, you can call the OpenVidu.StartArchive() method
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
package io.openvidu.java.client;
|
||||
|
||||
public enum MediaMode {
|
||||
RELAYED, // The session will attempt to transmit streams directly between clients
|
||||
ROUTED // The session will transmit streams using OpenVidu Media Server
|
||||
}
|
|
@ -54,4 +54,17 @@ public class OpenVidu {
|
|||
return s;
|
||||
}
|
||||
|
||||
public Session createSession(SessionProperties properties) throws OpenViduException {
|
||||
Session s = new Session(myHttpClient, urlOpenViduServer, properties);
|
||||
return s;
|
||||
}
|
||||
|
||||
public void startArchive(String sessionId) {
|
||||
// TODO: REST POST to start recording in OpenVidu Server
|
||||
}
|
||||
|
||||
public void stopArchive(String sessionId) {
|
||||
// TODO: REST POST to end recording in OpenVidu Server
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package io.openvidu.java.client;
|
||||
|
||||
public enum OpenViduRole {
|
||||
SUBSCRIBER,
|
||||
PUBLISHER,
|
||||
MODERATOR;
|
||||
SUBSCRIBER, // Can subscribe to published streams of other users
|
||||
PUBLISHER, // SUBSCRIBER permissions + can subscribe to published streams of other users and publish their own streams
|
||||
MODERATOR; // SUBSCRIBER + PUBLIHSER permissions + can force unpublish() and disconnect() over a third-party stream or user
|
||||
}
|
||||
|
|
|
@ -6,7 +6,6 @@ import java.io.InputStreamReader;
|
|||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.json.simple.JSONObject;
|
||||
|
@ -16,27 +15,47 @@ import org.json.simple.parser.ParseException;
|
|||
import io.openvidu.java.client.OpenViduException.Code;
|
||||
|
||||
public class Session {
|
||||
|
||||
|
||||
private HttpClient httpClient;
|
||||
private String urlOpenViduServer;
|
||||
private String sessionId;
|
||||
|
||||
private SessionProperties properties;
|
||||
|
||||
protected Session(HttpClient httpClient, String urlOpenViduServer) throws OpenViduException {
|
||||
this.httpClient = httpClient;
|
||||
this.urlOpenViduServer = urlOpenViduServer;
|
||||
this.httpClient = httpClient;
|
||||
this.urlOpenViduServer = urlOpenViduServer;
|
||||
this.properties = new SessionProperties();
|
||||
this.sessionId = this.getSessionId();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected Session(HttpClient httpClient, String urlOpenViduServer, SessionProperties properties) {
|
||||
this.httpClient = httpClient;
|
||||
this.urlOpenViduServer = urlOpenViduServer;
|
||||
this.properties = properties;
|
||||
this.sessionId = this.getSessionId();
|
||||
}
|
||||
|
||||
public String getSessionId() throws OpenViduException {
|
||||
|
||||
|
||||
if (this.hasSessionId()) {
|
||||
return this.sessionId;
|
||||
}
|
||||
|
||||
try {
|
||||
HttpResponse response = httpClient.execute(new HttpPost(this.urlOpenViduServer + "api/sessions"));
|
||||
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("archiveMode", properties.archiveMode().name());
|
||||
json.put("archiveLayout", properties.archiveLayout().name());
|
||||
json.put("mediaMode", properties.mediaMode().name());
|
||||
|
||||
HttpPost request = new HttpPost(this.urlOpenViduServer + "api/sessions");
|
||||
StringEntity params = new StringEntity(json.toString());
|
||||
request.addHeader("content-type", "application/json");
|
||||
request.setEntity(params);
|
||||
|
||||
HttpResponse response = httpClient.execute(request);
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)){
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||
System.out.println("Returning a SESSIONID");
|
||||
String id = "";
|
||||
id = this.httpResponseToString(response);
|
||||
|
@ -46,64 +65,70 @@ public class Session {
|
|||
throw new OpenViduException(Code.SESSIONID_CANNOT_BE_CREATED_ERROR_CODE, Integer.toString(statusCode));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new OpenViduException(Code.SESSIONID_CANNOT_BE_CREATED_ERROR_CODE, "Unable to generate a sessionID: " + e.getMessage());
|
||||
throw new OpenViduException(Code.SESSIONID_CANNOT_BE_CREATED_ERROR_CODE,
|
||||
"Unable to generate a sessionId: " + e.getMessage());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public String generateToken() throws OpenViduException {
|
||||
return this.generateToken(new TokenOptions.Builder().role(OpenViduRole.PUBLISHER).build());
|
||||
}
|
||||
|
||||
|
||||
public String generateToken(TokenOptions tokenOptions) throws OpenViduException {
|
||||
|
||||
if (!this.hasSessionId()){
|
||||
|
||||
if (!this.hasSessionId()) {
|
||||
this.getSessionId();
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
|
||||
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("session", this.sessionId);
|
||||
json.put("role", tokenOptions.getRole().name());
|
||||
json.put("data", tokenOptions.getData());
|
||||
|
||||
HttpPost request = new HttpPost(this.urlOpenViduServer + "api/tokens");
|
||||
|
||||
HttpPost request = new HttpPost(this.urlOpenViduServer + "api/tokens");
|
||||
StringEntity params = new StringEntity(json.toString());
|
||||
request.addHeader("content-type", "application/json");
|
||||
request.setEntity(params);
|
||||
|
||||
request.setEntity(params);
|
||||
|
||||
HttpResponse response = httpClient.execute(request);
|
||||
|
||||
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)){
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||
System.out.println("Returning a TOKEN");
|
||||
return this.httpResponseToString(response);
|
||||
} else {
|
||||
throw new OpenViduException(Code.TOKEN_CANNOT_BE_CREATED_ERROR_CODE, Integer.toString(statusCode));
|
||||
}
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new OpenViduException(Code.TOKEN_CANNOT_BE_CREATED_ERROR_CODE, "Unable to generate a token: " + e.getMessage());
|
||||
throw new OpenViduException(Code.TOKEN_CANNOT_BE_CREATED_ERROR_CODE,
|
||||
"Unable to generate a token: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public SessionProperties getProperties() {
|
||||
return this.properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.sessionId;
|
||||
}
|
||||
|
||||
private String httpResponseToString(HttpResponse response) throws IOException, ParseException{
|
||||
|
||||
private String httpResponseToString(HttpResponse response) throws IOException, ParseException {
|
||||
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
||||
StringBuffer buf = new StringBuffer();
|
||||
String line = "";
|
||||
StringBuffer buf = new StringBuffer();
|
||||
String line = "";
|
||||
while ((line = rd.readLine()) != null) {
|
||||
buf.append(line);
|
||||
buf.append(line);
|
||||
}
|
||||
JSONParser parser = new JSONParser();
|
||||
return ((String) ((JSONObject) parser.parse(buf.toString())).get("id"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private boolean hasSessionId() {
|
||||
return (this.sessionId != null && !this.sessionId.isEmpty());
|
||||
}
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
package io.openvidu.java.client;
|
||||
|
||||
public class SessionProperties {
|
||||
|
||||
private MediaMode mediaMode;
|
||||
private ArchiveMode archiveMode;
|
||||
private ArchiveLayout archiveLayout;
|
||||
|
||||
public static class Builder {
|
||||
|
||||
private MediaMode mediaMode = MediaMode.ROUTED;
|
||||
private ArchiveMode archiveMode = ArchiveMode.MANUAL;
|
||||
private ArchiveLayout archiveLayout = ArchiveLayout.BEST_FIT;
|
||||
|
||||
public SessionProperties build() {
|
||||
return new SessionProperties(this.mediaMode, this.archiveMode, this.archiveLayout);
|
||||
}
|
||||
|
||||
public SessionProperties.Builder mediaMode(MediaMode mediaMode) {
|
||||
this.mediaMode = mediaMode;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SessionProperties.Builder archiveMode(ArchiveMode archiveMode) {
|
||||
this.archiveMode = archiveMode;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SessionProperties.Builder archiveLayout(ArchiveLayout archiveLayout) {
|
||||
this.archiveLayout = archiveLayout;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected SessionProperties() {
|
||||
this.mediaMode = MediaMode.ROUTED;
|
||||
this.archiveMode = ArchiveMode.MANUAL;
|
||||
this.archiveLayout = ArchiveLayout.BEST_FIT;
|
||||
}
|
||||
|
||||
private SessionProperties(MediaMode mediaMode, ArchiveMode archiveMode, ArchiveLayout archiveLayout) {
|
||||
this.mediaMode = mediaMode;
|
||||
this.archiveMode = archiveMode;
|
||||
this.archiveLayout = archiveLayout;
|
||||
}
|
||||
|
||||
public ArchiveMode archiveMode() {
|
||||
return this.archiveMode;
|
||||
}
|
||||
|
||||
public MediaMode mediaMode() {
|
||||
return this.mediaMode;
|
||||
}
|
||||
|
||||
public ArchiveLayout archiveLayout() {
|
||||
return this.archiveLayout;
|
||||
}
|
||||
|
||||
}
|
|
@ -26,7 +26,7 @@ public class TokenOptions {
|
|||
|
||||
}
|
||||
|
||||
public TokenOptions(String data, OpenViduRole role){
|
||||
private TokenOptions(String data, OpenViduRole role){
|
||||
this.data = data;
|
||||
this.role = role;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue