/* * test_capture.c -- tests for the camera layer (T7.1) * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. * * The test pattern is checked to the frame (size, changing content, non-blank * Y and chroma). A camera is opened when /dev/video0 exists: a frame must * arrive and be non-uniform; a machine without a camera (or with one that is * busy) skips that part, because the pattern is the fallback and its check is * the one that always runs. */ #include "dc_internal.h" #include #include #include static int fails; #define CHECK(cond) do { \ if (!(cond)) { \ fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, \ #cond); \ fails++; \ } \ } while (0) static int nonuniform(const unsigned char *p, size_t n) { size_t i; for (i = 1; i < n; i++) if (p[i] != p[0]) return 1; return 0; } int main(void) { unsigned char *a; unsigned char *b; DcCapture *cap; char err[256] = ""; size_t ysize = (size_t)DC_VIDEO_W * DC_VIDEO_H; int i; a = malloc(ysize * 3 / 2); b = malloc(ysize * 3 / 2); CHECK(a != NULL && b != NULL); if (a == NULL || b == NULL) return 1; /* the pattern: two frames differ, both have a Y and chroma signal */ cap = dc_capture_open(DC_VIDEO_W, DC_VIDEO_H, DC_VIDEO_FPS, "pattern", err, sizeof(err)); CHECK(cap != NULL); if (cap == NULL) { fprintf(stderr, "pattern: %s\n", err); free(a); free(b); return 1; } CHECK(strcmp(dc_capture_backend(cap), "test-pattern") == 0); CHECK(dc_capture_frame(cap, a, 0) == 1); CHECK(dc_capture_frame(cap, b, 0) == 1); CHECK(memcmp(a, b, ysize * 3 / 2) != 0); CHECK(nonuniform(a, ysize)); CHECK(nonuniform(a + ysize, ysize / 2)); /* the bad sizes are refused */ { DcCapture *bad = dc_capture_open(0, 0, 15, "pattern", err, sizeof(err)); CHECK(bad == NULL); } dc_capture_close(cap); /* a real camera, when this machine has one */ { FILE *f = fopen("/dev/video0", "r"); if (f == NULL) { printf("capture: no /dev/video0, camera skipped\n"); } else { fclose(f); cap = dc_capture_open(DC_VIDEO_W, DC_VIDEO_H, DC_VIDEO_FPS, "auto", err, sizeof(err)); if (cap == NULL) { printf("capture: camera not usable (%s), " "skipped\n", err); } else { int got = 0; printf("capture: camera is %s\n", dc_capture_backend(cap)); for (i = 0; i < 20 && !got; i++) got = dc_capture_frame(cap, a, 500); CHECK(got == 1); if (got == 1) CHECK(nonuniform(a, ysize)); /* a second size goes through the scaler */ dc_capture_close(cap); cap = dc_capture_open(320, 180, 15, "auto", err, sizeof(err)); CHECK(cap != NULL); if (cap != NULL) { got = 0; for (i = 0; i < 20 && !got; i++) got = dc_capture_frame(cap, a, 500); CHECK(got == 1); dc_capture_close(cap); } } } } free(a); free(b); if (fails != 0) { fprintf(stderr, "%d test(s) failed\n", fails); return 1; } printf("test_capture: all ok\n"); return 0; }