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