@@ -33,19 +33,14 @@ namespace libfreenect2
33
33
{
34
34
35
35
DepthPacketStreamParser::DepthPacketStreamParser () :
36
- processor_ (noopProcessor<DepthPacket>()),
37
- current_sequence_ (0 ),
38
- current_subsequence_ (0 )
36
+ processor_ (noopProcessor<DepthPacket>()),
37
+ next_subsequence_ (0 )
39
38
{
40
- size_t single_image = 512 * 424 * 11 / 8 ;
39
+ size_t single_image = 512 * 424 * 11 / 8 ;
41
40
42
- buffer_.allocate ((single_image) * 10 );
43
- buffer_.front ().length = buffer_.front ().capacity ;
44
- buffer_.back ().length = buffer_.back ().capacity ;
45
-
46
- work_buffer_.data = new unsigned char [single_image * 2 ];
47
- work_buffer_.capacity = single_image * 2 ;
48
- work_buffer_.length = 0 ;
41
+ buffer_.allocate ((single_image)* 10 );
42
+ buffer_.front ().length = 0 ;
43
+ buffer_.back ().length = 0 ;
49
44
}
50
45
51
46
DepthPacketStreamParser::~DepthPacketStreamParser ()
@@ -59,122 +54,86 @@ void DepthPacketStreamParser::setPacketProcessor(libfreenect2::BaseDepthPacketPr
59
54
60
55
void DepthPacketStreamParser::onDataReceived (unsigned char * buffer, size_t in_length)
61
56
{
62
- // TODO: simplify this crap (so code, such unreadable, wow ; )
63
- Buffer &wb = work_buffer_ ;
57
+ if (in_length == 0 )
58
+ return ;
64
59
65
- size_t in_offset = 0 ;
60
+ DepthSubPacketFooter *footer = 0 ;
66
61
67
- while (in_offset < in_length)
68
- {
69
- unsigned char *ptr_in = buffer + in_offset, *ptr_out = wb.data + wb.length ;
70
- DepthSubPacketFooter *footer = 0 ;
71
- bool footer_found = false ;
62
+ Buffer &fb = buffer_.front ();
72
63
73
- size_t max_length = std::min<size_t >(wb.capacity - wb.length , in_length - 8 );
64
+ for (size_t i = 0 ; i < in_length; i++)
65
+ {
66
+ footer = reinterpret_cast <DepthSubPacketFooter *>(&buffer[i]);
74
67
75
- for (; in_offset < max_length; ++in_offset )
68
+ if (footer-> magic0 == 0x0 && footer-> magic1 == 0x9 && footer-> subsequence != 9 )
76
69
{
77
- footer = reinterpret_cast <DepthSubPacketFooter *>(ptr_in);
78
-
79
- if (footer->magic0 == 0x0 && footer->magic1 == 0x9 )
70
+ if (next_subsequence_ == footer->subsequence )
80
71
{
81
- footer_found = true ;
82
- break ;
72
+ // last part of current subsequence so copy up to where footer is found.
73
+ memcpy (&fb.data [fb.length ], buffer, i);
74
+ fb.length += i;
75
+ next_subsequence_ = footer->subsequence + 1 ;
83
76
}
84
-
85
- *ptr_out = *ptr_in;
86
- ++ptr_in;
87
- ++ptr_out;
77
+ else
78
+ {
79
+ // reset buffer if we get a sequence out of order.
80
+ std::cerr << " [DepthPacketStreamParser::handleNewData] Subsequence out of order. Got: " << footer->subsequence << " expected: " << next_subsequence_ << std::endl;
81
+ fb.length = 0 ;
82
+ next_subsequence_ = 0 ;
83
+ }
84
+ return ;
88
85
}
86
+ else if (footer->magic0 == 0x0 && footer->magic1 == 0x9 && footer->subsequence == 9 )
87
+ {
88
+ // got the last subsequence so copy up to where footer is found
89
+ next_subsequence_ = 0 ;
89
90
90
- wb.length = ptr_out - wb.data ;
91
+ memcpy (&fb.data [fb.length ], buffer, i);
92
+ fb.length += i;
91
93
92
- if (footer_found)
93
- {
94
- if ((in_length - in_offset) < sizeof (DepthSubPacketFooter))
95
- {
96
- std::cerr << " [DepthPacketStreamParser::handleNewData] incomplete footer detected!" << std::endl;
97
- }
98
- else if (footer->length > wb.length )
99
- {
100
- std::cerr << " [DepthPacketStreamParser::handleNewData] image data too short!" << std::endl;
101
- }
102
- else
94
+ // does the received amount of data match expected
95
+ if (fb.length == fb.capacity )
103
96
{
104
- if (current_sequence_ != footer-> sequence )
97
+ if (processor_-> ready () )
105
98
{
106
- if (current_subsequence_ == 0x3ff )
107
- {
108
- if (processor_->ready ())
109
- {
110
- buffer_.swap ();
111
-
112
- DepthPacket packet;
113
- packet.sequence = current_sequence_;
114
- packet.buffer = buffer_.back ().data ;
115
- packet.buffer_length = buffer_.back ().length ;
116
-
117
- processor_->process (packet);
118
- }
119
- else
120
- {
121
- // std::cerr << "[DepthPacketStreamParser::handleNewData] skipping depth packet!" << std::endl;
122
- }
123
- }
124
- else
125
- {
126
- std::cerr << " [DepthPacketStreamParser::handleNewData] not all subsequences received " << current_subsequence_ << std::endl;
127
- }
128
-
129
- current_sequence_ = footer->sequence ;
130
- current_subsequence_ = 0 ;
131
- }
99
+ buffer_.swap ();
132
100
133
- Buffer &fb = buffer_.front ();
101
+ DepthPacket packet;
102
+ packet.sequence = footer->sequence ;
103
+ packet.timestamp = footer->timestamp ;
104
+ packet.buffer = buffer_.back ().data ;
105
+ packet.buffer_length = buffer_.back ().length ;
134
106
135
- // set the bit corresponding to the subsequence number to 1
136
- current_subsequence_ |= 1 << footer->subsequence ;
107
+ processor_->process (packet);
137
108
138
- if (footer->subsequence * footer->length > fb.length )
139
- {
140
- std::cerr << " [DepthPacketStreamParser::handleNewData] front buffer too short! subsequence number is " << footer->subsequence << std::endl;
141
109
}
142
110
else
143
111
{
144
- memcpy (fb. data + (footer-> subsequence * footer-> length ), wb. data + (wb. length - footer-> length ), footer-> length ) ;
112
+ std::cerr << " [DepthPacketStreamParser::handleNewData] skipping depth packet! " << std::endl ;
145
113
}
146
- }
147
114
148
- // reset working buffer
149
- wb.length = 0 ;
150
- // skip header
151
- in_offset += sizeof (DepthSubPacketFooter);
152
- }
153
- else
154
- {
155
- if ((wb.length + 8 ) >= wb.capacity )
156
- {
157
- std::cerr << " [DepthPacketStreamParser::handleNewData] working buffer full, resetting it!" << std::endl;
158
- wb.length = 0 ;
159
- ptr_out = wb.data ;
115
+ // if a complete packet is processed or skipped, reset buffer.
116
+ fb.length = 0 ;
117
+ return ;
160
118
}
161
-
162
- // copy remaining 8 bytes
163
- if ((in_length - in_offset) != 8 )
164
- {
165
- std::cerr << " [DepthPacketStreamParser::handleNewData] remaining data should be 8 bytes, but is " << (in_length - in_offset) << std::endl;
166
- }
167
-
168
- for (; in_offset < in_length; ++in_offset)
119
+ else
169
120
{
170
- *ptr_out = *ptr_in;
171
- ++ptr_in;
172
- ++ptr_out;
121
+ // if data amount doesn't match, reset buffer.
122
+ std::cerr << " [DepthPacketStreamParser::handleNewData] Depth packet not complete - resetting buffer" << std::endl;
123
+ fb.length = 0 ;
124
+ return ;
173
125
}
174
-
175
- wb.length = ptr_out - wb.data ;
176
126
}
177
127
}
128
+ // copy data if space
129
+ if (fb.length + in_length > fb.capacity )
130
+ {
131
+ std::cerr << " [DepthPacketStreamParser::handleNewData] Buffer full - reseting" << std::endl;
132
+ fb.length = 0 ;
133
+ }
134
+
135
+ memcpy (&fb.data [fb.length ], buffer, in_length);
136
+ fb.length += in_length;
178
137
}
179
138
180
139
} /* namespace libfreenect2 */
0 commit comments