Skip to content

Commit

Permalink
Merge pull request kokorin#245 from kokorin/develop
Browse files Browse the repository at this point in the history
Fix ChannelInput issue.
  • Loading branch information
kokorin authored Nov 6, 2021
2 parents 172706c + 519752d commit 9477c7c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
16 changes: 9 additions & 7 deletions src/main/java/com/github/kokorin/jaffree/net/FtpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ private void doPasv(final OutputStream output, final ServerSocket dataServerSock
int portLow = port & 0xFF;
println(output, "227 Entering Passive Mode (" + address + ","
+ portHi + "," + portLow + ").");
//After entering passive mode FTP server should retrieve a file from the first byte.
channel.position(0);
}

/**
Expand All @@ -281,13 +283,13 @@ private void doRetr(final OutputStream output, final ServerSocket dataServerSock
throws IOException {
println(output, "150 File status okay; about to open data connection.");

long copied = 0;
try (Socket dataSocket = dataServerSocket.accept();
OutputStream dataOutput = dataSocket.getOutputStream()) {
LOGGER.debug("Data connection established: {}", dataSocket);
copied = IOUtil.copy(Channels.newInputStream(channel), dataOutput, buffer);
dataOutput.flush();
LOGGER.debug("Data connection established, position: {} socket: {}", channel.position(),
dataSocket);
long copied = IOUtil.copy(Channels.newInputStream(channel), dataOutput, buffer);
LOGGER.debug("Copied {} bytes to data socket", copied);
dataOutput.flush();
println(output, "226 Operation successful");
} catch (SocketException e) {
// ffmpeg can close data connection without fully reading requested data.
Expand All @@ -311,11 +313,11 @@ private void doStor(final OutputStream output, final ServerSocket dataServerSock
final String path) throws IOException {
println(output, "150 File status okay; about to open data connection.");

long copied = 0;
try (Socket dataSocket = dataServerSocket.accept();
InputStream dataInput = dataSocket.getInputStream()) {
LOGGER.debug("Data connection established: {}", dataSocket);
copied = IOUtil.copy(dataInput, Channels.newOutputStream(channel), buffer);
LOGGER.debug("Data connection established, position: {} socket: {}", channel.position(),
dataSocket);
long copied = IOUtil.copy(dataInput, Channels.newOutputStream(channel), buffer);
LOGGER.debug("Copied {} bytes from data socket", copied);
println(output, "226 Operation successful");
} catch (SocketException e) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/github/kokorin/jaffree/util/IOUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static long copy(final InputStream input, final OutputStream output,
public static long copy(final InputStream input, final OutputStream output, final byte[] buffer)
throws IOException {
if (buffer.length == 0) {
throw new IllegalArgumentException("Buffer must be not empty");
throw new IllegalArgumentException("Buffer must be not empty");
}

long count = 0;
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/com/github/kokorin/jaffree/Artifacts.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Artifacts {
public static final Path VIDEO_MP4 = getMp4Artifact(180);
public static final Path VIDEO_MKV = getMkvArtifact(180);
public static final Path VIDEO_FLV = getFlvArtifact(180);
public static final Path VIDEO_TS = getTsArtifact(180);
public static final Path SMALL_FLV = getFlvArtifact(20);
public static final Path SMALL_MP4 = getMp4Artifact(20);
public static final Path VIDEO_WITH_PROGRAMS = getTsArtifactWithPrograms();
Expand All @@ -36,6 +37,10 @@ private static Path getFlvArtifact(int duration) {
return getArtifact("640x480", 30, 44_100, "flv", duration);
}

private static Path getTsArtifact(int duration) {
return getArtifact("640x480", 30, 44_100, "ts", duration);
}

private static synchronized Path getNutArtifact(int duration) {
Path source = getMp4Artifact(duration);
String filename = source.getFileName().toString().replace(".mp4", ".nut");
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/com/github/kokorin/jaffree/ffmpeg/FFmpegTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -714,6 +715,32 @@ public void testChannelInputSeek() throws IOException {
assertTrue(Files.size(outputPath) > 1000);
}

@Test
public void testChannelWithNonSeekableInput() throws IOException {
Path inputTs = Artifacts.VIDEO_TS;
Path tempDir = Files.createTempDirectory("jaffree");
Path outputPng = tempDir.resolve("output1.png");
try (
SeekableByteChannel inputChannel = Files.newByteChannel(inputTs,
StandardOpenOption.READ)
) {
FFmpeg.atPath()
.addInput(ChannelInput.fromChannel(inputChannel).setPosition(2000L))
.setOverwriteOutput(true)
.addOutput(
UrlOutput.toPath(outputPng)
.setFormat("image2")
.setFrameCount(StreamType.VIDEO, 1L)
.addArguments("-q:v", "1")
.disableStream(StreamType.AUDIO)
.disableStream(StreamType.SUBTITLE)
)
.execute();
}

assertTrue(Files.size(outputPng) > 1000);
}

@Test
public void testChannelOutput() throws IOException {
Path tempDir = Files.createTempDirectory("jaffree");
Expand Down

0 comments on commit 9477c7c

Please sign in to comment.