Skip to content

Commit

Permalink
Merge pull request #5727 from ant-media/conferenceRoomParticipantCapa…
Browse files Browse the repository at this point in the history
…city

Add max participant capacity to conferences(aka. subtracks)
  • Loading branch information
mekya committed May 16, 2024
2 parents 6a0e440 + 10f905e commit 9c268c0
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 25 deletions.
17 changes: 17 additions & 0 deletions src/main/java/io/antmedia/datastore/db/types/Broadcast.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@ public class Broadcast {
@Schema(description ="Conference mode. It's used if this broadcast has some specific modes. It's created for backward compatibility. It will be deleted.")
@Deprecated(forRemoval = true, since = "2.9.1")
private String conferenceMode;


/**
* The number of subtracks that is allowed to be created for the broadcast.
* It's useful for limiting number of conference attendees. Default value is -1
* and -1 means no limit
*/
@Schema(description ="Number of subtracks that is allowed to be created for the broadcast. It's usefult for limiting number of conference attendees. Default value is -1 and it means no limit")
private int subtracksLimit = -1;

@Entity
public static class PlayListItem
Expand Down Expand Up @@ -904,4 +913,12 @@ public void setConferenceMode(String conferenceMode) {
this.conferenceMode = conferenceMode;
}

public int getSubtracksLimit() {
return subtracksLimit;
}

public void setSubtracksLimit(int subtracksLimit) {
this.subtracksLimit = subtracksLimit;
}

}
59 changes: 36 additions & 23 deletions src/main/java/io/antmedia/rest/RestServiceBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@

public abstract class RestServiceBase {

private static final String MAIN_TRACK_OF_THE_STREAM = "Main track of the stream ";

public static final String REPLACE_CHARS_FOR_SECURITY = "[\n\r]";

public class BroadcastStatistics {
Expand Down Expand Up @@ -1848,33 +1850,50 @@ public static void logWarning(String message, String... arguments) {
}
}

public static Result addSubTrack(String id, String subTrackId, DataStore store) {
public static Result addSubTrack(String id, String subTrackId, DataStore store)
{
Result result = new Result(false);
Broadcast subTrack = store.get(subTrackId);
Broadcast mainTrack = store.get(id);
String message = "";
if (subTrack != null)
if (subTrack != null && mainTrack != null)
{

int subtrackLimit = mainTrack.getSubtracksLimit();
List<String> subTrackStreamIds = mainTrack.getSubTrackStreamIds();
if (subtrackLimit != -1 && subTrackStreamIds != null && subTrackStreamIds.size() >= subtrackLimit)
{
message = "Subtrack limit is reached for the main track:" + id;
logWarning("Subtrack limit is reached for the main track:{}", id.replaceAll(REPLACE_CHARS, "_"));
result.setMessage(message);
return result;
}

if (subTrackStreamIds == null) {
subTrackStreamIds = new ArrayList<>();
}

subTrack.setMainTrackStreamId(id);
//Update subtrack's main Track Id

boolean success = store.updateBroadcastFields(subTrackId, subTrack);
if (success) {
success = store.addSubTrack(id, subTrackId);

if (success)
{
subTrackStreamIds.add(subTrackId);
success = store.updateBroadcastFields(id, mainTrack);
RestServiceBase.setResultSuccess(result, success, "Subtrack:" + subTrackId + " cannot be added to main track: " + id);

}
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, "_"));
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, "_"));
message = "There is no stream with id:" + subTrackId + " as subtrack or " + id + " as mainTrack";
logWarning("There is no stream with id:{} as subtrack or {} as mainTrack" , subTrackId.replaceAll(REPLACE_CHARS, "_"), id.replaceAll(REPLACE_CHARS, "_"));
}
result.setMessage(message);
return result;
Expand Down Expand Up @@ -1902,28 +1921,22 @@ public static Result removeSubTrack(String id, String subTrackId, DataStore stor
}
else
{
RestServiceBase.setResultSuccess(result, false, "Main track of the stream " + subTrackId + " which is " + id +" cannot be removed");
if (logger.isInfoEnabled()) {
logger.info( "Main track of the stream {} which is {} cannot be removed", subTrackId, id);
}

RestServiceBase.setResultSuccess(result, false, MAIN_TRACK_OF_THE_STREAM + subTrackId + " which is " + id +" cannot be removed");
logger.info(MAIN_TRACK_OF_THE_STREAM +" {} which is {} cannot be removed", subTrackId, id);
}
}
else {
RestServiceBase.setResultSuccess(result, false, "Main track of the stream " + subTrackId + " which is " + id +" cannot be updated");
if (logger.isInfoEnabled())
{
logger.info( "Main track of the stream {} which is {} not updated because either subtrack is null or its maintrack does not match with mainTrackId:{}", subTrackId, id, id);
}
RestServiceBase.setResultSuccess(result, false, MAIN_TRACK_OF_THE_STREAM + subTrackId + " which is " + id +" cannot be updated");
logger.info( MAIN_TRACK_OF_THE_STREAM +"{} which is {} not updated because either subtrack is null or its maintrack does not match with mainTrackId:{}", subTrackId, id, id);

}

}
else
{
RestServiceBase.setResultSuccess(result, false, "Subtrack(" + subTrackId + ") is not removed from mainTrack:" + id);
if (logger.isInfoEnabled())
{
logger.info("Subtrack({}) is not removed from mainTrack:{}", subTrackId, id);
}
logger.info("Subtrack({}) is not removed from mainTrack:{}", subTrackId, id);
}

}
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/io/antmedia/websocket/WebSocketConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ private WebSocketConstants() {
*/
public static final String NOT_ALLOWED_UNREGISTERED_STREAM = "not_allowed_unregistered_streams";


/**
* This is sent back to the user if mainTrack
*/
public static final String MAX_SUBTRACK_COUNT_REACHED = "main_track_has_max_subtrack_count__not_allowed_to_add_more_subtracks";


/**
* This is sent back to the user when there is no room specified in
* joining the video conference
Expand All @@ -168,8 +175,8 @@ private WebSocketConstants() {
* joining the video conference
*/
public static final String ROOM_TIME_INVALID = "room_not_active_or_expired";


/**
* This is sent back to the user when stream plannedStartDate and plannedEndDate
* values are in interval or not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2882,6 +2882,78 @@ public void testAddSubtrack() throws Exception {
conferenceRoom = datastore.get(mainTrackId);
assertEquals(1,conferenceRoom.getSubTrackStreamIds().size());

}

@Test
public void testAddSubtrackWhenThereIsALimit()
{
String mainTrackId = RandomStringUtils.randomAlphanumeric(8);
String subTrackId = RandomStringUtils.randomAlphanumeric(8);

BroadcastRestService broadcastRestService = new BroadcastRestService();
broadcastRestService.setApplication(Mockito.mock(AntMediaApplicationAdapter.class));
DataStore datastore = Mockito.spy(new InMemoryDataStore("dummy"));

broadcastRestService.setDataStore(datastore);

Result result = broadcastRestService.addSubTrack(mainTrackId, subTrackId);
assertFalse(result.isSuccess());

Broadcast mainTrack= new Broadcast();
try {
mainTrack.setStreamId(mainTrackId);
} catch (Exception e) {
e.printStackTrace();
}

datastore.save(mainTrack);

//it should be false because there is no subtrack
result = broadcastRestService.addSubTrack(mainTrackId, subTrackId);
assertFalse(result.isSuccess());

Broadcast subtrack = new Broadcast();
try {
subtrack.setStreamId(subTrackId);
} catch (Exception e) {
e.printStackTrace();
}
datastore.save(subtrack);


//it should return because mainTrackId and subtrackId exists
result = broadcastRestService.addSubTrack(mainTrackId, subTrackId);
assertTrue(result.isSuccess());


assertEquals(-1, mainTrack.getSubtracksLimit());


//set a subtrack limit
mainTrack.setSubtracksLimit(1);

String subTrackId2 = RandomStringUtils.randomAlphanumeric(8);

Broadcast subtrack2 = new Broadcast();
try {
subtrack2.setStreamId(subTrackId2);
} catch (Exception e) {
e.printStackTrace();
}
datastore.save(subtrack2);


result = broadcastRestService.addSubTrack(mainTrackId, subTrackId2);
assertFalse(result.isSuccess());

mainTrack.setSubtracksLimit(2);

result = broadcastRestService.addSubTrack(mainTrackId, subTrackId2);
assertTrue(result.isSuccess());

assertTrue(mainTrack.getSubTrackStreamIds().contains(subTrackId2));
assertTrue(mainTrack.getSubTrackStreamIds().contains(subTrackId));


}

Expand Down

0 comments on commit 9c268c0

Please sign in to comment.