⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.cpp

📁 用于Linux下的网络视频。包里包括了接收端和发送端。大部份是从Spcaview修改过来的
💻 CPP
📖 第 1 页 / 共 2 页
字号:
 
   pthread_mutex_init (&vd->grabmutex, NULL);
 return err;
}
static int
GetVideoPict (struct vdIn *vd)
{
 if (ioctl (vd->fd, VIDIOCGPICT, &vd->videopict) < 0)
    exit(1);
 
 
 printf ("VIDIOCGPICT brightnes=%d hue=%d color=%d contrast=%d whiteness=%d"
     "depth=%d palette=%d\n", vd->videopict.brightness,
     vd->videopict.hue, vd->videopict.colour, vd->videopict.contrast,
     vd->videopict.whiteness, vd->videopict.depth,
     vd->videopict.palette);
 return 0;
}
int
init_v4l (struct vdIn *vd)
{
 int f;
 int erreur = 0;
 int err;
 if ((vd->fd = open (vd->videodevice, O_RDWR)) == -1)
    exit(1);
 
 if (ioctl (vd->fd, VIDIOCGCAP, &(vd->videocap)) == -1)
    exit(1);
 
 printf ("Camera found: %s \n", vd->videocap.name);
 snprintf (vd->cameraname, 32, "%s", vd->videocap.name);
 
 erreur = GetVideoPict (vd);
   if (ioctl (vd->fd, VIDIOCGCHAN, &vd->videochan) == -1)
    {
      printf ("Hmm did not support Video_channel\n");
      vd->cameratype = UNOW;
    }
 else
    {
    if (vd->videochan.name){
      printf ("Bridge found: %s \n", vd->videochan.name);
      snprintf (vd->bridge, 9, "%s", vd->videochan.name);
      }
      else
      {
      vd->cameratype = UNOW;
      }
    }
   printf ("StreamId: %d Camera\n", vd->cameratype);
 
     printf (" Format asked %d check %d\n",vd->formatIn, err); 
 vd->videopict.palette = vd->formatIn;
 vd->videopict.depth = (8 * 3) >> 1;
 vd->bppIn = (8 * 3) >> 1;
 
   vd->framesizeIn = (vd->hdrwidth * vd->hdrheight * vd->bppIn) >> 3;
   erreur = SetVideoPict (vd);
 erreur = GetVideoPict (vd);
 if (vd->formatIn != vd->videopict.palette ||
      vd->bppIn != vd->videopict.depth)
    exit(1);
 if (erreur < 0)
    exit(1);
      printf (" grabbing method default MMAP asked \n");
      // MMAP VIDEO acquisition
      memset (&(vd->videombuf), 0, sizeof (vd->videombuf));
      if (ioctl (vd->fd, VIDIOCGMBUF, &(vd->videombuf)) < 0)
    {
     perror (" init VIDIOCGMBUF FAILED\n");
    }
      printf ("VIDIOCGMBUF size %d frames %d offets[0]=%d offsets[1]=%d\n",
          vd->videombuf.size, vd->videombuf.frames,
          vd->videombuf.offsets[0], vd->videombuf.offsets[1]);
      vd->pFramebuffer =
    (unsigned char *) mmap (0, vd->videombuf.size, PROT_READ | PROT_WRITE,
                MAP_SHARED, vd->fd, 0);
      vd->mmapsize = vd->videombuf.size;
      vd->vmmap.height = vd->hdrheight;
      vd->vmmap.width = vd->hdrwidth;
      vd->vmmap.format = vd->formatIn;
      for (f = 0; f < vd->videombuf.frames; f++)
    {
     vd->vmmap.frame = f;
     if (ioctl (vd->fd, VIDIOCMCAPTURE, &(vd->vmmap)))
        {
          perror ("cmcapture");
        }
    }
      vd->vmmap.frame = 0;
 
 vd->frame_cour = 0;
 return erreur;
}
 
int 
v4lGrab (struct vdIn *vd )
{
    static int frame = 0;
 
 int len;
 int status;
 int count = 0;
 int size;
 int erreur = 0;
 int m4vsize = 0;
 int qualite = 1024;
 struct frame_t *headerframe;
 double timecourant =0;
 double temps = 0;
/******XVID****/
      unsigned char *mp4_buffer = NULL;
      unsigned char *in_buffer = NULL;
      int key;
    int stats_type;
    int stats_quant;
    int stats_length;
      int sse[3];
      int enc_result;
      int input_num;
/******************/
      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);
/********************************XVID******************************************/
in_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM, YDIM));
    if (!in_buffer)
                 {printf("in_buffer err!");
        goto free_all_memory;}
mp4_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM, YDIM) * 2);
    if (mp4_buffer==NULL)
    {
        fprintf(stderr,"malloc error");
        goto free_all_memory;
    }
enc_result = enc_init(1);
    if (enc_result) {
        fprintf(stderr, "Encore INIT problem, return value %d\n", enc_result);
        goto release_all;
    }
memcpy(in_buffer,vd->pFramebuffer + vd->videombuf.offsets[vd->vmmap.frame],IMAGE_SIZE(XDIM, YDIM));
 printf("the framesize is %d",IMAGE_SIZE(XDIM, YDIM));
        
    m4vsize=enc_main((uint8_t *)in_buffer,
        mp4_buffer,&key, &stats_type,&stats_quant, &stats_length, sse);
         printf("the m4vsize is %d!!!!\n\n",m4vsize);
memcpy(vd->ptframe[vd->frame_cour]+ sizeof(struct frame_t),mp4_buffer,m4vsize);
/****************************************************************************/
     headerframe=(struct frame_t*)vd->ptframe[vd->frame_cour];
     snprintf(headerframe->header,5,"%s","SPCA"); 
     headerframe->deltatimes=(int)(headerframe->seqtimes-timecourant); 
     headerframe->w = vd->hdrwidth;
     headerframe->h = vd->hdrheight;
     headerframe->size = (( m4vsize < 0)?0:m4vsize);
     headerframe->format = vd->formatIn;
     headerframe->nbframe = frame++; 
 
    pthread_mutex_unlock (&vd->grabmutex); 
    /************************************/
    
      if ((ioctl (vd->fd, VIDIOCMCAPTURE, &(vd->vmmap))) < 0)
    {
     perror ("cmcapture");
     printf (">>cmcapture err %d\n", status);
     erreur = -1;
    }
      vd->vmmap.frame = (vd->vmmap.frame + 1) % vd->videombuf.frames;
      vd->frame_cour = (vd->frame_cour +1) % OUTFRMNUMB;
      //printf("frame nb %d\n",vd->vmmap.frame);
release_all:
 
 
    if (enc_handle) {
        enc_result = enc_stop();
        if (enc_result)
            fprintf(stderr, "Encore RELEASE problem return value %d\n",
                    enc_result);
    }
free_all_memory:
        printf("free the mem");
    free(mp4_buffer);
    free(in_buffer);
 return erreur;
}
static int
SetVideoPict (struct vdIn *vd)
{
 if (ioctl (vd->fd, VIDIOCSPICT, &vd->videopict) < 0)
    exit(1);
 
 printf ("VIDIOCSPICT brightnes=%d hue=%d color=%d contrast=%d whiteness=%d"
     "depth=%d palette=%d\n", vd->videopict.brightness,
     vd->videopict.hue, vd->videopict.colour, vd->videopict.contrast,
     vd->videopict.whiteness, vd->videopict.depth,
     vd->videopict.palette);
 
 return 0;
}
 
int
close_v4l (struct vdIn *vd)
{
 int i;
      printf ("unmapping frame buffer\n");
      munmap (vd->pFramebuffer, vd->mmapsize);
 printf ("close video_device\n"); 
 close (vd->fd);
 
 if (vd->videodevice)
    {
      free (vd->videodevice);
      vd->videodevice = NULL;
    }
 if (vd->cameraname)
    {
      free (vd->cameraname);
      vd->cameraname = NULL;
    }
 for (i = 0; i < OUTFRMNUMB; i++)
    {
      if (vd->ptframe[i])
    {
     free (vd->ptframe[i]);
     vd->ptframe[i] = NULL;
     vd->framelock[i] = 0;
     printf ("freeing output buffer %d\n",i);
    }
    }
     pthread_mutex_destroy (&vd->grabmutex);
}
int sendpacket ( unsigned char *framepointer, int framelength)
{     
   bool done = false;
    while (!done)
    {
      if (framelength > Packetfixlength)
    {
     framelength -= Packetfixlength;
     nread = Packetfixlength;
    }
      else
    {
     nread = framelength;
     framelength = 0;
    }
 
       
      if (nread!=frameheadfixlength&&framelength==0)
              {status = session.SendPacket(framepointer, nread,26,1,1000);}
         else {status = session.SendPacket(framepointer, nread,26,0,1000);}
      if (status < 0)
    {    
     exit (-1);
    }
      RTPTime::Wait (delay);
      framepointer += Packetfixlength;
      printf("%d",nread);
      if (framelength == 0)
    return (10); 
    }
 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -