Skip to content

Commit 9477c7c

Browse files
authored
Merge pull request #245 from kokorin/develop
Fix ChannelInput issue.
2 parents 172706c + 519752d commit 9477c7c

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

src/main/java/com/github/kokorin/jaffree/net/FtpServer.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ private void doPasv(final OutputStream output, final ServerSocket dataServerSock
268268
int portLow = port & 0xFF;
269269
println(output, "227 Entering Passive Mode (" + address + ","
270270
+ portHi + "," + portLow + ").");
271+
//After entering passive mode FTP server should retrieve a file from the first byte.
272+
channel.position(0);
271273
}
272274

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

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

314-
long copied = 0;
315316
try (Socket dataSocket = dataServerSocket.accept();
316317
InputStream dataInput = dataSocket.getInputStream()) {
317-
LOGGER.debug("Data connection established: {}", dataSocket);
318-
copied = IOUtil.copy(dataInput, Channels.newOutputStream(channel), buffer);
318+
LOGGER.debug("Data connection established, position: {} socket: {}", channel.position(),
319+
dataSocket);
320+
long copied = IOUtil.copy(dataInput, Channels.newOutputStream(channel), buffer);
319321
LOGGER.debug("Copied {} bytes from data socket", copied);
320322
println(output, "226 Operation successful");
321323
} catch (SocketException e) {

src/main/java/com/github/kokorin/jaffree/util/IOUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static long copy(final InputStream input, final OutputStream output,
6262
public static long copy(final InputStream input, final OutputStream output, final byte[] buffer)
6363
throws IOException {
6464
if (buffer.length == 0) {
65-
throw new IllegalArgumentException("Buffer must be not empty");
65+
throw new IllegalArgumentException("Buffer must be not empty");
6666
}
6767

6868
long count = 0;

src/test/java/com/github/kokorin/jaffree/Artifacts.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class Artifacts {
1818
public static final Path VIDEO_MP4 = getMp4Artifact(180);
1919
public static final Path VIDEO_MKV = getMkvArtifact(180);
2020
public static final Path VIDEO_FLV = getFlvArtifact(180);
21+
public static final Path VIDEO_TS = getTsArtifact(180);
2122
public static final Path SMALL_FLV = getFlvArtifact(20);
2223
public static final Path SMALL_MP4 = getMp4Artifact(20);
2324
public static final Path VIDEO_WITH_PROGRAMS = getTsArtifactWithPrograms();
@@ -36,6 +37,10 @@ private static Path getFlvArtifact(int duration) {
3637
return getArtifact("640x480", 30, 44_100, "flv", duration);
3738
}
3839

40+
private static Path getTsArtifact(int duration) {
41+
return getArtifact("640x480", 30, 44_100, "ts", duration);
42+
}
43+
3944
private static synchronized Path getNutArtifact(int duration) {
4045
Path source = getMp4Artifact(duration);
4146
String filename = source.getFileName().toString().replace(".mp4", ".nut");

src/test/java/com/github/kokorin/jaffree/ffmpeg/FFmpegTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.nio.file.Files;
2828
import java.nio.file.Path;
2929
import java.nio.file.Paths;
30+
import java.nio.file.StandardOpenOption;
3031
import java.util.List;
3132
import java.util.concurrent.CountDownLatch;
3233
import java.util.concurrent.TimeUnit;
@@ -714,6 +715,32 @@ public void testChannelInputSeek() throws IOException {
714715
assertTrue(Files.size(outputPath) > 1000);
715716
}
716717

718+
@Test
719+
public void testChannelWithNonSeekableInput() throws IOException {
720+
Path inputTs = Artifacts.VIDEO_TS;
721+
Path tempDir = Files.createTempDirectory("jaffree");
722+
Path outputPng = tempDir.resolve("output1.png");
723+
try (
724+
SeekableByteChannel inputChannel = Files.newByteChannel(inputTs,
725+
StandardOpenOption.READ)
726+
) {
727+
FFmpeg.atPath()
728+
.addInput(ChannelInput.fromChannel(inputChannel).setPosition(2000L))
729+
.setOverwriteOutput(true)
730+
.addOutput(
731+
UrlOutput.toPath(outputPng)
732+
.setFormat("image2")
733+
.setFrameCount(StreamType.VIDEO, 1L)
734+
.addArguments("-q:v", "1")
735+
.disableStream(StreamType.AUDIO)
736+
.disableStream(StreamType.SUBTITLE)
737+
)
738+
.execute();
739+
}
740+
741+
assertTrue(Files.size(outputPng) > 1000);
742+
}
743+
717744
@Test
718745
public void testChannelOutput() throws IOException {
719746
Path tempDir = Files.createTempDirectory("jaffree");

0 commit comments

Comments
 (0)