OpenVideo Documentation

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

State.h

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  * ======================================================================== */
00031  /* ======================================================================= */
00032 
00033 #ifndef _STATEOV_H
00034 #define _STATEOV_H
00035 
00036 
00037 #include "openVideo.h"
00038 #include "PixelFormat.h"
00039 
00040 #include <map>
00041 #include <string>
00042 #include <vector>
00043 #include <assert.h>
00044 
00045 
00046 class ACE_Thread_Mutex;
00047 
00048 
00049 namespace openvideo {
00050 
00051 
00052 class DSVLSrc;
00053 
00054 
00055 class OPENVIDEO_API Buffer
00056 {
00057 public:
00058     Buffer();
00059 
00060     virtual ~Buffer();
00061 
00062     virtual const unsigned char* getPixels() const  {  return buffer;  }
00063 
00064     virtual int getWidth() const  {  return width;  }
00065 
00066     virtual int getHeight() const  {  return height;  }
00067 
00068     virtual PIXEL_FORMAT getFormat() const  {  return format;  }
00069 
00070     virtual int getLockCounter() const  {  return lockCtr;  }
00071 
00072     virtual void lock();
00073 
00074     virtual void unlock();
00075 
00076     virtual bool isLocked() const  {  return lockCtr>0;  }
00077 
00078     virtual unsigned int getUpdateCounter() const  {  return updateCtr;  }
00079 
00080 protected:
00081     unsigned char*      buffer;
00082     int                 lockCtr;
00083     unsigned int        updateCtr;
00084     ACE_Thread_Mutex*   mutex;
00085     int                 width,height;
00086     PIXEL_FORMAT        format;
00087 };
00088 
00089 
00090 typedef std::vector<Buffer*> BufferVector;
00091 
00104 class OPENVIDEO_API State
00105 {
00106 public:
00110     State();
00111     
00115     virtual ~State();
00116     
00120     int width;
00121 
00125     int height;
00126 
00130     PIXEL_FORMAT format;
00131 
00135     void clear();
00136 
00140     void  addElement(std::string key,void* value);
00141 
00145     void* getElementData(std::string key);
00146 
00150     void  removeElement(std::string key);
00151 
00153     virtual Buffer* getCurrentBuffer()  {  return currentBuffer;  }
00154 
00156     void unlockAllBuffers();
00157 
00158 
00160     Buffer* findFreeBuffer();
00161 
00162     int getNumFrames() const  {  return buffers.size();  }
00163 
00164     int getNumLockedBuffers() const;
00165 
00166 private:
00170     std::map<std::string,void*> elements;
00171 
00172 
00173 protected:
00174     BufferVector buffers;
00175     Buffer* currentBuffer;
00176 };
00177 
00178 
00180 
00185 // This class prevents a problem that could happen
00186 // in a usual client internal thread:
00187 //
00188 //   buffer = currentBuffer;
00189 //   buffer->lock();
00190 //   ...
00191 //   buffer->unlock();
00192 //
00193 // The internal thread makes a copy of the variable to
00194 // make sure that another thread can update currentBuffer at any time.
00195 // The problem happens if currentBuffer is unlocked between
00196 // 'buffer = currentBuffer;' and 'buffer->lock();'
00197 // To prevent this a client should use BufferSynchronizer which
00198 // provides an atomic action for getting a locked buffer
00199 // without overwriting the variable before it is locked
00200 //
00201 class OPENVIDEO_API BufferSynchronizer
00202 {
00203 public:
00204     BufferSynchronizer();
00205     ~BufferSynchronizer();
00206 
00208 
00212     void assign(Buffer* newBuffer);
00213 
00215 
00221     Buffer* getLocked();
00222 
00223 protected:
00224     Buffer*             buffer;
00225     ACE_Thread_Mutex*   mutex;
00226 };
00227 
00228 
00229 
00230 // ----> inlines <-----
00231 // constructor method.
00232 using namespace openvideo;
00233 
00234 inline State::State()
00235 {
00236     clear();
00237 }
00238 inline State::~State()
00239 {
00240     clear();
00241 }
00242 
00243 inline void 
00244 State::clear()
00245 {
00246     elements.clear();
00247     width = height =0;
00248     format = FORMAT_UNKNOWN;
00249     currentBuffer = NULL;
00250     // FIXME: should this methods also free all buffers?
00251 }
00252 inline void 
00253 State::addElement(std::string key,void* value)
00254 {
00255     elements[key]=value;
00256 }
00257 
00258 inline void* 
00259 State::getElementData(std::string key)
00260 {
00261     std::map<std::string,void*>::iterator it = elements.find( key );
00262     if( it == elements.end())
00263         return NULL;
00264  
00265     return it->second;
00266 }
00267 
00268 inline void 
00269 State::removeElement(std::string key)
00270 {
00271     elements.erase(key);
00272 }
00273 
00274 
00275 }  //namespace openvideo
00276 
00277 
00278 #endif //_STATEOV_H
00279 
 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