/** * Handler for segmentation faults * This should go through all the opened webcams in _w and * clean them up. */ static void handler(int sig, siginfo_t *si, void *unused) { int i = 0; fprintf(stderr, "[v4l2] A segmentation fault occured. Cleaning up...\n"); for(i = 0; i < 16; i++) { if (_w[i] == NULL) continue; // If webcam is streaming, unlock the mutex, and stop streaming if (_w[i]->streaming) { pthread_mutex_unlock(&_w[i]->mtx_frame); webcam_stream(_w[i], false); } webcam_close(_w[i]); } exit(EXIT_FAILURE); }
int main(int argc, char **argv) { int i = 0; webcam_t *w = webcam_open("/dev/video0"); // Prepare frame, and filename, and file to store frame in buffer_t frame; frame.start = NULL; frame.length = 0; char *fn = calloc(16, sizeof(char)); FILE *fp; webcam_resize(w, 640, 480); webcam_stream(w, true); while(true) { webcam_grab(w, &frame); if (frame.length > 0) { printf("[v4l2] Storing frame %d\n", i); sprintf(fn, "frame_%d.rgb", i); fp = fopen(fn, "w+"); fwrite(frame.start, frame.length, 1, fp); fclose(fp); i++; } if (i > 10) break; } webcam_stream(w, false); webcam_close(w); if (frame.start != NULL) free(frame.start); free(fn); return 0; }
int main(int argc, char *argv[]) { struct optcfg_option options[] = { { 'p', "port", "Specify port number to serve", 0, "8888" }, { 'H', "host", "Specify host name to listen", 0, NULL }, { 'F', "fps", "Specify how many frames per second will be captured", 0, "5" }, { 'r', "root", "Specify root HTML directory", 0, NULL }, { 'c', "config", "Load configuration from file", OPTCFG_CFGFILE, NULL }, { 'd', "delay", "Sound delay and fragment size in seconds", 0, "1" }, { 'f', "frequency", "Sound frequency in Hz.", 0, "8000" }, { 'b', "bits", "Bits per fragment of sound", 0, "8" }, { 'S', "stereo", "Enable stereo mode", OPTCFG_FLAG, "no" }, { 'e', "exec", "Execute program as sound source", 0, NULL } }; unsigned options_cnt = sizeof(options) / sizeof(options[0]); struct optcfg *opts; int i; int cams[64]; unsigned cam_cnt = 64; webcam_t *cam; mihl_ctx_t *ctx; struct timeval last, cur; long dtime; int port; const char *host; int bsecs, freq, bits, stereo; const char *snd_cmd = NULL; const char *root = NULL; opts = optcfg_new(); if (!opts) { return EXIT_FAILURE; } if (optcfg_default_config(opts, "wwwcam")) { return EXIT_FAILURE; } if (optcfg_parse_options(opts, "wwwcam", argc - 1, argv + 1, options, options_cnt)) { return EXIT_FAILURE; } optcfg_save(opts, stdout); dtime = optcfg_get_int(opts, "fps", 5); if (dtime > 30 || dtime < 0) dtime = 5; dtime = 1000000 / dtime; port = optcfg_get_int(opts, "port", -1); if (port < 0) { fprintf(stderr, "Error: TCP port information is not found.\n"); return EXIT_FAILURE; } host = optcfg_get(opts, "host", NULL); root = optcfg_get(opts, "root", "."); snprintf(ROOT, sizeof(ROOT), "%s/", root); ctx = mihl_init(host, port, 16, MIHL_LOG_ERROR | MIHL_LOG_WARNING | MIHL_LOG_INFO | MIHL_LOG_INFO_VERBOSE); if (webcam_list(cams, &cam_cnt)) { fprintf(stderr, "Error: Can't get list of cameras!\n"); return 1; } if (cam_cnt == 0) { fprintf(stderr, "Error: no cameras connected!\n"); return 1; } if (!ctx) { fprintf(stderr, "Error: can't init mihl!\n"); return 1; } signal(SIGINT, sigint); cam = webcam_open(cams[0], 640, 480); if (!cam) { mihl_end(ctx); fprintf(stderr, "Error: can't open camera!\n"); return 1; } snprintf(CAM_NAME, sizeof(CAM_NAME), "WEBCAM: %s", cam->name); for (i = 0; handlers[i].path; i++) { if (handlers[i].get) { mihl_handle_get(ctx, handlers[i].path, handlers[i].get, handlers[i].param); } if (handlers[i].post) { mihl_handle_post(ctx, handlers[i].path, handlers[i].post, handlers[i].param); } } webcam_start(cam); printf("Waiting for the first frame..."); fflush(stdout); while (!FRAME) { if (webcam_wait_frame_cb(cam, new_frame, NULL, 10) < 0) { webcam_stop(cam); webcam_close(cam); mihl_end(ctx); fprintf(stderr, "Error: can't get frames from camera!\n"); return 1; } gettimeofday(&last, NULL); } printf(" Ok\n"); fflush(stdout); /* Init sound: */ snd_cmd = optcfg_get(opts, "exec", NULL); if (snd_cmd) { bsecs = optcfg_get_int(opts, "delay", 1); freq = optcfg_get_int(opts, "frequency", 8000); bits = optcfg_get_int(opts, "bits", 8); stereo = optcfg_get_flag(opts, "stereo"); printf("Starting sound...\n"); sound = snd_open(snd_cmd, bsecs, 8, freq, bits, stereo); if (!sound) { fprintf(stderr, "WARNING: can't open sound!\n"); } } for (;;) { int cam_status; int status = mihl_server(ctx); if (status == -2) break; gettimeofday(&cur, NULL); if (delta_time(&last, &cur) > dtime) { cam_status = webcam_wait_frame_cb(cam, new_frame, NULL, 10); if (cam_status < 0) break; if (cam_status > 0) memcpy(&last, &cur, sizeof(struct timeval)); } if (exit_now) break; } webcam_stop(cam); webcam_close(cam); if (sound) { snd_stop(sound); sound = NULL; } printf("EXIT!\n"); mihl_end(ctx); return 0; }