V4L2采集视频利用TCP协议远程传输
开发环境
system : ubuntu12.04
board : mini2440
arm-linux-gcc : v4.3.2
说明:该应用将V4L2视频采集和TCP网络传输结合起来,实现了实时视频传输,分为客户端client和服务器端server两部分,客服端采集视频并进行传输,服务器穿接收视频并保存成文件。 客户端编译了arm版本和x86版本,均测试正常。
本程序仅做学习用~~~~~~~~~~
1. v4l2_tcp_server.c
-
#include <stdio.h>
-
#include <string.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <unistd.h>
-
#include <sys/types.h>
-
#include <sys/socket.h>
-
#include <netinet/in.h>
-
#include <arpa/inet.h>
-
-
#define MAXLINE 1024
-
#define SERVER_PORT 3333
-
-
-
void usage(char *command)
-
{
-
printf("usage :%s filename\n", command);
-
exit(0);
-
}
-
int main(int argc,char **argv)
-
{
-
struct sockaddr_in serv_addr;
-
struct sockaddr_in clie_addr;
-
char buf[MAXLINE];
-
int sock_id;
-
int link_id;
-
int recv_len;
-
int write_leng;
-
int clie_addr_len;
-
FILE *fp;
-
-
if (argc != 2) {
-
usage(argv[0]);
-
}
-
if ((fp = fopen(argv[1], "w")) == NULL) {
-
perror("Open file failed\n");
-
exit(0);
-
}
-
if ((sock_id = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
-
perror("Create socket failed\n");
-
exit(0);
-
}
-
//memset(&serv_addr, 0, sizeof(serv_addr));
-
bzero(&serv_addr,sizeof(serv_addr));
-
serv_addr.sin_family = AF_INET; //IPv4
-
serv_addr.sin_port = htons(SERVER_PORT);
-
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
-
-
if (bind(sock_id, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0 ) {
-
perror("Bind socket failed\n");
-
exit(0);
-
}
-
-
if (-1 == listen(sock_id, 10)) {
-
perror("Listen socket failed\n");
-
exit(0);
-
}
-
while (1) {
-
clie_addr_len = sizeof(clie_addr);
-
link_id = accept(sock_id, (struct sockaddr *)&clie_addr, (socklen_t *)&clie_addr_len);
-
if (-1 == link_id) {
-
perror("Accept socket failed\n");
-
exit(0);
-
}
-
bzero(buf, MAXLINE);
-
while ((recv_len = recv(link_id, buf, MAXLINE, 0)) != 0) {
-
if(recv_len < 0) {
-
printf("Recieve Data From Server Failed!\n");
-
break;
-
}
-
printf("#");
-
write_leng = fwrite(buf, sizeof(char), recv_len, fp);
-
if (write_leng < recv_len) {
-
printf("Write file failed\n");
-
break;
-
}
-
bzero(buf,MAXLINE);
-
}
-
printf("\nFinish Recieve\n");
-
fclose(fp);
-
close(link_id);
-
}
-
close(sock_id);
-
return 0;
-
}
Makefile
-
all:v4l2_tcp_server
-
-
v4l2_tcp_server:v4l2_tcp_server.c
-
gcc -Wall -o $@ $<
-
-
clean:
-
rm -rf v4l2_tcp_server *.yuv
2. v4l2_tcp_client.c
-
/*
-
* V4L2 video capture and tcp transformation example
-
* AUTHOT : WANGTISHENG
-
* DATA : 2013-12-18
-
*/
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <assert.h>
-
#include <getopt.h> /* getopt_long() */
-
#include <fcntl.h> /* low-level i/o */
-
#include <unistd.h>
-
#include <errno.h>
-
#include <sys/stat.h>
-
#include <sys/types.h>
-
#include <sys/time.h>
-
#include <sys/mman.h>
-
#include <sys/ioctl.h>
-
#include <sys/socket.h>
-
#include <netinet/in.h>
-
#include <arpa/inet.h>
-
#include <linux/videodev2.h>
-
-
-
-
#define CLEAR(x) memset(&(x), 0, sizeof(x))
-
-
struct buffer {
-
void *start;
-
size_t length;
-
};
-
-
static char *dev_name;
-
static int fd = -1; //DEVICE NUMBER
-
struct buffer *buffers;
-
static unsigned int n_buffers;
-
static int frame_count = 100;
-
FILE *fp; //FILE POINTOR
-
#define CAPTURE_FILE "./frame_test.yuv"
-
-
/* variables and macros for socket */
-
int sock_id;
-
#define SERVER_IP "192.168.1.121"
-
#define CLIENT_PORT 3333
-
#define MAXLINE 1024
-
char buf[MAXLINE];
-
-
-
static void errno_exit(const char *s)
-
{
-
fprintf(stderr, "%s error %d, %s\n", s, errno, strerror(errno));
-
exit(EXIT_FAILURE);
-
}
-
-
static int xioctl(int fh, int request, void *arg)
-
{
-
int r;
-
do {
-
r = ioctl(fh, request, arg);
-
} while (-1 == r && EINTR == errno);
-
return r;
-
}
-
//处理函数
-
static void process_image(const void *p, int size)
-
{
-
int read_len;
-
FILE *fp_tmp;
-
int send_len;
-
-
fp_tmp = fopen(CAPTURE_FILE,"w");
-
fwrite(p,size, 1, fp_tmp);
-
fclose(fp_tmp);
-
//fwrite(p,size, 1, fp);
-
//memset(buf,0,MAXLINE);
-
fp_tmp = fopen(CAPTURE_FILE,"r");
-
while ((read_len = fread(buf, sizeof(char), MAXLINE, fp_tmp)) >0 ) {
-
send_len = send(sock_id, buf, read_len, 0);
-
if ( send_len < 0 ) {
-
perror("Send file failed\n");
-
exit(0);
-
}
-
//bzero(buf, MAXLINE); //no use
-
}
-
fclose(fp_tmp);
-
}
-
-
static int read_frame(FILE *fp)
-
{
-
struct v4l2_buffer buf;
-
CLEAR(buf);
-
-
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-
buf.memory = V4L2_MEMORY_MMAP;
-
-
if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf))
-
errno_exit("VIDIOC_DQBUF");
-
-
process_image(buffers[buf.index].start, buf.bytesused);
-
-
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
-
errno_exit("VIDIOC_QBUF");
-
-
return 1;
-
}
-
-
static void mainloop(void)
-
{
-
unsigned int count;
-
count = frame_count;
-
while (count-- > 0) {
-
printf("No.%d\n",frame_count - count); //显示当前帧数目
-
read_frame(fp);
-
}
-
-
printf("\nREAD AND SAVE DONE!\n");
-
}
-
-
static void stop_capturing(void)
-
{
-
enum v4l2_buf_type type;
-
-
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-
if (-1 == xioctl(fd, VIDIOC_STREAMOFF, &type))
-
errno_exit("VIDIOC_STREAMOFF");
-
}
-
-
static void start_capturing(void)
-
{
-
unsigned int i;
-
enum v4l2_buf_type type;
-
-
for (i = 0; i < n_buffers; ++i) {
-
struct v4l2_buffer buf;
-
-
CLEAR(buf);
-
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-
buf.memory = V4L2_MEMORY_MMAP;
-
buf.index = i;
-
-
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
-
errno_exit("VIDIOC_QBUF");
-
}
-
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-
if (-1 == xioctl(fd, VIDIOC_STREAMON, &type))
-
errno_exit("VIDIOC_STREAMON");
-
}
-
-
static void uninit_device(void)
-
{
-
unsigned int i;
-
-
for (i = 0; i < n_buffers; ++i)
-
if (-1 == munmap(buffers[i].start, buffers[i].length))
-
errno_exit("munmap");
-
-
free(buffers);
-
}
-
-
-
-
static void init_mmap(void)
-
{
-
struct v4l2_requestbuffers req;
-
-
CLEAR(req);
-
-
req.count = 4;
-
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-
req.memory = V4L2_MEMORY_MMAP;
-
-
if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) {
-
if (EINVAL == errno) {
-
fprintf(stderr, "%s does not support "
-
"memory mapping\n", dev_name);
-
exit(EXIT_FAILURE);
-
} else {
-
errno_exit("VIDIOC_REQBUFS");
-
}
-
}
-
-
if (req.count < 2) {
-
fprintf(stderr, "Insufficient buffer memory on %s\n",
-
dev_name);
-
exit(EXIT_FAILURE);
-
}
-
-
buffers = calloc(req.count, sizeof(*buffers));
-
-
if (!buffers) {
-
fprintf(stderr, "Out of memory\n");
-
exit(EXIT_FAILURE);
-
}
-
-
for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
-
struct v4l2_buffer buf;
-
-
CLEAR(buf);
-
-
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-
buf.memory = V4L2_MEMORY_MMAP;
-
buf.index = n_buffers;
-
-
if (-1 == xioctl(fd, VIDIOC_QUERYBUF, &buf))
-
errno_exit("VIDIOC_QUERYBUF");
-
-
buffers[n_buffers].length = buf.length;
-
buffers[n_buffers].start =
-
mmap(NULL /* start anywhere */,
-
buf.length,
-
PROT_READ | PROT_WRITE /* required */,
-
MAP_SHARED /* recommended */,
-
fd, buf.m.offset);
-
-
if (MAP_FAILED == buffers[n_buffers].start)
-
errno_exit("mmap");
-
}
-
}
-
-
-
static void init_device(void)
-
{
-
struct v4l2_capability cap;
-
struct v4l2_format fmt;
-
-
if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap)) { //测试参数
-
if (EINVAL == errno) {
-
fprintf(stderr, "%s is no V4L2 device\n",
-
dev_name);
-
exit(EXIT_FAILURE);
-
} else {
-
errno_exit("VIDIOC_QUERYCAP");
-
}
-
}
-
-
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
-
fprintf(stderr, "%s is no video capture device\n",
-
dev_name);
-
exit(EXIT_FAILURE);
-
}
-
-
if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
-
fprintf(stderr, "%s does not support streaming i/o\n",
-
dev_name);
-
exit(EXIT_FAILURE);
-
}
-
-
-
-
-
CLEAR(fmt);
-
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-
fmt.fmt.pix.width = 320;
-
fmt.fmt.pix.height = 240;
-
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
-
fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
-
-
if (-1 == xioctl(fd, VIDIOC_S_FMT, &fmt)) //设置格式
-
errno_exit("VIDIOC_S_FMT");
-
init_mmap();
-
}
-
-
static void close_device(void)
-
{
-
if (-1 == close(fd))
-
errno_exit("close");
-
-
fd = -1;
-
}
-
-
static void open_device(void)
-
{
-
//加上O_NONBLOCK会出现如下错误
-
//VIDIOC_DQBUF error 11, Resource temporarily unavailable
-
fd = open(dev_name, O_RDWR /* required */ /*| O_NONBLOCK*/, 0);
-
-
if (-1 == fd) {
-
fprintf(stderr, "Cannot open '%s': %d, %s\n",
-
dev_name, errno, strerror(errno));
-
exit(EXIT_FAILURE);
-
}
-
}
-
-
/* tcp initinalization */
-
int tcp_init(void)
-
{
-
struct sockaddr_in serv_addr;
-
int i_ret;
-
//sock_id = socket(AF_INET, SOCK_DGRAM, 0);
-
if ((sock_id = socket(AF_INET,SOCK_STREAM,0)) < 0) {
-
perror("Create socket failed\n");
-
exit(0);
-
}
-
-
memset(&serv_addr, 0, sizeof(serv_addr));
-
serv_addr.sin_family = AF_INET;
-
serv_addr.sin_port = htons(CLIENT_PORT);
-
serv_addr.sin_addr.s_addr=inet_addr(SERVER_IP);
-
-
/* connect the server commented by guoqingbo*/
-
i_ret = connect(sock_id, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr));
-
if (-1 == i_ret) {
-
printf("Connect socket failed\n");
-
return -1;
-
}
-
printf("Socket connected!\n");
-
return 0;
-
}
-
-
int main(int argc, char **argv)
-
{
-
dev_name = "/dev/video0";
-
-
//if(argc != 1)
-
//{
-
// printf("usage :%s filename\n", argv[0]);
-
// exit(0);
-
//}
-
//if ((fp = fopen(argv[1], "w")) == NULL) {
-
// perror("Creat file failed");
-
// exit(0);
-
//}
-
tcp_init();
-
open_device();
-
init_device();
-
start_capturing();
-
mainloop(); //主要处理均在该函数中实现
-
//fclose(fp);
-
stop_capturing();
-
uninit_device();
-
close_device();
-
//fprintf(stderr, "\n");
-
return 0;
-
}
Makefile:
-
all:v4l2_tcp_client_x86 v4l2_tcp_client_arm
-
-
v4l2_tcp_client_x86:v4l2_tcp_client.c
-
gcc -Wall -o $@ $<
-
v4l2_tcp_client_arm:v4l2_tcp_client.c
-
arm-linux-gcc -Wall -o $@ $<
-
clean:
-
rm -rf v4l2_tcp_client_x86 v4l2_tcp_client_arm
3. 编译测试
先执行服务器端程序
# ./v4l2_tcp_server file_receive.yuv
执行客户端程序
# ./v4l2_tcp_client_x86 或者
# ./v4l2_tcp_client_arm
阅读(1678) | 评论(0) | 转发(0) |