From 7f65f8ed5e66fb8dd017ff9e2668e50c6c78c5cb Mon Sep 17 00:00:00 2001 From: pabloFuente Date: Mon, 11 Dec 2017 14:50:41 +0100 Subject: [PATCH] 'signal' method --- openvidu-browser/package.json | 2 +- openvidu-browser/ts/OpenVidu/Session.ts | 21 +++++++- .../ts/OpenViduInternal/SessionInternal.ts | 32 ++++++----- .../io/openvidu/client/OpenViduException.java | 5 +- .../openvidu/client/ServerJsonRpcHandler.java | 14 ++--- .../client/internal/ProtocolElements.java | 6 +-- .../server/core/NotificationRoomManager.java | 17 ++++-- .../core/api/NotificationRoomHandler.java | 4 +- .../DefaultNotificationRoomHandler.java | 54 +++++++++++++++---- 9 files changed, 116 insertions(+), 39 deletions(-) diff --git a/openvidu-browser/package.json b/openvidu-browser/package.json index 9d40c397..a92093e6 100644 --- a/openvidu-browser/package.json +++ b/openvidu-browser/package.json @@ -1,6 +1,6 @@ { "name": "openvidu-browser", - "version": "1.1.2", + "version": "1.1.3", "description": "OpenVidu Browser", "author": "OpenVidu", "license": "Apache-2.0", diff --git a/openvidu-browser/ts/OpenVidu/Session.ts b/openvidu-browser/ts/OpenVidu/Session.ts index 4b05f83c..aaee9946 100644 --- a/openvidu-browser/ts/OpenVidu/Session.ts +++ b/openvidu-browser/ts/OpenVidu/Session.ts @@ -1,4 +1,4 @@ -import { SessionInternal, SessionOptions } from '../OpenViduInternal/SessionInternal'; +import { SessionInternal, SessionOptions, SignalOptions } from '../OpenViduInternal/SessionInternal'; import { Stream } from '../OpenViduInternal/Stream'; import { Connection } from "../OpenViduInternal/Connection"; @@ -128,4 +128,23 @@ export class Session { subscriber.stream.removeVideo(); } + signal(signal: SignalOptions, completionHandler?: Function) { + var signalMessage = {}; + + if (signal.to && signal.to.length > 0) { + let connectionIds: string[] = []; + for (let i = 0; i < signal.to.length; i++) { + connectionIds.push(signal.to[i].connectionId); + } + signalMessage['to'] = connectionIds; + } else { + signalMessage['to'] = []; + } + + signalMessage['data'] = signal.data ? signal.data : ''; + signalMessage['type'] = signal.type ? signal.type : ''; + + this.openVidu.openVidu.sendMessage(this.sessionId, this.connection.connectionId, JSON.stringify(signalMessage)); + } + } diff --git a/openvidu-browser/ts/OpenViduInternal/SessionInternal.ts b/openvidu-browser/ts/OpenViduInternal/SessionInternal.ts index c616b0d6..52d73b92 100644 --- a/openvidu-browser/ts/OpenViduInternal/SessionInternal.ts +++ b/openvidu-browser/ts/OpenViduInternal/SessionInternal.ts @@ -14,6 +14,12 @@ export interface SessionOptions { thresholdSpeaker?: number; } +export interface SignalOptions { + type?: string; + to?: Connection[]; + data?: string; +} + export class SessionInternal { private id: string; @@ -364,20 +370,20 @@ export class SessionInternal { onNewMessage(msg) { - console.info("New message: " + JSON.stringify(msg)); - let room = msg.room; - let user = msg.user; - let message = msg.message; + console.info("New signal: " + JSON.stringify(msg)); + + this.ee.emitEvent('signal', [{ + data: msg.data, + from: this.participants[msg.from], + type: msg.type + }]); + + this.ee.emitEvent('signal:' + msg.type, [{ + data: msg.data, + from: this.participants[msg.from], + type: msg.type + }]); - if (user !== undefined) { - this.ee.emitEvent('newMessage', [{ - room: room, - user: user, - message: message - }]); - } else { - console.warn("User undefined in new message:", msg); - } } recvIceCandidate(msg) { diff --git a/openvidu-client/src/main/java/io/openvidu/client/OpenViduException.java b/openvidu-client/src/main/java/io/openvidu/client/OpenViduException.java index bd38288a..6a89ce15 100644 --- a/openvidu-client/src/main/java/io/openvidu/client/OpenViduException.java +++ b/openvidu-client/src/main/java/io/openvidu/client/OpenViduException.java @@ -43,7 +43,10 @@ public class OpenViduException extends JsonRpcErrorException { USER_UNAUTHORIZED_ERROR_CODE(401), ROLE_NOT_FOUND_ERROR_CODE(402), SESSIONID_CANNOT_BE_CREATED_ERROR_CODE(403), TOKEN_CANNOT_BE_CREATED_ERROR_CODE(404), - USER_METADATA_FORMAT_INVALID_ERROR_CODE(500); + 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); private int value; diff --git a/openvidu-client/src/main/java/io/openvidu/client/ServerJsonRpcHandler.java b/openvidu-client/src/main/java/io/openvidu/client/ServerJsonRpcHandler.java index 98d2470a..3219d526 100644 --- a/openvidu-client/src/main/java/io/openvidu/client/ServerJsonRpcHandler.java +++ b/openvidu-client/src/main/java/io/openvidu/client/ServerJsonRpcHandler.java @@ -109,13 +109,13 @@ public class ServerJsonRpcHandler extends DefaultJsonRpcHandler { private Notification participantSendMessage(Transaction transaction, Request request) { - String room = JsonRoomUtils.getRequestParam(request, - ProtocolElements.PARTICIPANTSENDMESSAGE_ROOM_PARAM, String.class); - String user = JsonRoomUtils.getRequestParam(request, - ProtocolElements.PARTICIPANTSENDMESSAGE_USER_PARAM, String.class); - String message = JsonRoomUtils.getRequestParam(request, - ProtocolElements.PARTICIPANTSENDMESSAGE_MESSAGE_PARAM, String.class); - SendMessageInfo eventInfo = new SendMessageInfo(room, user, message); + String data = JsonRoomUtils.getRequestParam(request, + ProtocolElements.PARTICIPANTSENDMESSAGE_DATA_PARAM, String.class); + String from = JsonRoomUtils.getRequestParam(request, + ProtocolElements.PARTICIPANTSENDMESSAGE_FROM_PARAM, String.class); + String type = JsonRoomUtils.getRequestParam(request, + ProtocolElements.PARTICIPANTSENDMESSAGE_TYPE_PARAM, String.class); + SendMessageInfo eventInfo = new SendMessageInfo(data, from, type); log.debug("Recvd send message event {}", eventInfo); return eventInfo; } diff --git a/openvidu-client/src/main/java/io/openvidu/client/internal/ProtocolElements.java b/openvidu-client/src/main/java/io/openvidu/client/internal/ProtocolElements.java index d725dcf7..b72a9028 100644 --- a/openvidu-client/src/main/java/io/openvidu/client/internal/ProtocolElements.java +++ b/openvidu-client/src/main/java/io/openvidu/client/internal/ProtocolElements.java @@ -93,9 +93,9 @@ public class ProtocolElements { public static final String PARTICIPANTUNPUBLISHED_NAME_PARAM = "name"; public static final String PARTICIPANTSENDMESSAGE_METHOD = "sendMessage"; - public static final String PARTICIPANTSENDMESSAGE_USER_PARAM = "user"; - public static final String PARTICIPANTSENDMESSAGE_ROOM_PARAM = "room"; - public static final String PARTICIPANTSENDMESSAGE_MESSAGE_PARAM = "message"; + public static final String PARTICIPANTSENDMESSAGE_DATA_PARAM = "data"; + public static final String PARTICIPANTSENDMESSAGE_FROM_PARAM = "from"; + public static final String PARTICIPANTSENDMESSAGE_TYPE_PARAM = "type"; public static final String ROOMCLOSED_METHOD = "roomClosed"; public static final String ROOMCLOSED_ROOM_PARAM = "room"; diff --git a/openvidu-server/src/main/java/io/openvidu/server/core/NotificationRoomManager.java b/openvidu-server/src/main/java/io/openvidu/server/core/NotificationRoomManager.java index 6e3cba51..219ded79 100644 --- a/openvidu-server/src/main/java/io/openvidu/server/core/NotificationRoomManager.java +++ b/openvidu-server/src/main/java/io/openvidu/server/core/NotificationRoomManager.java @@ -27,6 +27,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +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; import io.openvidu.server.core.RoomManager.JoinRoomReturnValue; @@ -265,10 +269,17 @@ public class NotificationRoomManager { throw new OpenViduException(Code.ROOM_NOT_FOUND_ERROR_CODE, "Provided room name '" + roomName + "' differs from the participant's room"); } - notificationRoomHandler.onSendMessage(request, message, userName, roomName, - internalManager.getParticipants(roomName), null); + try { + JsonObject messageJSON = new JsonParser().parse(message).getAsJsonObject(); + + notificationRoomHandler.onSendMessage(request, messageJSON, userName, roomName, internalManager.getParticipants(roomName), null); + + } catch (JsonSyntaxException | IllegalStateException e) { + throw new OpenViduException(Code.SIGNAL_FORMAT_INVALID_ERROR_CODE, + "Provided signal object '" + message + "' has not a valid JSON format"); + } } catch (OpenViduException e) { - log.warn("PARTICIPANT {}: Error sending message", userName, e); + log.warn("PARTICIPANT {}: Error sending signal", userName, e); notificationRoomHandler.onSendMessage(request, null, null, null, null, e); } } diff --git a/openvidu-server/src/main/java/io/openvidu/server/core/api/NotificationRoomHandler.java b/openvidu-server/src/main/java/io/openvidu/server/core/api/NotificationRoomHandler.java index a7b00c10..da53b4f8 100644 --- a/openvidu-server/src/main/java/io/openvidu/server/core/api/NotificationRoomHandler.java +++ b/openvidu-server/src/main/java/io/openvidu/server/core/api/NotificationRoomHandler.java @@ -20,6 +20,8 @@ import java.util.Set; import org.kurento.client.MediaElement; +import com.google.gson.JsonObject; + import io.openvidu.client.OpenViduException; import io.openvidu.server.core.NotificationRoomManager; import io.openvidu.server.core.api.pojo.ParticipantRequest; @@ -190,7 +192,7 @@ public interface NotificationRoomHandler extends RoomHandler { * null, then the operation was unsuccessful and the user should be responded * accordingly. */ - void onSendMessage(ParticipantRequest request, String message, String userName, String roomName, + void onSendMessage(ParticipantRequest request, JsonObject message, String userName, String roomName, Set participants, OpenViduException error); /** diff --git a/openvidu-server/src/main/java/io/openvidu/server/core/internal/DefaultNotificationRoomHandler.java b/openvidu-server/src/main/java/io/openvidu/server/core/internal/DefaultNotificationRoomHandler.java index 5946e3dc..5fd430ff 100644 --- a/openvidu-server/src/main/java/io/openvidu/server/core/internal/DefaultNotificationRoomHandler.java +++ b/openvidu-server/src/main/java/io/openvidu/server/core/internal/DefaultNotificationRoomHandler.java @@ -16,15 +16,20 @@ package io.openvidu.server.core.internal; +import java.util.HashSet; +import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; import org.kurento.client.IceCandidate; import org.springframework.beans.factory.annotation.Autowired; import com.google.gson.JsonArray; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; import io.openvidu.client.OpenViduException; +import io.openvidu.client.OpenViduException.Code; import io.openvidu.client.internal.ProtocolElements; import io.openvidu.server.InfoHandler; import io.openvidu.server.core.api.NotificationRoomHandler; @@ -200,23 +205,54 @@ public class DefaultNotificationRoomHandler implements NotificationRoomHandler { } @Override - public void onSendMessage(ParticipantRequest request, String message, String userName, + public void onSendMessage(ParticipantRequest request, JsonObject message, String userName, String roomName, Set participants, OpenViduException error) { if (error != null) { notifService.sendErrorResponse(request, null, error); return; } - notifService.sendResponse(request, new JsonObject()); JsonObject params = new JsonObject(); - params.addProperty(ProtocolElements.PARTICIPANTSENDMESSAGE_ROOM_PARAM, roomName); - params.addProperty(ProtocolElements.PARTICIPANTSENDMESSAGE_USER_PARAM, userName); - params.addProperty(ProtocolElements.PARTICIPANTSENDMESSAGE_MESSAGE_PARAM, message); - - for (UserParticipant participant : participants) { - notifService.sendNotification(participant.getParticipantId(), - ProtocolElements.PARTICIPANTSENDMESSAGE_METHOD, params); + params.addProperty(ProtocolElements.PARTICIPANTSENDMESSAGE_DATA_PARAM, message.get("data").getAsString()); + params.addProperty(ProtocolElements.PARTICIPANTSENDMESSAGE_FROM_PARAM, userName); + params.addProperty(ProtocolElements.PARTICIPANTSENDMESSAGE_TYPE_PARAM, message.get("type").getAsString()); + + Set toSet = new HashSet(); + + if (message.has("to")) { + JsonArray toJson = message.get("to").getAsJsonArray(); + for (int i=0; i < toJson.size(); i++) { + JsonElement el = toJson.get(i); + if (el.isJsonNull()) { + throw new OpenViduException(Code.SIGNAL_TO_INVALID_ERROR_CODE, "Signal \"to\" field invalid format: null"); + } + toSet.add(el.getAsString()); + } } + + if (toSet.isEmpty()) { + for (UserParticipant participant : participants) { + notifService.sendNotification(participant.getParticipantId(), + ProtocolElements.PARTICIPANTSENDMESSAGE_METHOD, params); + } + } else { + Set participantNames = participants.stream() + .map(UserParticipant::getUserName) + .collect(Collectors.toSet()); + for (String to : toSet) { + if (participantNames.contains(to)) { + Optional p = participants.stream() + .filter(x -> to.equals(x.getUserName())) + .findFirst(); + notifService.sendNotification(p.get().getParticipantId(), + ProtocolElements.PARTICIPANTSENDMESSAGE_METHOD, params); + } else { + throw new OpenViduException(Code.SIGNAL_TO_INVALID_ERROR_CODE, "Signal \"to\" field invalid format: Connection [" + to + "] does not exist"); + } + } + } + + notifService.sendResponse(request, new JsonObject()); } @Override