Skip to content

Commit

Permalink
修复Android4.0上无法录制的bug,详细可见bytedeco/javacpp#39
Browse files Browse the repository at this point in the history
  • Loading branch information
szitguy committed Oct 21, 2015
1 parent c0827c5 commit 55fa7e7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 33 deletions.
Expand Up @@ -55,6 +55,8 @@
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import sz.itguy.utils.SystemVersionUtil;

import static org.bytedeco.javacpp.avcodec.AVPacket;
import static org.bytedeco.javacpp.avcodec.AVPicture;
import static org.bytedeco.javacpp.avcodec.avpicture_fill;
Expand Down Expand Up @@ -229,7 +231,6 @@ void startUnsafe() throws Exception {
/* buffer video sink: to terminate the filter chain. */
ret = avfilter_graph_create_filter(buffersink_ctx = new AVFilterContext(), buffersink, "out",
null, null, filter_graph);

if (ret < 0) {
throw new Exception("avfilter_graph_create_filter(): Cannot create buffer sink.");
}
Expand Down Expand Up @@ -357,15 +358,27 @@ public Frame pull() throws Exception {
} else {
frame.imageStride = frame.imageWidth;
int size = avpicture_get_size(filt_frame.format(), frame.imageWidth, frame.imageHeight);
if (image_ptr[0] == null || image_ptr[0].capacity() < size) {
image_ptr[0] = new BytePointer(size);
image_buf[0] = image_ptr[0].asBuffer();
// Fix bug on Android4.0,check out https://github.com/bytedeco/javacpp/issues/39
if (SystemVersionUtil.hasJellyBean()) {
if (image_ptr[0] == null || image_ptr[0].capacity() < size) {
image_ptr[0] = new BytePointer(size);
image_buf[0] = image_ptr[0].asBuffer();
}
frame.image = image_buf;
frame.image[0].position(0).limit(size);
frame.imageChannels = 2;
ret = avpicture_layout(new AVPicture(filt_frame), filt_frame.format(),
frame.imageWidth, frame.imageHeight, image_ptr[0].position(0), image_ptr[0].capacity());
} else {
if (image_buf[0] == null || image_buf[0].capacity() < size) {
image_buf[0] = ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder());
}
frame.image = image_buf;
frame.image[0].position(0).limit(size);
frame.imageChannels = 2;
ret = avpicture_layout(new AVPicture(filt_frame), filt_frame.format(),
frame.imageWidth, frame.imageHeight, (ByteBuffer) frame.image[0].position(0), frame.image[0].capacity());
}
frame.image = image_buf;
frame.image[0].position(0).limit(size);
frame.imageChannels = 2;
ret = avpicture_layout(new AVPicture(filt_frame), filt_frame.format(),
frame.imageWidth, frame.imageHeight, image_ptr[0].position(0), image_ptr[0].capacity());
}
return frame;
}
Expand Down
48 changes: 24 additions & 24 deletions WXLikeVideoRecorderLib/src/main/java/org/bytedeco/javacv/Frame.java
Expand Up @@ -132,37 +132,37 @@ public <I extends Indexer> I createIndexer(boolean direct, int i) {
Object array = buffer.hasArray() ? buffer.array() : null;
switch (imageDepth) {
case DEPTH_UBYTE:
return array != null ? (I) UByteIndexer.create((byte[]) array, sizes, strides)
: direct ? (I) UByteIndexer.create((ByteBuffer) buffer, sizes, strides)
: (I) UByteIndexer.create(new BytePointer((ByteBuffer) buffer), sizes, strides, false);
return array != null ? (I)UByteIndexer.create((byte[])array, sizes, strides)
: direct ? (I)UByteIndexer.create((ByteBuffer)buffer, sizes, strides)
: (I)UByteIndexer.create(new BytePointer((ByteBuffer)buffer), sizes, strides, false);
case DEPTH_BYTE:
return array != null ? (I) ByteIndexer.create((byte[]) array, sizes, strides)
: direct ? (I) ByteIndexer.create((ByteBuffer) buffer, sizes, strides)
: (I) ByteIndexer.create(new BytePointer((ByteBuffer) buffer), sizes, strides, false);
return array != null ? (I)ByteIndexer.create((byte[])array, sizes, strides)
: direct ? (I)ByteIndexer.create((ByteBuffer)buffer, sizes, strides)
: (I)ByteIndexer.create(new BytePointer((ByteBuffer)buffer), sizes, strides, false);
case DEPTH_USHORT:
return array != null ? (I) UShortIndexer.create((short[]) array, sizes, strides)
: direct ? (I) UShortIndexer.create((ShortBuffer) buffer, sizes, strides)
: (I) UShortIndexer.create(new ShortPointer((ShortBuffer) buffer), sizes, strides, false);
return array != null ? (I)UShortIndexer.create((short[])array, sizes, strides)
: direct ? (I)UShortIndexer.create((ShortBuffer)buffer, sizes, strides)
: (I)UShortIndexer.create(new ShortPointer((ShortBuffer)buffer), sizes, strides, false);
case DEPTH_SHORT:
return array != null ? (I) ShortIndexer.create((short[]) array, sizes, strides)
: direct ? (I) ShortIndexer.create((ShortBuffer) buffer, sizes, strides)
: (I) ShortIndexer.create(new ShortPointer((ShortBuffer) buffer), sizes, strides, false);
return array != null ? (I)ShortIndexer.create((short[])array, sizes, strides)
: direct ? (I)ShortIndexer.create((ShortBuffer)buffer, sizes, strides)
: (I)ShortIndexer.create(new ShortPointer((ShortBuffer)buffer), sizes, strides, false);
case DEPTH_INT:
return array != null ? (I) IntIndexer.create((int[]) array, sizes, strides)
: direct ? (I) IntIndexer.create((IntBuffer) buffer, sizes, strides)
: (I) IntIndexer.create(new IntPointer((IntBuffer) buffer), sizes, strides, false);
return array != null ? (I)IntIndexer.create((int[])array, sizes, strides)
: direct ? (I)IntIndexer.create((IntBuffer)buffer, sizes, strides)
: (I)IntIndexer.create(new IntPointer((IntBuffer)buffer), sizes, strides, false);
case DEPTH_LONG:
return array != null ? (I) LongIndexer.create((long[]) array, sizes, strides)
: direct ? (I) LongIndexer.create((LongBuffer) buffer, sizes, strides)
: (I) LongIndexer.create(new LongPointer((LongBuffer) buffer), sizes, strides, false);
return array != null ? (I)LongIndexer.create((long[])array, sizes, strides)
: direct ? (I)LongIndexer.create((LongBuffer)buffer, sizes, strides)
: (I)LongIndexer.create(new LongPointer((LongBuffer)buffer), sizes, strides, false);
case DEPTH_FLOAT:
return array != null ? (I) FloatIndexer.create((float[]) array, sizes, strides)
: direct ? (I) FloatIndexer.create((FloatBuffer) buffer, sizes, strides)
: (I) FloatIndexer.create(new FloatPointer((FloatBuffer) buffer), sizes, strides, false);
return array != null ? (I)FloatIndexer.create((float[])array, sizes, strides)
: direct ? (I)FloatIndexer.create((FloatBuffer)buffer, sizes, strides)
: (I)FloatIndexer.create(new FloatPointer((FloatBuffer)buffer), sizes, strides, false);
case DEPTH_DOUBLE:
return array != null ? (I) DoubleIndexer.create((double[]) array, sizes, strides)
: direct ? (I) DoubleIndexer.create((DoubleBuffer) buffer, sizes, strides)
: (I) DoubleIndexer.create(new DoublePointer((DoubleBuffer) buffer), sizes, strides, false);
return array != null ? (I)DoubleIndexer.create((double[])array, sizes, strides)
: direct ? (I)DoubleIndexer.create((DoubleBuffer)buffer, sizes, strides)
: (I)DoubleIndexer.create(new DoublePointer((DoubleBuffer)buffer), sizes, strides, false);
default: assert false;
}
return null;
Expand Down

0 comments on commit 55fa7e7

Please sign in to comment.