00001 /* --------------------------------------------------------------------------- 00002 Phission : 00003 Realtime Vision Processing System 00004 00005 Copyright (C) 2003 Philip D.S. Thoren (pthoren@cs.uml.edu) 00006 University of Massachusetts at Lowell, 00007 Laboratory for Artificial Intelligence and Robotics 00008 00009 This file is part of Phission. 00010 00011 ---------------------------------------------------------------------------*/ 00012 /* Phission must be first for some reason */ 00013 #include <NetLiveSourceTest.h> 00014 #include <phission.h> 00015 #include <signal.h> 00016 00017 /* ------------------------------------------------------------------------ */ 00018 int glbl_disable_displays = 0; 00019 int glbl_signal_caught = 0; 00020 00021 /* ----------------------------------------------------------------------- */ 00022 /* ----------------------------------------------------------------------- */ 00023 void signal_cleanup(int signal ) 00024 { 00025 phFUNCTION("signal_cleanup") 00026 00027 glbl_signal_caught = 1; 00028 00029 exit(1); 00030 } 00031 00032 /* ------------------------------------------------------------------------ */ 00033 int usage () 00034 { 00035 printf("\n\nUsage:\n"); 00036 printf("\t--help Display usage\n"); 00037 printf("\t--port <port#####> The port to bind the image server to\n"); 00038 printf("\t--nodisplay Disable the allocation, opening or any use of a display.\n"); 00039 printf("\t--test <value> Sleep 'value' seconds and then close test\n"); 00040 printf("\n\n"); 00041 exit(1); 00042 } 00043 00044 /* ------------------------------------------------------------------------ */ 00045 int main(int argc, char *argv[] ) 00046 { 00047 phFUNCTION("main") 00048 00049 unsigned int i = 0; 00050 00051 char *capture_path = NULL; 00052 int channel = 0; 00053 00054 unsigned int nDisplays = 1; 00055 int displaysOpen = 1; 00056 int basePortNum = 22345; 00057 00058 /* Zlib supports any format; the following is a sample */ 00059 //uint32_t outputFormat = phImageYUV9 | phImageZlib; 00060 //uint32_t outputFormat = phImageGREY8 | phImageZlib; 00061 //uint32_t outputFormat = phImageRGB24 | phImageZlib; 00062 00063 /* Valid JPEG supported format combinations: */ 00064 //uint32_t outputFormat = phImageRGB24; 00065 uint32_t outputFormat = phImageRGB24 | phImageJPEG; 00066 //uint32_t outputFormat = phImageGREY8 | phImageJPEG; 00067 //uint32_t outputFormat = phImageRGB24 | phImageJPEG | phImageZlib; 00068 //uint32_t outputFormat = phImageGREY8 | phImageJPEG | phImageZlib; 00069 00070 /* Invalid format; Contains phImageRGBA32 which isn't supported by the 00071 * JPEG code. */ 00072 //uint32_t outputFormat = phImageRGBA32 | phImageJPEG; 00073 00074 00075 uint32_t width = 320; 00076 uint32_t height = 240; 00077 phDisplayInterface **display = NULL; 00078 00079 /* Used for verifing transport */ 00080 int verify = 0; 00081 uint8_t *vdata = NULL; 00082 uint32_t vsize = width * height * 3; /* RGBish */ 00083 uint8_t x = 0; 00084 phImage vImage; 00085 00086 #ifdef WIN32 00087 VFWSource *capture = new VFWSource(); 00088 #else 00089 V4LCapture *capture = new V4LCapture(); 00090 #endif 00091 00092 /* Utility class to ease the starting and stopping of displays, captures and 00093 * pipelines */ 00094 phSystem net_system; 00095 00096 /* Remove the code below when using this code as an example. 00097 * 00098 * This just checks whether "--test" has been specified with 00099 * a time value argument. It's for testing all the examples 00100 * without the need for human intervention. */ 00101 int test = 0; 00102 00103 phArgTable *arg_parser = new phArgTable(); 00104 00105 /* Setup and parse all the arguments */ 00106 rc = arg_parser->add("--test", &test, phARG_INT); 00107 phCHECK_RC(rc,NULL,"arg_parser->add"); 00108 rc = arg_parser->add("--verify", &verify, phARG_BOOL); 00109 phCHECK_RC(rc,NULL,"arg_parser->add"); 00110 rc = arg_parser->add("--port", &basePortNum, phARG_INT); 00111 phCHECK_RC(rc,NULL,"arg_parser->add"); 00112 rc = arg_parser->add("--help",(void *)&usage, phARG_FUNC); 00113 phCHECK_RC(rc,NULL,"arg_parser->add"); 00114 00115 rc = arg_parser->add("--path",&capture_path,phARG_CHAR); 00116 phCHECK_RC(rc,NULL,"arg_parser->add"); 00117 00118 rc = arg_parser->add("--channel",&channel,phARG_INT); 00119 phCHECK_RC(rc,NULL,"arg_parser->add"); 00120 00121 rc = arg_parser->parse(argc,argv); 00122 phCHECK_RC(rc,NULL,"arg_parser->parse"); 00123 00124 if (!((basePortNum > 1024) && (basePortNum < 66534))) 00125 { 00126 basePortNum = 22345; 00127 } 00128 00129 if (glbl_disable_displays) nDisplays = 0; 00130 00131 /* Set up a signal handler to shut the net_system down properly */ 00132 for (i = 0; i < 17; i++ ) 00133 { 00134 signal(i,signal_cleanup); 00135 } 00136 // signal(SIGINT,signal_cleanup); 00137 00138 if (verify == 0) 00139 { 00140 /* Set up the capture device */ 00141 capture->set(width,height,capture_path); 00142 00143 #if 0 00144 capture->setChannel(channel); 00145 capture->setColour(65000); 00146 capture->setHue(65500); 00147 capture->setContrast(31000); 00148 capture->setBrightness(29000); 00149 #else 00150 capture->setChannel(channel); 00151 capture->setSize(width,height); 00152 capture->setColour(31000); 00153 capture->setHue(33000); 00154 capture->setContrast(22000); 00155 capture->setBrightness(32000); 00156 #endif 00157 rc = net_system.addCapture(capture); 00158 phPRINT_RC(rc,NULL,"net_system.addCapture(capture)"); 00159 } 00160 else if (verify == 1) 00161 { 00162 int direction = 0; 00163 00164 vdata = (uint8_t *)phCalloc(1,vsize); 00165 phCHECK_NULLPTR(vdata,"phCalloc","phCalloc failed." ); 00166 00167 for (i = 0; i < vsize; i++) 00168 { 00169 vdata[i] = x; 00170 00171 if ((x == 255) && (direction == 0)) 00172 { 00173 direction = -1; 00174 } 00175 else if ((x == 0) && (direction == -1)) 00176 { 00177 direction = 0; 00178 } 00179 00180 if (direction == -1) 00181 { 00182 x--; 00183 } 00184 else 00185 { 00186 x++; 00187 } 00188 00189 if (((i + 1) % width) == 0) x = 0; 00190 } 00191 } 00192 00193 /* Set up the display stuff */ 00194 display = new phDisplayInterface *[nDisplays]; 00195 phCHECK_NULLPTR(display,"new","new phDisplayInterface *[nDisplays];"); 00196 00197 for (i = 0; i < nDisplays; i++) 00198 { 00199 NetDisplay *n = new NetDisplay(width,height); 00200 display[i] = (phDisplayInterface *)n; 00201 if (verify == 0) 00202 { 00203 display[i]->setLiveSourceInput(capture->getLiveSourceOutput()); 00204 } 00205 else if (verify == 1) 00206 { 00207 display[i]->setLiveSourceInput(&vImage); 00208 } 00209 00210 phPROGRESS("Setting display[%d] to listen on port %d\n", 00211 i, basePortNum + i); 00212 n->setPort(basePortNum + i); 00213 n->setFormat(outputFormat); 00214 00215 rc = net_system.addDisplay(display[i]); 00216 phPRINT_RC(rc,NULL,"net_system.addDisplay(display[i:%u])",i); 00217 } 00218 00219 00220 /* startup the net_system */ 00221 rc = net_system.startup(); 00222 phPRINT_RC(rc,NULL,"net_system.startup()"); 00223 00224 displaysOpen = 1; 00225 while ((displaysOpen) && (glbl_signal_caught == 0)) 00226 { 00227 if (verify == 1) 00228 { 00229 rc = vImage.setImage(width,height,phImageRGB24,vsize,vdata); 00230 phPRINT_RC(rc,NULL,"vImage.setImage()"); 00231 } 00232 /* Sleep a while, don't loop tightly */ 00233 00234 phMSleep(100); 00235 00236 /* Yielding is optional. This gives up the thread's timeslice 00237 * to prevent slow response in other threads. It consumes more 00238 * CPU cycles than sleeping. Use it instead of sleeping if 00239 * this loop is processing anything */ 00240 00241 /* phYield(); */ 00242 00243 if (nDisplays > 0) 00244 { 00245 displaysOpen = 0; 00246 for (i = 0; (i < nDisplays) && (displaysOpen == 0); i++) 00247 { 00248 if (display[i]->isOpen() == 1) 00249 { 00250 displaysOpen = 1; 00251 } 00252 } 00253 } 00254 00255 /* Remove this if block when using this code as an example */ 00256 /* Set the loop control value to end the loop when testing */ 00257 if (test > 0) 00258 { 00259 displaysOpen = 0; 00260 phSleep(test); /* test's value should be a time (in secs) value > 0*/ 00261 } 00262 } 00263 00264 glbl_signal_caught = 0; 00265 00266 rc = net_system.shutdown(); 00267 phPRINT_RC(rc,NULL,"net_system.shutdown()"); 00268 00269 error: 00270 phFree(vdata); 00271 vsize = 0; 00272 00273 phDelete(capture); 00274 00275 for (i = 0; (i < nDisplays) && (display != NULL); i++) 00276 { 00277 phDelete(display[i]); 00278 } 00279 00280 phDeleteArray(display); 00281 00282 phFree(capture_path); 00283 phDelete(arg_parser); 00284 00285 return phSUCCESS; 00286 }
Copyright (C) 2002 - 2007 |
Philip D.S. Thoren ( pthoren@users.sourceforge.net ) University Of Massachusetts at Lowell Robotics Lab |