openvidu-server: POST /api/signal

pull/375/head
pabloFuente 2019-09-16 10:56:39 +02:00
parent 2ed986053c
commit 620816da08
5 changed files with 118 additions and 24 deletions

View File

@ -46,7 +46,6 @@ public class OpenViduException extends JsonRpcErrorException {
USER_METADATA_FORMAT_INVALID_ERROR_CODE(500),
SIGNAL_FORMAT_INVALID_ERROR_CODE(600), SIGNAL_TO_INVALID_ERROR_CODE(601),
SIGNAL_MESSAGE_INVALID_ERROR_CODE(602),
DOCKER_NOT_FOUND(709), RECORDING_PATH_NOT_VALID(708), RECORDING_FILE_EMPTY_ERROR(707),
RECORDING_DELETE_ERROR_CODE(706), RECORDING_LIST_ERROR_CODE(705), RECORDING_STOP_ERROR_CODE(704),

View File

@ -312,15 +312,27 @@ public class SessionEventsHandler {
public void onSendMessage(Participant participant, JsonObject message, Set<Participant> participants,
Integer transactionId, OpenViduException error) {
boolean isRpcCall = transactionId != null;
if (isRpcCall) {
if (error != null) {
rpcNotificationService.sendErrorResponse(participant.getParticipantPrivateId(), transactionId, null, error);
rpcNotificationService.sendErrorResponse(participant.getParticipantPrivateId(), transactionId, null,
error);
return;
}
}
JsonObject params = new JsonObject();
if (message.has("data")) {
params.addProperty(ProtocolElements.PARTICIPANTSENDMESSAGE_DATA_PARAM, message.get("data").getAsString());
params.addProperty(ProtocolElements.PARTICIPANTSENDMESSAGE_FROM_PARAM, participant.getParticipantPublicId());
}
if (message.has("type")) {
params.addProperty(ProtocolElements.PARTICIPANTSENDMESSAGE_TYPE_PARAM, message.get("type").getAsString());
}
if (participant != null) {
params.addProperty(ProtocolElements.PARTICIPANTSENDMESSAGE_FROM_PARAM,
participant.getParticipantPublicId());
}
Set<String> toSet = new HashSet<String>();
@ -357,8 +369,10 @@ public class SessionEventsHandler {
}
}
if (isRpcCall) {
rpcNotificationService.sendResponse(participant.getParticipantPrivateId(), transactionId, new JsonObject());
}
}
public void onStreamPropertyChanged(Participant participant, Integer transactionId, Set<Participant> participants,
String streamId, String property, JsonElement newValue, String reason) {

View File

@ -36,6 +36,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import io.openvidu.client.OpenViduException;
import io.openvidu.client.OpenViduException.Code;
@ -94,7 +96,26 @@ public abstract class SessionManager {
public abstract void unsubscribe(Participant participant, String senderName, Integer transactionId);
public abstract void sendMessage(Participant participant, String message, Integer transactionId);
public void sendMessage(String message, String sessionId) {
try {
JsonObject messageJson = new JsonParser().parse(message).getAsJsonObject();
sessionEventsHandler.onSendMessage(null, messageJson, getParticipants(sessionId), null, null);
} catch (JsonSyntaxException | IllegalStateException e) {
throw new OpenViduException(Code.SIGNAL_FORMAT_INVALID_ERROR_CODE,
"Provided signal object '" + message + "' has not a valid JSON format");
}
}
public void sendMessage(Participant participant, String message, Integer transactionId) {
try {
JsonObject messageJson = new JsonParser().parse(message).getAsJsonObject();
sessionEventsHandler.onSendMessage(participant, messageJson, getParticipants(participant.getSessionId()),
transactionId, null);
} catch (JsonSyntaxException | IllegalStateException e) {
throw new OpenViduException(Code.SIGNAL_FORMAT_INVALID_ERROR_CODE,
"Provided signal object '" + message + "' has not a valid JSON format");
}
}
public abstract void streamPropertyChanged(Participant participant, Integer transactionId, String streamId,
String property, JsonElement newValue, String changeReason);

View File

@ -435,19 +435,6 @@ public class KurentoSessionManager extends SessionManager {
sessionEventsHandler.onUnsubscribe(participant, transactionId, null);
}
@Override
public void sendMessage(Participant participant, String message, Integer transactionId) {
try {
JsonObject messageJson = new JsonParser().parse(message).getAsJsonObject();
KurentoParticipant kParticipant = (KurentoParticipant) participant;
sessionEventsHandler.onSendMessage(participant, messageJson,
getParticipants(kParticipant.getSession().getSessionId()), transactionId, null);
} catch (JsonSyntaxException | IllegalStateException e) {
throw new OpenViduException(Code.SIGNAL_FORMAT_INVALID_ERROR_CODE,
"Provided signal object '" + message + "' has not a valid JSON format");
}
}
@Override
public void streamPropertyChanged(Participant participant, Integer transactionId, String streamId, String property,
JsonElement newValue, String reason) {

View File

@ -17,6 +17,7 @@
package io.openvidu.server.rest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@ -37,6 +38,8 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
@ -460,7 +463,7 @@ public class SessionRestController {
// Session is not in ROUTED MediMode or it is already being recorded
return new ResponseEntity<>(HttpStatus.CONFLICT);
} else {
// Session is not active
// Session is not active (no connected participants)
return new ResponseEntity<>(HttpStatus.NOT_ACCEPTABLE);
}
}
@ -473,7 +476,7 @@ public class SessionRestController {
return new ResponseEntity<>(HttpStatus.CONFLICT);
}
if (session.getParticipants().isEmpty()) {
// Session has no participants
// Session is not active (no connected participants)
return new ResponseEntity<>(HttpStatus.NOT_ACCEPTABLE);
}
@ -582,6 +585,76 @@ public class SessionRestController {
return new ResponseEntity<>(this.recordingManager.deleteRecordingFromHost(recordingId, false));
}
@RequestMapping(value = "/signal", method = RequestMethod.POST)
public ResponseEntity<?> signal(@RequestBody Map<?, ?> params) {
if (params == null) {
return this.generateErrorResponse("Error in body parameters. Cannot be empty", "/api/signal",
HttpStatus.BAD_REQUEST);
}
log.info("REST API: POST /api/signal {}", params.toString());
String sessionId;
String type;
ArrayList<String> to;
String data;
try {
sessionId = (String) params.get("session");
to = (ArrayList<String>) params.get("to");
type = (String) params.get("type");
data = (String) params.get("data");
} catch (ClassCastException e) {
return this.generateErrorResponse("Type error in some parameter", "/api/signal", HttpStatus.BAD_REQUEST);
}
JsonObject completeMessage = new JsonObject();
if (sessionId == null) {
// "session" parameter not found
return this.generateErrorResponse("\"session\" parameter is mandatory", "/api/signal",
HttpStatus.BAD_REQUEST);
}
Session session = sessionManager.getSession(sessionId);
if (session == null) {
session = sessionManager.getSessionNotActive(sessionId);
if (session != null) {
// Session is not active (no connected participants)
return new ResponseEntity<>(HttpStatus.NOT_ACCEPTABLE);
}
// Session does not exist
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
if (type != null) {
completeMessage.addProperty("type", type);
}
if (data != null) {
completeMessage.addProperty("data", data);
}
if (to != null) {
try {
Gson gson = new GsonBuilder().create();
JsonArray toArray = gson.toJsonTree(to).getAsJsonArray();
completeMessage.add("to", toArray);
} catch (IllegalStateException exception) {
return this.generateErrorResponse("\"to\" parameter is not a valid JSON array", "/api/signal",
HttpStatus.BAD_REQUEST);
}
}
try {
sessionManager.sendMessage(completeMessage.toString(), sessionId);
} catch (OpenViduException e) {
return this.generateErrorResponse("\"to\" array has no valid connection identifiers", "/api/signal",
HttpStatus.NOT_ACCEPTABLE);
}
return new ResponseEntity<>(HttpStatus.OK);
}
private ResponseEntity<String> generateErrorResponse(String errorMessage, String path, HttpStatus status) {
JsonObject responseJson = new JsonObject();
responseJson.addProperty("timestamp", System.currentTimeMillis());