From cf55bd886d59cb516e6d3c3bcbba7e20b23fa0e7 Mon Sep 17 00:00:00 2001 From: tahir Date: Tue, 15 Feb 2022 15:01:39 +0300 Subject: [PATCH 1/4] Make signaling handler the connection point --- .../io/antmedia/enterprise/streamapp/WebSocketLocalHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/antmedia/enterprise/streamapp/WebSocketLocalHandler.java b/src/main/java/io/antmedia/enterprise/streamapp/WebSocketLocalHandler.java index ae7c1dc4..600e14f9 100644 --- a/src/main/java/io/antmedia/enterprise/streamapp/WebSocketLocalHandler.java +++ b/src/main/java/io/antmedia/enterprise/streamapp/WebSocketLocalHandler.java @@ -98,7 +98,7 @@ private void createHandler(ApplicationContext context, Session session) { // If user want to RTMP play, should add rtmp query in websocket URL. if(io.antmedia.rest.RestServiceBase.isEnterprise() && !rtmpForward) { - Class clazz = Class.forName("io.antmedia.enterprise.webrtc.WebSocketEnterpriseHandler"); + Class clazz = Class.forName("io.antmedia.enterprise.webrtc.WebSocketSignalingHandler"); handler = (WebSocketCommunityHandler) clazz.getConstructor(ApplicationContext.class, Session.class).newInstance(context, session); } else { From 1aed34a496abfedc63959c3d40621004abd956d7 Mon Sep 17 00:00:00 2001 From: tahir Date: Tue, 22 Feb 2022 15:12:07 +0300 Subject: [PATCH 2/4] Implement signaling websocket entry point --- .../WebSocketSignalingLocalHandler.java | 126 ++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 src/main/java/io/antmedia/enterprise/streamapp/WebSocketSignalingLocalHandler.java diff --git a/src/main/java/io/antmedia/enterprise/streamapp/WebSocketSignalingLocalHandler.java b/src/main/java/io/antmedia/enterprise/streamapp/WebSocketSignalingLocalHandler.java new file mode 100644 index 00000000..83dab95a --- /dev/null +++ b/src/main/java/io/antmedia/enterprise/streamapp/WebSocketSignalingLocalHandler.java @@ -0,0 +1,126 @@ +package io.antmedia.enterprise.streamapp; + +import java.io.IOException; + +import javax.websocket.EndpointConfig; +import javax.websocket.OnClose; +import javax.websocket.OnError; +import javax.websocket.OnMessage; +import javax.websocket.OnOpen; +import javax.websocket.Session; +import javax.websocket.server.ServerEndpoint; + +import org.apache.catalina.core.ApplicationContextFacade; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.apache.commons.lang3.reflect.FieldUtils; + +import org.json.simple.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationContext; +import org.springframework.web.context.ConfigurableWebApplicationContext; +import org.springframework.web.context.support.WebApplicationContextUtils; + +import io.antmedia.websocket.WebSocketCommunityHandler; +import io.antmedia.websocket.WebSocketConstants; + + +@ServerEndpoint(value="/signaling", configurator=AMSEndpointConfigurator.class) +public class WebSocketSignalingLocalHandler { + + WebSocketCommunityHandler handler; + private String userAgent = "N/A"; + + protected static Logger logger = LoggerFactory.getLogger(WebSocketLocalHandler.class); + + @OnOpen + public void onOpen(Session session, EndpointConfig config) { + if(config.getUserProperties().containsKey(AMSEndpointConfigurator.USER_AGENT)) { + userAgent = (String) config.getUserProperties().get(AMSEndpointConfigurator.USER_AGENT); + } + + logger.info("Web Socket opened session:{} user-agent:{}", session.getId(), userAgent); + + //increase max text buffer size - Chrome 90 requires + session.setMaxTextMessageBufferSize(8192 * 10); + } + + + @OnClose + public void onClose(Session session) { + if(handler != null) { + handler.onClose(session); + } + } + + @OnError + public void onError(Session session, Throwable throwable) { + if(handler != null) { + handler.onError(session, throwable); + } + } + + @OnMessage + public void onMessage(Session session, String message) { + if(handler == null) { + ConfigurableWebApplicationContext ctxt = null; + try { + ApplicationContextFacade servletContext = (ApplicationContextFacade) FieldUtils.readField(session.getContainer(), "servletContext", true); + ctxt = (ConfigurableWebApplicationContext) WebApplicationContextUtils.getWebApplicationContext(servletContext); + } catch (Exception e) { + logger.error("Application context can not be set to WebSocket handler"); + logger.error(ExceptionUtils.getMessage(e)); + } + + if(ctxt != null && ctxt.isRunning()) { + createHandler(ctxt, session); + handler.onMessage(session, message); + } + else { + sendNotInitializedError(session); + } + } + else { + handler.onMessage(session, message); + } + } + + private void createHandler(ApplicationContext context, Session session) { + try { + + boolean rtmpForward; + + try { + rtmpForward = session.getRequestParameterMap().get("rtmpForward").get(0).contains("true"); + } catch (Exception e) { + rtmpForward = false; + } + + // If user want to RTMP play, should add rtmp query in websocket URL. + if(io.antmedia.rest.RestServiceBase.isEnterprise() && !rtmpForward) { + Class clazz = Class.forName("io.antmedia.enterprise.webrtc.WebSocketSignalingHandler"); + handler = (WebSocketCommunityHandler) clazz.getConstructor(ApplicationContext.class, Session.class).newInstance(context, session); + } + else { + handler = new WebSocketCommunityHandler(context, session); + } + + handler.setUserAgent(userAgent); + } catch (Exception e) { + logger.error("WebSocket handler cannot be created"); + logger.error(ExceptionUtils.getMessage(e)); + } + } + + + public void sendNotInitializedError(Session session) { + JSONObject jsonResponse = new JSONObject(); + jsonResponse.put(WebSocketConstants.COMMAND, WebSocketConstants.ERROR_COMMAND); + jsonResponse.put(WebSocketConstants.DEFINITION, WebSocketConstants.NOT_INITIALIZED_YET); + try { + session.getBasicRemote().sendText(jsonResponse.toJSONString()); + } catch (IOException e) { + logger.error(ExceptionUtils.getStackTrace(e)); + } + } +} From cfba01fcc816b9ed7907c1a745c73db399b8c3fa Mon Sep 17 00:00:00 2001 From: tahir Date: Tue, 22 Feb 2022 15:13:45 +0300 Subject: [PATCH 3/4] Fix typo --- .../enterprise/streamapp/WebSocketSignalingLocalHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/antmedia/enterprise/streamapp/WebSocketSignalingLocalHandler.java b/src/main/java/io/antmedia/enterprise/streamapp/WebSocketSignalingLocalHandler.java index 83dab95a..a713a747 100644 --- a/src/main/java/io/antmedia/enterprise/streamapp/WebSocketSignalingLocalHandler.java +++ b/src/main/java/io/antmedia/enterprise/streamapp/WebSocketSignalingLocalHandler.java @@ -31,7 +31,7 @@ public class WebSocketSignalingLocalHandler { WebSocketCommunityHandler handler; private String userAgent = "N/A"; - protected static Logger logger = LoggerFactory.getLogger(WebSocketLocalHandler.class); + protected static Logger logger = LoggerFactory.getLogger(WebSocketSignalingLocalHandler.class); @OnOpen public void onOpen(Session session, EndpointConfig config) { From 5d73ea46cfd222f868cdfc4048d80e502fdc248f Mon Sep 17 00:00:00 2001 From: mekya Date: Wed, 13 Apr 2022 23:39:54 +0300 Subject: [PATCH 4/4] Fix websocket endpoint class & refactor signaling endpoint --- .../antmedia/enterprise/streamapp/WebSocketLocalHandler.java | 2 +- .../enterprise/streamapp/WebSocketSignalingLocalHandler.java | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/antmedia/enterprise/streamapp/WebSocketLocalHandler.java b/src/main/java/io/antmedia/enterprise/streamapp/WebSocketLocalHandler.java index 600e14f9..ae7c1dc4 100644 --- a/src/main/java/io/antmedia/enterprise/streamapp/WebSocketLocalHandler.java +++ b/src/main/java/io/antmedia/enterprise/streamapp/WebSocketLocalHandler.java @@ -98,7 +98,7 @@ private void createHandler(ApplicationContext context, Session session) { // If user want to RTMP play, should add rtmp query in websocket URL. if(io.antmedia.rest.RestServiceBase.isEnterprise() && !rtmpForward) { - Class clazz = Class.forName("io.antmedia.enterprise.webrtc.WebSocketSignalingHandler"); + Class clazz = Class.forName("io.antmedia.enterprise.webrtc.WebSocketEnterpriseHandler"); handler = (WebSocketCommunityHandler) clazz.getConstructor(ApplicationContext.class, Session.class).newInstance(context, session); } else { diff --git a/src/main/java/io/antmedia/enterprise/streamapp/WebSocketSignalingLocalHandler.java b/src/main/java/io/antmedia/enterprise/streamapp/WebSocketSignalingLocalHandler.java index a713a747..cd20f236 100644 --- a/src/main/java/io/antmedia/enterprise/streamapp/WebSocketSignalingLocalHandler.java +++ b/src/main/java/io/antmedia/enterprise/streamapp/WebSocketSignalingLocalHandler.java @@ -25,7 +25,7 @@ import io.antmedia.websocket.WebSocketConstants; -@ServerEndpoint(value="/signaling", configurator=AMSEndpointConfigurator.class) +@ServerEndpoint(value="/websocket/signaling", configurator=AMSEndpointConfigurator.class) public class WebSocketSignalingLocalHandler { WebSocketCommunityHandler handler; @@ -43,6 +43,7 @@ public void onOpen(Session session, EndpointConfig config) { //increase max text buffer size - Chrome 90 requires session.setMaxTextMessageBufferSize(8192 * 10); + }