Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/302 invalid time na when using progress listener #315

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/main/java/net/bramp/ffmpeg/FFmpegUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,15 @@ public static String toTimecode(long duration, TimeUnit units) {
* format "hour:minute:second", where second can be a decimal number.
*
* @param time the timecode to parse.
* @return the number of nanoseconds.
* @return the number of nanoseconds or -1 if time is 'N/A'
*/
public static long fromTimecode(String time) {
checkNotEmpty(time, "time must not be empty string");

if (time.equals("N/A")) {
return -1;
}

Matcher m = TIME_REGEX.matcher(time);
if (!m.find()) {
throw new IllegalArgumentException("invalid time '" + time + "'");
Expand All @@ -97,6 +102,8 @@ public static long fromTimecode(String time) {
* @return the bitrate in bits per second or -1 if bitrate is 'N/A'
*/
public static long parseBitrate(String bitrate) {
checkNotEmpty(bitrate, "bitrate must not be empty string");

if ("N/A".equals(bitrate)) {
return -1;
}
Expand Down
14 changes: 11 additions & 3 deletions src/test/java/net/bramp/ffmpeg/fixtures/Progresses.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ private Progresses() {
}

public static final ImmutableList<String> allFiles =
ImmutableList.of("ffmpeg-progress-0", "ffmpeg-progress-1", "ffmpeg-progress-2");
ImmutableList.of(
"ffmpeg-progress-0",
"ffmpeg-progress-1",
"ffmpeg-progress-2");

public static final ImmutableList<String> naProgressFile = ImmutableList.of("ffmpeg-progress-na");

public static final ImmutableList<Progress> allProgresses =
ImmutableList.of(
new Progress(5, 0.0f, 800, 48, 512000000, 0, 0, 1.01f, Progress.Status.CONTINUE),
new Progress(118, 23.4f, -1, -1, 5034667000L, 0, 0, -1, Progress.Status.CONTINUE),
new Progress(
132, 23.1f, 1935500, 1285168, 5312000000L, 0, 0, 0.929f, Progress.Status.END));
new Progress(132, 23.1f, 1935500, 1285168, 5312000000L, 0, 0, 0.929f, Progress.Status.END));

public static final ImmutableList<Progress> naProgresses = ImmutableList.of(
new Progress(0, 0.0f, -1, -1, -1, 0, 0, -1, Progress.Status.END)
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import net.bramp.ffmpeg.fixtures.Progresses;
import org.junit.Test;

Expand All @@ -23,6 +22,18 @@ public void testNormal() throws IOException {
InputStream inputStream = combineResource(Progresses.allFiles);
parser.processStream(inputStream);

assertThat(listener.progesses, equalTo((List<Progress>) Progresses.allProgresses));
assertThat(listener.progesses, equalTo(Progresses.allProgresses));
}

@Test
public void testNaProgressPackets() throws IOException {
listener.reset();

StreamProgressParser parser = new StreamProgressParser(listener);

InputStream inputStream = combineResource(Progresses.naProgressFile);
parser.processStream(inputStream);

assertThat(listener.progesses, equalTo(Progresses.naProgresses));
}
}
30 changes: 27 additions & 3 deletions src/test/java/net/bramp/ffmpeg/progress/TcpProgressParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.io.OutputStream;
import java.net.Socket;
import java.net.URISyntaxException;
import java.util.List;
import net.bramp.ffmpeg.fixtures.Progresses;
import org.junit.Test;

Expand Down Expand Up @@ -44,12 +43,37 @@ public void testNormal() throws IOException, InterruptedException, URISyntaxExce
parser.stop();

assertThat(bytes, greaterThan(0L));
assertThat(progesses, equalTo((List<Progress>) Progresses.allProgresses));
assertThat(progesses, equalTo(Progresses.allProgresses));
}



@Test
public void testNaProgressPackets() throws IOException, InterruptedException, URISyntaxException {
parser.start();

Socket client = new Socket(uri.getHost(), uri.getPort());
assertTrue("Socket is connected", client.isConnected());

InputStream inputStream = combineResource(Progresses.naProgressFile);
OutputStream outputStream = client.getOutputStream();

long bytes = ByteStreams.copy(inputStream, outputStream);

// HACK, but give the TcpProgressParser thread time to actually handle the connection/data
// before the client is closed, and the parser is stopped.
Thread.sleep(100);

client.close();
parser.stop();

assertThat(bytes, greaterThan(0L));
assertThat(progesses, equalTo(Progresses.naProgresses));
}

@Test
public void testPrematureDisconnect()
throws IOException, InterruptedException, URISyntaxException {
throws IOException {
parser.start();
new Socket(uri.getHost(), uri.getPort()).close();
parser.stop();
Expand Down
29 changes: 27 additions & 2 deletions src/test/java/net/bramp/ffmpeg/progress/UdpProgressParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public ProgressParser newParser(ProgressListener listener)
}

@Test
public void testNormal() throws IOException, InterruptedException, URISyntaxException {
public void testNormal() throws IOException, InterruptedException {
parser.start();

final InetAddress addr = InetAddress.getByName(uri.getHost());
Expand All @@ -41,10 +41,35 @@ public void testNormal() throws IOException, InterruptedException, URISyntaxExce
}
}

Thread.sleep(1000); // HACK: Wait a short while to avoid closing the receiving socket

parser.stop();

assertThat(progesses, equalTo(Progresses.allProgresses));
}

@Test
public void testNaProgressPackets() throws IOException, InterruptedException, URISyntaxException {
parser.start();

final InetAddress addr = InetAddress.getByName(uri.getHost());
final int port = uri.getPort();

try (DatagramSocket socket = new DatagramSocket()) {
// Load each Progress Fixture, and send in a single datagram packet
for (String progressFixture : Progresses.naProgressFile) {
InputStream inputStream = loadResource(progressFixture);
byte[] bytes = ByteStreams.toByteArray(inputStream);

DatagramPacket packet = new DatagramPacket(bytes, bytes.length, addr, port);
socket.send(packet);
}
}

Thread.sleep(100); // HACK: Wait a short while to avoid closing the receiving socket

parser.stop();

assertThat(progesses, equalTo((List<Progress>) Progresses.allProgresses));
assertThat(progesses, equalTo(Progresses.naProgresses));
}
}
11 changes: 11 additions & 0 deletions src/test/resources/net/bramp/ffmpeg/fixtures/ffmpeg-progress-na
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
frame=0
fps=0
stream_0_0_q=-1.0
bitrate=N/A
total_size=N/A
out_time_ms=N/A
out_time=N/A
dup_frames=0
drop_frames=0
speed=N/A
progress=end
Loading