public class ImageServer
extends java.lang.Object
HTTP multipart/x-mixed-replace image server.
The source frames are provided via the hook updateImage(java.awt.image.BufferedImage)
, which
also sets the width and height. The compression type, compression quality
(if appliccable), and maximum served frames per second are controlled by URL
query parameters "fmt" (string, e.g. "jpeg" or "png"), "fps" (float), and
"qual" (int, 0=min, 100=max), respectively. They default to defFmt
, defFPS
, and defQual
if the query params are not
given.
Key and mouse events are collected from the client and made available in
image coordinates via the hooks handleKey(char)
and handleMouse(int, int, int, int)
.
Tested with Chrome and Safari. Firefox might or might not work (Mozilla bug 625012). On any browser, non-alphanumeric keycodes may differ from Java std.
The server knows about the following request paths:
Once the server is running, you can connect to it from a remote host by bringing up e.g. Google Chrome and browsing to the url
http://ADDR:PORT/server.htmlwhere ADDR is the IP address and PORT the listening port of the server. You can further control the image format, framerate, and quality like this:
http://ADDR:PORT/server.html?fmt=jpg&fps=10&qual=95
Modifier and Type | Class and Description |
---|---|
protected class |
ImageServer.ExchangeState
Wraps an HttpExchange with some derived values.
|
Modifier and Type | Field and Description |
---|---|
boolean |
dbg
Debug next server frame.
|
static java.lang.String |
DEF_FMT
Default
defFmt . |
static float |
DEF_FPS
Default
defFPS . |
static int |
DEF_PORT
Default TCP listen port.
|
static int |
DEF_QUAL
Default
defQual . |
static int |
DEF_STOP_SEC
Default
stop(int) delay seconds. |
java.lang.String |
defFmt
Default ImageIO format name.
|
float |
defFPS
Default max framerate.
|
int |
defQual
Default image quality in the range 0 (min) to 100 (max).
|
static int |
EVENT_FLAG_ALTKEY
Event flags for
handleMouse(int, int, int, int) . |
static int |
EVENT_FLAG_CTRLKEY
Event flags for
handleMouse(int, int, int, int) . |
static int |
EVENT_FLAG_LBUTTON
Event flags for
handleMouse(int, int, int, int) . |
static int |
EVENT_FLAG_MBUTTON
Event flags for
handleMouse(int, int, int, int) . |
static int |
EVENT_FLAG_RBUTTON
Event flags for
handleMouse(int, int, int, int) . |
static int |
EVENT_FLAG_SHIFTKEY
Event flags for
handleMouse(int, int, int, int) . |
static int |
EVENT_LBUTTONDBLCLK
Event types for
handleMouse(int, int, int, int) . |
static int |
EVENT_LBUTTONDOWN
Event types for
handleMouse(int, int, int, int) . |
static int |
EVENT_LBUTTONUP
Event types for
handleMouse(int, int, int, int) . |
static int |
EVENT_MBUTTONDBLCLK
Event types for
handleMouse(int, int, int, int) . |
static int |
EVENT_MBUTTONDOWN
Event types for
handleMouse(int, int, int, int) . |
static int |
EVENT_MBUTTONUP
Event types for
handleMouse(int, int, int, int) . |
static int |
EVENT_MOUSEMOVE
Event types for
handleMouse(int, int, int, int) . |
static int |
EVENT_RBUTTONDBLCLK
Event types for
handleMouse(int, int, int, int) . |
static int |
EVENT_RBUTTONDOWN
Event types for
handleMouse(int, int, int, int) . |
static int |
EVENT_RBUTTONUP
Event types for
handleMouse(int, int, int, int) . |
protected java.util.concurrent.ExecutorService |
executor
The internal transaction executor.
|
static int |
HTTP_FORBIDDEN
HTTP response codes
|
static int |
HTTP_NOT_FOUND
HTTP response codes
|
static int |
HTTP_OK
HTTP response codes
|
protected int |
imageClients
Number of clients to which we're currently serving images.
|
static java.lang.String |
MIME_BOUNDARY
MIME multipart boundary
|
java.io.PrintStream |
msgStream
Strems for messages and warnings, or null to disable.
|
static java.text.DecimalFormat |
NF
Number format for printouts.
|
protected boolean |
oneClient
Whether to limit serving images to one client.
|
int |
port
Server TCP listen port.
|
protected com.sun.net.httpserver.HttpServer |
server
The internal server object.
|
static java.lang.String |
SERVER_HTML
Server html file.
|
boolean |
spewMouseMoves
Whether to output lots of debug messages for mousemove events.
|
boolean |
spewStackTraces
Whether to dump stack traces on server errors.
|
java.io.PrintStream |
warnStream
Strems for messages and warnings, or null to disable.
|
Constructor and Description |
---|
ImageServer()
Creates server using
DEF_PORT . |
ImageServer(int port)
Creates server.
|
Modifier and Type | Method and Description |
---|---|
protected java.io.InputStream |
getServerHTML()
Hook to get the html the server sends to the client.
|
static java.net.InetAddress[] |
getServerIPs()
Get the server's non-local IP address(es).
|
protected void |
handleEcho(ImageServer.ExchangeState xs)
Serves debug echo info.
|
protected void |
handleExchange(com.sun.net.httpserver.HttpExchange xch)
Dispatches HTTP exchange to one of the handle*(ExchangeState) methods.
|
protected void |
handleImage(ImageServer.ExchangeState xs)
Serves images.
|
protected void |
handleKey(char c)
Hook to handle key events.
|
protected void |
handleKey(ImageServer.ExchangeState xs)
Handles key event requests.
|
protected void |
handleMouse(ImageServer.ExchangeState xs)
Handles mouse event requests.
|
protected void |
handleMouse(int t,
int x,
int y,
int m)
Hook to handle mouse events.
|
protected void |
handleServer(ImageServer.ExchangeState xs)
Serves
SERVER_HTML using getServerHTML() . |
protected void |
handleUnknown(ImageServer.ExchangeState xs)
Handles unknown request paths.
|
static void |
main(java.lang.String[] argv)
Starts a test server.
|
protected void |
msg(java.lang.String m)
Display a message to
msgStream , if any. |
protected static long |
nowMS()
Returns current time in milliseconds.
|
void |
start()
Starts server.
|
void |
stop()
stop(int) delaying DEF_STOP_SEC . |
void |
stop(int delay)
|
protected java.awt.image.BufferedImage |
updateImage(java.awt.image.BufferedImage bi)
Hook called to update the server image.
|
protected void |
warn(java.lang.String m)
Display a warning to
warnStream , if any. |
public static final int DEF_PORT
public static final java.lang.String DEF_FMT
defFmt
.public static final float DEF_FPS
defFPS
.public static final int DEF_QUAL
defQual
.public static final int DEF_STOP_SEC
stop(int)
delay seconds.public static final java.lang.String SERVER_HTML
public static final int HTTP_OK
public static final int HTTP_NOT_FOUND
public static final int HTTP_FORBIDDEN
public static final java.lang.String MIME_BOUNDARY
public static final int EVENT_MOUSEMOVE
Event types for handleMouse(int, int, int, int)
.
Compatible with OpenCV event types.
public static final int EVENT_LBUTTONDOWN
Event types for handleMouse(int, int, int, int)
.
Compatible with OpenCV event types.
public static final int EVENT_RBUTTONDOWN
Event types for handleMouse(int, int, int, int)
.
Compatible with OpenCV event types.
public static final int EVENT_MBUTTONDOWN
Event types for handleMouse(int, int, int, int)
.
Compatible with OpenCV event types.
public static final int EVENT_LBUTTONUP
Event types for handleMouse(int, int, int, int)
.
Compatible with OpenCV event types.
public static final int EVENT_RBUTTONUP
Event types for handleMouse(int, int, int, int)
.
Compatible with OpenCV event types.
public static final int EVENT_MBUTTONUP
Event types for handleMouse(int, int, int, int)
.
Compatible with OpenCV event types.
public static final int EVENT_LBUTTONDBLCLK
Event types for handleMouse(int, int, int, int)
.
Compatible with OpenCV event types.
public static final int EVENT_RBUTTONDBLCLK
Event types for handleMouse(int, int, int, int)
.
Compatible with OpenCV event types.
public static final int EVENT_MBUTTONDBLCLK
Event types for handleMouse(int, int, int, int)
.
Compatible with OpenCV event types.
public static final int EVENT_FLAG_LBUTTON
Event flags for handleMouse(int, int, int, int)
.
Compatible with OpenCV event flags.
public static final int EVENT_FLAG_RBUTTON
Event flags for handleMouse(int, int, int, int)
.
Compatible with OpenCV event flags.
public static final int EVENT_FLAG_MBUTTON
Event flags for handleMouse(int, int, int, int)
.
Compatible with OpenCV event flags.
public static final int EVENT_FLAG_CTRLKEY
Event flags for handleMouse(int, int, int, int)
.
Compatible with OpenCV event flags.
public static final int EVENT_FLAG_SHIFTKEY
Event flags for handleMouse(int, int, int, int)
.
Compatible with OpenCV event flags.
public static final int EVENT_FLAG_ALTKEY
Event flags for handleMouse(int, int, int, int)
.
Compatible with OpenCV event flags.
public static final java.text.DecimalFormat NF
public final int port
public volatile java.lang.String defFmt
public volatile float defFPS
public volatile int defQual
public volatile boolean dbg
public volatile java.io.PrintStream msgStream
public volatile java.io.PrintStream warnStream
public volatile boolean spewMouseMoves
public volatile boolean spewStackTraces
protected volatile int imageClients
protected volatile boolean oneClient
protected final com.sun.net.httpserver.HttpServer server
protected final java.util.concurrent.ExecutorService executor
public ImageServer(int port) throws java.io.IOException
Creates server.
You may configure public fields before calling start()
.
port
- the TCP listen portjava.io.IOException
public ImageServer() throws java.io.IOException
DEF_PORT
.java.io.IOException
protected void handleExchange(com.sun.net.httpserver.HttpExchange xch) throws java.io.IOException
java.io.IOException
protected void handleEcho(ImageServer.ExchangeState xs) throws java.io.IOException
java.io.IOException
protected void handleServer(ImageServer.ExchangeState xs) throws java.io.IOException
SERVER_HTML
using getServerHTML()
.java.io.IOException
protected java.io.InputStream getServerHTML() throws java.io.IOException
Hook to get the html the server sends to the client.
Default impl uses getResourceAsStream()
to open and return
SERVER_HTML
.
java.io.IOException
protected void handleImage(ImageServer.ExchangeState xs) throws java.lang.Exception
java.lang.Exception
protected java.awt.image.BufferedImage updateImage(java.awt.image.BufferedImage bi)
Hook called to update the server image.
bi
- the prior image, if any, else nullWill be called by a server exchange handling thread while synchronized
on this ImageServer
instance.
Default impl does nothing. Subclasses can override this to provide images. To avoid creating a lot of memory garbage it is recommended to create the image once and then to update it in place on subsequent frames.
protected void handleMouse(ImageServer.ExchangeState xs)
Handles mouse event requests.
Parses the event parameters and then calls handleMouse(int,
int, int, int)
which can be overridden as desired.
protected void handleMouse(int t, int x, int y, int m)
Hook to handle mouse events.
t
- one of the EVENT_* constantsx
- the x pixel coordinate of the mouse eventy
- the y pixel coordinate of the mouse eventm
- bitmask of the EVENT_FLAG_* constants
This is called when the client notifies the server that a mouse event has occured over the image.
Default impl does nothing. Subclasses can override this to take action on mouse events over the client image.
protected void handleKey(ImageServer.ExchangeState xs)
Handles key event requests.
Parses the event parameters and then calls handleKey(char)
which can be overridden as desired.
protected void handleKey(char c)
Hook to handle key events.
c
- the key character
This is called when the client notifies the server that a key event has occured over the image.
Default impl does nothing. Subclasses can override this to take action on key events over the client image.
protected void handleUnknown(ImageServer.ExchangeState xs) throws java.io.IOException
java.io.IOException
protected static long nowMS()
protected void msg(java.lang.String m)
msgStream
, if any.protected void warn(java.lang.String m)
warnStream
, if any.public void start() throws java.io.IOException
java.io.IOException
public void stop(int delay)
delay
- seconds to wait for any ongoing exchanges to finish
Call this to bring the server down.
public void stop()
stop(int)
delaying DEF_STOP_SEC
.public static java.net.InetAddress[] getServerIPs() throws java.io.IOException
java.io.IOException
public static void main(java.lang.String[] argv) throws java.io.IOException
Starts a test server.
argv
- up to one arg is accepted giving the TCP listen port
Serves synthetic images marked with their frame number.
java.io.IOException