JCodec Sequence Encoder throwing ClosedByInterruptException when adding a native Picture

146 views Asked by At

I'm trying to use the Sequence Encoder to get a bunch of ByteBuffers to a Video. I am instantiating the Encoder like this:

new SequenceEncoder(NIOUtils.writableFileChannel(movie.getAbsolutePath()), Rational.R(60, 1), Format.MOV, Codec.H264, null);

Then i add the Pictures on a different Thread:

public static void saveScreenshot(ByteBuffer buffer) throws IOException {
    Picture pic = Picture.create(width, height, ColorSpace.RGB);
    
    byte[] dstData = pic.getPlaneData(0);
    
    int j = 0;
    for (int y = (height - 1); y > 0; y--) {
        for (int x = 0; x < width; x++) {
            int i = (x + (width * y)) * 3;
            
            dstData[j++] = 
                    (byte) (buffer.get(i) - 128);
            dstData[j++] = 
                    (byte) (buffer.get(i + 1) - 128);
            dstData[j++] = 
                    (byte) (buffer.get(i + 2) - 128);
            
        }
    }
    
    synchronized (ScreenshotQueue.encoder) {
        ScreenshotQueue.encoder.encodeNativeFrame(pic);
    }
    
}

Then, once again on another Thread i finish the File:

encoder.finish();

This code works! My Problem is, that it works once. I have no idea why, but when i run this code again, it throws the ClosedByInterruptException when I add a Picture.

My first idea was to close the FileStream after the first recording manually so I made a FileOutputStream and used FileOutputStream#getChannel() to get the Channel. And after the finish() i close them manually. But that didn't work!

I also tried different Codecs/Formats. When I use VP8 it always works. The Problem is that VP8 doesn't work so I cannot use that one.

1

There are 1 answers

0
MCPfannkuchen On

Okay. I found the problem.

I looked at the JavaDocs of SE and turns out the Exception wasn't an multi-access stuff exception, but a Thread-Lock problem. Putting everything into 1 Thread seemed to fix it.