From 96f58f8c216b7ad9cf9af255cea3f71532a051fd Mon Sep 17 00:00:00 2001 From: Mustafa BOLEKEN Date: Fri, 16 Feb 2024 11:37:49 +0300 Subject: [PATCH 1/2] Move subtrack methods to rest service base --- .../antmedia/rest/BroadcastRestService.java | 127 +---------------- .../io/antmedia/rest/RestServiceBase.java | 132 ++++++++++++++++++ 2 files changed, 135 insertions(+), 124 deletions(-) diff --git a/src/main/java/io/antmedia/rest/BroadcastRestService.java b/src/main/java/io/antmedia/rest/BroadcastRestService.java index 33d489f29..34aa39a1e 100644 --- a/src/main/java/io/antmedia/rest/BroadcastRestService.java +++ b/src/main/java/io/antmedia/rest/BroadcastRestService.java @@ -1094,13 +1094,6 @@ public Result deleteConferenceRoomV2(@ApiParam(value = "the id of the conference return new Result(super.deleteConferenceRoom(roomId, getDataStore())); } - - public void logWarning(String message, String... arguments) { - if (logger.isWarnEnabled()) { - logger.warn(message , arguments); - } - } - @ApiOperation(value = "Add a subtrack to a main track (broadcast).", notes = "", response = Result.class) @POST @Consumes(MediaType.APPLICATION_JSON) @@ -1109,55 +1102,7 @@ public void logWarning(String message, String... arguments) { public Result addSubTrack(@ApiParam(value = "Broadcast id(main track)", required = true) @PathParam("id") String id, @ApiParam(value = "Subtrack Stream Id", required = true) @QueryParam("id") String subTrackId) { - - Result result = new Result(false); - Broadcast subTrack = getDataStore().get(subTrackId); - String message = ""; - if (subTrack != null) - { - subTrack.setMainTrackStreamId(id); - //Update subtrack's main Track Id - - boolean success = getDataStore().updateBroadcastFields(subTrackId, subTrack); - if (success) { - success = getDataStore().addSubTrack(id, subTrackId); - - setResultSuccess(result, success, "Subtrack:" + subTrackId + " cannot be added to main track: " + id, - "Subtrack:{} cannot be added to main track:{} ", subTrackId.replaceAll(REPLACE_CHARS, "_"), id.replaceAll(REPLACE_CHARS, "_")); - - if (success) { - //if it's a room, add it to the room as well - //Ugly fix - //REFACTOR: Migrate conference room to Broadcast object by keeping the interface backward compatible - addStreamToConferenceRoom(id, subTrackId, getDataStore()); - } - - } - else - { - message = "Main track of the stream " + subTrackId + " cannot be updated"; - logWarning("Main track of the stream:{} cannot be updated to {}", subTrackId.replaceAll(REPLACE_CHARS, "_"), id.replaceAll(REPLACE_CHARS, "_")); - } - } - else - { - message = "There is not stream with id:" + subTrackId; - logWarning("There is not stream with id:{}" , subTrackId.replaceAll(REPLACE_CHARS, "_")); - } - result.setMessage(message); - return result; - } - - public void setResultSuccess(Result result, boolean success, String failMessage, String failLog, String... arguments) - { - if (success) { - result.setSuccess(true); - } - else { - result.setSuccess(false); - result.setMessage(failMessage); - logWarning(failLog, arguments); - } + return RestServiceBase.addSubTrack(id, subTrackId, getDataStore()); } @ApiOperation(value = "Delete a subtrack from a main track (broadcast).", notes = "", response = Result.class) @@ -1168,35 +1113,7 @@ public void setResultSuccess(Result result, boolean success, String failMessage, public Result removeSubTrack(@ApiParam(value = "Broadcast id(main track)", required = true) @PathParam("id") String id, @ApiParam(value = "Subtrack Stream Id", required = true) @QueryParam("id") String subTrackId) { - - Result result = new Result(false); - Broadcast subTrack = getDataStore().get(subTrackId); - if (subTrack != null) - { - if(id != null && id.equals(subTrack.getMainTrackStreamId())) { - subTrack.setMainTrackStreamId(""); - } - - boolean success = getDataStore().updateBroadcastFields(subTrackId, subTrack); - if (success) { - success = getDataStore().removeSubTrack(id, subTrackId); - - setResultSuccess(result, success, "Subtrack:" + subTrackId + " cannot be removed from main track: " + id, - "Subtrack:{} cannot be removed from main track:{} ", subTrackId.replaceAll(REPLACE_CHARS, "_"), id != null ? id.replaceAll(REPLACE_CHARS, "_") : null); - - } - else - { - setResultSuccess(result, false, "Main track of the stream " + subTrackId + " which is " + id +" cannot be updated", - "Main track of the stream:{} cannot be updated to {}", subTrackId.replaceAll(REPLACE_CHARS, "_"), id != null ? id.replaceAll(REPLACE_CHARS, "_") : null); - } - } - else - { - setResultSuccess(result, false, "There is no stream with id:" + subTrackId, "There is no stream with id:{}" , subTrackId.replaceAll(REPLACE_CHARS, "_")); - } - - return result; + return RestServiceBase.removeSubTrack(id, subTrackId, getDataStore()); } @ApiOperation(value = "Returns the stream info(width, height, bitrates and video codec) of the stream", response= BasicStreamInfo[].class) @@ -1229,21 +1146,6 @@ public BasicStreamInfo[] getStreamInfo(@PathParam("id") String streamId) return basicStreamInfo; } - public boolean isMainTrack(String streamId) { - boolean result = false; - if (streamId != null) - { - Broadcast broadcast = getDataStore().get(streamId); - if (broadcast != null) - { - result = !broadcast.getSubTrackStreamIds().isEmpty(); - } - } - - return result; - - } - @ApiOperation(value = "Send stream participants a message through Data Channel in a WebRTC stream", notes = "", response = Result.class) @POST @Consumes(MediaType.APPLICATION_JSON) @@ -1253,31 +1155,8 @@ public Result sendMessage(@ApiParam(value = "Message through Data Channel which @ApiParam(value = "Broadcast id", required = true) @PathParam("id") String id) { AntMediaApplicationAdapter application = getApplication(); - // check if WebRTC data channels are supported in this edition - if(application != null && application.isDataChannelMessagingSupported()) { - // check if data channel is enabled in the settings - if(application.isDataChannelEnabled()) { - // check if stream with given stream id exists - if(application.doesWebRTCStreamExist(id) || isMainTrack(id)) { - // send the message through the application - boolean status = application.sendDataChannelMessage(id,message); - if(status) { - return new Result(true); - } else { - return new Result(false, "Operation not completed"); - } - - } else { - return new Result(false, "Requested WebRTC stream does not exist"); - } - } else { - return new Result(false, "Data channels are not enabled"); - } - - } else { - return new Result(false, "Operation not supported in the Community Edition. Check the Enterprise version for more features."); - } + return RestServiceBase.sendDataChannelMessage(id, message, application, getDataStore()); } @ApiOperation(value = "Gets the conference room list from database", notes = "",responseContainer = "List", response = ConferenceRoom.class) @GET diff --git a/src/main/java/io/antmedia/rest/RestServiceBase.java b/src/main/java/io/antmedia/rest/RestServiceBase.java index 7bf1efe2a..d5fb630eb 100755 --- a/src/main/java/io/antmedia/rest/RestServiceBase.java +++ b/src/main/java/io/antmedia/rest/RestServiceBase.java @@ -1862,6 +1862,138 @@ public static boolean addStreamToConferenceRoom(String roomId, String streamId, return true; } + public static void setResultSuccess(Result result, boolean success, String failMessage, String failLog, String... arguments) + { + if (success) { + result.setSuccess(true); + } + else { + result.setSuccess(false); + result.setMessage(failMessage); + } + } + + public static void logWarning(String message, String... arguments) { + if (logger.isWarnEnabled()) { + logger.warn(message , arguments); + } + } + + public static Result addSubTrack(String id, String subTrackId,DataStore store) { + Result result = new Result(false); + Broadcast subTrack = store.get(subTrackId); + String message = ""; + if (subTrack != null) + { + subTrack.setMainTrackStreamId(id); + //Update subtrack's main Track Id + + boolean success = store.updateBroadcastFields(subTrackId, subTrack); + if (success) { + success = store.addSubTrack(id, subTrackId); + + RestServiceBase.setResultSuccess(result, success, "Subtrack:" + subTrackId + " cannot be added to main track: " + id, + "Subtrack:{} cannot be added to main track:{} ", subTrackId.replaceAll(REPLACE_CHARS, "_"), id.replaceAll(REPLACE_CHARS, "_")); + + if (success) { + //if it's a room, add it to the room as well + //Ugly fix + //REFACTOR: Migrate conference room to Broadcast object by keeping the interface backward compatible + addStreamToConferenceRoom(id, subTrackId, store); + } + + } + else + + { + message = "Main track of the stream " + subTrackId + " cannot be updated"; + logWarning("Main track of the stream:{} cannot be updated to {}", subTrackId.replaceAll(REPLACE_CHARS, "_"), id.replaceAll(REPLACE_CHARS, "_")); + } + } + else + { + message = "There is not stream with id:" + subTrackId; + logWarning("There is not stream with id:{}" , subTrackId.replaceAll(REPLACE_CHARS, "_")); + } + result.setMessage(message); + return result; + } + + public static Result removeSubTrack(String id, String subTrackId,DataStore store) { + Result result = new Result(false); + Broadcast subTrack = store.get(subTrackId); + if (subTrack != null) + { + if(id != null && id.equals(subTrack.getMainTrackStreamId())) { + subTrack.setMainTrackStreamId(""); + } + + boolean success = store.updateBroadcastFields(subTrackId, subTrack); + if (success) { + success = store.removeSubTrack(id, subTrackId); + + RestServiceBase.setResultSuccess(result, success, "Subtrack:" + subTrackId + " cannot be removed from main track: " + id, + "Subtrack:{} cannot be removed from main track:{} ", subTrackId.replaceAll(REPLACE_CHARS, "_"), id != null ? id.replaceAll(REPLACE_CHARS, "_") : null); + + } + else + { + RestServiceBase.setResultSuccess(result, false, "Main track of the stream " + subTrackId + " which is " + id +" cannot be updated", + "Main track of the stream:{} cannot be updated to {}", subTrackId.replaceAll(REPLACE_CHARS, "_"), id != null ? id.replaceAll(REPLACE_CHARS, "_") : null); + } + } + else + { + RestServiceBase.setResultSuccess(result, false, "There is no stream with id:" + subTrackId, "There is no stream with id:{}" , subTrackId.replaceAll(REPLACE_CHARS, "_")); + } + + return result; + } + + public static boolean isMainTrack(String streamId, DataStore store) { + boolean result = false; + if (streamId != null) + { + Broadcast broadcast = store.get(streamId); + if (broadcast != null) + { + result = !broadcast.getSubTrackStreamIds().isEmpty(); + } + } + + return result; + + } + + public static Result sendDataChannelMessage(String id, String message, AntMediaApplicationAdapter application, DataStore store) + { + // check if WebRTC data channels are supported in this edition + if(application != null && application.isDataChannelMessagingSupported()) { + // check if data channel is enabled in the settings + if(application.isDataChannelEnabled()) { + // check if stream with given stream id exists + if(application.doesWebRTCStreamExist(id) || RestServiceBase.isMainTrack(id, store)) { + // send the message through the application + boolean status = application.sendDataChannelMessage(id,message); + if(status) { + return new Result(true); + } else { + return new Result(false, "Operation not completed"); + } + + } else { + return new Result(false, "Requested WebRTC stream does not exist"); + } + + } else { + return new Result(false, "Data channels are not enabled"); + } + + } else { + return new Result(false, "Operation not supported in the Community Edition. Check the Enterprise version for more features."); + } + } + public static synchronized boolean removeStreamFromRoom(String roomId, String streamId,DataStore store) { From 9cd64f24798b6cfb1c07300a6c612f26789df7d7 Mon Sep 17 00:00:00 2001 From: Mustafa BOLEKEN Date: Fri, 16 Feb 2024 12:51:15 +0300 Subject: [PATCH 2/2] Fix isMainTrack unit test --- .../test/rest/BroadcastRestServiceV2UnitTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/io/antmedia/test/rest/BroadcastRestServiceV2UnitTest.java b/src/test/java/io/antmedia/test/rest/BroadcastRestServiceV2UnitTest.java index c127b4056..54bda7964 100644 --- a/src/test/java/io/antmedia/test/rest/BroadcastRestServiceV2UnitTest.java +++ b/src/test/java/io/antmedia/test/rest/BroadcastRestServiceV2UnitTest.java @@ -1918,23 +1918,23 @@ public void testIsMainTrack() { Broadcast broadcast = Mockito.spy(new Broadcast()); // should return false because stream id is null - assertFalse(restServiceReal.isMainTrack(null)); + assertFalse(RestServiceBase.isMainTrack(null, store)); // should return false when broadcast does not exist when(store.get("streamId")).thenReturn(null); - assertFalse(restServiceReal.isMainTrack("streamId")); + assertFalse(RestServiceBase.isMainTrack("streamId", store)); // should return false when broadcast is not main track when(broadcast.getSubTrackStreamIds()).thenReturn(new ArrayList()); when(broadcast.getMainTrackStreamId()).thenReturn("mainTrackStreamId"); when(store.get("streamId")).thenReturn(broadcast); - assertFalse(restServiceReal.isMainTrack("streamId")); + assertFalse(RestServiceBase.isMainTrack("streamId", store)); // should return true when broadcast is main track when(broadcast.getSubTrackStreamIds()).thenReturn(List.copyOf(Arrays.asList("subTrackStreamId1", "subTrackStreamId2"))); when(broadcast.getMainTrackStreamId()).thenReturn(null); when(store.get("streamId")).thenReturn(broadcast); - assertTrue(restServiceReal.isMainTrack("streamId")); + assertTrue(RestServiceBase.isMainTrack("streamId", store)); } @Test