Skip to content

Commit 21b1bfd

Browse files
committed
Test vlcj direct rendering, cont
1 parent 76cdc99 commit 21b1bfd

File tree

1 file changed

+61
-111
lines changed

1 file changed

+61
-111
lines changed
Lines changed: 61 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
import java.awt.Transparency;
42
import java.awt.color.ColorSpace;
53
import java.awt.image.BufferedImage;
@@ -11,7 +9,6 @@
119
import java.awt.image.WritableRaster;
1210
import java.io.File;
1311
import java.io.IOException;
14-
import java.nio.ByteBuffer;
1512

1613
import javax.imageio.ImageIO;
1714

@@ -27,154 +24,107 @@
2724

2825
public class VlcjDirectTest {
2926

30-
private static class TestBufferFormatCallback implements BufferFormatCallback {
27+
private static class DirectBufferFormatCallback implements BufferFormatCallback {
3128

3229
@Override
3330
public BufferFormat getBufferFormat(int sourceWidth, int sourceHeight) {
3431
return new RV32BufferFormat(sourceWidth, sourceHeight);
3532
}
3633
}
3734

38-
/**
39-
* RGB offsets.
40-
*/
41-
private static final int[] BAND_OFFSETS = new int[] { 2, 1, 0 };
42-
/**
43-
* Number of bytes in each pixel.
44-
*/
45-
private static final int[] BITS = { 8, 8, 8 };
46-
47-
/**
48-
* Image color space.
49-
*/
50-
private static final ColorSpace COLOR_SPACE = ColorSpace.getInstance(ColorSpace.CS_sRGB);
51-
52-
/**
53-
* Data type used in image.
54-
*/
55-
private static final int DATA_TYPE = DataBuffer.TYPE_BYTE;
56-
5735
private final static String[] VLC_ARGS = {
58-
59-
// VLC args by Andrew Davison:
60-
// http://fivedots.coe.psu.ac.th/~ad/jg/nui025/snapsWithoutJMF.pdf
61-
62-
// no interface
6336
"--intf", "dummy",
64-
65-
// no video output
6637
"--vout", "dummy",
67-
68-
// no audio decoding
6938
"--no-audio",
70-
71-
// do not display title
7239
"--no-video-title-show",
73-
74-
// no stats
7540
"--no-stats",
76-
77-
// no subtitles
7841
"--no-sub-autodetect-file",
79-
80-
// no snapshot previews
8142
"--no-snapshot-preview",
82-
83-
// reduce capture lag/latency
8443
"--live-caching=50",
85-
86-
// turn off warnings
8744
"--quiet",
8845
};
8946

90-
public static void main(String[] args) throws IOException, InterruptedException {
91-
92-
MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(VLC_ARGS);
93-
94-
DirectMediaPlayer mediaPlayer = mediaPlayerFactory.newDirectMediaPlayer(
95-
new TestBufferFormatCallback(),
96-
new RenderCallback() {
97-
98-
int i = 0;
99-
100-
@Override
101-
public void display(DirectMediaPlayer player, Memory[] buffers, BufferFormat format) {
47+
private static BufferedImage convert(Memory[] buffers, BufferFormat format) {
10248

103-
final int width = format.getWidth();
104-
final int height = format.getHeight();
105-
106-
System.out.println(width + " x " + height);
107-
108-
ComponentSampleModel smodel = new ComponentSampleModel(
109-
DataBuffer.TYPE_BYTE,
110-
width,
111-
height,
112-
4, // pixel stride
113-
width * 4, // scanline stride
114-
BAND_OFFSETS);
115-
116-
ComponentColorModel cmodel = new ComponentColorModel(
117-
COLOR_SPACE, BITS, false, false, Transparency.BITMASK, DATA_TYPE);
118-
119-
if (buffers.length == 0) {
120-
System.err.println("No memory elements found!");
121-
return;
122-
}
123-
124-
Memory memory = buffers[0];
49+
if (buffers.length == 0) {
50+
throw new RuntimeException("No memory elements found!");
51+
}
12552

126-
if (memory == null) {
127-
System.err.println("Null memory!");
128-
return;
129-
}
53+
final Memory memory = buffers[0];
54+
if (memory == null) {
55+
throw new RuntimeException("Null memory!");
56+
}
13057

131-
System.out.println("meme " + memory.size());
58+
final int width = format.getWidth();
59+
final int height = format.getHeight();
60+
final int dataType = DataBuffer.TYPE_BYTE;
61+
final int pixelStride = 4;
62+
final int scanlineStride = width * pixelStride;
63+
final int[] bgrBandOffsets = new int[] { 2, 1, 0 };
64+
final int[] bits = { 8, 8, 8 };
65+
final int[] offsets = new int[] { 0 };
66+
final int transparency = Transparency.OPAQUE;
67+
68+
final byte[] bytes = new byte[scanlineStride * height];
69+
final byte[][] data = new byte[][] { bytes };
70+
71+
memory
72+
.getByteBuffer(0, memory.size())
73+
.get(bytes);
74+
75+
ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
76+
ComponentSampleModel sampleModel = new ComponentSampleModel(dataType, width, height, pixelStride, scanlineStride, bgrBandOffsets);
77+
ComponentColorModel colorModel = new ComponentColorModel(colorSpace, bits, false, false, transparency, dataType);
78+
DataBufferByte dataBuffer = new DataBufferByte(data, bytes.length, offsets);
79+
WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuffer, null);
80+
BufferedImage image = new BufferedImage(colorModel, raster, false, null);
81+
82+
image.flush();
83+
84+
return image;
85+
}
13286

133-
ByteBuffer buffer = memory.getByteBuffer(0, memory.size());
87+
private static MediaPlayerFactory factory = new MediaPlayerFactory(VLC_ARGS);
88+
private static DirectMediaPlayer player;
13489

135-
byte[] bytes = new byte[width * height * 4];
136-
byte[][] data = new byte[][] { bytes };
90+
public static void main(String[] args) throws IOException, InterruptedException {
13791

138-
buffer.get(bytes);
92+
player = factory.newDirectMediaPlayer(
93+
new DirectBufferFormatCallback(),
94+
new RenderCallback() {
13995

140-
DataBufferByte dbuf = new DataBufferByte(data, bytes.length, new int[] { 0 });
141-
WritableRaster raster = Raster.createWritableRaster(smodel, dbuf, null);
96+
int i = 0;
14297

143-
BufferedImage bi = new BufferedImage(cmodel, raster, false, null);
144-
bi.flush();
98+
@Override
99+
public void display(DirectMediaPlayer player, Memory[] buffers, BufferFormat format) {
145100

101+
BufferedImage bi = convert(buffers, format);
146102
try {
147-
ImageIO.write(bi, "JPG", new File((i++) + "-test.jpg"));
103+
ImageIO.write(bi, "JPG", new File(System.currentTimeMillis() + "-test.jpg"));
148104
} catch (IOException e) {
149-
// TODO Auto-generated catch block
150105
e.printStackTrace();
151106
}
152107

153-
System.out.println("display " + i);
108+
System.out.println("write " + i);
109+
110+
if (i++ > 10) {
111+
player.stop();
112+
System.exit(0);
113+
}
154114
}
155115
});
156116

157-
// Options setup.
158-
String mrl = "v4l2:///dev/video0"; // Linux
159-
117+
String device = "/dev/video0";
118+
String mrl = "v4l2://" + device;
160119
String[] options = new String[] {
161-
":v4l-vdev=/dev/video0",
162-
":v4l-width=640",
163-
":v4l-height=480",
120+
":v4l-vdev=" + device,
121+
":v4l-width=320", // XXX this setting does not have any effect!
122+
":v4l-height=240", // XXX this setting does not have any effect!
164123
":v4l-fps=30",
165124
":v4l-quality=20",
166-
":v4l-adev=none", // no audio device
125+
":v4l-adev=none",
167126
};
168127

169-
// Start preocessing.
170-
mediaPlayer.startMedia(mrl, options);
171-
172-
Thread.sleep(5000);
173-
174-
// Stop precessing.
175-
mediaPlayer.stop();
176-
mediaPlayer = null;
177-
178-
System.out.println("Finish!");
128+
player.startMedia(mrl, options);
179129
}
180130
}

0 commit comments

Comments
 (0)