-
Notifications
You must be signed in to change notification settings - Fork 762
/
Copy pathframe_listener_impl.cpp
156 lines (127 loc) · 3.79 KB
/
frame_listener_impl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* This file is part of the OpenKinect Project. http://www.openkinect.org
*
* Copyright (c) 2014 individual OpenKinect contributors. See the CONTRIB file
* for details.
*
* This code is licensed to you under the terms of the Apache License, version
* 2.0, or, at your option, the terms of the GNU General Public License,
* version 2.0. See the APACHE20 and GPL2 files for the text of the licenses,
* or the following URLs:
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.gnu.org/licenses/gpl-2.0.txt
*
* If you redistribute this file in source form, modified or unmodified, you
* may:
* 1) Leave this header intact and distribute it under the same terms,
* accompanying it with the APACHE20 and GPL20 files, or
* 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or
* 3) Delete the GPL v2 clause and accompany it with the APACHE20 file
* In all cases you must keep the copyright notice intact and include a copy
* of the CONTRIB file.
*
* Binary distributions must follow the binary distribution requirements of
* either License.
*/
/** @file frame_listener_impl.cpp Implementation classes for frame listeners. */
#include <libfreenect2/frame_listener_impl.h>
#include <libfreenect2/threading.h>
namespace libfreenect2
{
FrameListener::~FrameListener() {}
/** Implementation class for synchronizing different types of frames. */
class SyncMultiFrameListenerImpl
{
public:
libfreenect2::mutex mutex_;
libfreenect2::condition_variable condition_;
FrameMap next_frame_;
const unsigned int subscribed_frame_types_;
unsigned int ready_frame_types_;
SyncMultiFrameListenerImpl(unsigned int frame_types) :
subscribed_frame_types_(frame_types),
ready_frame_types_(0)
{
}
bool hasNewFrame() const
{
return ready_frame_types_ == subscribed_frame_types_;
}
};
SyncMultiFrameListener::SyncMultiFrameListener(unsigned int frame_types) :
impl_(new SyncMultiFrameListenerImpl(frame_types))
{
}
SyncMultiFrameListener::~SyncMultiFrameListener()
{
release(impl_->next_frame_);
delete impl_;
}
bool SyncMultiFrameListener::hasNewFrame() const
{
libfreenect2::unique_lock l(impl_->mutex_);
return impl_->hasNewFrame();
}
bool SyncMultiFrameListener::waitForNewFrame(FrameMap &frame, int milliseconds)
{
#ifdef LIBFREENECT2_THREADING_STDLIB
libfreenect2::unique_lock l(impl_->mutex_);
auto predicate = std::bind(&SyncMultiFrameListenerImpl::hasNewFrame, impl_);
if(impl_->condition_.wait_for(l, std::chrono::milliseconds(milliseconds), predicate))
{
frame = impl_->next_frame_;
impl_->next_frame_.clear();
impl_->ready_frame_types_ = 0;
return true;
}
else
{
return false;
}
#else
waitForNewFrame(frame);
return true;
#endif // LIBFREENECT2_THREADING_STDLIB
}
void SyncMultiFrameListener::waitForNewFrame(FrameMap &frame)
{
libfreenect2::unique_lock l(impl_->mutex_);
while(!impl_->hasNewFrame())
{
WAIT_CONDITION(impl_->condition_, impl_->mutex_, l)
}
frame = impl_->next_frame_;
impl_->next_frame_.clear();
impl_->ready_frame_types_ = 0;
}
void SyncMultiFrameListener::release(FrameMap &frame)
{
for(FrameMap::iterator it = frame.begin(); it != frame.end(); ++it)
{
delete it->second;
it->second = 0;
}
frame.clear();
}
bool SyncMultiFrameListener::onNewFrame(Frame::Type type, Frame *frame)
{
if((impl_->subscribed_frame_types_ & type) == 0) return false;
{
libfreenect2::lock_guard l(impl_->mutex_);
FrameMap::iterator it = impl_->next_frame_.find(type);
if(it != impl_->next_frame_.end())
{
// replace frame
delete it->second;
it->second = frame;
}
else
{
impl_->next_frame_[type] = frame;
}
impl_->ready_frame_types_ |= type;
}
impl_->condition_.notify_one();
return true;
}
} /* namespace libfreenect2 */