【文件属性】:
文件名称:缓冲区代码
文件大小:157KB
文件格式:ZIP
更新时间:2017-03-31 16:49:07
缓存
缓冲区的代码 #include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "stream.h"
int quit;
#define DELAY_MAX 5000 //最大延时5000毫秒
#define IN_NUM (2)
#define MAIN_STREAM (0)
#define SLAVE_STREAM (1)
void Init_Interface(int Msg_id)
{
int qid;
if(ApproDrvInit(Msg_id))
exit(1);
if (func_get_mem(&qid))
{
ApproDrvExit();
exit(1);
}
}
void Clean_Interface(void)
{
ApproInterfaceExit();
}
static int alarm_rec_enable;
static unsigned int deltime;
static unsigned int GetTimeStamp(void)
{
struct timeval timeval;
gettimeofday(&timeval, NULL);
return (timeval.tv_sec * 1000) + (timeval.tv_usec + 500) / 1000;;
}
void *get_frame(void *arg)
{
FILE *filefd=NULL;
char filename[80];
int ch=0;
stream_rpos_t rpos;
frame_head_t frame_head;
int first_frame_type=0;
int first_frame=0;
char *frame_pos;
char *data;
unsigned int cur_time;
printf("get ch %d stream\n", ch);
memset(&rpos, 0, sizeof(rpos));
st_stream_getStartPos(ch, MAIN_STREAM, &rpos);
st_stream_getOneFrame(ch, MAIN_STREAM, 10, &rpos);
while(!quit)
{
if(alarm_rec_enable)
{
if(st_stream_getOneFrame(ch, MAIN_STREAM, 200, &rpos) <= 0)
{
usleep(10*1000);
continue;
}
frame_pos = &rpos.p_buf_data[rpos.data_start_pos];
memcpy(&frame_head,&rpos.p_buf_data[rpos.data_start_pos],sizeof(frame_head));
data=frame_pos+sizeof(frame_head_t);
if(frame_head.frame_type==I_FRAME_TYPE)
{
first_frame_type=1;
if(first_frame==0)
first_frame=1;
}
if(first_frame==1)//第一帧
{
first_frame=2;
printf("first_frame\n");
sprintf(filename,"test%d.h264",frame_head.pts);
filefd=fopen(filename,"wb");
if(filefd==NULL)
{
printf("Error fopen\n");
quit=1;
return (void*) -1;
}
}
if(first_frame_type==1)//保证第一帧必需是I帧
{
if( fwrite(data , sizeof(char), frame_head.frame_size,filefd) <= 0)
{
printf("save file error!!");
quit=1;
return (void*) -1;
}
// printf("11ch %d frame no %d frame_type %d fram_size %d data\n"
// , ch
// , frame_head.frame_no
// , frame_head.frame_type
// , frame_head.frame_size
// );
}
usleep(10*1000);
}
else
{
unsigned int TimeStamp;
memcpy(&frame_head,&rpos.p_buf_data[rpos.data_start_pos],sizeof(frame_head));
TimeStamp=GetTimeStamp();
if(filefd)
{
fclose(filefd);
filefd=NULL;
}
if(TimeStamp-frame_head.pts>DELAY_MAX)
{
if(st_stream_getOneFrame(ch, MAIN_STREAM, 200, &rpos) <= 0)
{
usleep(10*1000);
continue;
}
frame_pos = &rpos.p_buf_data[rpos.data_start_pos];
data=frame_pos+sizeof(frame_head_t);
first_frame_type=0;
first_frame=0;
// printf("ch %d frame no %d frame_type %d fram_size %d data\n"
// , ch
// , frame_head.frame_no
// , frame_head.frame_type
// , frame_head.frame_size
// );
usleep(10*1000);
}
else
usleep(10*1000);
}
}
if(filefd)
{
fclose(filefd);
filefd=NULL;
}
return (void *)0;
}
void *put_frame(void *arg)
{
AV_DATA vol_data;
int vType = 1;//0为主码流,1为从码流
int error_code = 0;
int SerialBook = -1;
int SerialLock = -1;
int ret = 0;
frame_t cur_frame;
AV_DATA av_data;
char mpeg4_header[] = {0x00, 0x00, 0x01, 0xb0, 0x01, 0x00, 0x00, 0x01, 0xb5, 0x09};
static int mpeg4_field[7][2] =
{
{AV_OP_LOCK_MP4_VOL, AV_OP_LOCK_MP4_CIF_VOL},
{AV_OP_UNLOCK_MP4_VOL, AV_OP_UNLOCK_MP4_CIF_VOL},
{AV_OP_LOCK_MP4, AV_OP_LOCK_MP4_CIF},
{AV_OP_LOCK_MP4_IFRAME, AV_OP_LOCK_MP4_CIF_IFRAME},
{AV_OP_UNLOCK_MP4, AV_OP_UNLOCK_MP4_CIF},
{AV_OP_GET_MPEG4_SERIAL, AV_OP_GET_MPEG4_CIF_SERIAL},
{AV_OP_WAIT_NEW_MPEG4_SERIAL, AV_OP_WAIT_NEW_MPEG4_CIF_SERIAL}};
#define LOCK_MP4_VOL 0
#define UNLOCK_MP4_VOL 1
#define LOCK_MP4 2
#define LOCK_MP4_IFRAM 3
#define UNLOCK_MP4 4
#define GET_MPEG4_SERIAL 5
#define WAIT_NEW_MPEG4_SERIAL 6
if(GetAVData(mpeg4_field[LOCK_MP4_VOL][vType], -1, &vol_data) != RET_SUCCESS)
{
printf("Error on Get Vol data\n");
goto RECORD_QUIT;
}
#if 0
if( fwrite( vol_data.ptr , sizeof(char), vol_data.size, pfd ) <= 0)
{
printf("save file error!!");
error_code = -1;
goto RECORD_QUIT;
}
#endif
GetAVData(mpeg4_field[UNLOCK_MP4_VOL][vType], -1, &vol_data);
do
{
GetAVData(mpeg4_field[GET_MPEG4_SERIAL][vType], -1, &av_data );
} while (av_data.flags != AV_FLAGS_MP4_I_FRAME);
SerialBook = av_data.serial;
while(!quit)
{
ret = GetAVData(mpeg4_field[LOCK_MP4][vType], SerialBook, &av_data );
if (ret == RET_SUCCESS)
{
if(av_data.frameType==AV_FRAME_TYPE_I_FRAME)//AV_FRAME_TYPE_I_FRAME,AV_FRAME_TYPE_P_FRAME
cur_frame.frame_head.frame_type = I_FRAME_TYPE;
else
cur_frame.frame_head.frame_type = P_FRAME_TYPE;
cur_frame.frame_head.pts=GetTimeStamp();
cur_frame.frame_head.frame_no = av_data.serial;
cur_frame.frame_head.frame_size = av_data.size;
cur_frame.frame_data = av_data.ptr;
st_stream_sendFrameToPool(0, MAIN_STREAM, &cur_frame);
if (SerialLock >= 0)
{
GetAVData(mpeg4_field[UNLOCK_MP4][vType], SerialLock, &av_data);
}
SerialLock = SerialBook;
// save_cnt--;
SerialBook++;
}
else if (ret == RET_NO_VALID_DATA)
{
// wait new frame
usleep(1000);
}
else
{
GetAVData(mpeg4_field[GET_MPEG4_SERIAL][vType], -1, &av_data );
SerialBook = av_data.serial;
}
}
GetAVData(mpeg4_field[UNLOCK_MP4][vType], SerialLock, &av_data);
RECORD_QUIT:
return (void*)error_code;
}
int main(int argc, char *argv[])
{
int ret;
char get;
pthread_t get_id;
pthread_t put_id;
stream_manage_t stream_manage;
quit=0;
alarm_rec_enable=0;
// stream_writeFrame_t stream_write;
// memset(&stream_write, 0, sizeof(stream_write));
// stream_write.userfillfun = write_frameTobuf;
// srand(time(NULL));
memset(&stream_manage, 0, sizeof(stream_manage));
stream_manage.ch_num = 1;
stream_manage.two_stream = 2;
// stream_manage.get_recordStatCB = record_stat;
st_stream_avRegister(&stream_manage);
st_stream_init_avstream();
Init_Interface(16);
ret = pthread_create(&get_id, NULL, get_frame, NULL);
if(ret < 0)
{
printf("-------------------crete phtread error %s\n", strerror(errno));
}
ret = pthread_create(&put_id, NULL, put_frame, NULL);
if(ret < 0)
{
Clean_Interface();
printf("-------------------crete phtread error %s\n", strerror(errno));
}
while(!quit)
{
get=getchar();
switch(get)
{
case 'q':
quit=1;
break;
case 'r'://开始录像
alarm_rec_enable=1;
break;
case 's'://停止录像
alarm_rec_enable=0;
break;
default:break;
}
}
pthread_join(get_id,NULL);
pthread_join(put_id,NULL);
st_stream_destroy_avstream();
Clean_Interface();
printf("exit\n");
return 0;
}
【文件预览】:
stream_20140227.tar.bz2
stream.tar.bz2