00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include "DSVL.h"
00043
00044 #include <AR/video.h>
00045 #include <stdlib.h>
00046 #include "comutil.h"
00047
00048
00049
00050 struct _AR2VideoParamT {
00051 DSVL_VideoSource *graphManager;
00052 MemoryBufferHandle g_Handle;
00053 bool bufferCheckedOut;
00054 __int64 g_Timestamp;
00055
00056
00057 };
00058
00059
00060
00061 static AR2VideoParamT *gVid = NULL;
00062
00063 #ifdef FLIPPED // compatibility with videoLinux*
00064 static const bool FLIPPED_defined = true;
00065 #else
00066 static const bool FLIPPED_defined = false;
00067 #endif
00068 const long frame_timeout_ms = 0L;
00069
00070
00071
00072
00073 int arVideoDispOption(void)
00074 {
00075 return (ar2VideoDispOption());
00076 }
00077
00078 int arVideoOpen(char *config)
00079 {
00080 if (gVid != NULL) {
00081 fprintf(stderr, "arVideoOpen(): Error, device is already open.\n");
00082 return (-1);
00083 }
00084 gVid = ar2VideoOpen(config);
00085 if (gVid == NULL) return (-1);
00086
00087 return (0);
00088 }
00089
00090 int arVideoClose(void)
00091 {
00092 int result;
00093
00094 if (gVid == NULL) return (-1);
00095
00096 result = ar2VideoClose(gVid);
00097 gVid = NULL;
00098 return (result);
00099 }
00100
00101 int arVideoInqSize(int *x, int *y)
00102 {
00103 if (gVid == NULL) return (-1);
00104
00105 return (ar2VideoInqSize(gVid, x, y));
00106 }
00107
00108 ARUint8 *arVideoGetImage(void)
00109 {
00110 if (gVid == NULL) return (NULL);
00111
00112 return (ar2VideoGetImage(gVid));
00113 }
00114
00115 int arVideoCapStart(void)
00116 {
00117 if (gVid == NULL) return (-1);
00118
00119 return (ar2VideoCapStart(gVid));
00120 }
00121
00122 int arVideoCapStop(void)
00123 {
00124 if (gVid == NULL) return (-1);
00125
00126 return (ar2VideoCapStop(gVid));
00127 }
00128
00129 int arVideoCapNext(void)
00130 {
00131 if (gVid == NULL) return (-1);
00132
00133 return (ar2VideoCapNext(gVid));
00134 }
00135
00136
00137
00138 int ar2VideoDispOption(void)
00139 {
00140 printf("parameter is a file name (e.g. 'config.XML') conforming to the DSVideoLib XML Schema (DsVideoLib.xsd).\n");
00141 return (0);
00142 }
00143
00144 AR2VideoParamT *ar2VideoOpen(char *config)
00145 {
00146 AR2VideoParamT *vid = NULL;
00147 char config_default[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dsvl_input><camera show_format_dialog=\"true\" friendly_name=\"\"><pixel_format><RGB32 flip_h=\"false\" flip_v=\"true\"/></pixel_format></camera></dsvl_input>";
00148
00149
00150
00151 arMalloc(vid, AR2VideoParamT, 1);
00152 memset(vid, 0, sizeof(AR2VideoParamT));
00153
00154 CoInitialize(NULL);
00155
00156 vid->graphManager = new DSVL_VideoSource();
00157 if (!config) {
00158 config = getenv("ARTOOLKIT_CONFIG");
00159 if (!config) config = config_default;
00160 if (FAILED(vid->graphManager->BuildGraphFromXMLString(config))) return(NULL);
00161 } else {
00162 if (!config[0]) {
00163 if (FAILED(vid->graphManager->BuildGraphFromXMLString(config_default))) return(NULL);
00164 } else if (strncmp(config, "<?xml", 5) == 0) {
00165 if (FAILED(vid->graphManager->BuildGraphFromXMLString(config))) return(NULL);
00166 } else {
00167 if (FAILED(vid->graphManager->BuildGraphFromXMLFile(config))) return(NULL);
00168 }
00169 }
00170 if (FAILED(vid->graphManager->EnableMemoryBuffer())) return(NULL);
00171
00172 return (vid);
00173 }
00174
00175
00176 int ar2VideoClose(AR2VideoParamT *vid)
00177 {
00178 int _ret = -1;
00179
00180 if (vid == NULL) return (_ret);
00181
00182 if (vid->graphManager != NULL) {
00183
00184 if (vid->bufferCheckedOut)
00185 vid->graphManager->CheckinMemoryBuffer(vid->g_Handle, true);
00186
00187 vid->graphManager->Stop();
00188 delete vid->graphManager;
00189 vid->graphManager = NULL;
00190
00191 _ret = 0;
00192 }
00193
00194 free(vid);
00195
00196
00197 vid = NULL;
00198
00199
00200 CoUninitialize();
00201
00202 return(_ret);
00203 }
00204
00205 unsigned char *ar2VideoGetImage(AR2VideoParamT *vid)
00206 {
00207 DWORD wait_result;
00208 unsigned char* pixelBuffer;
00209
00210 if (vid == NULL) return (NULL);
00211 if (vid->graphManager == NULL) return (NULL);
00212
00213 if (vid->bufferCheckedOut) {
00214 if (FAILED(vid->graphManager->CheckinMemoryBuffer(vid->g_Handle))) return (NULL);
00215 vid->bufferCheckedOut = false;
00216 }
00217 wait_result = vid->graphManager->WaitForNextSample(frame_timeout_ms);
00218 if (wait_result == WAIT_OBJECT_0) {
00219 if (FAILED(vid->graphManager->CheckoutMemoryBuffer(&(vid->g_Handle), &pixelBuffer, NULL, NULL, NULL, &(vid->g_Timestamp)))) return(NULL);
00220 vid->bufferCheckedOut = true;
00221 return (pixelBuffer);
00222 }
00223
00224 return(NULL);
00225 }
00226
00227 int ar2VideoCapStart(AR2VideoParamT *vid)
00228 {
00229 if (vid == NULL) return (-1);
00230 if (vid->graphManager == NULL) return (-1);
00231
00232 if (FAILED(vid->graphManager->Run())) return (-1);
00233 return (0);
00234 }
00235
00236 int ar2VideoCapStop(AR2VideoParamT *vid)
00237 {
00238 if (vid == NULL) return (-1);
00239 if (vid->graphManager == NULL) return (-1);
00240
00241 if (vid->bufferCheckedOut) {
00242 if (FAILED(vid->graphManager->CheckinMemoryBuffer(vid->g_Handle, true))) return (-1);
00243 vid->bufferCheckedOut = false;
00244 }
00245
00246
00247
00248
00249
00250
00251 return (0);
00252 }
00253
00254 int ar2VideoCapNext(AR2VideoParamT *vid)
00255 {
00256 if (vid == NULL) return (-1);
00257 if (vid->graphManager == NULL) return (-1);
00258
00259 if (vid->bufferCheckedOut) {
00260 if (FAILED(vid->graphManager->CheckinMemoryBuffer(vid->g_Handle, true))) return (-1);
00261 vid->bufferCheckedOut = false;
00262 }
00263 return (0);
00264 }
00265
00266 int ar2VideoInqSize(AR2VideoParamT *vid, int *x, int *y)
00267 {
00268 if (vid == NULL) return (-1);
00269 if (vid->graphManager == NULL) return(-1);
00270
00271 long frame_width;
00272
00273 long frame_height;
00274
00275 vid->graphManager->GetCurrentMediaFormat(&frame_width, &frame_height,NULL,NULL);
00276
00277 *x = (int) frame_width;
00278
00279 *y = (int) frame_height;
00280
00281
00282 return (0);
00283 }
00284
00285
00286
00287 int ar2VideoInqFlipping(AR2VideoParamT *vid, int *flipH, int *flipV)
00288 {
00289
00290
00291
00292
00293
00294 return (-1);
00295 }
00296
00297 int ar2VideoInqFreq(AR2VideoParamT *vid, float *fps)
00298 {
00299 if (vid == NULL) return (-1);
00300 if (vid->graphManager == NULL) return(-1);
00301
00302 double frames_per_second;
00303
00304 vid->graphManager->GetCurrentMediaFormat(NULL,NULL,&frames_per_second,NULL);
00305
00306 *fps = (float) frames_per_second;
00307
00308
00309 return (0);
00310 }
00311
00312 unsigned char *ar2VideoLockBuffer(AR2VideoParamT *vid, MemoryBufferHandle* pHandle)
00313 {
00314 unsigned char *pixelBuffer;
00315
00316 if (vid == NULL) return (NULL);
00317 if (vid->graphManager == NULL) return (NULL);
00318
00319 if (FAILED(vid->graphManager->CheckoutMemoryBuffer(pHandle, &pixelBuffer))) return (NULL);
00320 vid->bufferCheckedOut = true;
00321
00322 return (pixelBuffer);
00323 }
00324
00325 int ar2VideoUnlockBuffer(AR2VideoParamT *vid, MemoryBufferHandle Handle)
00326 {
00327 if (vid == NULL) return (-1);
00328 if (vid->graphManager == NULL) return(-1);
00329
00330 if (FAILED(vid->graphManager->CheckinMemoryBuffer(Handle))) return(-1);
00331 vid->bufferCheckedOut = false;
00332
00333 return (0);
00334 }
00335
00336
00337