Fawkes API  Fawkes Development Version
transfer_thread.cpp
1 
2 /***************************************************************************
3  * transfer_thread.cpp - OpenNI Visualization: network transfer thread
4  *
5  * Created: Sat Apr 02 20:19:21 2011
6  * Copyright 2006-2011 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "transfer_thread.h"
24 
25 #include <core/threading/read_write_lock.h>
26 #include <fvcams/camera.h>
27 #include <fvutils/color/colorspaces.h>
28 
29 #include <cstdlib>
30 #include <cstring>
31 
32 using namespace fawkes;
33 using namespace firevision;
34 
35 /** @class PclViewerTransferThread "transfer_thread.h"
36  * PCL viewer transfer thread.
37  * Especially for transmission over slow Wifi networks tranmission
38  * must be pushed to its own thread. It captures the frame and copies
39  * it to an internal buffer to anytime use.
40  * @author Tim Niemueller
41  */
42 
43 /** Constructor. */
45 : Thread("PclViewerTransferThread", Thread::OPMODE_CONTINUOUS)
46 {
47  rwlock_ = new ReadWriteLock();
48 }
49 
50 /** Destructor. */
52 {
53  delete rwlock_;
54  std::map<std::string, unsigned char *>::iterator c;
55  for (c = buffers_.begin(); c != buffers_.end(); ++c) {
56  free(c->second);
57  }
58 }
59 
60 /** Lock for reading.
61  * Images will not be updated while the lock is held. Any number of
62  * readers can hold a read lock at the same time. Make sure that the
63  * thread does not starve.
64  */
65 void
67 {
68  rwlock_->lock_for_read();
69 }
70 
71 /** Unlock. */
72 void
74 {
75  rwlock_->unlock();
76 }
77 
78 /** Add a camera from which to pull images.
79  * @param name symbolic name, used to access buffers
80  * @param cam camera to add
81  */
82 void
84 {
85  cams_[name] = cam;
86  buffers_[name] = malloc_buffer(cam->colorspace(), cam->pixel_width(), cam->pixel_height());
87  buffer_sizes_[name] =
88  colorspace_buffer_size(cam->colorspace(), cam->pixel_width(), cam->pixel_height());
89 }
90 
91 void
93 {
94  std::map<std::string, firevision::Camera *>::iterator c;
95  for (c = cams_.begin(); c != cams_.end(); ++c) {
96  c->second->capture();
97  rwlock_->lock_for_write();
98  memcpy(buffers_[c->first], c->second->buffer(), buffer_sizes_[c->first]);
99  rwlock_->unlock();
100  c->second->dispose_buffer();
101  }
102 }
PclViewerTransferThread::loop
void loop()
Code to execute in the thread.
Definition: transfer_thread.cpp:92
firevision::Camera::colorspace
virtual colorspace_t colorspace()=0
fawkes::Thread::name
const char * name() const
Definition: thread.h:100
PclViewerTransferThread::~PclViewerTransferThread
~PclViewerTransferThread()
Destructor.
Definition: transfer_thread.cpp:51
fawkes::ReadWriteLock::lock_for_read
void lock_for_read()
Aquire a reader lock.
Definition: read_write_lock.cpp:99
fawkes::ReadWriteLock
Definition: read_write_lock.h:37
fawkes
firevision::Camera::pixel_height
virtual unsigned int pixel_height()=0
fawkes::ReadWriteLock::lock_for_write
void lock_for_write()
Aquire a writer lock.
Definition: read_write_lock.cpp:110
fawkes::ReadWriteLock::unlock
void unlock()
Release the lock.
Definition: read_write_lock.cpp:143
firevision::Camera::pixel_width
virtual unsigned int pixel_width()=0
PclViewerTransferThread::PclViewerTransferThread
PclViewerTransferThread()
Constructor.
Definition: transfer_thread.cpp:44
PclViewerTransferThread::lock_for_read
void lock_for_read()
Lock for reading.
Definition: transfer_thread.cpp:66
fawkes::Thread
Definition: thread.h:45
firevision::Camera
Definition: camera.h:38
PclViewerTransferThread::unlock
void unlock()
Unlock.
Definition: transfer_thread.cpp:73
PclViewerTransferThread::add_camera
void add_camera(std::string name, firevision::Camera *cam)
Add a camera from which to pull images.
Definition: transfer_thread.cpp:83