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 #include <core/threading/read_write_lock.h>
25 #include <fvcams/camera.h>
26 #include <fvutils/color/colorspaces.h>
27 
28 #include <cstdlib>
29 #include <cstring>
30 
31 using namespace fawkes;
32 using namespace firevision;
33 
34 /** @class PclViewerTransferThread "transfer_thread.h"
35  * PCL viewer transfer thread.
36  * Especially for transmission over slow Wifi networks tranmission
37  * must be pushed to its own thread. It captures the frame and copies
38  * it to an internal buffer to anytime use.
39  * @author Tim Niemueller
40  */
41 
42 /** Constructor. */
44  : Thread("PclViewerTransferThread", Thread::OPMODE_CONTINUOUS)
45 {
46  __rwlock = new ReadWriteLock();
47 }
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 
61 /** Lock for reading.
62  * Images will not be updated while the lock is held. Any number of
63  * readers can hold a read lock at the same time. Make sure that the
64  * thread does not starve.
65  */
66 void
68 {
69  __rwlock->lock_for_read();
70 }
71 
72 
73 /** Unlock. */
74 void
76 {
77  __rwlock->unlock();
78 }
79 
80 
81 /** Add a camera from which to pull images.
82  * @param name symbolic name, used to access buffers
83  * @param cam camera to add
84  */
85 void
87 {
88  __cams[name] = cam;
89  __buffers[name] = malloc_buffer(cam->colorspace(), cam->pixel_width(),
90  cam->pixel_height());
91  __buffer_sizes[name] = colorspace_buffer_size(cam->colorspace(),
92  cam->pixel_width(),
93  cam->pixel_height());
94 }
95 
96 
97 void
99 {
100  std::map<std::string, firevision::Camera *>::iterator c;
101  for (c = __cams.begin(); c != __cams.end(); ++c) {
102  c->second->capture();
103  __rwlock->lock_for_write();
104  memcpy(__buffers[c->first], c->second->buffer(), __buffer_sizes[c->first]);
105  __rwlock->unlock();
106  c->second->dispose_buffer();
107  }
108 }
void add_camera(std::string name, firevision::Camera *cam)
Add a camera from which to pull images.
void loop()
Code to execute in the thread.
Camera interface for image aquiring devices in FireVision.
Definition: camera.h:35
void lock_for_read()
Aquire a reader lock.
Fawkes library namespace.
virtual unsigned int pixel_width()=0
Width of image in pixels.
Thread class encapsulation of pthreads.
Definition: thread.h:42
virtual colorspace_t colorspace()=0
Colorspace of returned image.
~PclViewerTransferThread()
Destructor.
void lock_for_write()
Aquire a writer lock.
Read/write lock to allow multiple readers but only a single writer on the resource at a time...
const char * name() const
Get name of thread.
Definition: thread.h:95
PclViewerTransferThread()
Constructor.
void lock_for_read()
Lock for reading.
virtual unsigned int pixel_height()=0
Height of image in pixels.
void unlock()
Release the lock.