Exemplo n.º 1
0
int main(int argc, char *argv[])
{
	RtpSession *session;
	unsigned char buffer[160];
	int err;
	FILE *outfile;
	uint32_t ts=0;
	int have_more;

	if (argc<3){
		printf("%s",help);
		return -1;
	}

	ortp_init();
	ortp_scheduler_init();
	
	/* set the telephony event payload type to 96 in the av profile.*/
	rtp_profile_set_payload(&av_profile,96,&payload_type_telephone_event);
	
	session=rtp_session_new(RTP_SESSION_RECVONLY);	
	
	rtp_session_set_scheduling_mode(session,1);
	rtp_session_set_blocking_mode(session,1);
	rtp_session_set_local_addr(session,"0.0.0.0",atoi(argv[2]));
	rtp_session_set_payload_type(session,0);
	
	/* register for telephony events */
	rtp_session_signal_connect(session,"telephone-event",(RtpCallback)recv_tev_cb,0);
		
	outfile=fopen(argv[1],"wb");
	if (outfile==NULL) {
		perror("Cannot open file");
		return -1;
	}
	signal(SIGINT,stophandler);
	while(runcond)
	{
		have_more=1;
		while (have_more){
			err=rtp_session_recv_with_ts(session,buffer,160,ts,&have_more);
			if (err>0) {
				size_t ret = fwrite(buffer,1,err,outfile);
				assert( ret == err );
			}
		}
		ts+=160;
		//ortp_message("Receiving packet.");
	}
	fclose(outfile);
	rtp_session_destroy(session);
	ortp_exit();
	ortp_global_stats_display();
	printf("Total dtmf events received: %i\n",dtmf_count);
	return 0;
}
Exemplo n.º 2
0
void *os_sound_start_thread(void *_ca)
{
  jcall_t *ca = (jcall_t*)_ca;
  char data_in[160];
  short sp_data_in_s[640];
  float sp_data_in_f[640];
  int have_more;
  int timestamp = 0;
  int i;

  while (ca->enable_audio != -1)
    {
      memset(data_in, 0, 160);
      /* receive ONE packet */
      i = rtp_session_recv_with_ts(ca->rtp_session, data_in, 160, timestamp, &have_more);

      speex_bits_reset(&ca->dec_speex_bits);
      speex_bits_read_from(&ca->dec_speex_bits, data_in, i);

      while(1)
	{
	  int k;
	  k = speex_decode(ca->speex_dec, &ca->dec_speex_bits, sp_data_in_f);
	  if (k==-2)
	    {
	      exit(0);
	    }
	  else if (k==-1)
	    {
	      break;
	    }
	  else if (k==0)
	    {
	      int j;
	      for (j=0;j<ca->speex_fsize;j++)
		{
		  if (sp_data_in_f[j]>32767)
		    sp_data_in_f[j]=32767;
		  if (sp_data_in_f[j]<-32767)
		    sp_data_in_f[j]=-32767;
		  sp_data_in_s[j] = (short) sp_data_in_f[j];
		}
	      write(fd, sp_data_in_s, ca->speex_fsize);
	    }
	}
      timestamp += 160;
    }
  return NULL;
}
Exemplo n.º 3
0
int rtp2disk(RtpSession *session,guint32 ts, int fd)
{
	char buffer[160];
	int err,havemore=1;
	while (havemore){
		err=rtp_session_recv_with_ts(session,buffer,160,ts,&havemore);
		if (err>0){
			rtp_session_set_data(session,(gpointer)1);
			/* to indicate that (for the application) the stream has started, so we can start
			recording on disk */
		}
		if (session->user_data != NULL)
			write(fd,buffer,err);
	}
	return 0;
}
Exemplo n.º 4
0
char * yy_rtp_recv_amr(int handle ,unsigned char * speex_buffer) {

	RtpSession *session = (RtpSession *) handle;
	int have_more = 1;
	static uint32_t user_ts = 0;
//	static unsigned char *buffer = NULL;
//	if (user_ts == 0) {
//		buffer = (unsigned char *)malloc(13);
//	}
	int stream_received = 0;
	__android_log_print(ANDROID_LOG_INFO,"zhangyanlong", "before rtp_session_recv_with_ts");
	printf("before rtp_session_recv_with_ts \n");
	while(1){

		have_more=1;
		while (have_more){
			int err = rtp_session_recv_with_ts(session, speex_buffer /*buffer*/, SPEEX_FRAME_SIZE, user_ts,
								&have_more);
			printf("have_more = %d ,err = %d  \n"  ,have_more ,err);
			if (err >0) stream_received=1;
			/* this is to avoid to write to disk some silence before the first RTP packet is returned*/
			if ((stream_received) && (err>0)) {
//				size_t ret = fwrite(buffer,1,err,outfile);
//				int ii;
//				for(ii =0 ; ii < 32 ; ii ++)
//					printf("%x " ,buffer[ii]);
//				printf("\n");
//				__android_log_print(ANDROID_LOG_INFO, "zhangyanlong",
//						"%x ,%x ,%x ,%x ,%x", buffer[0], buffer[1], buffer[2],
//						buffer[3], buffer[4]);
//				printf("%x ,%x ,%x ,%x ,%x \n", buffer[0], buffer[1], buffer[2],
//						buffer[3], buffer[4]);
//				printf("stream_recv = %d \n" ,err);
				__android_log_print(ANDROID_LOG_INFO,"zhangyanlong", "stream_recv = %d \n" ,err);
				__android_log_print(ANDROID_LOG_INFO, "zhangyanlong",
						"...................return buffer");
				user_ts+=160;
				return (char* )speex_buffer;
			}
			//printf("...\n");
		}
		user_ts+=160;

	}
		//ortp_message("Receiving packet.");
//	return null;
}
Exemplo n.º 5
0
static void *capture_loop( void *d )
{
	struct rtp_spook_input *conf = (struct rtp_spook_input *)d;
	struct frame *f;
	int status;
    unsigned char buffer[38400];
    int have_more, err, stream_received=0;
    uint32_t ts=0;

    do
    {
		have_more=1;
		while (have_more)
        {
			err=rtp_session_recv_with_ts(conf->session,buffer,sizeof(buffer),ts,&have_more);
			if (err>0) 
			{
                stream_received=1;
			}
			
			if ((stream_received) && (err>0)) 
            {
				if( ( f = get_next_frame( conf->ex, 0 ) ) )
        		{
        			f->length = err;
        			f->format = conf->format;
        			f->width = conf->width;
        			f->height = conf->height;
        			f->key = 1;

        			memcpy( f->d, buffer, err);

        			deliver_frame( conf->ex, f );
					printf("\r\nDeliver packet with length %d", err);
        		} 
                else
        		{
        			spook_log( SL_WARN, "video: dropping frame" );
        		}
			}
		}
		ts+=160;
		//ortp_message("Receiving packet.");
	}while(1);

	return NULL;
}
Exemplo n.º 6
0
int rtp_receive (RtpSession *session, unsigned char *buffer,int length) {
    int rc = -1;
    
    if (rtp_initialized == 0) {
       fprintf (stderr, "rtp_receive: ERROR: attempting to receive without init !!!!!!");
       return rc;
    }

    if (rtp_listening == 0) {
       return rc;
    }

    if (rtp_connected){
	rc=rtp_session_recv_with_ts(session,buffer,length,recv_ts,&rtp_receive_has_more);
    }

    return rc;
}
Exemplo n.º 7
0
int rtp2disk(RtpSession *session,uint32_t ts, int fd)
{
	int err,havemore=1;
	while (havemore){
		err=rtp_session_recv_with_ts(session,recvbuf,160,ts,&havemore);
		if (havemore) printf("warning: havemore=%i!\n",havemore);
		if (err>0){
			rtp_session_set_data(session,(void*)1);
			/* to indicate that (for the application) the stream has started, so we can start
			recording on disk */
		}
		if (session->user_data != NULL) {
			size_t ret = write(fd,recvbuf,err);
			assert( ret == err );
		}
	}
	return 0;
}
Exemplo n.º 8
0
int main(int argc, char*argv[])
{
	RtpSession *session;
	unsigned char buffer[32];
	int err;
	uint32_t ts=0;
	int stream_received=0;
	FILE *outfile;
	int local_port;
	int have_more;
	int i;
	int format=0;
	int soundcard=0;
	int sound_fd=0;
	int jittcomp=40;
	bool_t adapt=TRUE;
	
	/* init the lib */
	if (argc<3){
		printf("%s",help);
		return -1;
	}
	local_port=atoi(argv[2]);
	if (local_port<=0) {
		printf("%s",help);
		return -1;
	}
	for (i=3;i<argc;i++)
	{
		if (strcmp(argv[i],"--noadapt")==0) adapt=FALSE;
		if (strcmp(argv[i],"--format")==0){
			i++;
			if (i<argc){
				if (strcmp(argv[i],"mulaw")==0){
					format=MULAW;
				}else
				if (strcmp(argv[i],"alaw")==0){
					format=ALAW;
				}else{
					printf("Unsupported format %s\n",argv[i]);
					return -1;
				}
			}
		}
		else if (strcmp(argv[i],"--soundcard")==0){
			soundcard=1;
		}
		else if (strcmp(argv[i],"--with-jitter")==0){
			i++;
			if (i<argc){
				jittcomp=atoi(argv[i]);
				printf("Using a jitter buffer of %i milliseconds.\n",jittcomp);
			}
		}
	}
	
	outfile=fopen(argv[1],"wb");
	if (outfile==NULL) {
		perror("Cannot open file for writing");
		return -1;
	}
	
	
	if (soundcard){
		sound_fd=sound_init(format);
	}
	
	ortp_init();
	ortp_scheduler_init();
	ortp_set_log_level_mask(ORTP_DEBUG|ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR);
	signal(SIGINT,stop_handler);
	session=rtp_session_new(RTP_SESSION_RECVONLY);	
	rtp_session_set_scheduling_mode(session,1);
	rtp_session_set_blocking_mode(session,1);
	rtp_session_set_local_addr(session,"0.0.0.0",atoi(argv[2]));
	rtp_session_set_connected_mode(session,TRUE);
	rtp_session_set_symmetric_rtp(session,TRUE);
	rtp_session_enable_adaptive_jitter_compensation(session,adapt);
	rtp_session_set_jitter_compensation(session,jittcomp);
	rtp_session_set_payload_type(session,0);
	rtp_session_signal_connect(session,"ssrc_changed",(RtpCallback)ssrc_cb,0);
	rtp_session_signal_connect(session,"ssrc_changed",(RtpCallback)rtp_session_reset,0);
	

	fwrite("#!AMR\n",1,6,outfile);
//	write(sound_fd,"#!AMR\n",err);
	while(cond)
	{
		have_more=1;
		while (have_more){
			err=rtp_session_recv_with_ts(session,buffer,32,ts,&have_more);
			if (err>0) stream_received=1;
			/* this is to avoid to write to disk some silence before the first RTP packet is returned*/	
			if ((stream_received) && (err>0)) {
				size_t ret = fwrite(buffer,1,err,outfile);
				int ii;
				for(ii =0 ; ii < 32 ; ii ++)
					printf("%x " ,buffer[ii]);
				printf("\n");
//				while(1);
				if (sound_fd>0){
					ret = write(sound_fd,buffer,err);
					if (ret==-1){
						fprintf(stderr,"write to sound card failed (%s)",strerror(errno));
					}
				}
			}
		}
		ts+=160;
		//ortp_message("Receiving packet.");
	}
	
	rtp_session_destroy(session);
	ortp_exit();
	
	ortp_global_stats_display();
	
	return 0;
}
Exemplo n.º 9
0
qint64 Audio_playback::readData(char *data, qint64 maxlen)
{
    qint64 bytes_read;
    qint16 v;
    qint64 bytes_to_read = maxlen > 2000 ? 2000: maxlen;
    //qint64 bytes_to_read = maxlen;
    int has_more;

    if (useRTP && rtp_connected) {
        int i;
        short v;
        int length;

        uint8_t* buffer;

        // aLaw encoded from RTP, but readData is reading in bytes (each sample 2 bytes for PCM)
        buffer = (uint8_t*)malloc(bytes_to_read/2);

        length=rtp_session_recv_with_ts(rtpSession,buffer,bytes_to_read/2,recv_ts,&has_more);
        if (length>0) {
            recv_ts += length;
            if (audio_encoding == 0) {
                #pragma omp parallel for schedule(static) ordered
                for(i=0; i<length; i++) {
                    v=g711a.decode(buffer[i]);
                    #pragma omp ordered
                    pdecoded_buffer->enqueue(v);
                }
            } else {
                qDebug() << "Audio: rtp_audio only support aLaw";
            }
        }
        free(buffer);

        if (length <= 0) {                 // probably not connected or late arrival.  Send silence.
            recv_ts+=bytes_to_read/2;
            memset(data, 0, bytes_to_read);
            bytes_read = bytes_to_read;
            return bytes_read;
        }

    }

    // note both TCP and RTP audio enqueue PCM data in decoded_buffer
    bytes_read = 0;

    if (pdecoded_buffer->isEmpty()) {
        // probably not connected or late arrival of packets.  Send silence.
        memset(data, 0, bytes_to_read);
        bytes_read = bytes_to_read;
    } else {
        while ((!pdecoded_buffer->isEmpty()) && (bytes_read < bytes_to_read)) {
            v = pdecoded_buffer->dequeue();
            switch(audio_byte_order) {
            case QAudioFormat::LittleEndian:
                data[bytes_read++]=(char)(v&0xFF);
                data[bytes_read++]=(char)((v>>8)&0xFF);
                break;
            case QAudioFormat::BigEndian:
                data[bytes_read++]=(char)((v>>8)&0xFF);
                data[bytes_read++]=(char)(v&0xFF);
                break;
            }
        }
        while (bytes_read < bytes_to_read) data[bytes_read++] = 0;
    }

    return bytes_read;
}
Exemplo n.º 10
0
int main(int argc, char*argv[])
{
	RtpSession *session;
#ifndef SBUS
	unsigned char buffer[160];
#else
	unsigned char buffer[SBUS_FRAME_SIZE];
#endif
	int err;
	uint32_t ts=0;
	int stream_received=0;
	FILE *outfile;
	int local_port;
	int have_more;
	int i;
	int format=0;
	int soundcard=0;
	int sound_fd=0;
	int jittcomp=40;
	bool_t adapt=TRUE;
#ifdef SBUS
	RtpProfile prof;
#endif
	
	/* init the lib */
	if (argc<3){
		printf("%s",help);
		return -1;
	}
	local_port=atoi(argv[2]);
	if (local_port<=0) {
		printf("%s",help);
		return -1;
	}
	for (i=3;i<argc;i++)
	{
		if (strcmp(argv[i],"--noadapt")==0) adapt=FALSE;
		if (strcmp(argv[i],"--format")==0){
			i++;
			if (i<argc){
				if (strcmp(argv[i],"mulaw")==0){
					format=MULAW;
				}else
				if (strcmp(argv[i],"alaw")==0){
					format=ALAW;
				}else{
					printf("Unsupported format %s\n",argv[i]);
					return -1;
				}
			}
		}
		else if (strcmp(argv[i],"--soundcard")==0){
			soundcard=1;
		}
		else if (strcmp(argv[i],"--with-jitter")==0){
			i++;
			if (i<argc){
				jittcomp=atoi(argv[i]);
				printf("Using a jitter buffer of %i milliseconds.\n",jittcomp);
			}
		}
	}
	
	outfile=fopen(argv[1],"wb");
	if (outfile==NULL) {
		perror("Cannot open file for writing");
		return -1;
	}
#ifdef SBUS
	setvbuf(outfile, NULL, _IONBF, 0);
#endif
	
	
	if (soundcard){
		sound_fd=sound_init(format);
	}
	
	ortp_init();
	ortp_scheduler_init();
	ortp_set_log_level_mask(ORTP_DEBUG|ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR);
	signal(SIGINT,stop_handler);
	session=rtp_session_new(RTP_SESSION_RECVONLY);	
	rtp_session_set_scheduling_mode(session,1);
	rtp_session_set_blocking_mode(session,1);
	rtp_session_set_local_addr(session,"0.0.0.0",atoi(argv[2]),-1);
	rtp_session_set_connected_mode(session,TRUE);
	rtp_session_set_symmetric_rtp(session,TRUE);
	rtp_session_enable_adaptive_jitter_compensation(session,adapt);
	rtp_session_set_jitter_compensation(session,jittcomp);
#ifndef SBUS
	rtp_session_set_payload_type(session,0);
#else
	rtp_profile_clear_all(&prof);
	//rtp_profile_set_name(&prof, "SBUS");
	rtp_profile_set_payload(&prof, 71, &payload_type_sbus);
	rtp_session_set_profile(session, &prof);
	rtp_session_set_payload_type(session, 71);
#endif
	rtp_session_signal_connect(session,"ssrc_changed",(RtpCallback)ssrc_cb,0);
	rtp_session_signal_connect(session,"ssrc_changed",(RtpCallback)rtp_session_reset,0);

	while(cond)
	{
		have_more=1;
		while (have_more){
#ifndef SBUS
			err=rtp_session_recv_with_ts(session,buffer,160,ts,&have_more);
#else
			err=rtp_session_recv_with_ts(session,buffer,SBUS_FRAME_SIZE,ts,&have_more);
#endif
			if (err>0) stream_received=1;
			/* this is to avoid to write to disk some silence before the first RTP packet is returned*/	
			if ((stream_received) && (err>0)) {
#ifdef SBUS
				clock_gettime(CLOCK_REALTIME, &tsnew);
				printf("%09ld\n", tsnew.tv_nsec);
#endif
				size_t ret = fwrite(buffer,1,err,outfile);
				if (sound_fd>0){
					ret = write(sound_fd,buffer,err);
					if (ret==-1){
						fprintf(stderr,"write to sound card failed (%s)",strerror(errno));
					}
				}
			}
		}
#ifndef SBUS
		ts+=160;
#else
		ts+=70;
#endif
		//ortp_message("Receiving packet.");
	}
	
	rtp_session_destroy(session);
	ortp_exit();
	
	ortp_global_stats_display();
	
	return 0;
}
Exemplo n.º 11
0
void
*os_sound_start_thread(void *_ca)
{
  jcall_t *ca = (jcall_t*)_ca;
  char data_in[512];
#ifdef USE_PCM
  char data_in_dec[1024];
#endif
  int have_more = 0;
  int timestamp = 0;
  int i;
  mblk_t *mp;
  struct timeval pkt_in_time, pkt_out_time, sleeptime;
  struct timespec sleepns;

  printf("rtp reading thread started\n");
  while (ca->enable_audio != -1)
    {
#if !MINIMIZE_COPYING
      do
	{
	  memset(data_in, 0, 160);
	  i = rtp_session_recv_with_ts(ca->rtp_session, data_in, 160, timestamp, &have_more);
	  if (i>0) {

#ifdef USE_PCM
	    if (ca->payload==8) /* A-Law */
	      alaw_dec(data_in, data_in_dec, 160);
	    if (ca->payload==0) /* Mu-Law */
	      mulaw_dec(data_in, data_in_dec, 160);

	    write(fd, data_in_dec, i*2);
#else
	    write(fd, data_in, i);
#endif
	  }
	} while(have_more);
#else /* MINIMIZE_COPYING */


      if ( (mp=rtp_session_recvm_with_ts(ca->rtp_session,timestamp))!=NULL) 
	{
	  int 	len=mp->b_cont->b_wptr-mp->b_cont->b_rptr;
	  
	  gettimeofday(&pkt_in_time, 0);
	  if (ca->enable_audio != -1)
	    {
#ifdef USE_PCM
	      if (ca->payload==8) /* A-Law */
		alaw_dec(mp->b_cont->b_rptr, data_in_dec, len);
	    
	      if (ca->payload==0) /* Mu-Law */
		mulaw_dec(mp->b_cont->b_rptr, data_in_dec, len);

	      write(fd, data_in_dec, len*2);
#else
	      write(fd, mp->b_cont->b_rptr, len);
#endif
	    }
	  if (ca->enable_audio == -1)
	    dbgbpt1();
	  freemsg(mp);
	  gettimeofday(&pkt_out_time, 0);
	  /* compute the time we spent processing the packet */
	  tvsub(&pkt_out_time, &pkt_in_time);
	}
#endif

      sleeptime.tv_usec = 10000; sleeptime.tv_sec = 0;
      tvsub(&sleeptime, &pkt_out_time);
      TIMEVAL_TO_TIMESPEC(&sleeptime, &sleepns);
#if !BLOCKING_MODE
      if (ca->enable_audio != -1)
	nanosleep(&sleepns, 0);
#endif
      timestamp += 160;
    }

  printf("rtp reading thread stopping\n");
  dbgbpt2();
  return NULL;
}