00001
package org.nees.buffalo.video;
00002
00003
import java.awt.BorderLayout;
00004
import java.util.Date;
00005
00006
import javax.swing.ImageIcon;
00007
import javax.swing.JFrame;
00008
import javax.swing.JLabel;
00009
00010
import com.rbnb.sapi.ChannelMap;
00011
import com.rbnb.sapi.SAPIException;
00012
import com.rbnb.sapi.Sink;
00013
00014
import com.rbnb.utility.ArgHandler;
00015
import com.rbnb.utility.RBNBProcess;
00016
00017
00018
00019
00020
00039 public class AxisSink {
00040
00041
private final static String DEFAULT_HOST =
"localhost:3333";
00042
private final static String DEFAULT_SINK_NAME =
"AxisVideoSink";
00043
00044
private String rbnbHostName = DEFAULT_HOST;
00045
private String rbnbSinkName = DEFAULT_SINK_NAME;
00046
private String sourceName = null;
00047
private int timeoutInterval = 1000;
00048
private int retryCount = 30;
00049
00050
private void printUsage() {
00051 System.out.println(
"AxisSink: usage is...");
00052 System.out.print(
"AxisSink ");
00053 System.out.print(
"[-h server_hostname *localhost:3333] ");
00054 System.out.print(
"[-n sink_name *AxisVideoSink] ");
00055 System.out.print(
"[-s source_name (required) Example: \"AxisVideoSource/camera\"] ");
00056 System.out.println();
00057 }
00058
00062
public AxisSink(String[] args) {
00063
00064
00065
try {
00066 ArgHandler ah=
new ArgHandler(args);
00067
if (ah.checkFlag(
'h')) {
00068 String a=ah.getOption(
'h');
00069
if (a!=null) rbnbHostName=a;
00070 }
00071
if (ah.checkFlag(
'n')) {
00072 String a=ah.getOption(
'n');
00073
if (a!=null) rbnbSinkName=a;
00074 }
00075
if (ah.checkFlag(
's')) {
00076 String a=ah.getOption(
's');
00077
if (a!=null) sourceName=a;
00078 }
00079 }
catch (Exception e) {
00080 System.err.println(
"AxisSink argument exception "+e.getMessage());
00081 e.printStackTrace();
00082 RBNBProcess.exit(0);
00083 }
00084
00085
if (sourceName == null)
00086 {
00087 printUsage();
00088 System.out.println(
"Source name (-s) is required. Example: AxisVideoSource/camera");
00089 RBNBProcess.exit(0);
00090 }
00091
00092 System.out.println(
"Starting AxisSink on " + rbnbHostName +
" as " + rbnbSinkName);
00093 System.out.println(
" Watching for data (with Monitor) from " + sourceName);
00094
00095
try {
00096
00097 JFrame frame =
new JFrame(
"SwingApplication");
00098 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
00099 JLabel image =
new JLabel();
00100 JLabel label =
new JLabel();
00101 frame.getContentPane().add(image, BorderLayout.CENTER);
00102 frame.getContentPane().add(label, BorderLayout.SOUTH);
00103 frame.pack();
00104 frame.setVisible(
true);
00105
00106 Sink mySink =
new Sink();
00107 mySink.OpenRBNBConnection(rbnbHostName, rbnbSinkName);
00108
00109 ChannelMap reqmap =
new ChannelMap();
00110
int inputChannelIndex = reqmap.Add(sourceName);
00111
00112 mySink.Monitor(reqmap, 0);
00113
00114 byte[] imageData;
00115
00116
int numberRetries = 0;
00117
int imageIndex = 0;
00118
double startTime = 0;
00119
double oldStartTime = 0;
00120
double durationTime = 0;
00121
double averageInputSampleRate = 30;
00122
while (
true) {
00123
00124 ChannelMap getmap = mySink.Fetch(timeoutInterval);
00125
00126
if (getmap.GetIfFetchTimedOut()) {
00127
if (++numberRetries == retryCount) {
00128 System.out.println(
"Failed to get any data after "
00129 + retryCount +
" retries.");
00130
break;
00131 }
else {
00132 System.out.println(
"Data request timed out (" +
00133 numberRetries +
" out of " + retryCount +
"), retrying.");
00134
continue;
00135 }
00136 }
00137
00138 numberRetries = 0;
00139 oldStartTime = startTime;
00140
00141 imageData = getmap.GetData(inputChannelIndex);
00142 startTime = getmap.GetTimeStart(inputChannelIndex);
00143 durationTime = getmap.GetTimeDuration(inputChannelIndex);
00144
00145
double sampleRate;
00146
if (imageIndex > 0) {
00147 sampleRate = 1/(startTime-oldStartTime);
00148 }
else {
00149 sampleRate = 30;
00150 }
00151
00152
if (Double.isInfinite(sampleRate)) {
00153 sampleRate = averageInputSampleRate;
00154 }
00155
00156 averageInputSampleRate = averageInputSampleRate*0.995 + sampleRate*0.005;
00157
00158
if (imageIndex % 30 == 0) System.out.print(
"Receiving " + ((
double)Math.round(averageInputSampleRate*10))/10 +
" fps \r");
00159
00160 image.setIcon(
new ImageIcon(imageData));
00161 label.setText(
new Date((
long)(startTime*1000)).toString());
00162
if (imageIndex == 0) frame.pack();
00163 frame.repaint();
00164
00165 imageIndex++;
00166 }
00167
00168 mySink.CloseRBNBConnection();
00169
00170 }
catch (SAPIException e) {
00171 e.printStackTrace();
00172 }
00173
00174 System.exit(-1);
00175 }
00176
00181 public static void main(String[] args) {
00182
new AxisSink(args);
00183 }
00184
00185 }