-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcapture_video.cpp
More file actions
201 lines (161 loc) · 4.27 KB
/
capture_video.cpp
File metadata and controls
201 lines (161 loc) · 4.27 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <syslog.h>
#include <pthread.h>
#include <sched.h>
#include <sys/sysinfo.h>
#include <sys/time.h>
#include <signal.h>
#include <memory.h>
#include <semaphore.h>
sem_t sem1, sem2, sem3;
int stop_flag = 0;
void* start_image_capture(void* args)
{
int cpu = sched_getcpu();
while(!stop_flag)
{
if (sem_wait(&sem1)!=0)
{
syslog(LOG_ERR, "Semaphore problem");
break;
}
if (stop_flag) break;
syslog(LOG_INFO, "Read Image Started on CPU %d", cpu);
}
syslog(LOG_INFO, "Read Image Completed");
return NULL;
}
void* process_image(void* args)
{
int cpu = sched_getcpu();
while(!stop_flag)
{
if (sem_wait(&sem2)!=0)
{
syslog(LOG_ERR, "Semaphore problem");
break;
}
if (stop_flag) break;
syslog(LOG_INFO, "Process Image Started on CPU %d", cpu);
}
syslog(LOG_INFO, "Process Image Completed");
return NULL;
}
void* write_image(void* args)
{
int cpu = sched_getcpu();
while(!stop_flag)
{
if (sem_wait(&sem3)!=0)
{
syslog(LOG_ERR, "Semaphore problem");
break;
}
if (stop_flag) break;
syslog(LOG_INFO, "Write Image Started on CPU %d", cpu);
}
syslog(LOG_INFO, "Write Image Completed");
return NULL;
}
int counter = 0;
// Signal handler here
void scheduler(int sig)
{
counter++;
// 100ms/4 = 25HZ
if (counter%4==0)
{
sem_post(&sem1);
}
if (counter%4==0)
{
sem_post(&sem2);
}
if (counter%4==0)
{
sem_post(&sem3);
}
}
void program_interrupt(int sig)
{
syslog(LOG_INFO, "Interrupt signal has been received");
stop_flag = 1;
sem_post(&sem1);
sem_post(&sem2);
sem_post(&sem3);
}
int main()
{
pthread_t read_thread, process_thread, write_thread;
pthread_attr_t attr[3];
struct sched_param param[3];
cpu_set_t cpu[3];
syslog(LOG_INFO, "Start Capture Application");
syslog(LOG_INFO, "System has %d cores and %d cores are available", get_nprocs_conf(), get_nprocs());
if (sem_init(&sem1,0 ,0)!=0)
{
syslog(LOG_ERR, "Semaphore cannot be created!!!");
return 1;
}
if (sem_init(&sem2,0 ,0)!=0)
{
syslog(LOG_ERR, "Semaphore cannot be created!!!");
return 1;
}
if (sem_init(&sem3,0 ,0)!=0)
{
syslog(LOG_ERR, "Semaphore cannot be created!!!");
return 1;
}
int max_priority = sched_get_priority_max(SCHED_FIFO);
for (int i=0; i<3; i++)
{
pthread_attr_init(&attr[i]);
pthread_attr_setschedpolicy(&attr[i], SCHED_FIFO);
param[i].sched_priority = max_priority;
pthread_attr_setschedparam(&attr[i], ¶m[i]);
CPU_ZERO(&cpu[i]);
CPU_SET(i, &cpu[i]);
pthread_attr_setaffinity_np(&attr[i], sizeof(cpu_set_t), &cpu[i]);
pthread_attr_setinheritsched(&attr[i], PTHREAD_EXPLICIT_SCHED);
}
if (pthread_create(&read_thread, &attr[0], start_image_capture, NULL) != 0)
{
syslog(LOG_ERR, "Failed to create read thread");
return 1;
}
if (pthread_create(&process_thread, &attr[1], process_image, NULL) != 0)
{
syslog(LOG_ERR, "Failed to create read thread");
return 1;
}
if (pthread_create(&write_thread, &attr[2], write_image, NULL) != 0)
{
syslog(LOG_ERR, "Failed to create read thread");
return 1;
}
// Create a interrupt signal handler
struct sigaction sa_in;
memset(&sa_in, 0, sizeof(sa_in));
sa_in.sa_handler = &program_interrupt;
sigaction(SIGINT, &sa_in, NULL);
sigaction(SIGTERM, &sa_in, NULL);
// Create a signal handler
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = &scheduler;
sigaction(SIGALRM, &sa, NULL);
// create a timer 100Hz
struct itimerval timer;
timer.it_value.tv_sec = 0;
timer.it_value.tv_usec = 10000; //10 ms
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = 10000; //10 ms
setitimer(ITIMER_REAL, &timer, NULL);
pthread_join(read_thread, NULL);
pthread_join(process_thread, NULL);
pthread_join(write_thread, NULL);
sem_destroy(&sem1);
sem_destroy(&sem2);
sem_destroy(&sem3);
return 0;
}