Exemplo n.º 1
0
void rtp_send(const char *sendfile,
        const char *clientIP, const int clientPort,
        const char *remoteIP, const int remotePort) {
    unsigned char buffer[SEND_LEN];
    unsigned int user_ts = 0;
    int readlen = 0;
    int sendlen = 0;

    rtp_send_init();
    RtpSession *rtpsession = rtp_send_createSession(
            clientIP, clientPort, remoteIP, remotePort);
    assert(rtpsession != NULL);

    assert(sendfile != NULL);
    FILE *fp = fopen(sendfile, "r");
    assert(fp != NULL);

    while ((readlen = fread(buffer,
                    sizeof(unsigned char), SEND_LEN, fp)) > 0) {
        sendlen = rtp_session_send_with_ts(rtpsession,
                buffer, readlen, user_ts);
        printf("read %d bytes, send %d bytes\n", readlen, sendlen);

        user_ts += VIDEO_TIME_STAMP_INC;
    }

    fclose(fp);

    rtp_session_destroy(rtpsession);
    rtp_send_release();
    ortp_global_stats_display();
}
Exemplo n.º 2
0
static void CALLBACK WaveInCallback(HWAVEIN hWaveIn, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2)
{
	WAVEHDR	*wHdr;
  	MMRESULT	mr = NOERROR;
	phmstream_t *stream = (phmstream_t *) dwInstance;
	int					framesize = 320;
	int					enclen;
	char				data_out_enc[1000];
#if USE_CODECS
	phcodec_t		*codec = stream->codec;

	framesize = codec->decoded_framesize;
#endif /* !USE_CODECS */
	switch(uMsg) {
	case WIM_OPEN:
/*	!	fprintf(stderr, "WaveInCallback : WIM_OPEN\n"); */
		break;
	case WIM_CLOSE:
/*	!	fprintf(stderr, "WaveInCallback : WIM_CLOSE\n"); */
		break;
	case WIM_DATA:
		wHdr = (WAVEHDR *)dwParam1;
/* !	fprintf(stderr, "WaveInCallback : WIM_DATA\n"); */
		if (!stream->running)
		  break;

#if USE_CODECS
		enclen = codec->encode(stream->encoder_ctx, wHdr->lpData, wHdr->dwBytesRecorded, data_out_enc, sizeof (data_out_enc));
		rtp_session_send_with_ts(stream->rtp_session, data_out_enc, enclen, stream->timestamp);
		stream->timestamp+=wHdr->dwBytesRecorded / 2;
#else
		rtp_session_send_with_ts(stream->rtp_session, wHdr->lpData, wHdr->dwBytesRecorded, stream->timestamp);
		stream->timestamp+=wHdr->dwBytesRecorded;
#endif /* !USE_CODECS */
		
		mr = waveInAddBuffer(hWaveIn, wHdr, sizeof(*wHdr));

		if (mr != MMSYSERR_NOERROR)
		{
/* !			fprintf(stderr, "__call_free: waveInAddBuffer: 0x%i\n", mr); */
			/* exit(-1); */
		}
		break;
	default:
		break;
	}
}
Exemplo n.º 3
0
int main(int argc, char *argv[])
{
	RtpSession *session;
	unsigned char buffer[160];
	int i;
	FILE *infile;
	char *ssrc;
	uint32_t user_ts=0;
	int tel=0;
	
	if (argc<4){
		printf(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_SENDONLY);
	
	rtp_session_set_scheduling_mode(session,1);
	rtp_session_set_blocking_mode(session,1);
	rtp_session_set_remote_addr(session,argv[2],atoi(argv[3]));
	rtp_session_set_send_payload_type(session,0);
	
	ssrc=getenv("SSRC");
	if (ssrc!=NULL) {
		printf("using SSRC=%i.\n",atoi(ssrc));
		rtp_session_set_ssrc(session,atoi(ssrc));
	}
		
	infile=fopen(argv[1],"rb");
	if (infile==NULL) {
		perror("Cannot open file");
		return -1;
	}
	signal(SIGINT,stophandler);
	while( ((i=fread(buffer,1,160,infile))>0) && (runcond) )
	{
		//ortp_message("Sending packet.");
		rtp_session_send_with_ts(session,buffer,i,user_ts);
		user_ts+=160;
		tel++;
		if (tel==50){
			tel=0;
			ortp_message("Sending telephony event packet.");
			rtp_session_send_dtmf(session,'*',user_ts);
			user_ts+=160+160+160; /* the duration of the dtmf */
		}
	}
	fclose(infile);
	rtp_session_destroy(session);
	ortp_exit();
	ortp_global_stats_display();
	return 0;
}
Exemplo n.º 4
0
void ms_rtp_send_process(MSRtpSend *r)
{
	MSFifo *fi;
	MSQueue *qi;
	MSSync *sync= r->sync;
	int gran=ms_sync_get_samples_per_tick(sync);
	guint32 ts;
	void *s;
	guint skip;
	guint32 synctime=sync->time;
	
	g_return_if_fail(gran>0);
	if (r->rtpsession==NULL) return;

	ms_filter_lock(MS_FILTER(r));
	skip=r->delay!=0;
	if (skip) r->delay--;
	/* process output fifo and output queue*/
	fi=r->f_inputs[0];
	if (fi!=NULL)
	{
		ts=get_new_timestamp(r,synctime);
		/* try to read r->packet_size bytes and send them in a rtp packet*/
		ms_fifo_get_read_ptr(fi,r->packet_size,&s);
		if (!skip){
			rtp_session_send_with_ts(r->rtpsession,s,r->packet_size,ts);
			ms_trace("len=%i, ts=%i ",r->packet_size,ts);
		}
	}
	qi=r->q_inputs[0];
	if (qi!=NULL)
	{
		MSMessage *msg;
		/* read a MSMessage and send it through the network*/
		while ( (msg=ms_queue_get(qi))!=NULL){
			ts=get_new_timestamp(r,synctime);
			if (!skip) {
				/*g_message("Sending packet with ts=%u",ts);*/
				rtp_session_send_with_ts(r->rtpsession,msg->data,msg->size,ts);
				
			}
			ms_message_destroy(msg);
		}
	}
	ms_filter_unlock(MS_FILTER(r));
}
Exemplo n.º 5
0
void
*os_sound_start_out_thread(void *_ca)
{
  jcall_t *ca = (jcall_t*)_ca;
  char data_out[1000];
#ifdef USE_PCM
  char data_out_enc[1000];
#endif
  int timestamp = 0;
  int i;
  int min_size = 160;

  printf("rtp writing thread started\n");
  while (ca->enable_audio != -1)
    {
#ifdef USE_PCM
      memset(data_out, 0, min_size*2);
      i=read(fd, data_out, min_size*2);
#else
      memset(data_out, 0, min_size);
      i=read(fd, data_out, min_size);
#endif
      if (i>0)
        {
	  
#ifdef USE_PCM
	  if (ca->payload==8) /* A-Law */
	    alaw_enc(data_out, data_out_enc, i);
	  if (ca->payload==0) /* Mu-Law */
	    mulaw_enc(data_out, data_out_enc, i);
	  
	  rtp_session_send_with_ts(ca->rtp_session, data_out_enc, i/2,timestamp);
	  timestamp=timestamp+(i/2);
#else
	  rtp_session_send_with_ts(ca->rtp_session, data_out, i,timestamp);
	  timestamp+=i;
#endif
        }
    }
  printf("rtp writing thread stopped\n");
  return NULL;
}
Exemplo n.º 6
0
void rtp_send(RtpSession *session,unsigned char* buffer,int length) {
    int rc;

    if (rtp_initialized == 0) {
       fprintf (stderr, "rtp_listen: ERROR: attempting to send without init !!!!!!");
       return;
    }

    if(rtp_connected)  {
        rc=rtp_session_send_with_ts(session,buffer,length,send_ts);
        if(rc<=0) {
            fprintf(stderr,"rtp_send: ERROR rc=%d\n",rc);
        }
    }
}
Exemplo n.º 7
0
void
*os_sound_start_out_thread(void *_ca)
{
  jcall_t *ca = (jcall_t*)_ca;
  char data_out[10000];
  short sp_data_out_s[640];
  float sp_data_out_f[640];
  int timestamp = 0;
  int i;
  while (ca->enable_audio != -1)
    {
      int k;
      speex_bits_reset(&ca->speex_bits);
      for (k=0; k<ca->speex_nb_packet;k++)
	{
	  int j;
	  i=read(fd, sp_data_out_s, ca->speex_fsize * sizeof(short));
	  if (i>0)
	    {	  
	      for (j=0; j<ca->speex_fsize;j++)
		{ /* convert to float */
		  sp_data_out_f[j] = sp_data_out_s[j];
		}
	      speex_encode(ca->speex_enc, sp_data_out_f, &ca->speex_bits);
	    }
	}    
      speex_bits_insert_terminator(&ca->speex_bits);

      /* convert to char */
      i = speex_bits_write(&ca->speex_bits, data_out, sizeof(data_out));
      
      rtp_session_send_with_ts(ca->rtp_session, data_out, i,timestamp);
      timestamp+=i;
    }
  return NULL;
}
Exemplo n.º 8
0
void * 
ph_audio_out_thread(void *_p)
{
  phmstream_t	*stream = (phmstream_t *) _p;
  WAVEHDR		*wHdr;
  MMRESULT	mr = NOERROR;
  int					pos_whdr=0;
  int					framesize = 320;
  int					enclen;
  char				data_out_enc[1000];
  int					i;

#if USE_CODECS
	phcodec_t		*codec = stream->codec;

	boostPriority(1);

	framesize = codec->decoded_framesize;

	gettimeofday(&stream->last_audio_read, 0);
    mr = waveInStart(stream->hWaveIn);
	if (mr != MMSYSERR_NOERROR)
	{
		fprintf(stderr, "__call_free: waveInStart: 0x%i\n", mr); 
		exit(-1);
    }


#endif /* !USE_CODECS */
  AEC_MUTEX_LOCK(stream);
  while (stream->running)
  {
	   int waitResult;
	   if (pos_whdr < 0 || pos_whdr >= USED_IN_BUFFERS)
			fprintf(stderr, "pos_whdr=%d\n", pos_whdr);


	    wHdr = &stream->waveHdrIn[pos_whdr];
		if (!(wHdr->dwFlags & WHDR_DONE))
		{

			gettimeofday(&stream->last_audio_read, 0);
			AEC_MUTEX_UNLOCK(stream);
			waitResult = WaitForSingleObject(stream->event, 100);
			AEC_MUTEX_LOCK(stream);
			if (waitResult == WAIT_TIMEOUT)
				continue;


		}

		if (!stream->running)
			break;

		if (!(wHdr->dwFlags & WHDR_DONE))
			continue;


#if DO_ECHO_CAN
		do_echo_update(stream, wHdr->lpData, wHdr->dwBytesRecorded);
#endif /* !DO_ECHO_CAN */


		if ((stream->dtmfg_phase != DTMF_IDLE) || (stream->dtmfq_cnt != 0))
			ph_generate_out_dtmf(stream, (short *) wHdr->lpData, wHdr->dwBytesRecorded / 2);

#if USE_CODECS

		enclen = codec->encode(stream->encoder_ctx, wHdr->lpData, wHdr->dwBytesRecorded, data_out_enc, sizeof (data_out_enc));
		if (enclen < 0 || enclen > 1000)
		fprintf(stderr, "enclen=%d\n", enclen);

		rtp_session_send_with_ts(stream->rtp_session, data_out_enc, enclen, stream->timestamp);
	
		stream->timestamp+=wHdr->dwBytesRecorded / 2;
#else
		rtp_session_send_with_ts(stream->rtp_session, wHdr->lpData, wHdr->dwBytesRecorded, stream->timestamp);
		stream->timestamp+=wHdr->dwBytesRecorded;
#endif /* !USE_CODECS */

		wHdr->dwBytesRecorded = 0;
	    mr = waveInAddBuffer(stream->hWaveIn, wHdr, sizeof(*wHdr));
		if (mr != MMSYSERR_NOERROR)
			fprintf(stderr, "__call_free: waveInAddBuffer: 0x%i\n", mr); 

		pos_whdr++;
		if (pos_whdr == USED_IN_BUFFERS) 		
			pos_whdr = 0; /* loop over the prepared blocks */

		switch (mr)
		{
			case MMSYSERR_NOERROR :
				break;
			case MMSYSERR_INVALHANDLE : 
				fprintf(stderr, "waveInAddBuffer : Specified device handle is invalid.\n");
				break;
			case MMSYSERR_NODRIVER : 
				fprintf(stderr, "waveInAdBuffer : No device driver is present.\n");
				break;
			case MMSYSERR_NOMEM : 
				fprintf(stderr, "waveInAddBuffer : Unable to allocate or lock memory.\n");
				break;
			case WAVERR_UNPREPARED : 
				fprintf(stderr, "waveInAddBuffer : The buffer pointed to by the pwh parameter hasn't been prepared.\n");
				break;
			case WAVERR_STILLPLAYING :
				fprintf(stderr, "waveInAddBuffer : still something playing.\n");
			default :
				fprintf(stderr, "waveInAddBuffer error = 0x%x\n", mr);
		}
	}
  AEC_MUTEX_UNLOCK(stream);
  return NULL;
}
Exemplo n.º 9
0
int __cdecl main(int argc, char *argv[])
{
	FILE		*	infile				= NULL;
	SessionSet	*	pSessionSet			= NULL;
	int				nCounter			= 0;
	UINT32			m_nUser_Timestamp	= 0;

	ProductVersion();

	if (GetCommandArguments(argc, argv) != 0)
	{
		printf("==> Sorry dude...\n");
		Sleep(1000);
		return -1;
	}

	printf("==> Starting the RTP Sender test\n");


	// =============== INSTALL THE CONTROL HANDLER ===============
	if (SetConsoleCtrlHandler( (PHANDLER_ROUTINE) ctrlHandlerFunction, TRUE) == 0)
	{
		printf("==> Cannot handle the CTRL-C...\n");
	}


	printf("==> Timestamp increment will be %i\n"	, m_nTimestamp_Inc);
	printf("==> Packet size will be %i\n"			, m_nPacket_Size);

	m_pBuffer = (char *) ortp_malloc(m_nPacket_Size);

	ortp_init();
	ortp_scheduler_init();
	printf("==> Scheduler initialized\n");

	m_SSRC	= getenv("SSRC");
	m_nPort	= atoi(argv[3]);

	for (nCounter=0; nCounter < m_nChannels; nCounter++)
	{
		//printf("==> Channel [#%d]\n", nCounter);

		m_Session[nCounter] = rtp_session_new(RTP_SESSION_SENDONLY);	

		rtp_session_set_scheduling_mode(m_Session[nCounter],1);
		rtp_session_set_blocking_mode(m_Session[nCounter],0);
		rtp_session_set_remote_addr(m_Session[nCounter],argv[2], m_nPort);
		rtp_session_set_send_payload_type(m_Session[nCounter],0);
		
		if (m_SSRC != NULL) 
		{
			rtp_session_set_ssrc(m_Session[nCounter],atoi(m_SSRC));
		}

		m_nPort+=2;
	}

	infile=fopen(argv[1],"rb");

	if (infile==NULL) 
	{
		printf("==> Cannot open file !!!!");
		Sleep(1000);
		return -1;
	}

//	printf("==> Open file\n");
	
	/* Create a set */
	pSessionSet = session_set_new();
//	printf("==> Session set\n");

	while( ((nCounter= (int) fread(m_pBuffer,1,m_nPacket_Size,infile))>0) && (m_bExit == FALSE) )
	{
		int k;
		//g_message("Sending packet.");
		for (k=0;k<m_nChannels;k++){	
			/* add the session to the set */
			session_set_set(pSessionSet,m_Session[k]);
			//printf("==> Session set set %d\n", k);
		}
		/* and then suspend the process by selecting() */
		session_set_select(NULL,pSessionSet,NULL);
		//printf("==> Session set select\n");

		for (k=0;k<m_nChannels;k++)
		{
			//printf("---\n");
			/* this is stupid to do this test, because all session work the same way,
			as the same user_ts is used for all sessions, here. */
			if (session_set_is_set(pSessionSet,m_Session[k]))
			{
				//printf("==> Session set is set %d\n", k);
				rtp_session_send_with_ts(m_Session[k],m_pBuffer,nCounter,m_nUser_Timestamp);
				//g_message("packet sended !");
			}
		}
		m_nUser_Timestamp+=m_nTimestamp_Inc;
	}

	fclose(infile);
	printf("==> Close file\n");



	for(nCounter=0;nCounter<m_nChannels;nCounter++)
	{
		rtp_session_destroy(m_Session[nCounter]);
	}

	session_set_destroy(pSessionSet);

	// Give us some time
	Sleep(250);

	ortp_exit();
	ortp_global_stats_display();

	ortp_free(m_pBuffer);

	printf("==> Remove the CTRL-C handler...\n");
	SetConsoleCtrlHandler( (PHANDLER_ROUTINE) ctrlHandlerFunction, FALSE);

	// Wait for an input key
	printf("Waiting for exit : ");

	for (nCounter = 0; nCounter < 4*5; nCounter++)
	{
		printf(".");
		Sleep(250);
	}

	return 0;
}
Exemplo n.º 10
0
int main(int argc, char *argv[])
{
	RtpSession *session;
	unsigned char buffer[160];
	int i;
	FILE *infile;
	char *ssrc;
	uint32_t user_ts=0;
	int clockslide=0;
	int jitter=0;
	if (argc<4){
		printf("%s", help);
		return -1;
	}
	for(i=4;i<argc;i++){
		if (strcmp(argv[i],"--with-clockslide")==0){
			i++;
			if (i>=argc) {
				printf("%s", help);
				return -1;
			}
			clockslide=atoi(argv[i]);
			ortp_message("Using clockslide of %i milisecond every 50 packets.",clockslide);
		}else if (strcmp(argv[i],"--with-jitter")==0){
			ortp_message("Jitter will be added to outgoing stream.");
			i++;
			if (i>=argc) {
				printf("%s", help);
				return -1;
			}
			jitter=atoi(argv[i]);
		}
	}
	
	ortp_init();
	ortp_scheduler_init();
	ortp_set_log_level_mask(NULL, ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR);
	session=rtp_session_new(RTP_SESSION_SENDONLY);	
	
	rtp_session_set_scheduling_mode(session,1);
	rtp_session_set_blocking_mode(session,1);
	rtp_session_set_connected_mode(session,TRUE);
	rtp_session_set_remote_addr(session,argv[2],atoi(argv[3]));
	rtp_session_set_payload_type(session,0);
	
	ssrc=getenv("SSRC");
	if (ssrc!=NULL) {
		printf("using SSRC=%i.\n",atoi(ssrc));
		rtp_session_set_ssrc(session,atoi(ssrc));
	}
		
	#ifndef _WIN32
	infile=fopen(argv[1],"r");
	#else
	infile=fopen(argv[1],"rb");
	#endif

	if (infile==NULL) {
		perror("Cannot open file");
		return -1;
	}

	signal(SIGINT,stophandler);
	while( ((i=fread(buffer,1,160,infile))>0) && (runcond) )
	{
		rtp_session_send_with_ts(session,buffer,i,user_ts);
		user_ts+=160;
		if (clockslide!=0 && user_ts%(160*50)==0){
			ortp_message("Clock sliding of %i miliseconds now",clockslide);
			rtp_session_make_time_distorsion(session,clockslide);
		}
		/*this will simulate a burst of late packets */
		if (jitter && (user_ts%(8000)==0)) {
			ortp_message("Simulating late packets now (%i milliseconds)",jitter);
			ortp_sleep_ms(jitter);
		}
	}

	fclose(infile);
	rtp_session_destroy(session);
	ortp_exit();
	ortp_global_stats_display();

	return 0;
}