videoWin32DirectShow.cpp
Go to the documentation of this file.
00001 /*
00002         ========================================================================
00003         PROJECT: DirectShow Video Processing Library
00004         Version: 0.0.8 (05/04/2005)
00005         ========================================================================
00006         Author:  Thomas Pintaric, Vienna University of Technology
00007         Contact: pintaric@ims.tuwien.ac.at http://ims.tuwien.ac.at/~thomas
00008         =======================================================================
00009         
00010         Copyright (C) 2005  Vienna University of Technology
00011         
00012         This library is free software; you can redistribute it and/or
00013         modify it under the terms of the GNU General Public License
00014         as published by the Free Software Foundation; either version 2
00015         of the License, or (at your option) any later version.
00016         
00017         This program is distributed in the hope that it will be useful,
00018         but WITHOUT ANY WARRANTY; without even the implied warranty of
00019         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020         GNU General Public License for more details.
00021         
00022         You should have received a copy of the GNU General Public License
00023         along with this program; if not, write to the Free Software
00024         Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
00025         USA.
00026         
00027         For further information please contact Thomas Pintaric under
00028         <pintaric@ims.tuwien.ac.at> or write to Thomas Pintaric,
00029         Vienna University of Technology, Favoritenstr. 9-11/E188/2, A-1040
00030         Vienna, Austria.
00031         ========================================================================
00032  */
00033 /*
00034  *      Copyright (c) 2004-2007 Philip Lamb (PRL) phil@eden.net.nz. All rights reserved.
00035  *      
00036  *      Rev             Date            Who             Changes
00037  *      2.68.2  2004-07-20      PRL             Rewrite for ARToolKit 2.68.2
00038  *      2.71.0  2005-08-05      PRL             Incorporate DSVL-0.0.8b
00039  *
00040  */
00041 
00042 #include "DSVL.h"
00043 //#include <string.h>
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; // deprecated, use (g_Handle.t) instead.
00055         //bool flip_horizontal = false; // deprecated.
00056         //bool flip_vertical = false;   // deprecated.
00057 };
00058 
00059 // -----------------------------------------------------------------------------------------------------------------
00060 
00061 static AR2VideoParamT   *gVid = NULL;
00062 
00063 #ifdef FLIPPED // compatibility with videoLinux*
00064 static const bool               FLIPPED_defined =  true;                // deprecated
00065 #else
00066 static const bool               FLIPPED_defined =  false;               // deprecated
00067 #endif
00068 const long                              frame_timeout_ms = 0L;  // set to INFINITE if arVideoGetImage()
00069                                                                                                                 // is called from a separate worker thread
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         // Allocate the parameters structure and fill it in.
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 
00159                 config = getenv("ARTOOLKIT_CONFIG");
00160 
00161                 if (config == NULL) {
00162                         config = &config_default[0];
00163                 }
00164                 if (FAILED(vid->graphManager->BuildGraphFromXMLString(config))) return(NULL);
00165 
00166         } else {
00167                 if (strncmp(config, "<?xml", 5) == 0) {
00168                         if (FAILED(vid->graphManager->BuildGraphFromXMLString(config))) return(NULL);
00169                 } else {
00170                         if (FAILED(vid->graphManager->BuildGraphFromXMLFile(config))) return(NULL);
00171                 }
00172         }
00173         if (FAILED(vid->graphManager->EnableMemoryBuffer())) return(NULL);
00174 
00175         return (vid);
00176 }
00177 
00178 
00179 int ar2VideoClose(AR2VideoParamT *vid)
00180 {
00181         int _ret = -1;
00182 
00183         if (vid == NULL) return (_ret);
00184 
00185         if (vid->graphManager != NULL) {
00186         
00187                 if (vid->bufferCheckedOut) 
00188                         vid->graphManager->CheckinMemoryBuffer(vid->g_Handle, true);
00189 
00190                 vid->graphManager->Stop();
00191                 delete vid->graphManager;
00192                 vid->graphManager = NULL;
00193 
00194                 _ret = 0;
00195         }
00196 
00197         free(vid);
00198 
00199         // do not assume free NULL's the pointer
00200         vid = NULL;
00201 
00202         // COM should be closed down in the same context
00203         CoUninitialize();
00204         
00205     return(_ret);
00206 }
00207 
00208 unsigned char *ar2VideoGetImage(AR2VideoParamT *vid)
00209 {
00210         DWORD wait_result;
00211         unsigned char* pixelBuffer;
00212         
00213         if (vid == NULL) return (NULL);
00214         if (vid->graphManager == NULL) return (NULL);
00215         
00216         if (vid->bufferCheckedOut) {
00217                 if (FAILED(vid->graphManager->CheckinMemoryBuffer(vid->g_Handle))) return (NULL);
00218                 vid->bufferCheckedOut = false;
00219         }
00220         wait_result = vid->graphManager->WaitForNextSample(frame_timeout_ms);
00221         if (wait_result == WAIT_OBJECT_0) {
00222                 if (FAILED(vid->graphManager->CheckoutMemoryBuffer(&(vid->g_Handle), &pixelBuffer, NULL, NULL, NULL, &(vid->g_Timestamp)))) return(NULL);
00223                 vid->bufferCheckedOut = true;
00224                 return (pixelBuffer);
00225         }
00226 
00227         return(NULL);
00228 }
00229 
00230 int ar2VideoCapStart(AR2VideoParamT *vid)
00231 {
00232         if (vid == NULL) return (-1);
00233         if (vid->graphManager == NULL) return (-1);
00234         
00235         if (FAILED(vid->graphManager->Run())) return (-1);
00236         return (0);
00237 }
00238 
00239 int ar2VideoCapStop(AR2VideoParamT *vid)
00240 {
00241         if (vid == NULL) return (-1);
00242         if (vid->graphManager == NULL) return (-1);
00243 
00244         if (vid->bufferCheckedOut) {
00245                 if (FAILED(vid->graphManager->CheckinMemoryBuffer(vid->g_Handle, true))) return (-1);
00246                 vid->bufferCheckedOut = false;
00247         }
00248 
00249         // PRL 2005-09-21: Commented out due to issue where stopping the
00250         // media stream cuts off glut's periodic tasks, including functions
00251         // registered with glutIdleFunc() and glutDisplayFunc();
00252         //if(FAILED(vid->graphManager->Stop())) return (-1);
00253 
00254         return (0);
00255 }
00256 
00257 int ar2VideoCapNext(AR2VideoParamT *vid)
00258 {
00259         if (vid == NULL) return (-1);
00260         if (vid->graphManager == NULL) return (-1);
00261 
00262         if (vid->bufferCheckedOut) {
00263                 if (FAILED(vid->graphManager->CheckinMemoryBuffer(vid->g_Handle, true))) return (-1);
00264                 vid->bufferCheckedOut = false;
00265         }
00266         return (0);
00267 }
00268 
00269 int ar2VideoInqSize(AR2VideoParamT *vid, int *x, int *y)
00270 {
00271         if (vid == NULL) return (-1);
00272         if (vid->graphManager == NULL) return(-1);
00273 
00274         long frame_width;
00275 
00276         long frame_height;
00277 
00278         vid->graphManager->GetCurrentMediaFormat(&frame_width, &frame_height,NULL,NULL);
00279 
00280         *x = (int) frame_width;
00281 
00282         *y = (int) frame_height;
00283 
00284 
00285     return (0);
00286 }
00287 
00288 // -----------------------------------------------------------------------------------------------------------------
00289 
00290 int ar2VideoInqFlipping(AR2VideoParamT *vid, int *flipH, int *flipV)
00291 {
00292         // DEPRECATED
00293         // image flipping can be specified in the XML config file, but can
00294 
00295         // no longer be queried via arVideoInqFlipping()
00296 
00297         return (-1); // not implemented
00298 }
00299 
00300 int ar2VideoInqFreq(AR2VideoParamT *vid, float *fps)
00301 {
00302         if (vid == NULL) return (-1);
00303         if (vid->graphManager == NULL) return(-1);
00304 
00305         double frames_per_second;
00306 
00307         vid->graphManager->GetCurrentMediaFormat(NULL,NULL,&frames_per_second,NULL);
00308 
00309         *fps = (float) frames_per_second;
00310 
00311 
00312     return (0);
00313 }
00314 
00315 unsigned char *ar2VideoLockBuffer(AR2VideoParamT *vid, MemoryBufferHandle* pHandle)
00316 {
00317         unsigned char *pixelBuffer;
00318         
00319         if (vid == NULL) return (NULL);
00320         if (vid->graphManager == NULL) return (NULL);
00321         
00322         if (FAILED(vid->graphManager->CheckoutMemoryBuffer(pHandle, &pixelBuffer))) return (NULL);
00323         vid->bufferCheckedOut = true;
00324         
00325         return (pixelBuffer);
00326 }
00327 
00328 int ar2VideoUnlockBuffer(AR2VideoParamT *vid, MemoryBufferHandle Handle)
00329 {
00330         if (vid == NULL) return (-1);
00331         if (vid->graphManager == NULL) return(-1);
00332         
00333         if (FAILED(vid->graphManager->CheckinMemoryBuffer(Handle))) return(-1);
00334         vid->bufferCheckedOut = false;
00335 
00336         return (0);
00337 }
00338 
00339 // -----------------------------------------------------------------------------------------------------------------
00340 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


ar_recog
Author(s): Graylin Trevor Jay and Christopher Crick
autogenerated on Fri Jan 25 2013 12:15:00