Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

RawCamBehavior.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_RawCamBehavior_h_
00003 #define INCLUDED_RawCamBehavior_h_
00004 
00005 #include "CameraStreamBehavior.h"
00006 #include "Shared/Config.h"
00007 
00008 class Socket;
00009 class FilterBankGenerator;
00010 class FilterBankEvent;
00011 
00012 //! Forwards images from camera over wireless
00013 /*! The format used for serialization is basically defined by the
00014  *  subclass of FilterBankGenerator being used.  I suggest looking at
00015  *  that classes's documentation to determine the format used. (Generally
00016  *  either RawCameraGenerator or JPEGGenerator)
00017  *
00018  *  However, RawCamBehavior will add a few fields at the beginning of
00019  *  each packet to assist in processing the image stream.
00020  *
00021  *  I emphasize: <i>beginning</i> of each Vision packet, <i>before</i> the FilterBankGenerator header. 
00022  *  - <@c string:"TekkotsuImage">
00023  *  - <<tt>Config::vision_config::encoding_t</tt>: rawcam.encoding> <i>(expect single or multiple channels, 0 means color (3 channels), 1 means intensity (1 channel))</i>
00024  *  - <<tt>Config::vision_config::compression_t</tt>: rawcam.compression> <i>(0==none, 1==jpeg, 2==rle)</i>
00025  *  - <@c unsigned @c int: width> <i>(this is the width of the largest channel - note different channels can be sent at different resolutions!  Provides cheap "compression" of chromaticity channels)</i>
00026  *  - <@c unsigned @c int: height> <i>(similarly, height of largest channel)</i>
00027  *  - <@c unsigned @c int: timestamp> <i>(time image was taken, milliseconds since boot)</i>
00028  *  - <@c unsigned @c int: framenumber> <i>(incremented for each frame, so we can tell if/when we drop one)</i>
00029  *
00030  *  Alternatively, RawCameraGenerator may send a "Close Connection" packet
00031  *  when the server is shutting down.  This is to help UDP connections, which
00032  *  otherwise wouldn't realize that they need to start trying to reconnect.
00033  *  - <@c string:"CloseConnection">
00034  *  
00035  *  This is exactly the same protocol that is followed by the SegCamBehavior as well - the same code can parse either stream.
00036  *
00037  *  This is a binary protocol -- the fields listed indicate binary values
00038  *  in the AIBO's byte order (little endian).  Strings are encoded using
00039  *  the LoadSave::encode(char*,unsigned int, unsigned int) method.
00040  */ 
00041 class RawCamBehavior : public CameraStreamBehavior {
00042 public:
00043   //! constructor
00044   RawCamBehavior();
00045   
00046   //! destructor
00047   ~RawCamBehavior() { theOne=NULL; }
00048 
00049 #ifdef PLATFORM_APERIOS
00050   static const unsigned int TCP_WIRELESS_BUFFER_SIZE=200000; //!< 200000 bytes for use up to 416x320 + 2*208x160 (double res Y, full res UV on ERS-7)
00051 #else
00052   static const unsigned int TCP_WIRELESS_BUFFER_SIZE=901*1024; //!< 900KB for max of full-color 640x480 + 1KB for header
00053 #endif
00054   static const unsigned int UDP_WIRELESS_BUFFER_SIZE=64*1024; //!< 64KB is the max udp packet size
00055 
00056   virtual void DoStart();
00057 
00058   virtual void DoStop();
00059 
00060   virtual void processEvent(const EventBase& e);
00061 
00062   static std::string getClassDescription() {
00063     char tmp[20];
00064     snprintf(tmp,20,"%d",*config->vision.rawcam.port); tmp[19]='\0';
00065     return std::string("Forwards images from camera over port ")+tmp;
00066   }
00067   virtual std::string getDescription() const { return getClassDescription(); }
00068 
00069   static unsigned int getSourceLayer(unsigned int chan, unsigned int numLayers); //!< returns the layer which will be used out of the source, based on current ::config settings (i.e. compression, skip, etc)
00070   static unsigned int getSourceYLayer(unsigned int numLayers); //!< returns the layer which will be used out of the source, based on current ::config settings (i.e. compression, skip, etc)
00071   static unsigned int getSourceULayer(unsigned int numLayers); //!< returns the layer which will be used out of the source, based on current ::config settings (i.e. compression, skip, etc)
00072   static unsigned int getSourceVLayer(unsigned int numLayers); //!< returns the layer which will be used out of the source, based on current ::config settings (i.e. compression, skip, etc)
00073 
00074 protected:
00075   static RawCamBehavior* theOne; //!< global instance of RawCamBehavior acting as server
00076   //! function for network data to be sent to -- forwards to #theOne's receiveData()
00077   static int networkCallback(char* buf, int bytes) { return theOne->receiveData(buf,bytes); }
00078 
00079   void closeServer(); //!<tear down the server socket (#visRaw)
00080   void setupServer(); //!<setup the server socket (#visRaw)
00081   
00082   //! opens a new packet, writes header info; returns true if open, false if otherwise open (check cur==NULL for error)
00083   /*! see the class documentation for RawCamBehavior for the protocol documentation */
00084   bool openPacket(FilterBankGenerator& fbkgen, unsigned int time, unsigned int layer);
00085   bool writeColor(const FilterBankEvent& fbke); //!< writes a color image
00086   bool writeSingleChannel(const FilterBankEvent& fbke); //!< writes a single channel
00087   void closePacket(); //!< closes and sends a packet, does nothing if no packet open
00088 
00089   //! sends a packet signaling the server is closing the connection (good for UDP connections)
00090   bool sendCloseConnectionPacket();
00091     
00092   Socket * visRaw; //!< socket for sending the image stream
00093   char * packet; //!< point to the current buffer being prepared to be sent
00094   char * cur; //!< current location within that buffer
00095   unsigned int avail; //!< the number of bytes remaining in the buffer
00096   unsigned int max_buf; //!< the buffer size requested from Wireless when the socket was allocated
00097   unsigned int lastProcessedTime; //!< the time that the last event was processed
00098 private:
00099   RawCamBehavior(const RawCamBehavior&); //!< don't call
00100   RawCamBehavior& operator=(const RawCamBehavior&); //!< don't call
00101 };
00102 
00103 /*! @file
00104  * @brief Describes RawCamBehavior, which forwards images from camera over wireless
00105  * @author ejt (Creator)
00106  *
00107  * $Author: ejt $
00108  * $Name: tekkotsu-4_0 $
00109  * $Revision: 1.21 $
00110  * $State: Exp $
00111  * $Date: 2007/06/07 18:24:42 $
00112  */
00113 
00114 #endif

Tekkotsu v4.0
Generated Thu Nov 22 00:54:55 2007 by Doxygen 1.5.4