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