-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.c
142 lines (123 loc) · 3.91 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "camera.h"
#include "shm.h"
#define BUFF_SIZE 1024*1000 //50kB
#ifdef PC_DEBUG
char* device_fname = "/dev/video1";
#else
char *device_fname = "/dev/video0";
#endif
char* output_fname_yuyv = "./pic/pic1.yuyv"; //yuyv file name
char *output_fname_rgb_bmp = "./pic/pic1.bmp";
char *output_fname_jpg = "./pic/pic1.jpg";
int yuyv_out_fd = -1; //file description of out put .yuyv file
int camera_fd = -1; // file description of camera
single_buff* frame_buf= NULL; //(用户空间)记录帧 映射到用户空间(包含4个用户可用的帧)
struct v4l2_requestbuffers req_buffers;
struct v4l2_capability cap; //capabilities of device
void err_exit(char* process_name)
{
perror(process_name);
exit(-1);
}
void old_deal()
{
/*1. open video0 device*/
camera_fd = open_camera_file(device_fname);
if(camera_fd < 0)
err_exit("open_camera_file");
/*2. 查看设备能力,是否有音视频输入输出?(VIDIOC_QUERYCAP:查询驱动功能)*/
check_device_info();
// /*3. 设备初始化*/
if(device_init() < 0)
err_exit("device_init");
/*4. 开始获取图像*/
if( start_capture() < 0)
err_exit("start_capture");
// while (1) {
/*5. 处理图片*/
save_to_yuyv();
yuyv_to_rgb24();
// rgb24_to_shm(shm_addr); //Do NOT put the rgb stream into share-memory instead of files
write_JPEG_file(output_fname_rgb_bmp, output_fname_jpg, JPEG_QUALITY);
// }
/*6. 关闭设备*/
close_camera();
}
int main(int argc, char* argv[])
{
pid_t pid; //进程id
key_t key;
int shmid; //share memory id
char* shm_addr;
char *flag = "JPG"; //标志头
char buff[BUFF_SIZE];
//创建共享内存
if((key = ftok(".", 'c')) == -1){
err_exit("ftok");
printf("key = %d\n", key);
}
printf("ftok ok...key = %d\n", key);
if( (shmid = shmget(key, BUFF_SIZE, 0666|IPC_CREAT)) < 0){
printf("shmid = %d\n", shmid);
err_exit("shmget");
}else{
printf("shmget ok. shmid = %d\n", shmid);
}
//show the share memory status
system("ipcs -m");
pid = fork();
if(pid < 0){
err_exit("fork");
}
if(0 == pid){ // child process
shm_addr = shmat(shmid, 0, 0);
if( shm_addr == (void *)-1 ){
shmdt(shm_addr);
err_exit("shmat");
}else{
printf("------Child: Attach share-memory ok: %p\n", shm_addr);
}
system("ipcs -m");
/*put the jpg data stream to the share-memory*/
/*1. open video0 device*/
camera_fd = open_camera_file(device_fname);
if(camera_fd < 0)
err_exit("open_camera_file");
/*2. 查看设备能力,是否有音视频输入输出?(VIDIOC_QUERYCAP:查询驱动功能)*/
check_device_info();
// /*3. 设备初始化*/
if(device_init() < 0)
err_exit("device_init");
/*4. 开始获取图像*/
if( start_capture() < 0)
err_exit("start_capture");
while (1) {
/*5. 处理图片*/
save_to_yuyv();
// yuyv_to_rgb24();
rgb24_to_shm(shm_addr); //Do NOT put the rgb stream into share-memory instead of files
// write_JPEG_file(output_fname_rgb_bmp, output_fname_jpg, JPEG_QUALITY);
shmdt(shm_addr);
}
/*6. 关闭设备*/
close_camera();
printf("-----Child process End...\n");
}
else //parent process
{
shm_addr = shmat(shmid, 0, 0);
if( shm_addr == (void *)-1 ){
err_exit("shmat");
}else{
printf("----Parent: Attach share-memory ok: %p\n", shm_addr);
}
system("ipcs -m");
sleep(1);
char buf[BUFF_SIZE];
strncpy(shm_addr, buf, BUFF_SIZE);
printf("shm_addr---------\n %p \n", shm_addr);
printf("Parent process End...\n");
}
// old_deal();
return 0;
}