00001 #include <PlaybackCamera.H>
00002 #include <Logger.H>
00003 #include <signal.h>
00004 #include <opencv/highgui.h>
00005
00006 using namespace std;
00007 using namespace WURDE;
00008 using namespace WURDEVision;
00009
00010
00011
00012 WURDEPlaybackCamera::WURDEPlaybackCamera(std::string name, std::map<std::string,std::string> options)
00013 :WURDECamera(name,options){
00014
00015
00016 m_init=false;
00017
00018 buffMutex[0]=(pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER;
00019 buffMutex[1]=(pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER;
00020 buffMutex[2]=(pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER;
00021
00022 buffers[0]=NULL;
00023 buffers[1]=NULL;
00024 buffers[2]=NULL;
00025 currentBuffer=0;
00026 nextFrame=0;
00027 m_timer.setFrequency(30.0);
00028 g_logdebug << "Done loading. " << endl;
00029 }
00030
00031 bool WURDEPlaybackCamera::init(){
00032 g_logdebug << "in init. " << endl;
00033 m_init=true;
00034 if(m_options.find("imageDirectory")!=m_options.end()){
00035 m_frameLoader.setPath(m_options["imageDirectory"]);
00036 }else{
00037 g_logerror << "Couldn't find playback directory." << endl;
00038 m_init=false;
00039 return false;
00040 }
00041
00042 if(m_options.find("imageSuffix")!=m_options.end()){
00043 m_frameLoader.addPattern(m_options["imageSuffix"]);
00044 }else{
00045 g_logwarn << "No playbackImageSuffix, assuming .ppm" << endl;
00046 m_frameLoader.addPattern(".ppm");
00047 }
00048
00049 if(m_options.find("imagePrefix")!=m_options.end()){
00050 m_frameLoader.addPattern(m_options["imagePrefix"]);
00051 }
00052
00053 if(m_options.find("imageDevice")!=m_options.end()){
00054 m_frameLoader.addPattern(m_options["imageDevice"]);
00055 }
00056
00057 g_logdebug << "Opening directory " << endl;
00058 m_frameLoader.openDirectory();
00059 g_logdebug << "Done. " << endl;
00060 m_frameList=m_frameLoader.getSortedFileList();
00061
00062 return true;
00063
00064 }
00065
00066 bool WURDEPlaybackCamera::startCapture(){
00067 init();
00068 g_logdebug << " starting capture. " << endl;
00069 m_timer.setFakeZeroTime(m_frameList[0].timestamp);
00070 thread_state=STATE_RUN;
00071 pthread_create(&m_captureThread,NULL,runPlaybackCameraCaptureThread,(void *)this);
00072 m_capturing=true;
00073 return true;
00074 }
00075
00076 void WURDEPlaybackCamera::stopCapture(){
00077 m_capturing=false;
00078 thread_state=STATE_QUIT;
00079 pthread_join(m_captureThread,NULL);
00080 if(buffers[0]) cvReleaseImage(&buffers[0]);
00081 if(buffers[1]) cvReleaseImage(&buffers[1]);
00082 if(buffers[2]) cvReleaseImage(&buffers[2]);
00083 currentBuffer=0;
00084 nextFrame=0;
00085 }
00086
00087 WURDEImage WURDEPlaybackCamera::captureRGB(){
00088 if(!m_haveRGB&&m_capturing){
00089 pthread_mutex_lock(&buffMutex[currentBuffer]);
00090 m_imageRGB.first=timestamps[currentBuffer];
00091 m_imageRGB.second=buffers[currentBuffer];
00092 m_haveRGB=true;
00093 }
00094 if(!m_imageRGB.second||!m_capturing){
00095 m_imageRGB.second=NULL;
00096 g_wurdeCamErrno=ERROR_IMAGE_NOT_AVAILABLE;
00097 }else{
00098 g_wurdeCamErrno=ERROR_NOERROR;
00099 }
00100 return m_imageRGB;
00101 }
00102
00103
00104 void WURDEPlaybackCamera::resetFrame(){
00105 m_haveRGB=false;
00106 pthread_mutex_unlock(&buffMutex[currentBuffer]);
00107 }
00108
00109 void WURDEPlaybackCamera::captureThread(){
00110 unsigned int activeBuffer;
00111 bool haveLock;
00112 IplImage *readImage;
00113 while(thread_state==STATE_RUN){
00114 if(m_capturing){
00115 if(nextFrame>=m_frameList.size()){
00116
00117 m_capturing=false;
00118 }else{
00119 haveLock=false;
00120 activeBuffer=currentBuffer+1;
00121
00122
00123
00124 while(!haveLock){
00125 if(activeBuffer>2){
00126 activeBuffer=0;
00127 }
00128 if(pthread_mutex_trylock(&buffMutex[activeBuffer])==0){
00129 haveLock=true;
00130 }else{
00131 activeBuffer++;
00132 }
00133 }
00134
00135
00136
00137
00138
00139
00140 if(!buffers[activeBuffer]){
00141 buffers[activeBuffer]=cvCreateImage(cvSize(320, 240), IPL_DEPTH_8U, 3);
00142 }
00143
00144 readImage=cvLoadImage(m_frameList[nextFrame].filename.c_str());
00145 cvConvertImage(readImage, buffers[activeBuffer], CV_CVTIMG_SWAP_RB);
00146 cvReleaseImage(&readImage);
00147 readImage=NULL;
00148 timestamps[activeBuffer]=m_frameList[nextFrame].timestamp;
00149 nextFrame++;
00150 currentBuffer=activeBuffer;
00151 pthread_mutex_unlock(&buffMutex[activeBuffer]);
00152 }
00153 }
00154 m_timer.sleep();
00155
00156 }
00157 }
00158
00159 extern "C" {
00160 void *runPlaybackCameraCaptureThread(void *ptr){
00161 WURDEVision::WURDEPlaybackCamera *theCamera=(WURDEVision::WURDEPlaybackCamera *)ptr;
00162 theCamera->captureThread();
00163 pthread_exit(NULL);
00164 }
00165 }