| 1 |
|
package swingjs.api.js; |
| 2 |
|
|
| 3 |
|
import java.awt.Container; |
| 4 |
|
import java.awt.Dimension; |
| 5 |
|
import java.awt.Frame; |
| 6 |
|
import java.awt.event.ActionEvent; |
| 7 |
|
import java.awt.event.ActionListener; |
| 8 |
|
import java.awt.image.BufferedImage; |
| 9 |
|
import java.io.File; |
| 10 |
|
import java.net.URL; |
| 11 |
|
import java.nio.file.Files; |
| 12 |
|
import java.util.ArrayList; |
| 13 |
|
import java.util.function.Function; |
| 14 |
|
|
| 15 |
|
import javax.swing.BoxLayout; |
| 16 |
|
import javax.swing.ImageIcon; |
| 17 |
|
import javax.swing.JButton; |
| 18 |
|
import javax.swing.JDialog; |
| 19 |
|
import javax.swing.JLabel; |
| 20 |
|
import javax.swing.JPanel; |
| 21 |
|
|
| 22 |
|
import swingjs.api.JSUtilI; |
| 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 |
|
@author |
| 48 |
|
|
| 49 |
|
|
| |
|
| 0% |
Uncovered Elements: 177 (177) |
Complexity: 51 |
Complexity Density: 0.45 |
|
| 50 |
|
public interface HTML5Video extends DOMNode { |
| 51 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
| 52 |
|
public interface Promise { |
| 53 |
|
|
| 54 |
|
} |
| 55 |
|
|
| 56 |
|
final static String[] eventTypes = new String[] { "audioprocess", |
| 57 |
|
|
| 58 |
|
"canplay", |
| 59 |
|
|
| 60 |
|
|
| 61 |
|
"canplaythrough", |
| 62 |
|
|
| 63 |
|
"complete", |
| 64 |
|
"durationchange", |
| 65 |
|
"emptied", |
| 66 |
|
|
| 67 |
|
|
| 68 |
|
"ended", |
| 69 |
|
"loadeddata", |
| 70 |
|
"loadedmetadata", |
| 71 |
|
"pause", |
| 72 |
|
"play", |
| 73 |
|
"playing", |
| 74 |
|
|
| 75 |
|
"progress", |
| 76 |
|
"ratechange", |
| 77 |
|
"seeked", |
| 78 |
|
"seeking", |
| 79 |
|
"stalled", |
| 80 |
|
|
| 81 |
|
"suspend", |
| 82 |
|
"timeupdate", |
| 83 |
|
"volumechange", |
| 84 |
|
"waiting", |
| 85 |
|
}; |
| 86 |
|
|
| 87 |
|
|
| 88 |
|
|
| 89 |
|
public void addTextTrack() throws Throwable; |
| 90 |
|
|
| 91 |
|
public Object captureStream() throws Throwable; |
| 92 |
|
|
| 93 |
|
public String canPlayType(String mediaType) throws Throwable; |
| 94 |
|
|
| 95 |
|
public void fastSeek(double time) throws Throwable; |
| 96 |
|
|
| 97 |
|
public void load() throws Throwable; |
| 98 |
|
|
| 99 |
|
public void mozCaptureStream() throws Throwable; |
| 100 |
|
|
| 101 |
|
public void mozCaptureStreamUntilEnded() throws Throwable; |
| 102 |
|
|
| 103 |
|
public void mozGetMetadata() throws Throwable; |
| 104 |
|
|
| 105 |
|
public void pause() throws Throwable; |
| 106 |
|
|
| 107 |
|
public Promise play() throws Throwable; |
| 108 |
|
|
| 109 |
|
public Promise seekToNextFrame() throws Throwable; |
| 110 |
|
|
| 111 |
|
public Promise setMediaKeys(Object mediaKeys) throws Throwable; |
| 112 |
|
|
| 113 |
|
public Promise setSinkId(String id) throws Throwable; |
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 117 |
0 |
public static double getDuration(HTML5Video v) {... |
| 118 |
0 |
return |
| 119 |
|
0; |
| 120 |
|
} |
| 121 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 122 |
0 |
public static double setCurrentTime(HTML5Video v, double time) {... |
| 123 |
0 |
return |
| 124 |
|
0; |
| 125 |
|
} |
| 126 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 127 |
0 |
public static double getCurrentTime(HTML5Video v) {... |
| 128 |
0 |
return |
| 129 |
|
0; |
| 130 |
|
} |
| 131 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 132 |
0 |
public static Dimension getSize(HTML5Video v) {... |
| 133 |
0 |
return new Dimension( |
| 134 |
|
0, |
| 135 |
|
0); |
| 136 |
|
} |
| 137 |
|
|
| 138 |
|
|
| 139 |
|
|
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
| 143 |
|
|
| 144 |
|
@param |
| 145 |
|
@param |
| 146 |
|
@return |
| 147 |
|
|
| |
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 5 |
Complexity Density: 0.71 |
|
| 148 |
0 |
public static BufferedImage getImage(HTML5Video v, int imageType) {... |
| 149 |
0 |
Dimension d = HTML5Video.getSize(v); |
| 150 |
0 |
BufferedImage image = (BufferedImage) HTML5Video.getProperty(v, "_image"); |
| 151 |
0 |
if (image == null || image.getWidth() != d.width || image.getHeight() != d.height) { |
| 152 |
0 |
image = new BufferedImage(d.width, d.height, imageType == Integer.MIN_VALUE ? JSUtilI.TYPE_4BYTE_HTML5 : imageType); |
| 153 |
0 |
HTML5Video.setProperty(v, "_image", image); |
| 154 |
|
} |
| 155 |
0 |
HTML5Canvas.setImageNode(v, image); |
| 156 |
0 |
return image; |
| 157 |
|
} |
| 158 |
|
|
| 159 |
|
|
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
|
|
| 164 |
|
|
| 165 |
|
@param |
| 166 |
|
@param |
| 167 |
|
@param |
| 168 |
|
|
| |
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 3 |
Complexity Density: 1.5 |
|
| 169 |
0 |
public static void setProperty(HTML5Video jsvideo, String key, Object value) {... |
| 170 |
0 |
if (value instanceof Number) { |
| 171 |
|
|
| 172 |
0 |
} else if (value instanceof Boolean) { |
| 173 |
|
|
| 174 |
|
} else { |
| 175 |
|
|
| 176 |
|
} |
| 177 |
|
} |
| 178 |
|
|
| 179 |
|
|
| 180 |
|
|
| 181 |
|
|
| 182 |
|
|
| 183 |
|
@param |
| 184 |
|
|
| 185 |
|
@param |
| 186 |
|
@return |
| 187 |
|
|
| |
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 4 |
Complexity Density: 0.4 |
|
| 188 |
0 |
@SuppressWarnings("unused")... |
| 189 |
|
public static Object getProperty(HTML5Video jsvideo, String key) { |
| 190 |
0 |
Object val = ( |
| 191 |
|
null); |
| 192 |
0 |
if (val == null) |
| 193 |
0 |
return null; |
| 194 |
0 |
switch ( |
| 195 |
|
"") { |
| 196 |
0 |
case "number": |
| 197 |
0 |
return Double.valueOf( |
| 198 |
|
0); |
| 199 |
0 |
case "boolean": |
| 200 |
0 |
return Boolean.valueOf( |
| 201 |
|
false); |
| 202 |
0 |
default: |
| 203 |
0 |
return val; |
| 204 |
|
} |
| 205 |
|
} |
| 206 |
|
|
| 207 |
|
|
| 208 |
|
|
| 209 |
|
|
| 210 |
|
|
| 211 |
|
|
| 212 |
|
@param |
| 213 |
|
@param |
| 214 |
|
@param |
| 215 |
|
|
| 216 |
|
@return |
| 217 |
|
|
| |
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 5 |
Complexity Density: 0.45 |
|
| 218 |
0 |
public static Object[] addActionListener(HTML5Video jsvideo, ActionListener listener, String... events) {... |
| 219 |
0 |
if (events == null || events.length == 0) |
| 220 |
0 |
events = eventTypes; |
| 221 |
0 |
@SuppressWarnings("unused") |
| 222 |
|
Function<Object, Void> f = new Function<Object, Void>() { |
| 223 |
|
|
| |
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
| 224 |
0 |
@Override... |
| 225 |
|
public Void apply(Object jsevent) { |
| 226 |
0 |
String name = ( |
| 227 |
|
"?"); |
| 228 |
0 |
System.out.println("HTML5Video " + name); |
| 229 |
0 |
ActionEvent e = new ActionEvent(new Object[] { jsvideo, jsevent }, 12345, name, |
| 230 |
|
System.currentTimeMillis(), 0); |
| 231 |
0 |
listener.actionPerformed(e); |
| 232 |
0 |
return null; |
| 233 |
|
} |
| 234 |
|
}; |
| 235 |
0 |
ArrayList<Object> listeners = new ArrayList<>(); |
| 236 |
0 |
for (int i = 0; i < events.length; i++) { |
| 237 |
0 |
Object func = |
| 238 |
|
|
| 239 |
|
|
| 240 |
|
null; |
| 241 |
0 |
listeners.add(events[i]); |
| 242 |
0 |
listeners.add(func); |
| 243 |
0 |
if (jsvideo != null) |
| 244 |
0 |
jsvideo.addEventListener(events[i], func); |
| 245 |
|
|
| 246 |
|
} |
| 247 |
0 |
return listeners.toArray(new Object[listeners.size()]); |
| 248 |
|
} |
| 249 |
|
|
| 250 |
|
|
| 251 |
|
|
| 252 |
|
|
| 253 |
|
@param |
| 254 |
|
@param |
| 255 |
|
|
| 256 |
|
|
| |
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 4 |
Complexity Density: 0.57 |
|
| 257 |
0 |
public static void removeActionListener(HTML5Video jsvideo, Object[] listeners) {... |
| 258 |
0 |
if (listeners == null) { |
| 259 |
0 |
for (int i = 0; i < eventTypes.length; i++) { |
| 260 |
0 |
jsvideo.removeEventListener(eventTypes[i]); |
| 261 |
|
} |
| 262 |
|
} |
| 263 |
|
|
| 264 |
0 |
for (int i = 0; i < listeners.length; i += 2) { |
| 265 |
0 |
String event = (String) listeners[i]; |
| 266 |
0 |
Object listener = listeners[i + 1]; |
| 267 |
0 |
jsvideo.removeEventListener(event, listener); |
| 268 |
|
} |
| 269 |
|
} |
| 270 |
|
|
| 271 |
|
|
| 272 |
|
|
| 273 |
|
|
| 274 |
|
@param |
| 275 |
|
@return |
| 276 |
|
|
| |
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 5 |
Complexity Density: 0.56 |
|
| 277 |
0 |
public static ImageIcon createIcon(Object source) {... |
| 278 |
0 |
try { |
| 279 |
0 |
if (source instanceof URL) { |
| 280 |
0 |
return new ImageIcon((URL) source, "jsvideo"); |
| 281 |
0 |
} else if (source instanceof byte[]) { |
| 282 |
0 |
return new ImageIcon((byte[]) source, "jsvideo"); |
| 283 |
0 |
} else if (source instanceof File) { |
| 284 |
0 |
return new ImageIcon(Files.readAllBytes(((File) source).toPath())); |
| 285 |
|
} else { |
| 286 |
0 |
return new ImageIcon(Files.readAllBytes(new File(source.toString()).toPath())); |
| 287 |
|
} |
| 288 |
|
} catch (Throwable t) { |
| 289 |
0 |
return null; |
| 290 |
|
} |
| 291 |
|
} |
| 292 |
|
|
| 293 |
|
|
| 294 |
|
|
| 295 |
|
|
| 296 |
|
@param |
| 297 |
|
@return |
| 298 |
|
|
| |
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 3 |
Complexity Density: 1.5 |
|
| 299 |
0 |
public static JLabel createLabel(Object source) {... |
| 300 |
0 |
ImageIcon icon = (source instanceof ImageIcon ? (ImageIcon) source : createIcon(source)); |
| 301 |
0 |
return (icon == null ? null : new JLabel(icon)); |
| 302 |
|
} |
| 303 |
|
|
| 304 |
|
|
| 305 |
|
|
| 306 |
|
|
| 307 |
|
@param |
| 308 |
|
@param |
| 309 |
|
@param |
| 310 |
|
@return |
| 311 |
|
|
| |
|
| 0% |
Uncovered Elements: 18 (18) |
Complexity: 2 |
Complexity Density: 0.12 |
|
| 312 |
0 |
public static JDialog createDialog(Frame parent, Object source, int maxWidth, Function<HTML5Video, Void> whenReady) {... |
| 313 |
0 |
JDialog dialog = new JDialog(parent); |
| 314 |
0 |
Container p = dialog.getContentPane(); |
| 315 |
0 |
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); |
| 316 |
0 |
JLabel label = (source instanceof JLabel ? (JLabel) source : createLabel(source)); |
| 317 |
0 |
label.setAlignmentX(0.5f); |
| 318 |
|
|
| 319 |
0 |
p.add(label); |
| 320 |
0 |
label.setVisible(false); |
| 321 |
0 |
p.add(getControls(label)); |
| 322 |
0 |
dialog.setModal(false); |
| 323 |
0 |
dialog.pack(); |
| 324 |
0 |
dialog.setVisible(true); |
| 325 |
0 |
dialog.setVisible(false); |
| 326 |
0 |
HTML5Video jsvideo = (HTML5Video) label.getClientProperty("jsvideo"); |
| 327 |
0 |
HTML5Video.addActionListener(jsvideo, new ActionListener() { |
| 328 |
|
|
| |
|
| 0% |
Uncovered Elements: 18 (18) |
Complexity: 4 |
Complexity Density: 0.33 |
|
| 329 |
0 |
@Override... |
| 330 |
|
public void actionPerformed(ActionEvent e) { |
| 331 |
0 |
if (label.getClientProperty("jsvideo.size") != null) |
| 332 |
0 |
return; |
| 333 |
0 |
Dimension dim = HTML5Video.getSize(jsvideo); |
| 334 |
0 |
while (dim.width > maxWidth) { |
| 335 |
0 |
dim.width /= 2; |
| 336 |
0 |
dim.height /= 2; |
| 337 |
|
} |
| 338 |
0 |
label.putClientProperty("jsvideo.size", dim); |
| 339 |
0 |
label.setPreferredSize(dim); |
| 340 |
0 |
label.setVisible(true); |
| 341 |
|
|
| 342 |
0 |
dialog.pack(); |
| 343 |
|
|
| 344 |
0 |
if (whenReady != null) |
| 345 |
0 |
whenReady.apply(jsvideo); |
| 346 |
|
} |
| 347 |
|
|
| 348 |
|
}, "canplaythrough"); |
| 349 |
0 |
HTML5Video.setCurrentTime(jsvideo, 0); |
| 350 |
0 |
return dialog; |
| 351 |
|
} |
| 352 |
|
|
| |
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
|
| 353 |
0 |
static JPanel getControls(JLabel label) {... |
| 354 |
|
|
| 355 |
0 |
JPanel controls = new JPanel(); |
| 356 |
0 |
controls.setAlignmentX(0.5f); |
| 357 |
0 |
JButton btn = new JButton("play"); |
| 358 |
0 |
btn.addActionListener(new ActionListener() { |
| 359 |
|
|
| |
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 360 |
0 |
@Override... |
| 361 |
|
public void actionPerformed(ActionEvent e) { |
| 362 |
0 |
try { |
| 363 |
0 |
((HTML5Video) label.getClientProperty("jsvideo")).play(); |
| 364 |
|
} catch (Throwable e1) { |
| 365 |
0 |
e1.printStackTrace(); |
| 366 |
|
} |
| 367 |
|
} |
| 368 |
|
|
| 369 |
|
}); |
| 370 |
0 |
controls.add(btn); |
| 371 |
|
|
| 372 |
0 |
btn = new JButton("pause"); |
| 373 |
0 |
btn.addActionListener(new ActionListener() { |
| 374 |
|
|
| |
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 375 |
0 |
@Override... |
| 376 |
|
public void actionPerformed(ActionEvent e) { |
| 377 |
0 |
try { |
| 378 |
0 |
((HTML5Video) label.getClientProperty("jsvideo")).pause(); |
| 379 |
|
} catch (Throwable e1) { |
| 380 |
0 |
e1.printStackTrace(); |
| 381 |
|
} |
| 382 |
|
} |
| 383 |
|
|
| 384 |
|
}); |
| 385 |
0 |
controls.add(btn); |
| 386 |
|
|
| 387 |
0 |
btn = new JButton("reset"); |
| 388 |
0 |
btn.addActionListener(new ActionListener() { |
| 389 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 390 |
0 |
@Override... |
| 391 |
|
public void actionPerformed(ActionEvent e) { |
| 392 |
0 |
HTML5Video.setCurrentTime((HTML5Video) label.getClientProperty("jsvideo"), 0); |
| 393 |
|
} |
| 394 |
|
|
| 395 |
|
}); |
| 396 |
0 |
controls.add(btn); |
| 397 |
|
|
| 398 |
0 |
return controls; |
| 399 |
|
} |
| 400 |
|
|
| 401 |
|
|
| 402 |
|
|
| 403 |
|
|
| 404 |
|
@param |
| 405 |
|
@param |
| 406 |
|
@return |
| 407 |
|
|
| 408 |
|
|
| |
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
| 409 |
0 |
public static boolean nextFrame(HTML5Video jsvideo, double dt) {... |
| 410 |
0 |
Boolean canSeek = (Boolean) getProperty(jsvideo,"_canseek"); |
| 411 |
0 |
if (canSeek == null) { |
| 412 |
0 |
setProperty(jsvideo, "_canseek", canSeek = Boolean.valueOf(getProperty(jsvideo, "seekToNextFrame") != null)); |
| 413 |
|
} |
| 414 |
0 |
try { |
| 415 |
0 |
if (canSeek) { |
| 416 |
0 |
jsvideo.seekToNextFrame(); |
| 417 |
|
} else { |
| 418 |
0 |
HTML5Video.setCurrentTime(jsvideo, HTML5Video.getCurrentTime(jsvideo) + dt); |
| 419 |
|
} |
| 420 |
|
} catch (Throwable e1) { |
| 421 |
|
} |
| 422 |
0 |
return canSeek.booleanValue(); |
| 423 |
|
} |
| 424 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 425 |
0 |
public static int getFrameCount(HTML5Video jsvideo) {... |
| 426 |
0 |
return (int) (getDuration(jsvideo) / 0.033334); |
| 427 |
|
} |
| 428 |
|
|
| 429 |
|
|
| 430 |
|
|
| 431 |
|
|
| 432 |
|
|
| 433 |
|
|
| 434 |
|
|
| 435 |
|
|
| 436 |
|
|
| 437 |
|
|
| 438 |
|
|
| 439 |
|
|
| 440 |
|
|
| 441 |
|
|
| 442 |
|
|
| 443 |
|
|
| 444 |
|
|
| 445 |
|
|
| 446 |
|
|
| 447 |
|
|
| 448 |
|
|
| 449 |
|
|
| 450 |
|
|
| 451 |
|
|
| 452 |
|
|
| 453 |
|
|
| 454 |
|
|
| 455 |
|
|
| 456 |
|
|
| 457 |
|
|
| 458 |
|
|
| 459 |
|
|
| 460 |
|
|
| 461 |
|
|
| 462 |
|
|
| 463 |
|
|
| 464 |
|
|
| 465 |
|
|
| 466 |
|
|
| 467 |
|
|
| 468 |
|
|
| 469 |
|
|
| 470 |
|
|
| 471 |
|
|
| 472 |
|
} |