25
25
*/
26
26
27
27
#include < libfreenect2/frame_listener_impl.h>
28
+ #include < libfreenect2/threading.h>
28
29
29
30
namespace libfreenect2
30
31
{
31
32
32
33
FrameListener::~FrameListener () {}
33
34
34
- SyncMultiFrameListener::SyncMultiFrameListener (unsigned int frame_types) :
35
+ class SyncMultiFrameListenerImpl
36
+ {
37
+ public:
38
+ libfreenect2::mutex mutex_;
39
+ libfreenect2::condition_variable condition_;
40
+ FrameMap next_frame_;
41
+
42
+ const unsigned int subscribed_frame_types_;
43
+ unsigned int ready_frame_types_;
44
+
45
+ SyncMultiFrameListenerImpl (unsigned int frame_types) :
35
46
subscribed_frame_types_ (frame_types),
36
47
ready_frame_types_ (0 )
48
+ {
49
+ }
50
+
51
+ bool hasNewFrame () const
52
+ {
53
+ return ready_frame_types_ == subscribed_frame_types_;
54
+ }
55
+ };
56
+
57
+ SyncMultiFrameListener::SyncMultiFrameListener (unsigned int frame_types) :
58
+ impl_ (new SyncMultiFrameListenerImpl(frame_types))
37
59
{
38
60
}
39
61
40
62
SyncMultiFrameListener::~SyncMultiFrameListener ()
41
63
{
64
+ delete impl_;
65
+ }
66
+
67
+ bool SyncMultiFrameListener::hasNewFrame () const
68
+ {
69
+ libfreenect2::unique_lock l (impl_->mutex_ );
70
+
71
+ return impl_->hasNewFrame ();
72
+ }
73
+
74
+ #ifdef LIBFREENECT2_THREADING_STDLIB
75
+ bool SyncMultiFrameListener::waitForNewFrame (FrameMap &frame, int milliseconds)
76
+ {
77
+ libfreenect2::unique_lock l (impl_->mutex_ );
78
+
79
+ auto predicate = std::bind (&SyncMultiFrameListenerImpl::hasNewFrame, impl_);
80
+
81
+ if (impl_->condition_ .wait_for (l, std::chrono::milliseconds (milliseconds), predicate))
82
+ {
83
+ frame = impl_->next_frame_ ;
84
+ impl_->next_frame_ .clear ();
85
+ impl_->ready_frame_types_ = 0 ;
86
+
87
+ return true ;
88
+ }
89
+ else
90
+ {
91
+ return false ;
92
+ }
42
93
}
94
+ #endif // LIBFREENECT2_THREADING_STDLIB
43
95
44
96
void SyncMultiFrameListener::waitForNewFrame (FrameMap &frame)
45
97
{
46
- libfreenect2::unique_lock l (mutex_);
98
+ libfreenect2::unique_lock l (impl_-> mutex_ );
47
99
48
- while (ready_frame_types_ != subscribed_frame_types_ )
100
+ while (!impl_-> hasNewFrame () )
49
101
{
50
- WAIT_CONDITION (condition_, mutex_, l)
102
+ WAIT_CONDITION (impl_-> condition_ , impl_-> mutex_ , l)
51
103
}
52
104
53
- frame = next_frame_;
54
- next_frame_.clear ();
55
- ready_frame_types_ = 0 ;
105
+ frame = impl_-> next_frame_ ;
106
+ impl_-> next_frame_ .clear ();
107
+ impl_-> ready_frame_types_ = 0 ;
56
108
}
57
109
58
110
void SyncMultiFrameListener::release (FrameMap &frame)
@@ -68,28 +120,28 @@ void SyncMultiFrameListener::release(FrameMap &frame)
68
120
69
121
bool SyncMultiFrameListener::onNewFrame (Frame::Type type, Frame *frame)
70
122
{
71
- if ((subscribed_frame_types_ & type) == 0 ) return false ;
123
+ if ((impl_-> subscribed_frame_types_ & type) == 0 ) return false ;
72
124
73
125
{
74
- libfreenect2::lock_guard l (mutex_);
126
+ libfreenect2::lock_guard l (impl_-> mutex_ );
75
127
76
- FrameMap::iterator it = next_frame_.find (type);
128
+ FrameMap::iterator it = impl_-> next_frame_ .find (type);
77
129
78
- if (it != next_frame_.end ())
130
+ if (it != impl_-> next_frame_ .end ())
79
131
{
80
132
// replace frame
81
133
delete it->second ;
82
134
it->second = frame;
83
135
}
84
136
else
85
137
{
86
- next_frame_[type] = frame;
138
+ impl_-> next_frame_ [type] = frame;
87
139
}
88
140
89
- ready_frame_types_ |= type;
141
+ impl_-> ready_frame_types_ |= type;
90
142
}
91
143
92
- condition_.notify_one ();
144
+ impl_-> condition_ .notify_one ();
93
145
94
146
return true ;
95
147
}
0 commit comments