<commit_before>#include <stdlib.h>
#include <string.h>
#define __USE_BSD
#include <unistd.h>
#undef __USE_BSD
#include <assert.h>

#include "config.h"
#include "uca.h"
#include "uca-cam.h"
#include "uca-grabber.h"

#ifdef HAVE_PTHREADS
#include <pthread.h>
#endif

/**
 * User structure for the dummy camera.
 */
typedef struct dummy_cam {
    uint32_t bitdepth;
    uint32_t frame_rate;
#ifdef HAVE_PTHREADS
    pthread_t   grab_thread;
    bool        thread_running;
    char        *buffer;
#endif
} dummy_cam_t;


static const char digits[10][20] = {
    /* 0 */
    { 0x00, 0xff, 0xff, 0x00,
      0xff, 0x00, 0x00, 0xff,
      0xff, 0x00, 0x00, 0xff,
      0xff, 0x00, 0x00, 0xff,
      0x00, 0xff, 0xff, 0x00 },
    /* 1 */
    { 0x00, 0x00, 0xff, 0x00,
      0x00, 0xff, 0xff, 0x00,
      0x00, 0x00, 0xff, 0x00,
      0x00, 0x00, 0xff, 0x00,
      0x00, 0x00, 0xff, 0x00 },
    /* 2 */
    { 0x00, 0xff, 0xff, 0x00,
      0xff, 0x00, 0x00, 0xff,
      0x00, 0x00, 0xff, 0x00,
      0x00, 0xff, 0x00, 0x00,
      0xff, 0xff, 0xff, 0xff },
    /* 3 */
    { 0x00, 0xff, 0xff, 0x00,
      0xff, 0x00, 0x00, 0xff,
      0x00, 0x00, 0xff, 0x00,
      0xff, 0x00, 0x00, 0xff,
      0x00, 0xff, 0xff, 0x00 },
    /* 4 */
    { 0xff, 0x00, 0x00, 0x00,
      0xff, 0x00, 0x00, 0xff,
      0xff, 0xff, 0xff, 0xff,
      0x00, 0x00, 0x00, 0xff,
      0x00, 0x00, 0x00, 0xff },
    /* 5 */
    { 0xff, 0xff, 0xff, 0xff,
      0xff, 0x00, 0x00, 0x00,
      0x00, 0xff, 0xff, 0x00,
      0x00, 0x00, 0x00, 0xff,
      0xff, 0xff, 0xff, 0x00 },
    /* 6 */
    { 0x00, 0xff, 0xff, 0xff,
      0xff, 0x00, 0x00, 0x00,
      0xff, 0xff, 0xff, 0x00,
      0xff, 0x00, 0x00, 0xff,
      0x00, 0xff, 0xff, 0x00 },
    /* 7 */
    { 0xff, 0xff, 0xff, 0xff,
      0x00, 0x00, 0x00, 0xff,
      0x00, 0x00, 0xff, 0x00,
      0x00, 0xff, 0x00, 0x00,
      0xff, 0x00, 0x00, 0x00 },
    /* 8 */
    { 0x00, 0xff, 0xff, 0x00,
      0xff, 0x00, 0x00, 0xff,
      0x00, 0xff, 0xff, 0x00,
      0xff, 0x00, 0x00, 0xff,
      0x00, 0xff, 0xff, 0x00 },
    /* 9 */
    { 0x00, 0xff, 0xff, 0x00,
      0xff, 0x00, 0x00, 0xff,
      0x00, 0xff, 0xff, 0xff,
      0x00, 0x00, 0x00, 0xff,
      0xff, 0xff, 0xff, 0x00 }
};

const int DIGIT_WIDTH = 4;
const int DIGIT_HEIGHT = 5;

#define GET_DUMMY(uca) ((struct dummy_cam *)(uca->user))
#define set_void(p, type, value) { *((type *) p) = value; }

static void uca_dummy_print_number(char *buffer, int number, int x, int y, int width)
{
    for (int i = 0; i < DIGIT_WIDTH; i++) {
        for (int j = 0; j < DIGIT_HEIGHT; j++) {
            buffer[(y+j)*width + (x+i)] = digits[number][j*DIGIT_WIDTH+i];
        }
    }
}

static void uca_dummy_memcpy(struct uca_camera *cam, char *buffer)
{
    /* print current frame number */
    unsigned int number = cam->current_frame;
    unsigned int divisor = 100000000;
    int x = 10;
    while (divisor > 1) {
        uca_dummy_print_number(buffer, number / divisor, x, 10, cam->frame_width);
        number = number % divisor;
        divisor = divisor / 10;
        x += 5;
    }
}

static void *uca_dummy_grab_thread(void *arg)
{
    struct uca_camera *cam = ((struct uca_camera *) arg);
    struct dummy_cam *dc = GET_DUMMY(cam);

    assert(dc->frame_rate > 0);
    const __useconds_t sleep_time = (unsigned int) 1000000.0f / dc->frame_rate;

    while (dc->thread_running) {
        uca_dummy_memcpy(cam, dc->buffer);
        cam->callback(cam->current_frame, dc->buffer, cam->user_callback);
        cam->current_frame++;
        usleep(sleep_time);
    }
    return NULL;
}


/*
 * --- interface implementations ----------------------------------------------
 */
static uint32_t uca_dummy_set_property(struct uca_camera *cam, enum uca_property_ids property, void *data)
{
    if (cam->state == UCA_CAM_RECORDING)
        return UCA_ERR_PROP_CAMERA_RECORDING;

    switch (property) {
        case UCA_PROP_WIDTH:
            cam->frame_width = *((uint32_t *) data);
            break;
<commit_msg>Check call time and update current_frame with missed frames<commit_after>#include <stdlib.h>
#include <string.h>
#define __USE_BSD
#include <unistd.h>
#undef __USE_BSD
#include <sys/time.h>
#include <assert.h>

#include "config.h"
#include "uca.h"
#include "uca-cam.h"
#include "uca-grabber.h"

#ifdef HAVE_PTHREADS
#include <pthread.h>
#endif

/**
 * User structure for the dummy camera.
 */
typedef struct dummy_cam {
    uint32_t bitdepth;
    uint32_t frame_rate;
#ifdef HAVE_PTHREADS
    pthread_t   grab_thread;
    bool        thread_running;
    char        *buffer;
#endif
} dummy_cam_t;


static const char digits[10][20] = {
    /* 0 */
    { 0x00, 0xff, 0xff, 0x00,
      0xff, 0x00, 0x00, 0xff,
      0xff, 0x00, 0x00, 0xff,
      0xff, 0x00, 0x00, 0xff,
      0x00, 0xff, 0xff, 0x00 },
    /* 1 */
    { 0x00, 0x00, 0xff, 0x00,
      0x00, 0xff, 0xff, 0x00,
      0x00, 0x00, 0xff, 0x00,
      0x00, 0x00, 0xff, 0x00,
      0x00, 0x00, 0xff, 0x00 },
    /* 2 */
    { 0x00, 0xff, 0xff, 0x00,
      0xff, 0x00, 0x00, 0xff,
      0x00, 0x00, 0xff, 0x00,
      0x00, 0xff, 0x00, 0x00,
      0xff, 0xff, 0xff, 0xff },
    /* 3 */
    { 0x00, 0xff, 0xff, 0x00,
      0xff, 0x00, 0x00, 0xff,
      0x00, 0x00, 0xff, 0x00,
      0xff, 0x00, 0x00, 0xff,
      0x00, 0xff, 0xff, 0x00 },
    /* 4 */
    { 0xff, 0x00, 0x00, 0x00,
      0xff, 0x00, 0x00, 0xff,
      0xff, 0xff, 0xff, 0xff,
      0x00, 0x00, 0x00, 0xff,
      0x00, 0x00, 0x00, 0xff },
    /* 5 */
    { 0xff, 0xff, 0xff, 0xff,
      0xff, 0x00, 0x00, 0x00,
      0x00, 0xff, 0xff, 0x00,
      0x00, 0x00, 0x00, 0xff,
      0xff, 0xff, 0xff, 0x00 },
    /* 6 */
    { 0x00, 0xff, 0xff, 0xff,
      0xff, 0x00, 0x00, 0x00,
      0xff, 0xff, 0xff, 0x00,
      0xff, 0x00, 0x00, 0xff,
      0x00, 0xff, 0xff, 0x00 },
    /* 7 */
    { 0xff, 0xff, 0xff, 0xff,
      0x00, 0x00, 0x00, 0xff,
      0x00, 0x00, 0xff, 0x00,
      0x00, 0xff, 0x00, 0x00,
      0xff, 0x00, 0x00, 0x00 },
    /* 8 */
    { 0x00, 0xff, 0xff, 0x00,
      0xff, 0x00, 0x00, 0xff,
      0x00, 0xff, 0xff, 0x00,
      0xff, 0x00, 0x00, 0xff,
      0x00, 0xff, 0xff, 0x00 },
    /* 9 */
    { 0x00, 0xff, 0xff, 0x00,
      0xff, 0x00, 0x00, 0xff,
      0x00, 0xff, 0xff, 0xff,
      0x00, 0x00, 0x00, 0xff,
      0xff, 0xff, 0xff, 0x00 }
};

const int DIGIT_WIDTH = 4;
const int DIGIT_HEIGHT = 5;

#define GET_DUMMY(uca) ((struct dummy_cam *)(uca->user))
#define set_void(p, type, value) { *((type *) p) = value; }

static void uca_dummy_print_number(char *buffer, int number, int x, int y, int width)
{
    for (int i = 0; i < DIGIT_WIDTH; i++) {
        for (int j = 0; j < DIGIT_HEIGHT; j++) {
            buffer[(y+j)*width + (x+i)] = digits[number][j*DIGIT_WIDTH+i];
        }
    }
}

static void uca_dummy_memcpy(struct uca_camera *cam, char *buffer)
{
    /* print current frame number */
    unsigned int number = cam->current_frame;
    unsigned int divisor = 100000000;
    int x = 10;
    while (divisor > 1) {
        uca_dummy_print_number(buffer, number / divisor, x, 10, cam->frame_width);
        number = number % divisor;
        divisor = divisor / 10;
        x += 5;
    }
}

static __suseconds_t uca_dummy_time_diff(struct timeval *start, struct timeval *stop)
{
    return (stop->tv_sec - start->tv_sec)*1000000 + (stop->tv_usec - start->tv_usec);
}

static void *uca_dummy_grab_thread(void *arg)
{
    struct uca_camera *cam = ((struct uca_camera *) arg);
    struct dummy_cam *dc = GET_DUMMY(cam);

    assert(dc->frame_rate > 0);
    const __useconds_t sleep_time = (unsigned int) 1000000.0f / dc->frame_rate;
    __suseconds_t call_time;
    struct timeval start, stop;

    while (dc->thread_running) {
        uca_dummy_memcpy(cam, dc->buffer);
        gettimeofday(&start, NULL);
        cam->callback(cam->current_frame, dc->buffer, cam->user_callback);
        gettimeofday(&stop, NULL);

        call_time = uca_dummy_time_diff(&start, &stop);
        if (call_time < sleep_time) {
            cam->current_frame++;
            usleep(sleep_time - call_time);
        }
        else
            cam->current_frame += call_time / sleep_time;
    }
    return NULL;
}


/*
 * --- interface implementations ----------------------------------------------
 */
static uint32_t uca_dummy_set_property(struct uca_camera *cam, enum uca_property_ids property, void *data)
{
    if (cam->state == UCA_CAM_RECORDING)
        return UCA_ERR_PROP_CAMERA_RECORDING;

    switch (property) {
        case UCA_PROP_WIDTH:
            cam->frame_width = *((uint32_t *) data);
            break;

{'fraction_non_alphanumeric': 0.14818725800774374, 'fraction_numerical': 0.10923383785052211, 'mean_word_length': 2.3665086887835702, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5244751', 'n_tokens_mistral': 4227, 'n_tokens_neox': 3550, 'n_words': 862}