servfox源码分析(五)
时间:2010-09-17 来源:swinner1985
在网上看到有个servfox源码的分析,但是很遗憾好像还不完整。正好最近要学习一下servfox,就把自己的学习记录一下,希望大家一起学习。主要是接着那个servfox源码分析(四)开始写的
到此,视频设备的初始化工作完成,现在我们退出init_videoIn()函数,进入main()函数,我们看看它后面又是怎么工作的。
*********************************************************************************/ pthread_create (&w1, NULL, (void *) grab, NULL);
*********************************************************************************/ pthread_create()函数语法要点
所需头文件 #include <pthread.h>
函数原型 int pthread_create ((pthread_t *thread, pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg))
函数传入值 thread:线程标识符
attr:线程属性设置(其具体设置参见9.2.3小节),通常取为NULL
start_routine:线程函数的起始地址,是一个以指向void的指针作为参数和返回值的函数指针
arg:传递给start_routine的参数
函数返回值 成功:0
出错:返回错误码
创建线程,运行函数(void *) grab 跟踪进入void grab (void)
*********************************************************************************/
void
grab (void)
{
int err = 0;
for (;;)
{
if(debug) printf("I am the GRABBER !!!!! \n");
err = v4lGrab(&videoIn);
if (!videoIn.signalquit || (err < 0)){
if(debug) printf("GRABBER going out !!!!! \n");
break;
}
}
}
*********************************************************************************/ 在void grab (void)函数中,利用err = v4lGrab(&videoIn);调用v4lGrab函数 跟踪进入v4lGrab函数
*********************************************************************************/
int
v4lGrab (struct vdIn *vd )
{
static int frame = 0;
int len;
int size;
int erreur = 0;
int jpegsize = 0; struct frame_t *headerframe;
double timecourant =0;
double temps = 0;
timecourant = ms_time();
if (vd->grabMethod)
{
/* mmap method */
vd->vmmap.height = vd->hdrheight;
vd->vmmap.width = vd->hdrwidth;
vd->vmmap.format = vd->formatIn;
if (ioctl (vd->fd, VIDIOCSYNC, &vd->vmmap.frame) < 0)
{
perror ("cvsync err\n");
erreur = -1;
}
/* Is there someone using the frame */
while((vd->framelock[vd->frame_cour] != 0) && vd->signalquit)
usleep(1000);
pthread_mutex_lock (&vd->grabmutex); temps = ms_time();
jpegsize= convertframe(vd->ptframe[vd->frame_cour]+ sizeof(struct frame_t),
vd->pFramebuffer + vd->videombuf.offsets[vd->vmmap.frame],
vd->hdrwidth,vd->hdrheight,vd->formatIn,vd->framesizeIn);
headerframe=(struct frame_t*)vd->ptframe[vd->frame_cour];
snprintf(headerframe->header,5,"%s","SPCA");
headerframe->seqtimes = ms_time();
headerframe->deltatimes=(int)(headerframe->seqtimes-timecourant);
headerframe->w = vd->hdrwidth;
headerframe->h = vd->hdrheight;
headerframe->size = (( jpegsize < 0)?0:jpegsize);
headerframe->format = vd->formatIn;
headerframe->nbframe = frame++;
// printf("compress frame %d times %f\n",frame, headerframe->seqtimes-temps);
pthread_mutex_unlock (&vd->grabmutex);
/************************************/
if ((ioctl (vd->fd, VIDIOCMCAPTURE, &(vd->vmmap))) < 0)
{
perror ("cmcapture");
if(debug) printf (">>cmcapture err \n");
erreur = -1;
}
vd->vmmap.frame = (vd->vmmap.frame + 1) % vd->videombuf.frames;
vd->frame_cour = (vd->frame_cour +1) % OUTFRMNUMB;
//if(debug) printf("frame nb %d\n",vd->vmmap.frame); }
else
{
/* read method */
size = vd->framesizeIn;
len = read (vd->fd, vd->pFramebuffer, size);
if (len < 0 )
{
if(debug) printf ("v4l read error\n");
if(debug) printf ("len %d asked %d \n", len, size);
return 0;
}
/* Is there someone using the frame */
while((vd->framelock[vd->frame_cour] != 0)&& vd->signalquit)
usleep(1000);
pthread_mutex_lock (&vd->grabmutex); temps = ms_time();
jpegsize= convertframe(vd->ptframe[vd->frame_cour]+ sizeof(struct frame_t),
vd->pFramebuffer ,
vd->hdrwidth,vd->hdrheight,vd->formatIn,vd->framesizeIn);
headerframe=(struct frame_t*)vd->ptframe[vd->frame_cour];
snprintf(headerframe->header,5,"%s","SPCA");
headerframe->seqtimes = ms_time();
headerframe->deltatimes=(int)(headerframe->seqtimes-timecourant);
headerframe->w = vd->hdrwidth;
headerframe->h = vd->hdrheight;
headerframe->size = (( jpegsize < 0)?0:jpegsize);
headerframe->format = vd->formatIn;
headerframe->nbframe = frame++;
// if(debug) printf("compress frame %d times %f\n",frame, headerframe->seqtimes-temps);
vd->frame_cour = (vd->frame_cour +1) % OUTFRMNUMB;
pthread_mutex_unlock (&vd->grabmutex);
/************************************/
}
return erreur;
}
*********************************************************************************/ 在v4lGrab函数中,主要完成了图像的采集,同步以及存贮。将采集的图像数据储存到vd->ptframe[vd->frame_cour]+ sizeof(struct frame_t)中,在后面的客户端显示socket传送中要用到。
涉及到获取JPEG格式图像帧的大小,JPEG图像
EOI(End of Image) 结束标志
标记结构 字节数 意义
0XFF????1
0XD9????1 退出,进入grab函数,该函数不断采集图像,不难理解。这个是在线程中运行的,同时main函数中其他的也在运行,回到main中
*********************************************************************************/ pthread_create (&w1, NULL, (void *) grab, NULL);
*********************************************************************************/ pthread_create()函数语法要点
所需头文件 #include <pthread.h>
函数原型 int pthread_create ((pthread_t *thread, pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg))
函数传入值 thread:线程标识符
attr:线程属性设置(其具体设置参见9.2.3小节),通常取为NULL
start_routine:线程函数的起始地址,是一个以指向void的指针作为参数和返回值的函数指针
arg:传递给start_routine的参数
函数返回值 成功:0
出错:返回错误码
创建线程,运行函数(void *) grab 跟踪进入void grab (void)
*********************************************************************************/
void
grab (void)
{
int err = 0;
for (;;)
{
if(debug) printf("I am the GRABBER !!!!! \n");
err = v4lGrab(&videoIn);
if (!videoIn.signalquit || (err < 0)){
if(debug) printf("GRABBER going out !!!!! \n");
break;
}
}
}
*********************************************************************************/ 在void grab (void)函数中,利用err = v4lGrab(&videoIn);调用v4lGrab函数 跟踪进入v4lGrab函数
*********************************************************************************/
int
v4lGrab (struct vdIn *vd )
{
static int frame = 0;
int len;
int size;
int erreur = 0;
int jpegsize = 0; struct frame_t *headerframe;
double timecourant =0;
double temps = 0;
timecourant = ms_time();
if (vd->grabMethod)
{
/* mmap method */
vd->vmmap.height = vd->hdrheight;
vd->vmmap.width = vd->hdrwidth;
vd->vmmap.format = vd->formatIn;
if (ioctl (vd->fd, VIDIOCSYNC, &vd->vmmap.frame) < 0)
{
perror ("cvsync err\n");
erreur = -1;
}
/* Is there someone using the frame */
while((vd->framelock[vd->frame_cour] != 0) && vd->signalquit)
usleep(1000);
pthread_mutex_lock (&vd->grabmutex); temps = ms_time();
jpegsize= convertframe(vd->ptframe[vd->frame_cour]+ sizeof(struct frame_t),
vd->pFramebuffer + vd->videombuf.offsets[vd->vmmap.frame],
vd->hdrwidth,vd->hdrheight,vd->formatIn,vd->framesizeIn);
headerframe=(struct frame_t*)vd->ptframe[vd->frame_cour];
snprintf(headerframe->header,5,"%s","SPCA");
headerframe->seqtimes = ms_time();
headerframe->deltatimes=(int)(headerframe->seqtimes-timecourant);
headerframe->w = vd->hdrwidth;
headerframe->h = vd->hdrheight;
headerframe->size = (( jpegsize < 0)?0:jpegsize);
headerframe->format = vd->formatIn;
headerframe->nbframe = frame++;
// printf("compress frame %d times %f\n",frame, headerframe->seqtimes-temps);
pthread_mutex_unlock (&vd->grabmutex);
/************************************/
if ((ioctl (vd->fd, VIDIOCMCAPTURE, &(vd->vmmap))) < 0)
{
perror ("cmcapture");
if(debug) printf (">>cmcapture err \n");
erreur = -1;
}
vd->vmmap.frame = (vd->vmmap.frame + 1) % vd->videombuf.frames;
vd->frame_cour = (vd->frame_cour +1) % OUTFRMNUMB;
//if(debug) printf("frame nb %d\n",vd->vmmap.frame); }
else
{
/* read method */
size = vd->framesizeIn;
len = read (vd->fd, vd->pFramebuffer, size);
if (len < 0 )
{
if(debug) printf ("v4l read error\n");
if(debug) printf ("len %d asked %d \n", len, size);
return 0;
}
/* Is there someone using the frame */
while((vd->framelock[vd->frame_cour] != 0)&& vd->signalquit)
usleep(1000);
pthread_mutex_lock (&vd->grabmutex); temps = ms_time();
jpegsize= convertframe(vd->ptframe[vd->frame_cour]+ sizeof(struct frame_t),
vd->pFramebuffer ,
vd->hdrwidth,vd->hdrheight,vd->formatIn,vd->framesizeIn);
headerframe=(struct frame_t*)vd->ptframe[vd->frame_cour];
snprintf(headerframe->header,5,"%s","SPCA");
headerframe->seqtimes = ms_time();
headerframe->deltatimes=(int)(headerframe->seqtimes-timecourant);
headerframe->w = vd->hdrwidth;
headerframe->h = vd->hdrheight;
headerframe->size = (( jpegsize < 0)?0:jpegsize);
headerframe->format = vd->formatIn;
headerframe->nbframe = frame++;
// if(debug) printf("compress frame %d times %f\n",frame, headerframe->seqtimes-temps);
vd->frame_cour = (vd->frame_cour +1) % OUTFRMNUMB;
pthread_mutex_unlock (&vd->grabmutex);
/************************************/
}
return erreur;
}
*********************************************************************************/ 在v4lGrab函数中,主要完成了图像的采集,同步以及存贮。将采集的图像数据储存到vd->ptframe[vd->frame_cour]+ sizeof(struct frame_t)中,在后面的客户端显示socket传送中要用到。
涉及到获取JPEG格式图像帧的大小,JPEG图像
EOI(End of Image) 结束标志
标记结构 字节数 意义
0XFF????1
0XD9????1 退出,进入grab函数,该函数不断采集图像,不难理解。这个是在线程中运行的,同时main函数中其他的也在运行,回到main中
相关阅读 更多 +