示例#1
0
void capture_encode_thread(void) {
	int count = 1;
	for (;;) {
		//SKY_LOG(,arg);
		//printf("\n\n-->this is the %dth frame\n", count);
		if (count++ >= 200) 
				{
			printf("------need to exit from thread------- \n");
			break;
		}

		fd_set fds;
		struct timeval tv;
		int r;

		FD_ZERO(&fds);
		FD_SET(cam->fd, &fds);

		/* Timeout. */
		tv.tv_sec = 2;
		tv.tv_usec = 0;

		r = select(cam->fd + 1, &fds, NULL, NULL, &tv);

		if (-1 == r) {
			if (EINTR == errno)
				continue;

			errno_exit("select");
		}

		if (0 == r) {
			fprintf(stderr, "select timeout\n");
			exit(EXIT_FAILURE);
		}

		/*
		if (read_yuv_frame(cam) != 1) {
			fprintf(stderr, "read_fram fail in thread\n");
			break;
		}
		*/
		int h264_length = 0 ; 
		if(!read_and_encode_frame(cam,enc,h264_buf,&h264_length))
		{
			fprintf(stderr, "read_fram fail in thread\n");
			break;
		}
	}
}
示例#2
0
int main(int argc, char **argv)
{
	struct camera *cam = NULL;

	cam = (struct camera *)malloc(sizeof(struct camera));
	if (!cam) {
		printf("malloc camera failure!\n");
		exit(1);
	}

	memset(cam, 0, sizeof(struct camera));
	cam->device_name = "/dev/video0";
	cam->buffers = NULL;
	cam->width = 640;
	cam->height = 480;
	cam->display_depth = 5;		/* RGB24 */
	cam->h264_file_name = "test.h264";

	camera_open(cam);
	camera_init(cam);
	camera_capturing_start(cam);
	h264_compress_init(&cam->en, cam->width, cam->height);
	cam->h264_buf = (uint8_t *) malloc(sizeof(uint8_t) * cam->width * cam->height * 3);	// 设置缓冲区
	if ((cam->h264_fp = fopen(cam->h264_file_name, "wa+")) == NULL) {
		printf("open file error!\n");
		return -1;
	}

	while (1) {
		if (read_and_encode_frame(cam) < 0) {
			fprintf(stderr, "read_fram fail in thread\n");
			//break;
		}
	}

	printf("-----------end program------------");
	if (cam->h264_fp != NULL)
		fclose(cam->h264_fp);
	h264_compress_uninit(&cam->en);
	free(cam->h264_buf);

	camera_capturing_stop(cam);
	camera_uninit(cam);
	camera_close(cam);

	free(cam);

	return 0;
}