Skip to content

Commit 8f09a8c

Browse files
committed
Squelch streaming errors until two valid frames arrive
Signed-off-by: Hector Martin <[email protected]>
1 parent f7f74db commit 8f09a8c

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

c/lib/cameras.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ static int stream_process(freenect_context *ctx, packet_stream *strm, uint8_t *p
5555
int datalen = len - sizeof(*hdr);
5656

5757
if (hdr->magic[0] != 'R' || hdr->magic[1] != 'B') {
58-
// this is currently expected on init
59-
FN_DEBUG("[Stream %02x] Invalid magic %02x%02x\n", strm->flag, hdr->magic[0], hdr->magic[1]);
58+
FN_LOG(strm->valid_frames < 2 ? LL_SPEW : LL_NOTICE, \
59+
"[Stream %02x] Invalid magic %02x%02x\n", strm->flag, hdr->magic[0], hdr->magic[1]);
6060
return 0;
6161
}
6262

@@ -84,9 +84,11 @@ static int stream_process(freenect_context *ctx, packet_stream *strm, uint8_t *p
8484
// handle lost packets
8585
if (strm->seq != hdr->seq) {
8686
uint8_t lost = strm->seq - hdr->seq;
87-
FN_INFO("[Stream %02x] Lost %d packets\n", strm->flag, lost);
87+
FN_LOG(strm->valid_frames < 2 ? LL_SPEW : LL_INFO, \
88+
"[Stream %02x] Lost %d packets\n", strm->flag, lost);
8889
if (lost > 5) {
89-
FN_NOTICE("[Stream %02x] Lost too many packets, resyncing...\n", strm->flag);
90+
FN_LOG(strm->valid_frames < 2 ? LL_SPEW : LL_NOTICE, \
91+
"[Stream %02x] Lost too many packets, resyncing...\n", strm->flag);
9092
strm->synced = 0;
9193
return 0;
9294
}
@@ -98,6 +100,7 @@ static int stream_process(freenect_context *ctx, packet_stream *strm, uint8_t *p
98100
strm->got_pkts = 0;
99101
got_frame = 1;
100102
strm->timestamp = strm->last_timestamp;
103+
strm->valid_frames++;
101104
} else {
102105
strm->pkt_num += lost;
103106
}
@@ -107,20 +110,23 @@ static int stream_process(freenect_context *ctx, packet_stream *strm, uint8_t *p
107110
if (!(strm->pkt_num == 0 && hdr->flag == sof) &&
108111
!(strm->pkt_num == strm->pkts_per_frame-1 && hdr->flag == eof) &&
109112
!(strm->pkt_num > 0 && strm->pkt_num < strm->pkts_per_frame-1 && hdr->flag == mof)) {
110-
FN_NOTICE("[Stream %02x] Inconsistent flag %02x with %d packets in buf (%d total), resyncing...\n",
113+
FN_LOG(strm->valid_frames < 2 ? LL_SPEW : LL_NOTICE, \
114+
"[Stream %02x] Inconsistent flag %02x with %d packets in buf (%d total), resyncing...\n",
111115
strm->flag, hdr->flag, strm->pkt_num, strm->pkts_per_frame);
112116
strm->synced = 0;
113-
return 0;
117+
return got_frame;
114118
}
115119

116120
// copy data
117121
if (datalen > strm->pkt_size) {
118-
FN_WARNING("[Stream %02x] Expected %d data bytes, but got %d. Dropping...\n", strm->flag, strm->pkt_size, datalen);
119-
return 0;
122+
FN_LOG(strm->valid_frames < 2 ? LL_SPEW : LL_WARNING, \
123+
"[Stream %02x] Expected %d data bytes, but got %d. Dropping...\n", strm->flag, strm->pkt_size, datalen);
124+
return got_frame;
120125
}
121126

122127
if (datalen != strm->pkt_size && hdr->flag != eof)
123-
FN_WARNING("[Stream %02x] Expected %d data bytes, but got only %d\n", strm->flag, strm->pkt_size, datalen);
128+
FN_LOG(strm->valid_frames < 2 ? LL_SPEW : LL_WARNING, \
129+
"[Stream %02x] Expected %d data bytes, but got only %d\n", strm->flag, strm->pkt_size, datalen);
124130

125131
uint8_t *dbuf = strm->buf + strm->pkt_num * strm->pkt_size;
126132
memcpy(dbuf, data, datalen);
@@ -136,6 +142,7 @@ static int stream_process(freenect_context *ctx, packet_stream *strm, uint8_t *p
136142
strm->valid_pkts = strm->got_pkts;
137143
strm->got_pkts = 0;
138144
strm->timestamp = hdr->timestamp;
145+
strm->valid_frames++;
139146
return 1;
140147
} else {
141148
return got_frame;
@@ -386,6 +393,7 @@ int freenect_start_depth(freenect_device *dev)
386393
dev->depth_stream.pkt_size = DEPTH_PKTDSIZE;
387394
dev->depth_stream.synced = 0;
388395
dev->depth_stream.flag = 0x70;
396+
dev->depth_stream.valid_frames = 0;
389397

390398
res = fnusb_start_iso(&dev->usb_cam, &dev->depth_isoc, depth_process, 0x82, NUM_XFERS, PKTS_PER_XFER, DEPTH_PKTBUF);
391399
if (res < 0)
@@ -423,6 +431,7 @@ int freenect_start_rgb(freenect_device *dev)
423431
dev->rgb_stream.pkt_size = RGB_PKTDSIZE;
424432
dev->rgb_stream.synced = 0;
425433
dev->rgb_stream.flag = 0x80;
434+
dev->rgb_stream.valid_frames = 0;
426435

427436
res = fnusb_start_iso(&dev->usb_cam, &dev->rgb_isoc, rgb_process, 0x81, NUM_XFERS, PKTS_PER_XFER, RGB_PKTBUF);
428437
if (res < 0)

c/lib/freenect_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ typedef struct {
9292
int pkts_per_frame;
9393
int pkt_size;
9494
int valid_pkts;
95+
int valid_frames;
9596
uint32_t last_timestamp;
9697
uint32_t timestamp;
9798
uint8_t *buf;

0 commit comments

Comments
 (0)