openvidu-java-client: code structure closer to openvidu-node-client

pull/87/merge
pabloFuente 2018-07-22 22:20:21 +02:00
parent 42a4feb1d9
commit d2f7422b8f
8 changed files with 153 additions and 127 deletions

View File

@ -63,9 +63,9 @@ public class OpenVidu {
private static final Logger log = LoggerFactory.getLogger(OpenVidu.class); private static final Logger log = LoggerFactory.getLogger(OpenVidu.class);
private String urlOpenViduServer;
private String secret; private String secret;
private HttpClient myHttpClient; protected static String urlOpenViduServer;
protected static HttpClient httpClient;
protected static Map<String, Session> activeSessions = new ConcurrentHashMap<>(); protected static Map<String, Session> activeSessions = new ConcurrentHashMap<>();
protected final static String API_SESSIONS = "api/sessions"; protected final static String API_SESSIONS = "api/sessions";
@ -83,10 +83,10 @@ public class OpenVidu {
*/ */
public OpenVidu(String urlOpenViduServer, String secret) { public OpenVidu(String urlOpenViduServer, String secret) {
this.urlOpenViduServer = urlOpenViduServer; OpenVidu.urlOpenViduServer = urlOpenViduServer;
if (!this.urlOpenViduServer.endsWith("/")) { if (!OpenVidu.urlOpenViduServer.endsWith("/")) {
this.urlOpenViduServer += "/"; OpenVidu.urlOpenViduServer += "/";
} }
this.secret = secret; this.secret = secret;
@ -114,7 +114,7 @@ public class OpenVidu {
requestBuilder = requestBuilder.setConnectTimeout(30000); requestBuilder = requestBuilder.setConnectTimeout(30000);
requestBuilder = requestBuilder.setConnectionRequestTimeout(30000); requestBuilder = requestBuilder.setConnectionRequestTimeout(30000);
this.myHttpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestBuilder.build()) OpenVidu.httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestBuilder.build())
.setConnectionTimeToLive(30, TimeUnit.SECONDS).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) .setConnectionTimeToLive(30, TimeUnit.SECONDS).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.setSSLContext(sslContext).setDefaultCredentialsProvider(provider).build(); .setSSLContext(sslContext).setDefaultCredentialsProvider(provider).build();
} }
@ -128,7 +128,7 @@ public class OpenVidu {
* @throws OpenViduHttpException * @throws OpenViduHttpException
*/ */
public Session createSession() throws OpenViduJavaClientException, OpenViduHttpException { public Session createSession() throws OpenViduJavaClientException, OpenViduHttpException {
Session s = new Session(myHttpClient, urlOpenViduServer); Session s = new Session();
OpenVidu.activeSessions.put(s.getSessionId(), s); OpenVidu.activeSessions.put(s.getSessionId(), s);
return s; return s;
} }
@ -153,7 +153,7 @@ public class OpenVidu {
*/ */
public Session createSession(SessionProperties properties) public Session createSession(SessionProperties properties)
throws OpenViduJavaClientException, OpenViduHttpException { throws OpenViduJavaClientException, OpenViduHttpException {
Session s = new Session(myHttpClient, urlOpenViduServer, properties); Session s = new Session(properties);
OpenVidu.activeSessions.put(s.getSessionId(), s); OpenVidu.activeSessions.put(s.getSessionId(), s);
return s; return s;
} }
@ -189,7 +189,7 @@ public class OpenVidu {
public Recording startRecording(String sessionId, RecordingProperties properties) public Recording startRecording(String sessionId, RecordingProperties properties)
throws OpenViduJavaClientException, OpenViduHttpException { throws OpenViduJavaClientException, OpenViduHttpException {
HttpPost request = new HttpPost(this.urlOpenViduServer + API_RECORDINGS + API_RECORDINGS_START); HttpPost request = new HttpPost(OpenVidu.urlOpenViduServer + API_RECORDINGS + API_RECORDINGS_START);
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("session", sessionId); json.put("session", sessionId);
@ -208,7 +208,7 @@ public class OpenVidu {
HttpResponse response; HttpResponse response;
try { try {
response = myHttpClient.execute(request); response = OpenVidu.httpClient.execute(request);
} catch (IOException e2) { } catch (IOException e2) {
throw new OpenViduJavaClientException(e2.getMessage(), e2.getCause()); throw new OpenViduJavaClientException(e2.getMessage(), e2.getCause());
} }
@ -216,7 +216,9 @@ public class OpenVidu {
try { try {
int statusCode = response.getStatusLine().getStatusCode(); int statusCode = response.getStatusLine().getStatusCode();
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) { if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
return new Recording(httpResponseToJson(response)); Recording r = new Recording(httpResponseToJson(response));
OpenVidu.activeSessions.get(r.getSessionId()).setIsBeingRecorded(true);
return r;
} else { } else {
throw new OpenViduHttpException(statusCode); throw new OpenViduHttpException(statusCode);
} }
@ -233,7 +235,9 @@ public class OpenVidu {
* @param name * @param name
* The name you want to give to the video file. You can access this * The name you want to give to the video file. You can access this
* same value in your clients on recording events (recordingStarted, * same value in your clients on recording events (recordingStarted,
* recordingStopped) * recordingStopped). <strong>WARNING: this parameter follows an
* overwriting policy.</strong> If you name two recordings the same,
* the newest MP4 file will overwrite the oldest one
* *
* @return The started recording. If this method successfully returns the * @return The started recording. If this method successfully returns the
* Recording object it means that the recording can be stopped with * Recording object it means that the recording can be stopped with
@ -316,10 +320,10 @@ public class OpenVidu {
*/ */
public Recording stopRecording(String recordingId) throws OpenViduJavaClientException, OpenViduHttpException { public Recording stopRecording(String recordingId) throws OpenViduJavaClientException, OpenViduHttpException {
HttpPost request = new HttpPost( HttpPost request = new HttpPost(
this.urlOpenViduServer + API_RECORDINGS + API_RECORDINGS_STOP + "/" + recordingId); OpenVidu.urlOpenViduServer + API_RECORDINGS + API_RECORDINGS_STOP + "/" + recordingId);
HttpResponse response; HttpResponse response;
try { try {
response = myHttpClient.execute(request); response = OpenVidu.httpClient.execute(request);
} catch (IOException e) { } catch (IOException e) {
throw new OpenViduJavaClientException(e.getMessage(), e.getCause()); throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
} }
@ -327,7 +331,9 @@ public class OpenVidu {
try { try {
int statusCode = response.getStatusLine().getStatusCode(); int statusCode = response.getStatusLine().getStatusCode();
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) { if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
return new Recording(httpResponseToJson(response)); Recording r = new Recording(httpResponseToJson(response));
OpenVidu.activeSessions.get(r.getSessionId()).setIsBeingRecorded(false);
return r;
} else { } else {
throw new OpenViduHttpException(statusCode); throw new OpenViduHttpException(statusCode);
} }
@ -352,10 +358,10 @@ public class OpenVidu {
* </ul> * </ul>
*/ */
public Recording getRecording(String recordingId) throws OpenViduJavaClientException, OpenViduHttpException { public Recording getRecording(String recordingId) throws OpenViduJavaClientException, OpenViduHttpException {
HttpGet request = new HttpGet(this.urlOpenViduServer + API_RECORDINGS + "/" + recordingId); HttpGet request = new HttpGet(OpenVidu.urlOpenViduServer + API_RECORDINGS + "/" + recordingId);
HttpResponse response; HttpResponse response;
try { try {
response = myHttpClient.execute(request); response = OpenVidu.httpClient.execute(request);
} catch (IOException e) { } catch (IOException e) {
throw new OpenViduJavaClientException(e.getMessage(), e.getCause()); throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
} }
@ -382,10 +388,10 @@ public class OpenVidu {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public List<Recording> listRecordings() throws OpenViduJavaClientException, OpenViduHttpException { public List<Recording> listRecordings() throws OpenViduJavaClientException, OpenViduHttpException {
HttpGet request = new HttpGet(this.urlOpenViduServer + API_RECORDINGS); HttpGet request = new HttpGet(OpenVidu.urlOpenViduServer + API_RECORDINGS);
HttpResponse response; HttpResponse response;
try { try {
response = myHttpClient.execute(request); response = OpenVidu.httpClient.execute(request);
} catch (IOException e) { } catch (IOException e) {
throw new OpenViduJavaClientException(e.getMessage(), e.getCause()); throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
} }
@ -414,6 +420,7 @@ public class OpenVidu {
* {@link io.openvidu.java.client.Recording.Status#available} * {@link io.openvidu.java.client.Recording.Status#available}
* *
* @param recordingId * @param recordingId
* The id property of the recording you want to delete
* *
* @throws OpenViduJavaClientException * @throws OpenViduJavaClientException
* @throws OpenViduHttpException * @throws OpenViduHttpException
@ -427,10 +434,10 @@ public class OpenVidu {
* </ul> * </ul>
*/ */
public void deleteRecording(String recordingId) throws OpenViduJavaClientException, OpenViduHttpException { public void deleteRecording(String recordingId) throws OpenViduJavaClientException, OpenViduHttpException {
HttpDelete request = new HttpDelete(this.urlOpenViduServer + API_RECORDINGS + "/" + recordingId); HttpDelete request = new HttpDelete(OpenVidu.urlOpenViduServer + API_RECORDINGS + "/" + recordingId);
HttpResponse response; HttpResponse response;
try { try {
response = myHttpClient.execute(request); response = OpenVidu.httpClient.execute(request);
} catch (IOException e) { } catch (IOException e) {
throw new OpenViduJavaClientException(e.getMessage(), e.getCause()); throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
} }
@ -461,6 +468,10 @@ public class OpenVidu {
* <li>Calling {@link io.openvidu.java.client.Session#forceUnpublish(Publisher)} * <li>Calling {@link io.openvidu.java.client.Session#forceUnpublish(Publisher)}
* also automatically updates the inner affected connections for that specific * also automatically updates the inner affected connections for that specific
* Session</li> * Session</li>
* <li>Calling {@link io.openvidu.java.client.OpenVidu#startRecording(String)}
* and {@link io.openvidu.java.client.OpenVidu#stopRecording(String)}
* automatically updates the recording status of the Session
* ({@link io.openvidu.java.client.Session#isBeingRecorded()})</li>
* </ul> * </ul>
* <br> * <br>
* To get the list of active sessions with their current actual value, you must * To get the list of active sessions with their current actual value, you must
@ -473,20 +484,24 @@ public class OpenVidu {
/** /**
* Updates every property of every active Session with the current status they * Updates every property of every active Session with the current status they
* have in OpenVidu Server. After calling this method you can acces the updated * have in OpenVidu Server. After calling this method you can access the updated
* list of active sessions by calling * list of active sessions by calling
* {@link io.openvidu.java.client.OpenVidu#getActiveSessions()} * {@link io.openvidu.java.client.OpenVidu#getActiveSessions()}
* *
* @return true if any Session status has changed with respect to the server,
* false if not. This applies to any property or sub-property of any of
* the sessions locally stored in OpenVidu Java Client
*
* @throws OpenViduHttpException * @throws OpenViduHttpException
* @throws OpenViduJavaClientException * @throws OpenViduJavaClientException
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public boolean fetch() throws OpenViduJavaClientException, OpenViduHttpException { public boolean fetch() throws OpenViduJavaClientException, OpenViduHttpException {
HttpGet request = new HttpGet(this.urlOpenViduServer + API_SESSIONS); HttpGet request = new HttpGet(OpenVidu.urlOpenViduServer + API_SESSIONS);
HttpResponse response; HttpResponse response;
try { try {
response = myHttpClient.execute(request); response = OpenVidu.httpClient.execute(request);
} catch (IOException e) { } catch (IOException e) {
throw new OpenViduJavaClientException(e.getMessage(), e.getCause()); throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
} }
@ -500,7 +515,7 @@ public class OpenVidu {
// Set to store fetched sessionIds and later remove closed sessions // Set to store fetched sessionIds and later remove closed sessions
Set<String> fetchedSessionIds = new HashSet<>(); Set<String> fetchedSessionIds = new HashSet<>();
// Boolean to store if any Session has changed // Boolean to store if any Session has changed
boolean hasChanged = false; final boolean[] hasChanged = { false };
jsonArraySessions.forEach(session -> { jsonArraySessions.forEach(session -> {
String sessionId = (String) ((JSONObject) session).get("sessionId"); String sessionId = (String) ((JSONObject) session).get("sessionId");
fetchedSessionIds.add(sessionId); fetchedSessionIds.add(sessionId);
@ -508,12 +523,14 @@ public class OpenVidu {
String beforeJSON = s.toJson(); String beforeJSON = s.toJson();
s = s.resetSessionWithJson((JSONObject) session); s = s.resetSessionWithJson((JSONObject) session);
String afterJSON = s.toJson(); String afterJSON = s.toJson();
log.info("Available session '{}' info fetched. Any change: {}", sessionId, boolean changed = !beforeJSON.equals(afterJSON);
!beforeJSON.equals(afterJSON)); hasChanged[0] = hasChanged[0] || changed;
log.info("Available session '{}' info fetched. Any change: {}", sessionId, changed);
return s; return s;
}); });
OpenVidu.activeSessions.computeIfAbsent(sessionId, sId -> { OpenVidu.activeSessions.computeIfAbsent(sessionId, sId -> {
log.info("New session '{}' fetched", sessionId); log.info("New session '{}' fetched", sessionId);
hasChanged[0] = true;
return new Session((JSONObject) session); return new Session((JSONObject) session);
}); });
}); });
@ -524,11 +541,12 @@ public class OpenVidu {
return true; return true;
} else { } else {
log.info("Removing closed session {}" + entry.getKey()); log.info("Removing closed session {}" + entry.getKey());
hasChanged[0] = true;
return false; return false;
} }
}).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())); }).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
log.info("Active sessions info fetched: {}", OpenVidu.activeSessions.keySet()); log.info("Active sessions info fetched: {}", OpenVidu.activeSessions.keySet());
return hasChanged; return hasChanged[0];
} else { } else {
throw new OpenViduHttpException(statusCode); throw new OpenViduHttpException(statusCode);
} }

View File

@ -25,10 +25,13 @@ public class OpenViduHttpException extends Exception {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private int status; private int status;
public OpenViduHttpException(int status) { protected OpenViduHttpException(int status) {
super(Integer.toString(status)); super(Integer.toString(status));
} }
/**
* @return The unexpected status of the HTTP request
*/
public int getStatus() { public int getStatus() {
return this.status; return this.status;
} }

View File

@ -24,11 +24,11 @@ public class OpenViduJavaClientException extends Exception {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public OpenViduJavaClientException(String message) { protected OpenViduJavaClientException(String message) {
super(message); super(message);
} }
public OpenViduJavaClientException(String message, Throwable cause) { protected OpenViduJavaClientException(String message, Throwable cause) {
super(message, cause); super(message, cause);
} }

View File

@ -20,89 +20,104 @@ package io.openvidu.java.client;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
/** /**
* See {@link io.openvidu.java.client.Connection#getPublishers()} * See {@link io.openvidu.java.client.Connection#getPublishers()}.
*
* <br>
* This is a backend representation of a published media stream (see
* <a href="/api/openvidu-browser/classes/stream.html" target="_blank"> OpenVidu
* Browser Stream class</a>).
*/ */
public class Publisher { public class Publisher {
class MediaOptions {
protected MediaOptions(boolean hasAudio, boolean hasVideo, Boolean audioActive, Boolean videoActive,
Integer frameRate, String typeOfVideo, String videoDimensions) {
this.hasAudio = hasAudio;
this.hasVideo = hasVideo;
this.audioActive = audioActive;
this.videoActive = videoActive;
this.frameRate = frameRate;
this.typeOfVideo = typeOfVideo;
this.videoDimensions = videoDimensions;
}
boolean hasVideo;
boolean hasAudio;
Boolean audioActive;
Boolean videoActive;
Integer frameRate;
String typeOfVideo;
String videoDimensions;
}
private String streamId; private String streamId;
private MediaOptions mediaOptions; private boolean hasVideo;
private boolean hasAudio;
private Boolean audioActive;
private Boolean videoActive;
private Integer frameRate;
private String typeOfVideo;
private String videoDimensions;
public Publisher(String streamId, boolean hasAudio, boolean hasVideo, Object audioActive, Object videoActive, protected Publisher(String streamId, boolean hasAudio, boolean hasVideo, Object audioActive, Object videoActive,
Object frameRate, Object typeOfVideo, Object videoDimensions) { Object frameRate, Object typeOfVideo, Object videoDimensions) {
this.streamId = streamId; this.streamId = streamId;
Boolean audioActiveAux = null; this.hasAudio = hasAudio;
Boolean videoActiveAux = null; this.hasVideo = hasVideo;
Integer frameRateAux = null; this.audioActive = (Boolean) audioActive;
String typeOfVideoAux = null; this.videoActive = (Boolean) videoActive;
String videoDimensionsAux = null; if (frameRate != null) {
if (hasAudio) { this.frameRate = ((Long) frameRate).intValue();
audioActiveAux = (boolean) audioActive;
} }
if (hasVideo) { this.typeOfVideo = (String) typeOfVideo;
videoActiveAux = (boolean) videoActive; this.videoDimensions = (String) videoDimensions;
if (frameRate != null) {
frameRateAux = ((Long) frameRate).intValue();
}
typeOfVideoAux = (String) typeOfVideo;
videoDimensionsAux = (String) videoDimensions;
}
this.mediaOptions = new MediaOptions(hasAudio, hasVideo, audioActiveAux, videoActiveAux, frameRateAux,
typeOfVideoAux, videoDimensionsAux);
} }
/**
* Returns the unique identifier of the
* <a href="/api/openvidu-browser/classes/stream.html" target=
* "_blank">Stream</a> associated to this Publisher. Each Publisher is paired
* with only one Stream, so you can identify each Publisher by its
* <a href="/api/openvidu-browser/classes/stream.html#streamid" target=
* "_blank"><code>Stream.streamId</code></a>
*/
public String getStreamId() { public String getStreamId() {
return streamId; return streamId;
} }
/**
* See properties of <a href="/api/openvidu-browser/classes/stream.html" target=
* "_blank">Stream</a> object in OpenVidu Browser library to find out more
*/
public boolean hasVideo() { public boolean hasVideo() {
return this.mediaOptions.hasVideo; return this.hasVideo;
} }
/**
* See properties of <a href="/api/openvidu-browser/classes/stream.html" target=
* "_blank">Stream</a> object in OpenVidu Browser library to find out more
*/
public boolean hasAudio() { public boolean hasAudio() {
return this.mediaOptions.hasAudio; return this.hasAudio;
} }
/**
* See properties of <a href="/api/openvidu-browser/classes/stream.html" target=
* "_blank">Stream</a> object in OpenVidu Browser library to find out more
*/
public Boolean isAudioActive() { public Boolean isAudioActive() {
return this.mediaOptions.audioActive; return this.audioActive;
} }
/**
* See properties of <a href="/api/openvidu-browser/classes/stream.html" target=
* "_blank">Stream</a> object in OpenVidu Browser library to find out more
*/
public Boolean isVideoActive() { public Boolean isVideoActive() {
return this.mediaOptions.videoActive; return this.videoActive;
} }
/**
* See properties of <a href="/api/openvidu-browser/classes/stream.html" target=
* "_blank">Stream</a> object in OpenVidu Browser library to find out more
*/
public Integer getFrameRate() { public Integer getFrameRate() {
return this.mediaOptions.frameRate; return this.frameRate;
} }
/**
* See properties of <a href="/api/openvidu-browser/classes/stream.html" target=
* "_blank">Stream</a> object in OpenVidu Browser library to find out more
*/
public String getTypeOfVideo() { public String getTypeOfVideo() {
return this.mediaOptions.typeOfVideo; return this.typeOfVideo;
} }
/**
* See properties of <a href="/api/openvidu-browser/classes/stream.html" target=
* "_blank">Stream</a> object in OpenVidu Browser library to find out more
*/
public String getVideoDimensions() { public String getVideoDimensions() {
return this.mediaOptions.videoDimensions; return this.videoDimensions;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")

View File

@ -71,7 +71,7 @@ public class Recording {
private boolean hasVideo = true; private boolean hasVideo = true;
private RecordingProperties recordingProperties; private RecordingProperties recordingProperties;
public Recording(JSONObject json) { protected Recording(JSONObject json) {
this.id = (String) json.get("id"); this.id = (String) json.get("id");
this.sessionId = (String) json.get("sessionId"); this.sessionId = (String) json.get("sessionId");
this.createdAt = (long) json.get("createdAt"); this.createdAt = (long) json.get("createdAt");

View File

@ -46,7 +46,9 @@ public class RecordingProperties {
/** /**
* Call this method to set the name of the video file. You can access this same * Call this method to set the name of the video file. You can access this same
* value in your clients on recording events (<code>recordingStarted</code>, * value in your clients on recording events (<code>recordingStarted</code>,
* <code>recordingStopped</code>) * <code>recordingStopped</code>). <strong>WARNING: this parameter follows an
* overwriting policy.</strong> If you name two recordings the same, the newest
* MP4 file will overwrite the oldest one
*/ */
public RecordingProperties.Builder name(String name) { public RecordingProperties.Builder name(String name) {
this.name = name; this.name = name;
@ -86,7 +88,9 @@ public class RecordingProperties {
/** /**
* Defines the name you want to give to the video file. You can access this same * Defines the name you want to give to the video file. You can access this same
* value in your clients on recording events (<code>recordingStarted</code>, * value in your clients on recording events (<code>recordingStarted</code>,
* <code>recordingStopped</code>) * <code>recordingStopped</code>). <strong>WARNING: this parameter follows an
* overwriting policy.</strong> If you name two recordings the same, the newest
* MP4 file will overwrite the oldest one
*/ */
public String name() { public String name() {
return this.name; return this.name;

View File

@ -27,7 +27,6 @@ import java.util.stream.Collectors;
import org.apache.http.HttpHeaders; import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
@ -44,25 +43,17 @@ public class Session {
private static final Logger log = LoggerFactory.getLogger(Session.class); private static final Logger log = LoggerFactory.getLogger(Session.class);
private HttpClient httpClient;
private String urlOpenViduServer;
private String sessionId; private String sessionId;
private SessionProperties properties; private SessionProperties properties;
private Map<String, Connection> activeConnections = new ConcurrentHashMap<>(); private Map<String, Connection> activeConnections = new ConcurrentHashMap<>();
private boolean recording = false; private boolean recording = false;
protected Session(HttpClient httpClient, String urlOpenViduServer) protected Session() throws OpenViduJavaClientException, OpenViduHttpException {
throws OpenViduJavaClientException, OpenViduHttpException {
this.httpClient = httpClient;
this.urlOpenViduServer = urlOpenViduServer;
this.properties = new SessionProperties.Builder().build(); this.properties = new SessionProperties.Builder().build();
this.getSessionIdHttp(); this.getSessionIdHttp();
} }
protected Session(HttpClient httpClient, String urlOpenViduServer, SessionProperties properties) protected Session(SessionProperties properties) throws OpenViduJavaClientException, OpenViduHttpException {
throws OpenViduJavaClientException, OpenViduHttpException {
this.httpClient = httpClient;
this.urlOpenViduServer = urlOpenViduServer;
this.properties = properties; this.properties = properties;
this.getSessionIdHttp(); this.getSessionIdHttp();
} }
@ -111,7 +102,7 @@ public class Session {
this.getSessionId(); this.getSessionId();
} }
HttpPost request = new HttpPost(this.urlOpenViduServer + OpenVidu.API_TOKENS); HttpPost request = new HttpPost(OpenVidu.urlOpenViduServer + OpenVidu.API_TOKENS);
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("session", this.sessionId); json.put("session", this.sessionId);
@ -129,7 +120,7 @@ public class Session {
HttpResponse response; HttpResponse response;
try { try {
response = httpClient.execute(request); response = OpenVidu.httpClient.execute(request);
} catch (IOException e2) { } catch (IOException e2) {
throw new OpenViduJavaClientException(e2.getMessage(), e2.getCause()); throw new OpenViduJavaClientException(e2.getMessage(), e2.getCause());
} }
@ -156,12 +147,12 @@ public class Session {
* @throws OpenViduHttpException * @throws OpenViduHttpException
*/ */
public void close() throws OpenViduJavaClientException, OpenViduHttpException { public void close() throws OpenViduJavaClientException, OpenViduHttpException {
HttpDelete request = new HttpDelete(this.urlOpenViduServer + OpenVidu.API_SESSIONS + "/" + this.sessionId); HttpDelete request = new HttpDelete(OpenVidu.urlOpenViduServer + OpenVidu.API_SESSIONS + "/" + this.sessionId);
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded"); request.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
HttpResponse response; HttpResponse response;
try { try {
response = httpClient.execute(request); response = OpenVidu.httpClient.execute(request);
} catch (IOException e) { } catch (IOException e) {
throw new OpenViduJavaClientException(e.getMessage(), e.getCause()); throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
} }
@ -185,8 +176,8 @@ public class Session {
* connections to the Session * connections to the Session
* ({@link io.openvidu.java.client.Session#getActiveConnections()}) and use * ({@link io.openvidu.java.client.Session#getActiveConnections()}) and use
* those values to call * those values to call
* {@link io.openvidu.java.client.Session#forceDisconnect(String)} or * {@link io.openvidu.java.client.Session#forceDisconnect(Connection)} or
* {@link io.openvidu.java.client.Session#forceUnpublish(String)} * {@link io.openvidu.java.client.Session#forceUnpublish(Publisher)}
* *
* @return true if the Session status has changed with respect to the server, * @return true if the Session status has changed with respect to the server,
* false if not. This applies to any property or sub-property of the * false if not. This applies to any property or sub-property of the
@ -197,12 +188,12 @@ public class Session {
*/ */
public boolean fetch() throws OpenViduJavaClientException, OpenViduHttpException { public boolean fetch() throws OpenViduJavaClientException, OpenViduHttpException {
String beforeJSON = this.toJson(); String beforeJSON = this.toJson();
HttpGet request = new HttpGet(this.urlOpenViduServer + OpenVidu.API_SESSIONS + "/" + this.sessionId); HttpGet request = new HttpGet(OpenVidu.urlOpenViduServer + OpenVidu.API_SESSIONS + "/" + this.sessionId);
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded"); request.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
HttpResponse response; HttpResponse response;
try { try {
response = httpClient.execute(request); response = OpenVidu.httpClient.execute(request);
} catch (IOException e) { } catch (IOException e) {
throw new OpenViduJavaClientException(e.getMessage(), e.getCause()); throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
} }
@ -257,13 +248,13 @@ public class Session {
* @throws OpenViduHttpException * @throws OpenViduHttpException
*/ */
public void forceDisconnect(String connectionId) throws OpenViduJavaClientException, OpenViduHttpException { public void forceDisconnect(String connectionId) throws OpenViduJavaClientException, OpenViduHttpException {
HttpDelete request = new HttpDelete( HttpDelete request = new HttpDelete(OpenVidu.urlOpenViduServer + OpenVidu.API_SESSIONS + "/" + this.sessionId
this.urlOpenViduServer + OpenVidu.API_SESSIONS + "/" + this.sessionId + "/connection/" + connectionId); + "/connection/" + connectionId);
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded"); request.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
HttpResponse response = null; HttpResponse response = null;
try { try {
response = httpClient.execute(request); response = OpenVidu.httpClient.execute(request);
} catch (IOException e) { } catch (IOException e) {
throw new OpenViduJavaClientException(e.getMessage(), e.getCause()); throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
} }
@ -277,7 +268,11 @@ public class Session {
// other connections // other connections
if (connectionClosed != null) { if (connectionClosed != null) {
for (Publisher publisher : connectionClosed.getPublishers()) { for (Publisher publisher : connectionClosed.getPublishers()) {
this.removeSubscribersForPublisher(publisher); String streamId = publisher.getStreamId();
for (Connection connection : this.activeConnections.values()) {
connection.setSubscribers(connection.getSubscribers().stream()
.filter(subscriber -> !streamId.equals(subscriber)).collect(Collectors.toList()));
}
} }
} else { } else {
log.warn( log.warn(
@ -332,12 +327,12 @@ public class Session {
*/ */
public void forceUnpublish(String streamId) throws OpenViduJavaClientException, OpenViduHttpException { public void forceUnpublish(String streamId) throws OpenViduJavaClientException, OpenViduHttpException {
HttpDelete request = new HttpDelete( HttpDelete request = new HttpDelete(
this.urlOpenViduServer + OpenVidu.API_SESSIONS + "/" + this.sessionId + "/stream/" + streamId); OpenVidu.urlOpenViduServer + OpenVidu.API_SESSIONS + "/" + this.sessionId + "/stream/" + streamId);
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded"); request.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
HttpResponse response; HttpResponse response;
try { try {
response = httpClient.execute(request); response = OpenVidu.httpClient.execute(request);
} catch (IOException e) { } catch (IOException e) {
throw new OpenViduJavaClientException(e.getMessage(), e.getCause()); throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
} }
@ -383,14 +378,7 @@ public class Session {
} }
/** /**
* Returns whether the session is being recorded or not. <strong>This value will * Returns whether the session is being recorded or not
* be the same as the one that returned method
* {@link io.openvidu.java.client.Session#fetch()} the last time it was
* called.</strong>
*
* To get the current actual value, you must call first
* {@link io.openvidu.java.client.Session#fetch()} and then
* {@link io.openvidu.java.client.Session#isBeingRecorded()}
*/ */
public boolean isBeingRecorded() { public boolean isBeingRecorded() {
return this.recording; return this.recording;
@ -418,7 +406,7 @@ public class Session {
return; return;
} }
HttpPost request = new HttpPost(this.urlOpenViduServer + OpenVidu.API_SESSIONS); HttpPost request = new HttpPost(OpenVidu.urlOpenViduServer + OpenVidu.API_SESSIONS);
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("mediaMode", properties.mediaMode().name()); json.put("mediaMode", properties.mediaMode().name());
@ -438,7 +426,7 @@ public class Session {
HttpResponse response; HttpResponse response;
try { try {
response = httpClient.execute(request); response = OpenVidu.httpClient.execute(request);
} catch (IOException e2) { } catch (IOException e2) {
throw new OpenViduJavaClientException(e2.getMessage(), e2.getCause()); throw new OpenViduJavaClientException(e2.getMessage(), e2.getCause());
} }
@ -470,12 +458,8 @@ public class Session {
return json; return json;
} }
private void removeSubscribersForPublisher(Publisher publisher) { protected void setIsBeingRecorded(boolean recording) {
String streamId = publisher.getStreamId(); this.recording = recording;
for (Connection connection : this.activeConnections.values()) {
connection.setSubscribers(connection.getSubscribers().stream()
.filter(subscriber -> !streamId.equals(subscriber)).collect(Collectors.toList()));
}
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")

View File

@ -383,7 +383,9 @@ public class KurentoSession implements Session {
JSONObject connections = new JSONObject(); JSONObject connections = new JSONObject();
JSONArray participants = new JSONArray(); JSONArray participants = new JSONArray();
this.participants.values().forEach(p -> { this.participants.values().forEach(p -> {
participants.add(toJsonFunction.apply(p)); if (!ProtocolElements.RECORDER_PARTICIPANT_PUBLICID.equals(p.getParticipantPublicId())) {
participants.add(toJsonFunction.apply(p));
}
}); });
connections.put("numberOfElements", participants.size()); connections.put("numberOfElements", participants.size());
connections.put("content", participants); connections.put("content", participants);