#include "stdint.h"
#include <stdlib.h>
#include "x264.h"
#include "x264_config.h"
#include "stdio.h"
/*
SOCK ADD
*/
#include<sys/types.h>
#include<sys/socket.h>
#include<errno.h>
#include<string.h>
#include<unistd.h>
#include<netinet/in.h>
#define PORT 8888
#define BUFFER_SIZE 1024
#define DSTIP "192.168.1.10"
#define TEXT "message"
int sockfd,sendbytes;
char buf[BUFFER_SIZE];
struct hostent *host;
struct sockaddr_in serv_addr;
/*
* v4l2Setp1.c
*
* Created on: 2011-8-9
* Author: jcracker
*/
#include<stdio.h>
#include<linux/videodev2.h>
#include<fcntl.h>
#include<malloc.h>
#include<sys/mman.h>
#include <assert.h>
int fd;
unsigned int min;
#define DEVICE "/dev/video0"
#define CLEAR(x) memset (&(x), 0, sizeof (x))
#define WIDTH 176
#define HEIGHT 144
#define FPS 6
static unsigned int n_buffers = 0;
struct buffer {
void * start;
size_t length;
};
struct x264_encoder{
x264_param_t param;
x264_t *x264;
x264_picture_t *pic_in;
x264_nal_t *headers;
x264_nal_t *nals;
int i_nal;
};
struct x264_encoder *x264_en=NULL ;
FILE *x264Stream;
int i_frame;
#define TOTAL_FRAME_NUMBER 50;
struct v4l2_capability cap;
struct v4l2_cropcap cropcap;
struct v4l2_crop crop;
struct v4l2_format fmt;
struct v4l2_requestbuffers req; //向驱动申请帧缓冲的请求,里面包含申请的个数
struct buffer * buffers = NULL, *buffers_target =NULL;
void deviceClose(){
if (-1 == close(fd))
printf("close device failed\n");
fd = -1;
}
void deviceUninit(){
unsigned int i;
for(i = 0;i<n_buffers;++i ){
if(-1 == munmap(buffers[i].start,buffers[i].length)){
printf("wrong~~~munmap\n");
}
}
free(buffers);
}
void captrueStop(){
enum v4l2_buf_type type;
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if(-1 == ioctl(fd,VIDIOC_STREAMOFF,&type)){
printf("wrong~~ VIDIOC_STREAMOFF\n");
}
}
/**
* 读取一帧
*/
int frameRead(){
struct v4l2_buffer buf;
CLEAR(buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
if(-1 == ioctl(fd,VIDIOC_DQBUF,&buf)){ // 从视频缓冲区的输出队列中取得一个已经保存有一帧视频数据的视频缓冲区;
printf("wrong!VIDIOC_DQBUF\n");
}
/**
* assert的作用是现计算表达式 expression ,如果其值为假(即为0),那么它先向stderr打印一条出错信息,
然后通过调用 abort 来终止程序运行。
*/
assert(buf.index < n_buffers);
assert(buffers[buf.index].start);
//videoProcess(buffers[buf.index].start);
x264Process(buf);
return 1;
}
void mainLoop(){
unsigned int count;
count = TOTAL_FRAME_NUMBER;
x264Init();
for(i_frame=0;i_frame<=count;i_frame++){
//作用不明
for(;;){
fd_set fds;
struct timeval tv;
int r;
FD_ZERO(&fds);
FD_SET(fd,&fds);
/*超时设置*/
tv.tv_sec = 2;
tv.tv_usec = 0;
r = select(fd+1,&fds,NULL,NULL,&tv);
if(-1 == r || 0 == r){
printf("select wrong!\n");
}
if(frameRead()){ //------>去读取帧
break;
}
linux视频采集X264编码
最新推荐文章于 2023-09-20 14:07:24 发布