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; }
/* Video Transmit loop * * only transmit Video data , fetch data from webcam , and ecoder that as YUV420 frame . */ static void *Video_Tx_loop() { unsigned int count = 0; /* init socket data*/ int Tx_socket ; struct sockaddr_in Tx_addr; if((Tx_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { printf("Socket Faile\n"); exit(-1); } Tx_addr.sin_family = AF_INET; Tx_addr.sin_port = htons(VIDEO_NET_PORT); Tx_addr.sin_addr.s_addr=inet_addr(NET_HOST); /* Webcam init */ sys_info.cam.handle = webcam_open(); webcam_show_info(sys_info.cam.handle); webcam_init(sys_info.cam.width, sys_info.cam.height, sys_info.cam.handle); webcam_set_framerate(sys_info.cam.handle, 15); webcam_start_capturing(sys_info.cam.handle); struct timeval t_start,t_end; double uTime =0.0; int total_size=0; struct vbuffer *webcam_buf; /* start video capture and transmit */ printf("Video Tx Looping\n"); gettimeofday(&t_start, NULL); while(sys_get_status() != SYS_STATUS_RELEASE) { /* start Tx Video */ while (sys_get_status() == SYS_STATUS_WORK) { webcam_buf = wait_webcam_data(); if (webcam_buf !=NULL) { count++; struct AVPacket *pkt_ptr = video_encoder(webcam_buf->start); /* ------------------------------------- */ /* direct display in localhost */ unsigned char *RGB_buffer = NULL; video_decoder(pkt_ptr->data, pkt_ptr->size, &RGB_buffer); RGB24_to_RGB565(RGB_buffer, RGB565_buffer, sys_info.cam.width, sys_info.cam.height); FB_display(RGB565_buffer, 0, 0, sys_info.cam.width, sys_info.cam.height); total_size += pkt_ptr->size; /* network transmit */ struct VOD_DataPacket_struct Tx_Buffer; /* Tx header */ Tx_Buffer.DataType = VOD_PACKET_TYPE_FRAME_HEADER; memcpy(&Tx_Buffer.header, pkt_ptr, sizeof(AVPacket)); sendto(Tx_socket, (char *)&Tx_Buffer, sizeof(Tx_Buffer), 0, (struct sockaddr *)&Tx_addr, sizeof(struct sockaddr_in)); /* Tx data , 1024 bit per transmit */ int offset =1024 ; int remain_size = pkt_ptr->size ; Tx_Buffer.DataType = VOD_PACKET_TYPE_FRAME_DATA; Tx_Buffer.data.ID = 0 ; while(remain_size > 0) { if (remain_size < 1024) { /* verify transmit data size*/ Tx_Buffer.data.size = remain_size ; } else { Tx_Buffer.data.size = 1024 ; } memcpy(&Tx_Buffer.data.data[0], pkt_ptr->data + Tx_Buffer.data.ID * 1024, sizeof(char) * Tx_Buffer.data.size); sendto(Tx_socket, (char *)&Tx_Buffer, sizeof(Tx_Buffer), 0, (struct sockaddr *)&Tx_addr, sizeof(struct sockaddr_in)); Tx_Buffer.data.ID ++ ; remain_size -= Tx_Buffer.data.size ; } } else { printf("webcam_read_frame error\n"); } } usleep(50000); } gettimeofday(&t_end, NULL); uTime = (t_end.tv_sec -t_start.tv_sec)*1000000.0 +(t_end.tv_usec -t_start.tv_usec); printf("Total size : %d bit , frame count : %d\n", total_size, count); printf("Time :%lf us\n", uTime); printf("kb/s %lf\n",total_size/ (uTime/1000.0)); webcam_stop_capturing(sys_info.cam.handle); webcam_release(sys_info.cam.handle); close(sys_info.cam.handle); pthread_exit(NULL); }