From ac52d0c3cb58621521abd39a278a55919fdbdbd4 Mon Sep 17 00:00:00 2001 From: pabloFuente Date: Sun, 22 Jul 2018 22:27:08 +0200 Subject: [PATCH] openvidu-recording-java: update to openvidu-java-client 2.3.0 --- openvidu-recording-java/.springBeans | 16 ++ openvidu-recording-java/pom.xml | 4 +- .../openvidu/{js => recording}/java/App.java | 2 +- .../java/MyRestController.java | 171 +++++++++++++++++- .../src/main/resources/static/app.js | 131 ++++++++++++-- .../src/main/resources/static/index.html | 32 +++- .../src/main/resources/static/style.css | 38 ++-- 7 files changed, 342 insertions(+), 52 deletions(-) create mode 100644 openvidu-recording-java/.springBeans rename openvidu-recording-java/src/main/java/io/openvidu/{js => recording}/java/App.java (88%) rename openvidu-recording-java/src/main/java/io/openvidu/{js => recording}/java/MyRestController.java (59%) diff --git a/openvidu-recording-java/.springBeans b/openvidu-recording-java/.springBeans new file mode 100644 index 00000000..8b813b9a --- /dev/null +++ b/openvidu-recording-java/.springBeans @@ -0,0 +1,16 @@ + + + 1 + + + + + + + java:io.openvidu.recording.java.App + + + + + + diff --git a/openvidu-recording-java/pom.xml b/openvidu-recording-java/pom.xml index c82f93d6..3b418d2d 100644 --- a/openvidu-recording-java/pom.xml +++ b/openvidu-recording-java/pom.xml @@ -19,7 +19,7 @@ UTF-8 1.8 - io.openvidu.js.java.App + io.openvidu.recording.java.App openvidu @@ -95,7 +95,7 @@ io.openvidu openvidu-java-client - 2.2.0 + 2.3.0 diff --git a/openvidu-recording-java/src/main/java/io/openvidu/js/java/App.java b/openvidu-recording-java/src/main/java/io/openvidu/recording/java/App.java similarity index 88% rename from openvidu-recording-java/src/main/java/io/openvidu/js/java/App.java rename to openvidu-recording-java/src/main/java/io/openvidu/recording/java/App.java index eda169a2..7929909a 100644 --- a/openvidu-recording-java/src/main/java/io/openvidu/js/java/App.java +++ b/openvidu-recording-java/src/main/java/io/openvidu/recording/java/App.java @@ -1,4 +1,4 @@ -package io.openvidu.js.java; +package io.openvidu.recording.java; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; diff --git a/openvidu-recording-java/src/main/java/io/openvidu/js/java/MyRestController.java b/openvidu-recording-java/src/main/java/io/openvidu/recording/java/MyRestController.java similarity index 59% rename from openvidu-recording-java/src/main/java/io/openvidu/js/java/MyRestController.java rename to openvidu-recording-java/src/main/java/io/openvidu/recording/java/MyRestController.java index db61edbd..eaa4689b 100644 --- a/openvidu-recording-java/src/main/java/io/openvidu/js/java/MyRestController.java +++ b/openvidu-recording-java/src/main/java/io/openvidu/recording/java/MyRestController.java @@ -1,4 +1,4 @@ -package io.openvidu.js.java; +package io.openvidu.recording.java; import java.util.Collection; import java.util.List; @@ -18,13 +18,13 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; -import io.openvidu.java.client.Recording; import io.openvidu.java.client.OpenVidu; import io.openvidu.java.client.OpenViduHttpException; import io.openvidu.java.client.OpenViduJavaClientException; +import io.openvidu.java.client.OpenViduRole; +import io.openvidu.java.client.Recording; import io.openvidu.java.client.Session; import io.openvidu.java.client.TokenOptions; -import io.openvidu.java.client.OpenViduRole; @RestController @RequestMapping("/api") @@ -35,7 +35,8 @@ public class MyRestController { // Collection to pair session names and OpenVidu Session objects private Map mapSessions = new ConcurrentHashMap<>(); - // Collection to pair session names and tokens (the inner Map pairs tokens and role associated) + // Collection to pair session names and tokens (the inner Map pairs tokens and + // role associated) private Map> mapSessionNamesTokens = new ConcurrentHashMap<>(); // Collection to pair session names and recording objects private Map sessionRecordings = new ConcurrentHashMap<>(); @@ -101,7 +102,8 @@ public class MyRestController { try { // Create a new OpenVidu Session - Session session = this.openVidu.createSession(); + Session session = this.openVidu.createSession();// new + // SessionProperties.Builder().customSessionId("CUSTOMSESSIONID").defaultRecordingLayout(RecordingLayout.CUSTOM).defaultCustomLayout("CUSTOM/LAYOUT").recordingMode(RecordingMode.ALWAYS).build()); // Generate a new token with the recently created tokenOptions String token = session.generateToken(tokenOptions); @@ -157,6 +159,119 @@ public class MyRestController { } } + @RequestMapping(value = "/close-session", method = RequestMethod.DELETE) + public ResponseEntity closeSession(@RequestBody String sessionName) throws Exception { + + System.out.println("Closing session | {sessionName}=" + sessionName); + + // Retrieve the param from BODY + JSONObject sessionNameJSON = (JSONObject) new JSONParser().parse(sessionName); + String session = (String) sessionNameJSON.get("sessionName"); + + // If the session exists + if (this.mapSessions.get(session) != null && this.mapSessionNamesTokens.get(session) != null) { + Session s = this.mapSessions.get(session); + s.close(); + this.mapSessions.remove(session); + this.mapSessionNamesTokens.remove(session); + this.sessionRecordings.remove(s.getSessionId()); + return new ResponseEntity<>(HttpStatus.OK); + } else { + // The SESSION does not exist + System.out.println("Problems in the app server: the SESSION does not exist"); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + @RequestMapping(value = "/fetch-info", method = RequestMethod.POST) + public ResponseEntity fetchInfo(@RequestBody String sessionName) { + try { + System.out.println("Fetching session info | {sessionName}=" + sessionName); + + // Retrieve the param from BODY + JSONObject sessionNameJSON = (JSONObject) new JSONParser().parse(sessionName); + String session = (String) sessionNameJSON.get("sessionName"); + + // If the session exists + if (this.mapSessions.get(session) != null && this.mapSessionNamesTokens.get(session) != null) { + Session s = this.mapSessions.get(session); + boolean changed = s.fetch(); + System.out.println("Any change: " + changed); + return new ResponseEntity<>(this.sessionToJson(s), HttpStatus.OK); + } else { + // The SESSION does not exist + System.out.println("Problems in the app server: the SESSION does not exist"); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } catch (ParseException | OpenViduJavaClientException | OpenViduHttpException e) { + e.printStackTrace(); + return getErrorResponse(e); + } + } + + @RequestMapping(value = "/fetch-all", method = RequestMethod.GET) + public ResponseEntity fetchAll() { + try { + System.out.println("Fetching all session info"); + boolean changed = this.openVidu.fetch(); + System.out.println("Any change: " + changed); + JSONArray jsonArray = new JSONArray(); + for (Session s : this.openVidu.getActiveSessions()) { + jsonArray.add(this.sessionToJson(s)); + } + return new ResponseEntity<>(jsonArray, HttpStatus.OK); + } catch (OpenViduJavaClientException | OpenViduHttpException e) { + e.printStackTrace(); + return getErrorResponse(e); + } + } + + @RequestMapping(value = "/force-disconnect", method = RequestMethod.DELETE) + public ResponseEntity forceDisconnect(@RequestBody String sessionName) { + try { + // Retrieve the param from BODY + JSONObject sessionNameConnectionIdJSON = (JSONObject) new JSONParser().parse(sessionName); + String session = (String) sessionNameConnectionIdJSON.get("sessionName"); + String connectionId = (String) sessionNameConnectionIdJSON.get("connectionId"); + + // If the session exists + if (this.mapSessions.get(session) != null && this.mapSessionNamesTokens.get(session) != null) { + Session s = this.mapSessions.get(session); + s.forceDisconnect(connectionId); + return new ResponseEntity<>(HttpStatus.OK); + } else { + // The SESSION does not exist + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } catch (ParseException | OpenViduJavaClientException | OpenViduHttpException e) { + e.printStackTrace(); + return getErrorResponse(e); + } + } + + @RequestMapping(value = "/force-unpublish", method = RequestMethod.DELETE) + public ResponseEntity forceUnpublish(@RequestBody String sessionName) { + try { + // Retrieve the param from BODY + JSONObject sessionNameStreamIdJSON = (JSONObject) new JSONParser().parse(sessionName); + String session = (String) sessionNameStreamIdJSON.get("sessionName"); + String streamId = (String) sessionNameStreamIdJSON.get("streamId"); + + // If the session exists + if (this.mapSessions.get(session) != null && this.mapSessionNamesTokens.get(session) != null) { + Session s = this.mapSessions.get(session); + s.forceUnpublish(streamId); + return new ResponseEntity<>(HttpStatus.OK); + } else { + // The SESSION does not exist + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + } catch (ParseException | OpenViduJavaClientException | OpenViduHttpException e) { + e.printStackTrace(); + return getErrorResponse(e); + } + } + /*******************/ /** Recording API **/ /*******************/ @@ -269,5 +384,51 @@ public class MyRestController { } return array; } + + @SuppressWarnings("unchecked") + protected JSONObject sessionToJson(Session session) { + JSONObject json = new JSONObject(); + json.put("sessionId", session.getSessionId()); + json.put("customSessionId", session.getProperties().customSessionId()); + json.put("recording", session.isBeingRecorded()); + json.put("mediaMode", session.getProperties().mediaMode()); + json.put("recordingMode", session.getProperties().recordingMode()); + json.put("defaultRecordingLayout", session.getProperties().defaultRecordingLayout()); + json.put("defaultCustomLayout", session.getProperties().defaultCustomLayout()); + JSONObject connections = new JSONObject(); + connections.put("numberOfElements", session.getActiveConnections().size()); + JSONArray jsonArrayConnections = new JSONArray(); + session.getActiveConnections().forEach(con -> { + JSONObject c = new JSONObject(); + c.put("connectionId", con.getConnectionId()); + c.put("role", con.getRole()); + c.put("token", con.getToken()); + c.put("clientData", con.getClientData()); + c.put("serverData", con.getServerData()); + JSONArray pubs = new JSONArray(); + con.getPublishers().forEach(p -> { + JSONObject jsonP = new JSONObject(); + jsonP.put("streamId", p.getStreamId()); + jsonP.put("hasAudio", p.hasAudio()); + jsonP.put("hasVideo", p.hasVideo()); + jsonP.put("audioActive", p.isAudioActive()); + jsonP.put("videoActive", p.isVideoActive()); + jsonP.put("frameRate", p.getFrameRate()); + jsonP.put("typeOfVideo", p.getTypeOfVideo()); + jsonP.put("videoDimensions", p.getVideoDimensions()); + pubs.add(jsonP); + }); + JSONArray subs = new JSONArray(); + con.getSubscribers().forEach(s -> { + subs.add(s); + }); + c.put("publishers", pubs); + c.put("subscribers", subs); + jsonArrayConnections.add(c); + }); + connections.put("content", jsonArrayConnections); + json.put("connections", connections); + return json; + } } diff --git a/openvidu-recording-java/src/main/resources/static/app.js b/openvidu-recording-java/src/main/resources/static/app.js index 20510773..29b9ee14 100644 --- a/openvidu-recording-java/src/main/resources/static/app.js +++ b/openvidu-recording-java/src/main/resources/static/app.js @@ -41,6 +41,18 @@ function joinSession() { }); }); + session.on('sessionDisconnected', (event) => { + if (event.reason !== 'disconnect') { + removeUser(); + } + if (event.reason !== 'sessionClosedByServer') { + session = null; + numVideos = 0; + $('#join').show(); + $('#session').hide(); + } + }); + // --- 4) Connect to the session passing the retrieved token and some more data from // the client (in this case a JSON with the nickname chosen by the user) --- @@ -97,13 +109,8 @@ function joinSession() { function leaveSession() { // --- 9) Leave the session by calling 'disconnect' method over the Session object --- - session.disconnect(); - session = null; - numVideos = 0; - - $('#join').show(); - $('#session').hide(); + } /* OPENVIDU METHODS */ @@ -118,7 +125,7 @@ function getToken(callback) { httpRequest( 'POST', 'api/get-token', - {sessionName: sessionName}, + { sessionName: sessionName }, 'Request of TOKEN gone WRONG:', (response) => { token = response[0]; // Get token from response @@ -132,14 +139,76 @@ function removeUser() { httpRequest( 'POST', 'api/remove-user', - {sessionName: sessionName, token: token}, - 'User couldn\'t be removed from session', + { sessionName: sessionName, token: token }, + 'User couldn\'t be removed from session', (response) => { console.warn("You have been removed from session " + sessionName); } ); } +function closeSession() { + httpRequest( + 'DELETE', + 'api/close-session', + { sessionName: sessionName }, + 'Session couldn\'t be closed', + (response) => { + console.warn("Session " + sessionName + " has been closed"); + } + ); +} + +function fetchInfo() { + httpRequest( + 'POST', + 'api/fetch-info', + { sessionName: sessionName }, + 'Session couldn\'t be fetched', + (response) => { + console.warn("Session info has been fetched"); + $('#text-area').text(JSON.stringify(response, null, "\t")); + } + ); +} + +function fetchAll() { + httpRequest( + 'GET', + 'api/fetch-all', + {}, + 'All session info couldn\'t be fetched', + (response) => { + console.warn("All session info has been fetched"); + $('#text-area').text(JSON.stringify(response, null, "\t")); + } + ); +} + +function forceDisconnect() { + httpRequest( + 'DELETE', + 'api/force-disconnect', + { sessionName: sessionName, connectionId: document.getElementById('forceValue').value }, + 'Connection couldn\'t be closed', + (response) => { + console.warn("Connection has been closed"); + } + ); +} + +function forceUnpublish() { + httpRequest( + 'DELETE', + 'api/force-unpublish', + { sessionName: sessionName, streamId: document.getElementById('forceValue').value }, + 'Stream couldn\'t be closed', + (response) => { + console.warn("Stream has been closed"); + } + ); +} + function httpRequest(method, url, body, errorMsg, callback) { $('#text-area').text(''); var http = new XMLHttpRequest(); @@ -154,7 +223,7 @@ function httpRequest(method, url, body, errorMsg, callback) { try { callback(JSON.parse(http.responseText)); } catch (e) { - callback(); + callback(e); } } else { console.warn(errorMsg + ' (' + http.status + ')'); @@ -165,28 +234,28 @@ function httpRequest(method, url, body, errorMsg, callback) { } } -var recordingId; - function startRecording() { httpRequest( 'POST', 'api/recording/start', - {session: session.sessionId}, - 'Start recording WRONG', + { session: session.sessionId }, + 'Start recording WRONG', (response) => { console.log(response); - recordingId = response.id; + document.getElementById('forceRecordingId').value = response.id; + checkBtnsRecordings(); $('#text-area').text(JSON.stringify(response, null, "\t")); } ); } function stopRecording() { + var forceRecordingId = document.getElementById('forceRecordingId').value; httpRequest( 'POST', 'api/recording/stop', - {recording: recordingId}, - 'Stop recording WRONG', + { recording: forceRecordingId }, + 'Stop recording WRONG', (response) => { console.log(response); $('#text-area').text(JSON.stringify(response, null, "\t")); @@ -195,10 +264,11 @@ function stopRecording() { } function deleteRecording() { + var forceRecordingId = document.getElementById('forceRecordingId').value; httpRequest( 'DELETE', 'api/recording/delete', - {recording: recordingId}, + { recording: forceRecordingId }, 'Delete recording WRONG', () => { console.log("DELETE ok"); @@ -208,9 +278,10 @@ function deleteRecording() { } function getRecording() { + var forceRecordingId = document.getElementById('forceRecordingId').value; httpRequest( 'GET', - 'api/recording/get/' + recordingId, + 'api/recording/get/' + forceRecordingId, {}, 'Get recording WRONG', (response) => { @@ -265,4 +336,26 @@ function updateNumVideos(i) { } } +function checkBtnsForce() { + if (document.getElementById("forceValue").value === "") { + document.getElementById('buttonForceUnpublish').disabled = true; + document.getElementById('buttonForceDisconnect').disabled = true; + } else { + document.getElementById('buttonForceUnpublish').disabled = false; + document.getElementById('buttonForceDisconnect').disabled = false; + } +} + +function checkBtnsRecordings() { + if (document.getElementById("forceRecordingId").value === "") { + document.getElementById('buttonGetRecording').disabled = true; + document.getElementById('buttonStopRecording').disabled = true; + document.getElementById('buttonDeleteRecording').disabled = true; + } else { + document.getElementById('buttonGetRecording').disabled = false; + document.getElementById('buttonStopRecording').disabled = false; + document.getElementById('buttonDeleteRecording').disabled = false; + } +} + /* APPLICATION BROWSER METHODS */ \ No newline at end of file diff --git a/openvidu-recording-java/src/main/resources/static/index.html b/openvidu-recording-java/src/main/resources/static/index.html index 341b15fd..621ec261 100644 --- a/openvidu-recording-java/src/main/resources/static/index.html +++ b/openvidu-recording-java/src/main/resources/static/index.html @@ -7,9 +7,12 @@ - - - + + + @@ -33,7 +36,7 @@ Recording Java + title="GitHub Repository" target="_blank"> @@ -66,16 +69,25 @@