使用自定义ByteChannel进行读取时,IContainer.open()失败

时间:2014-07-23 21:48:19

标签: ffmpeg xuggler xuggle

我正在尝试打开一个IContainer对象,该对象从自定义输入缓冲区读取而不是从媒体文件中读取。此自定义输入缓冲区的实现如下所示。

创建和打开容器的代码如下所示。

// Open up the container for READING
mInputCStore = new CStore();

IContainerFormat format = IContainerFormat.make();
if (format.setInputFormat("flv") < 0) {
    throw new IllegalArgumentException("Failed to initialize the input format");
}

// Open up the container
mInputContainer = IContainer.make();
int retval = mInputContainer.open(mPlaybackContainerStore, IContainer.Type.READ, format);       
if (retval < 0) {
    // This little trick converts the non friendly integer return value into  
    // a slightly more friendly object to get a human-readable error name
    IError error = IError.make(retval);
    throw new IllegalArgumentException("could not open input container: " + mPlaybackContainerStore + "; Error: " + error.getDescription());
}

上面的代码抛出了一个例外 -

Exception in thread "main" java.lang.IllegalArgumentException: could not open input container: com.client.video.ContainerStore@61981853; Error: Operation not permitted

在写入容器时使用相同的自定义缓冲区正在成功运行。有人请帮助我理解自定义缓冲区实现中缺少什么,只要在READ模式下使用它以及它失败的原因是什么?

package test;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;
import java.util.concurrent.ConcurrentLinkedQueue;

public class CStore implements ByteChannel {
    private ConcurrentLinkedQueue<DataChunk> mChunkQueue = null;
    private int mQueueSize = 0;

    // constructor
    public CStore(String type) {
        mQueueSize = 0;
        mChunkQueue = new ConcurrentLinkedQueue<DataChunk>();
        mChunkQueue.clear();
    }

    @Override
    public void close() throws IOException {
        return;
    }

    @Override
    public boolean isOpen() {
        return false;
    }

    @Override
    public int write(ByteBuffer buffer) throws IOException {
        DataChunk chunk = new DataChunk(buffer);
        mChunkQueue.add(chunk);
        mQueueSize += chunk.getLength();
        return 0;
    }

    public int read(ByteBuffer buffer) throws IOException {

        int result = 0;

        DataChunk chunk = mChunkQueue.poll();
        if (chunk != null) {
            buffer = chunk.getBuffer();
            if (buffer != null) {
                result = 0;
            } else {
                result = 1;
            }
        }

        return result;
    }
}

2 个答案:

答案 0 :(得分:0)

我已经打开了一个IContainer来读取InputStream的自定义实现。为此,您必须手动设置从文件读取时通常自动检测到的信息(即:输入格式,编解码器,编解码器详细信息)。

// Example input stream (raw audio encoded with mulaw).
InputStream input = new FileInputStream("/tmp/test.ul"); 

// Manually set the input format.
IContainerFormat inputFormat = IContainerFormat.make();
inputFormat.setInputFormat("mulaw");

// Open the container.
IContainer container = IContainer.make();
container.open(input, inputFormat);

// Initialize the decoder.
IStreamCoder coder = container.getStream(0).getStreamCoder();
coder.setSampleRate(8000);
coder.setChannels(1);
coder.open(null, null);

您现在可以像往常一样从容器中读取[即:container.readNextPacket(packet)]。

答案 1 :(得分:0)

我通过重新实现read()方法解决了这个问题,如下所示。

public int read(ByteBuffer buffer) {
    int bytesRead = 0;

    if (buffer == null)
        return 0;

    while (buffer.hasRemaining()) {
        byte b = mBuff.get();
        buffer.put(b);
        bytesRead++;
    }

    return bytesRead;
}

请注意,在调用此函数之前,我将mChunkQueue转换为ByteBuffer mBuff。尽管如此,仍然可以清理此类/实现。但它现在已经解决了。