Skip to content

Commit

Permalink
Merge pull request #6103 from ant-media/moveSubtrackMethodsToRestServ…
Browse files Browse the repository at this point in the history
…iceBase

Move subtrack methods to rest service base
  • Loading branch information
mekya authored Mar 6, 2024
2 parents 4479da2 + ec245d7 commit d6417fb
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 128 deletions.
127 changes: 3 additions & 124 deletions src/main/java/io/antmedia/rest/BroadcastRestService.java
Original file line number Diff line number Diff line change
Expand Up @@ -1124,13 +1124,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)
Expand All @@ -1139,55 +1132,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)
Expand All @@ -1198,35 +1143,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)
Expand Down Expand Up @@ -1259,21 +1176,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)
Expand All @@ -1283,31 +1185,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
Expand Down
132 changes: 132 additions & 0 deletions src/main/java/io/antmedia/rest/RestServiceBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -1868,6 +1868,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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1917,23 +1917,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
Expand Down

0 comments on commit d6417fb

Please sign in to comment.