From a421fc9645fd82aee619f6635e84e4ff9fc4160e Mon Sep 17 00:00:00 2001 From: mekya Date: Thu, 19 Dec 2024 09:37:00 +0300 Subject: [PATCH] Fix failing test scenarios --- .../antmedia/AntMediaApplicationAdapter.java | 55 +++++++++++++------ .../muxer/IAntMediaStreamHandler.java | 3 +- .../java/io/antmedia/muxer/RecordMuxer.java | 2 +- .../AntMediaApplicationAdaptorUnitTest.java | 27 +++++---- .../java/io/antmedia/test/Application.java | 4 +- 5 files changed, 58 insertions(+), 33 deletions(-) diff --git a/src/main/java/io/antmedia/AntMediaApplicationAdapter.java b/src/main/java/io/antmedia/AntMediaApplicationAdapter.java index 6a8a1d558..1f332b849 100644 --- a/src/main/java/io/antmedia/AntMediaApplicationAdapter.java +++ b/src/main/java/io/antmedia/AntMediaApplicationAdapter.java @@ -1014,30 +1014,50 @@ public static Broadcast saveUndefinedBroadcast(String streamId, String streamNam return null; } - //@Override + @Override + @Deprecated public void muxingFinished(String streamId, File File, long startTime, long duration, int resolution, String previewFilePath, String vodId) { - muxingFinished(getDataStore().get(streamId), File, startTime, duration, resolution, previewFilePath, vodId); + muxingFinished(getDataStore().get(streamId), streamId, File, startTime, duration, resolution, previewFilePath, vodId); } @Override - public void muxingFinished(@Nonnull Broadcast broadcast, File file, long startTime, long duration, int resolution, String previewFilePath, String vodId) { + public void muxingFinished(@Nonnull Broadcast broadcast, String streamId, File file, long startTime, long duration, int resolution, String previewFilePath, String vodId) { + + String listenerHookURL = null; + String streamName = file.getName(); + String description = null; + String metadata = null; + String longitude = null; + String latitude = null; + String altitude = null; + + if (broadcast != null) { + listenerHookURL = broadcast.getListenerHookURL(); + if(StringUtils.isNotBlank(broadcast.getName())){ + streamName = resolution != 0 ? broadcast.getName() + " (" + resolution + "p)" : broadcast.getName(); + } + description = broadcast.getDescription(); + metadata = broadcast.getMetaData(); + longitude = broadcast.getLongitude(); + latitude = broadcast.getLatitude(); + altitude = broadcast.getAltitude(); + } + else { + logger.error("Broadcast is null for muxing finished for stream: {} it's not supposed to happen", streamId); + } + String vodName = file.getName(); String filePath = file.getPath(); long fileSize = file.length(); long systemTime = System.currentTimeMillis(); String relativePath = getRelativePath(filePath); - String listenerHookURL = null; - String streamName = file.getName(); + - String streamId = broadcast.getStreamId(); - listenerHookURL = broadcast.getListenerHookURL(); - if(StringUtils.isNotBlank(broadcast.getName())){ - streamName = resolution != 0 ? broadcast.getName() + " (" + resolution + "p)" : broadcast.getName(); - } + logger.info("muxing finished for stream: {} with file: {}", streamId, file); @@ -1051,12 +1071,12 @@ public void muxingFinished(@Nonnull Broadcast broadcast, File file, long startTi vodId = RandomStringUtils.randomAlphanumeric(24); } - VoD newVod = new VoD(streamName, broadcast.getStreamId(), relativePath, vodName, systemTime, startTime, duration, fileSize, VoD.STREAM_VOD, vodId, previewFilePath); - newVod.setDescription(broadcast.getDescription()); - newVod.setMetadata(broadcast.getMetaData()); - newVod.setLongitude(broadcast.getLongitude()); - newVod.setLatitude(broadcast.getLatitude()); - newVod.setAltitude(broadcast.getAltitude()); + VoD newVod = new VoD(streamName, streamId, relativePath, vodName, systemTime, startTime, duration, fileSize, VoD.STREAM_VOD, vodId, previewFilePath); + newVod.setDescription(description); + newVod.setMetadata(metadata); + newVod.setLongitude(longitude); + newVod.setLatitude(latitude); + newVod.setAltitude(altitude); @@ -1072,9 +1092,8 @@ public void muxingFinished(@Nonnull Broadcast broadcast, File file, long startTi || ((index = vodName.lastIndexOf(".webm")) != -1) ) { final String baseName = vodName.substring(0, index); - final String metaData = broadcast.getMetaData(); logger.info("Setting timer for calling vod ready hook for stream:{}", streamId); - notifyHook(listenerHookURL, streamId, null, HOOK_ACTION_VOD_READY, null, null, baseName, vodId, metaData, null); + notifyHook(listenerHookURL, streamId, null, HOOK_ACTION_VOD_READY, null, null, baseName, vodId, metadata, null); } String muxerFinishScript = appSettings.getMuxerFinishScript(); diff --git a/src/main/java/io/antmedia/muxer/IAntMediaStreamHandler.java b/src/main/java/io/antmedia/muxer/IAntMediaStreamHandler.java index 43c2d1352..99a7c106d 100644 --- a/src/main/java/io/antmedia/muxer/IAntMediaStreamHandler.java +++ b/src/main/java/io/antmedia/muxer/IAntMediaStreamHandler.java @@ -58,12 +58,13 @@ public interface IAntMediaStreamHandler { * in some cases like there is already a file with that name * * @param broadcast object that muxed is finished + * @param streamId is the id of the stream * @param file video file that muxed is finished * @param duration of the video in milliseconds * @param resolution height of the video * */ - public void muxingFinished(Broadcast broadcast, File file, long startTime, long duration , int resolution, String path, String vodId); + public void muxingFinished(Broadcast broadcast, String streamId, File file, long startTime, long duration , int resolution, String path, String vodId); /** * Update stream quality, speed and number of pending packet size and update time diff --git a/src/main/java/io/antmedia/muxer/RecordMuxer.java b/src/main/java/io/antmedia/muxer/RecordMuxer.java index f466890a0..4f5dbcaf9 100644 --- a/src/main/java/io/antmedia/muxer/RecordMuxer.java +++ b/src/main/java/io/antmedia/muxer/RecordMuxer.java @@ -140,7 +140,7 @@ public synchronized void writeTrailer() { finalizeRecordFile(f); - adaptor.muxingFinished(broadcast, f, startTime, getDurationInMs(f,streamId), resolution, previewPath, vodId); + adaptor.muxingFinished(broadcast, streamId, f, startTime, getDurationInMs(f,streamId), resolution, previewPath, vodId); logger.info("File: {} exist: {}", fileTmp.getAbsolutePath(), fileTmp.exists()); diff --git a/src/test/java/io/antmedia/test/AntMediaApplicationAdaptorUnitTest.java b/src/test/java/io/antmedia/test/AntMediaApplicationAdaptorUnitTest.java index 7b386d302..41bdc1661 100644 --- a/src/test/java/io/antmedia/test/AntMediaApplicationAdaptorUnitTest.java +++ b/src/test/java/io/antmedia/test/AntMediaApplicationAdaptorUnitTest.java @@ -641,7 +641,7 @@ public void testSynchUserVoD() { } @Test - public void testMuxingFinishedWithPreview(){ + public void testMuxingFinishedWithPreview() throws Exception{ AppSettings appSettings = new AppSettings(); appSettings.setGeneratePreview(true); appSettings.setMuxerFinishScript("src/test/resources/echo.sh"); @@ -653,6 +653,9 @@ public void testMuxingFinishedWithPreview(){ DataStoreFactory dsf = Mockito.mock(DataStoreFactory.class); Mockito.when(dsf.getDataStore()).thenReturn(dataStore); adapter.setDataStoreFactory(dsf); + Broadcast broadcast = new Broadcast(); + broadcast.setStreamId("streamId"); + dataStore.save(broadcast); adapter.setVertx(vertx); @@ -662,7 +665,7 @@ public void testMuxingFinishedWithPreview(){ assertFalse(f.exists()); - adapter.muxingFinished("streamId", anyFile, 0, 100, 480, "src/test/resources/preview.png", null); + adapter.muxingFinished(broadcast, "streamId", anyFile, 0, 100, 480, "src/test/resources/preview.png", null); await().atMost(5, TimeUnit.SECONDS).until(()-> f.exists()); @@ -676,7 +679,7 @@ public void testMuxingFinishedWithPreview(){ } @Test - public void testMuxingFinished() { + public void testMuxingFinished() throws Exception { AppSettings appSettings = new AppSettings(); appSettings.setMuxerFinishScript("src/test/resources/echo.sh"); @@ -688,16 +691,19 @@ public void testMuxingFinished() { DataStoreFactory dsf = Mockito.mock(DataStoreFactory.class); Mockito.when(dsf.getDataStore()).thenReturn(dataStore); adapter.setDataStoreFactory(dsf); + + Broadcast broadcast = new Broadcast(); + broadcast.setStreamId("streamId"); + dataStore.save(broadcast); adapter.setVertx(vertx); File anyFile = new File("src/test/resources/sample_MP4_480.mp4"); { - assertFalse(f.exists()); - adapter.muxingFinished("streamId", anyFile, 0, 100, 480, null, null); + adapter.muxingFinished(broadcast, broadcast.getStreamId(), anyFile, 0, 100, 480, null, null); await().atMost(5, TimeUnit.SECONDS).until(()-> f.exists()); @@ -715,7 +721,7 @@ public void testMuxingFinished() { assertFalse(f.exists()); - adapter.muxingFinished("streamId", anyFile, 0, 100, 480, "", null); + adapter.muxingFinished(broadcast, broadcast.getStreamId(), anyFile, 0, 100, 480, "", null); await().pollDelay(3, TimeUnit.SECONDS).atMost(4, TimeUnit.SECONDS).until(()-> !f.exists()); } @@ -1121,7 +1127,6 @@ public void testNotifyHookFromMuxingFinished() { * So, no hook is posted */ - ArgumentCaptor captureUrl = ArgumentCaptor.forClass(String.class); ArgumentCaptor captureId = ArgumentCaptor.forClass(String.class); ArgumentCaptor captureMainTrackId = ArgumentCaptor.forClass(String.class); @@ -1137,7 +1142,7 @@ public void testNotifyHookFromMuxingFinished() { //call muxingFinished function - spyAdaptor.muxingFinished(streamId, anyFile, 0, 100, 480, null, null); + spyAdaptor.muxingFinished(broadcast, broadcast.getStreamId(), anyFile, 0, 100, 480, null, null); //verify that notifyHook is never called verify(spyAdaptor, never()).notifyHook(captureUrl.capture(), captureId.capture(), captureMainTrackId.capture(), captureAction.capture(), @@ -1162,7 +1167,7 @@ public void testNotifyHookFromMuxingFinished() { dataStore.updateBroadcastFields(streamId, update); //call muxingFinished function - spyAdaptor.muxingFinished(streamId, anyFile, 0, 100, 480, null, null); + spyAdaptor.muxingFinished(broadcast, broadcast.getStreamId(), anyFile, 0, 100, 480, null, null); await().atMost(10, TimeUnit.SECONDS).until(()-> { boolean called = false; @@ -1196,7 +1201,7 @@ public void testNotifyHookFromMuxingFinished() { dataStore.delete(streamId); //call muxingFinished function - spyAdaptor.muxingFinished(streamId, anyFile, 0, 100, 480, null, null); + spyAdaptor.muxingFinished("streamId", anyFile, 0, 100, 480, null, null); await().atMost(10, TimeUnit.SECONDS).until(()-> { boolean called = false; @@ -1225,7 +1230,7 @@ public void testNotifyHookFromMuxingFinished() { appSettings.setListenerHookURL("listenerHookURL"); //call muxingFinished function - spyAdaptor.muxingFinished(streamId, anyFile, 0, 100, 480, null, null); + spyAdaptor.muxingFinished(broadcast, broadcast.getStreamId(), anyFile, 0, 100, 480, null, null); await().atMost(10, TimeUnit.SECONDS).until(()-> { boolean called = false; diff --git a/src/test/java/io/antmedia/test/Application.java b/src/test/java/io/antmedia/test/Application.java index 21c760dd5..f95f3f702 100644 --- a/src/test/java/io/antmedia/test/Application.java +++ b/src/test/java/io/antmedia/test/Application.java @@ -29,8 +29,8 @@ public class Application extends AntMediaApplicationAdapter implements IAntMedia @Override - public void muxingFinished(Broadcast broadcast, File file, long startTime, long duration, int resolution, String previewPath, String vodId) { - super.muxingFinished(broadcast, file, startTime, duration, resolution, previewPath, vodId); + public void muxingFinished(Broadcast broadcast, String streamId, File file, long startTime, long duration, int resolution, String previewPath, String vodId) { + super.muxingFinished(broadcast, streamId, file, startTime, duration, resolution, previewPath, vodId); Application.id.add(broadcast.getStreamId()); Application.file.add(file); Application.duration.add(duration);