Skip to content

Commit 76cdc99

Browse files
committed
Test vlcj direct rendering
1 parent e52b03e commit 76cdc99

File tree

1 file changed

+180
-0
lines changed

1 file changed

+180
-0
lines changed
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
2+
3+
import java.awt.Transparency;
4+
import java.awt.color.ColorSpace;
5+
import java.awt.image.BufferedImage;
6+
import java.awt.image.ComponentColorModel;
7+
import java.awt.image.ComponentSampleModel;
8+
import java.awt.image.DataBuffer;
9+
import java.awt.image.DataBufferByte;
10+
import java.awt.image.Raster;
11+
import java.awt.image.WritableRaster;
12+
import java.io.File;
13+
import java.io.IOException;
14+
import java.nio.ByteBuffer;
15+
16+
import javax.imageio.ImageIO;
17+
18+
import uk.co.caprica.vlcj.player.MediaPlayerFactory;
19+
import uk.co.caprica.vlcj.player.direct.BufferFormat;
20+
import uk.co.caprica.vlcj.player.direct.BufferFormatCallback;
21+
import uk.co.caprica.vlcj.player.direct.DirectMediaPlayer;
22+
import uk.co.caprica.vlcj.player.direct.RenderCallback;
23+
import uk.co.caprica.vlcj.player.direct.format.RV32BufferFormat;
24+
25+
import com.sun.jna.Memory;
26+
27+
28+
public class VlcjDirectTest {
29+
30+
private static class TestBufferFormatCallback implements BufferFormatCallback {
31+
32+
@Override
33+
public BufferFormat getBufferFormat(int sourceWidth, int sourceHeight) {
34+
return new RV32BufferFormat(sourceWidth, sourceHeight);
35+
}
36+
}
37+
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+
57+
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
63+
"--intf", "dummy",
64+
65+
// no video output
66+
"--vout", "dummy",
67+
68+
// no audio decoding
69+
"--no-audio",
70+
71+
// do not display title
72+
"--no-video-title-show",
73+
74+
// no stats
75+
"--no-stats",
76+
77+
// no subtitles
78+
"--no-sub-autodetect-file",
79+
80+
// no snapshot previews
81+
"--no-snapshot-preview",
82+
83+
// reduce capture lag/latency
84+
"--live-caching=50",
85+
86+
// turn off warnings
87+
"--quiet",
88+
};
89+
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) {
102+
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];
125+
126+
if (memory == null) {
127+
System.err.println("Null memory!");
128+
return;
129+
}
130+
131+
System.out.println("meme " + memory.size());
132+
133+
ByteBuffer buffer = memory.getByteBuffer(0, memory.size());
134+
135+
byte[] bytes = new byte[width * height * 4];
136+
byte[][] data = new byte[][] { bytes };
137+
138+
buffer.get(bytes);
139+
140+
DataBufferByte dbuf = new DataBufferByte(data, bytes.length, new int[] { 0 });
141+
WritableRaster raster = Raster.createWritableRaster(smodel, dbuf, null);
142+
143+
BufferedImage bi = new BufferedImage(cmodel, raster, false, null);
144+
bi.flush();
145+
146+
try {
147+
ImageIO.write(bi, "JPG", new File((i++) + "-test.jpg"));
148+
} catch (IOException e) {
149+
// TODO Auto-generated catch block
150+
e.printStackTrace();
151+
}
152+
153+
System.out.println("display " + i);
154+
}
155+
});
156+
157+
// Options setup.
158+
String mrl = "v4l2:///dev/video0"; // Linux
159+
160+
String[] options = new String[] {
161+
":v4l-vdev=/dev/video0",
162+
":v4l-width=640",
163+
":v4l-height=480",
164+
":v4l-fps=30",
165+
":v4l-quality=20",
166+
":v4l-adev=none", // no audio device
167+
};
168+
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!");
179+
}
180+
}

0 commit comments

Comments
 (0)