VideoWrapperSrc.cxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
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;
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
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
00087 VIDEO_getFrame(hVideo, &buf, ×tamp);
00088 if(buf)
00089 {
00090 memcpy(buffer, buf, bufferSize);
00091 updateCtr++;
00092
00093
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
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];
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
00153
00154
00155
00156 if(VIDEO_startVideo(g_hVideo)!= VW_SUCCESS)
00157 {
00158 Manager::getInstance()->getLogger()->log("failed to start video\n");
00159 }
00160
00161
00162 VIDEO_getWidth(g_hVideo, &width);
00163 VIDEO_getHeight(g_hVideo, &height);
00164
00165
00166 VIDEO_getPixelFormat(g_hVideo, &format);
00167
00168
00169
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
00199
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 }
00268
00269
00270 #endif //ENABLE_VIDEOWRAPPERSRC