/alps/ipecamera

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/ipecamera

« back to all changes in this revision

Viewing changes to data.c

  • Committer: Suren A. Chilingaryan
  • Date: 2015-08-13 20:22:25 UTC
  • Revision ID: csa@suren.me-20150813202225-6pz4wlktd4u0fj77
Improve debugging of dropped frames

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
    uint16_t *pixels;
41
41
    
42
42
    int buf_ptr = ipecamera_resolve_event_id(ctx, event_id);
43
 
    if (buf_ptr < 0) return PCILIB_ERROR_TIMEOUT;
 
43
    if (buf_ptr < 0) return PCILIB_ERROR_OVERWRITTEN;
44
44
    
45
45
    if (ctx->frame[buf_ptr].event.image_ready) return 0;
46
46
    
58
58
 
59
59
    res = ufo_decoder_decode_frame(ctx->ipedec, ctx->buffer + buf_ptr * ctx->padded_size, ctx->frame[buf_ptr].event.raw_size, pixels, &ctx->frame[buf_ptr].event.meta);
60
60
    if (!res) {
61
 
        pcilib_warning("Decoding of frame %u has failed, ufodecode returns error %i", ctx->event_id, res);
 
61
        ipecamera_debug(HARDWARE, "Decoding of frame %u has failed, ufodecode returns error %i", ctx->event_id, res);
62
62
        ipecamera_debug_buffer(BROKEN_FRAMES, ctx->frame[buf_ptr].event.raw_size, ctx->buffer + buf_ptr * ctx->padded_size, PCILIB_DEBUG_BUFFER_MKDIR, "broken_frame.%4lu", ctx->event_id);
63
 
        err = PCILIB_ERROR_FAILED;
 
63
        err = PCILIB_ERROR_INVALID_DATA;
64
64
        ctx->frame[buf_ptr].event.image_broken = err;
65
65
        goto ready;
66
66
    }
72
72
 
73
73
    if (ipecamera_resolve_event_id(ctx, event_id) < 0) {
74
74
        ctx->frame[buf_ptr].event.image_ready = 0;
75
 
        return PCILIB_ERROR_TIMEOUT;
 
75
        return PCILIB_ERROR_OVERWRITTEN;
76
76
    }
77
 
    
 
77
 
78
78
    return err;
79
79
}
80
80
 
92
92
        return -1;
93
93
    }
94
94
 
95
 
    if ((ctx->event_id - ctx->preproc_id) > (ctx->buffer_size - IPECAMERA_RESERVE_BUFFERS)) ctx->preproc_id = ctx->event_id - (ctx->buffer_size - 1 - IPECAMERA_RESERVE_BUFFERS - 1);
 
95
    if ((ctx->event_id - ctx->preproc_id) > (ctx->buffer_size - IPECAMERA_RESERVE_BUFFERS)) {
 
96
        size_t preproc_id = ctx->preproc_id;
 
97
        ctx->preproc_id = ctx->event_id - (ctx->buffer_size - 1 - IPECAMERA_RESERVE_BUFFERS - 1);
 
98
        ipecamera_debug(HARDWARE, "Skipping events %zu to %zu as decoding is not fast enough. We are currently %zu buffers beyond, but only %zu buffers are available and safety limit is %zu",
 
99
            preproc_id, ctx->preproc_id - 1, ctx->event_id - ctx->preproc_id, ctx->buffer_size, IPECAMERA_RESERVE_BUFFERS);
 
100
    }
96
101
 
97
102
    res = ctx->preproc_id%ctx->buffer_size;
98
103
 
99
104
    if (pthread_rwlock_trywrlock(&ctx->frame[res].mutex)) {
100
105
        pthread_mutex_unlock(&ctx->preproc_mutex);
 
106
        ipecamera_debug(HARDWARE, "Can't lock buffer %i", res);
101
107
        return -1;
102
108
    }
103
109
    
111
117
 
112
118
 
113
119
void *ipecamera_preproc_thread(void *user) {
 
120
    int err;
114
121
    int buf_ptr;
115
122
    pcilib_event_id_t evid;
116
123
    
124
131
            continue;
125
132
        }
126
133
        
127
 
        ipecamera_decode_frame(ctx, evid);
 
134
        err = ipecamera_decode_frame(ctx, evid);
128
135
        
129
136
        pthread_rwlock_unlock(&ctx->frame[buf_ptr].mutex);
 
137
 
 
138
#ifdef IPECAMERA_DEBUG_HARDWARE
 
139
        if (err) {
 
140
            switch (err) {
 
141
             case PCILIB_ERROR_OVERWRITTEN:
 
142
                ipecamera_debug(HARDWARE, "The frame (%zu) was overwritten while preprocessing", evid);
 
143
             break;
 
144
             case PCILIB_ERROR_INVALID_DATA:
 
145
                ipecamera_debug(HARDWARE, "The frame (%zu) is corrupted, decoding have failed", evid);
 
146
             break;
 
147
             default:
 
148
                ipecamera_debug(HARDWARE, "The frame (%zu) is corrupted, decoding have failed", evid);
 
149
            }
 
150
        }
130
151
    }
 
152
#endif /* IPECAMERA_DEBUG_HARDWARE */
131
153
 
132
154
    return NULL;
133
155
}
138
160
    
139
161
    if (ctx->preproc) { 
140
162
        if (ctx->frame[buf_ptr].event.image_broken)
141
 
                return ctx->frame[buf_ptr].event.image_broken;
 
163
            return ctx->frame[buf_ptr].event.image_broken;
142
164
    } else {
143
165
        pthread_rwlock_rdlock(&ctx->frame[buf_ptr].mutex);
144
166
 
151
173
        
152
174
        return 0;
153
175
    }
154
 
    
155
 
    
 
176
 
 
177
 
156
178
    while (!ctx->frame[buf_ptr].event.image_ready) {
157
179
        usleep(IPECAMERA_NOFRAME_PREPROC_SLEEP);
158
180
 
159
181
        buf_ptr = ipecamera_resolve_event_id(ctx, event_id);
160
182
        if (buf_ptr < 0) return PCILIB_ERROR_OVERWRITTEN;
161
 
    }   
 
183
    }
162
184
 
163
185
    pthread_rwlock_rdlock(&ctx->frame[buf_ptr].mutex);
164
 
    
 
186
 
165
187
    buf_ptr = ipecamera_resolve_event_id(ctx, event_id);
166
188
    if ((buf_ptr < 0)||(!ctx->frame[buf_ptr].event.image_ready)) {
167
189
        pthread_rwlock_unlock(&ctx->frame[buf_ptr].mutex);
168
190
        return PCILIB_ERROR_OVERWRITTEN;
169
191
    }
170
 
    
171
 
    return 0;    
 
192
 
 
193
    return 0;
172
194
}
173
195
 
174
196
 
194
216
 
195
217
    buf_ptr = ipecamera_resolve_event_id(ctx, event_id);
196
218
    if (buf_ptr < 0) {
197
 
        pcilib_warning("The data of requested frame %zu was overwritten", event_id);
 
219
        ipecamera_debug(HARDWARE, "The data of the requested frame %zu has been meanwhile overwritten", event_id);
198
220
        return PCILIB_ERROR_OVERWRITTEN;
199
221
    }
200
222
 
202
224
        case IPECAMERA_RAW_DATA:
203
225
            raw_size = ctx->frame[buf_ptr].event.raw_size;
204
226
            if (data) {
205
 
                if ((!size)||(*size < raw_size)) return PCILIB_ERROR_TOOBIG;
 
227
                if ((!size)||(*size < raw_size)) {
 
228
                    pcilib_warning("The raw data associated with frame %zu is too big (%zu bytes) for user supplied buffer (%zu bytes)", event_id, raw_size, (size?*size:0));
 
229
                    return PCILIB_ERROR_TOOBIG;
 
230
                }
206
231
                memcpy(data, ctx->buffer + buf_ptr * ctx->padded_size, raw_size);
207
 
                if (ipecamera_resolve_event_id(ctx, event_id) < 0) return PCILIB_ERROR_OVERWRITTEN;
 
232
                if (ipecamera_resolve_event_id(ctx, event_id) < 0) {
 
233
                    ipecamera_debug(HARDWARE, "The data of requested frame %zu was overwritten while copying", event_id);
 
234
                    return PCILIB_ERROR_OVERWRITTEN;
 
235
                }
208
236
                *size = raw_size;
209
237
                return 0;
210
238
            }
213
241
            return 0;
214
242
        case IPECAMERA_IMAGE_DATA:
215
243
            err = ipecamera_get_frame(ctx, event_id);
216
 
            if (err) return err;
 
244
            if (err) {
 
245
#ifdef IPECAMERA_DEBUG_HARDWARE
 
246
                switch (err) {
 
247
                 case PCILIB_ERROR_OVERWRITTEN:
 
248
                    ipecamera_debug(HARDWARE, "The requested frame (%zu) was overwritten", event_id);
 
249
                    break;
 
250
                 case PCILIB_ERROR_INVALID_DATA:
 
251
                    ipecamera_debug(HARDWARE, "The requested frame (%zu) is corrupted", event_id);
 
252
                    break;
 
253
                 default:
 
254
                    ipecamera_debug(HARDWARE, "Error getting the data associated with the requested frame (%zu), error %i", event_id, err);
 
255
                }
 
256
#endif /* IPECAMERA_DEBUG_HARDWARE */
 
257
                return err;
 
258
            }
217
259
 
218
260
            if (data) {
219
 
                if ((!size)||(*size < ctx->image_size * sizeof(ipecamera_pixel_t))) return PCILIB_ERROR_TOOBIG;
 
261
                if ((!size)||(*size < ctx->image_size * sizeof(ipecamera_pixel_t))) {
 
262
                    pcilib_warning("The image associated with frame %zu is too big (%zu bytes) for user supplied buffer (%zu bytes)", event_id, ctx->image_size * sizeof(ipecamera_pixel_t), (size?*size:0));
 
263
                    return PCILIB_ERROR_TOOBIG;
 
264
                }
220
265
                memcpy(data, ctx->image + buf_ptr * ctx->image_size, ctx->image_size * sizeof(ipecamera_pixel_t));
221
266
                pthread_rwlock_unlock(&ctx->frame[buf_ptr].mutex);
222
267
                *size =  ctx->image_size * sizeof(ipecamera_pixel_t);