2013-02-07 15:19:47 +01:00
|
|
|
/**
|
2014-02-19 10:38:30 +01:00
|
|
|
*
|
|
|
|
* Copyright the original author or authors
|
2013-02-07 15:19:47 +01:00
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2013-02-07 15:19:47 +01:00
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2014-08-20 00:25:57 +02:00
|
|
|
package org.jivesoftware.smackx.jingleold.mediaimpl.sshare.api;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
import java.awt.AWTException;
|
|
|
|
import java.awt.Rectangle;
|
|
|
|
import java.awt.Robot;
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import java.awt.image.PixelGrabber;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.DatagramPacket;
|
|
|
|
import java.net.DatagramSocket;
|
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.util.Arrays;
|
2015-03-17 21:19:06 +01:00
|
|
|
import java.util.logging.Level;
|
2014-02-19 10:38:30 +01:00
|
|
|
import java.util.logging.Logger;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* UDP Image Receiver.
|
|
|
|
* It uses PNG Tiles into UDP packets.
|
|
|
|
*
|
|
|
|
* @author Thiago Rocha Camargo
|
|
|
|
*/
|
|
|
|
public class ImageTransmitter implements Runnable {
|
|
|
|
|
2017-02-07 22:02:40 +01:00
|
|
|
private static final Logger LOGGER = Logger.getLogger(ImageTransmitter.class.getName());
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2017-02-07 22:02:40 +01:00
|
|
|
private Robot robot;
|
2014-02-17 23:58:40 +01:00
|
|
|
private InetAddress localHost;
|
|
|
|
private InetAddress remoteHost;
|
|
|
|
private int localPort;
|
|
|
|
private int remotePort;
|
|
|
|
public static final int tileWidth = 25;
|
|
|
|
private boolean on = true;
|
|
|
|
private boolean transmit = false;
|
|
|
|
private DatagramSocket socket;
|
|
|
|
private Rectangle area;
|
2014-08-15 23:16:18 +02:00
|
|
|
private int[][][] tiles;
|
2014-02-17 23:58:40 +01:00
|
|
|
private int maxI;
|
|
|
|
private int maxJ;
|
|
|
|
private ImageEncoder encoder;
|
2018-03-29 12:35:11 +02:00
|
|
|
public static final int KEYFRAME = 10;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
public ImageTransmitter(DatagramSocket socket, InetAddress remoteHost, int remotePort, Rectangle area) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
robot = new Robot();
|
|
|
|
|
|
|
|
maxI = (int) Math.ceil(area.getWidth() / tileWidth);
|
|
|
|
maxJ = (int) Math.ceil(area.getHeight() / tileWidth);
|
|
|
|
|
|
|
|
tiles = new int[maxI][maxJ][tileWidth * tileWidth];
|
|
|
|
|
|
|
|
this.area = area;
|
|
|
|
this.socket = socket;
|
|
|
|
localHost = socket.getLocalAddress();
|
|
|
|
localPort = socket.getLocalPort();
|
|
|
|
this.remoteHost = remoteHost;
|
|
|
|
this.remotePort = remotePort;
|
|
|
|
this.encoder = new DefaultEncoder();
|
|
|
|
|
|
|
|
transmit = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (AWTException e) {
|
2015-03-17 21:19:06 +01:00
|
|
|
LOGGER.log(Level.WARNING, "exception", e);
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void start() {
|
2014-08-15 23:16:18 +02:00
|
|
|
byte[] buf = new byte[1024];
|
2014-02-17 23:58:40 +01:00
|
|
|
final DatagramPacket p = new DatagramPacket(buf, 1024);
|
|
|
|
|
|
|
|
int keyframe = 0;
|
|
|
|
|
|
|
|
while (on) {
|
|
|
|
if (transmit) {
|
|
|
|
|
|
|
|
BufferedImage capture = robot.createScreenCapture(area);
|
|
|
|
|
|
|
|
QuantizeFilter filter = new QuantizeFilter();
|
|
|
|
capture = filter.filter(capture, null);
|
|
|
|
|
|
|
|
long trace = System.currentTimeMillis();
|
|
|
|
|
|
|
|
if (++keyframe > KEYFRAME) {
|
|
|
|
keyframe = 0;
|
|
|
|
}
|
2014-02-19 10:38:30 +01:00
|
|
|
LOGGER.fine("KEYFRAME:" + keyframe);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < maxI; i++) {
|
|
|
|
for (int j = 0; j < maxJ; j++) {
|
|
|
|
|
|
|
|
final BufferedImage bufferedImage = capture.getSubimage(i * tileWidth, j * tileWidth, tileWidth, tileWidth);
|
|
|
|
|
2014-08-15 23:16:18 +02:00
|
|
|
int[] pixels = new int[tileWidth * tileWidth];
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
PixelGrabber pg = new PixelGrabber(bufferedImage, 0, 0, tileWidth, tileWidth, pixels, 0, tileWidth);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (pg.grabPixels()) {
|
|
|
|
|
|
|
|
if (keyframe == KEYFRAME || !Arrays.equals(tiles[i][j], pixels)) {
|
|
|
|
|
|
|
|
ByteArrayOutputStream baos = encoder.encode(bufferedImage);
|
|
|
|
|
|
|
|
if (baos != null) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
Thread.sleep(1);
|
|
|
|
|
|
|
|
baos.write(i);
|
|
|
|
baos.write(j);
|
|
|
|
|
|
|
|
byte[] bytesOut = baos.toByteArray();
|
|
|
|
|
|
|
|
if (bytesOut.length > 1000)
|
2014-02-19 10:38:30 +01:00
|
|
|
LOGGER.severe("Bytes out > 1000. Equals " + bytesOut.length);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
p.setData(bytesOut);
|
|
|
|
p.setAddress(remoteHost);
|
|
|
|
p.setPort(remotePort);
|
|
|
|
|
|
|
|
try {
|
|
|
|
socket.send(p);
|
|
|
|
}
|
|
|
|
catch (IOException e) {
|
2015-03-17 21:19:06 +01:00
|
|
|
LOGGER.log(Level.WARNING, "exception", e);
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tiles[i][j] = pixels;
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (InterruptedException e) {
|
2015-03-17 21:19:06 +01:00
|
|
|
LOGGER.log(Level.WARNING, "exception", e);
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trace = (System.currentTimeMillis() - trace);
|
2014-02-19 10:38:30 +01:00
|
|
|
LOGGER.fine("Loop Time:" + trace);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
if (trace < 500) {
|
|
|
|
try {
|
|
|
|
Thread.sleep(500 - trace);
|
|
|
|
}
|
|
|
|
catch (InterruptedException e) {
|
2015-03-17 21:19:06 +01:00
|
|
|
LOGGER.log(Level.WARNING, "exception", e);
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-11 16:16:41 +01:00
|
|
|
@Override
|
2014-02-17 23:58:40 +01:00
|
|
|
public void run() {
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-29 12:15:32 +02:00
|
|
|
* Set Transmit Enabled/Disabled.
|
2014-02-17 23:58:40 +01:00
|
|
|
*
|
|
|
|
* @param transmit boolean Enabled/Disabled
|
|
|
|
*/
|
|
|
|
public void setTransmit(boolean transmit) {
|
|
|
|
this.transmit = transmit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-29 12:15:32 +02:00
|
|
|
* Get the encoder used to encode Images Tiles.
|
2014-02-17 23:58:40 +01:00
|
|
|
*
|
|
|
|
* @return encoder
|
|
|
|
*/
|
|
|
|
public ImageEncoder getEncoder() {
|
|
|
|
return encoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-29 12:15:32 +02:00
|
|
|
* Set the encoder used to encode Image Tiles.
|
2014-02-17 23:58:40 +01:00
|
|
|
*
|
|
|
|
* @param encoder encoder
|
|
|
|
*/
|
|
|
|
public void setEncoder(ImageEncoder encoder) {
|
|
|
|
this.encoder = encoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-29 12:15:32 +02:00
|
|
|
* Stops Transmitter.
|
2014-02-17 23:58:40 +01:00
|
|
|
*/
|
|
|
|
public void stop() {
|
|
|
|
this.transmit = false;
|
|
|
|
this.on = false;
|
|
|
|
socket.close();
|
|
|
|
}
|
|
|
|
}
|