21,616
社区成员




#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include<fcntl.h>
unsigned char *pu8Buf = NULL;
void ExitHandleSig(int signo){
if (SIGINT == signo || SIGTSTP == signo){
if(pu8Buf != NULL) free(pu8Buf);
printf("\033[0;31mprogram exit abnormally!\033[0;39m\n");
}
exit(0);
}
int main(){
#if 1
//fread fopen 版本
FILE *fpStrm=NULL;
int s32UsedBytes = 0, s32ReadLen = 0;
unsigned int bufSize=1280 * 720 * 3 / 2;
pu8Buf = (unsigned char*)malloc(bufSize);
fpStrm = fopen("../videofile/1280_720_H264.h264", "rb");
if(fpStrm == NULL){
printf("Can't open file \n");
exit(0);
}
signal(SIGINT, ExitHandleSig);
signal(SIGTERM, ExitHandleSig);
while(1){
fseek(fpStrm, s32UsedBytes, SEEK_SET);
s32ReadLen = fread(pu8Buf, 1, bufSize, fpStrm);
if(s32ReadLen != bufSize){
printf("end of file \n");
fseek(fpStrm, 0, SEEK_SET);
s32UsedBytes = 0;
s32ReadLen = 0;
}
s32UsedBytes = s32UsedBytes +s32ReadLen ;
sleep(5);
}
#else
//read open 版本
int fileFd;
int s32UsedBytes = 0, s32ReadLen = 0;
unsigned int bufSize=720 * 480 * 3 / 2;
pu8Buf = (unsigned char*)malloc(bufSize);
fileFd = open("../videofile/720_480_H264.h264",O_RDONLY,S_IRUSR);
if(fileFd == -1)
printf("Can't open file \n");
signal(SIGINT, ExitHandleSig);
signal(SIGTERM, ExitHandleSig);
while(1){
lseek(fileFd, s32UsedBytes, SEEK_SET);
s32ReadLen = read(fileFd,pu8Buf,bufSize);
if(s32ReadLen != bufSize){
printf("end of file \n");
lseek(fileFd, 0, SEEK_SET);
s32UsedBytes = 0;
s32ReadLen = 0;
}
s32UsedBytes = s32UsedBytes +s32ReadLen ;
usleep(50000);
}
#endif
return 1;
}