我试图打开一个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在写入容器时使用的相同的自定义缓冲区正在成功工作。请有人帮助我理解自定义缓冲区实现中缺少的内容,比如在读取模式中使用它,以及它失败的原因?
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;
}
}发布于 2014-08-13 21:55:48
我通过重新实现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。不过,仍有清理该类/实现的余地。但现在已经解决了。
发布于 2014-07-25 17:08:39
我打开了一个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(数据包)。
https://stackoverflow.com/questions/24921649
复制相似问题