📄 dsserver.cpp
字号:
int DsServer::StopServer(){ isServerStarted = false; *isExit = 1; printf("\r\nTermal all child!\n");// if(writedata_pid != INVALID_PID){// kill(writedata_pid,SIGTERM);// while(wait(NULL)>0);} shm_id1 = shmget(SHM_KEY1,0,0); if(shm_id1==-1){ perror("Get ShareMemory1"); }else{ if(shmctl(shm_id1,IPC_RMID,0)==-1) perror("Remove Share Memory1!"); } shm_id2 = shmget(SHM_KEY2,0,0); if(shm_id2==-1){ perror("Get ShareMemory2"); }else{ if(shmctl(shm_id2,IPC_RMID,0)==-1) perror("Remove Share Memory2!"); } shm_id3 = shmget(SHM_KEY3,0,0); if(shm_id3==-1){ perror("Get ShareMemory3"); }else{ if(shmctl(shm_id3,IPC_RMID,0)==-1) perror("Remove Share Memory3!"); } shm_id4 = shmget(SHM_KEY4,0,0); if (shm_id4 == -1) { perror("Get ShareMemory4"); } else { if (shmctl(shm_id4, IPC_RMID, 0) == -1) perror("Remove Share Memory4!"); } shm_id5 = shmget(SHM_KEY5, 0, 0); if (shm_id5 == -1) { perror("Get ShareMemory5"); } else { if (shmctl(shm_id5, IPC_RMID, 0) == -1) perror("Remove Share Memory5!"); } //========================================================================================= //主进程退出时候要清空socket所有资源 //=========================================================================================// if(sockudpfd>0) close(sockudpfd);// write(1,"All Resource Release and All Procedure Quit!\n",46); //========================================================================================= //主进程退出时候要清空视频所有资源 //========================================================================================= freeVideo(); return 0;}int DsServer::GetChannelCount(){ if(isServerStarted) { return *channelCount; } else { return -1; }}int DsServer::CreateShareMemory(void){ int i; shm_id1 = shmget(SHM_KEY1, 9 * sizeof(int), 0); if (shm_id1 == -1) { shm_id1 = shmget(SHM_KEY1, 9 * sizeof(int), IPC_CREAT|IPC_EXCL|0666); if (shm_id1 == -1) { perror("Create ShareMemory1"); return 1; } else { pInt = (int*)shmat(shm_id1, 0, 0); if(pInt == (void *)-1) { perror("Attach share memory1"); return 1; } } } else { pInt = (int *)shmat(shm_id1, 0, 0); if(pInt ==(void *)-1) { perror("Attach share memory1"); return 1; } } isExit = (int *)(pInt + 0); channelCount = (int *)(pInt + 1); gAlarm = (int *)(pInt + 2); head_size = (int *)(pInt + 3); isSF = (int *)(pInt + 4); isAlarm0 = (int *)(pInt + 5); isAlarm1 = (int *)(pInt + 6); isAlarm2 = (int *)(pInt + 7); isAlarm3 = (int *)(pInt + 8); *isExit = 0; *channelCount=0; *gAlarm = 0; *head_size = 0; *isSF =0; *isAlarm0 =0; *isAlarm1 =0; *isAlarm2 =0; *isAlarm3 =0; shm_id2 = shmget(SHM_KEY2, 4 * sizeof(int), 0); if (shm_id2 == -1) { shm_id2 = shmget(SHM_KEY2, 4 * sizeof(int), IPC_CREAT|IPC_EXCL|0666); if (shm_id2 == -1) { perror("Create ShareMemory2"); return 1; } else { record_fd = (int *)shmat(shm_id2, 0, 0); if (record_fd == (int *)-1) { perror("Attach share memory2"); return 1; } } } else { record_fd = (int *)shmat(shm_id2, 0, 0); if (record_fd == (int *)-1) { perror("Attach share memory2"); return 1; } } for (i = 0; i < 4; i++){ *(record_fd+i) = 0; } shm_id3 = shmget(SHM_KEY3, 4 * sizeof(unsigned short), 0); if (shm_id3 == -1) { shm_id3 = shmget(SHM_KEY3, 4 * sizeof(unsigned short), IPC_CREAT|IPC_EXCL|0666); if (shm_id3 == -1) { perror("Create ShareMemory3"); return 1; } else { gRecord = (unsigned short *)shmat(shm_id3, 0, 0); if(gRecord ==(unsigned short *)-1) { perror("Attach share memory3"); return 1; } } } else { gRecord = (unsigned short *)shmat(shm_id3, 0, 0); if (gRecord == (unsigned short *)-1) { perror("Attach share memory3"); return 1; } } for (i = 0; i < 4; i++){ *(gRecord + i*sizeof(unsigned short)) = 0; } shm_id4 = shmget(SHM_KEY4, 4 * sizeof(unsigned long), 0); if (shm_id4 == -1) { shm_id4 = shmget(SHM_KEY4, 4 * sizeof(unsigned long), IPC_CREAT|IPC_EXCL|0666); if (shm_id4==-1) { perror("Create ShareMemory4"); return 1; } else { gCurrentFileSize = (unsigned long *)shmat(shm_id4, 0, 0); if (gCurrentFileSize == (unsigned long *)-1) { perror("Attach share memory4"); return 1; } } } else { gCurrentFileSize = (unsigned long *)shmat(shm_id4, 0, 0); if (gCurrentFileSize == (unsigned long *)-1) { perror("Attach share memory4"); return 1; } } for (i = 0; i < 4; i++) { *(gCurrentFileSize + i*sizeof(unsigned long)) = 0; } shm_id5 = shmget(SHM_KEY5, 4 * 40 * sizeof(char), 0); if (shm_id5 == -1) { shm_id5 = shmget(SHM_KEY5, 4 * 40 * sizeof(char), IPC_CREAT|IPC_EXCL|0666); if (shm_id5 == -1) { perror("Create ShareMemory5"); return 1; } else { file_head = (char *)shmat(shm_id5, 0, 0); if (file_head == (void *)-1) { perror("Attach share memory5"); return 1; } } } else { file_head = (char *)shmat(shm_id5, 0, 0); if (file_head == (void *)-1) { perror("Attach share memory5"); return 1; } } memset((char *)file_head, 0, 4 * 40 *sizeof(char)); return 0;}int DsServer::freeVideo(){ int i,retVal; fprintf(stderr,"beging stop!\n"); MP4_ServerStop(); //stop preview fprintf(stderr,"stop video!\n"); for(i=0;i<*channelCount;i++){ StopVideoPreview(channelHandle[i]); } fprintf(stderr,"stop audio!\n"); SetAudioPreview(channelHandle[audio_port],0); fprintf(stderr,"stop motion detection!\n"); //stop motion detect for(i = 0; i < *channelCount; i++) { StopMotionDetection(channelHandle[i]); } fprintf(stderr,"stop video capture!\n"); //stop capture for(i=0;i<*channelCount;i++){ StopVideoCapture(channelHandle[i]); } fprintf(stderr,"close channel!\n"); //close channel for(i=0;i<*channelCount;i++){ ChannelClose(channelHandle[i]); } fprintf(stderr,"deinit dsp!\n"); retVal = DeInitDSPs(); if(retVal<0){ fprintf(stderr,"Do DeInitDSPs Error(%08x)\n",GetLastErrorNum()); return 0; } return 1;}void DsServer::WriteDB(){ struct statfs bbuf; int freespace; int i; if(statfs(savepath, &bbuf) < 0){ printf("statsfs() faied, error: %d\n", errno); //printf("savepath = %s, bbuf,f_bavail = %d, bbuf.f_blocks = %d\n", savepath, bbuf.f_bavail, bbuf.f_blocks); } else{ freespace = (int)((bbuf.f_bavail * (bbuf.f_bsize/1024))/1024); fprintf(stderr,"freespace:%3dM %3d\n", freespace,bbuf.f_bsize); } while (*isExit == 0) { int retval=0; for(i=0;i<4;i++){ if(*(record_fd+i)==0 && *(gRecord+i*sizeof(unsigned short)) && (*head_size)!=0) { char filename[255]; time_t now; struct tm *tm; char timebuf[128]; now = time(NULL); tm = localtime(&now); strftime(timebuf, sizeof(timebuf), "%Y-%m-%d", tm); sprintf(filename, "%s/%s_%02d.mp4", savepath,timebuf, i); /* modify at 2002.4.1 */ if((retval = open(filename, O_WRONLY | O_CREAT |O_TRUNC, S_IRWXU|S_IRWXG|S_IRWXO)) < 0) perror(">>> open"); else{ ftruncate(retval, 0); write(retval, file_head+i*40, 40); *(record_fd+i) = retval; } }else{ if(*(record_fd+i)!=0) { if(*(gCurrentFileSize+i*sizeof(unsigned long)) > gRecordFileSize*1000*1000) { close(*(record_fd+i)); *(gCurrentFileSize+i*sizeof(unsigned long)) = 0; *(record_fd+i) = 0; } } } } }// sleep(10);// fprintf(stderr,"======================================\r\n");}void* DsServer::intermediary(void *ptr){ int retval=0; int i; struct statfs bbuf; int freespace; int total = 0; char lFileName[255]; char gCurrentFile[4][255]; struct dirent **namelist; while(*isExit == 0){ if(statfs(savepath, &bbuf) < 0){ printf("statsfs() faied, error: %d\n", errno); } else{ freespace = (int)((bbuf.f_bavail * (bbuf.f_bsize/1024))/1024); if(freespace < 400){ fprintf(stderr,"freespace:%3dM %3d , delete files!\n", freespace,bbuf.f_bsize); total = scandir(savepath, &namelist, 0, alphasort); if (total < 4) { perror("total\7\7\7"); continue; } for (i = 0; i < 4; i++) { if (strcmp(namelist[i]->d_name, ".") == 0 || strcmp(namelist[i]->d_name, "..") == 0) continue; sprintf(lFileName,"%s/%s",savepath,namelist[i]->d_name); if(unlink(lFileName)!=0) fprintf(stderr,"\nDelete file %s Error!\n",namelist[i]->d_name); } } } for(i=0;i<4;i++){ if(*(record_fd+i)==0 && *(gRecord+i*sizeof(unsigned short)) && (*head_size)!=0) { char filename[255]; time_t now; struct tm *tm; char timebuf[128]; now = time(NULL); tm = localtime(&now); strftime(timebuf, sizeof(timebuf), "%Y%m%d", tm); sprintf(filename, "%s/%s%02d%02d%02d_%02d.mp4", savepath,timebuf,tm->tm_hour,tm->tm_min,tm->tm_sec,i); /* modify at 2002.4.1 */ if((retval = open(filename, O_WRONLY | O_CREAT |O_TRUNC, S_IRWXU|S_IRWXG|S_IRWXO)) < 0) perror(">>> open"); else{ ftruncate(retval, 0); write(retval, file_head+i*40, 40); memset(gCurrentFile[i],0,255); strncpy(gCurrentFile[i],filename,strlen(filename)); //printf(gCurrentFile[i]); *(record_fd+i) = retval; //sprintf(filename, "%s/%s%02d%02d%02d_%02d.index", savepath,timebuf,tm->tm_hour,tm->tm_min,tm->tm_sec,i); } }else{ if(*(record_fd+i)!=0) { if(*(gCurrentFileSize+i*sizeof(unsigned long)) > gRecordFileSize*1000*1000) { close(*(record_fd+i)); *(gCurrentFileSize+i*sizeof(unsigned long)) = 0; *(record_fd+i) = 0; VodCreateIndex(gCurrentFile[i],strlen(gCurrentFile[i])); memset(gCurrentFile[i],0,255); } } } } usleep(1000); } return (NULL);}void DsServer::CreateThread(){ pthread_attr_t attr;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -