OpenVideo Documentation

   Main Page       Modules       Class Hierarchy       Alphabetical List       Compound List       File List       Compound Members       Related Pages   

VideoWrapperSrc.cxx

Go to the documentation of this file.
00001 /* ========================================================================
00002  * Copyright (C) 2005  Graz University of Technology
00003  *
00004  * This framework is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This framework is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this framework; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  *
00018  * For further information please contact Denis Kalkofen under
00019  * <kalkofen@icg.tu-graz.ac.at> or write to Denis Kalkofen,
00020  * Graz University of Technology, Inffeldgasse 16a, A8010 Graz,
00021  * Austria.
00022  * ========================================================================
00023  * PROJECT: OpenVideo
00024  * ======================================================================== */
00033 #include <openvideo/nodes/VideoWrapperSrc.h>
00034 #include <openvideo/openVideo.h>
00035 
00036 #ifdef ENABLE_VIDEOWRAPPERSRC
00037 
00038 #include <VideoWrapper.h>
00039 #include <openvideo/Manager.h>
00040 
00041 #ifdef WIN32
00042     #ifdef _DEBUG
00043         #pragma comment(lib,"VideoWrapperd.lib")
00044     #else   
00045         #pragma comment(lib,"VideoWrapper.lib")
00046     #endif
00047 #else //WIN32
00048 
00049 #endif
00050 
00051 #include <openvideo/State.h>
00052 
00053 namespace openvideo {
00054 
00055 
00056 
00057 // DSVLSrcBuffer gives DSVLSrc full access to openvideo::Buffer
00058 class VideoWrapperSrcBuffer : public Buffer
00059 {
00060     friend class VideoWrapperSrc;
00061 public:
00062     VideoWrapperSrcBuffer(State* state, unsigned short video) : hVideo(video)
00063     {
00064         int bytesPerPixel = PixelFormat::getBitsPerPixel(state->format) / 8;        // assumes simple pixelformat (x times 8 bits)
00065         int stride = bytesPerPixel*state->width;
00066         bufferSize = stride*state->height;
00067         width = state->width;
00068         height = state->height;
00069         format = state->format;
00070 
00071         // VideoWrapper does not have doublebuffering, so we need to do copies...
00072         //
00073         buffer = new unsigned char[bufferSize];
00074     }
00075 
00076     ~VideoWrapperSrcBuffer()
00077     {
00078         delete buffer;
00079     }
00080 
00081     bool getNewFrame()
00082     {
00083         timeval timestamp;
00084         unsigned char* buf = NULL;
00085 
00086         // try to get a new frame
00087         VIDEO_getFrame(hVideo, &buf, &timestamp);
00088         if(buf)
00089         {
00090             memcpy(buffer, buf, bufferSize);
00091             updateCtr++;
00092 
00093             // since we made a copy we can immediately release the frame...
00094             VIDEO_releaseFrame(hVideo);
00095             return true;
00096         }
00097         return false;
00098     }
00099 
00100 protected:
00101     unsigned short  hVideo;
00102     int             bufferSize;
00103 };
00104 
00105 
00106 // DSVLSrcState gives DSVLSrc full access to openvideo::State
00107 class VideoWrapperSrcState : public State
00108 {
00109 public:
00110     BufferVector& getBuffers()  {  return buffers;  }
00111     void setCurrentBuffer(Buffer* buf)  {  currentBuffer = buf;  }
00112 };
00113 
00114 #define VideoWrapper_State(_STATE)    reinterpret_cast<VideoWrapperSrcState*>(_STATE)
00115 
00116 
00117 VideoWrapperSrc::VideoWrapperSrc()
00118 {
00119     name = typeName = "VideoWrapperSrc";
00120     strcpy(libId,"");
00121     strcpy(formatId,"");
00122     cameraNum=width=height=frameRate=format=0;
00123     scale=0.0;
00124     numBuffers = 2;
00125 }
00126     
00127 VideoWrapperSrc::~VideoWrapperSrc()
00128 {
00129 }
00130 
00131 void 
00132 VideoWrapperSrc::initPixelFormats()
00133 {
00134     pixelFormats.push_back(PIXEL_FORMAT(FORMAT_R8G8B8));
00135 }
00136 
00137 
00138 void
00139 VideoWrapperSrc::init()
00140 {
00141     Manager::getInstance()->getLogger()->log("OpenVideo: start VideoWrapperSrc\n");
00142 
00143     char camInitString[256];//="vc: 0 320x240 15 UYVY 0";
00144     sprintf(camInitString,"%s: %i %ix%i %i %s %f\n",libId,cameraNum,width,height,frameRate,formatId,scale);;
00145     Manager::getInstance()->getLogger()->logEx("camInitString= %s",camInitString);
00146 
00147     if(VIDEO_openVideo(camInitString, &g_hVideo)!= VW_SUCCESS)
00148     {
00149         Manager::getInstance()->getLogger()->log("failed to open video\n");
00150     }
00151 
00152 //  VIDEO_setPropertyLong(g_hVideo,CAMERAPROP_BRIGHT,1);
00153 
00154 
00155     // start video
00156     if(VIDEO_startVideo(g_hVideo)!= VW_SUCCESS)
00157     {
00158         Manager::getInstance()->getLogger()->log("failed to start video\n");
00159     }
00160 
00161     // get video size
00162     VIDEO_getWidth(g_hVideo, &width);
00163     VIDEO_getHeight(g_hVideo, &height);
00164 
00165     // get pixel format
00166     VIDEO_getPixelFormat(g_hVideo, &format);
00167 
00168     // display video properties
00169     //printf("video is %d x %d, and format is %d \n", width,height, format);
00170     
00171     state = new VideoWrapperSrcState();
00172     state->clear();
00173     state->width = width;
00174     state->height = height;
00175     state->format = PixelFormat::fromOGL(format);
00176     assert(state->format != FORMAT_UNKNOWN);
00177 
00178     for(int i=0; i<numBuffers; i++)
00179         VideoWrapper_State(state)->getBuffers().push_back(new VideoWrapperSrcBuffer(state, g_hVideo));
00180 }
00181 
00182 
00183 void
00184 VideoWrapperSrc::preProcess()
00185 {
00186 }
00187 
00188 
00189 void
00190 VideoWrapperSrc::process()
00191 {
00192     if(VideoWrapperSrcBuffer* buffer = reinterpret_cast<VideoWrapperSrcBuffer*>(state->findFreeBuffer()))
00193     {
00194         if(buffer->getNewFrame())
00195         {
00196             VideoWrapper_State(state)->setCurrentBuffer(buffer);
00197         }
00198         //else
00199         //  Manager::getInstance()->getLogger()->log("OpenVideo::VideoWrapperSrc: did not get a new frame\n");
00200     }
00201     else
00202         Manager::getInstance()->getLogger()->log("OpenVideo::VideoWrapperSrc all frames locked, can not read a new camera image!\n");
00203 }
00204 
00205 
00206 void
00207 VideoWrapperSrc::postProcess()
00208 {
00209 }
00210 
00211 
00212 bool 
00213 VideoWrapperSrc::setParameter(std::string key, std::string value)
00214 {
00215     if(Node::setParameter(key,value)) return true;
00216 
00217     if(key=="libId")
00218     {
00219         strcpy(this->libId,value.c_str());
00220         return true;
00221     }
00222     else if(key=="cameraNum")
00223     {
00224         this->cameraNum=atoi(value.c_str());
00225         return true;
00226     }
00227     else if(key=="width")
00228     {
00229         this->width=atoi(value.c_str());
00230         return true;
00231     }
00232     else if(key=="height")
00233     {
00234         this->height=atoi(value.c_str());
00235         return true;
00236     }
00237     else if(key=="frameRate")
00238     {
00239         this->frameRate=atoi(value.c_str());
00240         return true;
00241     }
00242     else if(key=="formatId")
00243     {
00244         strcpy(formatId,value.c_str());
00245         return true;
00246     }
00247     else if(key=="scale")
00248     {
00249         this->scale=(float)atof(value.c_str());
00250         return true;
00251     }
00252 
00253     if(key=="num-buffers")
00254     {
00255         numBuffers = atoi(value.c_str());
00256         if(numBuffers<1)
00257             numBuffers = 1;
00258         if(numBuffers>MAX_BUFFERS)
00259             numBuffers = MAX_BUFFERS;
00260         return true;
00261     }
00262 
00263     return false;
00264 }
00265 
00266 
00267 }  // namespace openvideo
00268 
00269 
00270 #endif //ENABLE_VIDEOWRAPPERSRC
 This page was generated at Wed May 31 13:04:16 2006 for OpenVideo by Doxygen.
 If you have any comments, please send a message to schmalstieg@icg.tu-graz.ac.at.
www.studierstube.org