Skip to content

Commit

Permalink
Write test code for recording missing packets
Browse files Browse the repository at this point in the history
  • Loading branch information
mekya committed Apr 17, 2021
1 parent 458e570 commit a265f2e
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,13 @@ protected void finished(Description description) {
private int numberOfClientsInHLSPlay;

private static int OS_TYPE;
private static String ffmpegPath = "ffmpeg";
public static String ffmpegPath = "ffmpeg";
public static String ffprobePath = "ffprobe";
static {
String osName = System.getProperty("os.name", "").toLowerCase();
if (osName.startsWith("mac os x") || osName.startsWith("darwin")) {
OS_TYPE = MAC_OS_X;
ffprobePath = "/usr/local/bin/ffprobe";
} else if (osName.startsWith("windows")) {
OS_TYPE = WINDOWS;
} else if (osName.startsWith("linux")) {
Expand Down
10 changes: 9 additions & 1 deletion src/test/java/io/antmedia/test/MuxerUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import io.antmedia.muxer.RtmpMuxer;
import io.antmedia.muxer.WebMMuxer;
import io.antmedia.social.endpoint.VideoServiceEndpoint;
import io.antmedia.test.utils.VideoInfo;
import io.antmedia.test.utils.VideoProber;
import io.vertx.core.Vertx;

import static org.bytedeco.ffmpeg.global.avcodec.*;
Expand Down Expand Up @@ -183,11 +185,13 @@ public void before() {
@After
public void after() {

/*
try {
AppFunctionalV2Test.delete(new File("webapps"));
} catch (IOException e) {
e.printStackTrace();
}
*/

//reset values in the bean
getAppSettings().resetDefaults();
Expand Down Expand Up @@ -1429,7 +1433,11 @@ else if (!firstVideoPacketReceived && streamPacket.getDataType() == Constants.TY

@Test
public void testMp4Muxing() {
testMp4Muxing("lkdlfkdlfkdlfk");
File mp4File = testMp4Muxing("lkdlfkdlfkdlfk");

VideoInfo fileInfo = VideoProber.getFileInfo(mp4File.getAbsolutePath());
assertEquals(252, fileInfo.videoPacketsCount);
assertEquals(431, fileInfo.audioPacketsCount);
}


Expand Down
32 changes: 32 additions & 0 deletions src/test/java/io/antmedia/test/utils/VideoInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.antmedia.test.utils;

import java.io.File;

public class VideoInfo{
public String path;
public boolean isExist;
public String videoWidth;
public String videoHeight;
public String videoCodec;
public String videoFps;
public String videoDuration;
public String videoBitrate;
public String audioCodec;
public String audioBitrate;
public String audioDuration;
public long videoDurationMS;
public long audioDurationMS;
public int videoPacketsCount;
public int audioPacketsCount;
public long audioStartTimeMs;

@Override
public String toString() {
return "file path: " + path + " video width: " + videoWidth + " video height: " + videoHeight
+ " video codec: " + videoCodec + " video fps: " + videoFps + " video duration: " + videoDuration
+ " video bitrate: " + videoBitrate + " audio codec: " + audioCodec + " audio bitrate: " + audioBitrate
+ " audio start time ms: " + audioStartTimeMs;
}


}
82 changes: 82 additions & 0 deletions src/test/java/io/antmedia/test/utils/VideoProber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package io.antmedia.test.utils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.IOUtils;

import io.antmedia.integration.AppFunctionalV2Test;


public class VideoProber {
public static final String WIDTH = "width";
public static final String HEIGHT = "height";
public static final String BITRATE = "bitrate";
public static final String CODEC = "codec_name";
public static final String DURATION = "duration";
public static final String FPS = "r_frame_rate";

public static final char VIDEO = 'v';
public static final char AUDIO = 'a';


public static VideoInfo getFileInfo(String path) {
VideoInfo info = new VideoInfo();
info.isExist = new File(path).exists();
info.path = path;
info.videoCodec = getProperty(path, VIDEO, CODEC);
info.videoWidth = getProperty(path, VIDEO, WIDTH);
info.videoHeight = getProperty(path, VIDEO, HEIGHT);
info.videoFps = getProperty(path, VIDEO, FPS);
info.videoBitrate = getProperty(path, VIDEO, BITRATE);
info.videoDuration = run(AppFunctionalV2Test.ffprobePath + " "+path+" -show_streams -select_streams v 2>&1 | sed -n 's/TAG:DURATION=//p'").trim();
info.audioDuration = run(AppFunctionalV2Test.ffprobePath + " "+path+" -show_streams -select_streams a 2>&1 | sed -n 's/TAG:DURATION=//p'").trim();
String videoPacketCountCommand = AppFunctionalV2Test.ffprobePath + " "+path+" -show_packets 2>&1 | grep codec_type=video | wc -l";
String videoPacketCount = run(videoPacketCountCommand).trim();
System.out.println("video packet count: " + videoPacketCountCommand);
info.videoPacketsCount = Integer.parseInt(videoPacketCount);
info.audioPacketsCount = Integer.parseInt(run(AppFunctionalV2Test.ffprobePath + " "+path+" -show_packets 2>&1 | grep codec_type=audio | wc -l").trim());
try {
info.audioStartTimeMs = (long)(Float.parseFloat(run(AppFunctionalV2Test.ffprobePath + " "+path+" -show_streams -select_streams a 2>&1 | sed -n 's/start_time=//p'").trim()) * 1000);
}
catch (Exception e) {
e.printStackTrace();
}
info.audioCodec = getProperty(path, AUDIO, CODEC);
info.audioBitrate = getProperty(path, AUDIO, BITRATE);
info.videoDurationMS = toMsDuration(info.videoDuration);
info.audioDurationMS = toMsDuration(info.audioDuration.replace(",", "."));
return info;
}

private static long toMsDuration(String strDuration) {
if(strDuration.isEmpty()) {
return 0;
}
String[] tokens = strDuration.split(":");
long hours = Long.parseLong(tokens[0])*3600*1000;
long minutes = Long.parseLong(tokens[1])*60*1000;
long seconds = (long) (Double.parseDouble(tokens[2])*1000);

return hours+minutes+seconds;
}

public static String getProperty(String path, char type, String property) {
String command = AppFunctionalV2Test.ffprobePath + " -v error -select_streams "+type+":0 -show_entries stream="+property+" -of csv=s=x:p=0 "+path;
return run(command).trim();
}

public static String run(String command) {
ProcessBuilder pb = new ProcessBuilder("/bin/sh", "-c", command);
try {
return IOUtils.toString(pb.start().getInputStream(), StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
return e.getMessage();
}
}



}

0 comments on commit a265f2e

Please sign in to comment.