?? server.cpp
字號:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <wait.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/videodev.h>
#include <pthread.h>
#include "jrtplib3/rtpsession.h"
#include "jrtplib3/rtppacket.h"
#include "jrtplib3/rtpudpv4transmitter.h"
#include "jrtplib3/rtpipv4address.h"
#include "jrtplib3/rtpsessionparams.h"
#include "jrtplib3/rtperrors.h"
#include <iostream>
#include "cxvid.h"
#include "tcp.h"
#define OUTFRMNUMB 4
static int XDIM=320,YDIM=240;
uint8_t localip[] = { 202, 194, 26, 67 };
#define Packetfixlength 7000
#define frameheadfixlength 50
RTPTime delay (0.0001);
int status;
int nread;
RTPSession session;
RTPSessionParams sessionparams;
RTPUDPv4TransmissionParams transparams;
int sendpacket (unsigned char *framepointer, int framelength);
struct frame_t{
char header[5];
int nbframe;
double seqtimes;
int deltatimes;
int w;
int h;
int size;
int format;
unsigned short bright;
unsigned short contrast;
unsigned short colors;
unsigned short exposure;
unsigned char wakeup;
int acknowledge;
} __attribute__ ((packed));
struct vdIn {
int fd;
char *videodevice ;
struct video_mmap vmmap;
struct video_capability videocap;
int mmapsize;
struct video_mbuf videombuf;
struct video_picture videopict;
struct video_window videowin;
struct video_channel videochan;
int cameratype ;
char *cameraname;
char bridge[9];
int palette; // available palette
int channel ; int grabMethod ;
unsigned char *pFramebuffer;
unsigned char *ptframe[4];
int framelock[4];
pthread_mutex_t grabmutex;
int framesizeIn ;
volatile int frame_cour;
int bppIn;
int hdrwidth;
int hdrheight;
int formatIn;
int signalquit;
};
struct vdIn videoIn;
int init_v4l (struct vdIn *vd);
static int SetVideoPict (struct vdIn *vd);
static int GetVideoPict (struct vdIn *vd);
int close_v4l (struct vdIn *vd);
int init_videoIn (struct vdIn *vd, char *device, int width, int height,int format ,int grabmethod);
int v4lGrab (struct vdIn *vd );
void *grab (void *);
void *service (void *ir);
void sigchld_handler(int s);
int main ()
{
char *videodevice = "/dev/video0";
int err;
int grabmethod = 1;
int width = 320;
int height = 240;
int i;
int serv_sock,new_sock;
pthread_t w1;
pthread_t server_th;
int sin_size;
unsigned short serverport = 7070;
struct sockaddr_in their_addr;
struct sigaction sa;
sessionparams.SetOwnTimestampUnit (1 / 90000);
sessionparams.SetAcceptOwnPackets (true);
sessionparams.SetMaximumPacketSize (10000);
transparams.SetPortbase (8000);
status = session.Create (sessionparams, &transparams);
if (status < 0)
{
exit (-1);
}
RTPIPv4Address addr (localip, 7000);
status = session.AddDestination (addr);
if (status < 0)
{
exit (-1);
}
memset (&videoIn, 0, sizeof (struct vdIn));
if (init_videoIn
(&videoIn, videodevice, width, height, VIDEO_PALETTE_YUV420P,grabmethod) != 0)
printf (" damned encore rate !!\n");
if((err= pthread_create (&w1, NULL,grab, NULL)) != 0){
printf("thread grabbing error %d \n",err);
close_v4l (&videoIn);
exit(1);
}
serv_sock = open_sock(serverport);
signal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
sa.sa_handler = sigchld_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
syslog(LOG_ERR," server Listening on Port %d\n",serverport);
printf("Waiting .... for connection. CTrl_c to stop !!!! \n");
while (videoIn.signalquit)
{
sin_size = sizeof(struct sockaddr_in);
if ((new_sock = accept(serv_sock, (struct sockaddr *)&their_addr, (size_t *)&sin_size)) == -1)
{
continue;
}
syslog(LOG_ERR,"Got connection from %s\n",inet_ntoa(their_addr.sin_addr));
printf("Got connection from %s\n",inet_ntoa(their_addr.sin_addr));
pthread_create(&server_th, NULL, service, &new_sock);
}
pthread_join (w1, NULL);
close(serv_sock);
close_v4l (&videoIn);
}
void
*grab (void*)
{
int err = 0;
for (;;)
{
err = v4lGrab(&videoIn);
if (!videoIn.signalquit || (err < 0)){
printf("GRABBER going out !!!!! \n");
break;
}
}
}
void
*service (void *ir)
{
int *id = (int*) ir;
int frameout = 1;
struct frame_t *headerframe;
int ret;
int sock;
int framecount=0;
int retsize=0;
int ack = 0;
unsigned char x = 0;
unsigned char y = 0;
int err;
unsigned char wakeup = 0;
unsigned char cmd = 0;
unsigned short bright;
unsigned short contrast;
sock = *id;
for (;;)
{
ack =1;
redo:
while ((frameout == videoIn.frame_cour) && videoIn.signalquit) usleep(1000);
if (videoIn.signalquit){
videoIn.framelock[frameout]++;
headerframe = (struct frame_t *) videoIn.ptframe[frameout];
headerframe->acknowledge = ack;
headerframe->bright = bright;
headerframe->contrast = contrast;
headerframe->wakeup = wakeup;
if(headerframe->size == 0){
// frame corrupt get another one
videoIn.framelock[frameout]--;
frameout = (frameout+1)% OUTFRMNUMB;
goto redo;
}
ret=sendpacket((unsigned char *)headerframe,sizeof(struct frame_t));
printf("\n sock send out");
if(!wakeup)
{
printf("\nthe packat is %d",headerframe->size);
ret=sendpacket((unsigned char*)(videoIn.ptframe[frameout]+sizeof(struct frame_t)),headerframe->size);
}
videoIn.framelock[frameout]--;
frameout = (frameout+1)% OUTFRMNUMB;
} else {
printf("reader %d going out \n",*id);
break;
}
}
close_sock(sock);
pthread_exit(NULL);
}
void sigchld_handler(int s)
{
videoIn.signalquit = 0;
}
int
init_videoIn (struct vdIn *vd, char *device, int width, int height,
int format, int grabmethod)
{
int err = -1;
int i;
if (vd == NULL || device == NULL)
return -1;
if (width == 0 || height == 0)
return -1;
if(grabmethod < 0 || grabmethod > 1)
grabmethod = 1; //read by default;
// check format
vd->videodevice = NULL;
vd->cameraname = NULL;
vd->videodevice = NULL;
vd->videodevice = (char *) realloc (vd->videodevice, 16);
vd->cameraname = (char *) realloc (vd->cameraname, 32);
snprintf (vd->videodevice, 12, "%s", device);
printf("video %s \n",vd->videodevice);
memset (vd->cameraname, 0, sizeof (vd->cameraname));
memset(vd->bridge, 0, sizeof(vd->bridge));
vd->signalquit = 1;
vd->hdrwidth = width;
vd->hdrheight = height;
/* compute the max frame size */
vd->formatIn = format;
vd->bppIn = (8 * 3) >> 1;
vd->grabMethod = grabmethod; //mmap or read
vd->pFramebuffer = NULL;
/* init and check all setting */
err = init_v4l (vd);
/* allocate the 4 frames output buffer */
for (i = 0; i < OUTFRMNUMB; i++)
{
vd->ptframe[i] = NULL;
vd->ptframe[i] =
(unsigned char *) realloc (vd->ptframe[i], sizeof(struct frame_t) + (size_t) vd->framesizeIn );
vd->framelock[i] = 0;
}
vd->frame_cour = 0;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -