forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestCamera.java
More file actions
executable file
·394 lines (355 loc) · 13.4 KB
/
Copy pathTestCamera.java
File metadata and controls
executable file
·394 lines (355 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package javaforce.utils;
/** Tests Camera function
*
* @author pquiring
*
* Created : Jun 9, 2014
*/
import java.io.*;
import java.util.*;
import javaforce.*;
import javaforce.awt.*;
import javaforce.service.*;
import javaforce.media.*;
import javaforce.webui.*;
import javaforce.webui.event.*;
public class TestCamera extends javax.swing.JFrame implements WebUIHandler, MediaIO {
/**
* Creates new form TestCamera
*/
public TestCamera() {
initComponents();
pack();
listCameras();
new WebUIServer().start(this, 8080);
encoder_dash = new MediaEncoder();
encoder_dash.setProfileLevel(MediaCoder.PROFILE_MAIN);
encoder_h264 = new MediaEncoder();
encoder_h264.setProfileLevel(MediaCoder.PROFILE_MAIN);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
start = new javax.swing.JButton();
stop = new javax.swing.JButton();
preview = new javax.swing.JLabel();
webView = new javax.swing.JButton();
cameraList = new javax.swing.JComboBox<>();
transcodeBox = new javax.swing.JCheckBox();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Camera Test");
start.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
start.setText("Start");
start.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
startActionPerformed(evt);
}
});
stop.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
stop.setText("Stop");
stop.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
stopActionPerformed(evt);
}
});
webView.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
webView.setText("Web Stream");
webView.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
webViewActionPerformed(evt);
}
});
cameraList.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
transcodeBox.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
transcodeBox.setText("transcode h264 -> dash");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(preview, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(start, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(stop, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(cameraList, 0, 370, Short.MAX_VALUE)
.addComponent(transcodeBox, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(webView, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(start, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
.addComponent(webView, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(stop, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(cameraList, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(transcodeBox)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(preview, javax.swing.GroupLayout.DEFAULT_SIZE, 492, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void startActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startActionPerformed
setState(false);
transcode = transcodeBox.isSelected();
camera = new Camera();
if (!camera.init()) {
JFAWT.showError("Error", "Camera init failed");
return;
}
String[] devices = camera.listDevices();
if (devices == null || devices.length == 0) {
camera.uninit();
JFAWT.showError("Error", "No camera found");
}
int camIdx = cameraList.getSelectedIndex();
if (camIdx >= devices.length) {
camIdx = 0;
}
JFLog.log("camera=" + devices[camIdx]);
for(int a=0;a<3;a++) {JFLog.log(" ------------------------------- ");}
encoder_dash.start(this, 640, 480, 10, -1, -1, "dash", true, false);
if (transcode) {
for(int a=0;a<3;a++) {JFLog.log(" ------------------------------- ");}
encoder_h264.start(new MediaIO() {
public int read(MediaCoder coder, byte[] data) {
return 0;
}
public int write(MediaCoder coder, byte[] data) {
JFLog.log("h264.write:" + data.length);
encoder_dash.addVideoEncodedTS(data, 0, data.length, false, encoder_h264.getLastDTS(), encoder_h264.getLastPTS());
encoder_dash.flush();
return data.length;
}
public long seek(MediaCoder coder, long pos, int how) {
return 0;
}
}, 640, 480, 10, -1, -1, "h264", true, false);
}
for(int a=0;a<3;a++) {JFLog.log(" ------------------------------- ");}
camera.start(camIdx, 640, 480);
width = camera.getWidth();
height = camera.getHeight();
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
java.awt.EventQueue.invokeLater(new Runnable() {public void run() {
int[] px = camera.getFrame();
if (px == null) return; //not ready
if (img == null) {
img = new JFImage(width, height);
}
System.arraycopy(px, 0, img.getBuffer(), 0, width * height);
preview.setIcon(img);
preview.repaint();
JFLog.log("addFrame:" + frameCount++);
if (transcode) {
encoder_h264.addVideo(px);
encoder_h264.flush();
} else {
encoder_dash.addVideo(px);
encoder_dash.flush();
}
JFLog.log("============================");
}});
}
}, 100, 100);
}//GEN-LAST:event_startActionPerformed
private void stopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopActionPerformed
if (camera == null) {
return;
}
timer.cancel();
timer = null;
camera.stop();
camera = null;
if (transcode) {
encoder_h264.stop();
}
encoder_dash.stop();
setState(true);
}//GEN-LAST:event_stopActionPerformed
private void webViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_webViewActionPerformed
JFAWT.openURL("http://localhost:8080");
}//GEN-LAST:event_webViewActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TestCamera().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JComboBox<String> cameraList;
private javax.swing.JLabel preview;
private javax.swing.JButton start;
private javax.swing.JButton stop;
private javax.swing.JCheckBox transcodeBox;
private javax.swing.JButton webView;
// End of variables declaration//GEN-END:variables
private Camera camera;
private Timer timer;
private int frameCount = 0;
private int encoderCount = 1;
private int fps = 10;
private JFImage img;
private int width, height;
private MediaEncoder encoder_dash;
private MediaEncoder encoder_h264;
private WebUIClient client;
private Video video;
private Video video_capture;
private byte[] init_segment;
private boolean transcode;
public void listCameras() {
camera = new Camera();
if (!camera.init()) {
JFAWT.showError("Error", "Camera init failed");
return;
}
String[] devices = camera.listDevices();
camera.uninit();
if (devices == null || devices.length == 0) {
JFAWT.showError("Error", "No camera found");
return;
}
JFLog.log("device count=" + devices.length);
for (int a = 0; a < devices.length; a++) {
JFLog.log("device=" + devices[a]);
cameraList.addItem(devices[a]);
}
}
public String getCurrentTime() {
double currentTime = encoderCount;
currentTime /= fps;
return String.format("%.3f", currentTime);
}
public void setState(boolean state) {
transcodeBox.setEnabled(state);
cameraList.setEnabled(state);
webView.setEnabled(state);
}
//WebUIHandler
public void clientConnected(WebUIClient client) {
JFLog.log("clientConnected:" + client);
this.client = client;
client.setProperty("init-segment", "false"); //got init segment (ftyp)
client.setProperty("start-segment", "false"); //got start of segment (styp) (else wait for next styp)
}
public void clientDisconnected(WebUIClient client) {
JFLog.log("clientDisconnected:" + client);
this.client = null;
//System.exit(0);
}
public byte[] getResource(String url, HTTP.Parameters params, WebResponse res) {
//TODO : return static images, etc needed by webpage
return null;
}
public Panel getPanel(String name, HTTP.Parameters params, WebUIClient client) {
Panel panel = new Panel();
video = new Video();
video.setWidth(640);
video.setHeight(480);
panel.add(video);
video.addActionListener(new Action() {
public void action(Component cmp) {
video.setLiveSource(encoder_dash.getCodecMimeType("dash", true, false));
}
});
video_capture = new Video();
video_capture.setWidth(1024);
video_capture.setHeight(720);
panel.add(video_capture);
video_capture.addActionListener(new Action() {
public void action(Component cmp) {
video_capture.setCapture(true, true);
try {
client.setOutputStream(new FileOutputStream("test.mkv"));
} catch (Exception e) {}
}
});
return panel;
}
//MediaIO
public int read(MediaCoder coder, byte[] data) {
JFLog.log("read:" + data.length);
return -1;
}
/*
struct mp4_header { int size; char type[4]; }
types:
ftyp / moov = init segment (describes video details)
styp = start of segment
moof / mdat = frame
*/
public int write(MediaCoder coder, byte[] data) {
JFLog.log("dash.write:" + data.length + ":" + getCurrentTime());
boolean is_init = init_segment == null;
boolean is_start_segment = false;
if (data.length >= 8) {
String type = new String(data, 4, 4);
if (type.equals("styp")) {
is_start_segment = true;
}
}
if (init_segment == null) {
init_segment = new byte[data.length];
System.arraycopy(data, 0, init_segment, 0, data.length);
}
//send fragments
if (client != null) {
if (!video.isPlaying()) {
return data.length;
}
JFLog.log("send frame:" + data.length);
if (client.getProperty("init-segment").equals("false")) {
if (is_init) {
client.setProperty("init-segment", "true");
client.setProperty("start-segment", "true");
}
}
if (!is_start_segment) {
if (client.getProperty("start-segment").equals("false")) {
return data.length;
}
} else {
if (client.getProperty("start-segment").equals("false")) {
client.setProperty("init-segment", "true");
client.sendDataEvent(init_segment, video.getID(), "media_add_buffer", null);
client.setProperty("start-segment", "true");
//because player has missed some segments it will need to seek to currentTime
client.sendEvent(video.getID(), "media_seek", new String[] {"time=" + getCurrentTime()});
}
}
client.sendDataEvent(data, video.getID(), "media_add_buffer", null);
if (is_init) {
client.setProperty("init-segment", "true");
}
}
return data.length;
}
public long seek(MediaCoder coder, long pos, int type) {
JFLog.log("seek:" + pos + "," + type);
return pos;
}
}