Skip to content

Commit

Permalink
implement subtrack deletion on maintrack deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
burak-58 committed Jan 20, 2025
1 parent 454f4cf commit 5e7b335
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/main/java/io/antmedia/rest/BroadcastRestService.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,9 @@ else if (!StreamIdValidator.isStreamIdValid(broadcast.getStreamId()))
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
@Override
public Result deleteBroadcast(@Parameter(description = " Id of the broadcast", required = true) @PathParam("id") String id) {
return super.deleteBroadcast(id);
public Result deleteBroadcast(@Parameter(description = " Id of the broadcast", required = true) @PathParam("id") String id,
@Parameter(description = "Deletion request for subtracks also", required = false) @QueryParam("deleteSubtracks") Boolean deleteSubtracks) {
return super.deleteBroadcast(id, deleteSubtracks);
}


Expand Down Expand Up @@ -1622,7 +1623,7 @@ public Response editConferenceRoom(@Parameter(description="Room id") @PathParam(
@Hidden
@Deprecated(since="2.9.1", forRemoval=true)
public Result deleteConferenceRoomV2(@Parameter(description = "the id of the conference room", required = true) @PathParam("room_id") String roomId) {
return deleteBroadcast(roomId);
return deleteBroadcast(roomId, false);
}

@Operation(summary = "Add a subtrack to a main track (broadcast)",
Expand Down
44 changes: 42 additions & 2 deletions src/main/java/io/antmedia/rest/RestServiceBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import io.antmedia.datastore.db.types.Broadcast;
import io.antmedia.datastore.db.types.BroadcastUpdate;
import io.antmedia.datastore.db.types.Broadcast.PlayListItem;
import io.antmedia.filter.JWTFilter;
import io.antmedia.datastore.db.types.ConferenceRoom;
import io.antmedia.datastore.db.types.Endpoint;
import io.antmedia.datastore.db.types.TensorFlowObject;
Expand Down Expand Up @@ -337,7 +338,11 @@ public void setServerSettings(ServerSettings serverSettings) {
}


protected Result deleteBroadcast(String id) {
protected Result deleteBroadcast(String id, Boolean deleteSubtracks) {
if(deleteSubtracks == null) {
deleteSubtracks = true;
}

Result result = new Result (false);
boolean stopResult = false;
Broadcast broadcast = null;
Expand All @@ -352,6 +357,11 @@ protected Result deleteBroadcast(String id) {
getApplication().cancelPlaylistSchedule(broadcast.getStreamId());

result.setSuccess(getDataStore().delete(id));

if(deleteSubtracks) {
boolean subtrackDeletionResult = deleteSubtracks(broadcast);
result.setSuccess(subtrackDeletionResult);
}

if(result.isSuccess())
{
Expand All @@ -373,6 +383,36 @@ protected Result deleteBroadcast(String id) {
return result;
}

private boolean deleteSubtracks(Broadcast broadcast) {
boolean result = true;
long subtrackCount = getDataStore().getSubtrackCount(broadcast.getStreamId(), "", "");
logger.info("{} Subtracks of maintrack {} will also be deleted.", subtrackCount, broadcast.getStreamId());

if(subtrackCount > 0) {
List<Broadcast> subtracks = getDataStore().getSubtracks(broadcast.getStreamId(), 0, (int)subtrackCount, "");

for (Broadcast subtrack : subtracks) {
String jwtToken = JWTFilter.generateJwtToken(
getAppSettings().getClusterCommunicationKey(),
System.currentTimeMillis() + 5000
);

String restRouteOfNode = "http://" + subtrack.getOriginAdress() + ":" +
getServerSettings().getDefaultHttpPort() +
File.separator + getAppSettings().getAppName() +
File.separator + "rest" +
File.separator + "v2" +
File.separator + "broadcasts" +
File.separator + subtrack.getStreamId();

result &= getApplication().sendClusterDelete(restRouteOfNode, jwtToken);
}
}


return result;
}

protected Result deleteBroadcasts(String[] streamIds) {

Result result = new Result(false);
Expand All @@ -381,7 +421,7 @@ protected Result deleteBroadcasts(String[] streamIds) {
{
for (String id : streamIds)
{
result = deleteBroadcast(id);
result = deleteBroadcast(id, false);
if (!result.isSuccess())
{
id = id.replaceAll(REPLACE_CHARS_FOR_SECURITY, "_" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,7 @@ public void testDeleteBroadcast() {
assertEquals(streamCount, broadcastList.size());

for (Broadcast item: broadcastList) {
Result result = restServiceReal.deleteBroadcast(item.getStreamId());
Result result = restServiceReal.deleteBroadcast(item.getStreamId(), false);
assertTrue(result.isSuccess());
}

Expand All @@ -1236,7 +1236,7 @@ public void testDeleteBroadcast() {

when(restServiceReal.getServerSettings().getHostAddress()).thenReturn("55.55.55.55");

Result result = restServiceReal.deleteBroadcast(broadcast.getStreamId());
Result result = restServiceReal.deleteBroadcast(broadcast.getStreamId(), false);
assertTrue(result.isSuccess());
}

Expand All @@ -1251,7 +1251,7 @@ public void testDeleteBroadcast() {

when(restServiceReal.getServerSettings().getHostAddress()).thenReturn("127.0.0.1");

Result result = restServiceReal.deleteBroadcast(broadcast.getStreamId());
Result result = restServiceReal.deleteBroadcast(broadcast.getStreamId(), false);
assertTrue(result.isSuccess());
}

Expand All @@ -1266,7 +1266,7 @@ public void testDeleteBroadcast() {

when(restServiceReal.getServerSettings().getHostAddress()).thenReturn("55.55.55.55");

Result result = restServiceReal.deleteBroadcast(broadcast.getStreamId());
Result result = restServiceReal.deleteBroadcast(broadcast.getStreamId(), false);
assertTrue(result.isSuccess());
}

Expand Down Expand Up @@ -3747,4 +3747,85 @@ public void testCheckURL() {
assertFalse(restServiceReal.checkStreamUrl("dummy://something"));
}


@Test
public void testDeleteBroadcastWithSubreacks() {
AppSettings settings = new AppSettings();
String serverName = "fully.qualified.domain.name";
restServiceReal.setAppSettings(settings);
ServerSettings serverSettings = mock(ServerSettings.class);
when(serverSettings.getServerName()).thenReturn(serverName);
restServiceReal.setServerSettings(serverSettings);

ApplicationContext context = mock(ApplicationContext.class);
restServiceReal.setAppCtx(context);
when(context.containsBean(any())).thenReturn(false);


DataStore store = new InMemoryDataStore("testdb");
restServiceReal.setDataStore(store);

Scope scope = mock(Scope.class);
String scopeName = "scope";
when(scope.getName()).thenReturn(scopeName);
restServiceReal.setScope(scope);

AntMediaApplicationAdapter appAdaptor = Mockito.mock(AntMediaApplicationAdapter.class);
Mockito.when(appAdaptor.stopStreaming(any())).thenReturn(new Result(true));

restServiceReal.setApplication(appAdaptor);

Broadcast mainTrack = new Broadcast();
try {
mainTrack.setStreamId("mainTrack");
} catch (Exception e) {
e.printStackTrace();
}
mainTrack.setStatus(IAntMediaStreamHandler.BROADCAST_STATUS_BROADCASTING);
mainTrack.setUpdateTime(System.currentTimeMillis());

Broadcast subtrack1 = new Broadcast();
try {
subtrack1.setStreamId("subtrack1");
} catch (Exception e) {
e.printStackTrace();
}
subtrack1.setMainTrackStreamId(mainTrack.getStreamId());
subtrack1.setStatus(IAntMediaStreamHandler.BROADCAST_STATUS_BROADCASTING);
subtrack1.setUpdateTime(System.currentTimeMillis());


Broadcast subtrack2 = new Broadcast();
try {
subtrack2.setStreamId("subtrack2");
} catch (Exception e) {
e.printStackTrace();
}
subtrack2.setMainTrackStreamId(mainTrack.getStreamId());
subtrack2.setStatus(IAntMediaStreamHandler.BROADCAST_STATUS_BROADCASTING);
subtrack2.setUpdateTime(System.currentTimeMillis());

Broadcast subtrack3 = new Broadcast();
try {
subtrack3.setStreamId("subtrack3");
} catch (Exception e) {
e.printStackTrace();
}
subtrack3.setMainTrackStreamId(mainTrack.getStreamId());
subtrack3.setStatus(IAntMediaStreamHandler.BROADCAST_STATUS_FINISHED);
subtrack3.setUpdateTime(System.currentTimeMillis() - 50000);


store.save(mainTrack);
store.save(subtrack1);
store.save(subtrack2);
store.save(subtrack3);

Result result = restServiceReal.deleteBroadcast(mainTrack.getStreamId(), true);

verify(appAdaptor).stopStreaming(eq(mainTrack));
verify(appAdaptor, times(3)).sendClusterDelete(anyString(), anyString());

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -283,20 +283,20 @@ public void testDeletePlaylist() {
assertEquals("testPlaylistId", streamId);


result = restServiceReal.deleteBroadcast(playlist.getStreamId());
result = restServiceReal.deleteBroadcast(playlist.getStreamId(), false);
Mockito.verify(app).cancelPlaylistSchedule(playlist.getStreamId());

assertEquals(true, result.isSuccess());

//If there is no Playlist in DB.

result = restServiceReal.deleteBroadcast("testDbNullPlaylistId");
result = restServiceReal.deleteBroadcast("testDbNullPlaylistId", false);

assertEquals(false, result.isSuccess());

// If Playlist ID is null.

result = restServiceReal.deleteBroadcast(null);
result = restServiceReal.deleteBroadcast(null, false);

assertEquals(false, result.isSuccess());

Expand Down

0 comments on commit 5e7b335

Please sign in to comment.