|
1 | | - |
2 | | - |
3 | 1 | import java.awt.Transparency; |
4 | 2 | import java.awt.color.ColorSpace; |
5 | 3 | import java.awt.image.BufferedImage; |
|
11 | 9 | import java.awt.image.WritableRaster; |
12 | 10 | import java.io.File; |
13 | 11 | import java.io.IOException; |
14 | | -import java.nio.ByteBuffer; |
15 | 12 |
|
16 | 13 | import javax.imageio.ImageIO; |
17 | 14 |
|
|
27 | 24 |
|
28 | 25 | public class VlcjDirectTest { |
29 | 26 |
|
30 | | - private static class TestBufferFormatCallback implements BufferFormatCallback { |
| 27 | + private static class DirectBufferFormatCallback implements BufferFormatCallback { |
31 | 28 |
|
32 | 29 | @Override |
33 | 30 | public BufferFormat getBufferFormat(int sourceWidth, int sourceHeight) { |
34 | 31 | return new RV32BufferFormat(sourceWidth, sourceHeight); |
35 | 32 | } |
36 | 33 | } |
37 | 34 |
|
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 | 35 | 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 | 36 | "--intf", "dummy", |
64 | | - |
65 | | - // no video output |
66 | 37 | "--vout", "dummy", |
67 | | - |
68 | | - // no audio decoding |
69 | 38 | "--no-audio", |
70 | | - |
71 | | - // do not display title |
72 | 39 | "--no-video-title-show", |
73 | | - |
74 | | - // no stats |
75 | 40 | "--no-stats", |
76 | | - |
77 | | - // no subtitles |
78 | 41 | "--no-sub-autodetect-file", |
79 | | - |
80 | | - // no snapshot previews |
81 | 42 | "--no-snapshot-preview", |
82 | | - |
83 | | - // reduce capture lag/latency |
84 | 43 | "--live-caching=50", |
85 | | - |
86 | | - // turn off warnings |
87 | 44 | "--quiet", |
88 | 45 | }; |
89 | 46 |
|
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) { |
102 | 48 |
|
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 | + } |
125 | 52 |
|
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 | + } |
130 | 57 |
|
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 | + } |
132 | 86 |
|
133 | | - ByteBuffer buffer = memory.getByteBuffer(0, memory.size()); |
| 87 | + private static MediaPlayerFactory factory = new MediaPlayerFactory(VLC_ARGS); |
| 88 | + private static DirectMediaPlayer player; |
134 | 89 |
|
135 | | - byte[] bytes = new byte[width * height * 4]; |
136 | | - byte[][] data = new byte[][] { bytes }; |
| 90 | + public static void main(String[] args) throws IOException, InterruptedException { |
137 | 91 |
|
138 | | - buffer.get(bytes); |
| 92 | + player = factory.newDirectMediaPlayer( |
| 93 | + new DirectBufferFormatCallback(), |
| 94 | + new RenderCallback() { |
139 | 95 |
|
140 | | - DataBufferByte dbuf = new DataBufferByte(data, bytes.length, new int[] { 0 }); |
141 | | - WritableRaster raster = Raster.createWritableRaster(smodel, dbuf, null); |
| 96 | + int i = 0; |
142 | 97 |
|
143 | | - BufferedImage bi = new BufferedImage(cmodel, raster, false, null); |
144 | | - bi.flush(); |
| 98 | + @Override |
| 99 | + public void display(DirectMediaPlayer player, Memory[] buffers, BufferFormat format) { |
145 | 100 |
|
| 101 | + BufferedImage bi = convert(buffers, format); |
146 | 102 | try { |
147 | | - ImageIO.write(bi, "JPG", new File((i++) + "-test.jpg")); |
| 103 | + ImageIO.write(bi, "JPG", new File(System.currentTimeMillis() + "-test.jpg")); |
148 | 104 | } catch (IOException e) { |
149 | | - // TODO Auto-generated catch block |
150 | 105 | e.printStackTrace(); |
151 | 106 | } |
152 | 107 |
|
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 | + } |
154 | 114 | } |
155 | 115 | }); |
156 | 116 |
|
157 | | - // Options setup. |
158 | | - String mrl = "v4l2:///dev/video0"; // Linux |
159 | | - |
| 117 | + String device = "/dev/video0"; |
| 118 | + String mrl = "v4l2://" + device; |
160 | 119 | 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! |
164 | 123 | ":v4l-fps=30", |
165 | 124 | ":v4l-quality=20", |
166 | | - ":v4l-adev=none", // no audio device |
| 125 | + ":v4l-adev=none", |
167 | 126 | }; |
168 | 127 |
|
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); |
179 | 129 | } |
180 | 130 | } |
0 commit comments