/* Receive raw data from network */ #include #include #include /* struct sockaddr_in */ #include /* inet_ntoa() */ #include /* gettimeofday() */ #include #include #include #include #if !defined(nextstep) #include #endif #include #include /* select(), perror() */ #include /* getopt(), atoi() */ #include /* fmod() */ #include "raw_rtp.h" #include "ansi.h" #include "sysdep.h" #define VERSION "1.0" static int sock[2]; static struct evt_queue_elt* evt_queue = NULL; static int open_network(char *host, struct sockaddr_in *sin); int packet_handler(struct timeval now, struct sockaddr_in sin, int len, u_int8* packet); #define DEBUG extern void getLocal(struct sockaddr_in *p, int ssock); extern int OpenSock(u_int32_t addr, u_short port, unsigned char* ttl); extern int update_seq(source *s, u_int16 seq); extern float lost_rate(source*s); extern void insert_in_evt_queue(struct evt_queue_elt *elt, struct evt_queue_elt* evt_queue); extern void RTPSchedule(void* opaque, struct timeval *tp, struct evt_queue_elt* evt_queue); extern double tdbl(struct timeval *a); extern struct timeval db2tv(double d); source s; static void usage(char *argv0) { fprintf(stderr, "Usage: %s [IP]/port\n", argv0); } static void done(int sig) { exit(0); } /* * Open network sockets. */ static int open_network(char *host, struct sockaddr_in *sin) { int i; int nfds = 0; struct sockaddr_in recv_local; extern int hpt(char *h, struct sockaddr *sa, unsigned char *ttl); if (hpt(host, (struct sockaddr *)sin, 0) < 0) { fprintf(stderr, "Usage: [IP]/port\n"); } for (i = 0; i < 2; i++) { sin->sin_port = htons(ntohs(sin->sin_port) + i); sock[i] = OpenSock(sin->sin_addr.s_addr, sin->sin_port, 0); printf("\n"); printf("########### local socket[%d]information#############\n", i); printf(" receive from %s\n", host); getLocal(&recv_local, sock[i]); printf(" sock[%d] fd is %d\n", i, sock[i]); printf("########### local socket[%d] information#############\n", i); printf("\n"); if (sock[i] > nfds) nfds = sock[i]; } return nfds; } int packet_handler(struct timeval now, struct sockaddr_in sin, int len, u_int8* packet) { int x, y, width, height; u_int32 offset; rtp_hdr_t *r = (rtp_hdr_t *)packet; struct jpeghdr *raw = (struct jpeghdr*)(packet+RTP_HDR_SZ); // printf("%8ld.%06ld len=%d from=%s:%u \n", // now.tv_sec, now.tv_usec,len, inet_ntoa(sin.sin_addr), // ntohs(sin.sin_port)); if (len < RTP_HDR_SZ) { printf("RTP header too short (%d bytes for %d CSRCs).\n", len, r->cc); exit(0); } // printf( // "v=%d p=%d x=%d cc=%d m=%d pt=%d seq=%u ts=%lu ssrc=0x%lx\n", // r->version, r->p, r->x, r->cc, r->m,r->pt,ntohs(r->seq), // (unsigned long)ntohl(r->ts),(unsigned long)ntohl(r->ssrc)); // if(r->m == 1) //printf("end of frame \n"); // end_of_frame_processing(packet); offset = ntohl(raw->off); x = 2*raw->x; y = 2*raw->y; width = 8*raw->width; height = 8*raw->height; // printf(" x is %d, y is %d width is %d and height = %d offset is %u \n", // x, y, width, height, offset); #ifdef DEBUG //rintf(" offset in packet is %u\n", raw->off); #endif return ntohs(r->seq); } int main(int argc, char *argv[]) { int c; int now_seq =0; struct sockaddr_in sin; struct timeval start; struct timeval timeout; /* timeout to limit recording */ double laps; double dstart; /* time as double */ double duration = 5; fd_set readfds; extern char *optarg; extern int optind; int i; int nfds = 0; int connected =0; while ((c = getopt(argc, argv, "h")) != EOF) { switch(c) { case 'h': usage(argv[0]); exit(1); break; } } /* if no optional arguments, we are reading from a file */ if (optind < argc) { #ifdef DEBUG printf("%s\n", argv[optind]); #endif nfds = open_network(argv[optind], &sin); #ifdef DEBUG printf("dstart initialized %f\n", dstart); #endif } timeout.tv_usec = 0; timeout.tv_sec = duration; gettimeofday(&start, 0); dstart = tdbl(&start); /* signal handler */ signal(SIGINT, done); signal(SIGTERM, done); signal(SIGHUP, done); #ifdef DEBUG printf("after signals\n"); #endif /* main loop */ printf("before main loop\n"); while (1) { int len; float fraction = 0; u_int8 packet_buf[PACKET_SIZE]; u_int8 rtcp_buf[100]; struct timeval now; double dnow; /* set maximum time to gather packets */ FD_ZERO(&readfds); if (sock[0] >= 0) FD_SET(sock[0], &readfds); if (sock[1] >= 0) FD_SET(sock[1], &readfds); c = select(nfds+1, &readfds, 0, 0, &timeout); if (c < 0) { perror("select"); exit(1); } else if(c > 0) { for (i = 0; i < 2; i++) { if (sock[i] >= 0 && FD_ISSET(sock[i], &readfds)) { int alen = sizeof(sin); /* subtract elapsed time from remaining timeout */ gettimeofday(&now, 0); dnow = tdbl(&now); laps = duration - (dnow - dstart); if (laps < 0) { timeout.tv_sec = 0; timeout.tv_usec = 0; } else timeout = db2tv(laps); len = recvfrom(sock[i], packet_buf, sizeof(packet_buf), 0, (struct sockaddr *)&sin, &alen); if(len > 0) { connected = 1; now_seq = packet_handler(now, sin, len, packet_buf); update_seq(&s, now_seq); } } } } else if (c == 0) { timeout.tv_usec = 0; timeout.tv_sec = duration; gettimeofday(&start, 0); dstart = tdbl(&start); if(connected) { fraction = lost_rate(&s); sprintf(rtcp_buf, "lost rate is %f \n",fraction/256 ); printf("\n%s\n", rtcp_buf); send(sock[1], rtcp_buf, sizeof(rtcp_buf), 0); } else { printf("break if timeout\n"); continue; } } } printf("after while loop\n"); return 0; } /* main */