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
32using namespace fawkes;
33using 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 */
65void
67{
68 rwlock_->lock_for_read();
69}
70
71/** Unlock. */
72void
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 */
82void
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
91void
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}
void add_camera(std::string name, firevision::Camera *cam)
Add a camera from which to pull images.
~PclViewerTransferThread()
Destructor.
PclViewerTransferThread()
Constructor.
void loop()
Code to execute in the thread.
void lock_for_read()
Lock for reading.
Read/write lock to allow multiple readers but only a single writer on the resource at a time.
void unlock()
Release the lock.
void lock_for_read()
Aquire a reader lock.
void lock_for_write()
Aquire a writer lock.
Thread class encapsulation of pthreads.
Definition: thread.h:46
const char * name() const
Get name of thread.
Definition: thread.h:100
Camera interface for image aquiring devices in FireVision.
Definition: camera.h:33
virtual unsigned int pixel_height()=0
Height of image in pixels.
virtual unsigned int pixel_width()=0
Width of image in pixels.
virtual colorspace_t colorspace()=0
Colorspace of returned image.
Fawkes library namespace.