Files

828 lines
18 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include<time.h>
#include <sys/mman.h>
#include <time.h>
#include <sys/time.h>
#include <signal.h>
#include <pthread.h>
int port = 12555;
char line[4096];
char file[64];
char file1[64];
char file2[64];
char file3[64];
int count=0;
int count_flag=1;
int wr_idx = 0;
int wr_idx1 = 0;
int wr_idx2 = 0;
int wr_idx3 = 0;
int rec_st = 1;
int rec_cnt = 1;
float card_cap = 0;
void rec_off(void);
long get_file_size(const char *path)
{
unsigned long filesize = -1;
struct stat statbuff;
if(stat(path, &statbuff) < 0)
return filesize;
else
return statbuff.st_size;
}
int readline(FILE* f)
{
char c;
int len = 0;
//printf("++%s\n", __FUNCTION__);
while ( (c = fgetc(f) ) != EOF && c != '\n' && (len < 1024))
{
//printf("%c", c);
line[len++] = c;
line[len] = '\0';
}
//printf("--%s\n", __FUNCTION__);
if(len > 1000)
return 0;
return len;
}
int searchstr(char *main, char *pattern)
{
int step, i;
int mlen = strlen(main);
int plen = strlen(pattern);
//printf("++%s\n", __FUNCTION__);
step = mlen - plen;
if(step < 0)
return 0;
i = 0;
do {
if(strncmp(&main[i], pattern, plen) == 0)
return 1;
i++;
step--;
} while(step >= 0);
return 0;
}
int kill_ffmpeg(void)
{
char kill_cmd[64];
FILE* fp;
int len;
int hit;
int pid;
system("ps > /tmp/ps.txt");
fp = fopen("/tmp/ps.txt", "r");
if(fp == NULL) {
printf("cannot open /tmp/ps.txt\n");
//rec_off();
//exit(0);
return -1;
}
do {
len = readline(fp);
//printf("len %d\n", len);
hit = 0;
if(len > 12) { //9+3
//check "mmcblk1p1" "mnt" present on line
//printf("%s\n", line);
hit = searchstr(line, "ffmpeg");
if(!hit)
continue;
else {
sscanf(line,"%05lu ", &pid);
sprintf(kill_cmd, "kill -9 %d", pid);
printf("found ffmpeg pid %d!\n", pid);
system(kill_cmd);
fclose(fp);
return 1;
}
//break;
}
} while(len>0);
fclose(fp);
}
int check_sdcard(int *usage)
{
int cd_prsnt;
FILE* fp;
int len;
int hit1, hit2;
int mount;
int cd;
unsigned long total, used, aval;
//float total, used, aval;
char dev[52];
system("df > /tmp/df.txt");
fp = fopen("/tmp/df.txt", "r");
if(fp == NULL) {
printf("cannot open /tmp/df.txt\n");
rec_off();
exit(0);
}
do {
len = readline(fp);
//printf("len %d\n", len);
hit1 = 0;
hit2 = 0;
if(len > 12) { //9+3
//check "mmcblk1p1" "mnt" present on line
//printf("%s\n", line);
hit1 = searchstr(line, "sda1");
if(!hit1)
continue;
hit2 = searchstr(line, "media");
if(hit1 && hit2) {
sscanf(line,"%20s %10lu %10lu %10lu %03u%%", dev, &total, &used, &aval, usage);
//sscanf(line,"%20s %09lu %9lu %09lu %03u%%", dev, &total, &used, &aval, usage);
//printf("parse result:\n");
//printf("%20s %10lu %10lu %10lu %03u%%\n", dev, total, used, aval, *usage);
//printf("%20s %09lu %9lu %9lu %03u%%\n", dev, total, used, aval, *usage);
//card_cap = total/1000.0/1000.0;
card_cap = total/1024.0/1024.0;
break;
}
}
} while(len>0);
fclose(fp);
mount = hit1 && hit2;
cd = access("/dev/sda1", 0);
if(mount && (cd < 0))
system("umount /media");
return mount && (cd == 0);
}
void valid_filename(char *p)
{
while(*p != '\0') {
if((*p==' ') || (*p==':'))
*p = '_';
p++;
}
}
#if 1
void take_filename(void)
{
int i, existed, existed1, existed2, existed3;
int pre_found = 0;
int pre_found1 = 0;
int pre_found2 = 0;
int pre_found3 = 0;
char pre_file[64];
char pre_file1[64];
char pre_file2[64];
char pre_file3[64];
int reach_end;
int reach_end1;
int reach_end2;
int reach_end3;
char rm_cmd[64];
char rm_cmd1[64];
char rm_cmd2[64];
char rm_cmd3[64];
//existed = access("/media/HD999.ts", 0);
existed = access("/media/HD_1_999.ts", 0);
existed1 = access("/media/HD_2_999.ts", 0);
existed2 = access("/media/HD_3_999.ts", 0);
existed3 = access("/media/HD_4_999.ts", 0);
if(existed < 0)
reach_end = 0;
else
reach_end = 1;
if(existed1 < 0)
reach_end1 = 0;
else
reach_end1 = 1;
if(existed2 < 0)
reach_end2 = 0;
else
reach_end2 = 1;
if(existed3 < 0)
reach_end3 = 0;
else
reach_end3 = 1;
for(i=0; i<1000; i++) {
sprintf(file, "/media/HD_1_%d.ts", i);
existed = access(file, 0);
if(existed < 0) {
if(!pre_found) {
pre_found = 1;
memcpy(pre_file, file, sizeof(file));
wr_idx = i;
continue;
}
} else {
if((i>wr_idx) && (reach_end == 0))
pre_found = 0;
}
}
for(i=0; i<1000; i++) {
sprintf(file1, "/media/HD_2_%d.ts", i);
existed1 = access(file1, 0);
if(existed1 < 0) {
if(!pre_found1) {
pre_found1 = 1;
memcpy(pre_file1, file1, sizeof(file1));
wr_idx1 = i;
continue;
}
} else {
if((i>wr_idx1) && (reach_end1 == 0))
pre_found1 = 0;
}
}
for(i=0; i<1000; i++) {
sprintf(file2, "/media/HD_3_%d.ts", i);
existed2 = access(file2, 0);
if(existed2 < 0) {
if(!pre_found2) {
pre_found2 = 1;
memcpy(pre_file2, file2, sizeof(file2));
wr_idx2 = i;
continue;
}
} else {
if((i>wr_idx2) && (reach_end2 == 0))
pre_found2 = 0;
}
}
for(i=0; i<1000; i++) {
sprintf(file3, "/media/HD_4_%d.ts", i);
existed3 = access(file3, 0);
if(existed3 < 0) {
if(!pre_found3) {
pre_found3 = 1;
memcpy(pre_file3, file3, sizeof(file3));
wr_idx3 = i;
continue;
}
} else {
if((i>wr_idx3) && (reach_end3 == 0))
pre_found3 = 0;
}
}
if(pre_found) {
memcpy(file, pre_file, sizeof(file));
}
else { //take care if 1000 ts file exitsed.
wr_idx = wr_idx + 1;
if(wr_idx>=1000)
wr_idx = 0;
sprintf(file, "/media/HD_1_%d.ts", wr_idx);
sprintf(rm_cmd, "rm %s", file);
system(rm_cmd);
printf("1000 ts file existed!, %s\n", rm_cmd);
}
if(pre_found1){
memcpy(file1, pre_file1, sizeof(file1));
}
else { //take care if 1000 ts file exitsed.
wr_idx1 = wr_idx1 + 1;
if(wr_idx1>=1000)
wr_idx1 = 0;
sprintf(file1, "/media/HD_2_%d.ts", wr_idx1);
sprintf(rm_cmd1, "rm %s", file1);
system(rm_cmd1);
printf("1000 ts file1 existed1!, %s\n", rm_cmd1);
}
if(pre_found2){
memcpy(file2, pre_file2, sizeof(file2));
}
else { //take care if 1000 ts file exitsed.
wr_idx2 = wr_idx2 + 1;
if(wr_idx2>=1000)
wr_idx2 = 0;
sprintf(file2, "/media/HD_3_%d.ts", wr_idx2);
sprintf(rm_cmd2, "rm %s", file2);
system(rm_cmd2);
printf("1000 ts file2 existed2!, %s\n", rm_cmd2);
}
if(pre_found3)
memcpy(file3, pre_file3, sizeof(file3));
else { //take care if 1000 ts file exitsed.
wr_idx3 = wr_idx3 + 1;
if(wr_idx3>=1000)
wr_idx3 = 0;
sprintf(file3, "/media/HD_4_%d.ts", wr_idx3);
sprintf(rm_cmd3, "rm %s", file3);
system(rm_cmd3);
printf("1000 ts file3 existed3!, %s\n", rm_cmd3);
}
}
#endif
void rm_file(void)
{
int i, existed, existed1, existed2, existed3;
int pre_found = 0;
int pre_found1 = 0;
int pre_found2 = 0;
int pre_found3 = 0;
char pre_file[64];
char pre_file1[64];
char pre_file2[64];
char pre_file3[64];
int reach_end;
int reach_end1;
int reach_end2;
int reach_end3;
char rm_cmd[64];
existed = access("/media/HD_1_999.ts", 0);
if(existed < 0)
reach_end = 0;
else
reach_end = 1;
existed1 = access("/media/HD_2_999.ts", 0);
if(existed1 < 0)
reach_end1 = 0;
else
reach_end1 = 1;
existed2 = access("/media/HD_3_999.ts", 0);
if(existed2 < 0)
reach_end2 = 0;
else
reach_end2 = 1;
existed3 = access("/media/HD_4_999.ts", 0);
if(existed3 < 0)
reach_end3 = 0;
else
reach_end3 = 1;
printf("rm file wr_idx:%d\n",wr_idx);
printf("rm file wr_idx1:%d\n",wr_idx1);
printf("rm file wr_idx2:%d\n",wr_idx2);
printf("rm file wr_idx3:%d\n",wr_idx3);
if(reach_end && (wr_idx != 999)) {
for(i=999; i>wr_idx; i--) {
sprintf(file, "/media/HD_1_%d.ts", i);
existed = access(file, 0);
if(existed == 0) {
pre_found = 1;
memcpy(pre_file, file, sizeof(file));
}
}
} else {
for(i=0; i<1000; i++) {
sprintf(file, "/media/HD_1_%d.ts", i);
existed = access(file, 0);
if(existed == 0) {
pre_found = 1;
memcpy(pre_file, file, sizeof(file));
break;
}
}
}
//2
if(reach_end1 && (wr_idx1 != 999)) {
for(i=999; i>wr_idx1; i--) {
sprintf(file1, "/media/HD_2_%d.ts", i);
existed1 = access(file1, 0);
if(existed1 == 0) {
pre_found1 = 1;
memcpy(pre_file1, file1, sizeof(file1));
}
}
} else {
for(i=0; i<1000; i++) {
sprintf(file1, "/media/HD_2_%d.ts", i);
existed1 = access(file1, 0);
if(existed1 == 0) {
pre_found1 = 1;
memcpy(pre_file1, file1, sizeof(file1));
break;
}
}
}
if(reach_end2 && (wr_idx2 != 999)) {
for(i=999; i>wr_idx2; i--) {
sprintf(file2, "/media/HD_3_%d.ts", i);
existed2 = access(file2, 0);
if(existed2 == 0) {
pre_found2 = 1;
memcpy(pre_file2, file2, sizeof(file2));
}
}
} else {
for(i=0; i<1000; i++) {
sprintf(file2, "/media/HD_3_%d.ts", i);
existed2 = access(file2, 0);
if(existed2 == 0) {
pre_found2 = 1;
memcpy(pre_file2, file2, sizeof(file2));
break;
}
}
}
if(reach_end3 && (wr_idx3 != 999)) {
for(i=999; i>wr_idx3; i--) {
sprintf(file3, "/media/HD_4_%d.ts", i);
existed3 = access(file3, 0);
if(existed3 == 0) {
pre_found3 = 1;
memcpy(pre_file3, file3, sizeof(file3));
}
}
} else {
for(i=0; i<1000; i++) {
sprintf(file3, "/media/HD_4_%d.ts", i);
existed3 = access(file3, 0);
if(existed3 == 0) {
pre_found3 = 1;
memcpy(pre_file3, file3, sizeof(file3));
break;
}
}
}
if(pre_found) {
sprintf(rm_cmd, "rm %s", pre_file);
system(rm_cmd);
printf("do rm, %s\n", rm_cmd);
}
if(pre_found1) {
sprintf(rm_cmd, "rm %s", pre_file1);
system(rm_cmd);
printf("do rm, %s\n", rm_cmd);
}
if(pre_found2) {
sprintf(rm_cmd, "rm %s", pre_file2);
system(rm_cmd);
printf("do rm, %s\n", rm_cmd);
}
if(pre_found3) {
sprintf(rm_cmd, "rm %s", pre_file3);
system(rm_cmd);
printf("do rm, %s\n", rm_cmd);
}
}
/*
int detect_audio(void)
{
struct sockaddr_in clientAddr;
socklen_t alen;
struct sockaddr_in sockaddr;
int timeout = 0;
int sock;
unsigned int hdr;
int len;
int hit = 0;
char rxbuf[1504];
sock = init_rx_port(20003, &sockaddr);
do {
len = recvfrom(sock, rxbuf, 1504, MSG_DONTWAIT, (struct sockaddr*)&clientAddr, &alen);
if(len>0) {
hit++;
} else {
timeout++;
}
if(hit>10) {
close(sock);
return 1;
}
usleep(10000);
} while(timeout < 200);
close(sock);
return 0;
}
int detect_fps(void)
{
struct sockaddr_in clientAddr;
socklen_t alen;
struct sockaddr_in sockaddr;
int fps = 0x1234;
int sock;
unsigned int hdr;
int len;
int timeout = 0;
char rxbuf[1504];
sock = init_rx_port(12555, &sockaddr);
printf("vstream port opened\n");
do {
len = recvfrom(sock, rxbuf, 1504, MSG_DONTWAIT, (struct sockaddr*)&clientAddr, &alen);
if(len>15) {
memcpy(&hdr, rxbuf, sizeof(hdr));
if((hdr == 0x1000000) && (rxbuf[4] == 0x00) && (rxbuf[15] == 0x80)) {
fps = rxbuf[6];
}
}
if(len <= 0) {
timeout++;
usleep(500);
if(timeout%1000 == 0)
printf("detect fps timeout %d", timeout);
}
} while(fps == 0x1234);
close(sock);
return fps;
}
*/
void rec_off(void)
{
printf("kill record process\n");
system("echo ""REC OFF"" > /tmp/rec");
system("sync");
count=0xAA;
//system("pkill.sh ffmpeg");
//system("pkill.sh ffmpeg");
kill_ffmpeg();
rec_cnt = 1;
//sleep(1);
}
int rec_on(void)
{
int rtn, errno, errno1, errno2, errno3;
int fps;
int audio;
char ffmpeg_cmd[512];
char ffmpeg_cmd1[512];
char ffmpeg_cmd2[512];
char ffmpeg_cmd3[512];
int do_rec = 0;
int usage = 100;
if(check_sdcard(&usage)) {
printf("1:found sd card!\n");
do_rec = 1;
} else if(access("/dev/sda1", 0) == 0) {
system("umount /media");
system("mount /dev/sda1 /media");
sleep(2);
if(check_sdcard(&usage)) {
printf("2:found sd card!\n");
do_rec = 1;
}
} else {
printf("No TF card!\n");
do_rec = 0;
system("echo ""REC OFF(No Card)"" > /tmp/rec");
}
if(do_rec) {
count=0x55;
// system("echo ""REC startup..."" > /tmp/rec");
printf("--detected fps %d\n", fps);
//fps = detect_fps();
fps = 60;
printf("++detected fps %d\n", fps);
// sprintf(ffmpeg_cmd, "echo ""REC ON %d%% of %.2fGB"" > /tmp/rec", usage, card_cap);
system(ffmpeg_cmd);
system(ffmpeg_cmd1);
system(ffmpeg_cmd2);
system(ffmpeg_cmd3);
//audio = detect_audio();
audio = 0;
printf("detected audio %s\n", audio ? "enable" : "disable");
take_filename();
printf("save file name is %s\n", file);
printf("save file1 name is %s\n", file1);
printf("save file2 name is %s\n", file2);
printf("save file3 name is %s\n", file3);
if(audio){
sprintf(ffmpeg_cmd, "ffmpeg -f h264 -r %d -i udp://localhost:12555 -f aac -i udp://localhost:20003 -vcodec copy -acodec copy -ar 16000 -r %d -f mpegts %s </dev/null &", fps, fps, file);
sprintf(ffmpeg_cmd1, "ffmpeg -f h264 -r %d -i udp://localhost:12556 -f aac -i udp://localhost:20003 -vcodec copy -acodec copy -ar 16000 -r %d -f mpegts %s </dev/null &", fps, fps, file1);
sprintf(ffmpeg_cmd2, "ffmpeg -f h264 -r %d -i udp://localhost:12557 -f aac -i udp://localhost:20003 -vcodec copy -acodec copy -ar 16000 -r %d -f mpegts %s </dev/null &", fps, fps, file2);
sprintf(ffmpeg_cmd3, "ffmpeg -f h264 -r %d -i udp://localhost:12558 -f aac -i udp://localhost:20003 -vcodec copy -acodec copy -ar 16000 -r %d -f mpegts %s </dev/null &", fps, fps, file3);
}
else{
sprintf(ffmpeg_cmd, "ffmpeg -f hevc -r 25 -i udp://127.0.0.1:12565 -vcodec copy -an -f mpegts %s </dev/null &", file);
sprintf(ffmpeg_cmd1, "ffmpeg -f hevc -r 25 -i udp://127.0.0.1:12566 -vcodec copy -an -f mpegts %s </dev/null &", file1);
sprintf(ffmpeg_cmd2, "ffmpeg -f hevc -r 25 -i udp://127.0.0.1:12567 -vcodec copy -an -f mpegts %s </dev/null &", file2);
sprintf(ffmpeg_cmd3, "ffmpeg -f hevc -r 25 -i udp://127.0.0.1:12568 -vcodec copy -an -f mpegts %s </dev/null &", file3);
//printf("file %s",file);
//printf("file1 %s",file1);
//printf("file2 %s",file2);
//printf("file3 %s",file3);
}
printf("ffmpeg_cmd: %s\n", ffmpeg_cmd);
printf("ffmpeg_cmd1: %s\n", ffmpeg_cmd1);
printf("ffmpeg_cmd2: %s\n", ffmpeg_cmd2);
printf("ffmpeg_cmd3: %s\n", ffmpeg_cmd3);
errno = system(ffmpeg_cmd);
errno1 = system(ffmpeg_cmd1);
errno2 = system(ffmpeg_cmd2);
errno3 = system(ffmpeg_cmd3);
printf("ffmpeg lanuch %d\n", errno);
system("echo ""REC ON"" > /tmp/rec");
//rec_st = !rec_st;
//return 1;
} else {
system("echo ""REC OFF No Card"" > /tmp/rec");
}
sleep(1);
return do_rec;
}
void check_disk(void)
{
int usage = 0;
//int rec_cnt = 1;
int do_rec;
char card_st[64];
do {
do_rec = check_sdcard(&usage);
if(!do_rec) {
//rec_st = 0;
rec_off();
} else {
sprintf(card_st, "echo ""REC ON %d%% of %.2fGB"" > /tmp/rec", usage, card_cap);
system(card_st);
if(count == 0xAA){
rec_on();
}
}
if(usage > 90) {
rec_off();
rm_file();
//rm_file();
system("echo ""card almost full"" > /tmp/rec");
do_rec = check_sdcard(&usage);
if(!do_rec)
system("echo ""REC OFF no card"" > /tmp/rec");
if(usage<=90 && do_rec) {
system("echo ""REC ON"" > /tmp/rec");
rec_on();
rec_cnt = 1;
}
}
} while(usage>90);
rec_cnt++;
if((rec_cnt%30) == 0) { //a file about 300 second
printf("split file\n");
rec_off();
rec_on();
rec_cnt = 1;
}
}
void set_timer()
{
struct itimerval itv, oldtv;
itv.it_interval.tv_sec = 300;
itv.it_interval.tv_usec = 0;
itv.it_value.tv_sec = 300;
itv.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &itv, &oldtv);
}
void sigalrm_handler(int sig)
{
// count++;
// count_flag =1;
printf("timer signal.. %d\n", count);
}
int main(int argc, char **argv)
{
int i, j=0;
int rtn, errno;
int fps;
int audio;
uint8_t led = 0, led_ctrl;
int led_fd;
int cnt = 1;
//int i, j=0;
//int rtn, errno;
int do_rec;
//int rec_st = 0;
int usage = 100;
int rec_init = 1;
int fd;
unsigned int tmp;
signal(SIGALRM, sigalrm_handler);
set_timer();
/*
led_fd = open("/sys/class/leds/led2/brightness", O_WRONLY); //data receive indicator
if(led_fd < 0) {
printf("open led failed\n");
return -1;
}
*/
rec_on();
while(1) {
// if(count_flag == 1){
// count_flag =0;
// rec_off();
// rec_on();
// }
usleep(20000);
cnt++;
if((cnt%45 == 0) && rec_st) {
// led ^= 0x1;
// led_ctrl = led+0x30;
// write(led_fd, &led_ctrl, 1);
if(cnt%270 == 0)
system("sync");
} else if(!rec_st && (cnt%10 == 0)){
// led ^= 0x1;
// led_ctrl = led+0x30;
// write(led_fd, &led_ctrl, 1);
}
if((cnt%250 == 0) && rec_st){ //check disk every 10s
#if 0
if(access("/dev/sda1", 0) == 0){
system("mount /dev/sda1 /media");
usleep(2000000);
}
#endif
check_disk();
}
}//while 1
close(led_fd);
printf("write file finish\r\n");
}