Fawkes API
Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * transfer_thread.cpp - OpenNI Visualization: network transfer thread 00004 * 00005 * Created: Sat Apr 02 20:19:21 2011 00006 * Copyright 2006-2011 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. 00014 * 00015 * This program is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 * GNU Library General Public License for more details. 00019 * 00020 * Read the full text in the LICENSE.GPL file in the doc directory. 00021 */ 00022 00023 #include "transfer_thread.h" 00024 #include <core/threading/read_write_lock.h> 00025 #include <fvcams/camera.h> 00026 #include <fvutils/color/colorspaces.h> 00027 00028 #include <cstdlib> 00029 #include <cstring> 00030 00031 using namespace fawkes; 00032 using namespace firevision; 00033 00034 /** @class PclViewerTransferThread "transfer_thread.h" 00035 * PCL viewer transfer thread. 00036 * Especially for transmission over slow Wifi networks tranmission 00037 * must be pushed to its own thread. It captures the frame and copies 00038 * it to an internal buffer to anytime use. 00039 * @author Tim Niemueller 00040 */ 00041 00042 /** Constructor. */ 00043 PclViewerTransferThread::PclViewerTransferThread() 00044 : Thread("PclViewerTransferThread", Thread::OPMODE_CONTINUOUS) 00045 { 00046 __rwlock = new ReadWriteLock(); 00047 } 00048 00049 00050 /** Destructor. */ 00051 PclViewerTransferThread::~PclViewerTransferThread() 00052 { 00053 delete __rwlock; 00054 std::map<std::string, unsigned char *>::iterator c; 00055 for (c = __buffers.begin(); c != __buffers.end(); ++c) { 00056 free(c->second); 00057 } 00058 } 00059 00060 00061 /** Lock for reading. 00062 * Images will not be updated while the lock is held. Any number of 00063 * readers can hold a read lock at the same time. Make sure that the 00064 * thread does not starve. 00065 */ 00066 void 00067 PclViewerTransferThread::lock_for_read() 00068 { 00069 __rwlock->lock_for_read(); 00070 } 00071 00072 00073 /** Unlock. */ 00074 void 00075 PclViewerTransferThread::unlock() 00076 { 00077 __rwlock->unlock(); 00078 } 00079 00080 00081 /** Add a camera from which to pull images. 00082 * @param name symbolic name, used to access buffers 00083 * @param cam camera to add 00084 */ 00085 void 00086 PclViewerTransferThread::add_camera(std::string name, firevision::Camera *cam) 00087 { 00088 __cams[name] = cam; 00089 __buffers[name] = malloc_buffer(cam->colorspace(), cam->pixel_width(), 00090 cam->pixel_height()); 00091 __buffer_sizes[name] = colorspace_buffer_size(cam->colorspace(), 00092 cam->pixel_width(), 00093 cam->pixel_height()); 00094 } 00095 00096 00097 void 00098 PclViewerTransferThread::loop() 00099 { 00100 std::map<std::string, firevision::Camera *>::iterator c; 00101 for (c = __cams.begin(); c != __cams.end(); ++c) { 00102 c->second->capture(); 00103 __rwlock->lock_for_write(); 00104 memcpy(__buffers[c->first], c->second->buffer(), __buffer_sizes[c->first]); 00105 __rwlock->unlock(); 00106 c->second->dispose_buffer(); 00107 } 00108 }