示例#1
0
void receiver_test() {

	krad_rebuilder_t *krad_rebuilder;
	int port;
	int sd;
	int ret;
	int slen;
	unsigned char *buffer;
	unsigned char *packet_buffer;
	struct sockaddr_in local_address;
	struct sockaddr_in remote_address;
	
	slen = sizeof (remote_address);
	
	buffer = calloc (1, 2000);
	packet_buffer = calloc (1, 200000);
	sd = socket (AF_INET, SOCK_DGRAM, 0);
	port = TEST_PORT;
	krad_rebuilder = krad_rebuilder_create ();

	memset((char *) &local_address, 0, sizeof(local_address));
	local_address.sin_family = AF_INET;
	local_address.sin_port = htons (port);
	local_address.sin_addr.s_addr = htonl(INADDR_ANY);

	if (bind (sd, (struct sockaddr *)&local_address, sizeof(local_address)) == -1 ) {
		printf("bind error\n");
		exit(1);
	}

	while (1) {
	
		ret = recvfrom(sd, buffer, 2000, 0, (struct sockaddr *)&remote_address, (socklen_t *)&slen);
		
		if (ret == -1) {
			printf("failed recvin udp\n");
			exit(1);
		}
		
		//printf("Received packet from %s:%d\n", 
		//		inet_ntoa(remote_address.sin_addr), ntohs(remote_address.sin_port));


		krad_rebuilder_write (krad_rebuilder, buffer, ret);

		ret = krad_rebuilder_read_packet (krad_rebuilder, packet_buffer, 1);
		if (ret != 0) {
			printf("read a packet with %d bytes: -%s-\n", ret, packet_buffer);
		}

	}

	krad_rebuilder_destroy (krad_rebuilder);
	close (sd);
	free (packet_buffer);
	free (buffer);
}
示例#2
0
void kr_udp_recvr (kr_udp_recvr_t *udp_recvr, int port) {

	krad_rebuilder_t *krad_rebuilder;
	int sd;
	int keyframe;
	int started;
	int ret;
	int slen;
	unsigned char *buffer;
	unsigned char *packet_buffer;
	struct sockaddr_in local_address;
	struct sockaddr_in remote_address;
  struct SwsContext *scaler;
  struct pollfd pollfds[4];

	scaler = NULL;
	started = 0;
	slen = sizeof (remote_address);
	
	buffer = calloc (1, 45000);
	packet_buffer = calloc (1, 2300000);
	sd = socket (AF_INET, SOCK_DGRAM, 0);
	krad_rebuilder = krad_rebuilder_create ();

	memset((char *) &local_address, 0, sizeof(local_address));
	local_address.sin_family = AF_INET;
	local_address.sin_port = htons (port);
	local_address.sin_addr.s_addr = htonl(INADDR_ANY);

	if (bind (sd, (struct sockaddr *)&local_address, sizeof(local_address)) == -1 ) {
		printf("bind error\n");
		exit(1);
	}
	
  kr_udp_recvr_alloc_framebuf (udp_recvr);
	
  udp_recvr->vpxdec = krad_vpx_decoder_create ();

	while (1) {
	
	  if (destroy == 1) {
		  printf ("Got signal!\n");
	    break;
	  }
	  
    pollfds[0].fd = kr_wayland_get_fd(udp_recvr->wayland);
    pollfds[0].events = POLLIN;

    pollfds[1].fd = sd;
    pollfds[1].events = POLLIN;
	  
    ret = poll (pollfds, 2, 1);
	  
	  if (ret < 0) {
	    break;
	  }
	  
    if (pollfds[0].revents == POLLIN) { 
      kr_wayland_process(udp_recvr->wayland);
    }
	
    if (pollfds[1].revents == POLLIN) {
		  ret = recvfrom (sd, buffer, 2000, 0, (struct sockaddr *)&remote_address, (socklen_t *)&slen);
		
		  if (ret == -1) {
			  printf("failed recvin udp\n");
			  exit (1);
		  }
		
      //printf ("Received packet from %s:%d\n", 
    	//	inet_ntoa(remote_address.sin_addr), ntohs(remote_address.sin_port));


		  krad_rebuilder_write (krad_rebuilder, buffer, ret);
		  ret = krad_rebuilder_read_packet (krad_rebuilder, packet_buffer, 1, &keyframe);
    
      if (ret != 0) {

		    //printf ("read a packet with %d bytes key: %d     \n", ret, keyframe);

        if ((started == 1) || ((started == 0) && (keyframe == 1))) {
		      started = 1;
			  } else {
			    continue;
			  }
			
        krad_vpx_decoder_decode (udp_recvr->vpxdec, packet_buffer, ret);
        
        while (udp_recvr->vpxdec->img != NULL) {
       
            int rgb_stride_arr[3] = {4*udp_recvr->width, 0, 0};
            uint8_t *dst[4];
            
            scaler = sws_getCachedContext ( scaler,
                                            udp_recvr->vpxdec->width,
                                            udp_recvr->vpxdec->height,
                                            PIX_FMT_YUV420P,
                                            udp_recvr->width,
                                            udp_recvr->height,
                                            PIX_FMT_RGB32, 
                                            SWS_BICUBIC,
                                            NULL, NULL, NULL);


          int pos = ((udp_recvr->frames_dec + 1) % udp_recvr->framebufsize) * udp_recvr->frame_size;
          dst[0] = (unsigned char *)udp_recvr->rgba + pos;

          sws_scale (scaler,
                     (const uint8_t * const*)udp_recvr->vpxdec->img->planes,
                      udp_recvr->vpxdec->img->stride,
                     0, udp_recvr->vpxdec->height,
                     dst, rgb_stride_arr);


          udp_recvr->frames_dec++;
            //printf ("Received frame!\n");
          krad_vpx_decoder_decode_again (udp_recvr->vpxdec);
            
		    }
	    }
	  }
	}

  krad_vpx_decoder_destroy (&udp_recvr->vpxdec);

  kr_udp_recvr_free_framebuf (udp_recvr);

	krad_rebuilder_destroy (krad_rebuilder);
	close (sd);
  sws_freeContext ( scaler );
	free (packet_buffer);
	free (buffer);
}