char *ortp_strndup(const char *str,int n){ int min=MIN((int)strlen(str),n)+1; char *ret=(char*)ortp_malloc(min); strncpy(ret,str,n); ret[min-1]='\0'; return ret; }
static char * _strdup_vprintf(const char *fmt, va_list ap) { /* Guess we need no more than 100 bytes. */ int n, size = 200; char *p,*np; //va_list ap; if ((p = (char *) ortp_malloc (size)) == NULL) return NULL; while (1) { /* Try to print in the allocated space. */ //va_start(ap, fmt); n = vsnprintf (p, size, fmt, ap); //va_end(ap); /* If that worked, return the string. */ if (n > -1 && n < size) return p; //printf("Reallocing space.\n"); /* Else try again with more space. */ if (n > -1) /* glibc 2.1 */ size = n + 1; /* precisely what is needed */ else /* glibc 2.0 */ size *= 2; /* twice the old size */ if ((np = (char *) ortp_realloc (p, size)) == NULL) { free(p); return NULL; } else { p = np; } } }
RtpScheduler * rtp_scheduler_new() { RtpScheduler *sched=(RtpScheduler *) ortp_malloc(sizeof(RtpScheduler)); memset(sched,0,sizeof(RtpScheduler)); rtp_scheduler_init(sched); return sched; }
char *payload_type_get_rtpmap(PayloadType *pt) { int len=(int)strlen(pt->mime_type)+15; char *rtpmap=(char *) ortp_malloc(len); if (pt->channels>0) snprintf(rtpmap,len,"%s/%i/%i",pt->mime_type,pt->clock_rate,pt->channels); else snprintf(rtpmap,len,"%s/%i",pt->mime_type,pt->clock_rate); return rtpmap; }
dblk_t *datab_alloc(int size){ dblk_t *db; int total_size=sizeof(dblk_t)+size; db=(dblk_t *) ortp_malloc(total_size); db->db_base=(uint8_t*)db+sizeof(dblk_t); db->db_lim=db->db_base+size; db->db_ref=1; db->db_freefn=NULL; /* the buffer pointed by db_base must never be freed !*/ return db; }
char * ortp_strdup(const char *tmp){ size_t sz; char *ret; if (tmp==NULL) return NULL; sz=strlen(tmp)+1; ret=(char*)ortp_malloc(sz); strcpy(ret,tmp); ret[sz-1]='\0'; return ret; }
mblk_t *esballoc(uint8_t *buf, int size, int pri, void (*freefn)(void*) ) { mblk_t *mp; dblk_t *datab; mp=(mblk_t *) ortp_malloc(sizeof(mblk_t)); mblk_init(mp); datab=(dblk_t *) ortp_malloc(sizeof(dblk_t)); datab->db_base=buf; datab->db_lim=buf+size; datab->db_ref=1; datab->db_freefn=freefn; mp->b_datap=datab; mp->b_rptr=mp->b_wptr=buf; mp->b_next=mp->b_prev=mp->b_cont=NULL; return mp; }
mblk_t *dupb(mblk_t *mp) { mblk_t *newm; return_val_if_fail(mp->b_datap!=NULL,NULL); return_val_if_fail(mp->b_datap->db_base!=NULL,NULL); datab_ref(mp->b_datap); newm=(mblk_t *) ortp_malloc(sizeof(mblk_t)); mblk_init(newm); newm->b_datap=mp->b_datap; newm->b_rptr=mp->b_rptr; newm->b_wptr=mp->b_wptr; return newm; }
mblk_t *allocb(int size, int pri) { mblk_t *mp; dblk_t *datab; mp=(mblk_t *) ortp_malloc(sizeof(mblk_t)); mblk_init(mp); datab=datab_alloc(size); mp->b_datap=datab; mp->b_rptr=mp->b_wptr=datab->db_base; mp->b_next=mp->b_prev=mp->b_cont=NULL; return mp; }
static double powerspectrum_stat_beyond8K(struct Channel *chan){ spx_int32_t ps_size = 0; spx_int32_t *ps = NULL; double mystat = 0; float fftmul = 1.0 / (32768.0); int i; speex_preprocess_ctl(chan->speex_pp, SPEEX_PREPROCESS_GET_PSD_SIZE, &ps_size); ps = (spx_int32_t*)ortp_malloc(sizeof(spx_int32_t)*ps_size); speex_preprocess_ctl(chan->speex_pp, SPEEX_PREPROCESS_GET_PSD, ps); mystat = 0; for (i=ps_size/2;i < ps_size; i++) { double yp; #if defined(__cplusplus) yp = sqrtf(sqrtf(static_cast<float>(ps[i]))) - 1.0f; #else yp = sqrtf(sqrtf((float)(ps[i]))) - 1.0f; #endif yp = yp * fftmul; yp = MIN(yp * 3000.0, 1.0); yp = (1 - yp) * (100 - 1.0f); mystat = yp + mystat; } mystat = (mystat*2)/ps_size; ortp_free(ps); /* values: Maximum: 108,064 low volume on high frequency. Decrease when volume increase. */ /* return value between 0 and 108,064? */ mystat = 108.064-mystat; chan->average_psd=(mystat*mystat*coef) + (1.0-coef)*chan->average_psd; //ms_message("average power spectrum on half highest values ONLY: stat=%.3lf", chan->average_psd); return mystat; }
char * ortp_strdup_vprintf(const char *fmt, va_list ap) { /* Guess we need no more than 100 bytes. */ int n, size = 200; char *p,*np; #ifndef WIN32 va_list cap;/*copy of our argument list: a va_list cannot be re-used (SIGSEGV on linux 64 bits)*/ #endif if ((p = (char *) ortp_malloc (size)) == NULL) return NULL; while (1) { /* Try to print in the allocated space. */ #ifndef WIN32 va_copy(cap,ap); n = vsnprintf (p, size, fmt, cap); va_end(cap); #else /*this works on 32 bits, luckily*/ n = vsnprintf (p, size, fmt, ap); #endif /* If that worked, return the string. */ if (n > -1 && n < size) return p; //printf("Reallocing space.\n"); /* Else try again with more space. */ if (n > -1) /* glibc 2.1 */ size = n + 1; /* precisely what is needed */ else /* glibc 2.0 */ size *= 2; /* twice the old size */ if ((np = (char *) ortp_realloc (p, size)) == NULL) { free(p); return NULL; } else { p = np; } } }
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; }
void * ortp_malloc0(size_t size){ void *ptr=ortp_malloc(size); memset(ptr,0,size); return ptr; }
int init_bench(struct bench_config *bench) { PayloadType *pt; int pos; int val; int count; bench->ticker=ms_ticker_new(); count = 0; /* creates the couple of encoder/decoder */ pt=rtp_profile_get_payload(&av_profile,bench->payload); if (pt==NULL){ ms_error("audiostream.c: undefined payload type."); return count; } if (pt->clock_rate!=8000 && pt->clock_rate!=16000 && pt->clock_rate!=32000){ ms_error("audiostream.c: wrong rate."); return count; } for (pos=0;pos<bench->num_session;pos++) { struct test_session *ts = (struct test_session *)ortp_malloc(sizeof(struct test_session)); memset(ts, 0, sizeof(struct test_session)); ts->rtps = create_duplex_rtpsession(bench->port_origin+pos*2); if (ts->rtps==NULL) { ms_error("bench.c: cannot create rtp_session!"); ortp_free(ts); return count; } rtp_session_set_payload_type(ts->rtps,bench->payload); rtp_session_set_remote_addr_full(ts->rtps, bench->ip_destination, bench->port_destination+pos*2, bench->ip_destination, bench->port_destination+1+pos*2); ts->fplayer = ms_filter_new(MS_FILE_PLAYER_ID); if (strstr(bench->wavfile, ".au")==NULL) ts->encoder = ms_filter_create_encoder(pt->mime_type); ts->rtpsend = ms_filter_new(MS_RTP_SEND_ID); ts->rtprecv = ms_filter_new(MS_RTP_RECV_ID); ts->decoder = ms_filter_create_decoder(pt->mime_type); ts->frecorder = ms_filter_new(MS_FILE_REC_ID); if ((ts->encoder==NULL && strstr(bench->wavfile, ".au")==NULL) || (ts->decoder==NULL )){ ms_error("bench.c: No decoder available for payload %i.",bench->payload); if (ts->fplayer) ms_filter_destroy(ts->fplayer); if (ts->encoder) ms_filter_destroy(ts->encoder); if (ts->rtpsend) ms_filter_destroy(ts->rtpsend); if (ts->rtprecv) ms_filter_destroy(ts->rtprecv); if (ts->decoder) ms_filter_destroy(ts->decoder); if (ts->frecorder) ms_filter_destroy(ts->frecorder); ortp_free(ts); return count; } if (ts->fplayer==NULL){ ms_error("bench.c: missing player filter."); if (ts->fplayer) ms_filter_destroy(ts->fplayer); if (ts->encoder) ms_filter_destroy(ts->encoder); if (ts->rtpsend) ms_filter_destroy(ts->rtpsend); if (ts->rtprecv) ms_filter_destroy(ts->rtprecv); if (ts->decoder) ms_filter_destroy(ts->decoder); if (ts->frecorder) ms_filter_destroy(ts->frecorder); ortp_free(ts); return count; } if (ts->frecorder==NULL){ ms_error("bench.c: missing recorder filter."); if (ts->fplayer) ms_filter_destroy(ts->fplayer); if (ts->encoder) ms_filter_destroy(ts->encoder); if (ts->rtpsend) ms_filter_destroy(ts->rtpsend); if (ts->rtprecv) ms_filter_destroy(ts->rtprecv); if (ts->decoder) ms_filter_destroy(ts->decoder); if (ts->frecorder) ms_filter_destroy(ts->frecorder); ortp_free(ts); return count; } if (ts->rtpsend==NULL){ ms_error("bench.c: missing rtpsend filter."); if (ts->fplayer) ms_filter_destroy(ts->fplayer); if (ts->encoder) ms_filter_destroy(ts->encoder); if (ts->rtpsend) ms_filter_destroy(ts->rtpsend); if (ts->rtprecv) ms_filter_destroy(ts->rtprecv); if (ts->decoder) ms_filter_destroy(ts->decoder); if (ts->frecorder) ms_filter_destroy(ts->frecorder); ortp_free(ts); return count; } if (ts->rtprecv==NULL){ ms_error("bench.c: missing rtprecv filter."); if (ts->fplayer) ms_filter_destroy(ts->fplayer); if (ts->encoder) ms_filter_destroy(ts->encoder); if (ts->rtpsend) ms_filter_destroy(ts->rtpsend); if (ts->rtprecv) ms_filter_destroy(ts->rtprecv); if (ts->decoder) ms_filter_destroy(ts->decoder); if (ts->frecorder) ms_filter_destroy(ts->frecorder); ortp_free(ts); return count; } ms_filter_call_method(ts->rtpsend,MS_RTP_SEND_SET_SESSION,ts->rtps); ms_filter_call_method(ts->rtprecv,MS_RTP_RECV_SET_SESSION,ts->rtps); ms_filter_call_method (ts->rtprecv, MS_FILTER_SET_SAMPLE_RATE, &pt->clock_rate); ms_filter_call_method (ts->frecorder, MS_FILTER_SET_SAMPLE_RATE, &pt->clock_rate); val = ms_filter_call_method(ts->fplayer,MS_FILE_PLAYER_OPEN,(void*)bench->wavfile); if (val!=0) { ms_error("bench.c: Cannot open wav file (%s)", bench->wavfile); if (ts->fplayer) ms_filter_destroy(ts->fplayer); if (ts->encoder) ms_filter_destroy(ts->encoder); if (ts->rtpsend) ms_filter_destroy(ts->rtpsend); if (ts->rtprecv) ms_filter_destroy(ts->rtprecv); if (ts->decoder) ms_filter_destroy(ts->decoder); if (ts->frecorder) ms_filter_destroy(ts->frecorder); ortp_free(ts); return count; } val=0; ms_filter_call_method (ts->fplayer, MS_FILTER_GET_SAMPLE_RATE, &val); if (val!=pt->clock_rate) { ms_error("bench.c: unsupported rate for wav file: codec=%i / file=%i", pt->clock_rate, val); if (ts->fplayer) ms_filter_destroy(ts->fplayer); if (ts->encoder) ms_filter_destroy(ts->encoder); if (ts->rtpsend) ms_filter_destroy(ts->rtpsend); if (ts->rtprecv) ms_filter_destroy(ts->rtprecv); if (ts->decoder) ms_filter_destroy(ts->decoder); if (ts->frecorder) ms_filter_destroy(ts->frecorder); ortp_free(ts); return count; } ms_filter_call_method (ts->fplayer, MS_FILTER_GET_NCHANNELS, &val); if (val!=1) { ms_error("bench.c: unsupported number of channel for wav file: codec=1 / file=%i", val); if (ts->fplayer) ms_filter_destroy(ts->fplayer); if (ts->encoder) ms_filter_destroy(ts->encoder); if (ts->rtpsend) ms_filter_destroy(ts->rtpsend); if (ts->rtprecv) ms_filter_destroy(ts->rtprecv); if (ts->decoder) ms_filter_destroy(ts->decoder); if (ts->frecorder) ms_filter_destroy(ts->frecorder); ortp_free(ts); return count; } ms_filter_call_method_noarg(ts->fplayer,MS_FILE_PLAYER_START); if (strstr(bench->wavfile, ".au")==NULL) { ms_filter_link(ts->fplayer,0,ts->encoder,0); ms_filter_link(ts->encoder,0,ts->rtpsend,0); } else { ms_filter_link(ts->fplayer,0,ts->rtpsend,0); } ms_filter_link(ts->rtprecv,0,ts->decoder,0); ms_filter_link(ts->decoder,0,ts->frecorder,0); ms_ticker_attach(bench->ticker,ts->fplayer); ms_ticker_attach(bench->ticker,ts->rtprecv); if (pos < bench->num_session_record) { char rec_file[128]; snprintf(rec_file, sizeof(rec_file), "rec_%s_%i.wav", bench->ip_destination, bench->port_destination+pos*2); ms_filter_call_method(ts->frecorder,MS_FILE_REC_OPEN,(void*)rec_file); ms_filter_call_method_noarg(ts->frecorder,MS_FILE_REC_START); } bench->tsessions = ms_list_append(bench->tsessions, (void*)ts); count++; } return count; }
int main(int argc, char *argv[]) { RtpSession *session[STREAMS_COUNT]; int i; int filefd[STREAMS_COUNT]; int port; uint32_t user_ts=0; int channels; SessionSet *set; char *filename; if (argc<4){ printf(help); return -1; } channels=atoi(argv[3]); if (channels==0){ 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); port=atoi(argv[2]); p_channel_id = (int *)ortp_malloc(channels*sizeof(int)); for (i=0;i<channels;i++){ session[i]=rtp_session_new(RTP_SESSION_RECVONLY); rtp_session_set_scheduling_mode(session[i],1); rtp_session_set_blocking_mode(session[i],0); rtp_session_set_local_addr(session[i],"0.0.0.0",port); rtp_session_set_recv_payload_type(session[i],0); rtp_session_set_recv_buf_size(session[i],256); p_channel_id[i] = i; /* register for telephony events */ rtp_session_signal_connect(session[i],"telephone-event",(RtpCallback)recv_tev_cb,(long)&p_channel_id[i]); port+=2; } filename=ortp_malloc(strlen(argv[1])+8); for (i=0;i<channels;i++){ sprintf(filename,"%s%4.4d.dat",argv[1],i); #ifndef _WIN32 filefd[i]=open(filename,O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); #else filefd[i]=open(filename,_O_BINARY | O_WRONLY | O_CREAT | O_TRUNC); #endif if (filefd[i]<0) ortp_error("Could not open %s for writing: %s",filename,strerror(errno)); } signal(SIGINT,stophandler); /* create a set */ set=session_set_new(); while(runcond) { int k; for (k=0;k<channels;k++){ /* add the session to the set */ session_set_set(set,session[k]); } /* and then suspend the process by selecting() */ session_set_select(set,NULL,NULL); for (k=0;k<channels;k++){ if (session_set_is_set(set,session[k])){ rtp2disk(session[k],user_ts,filefd[k]); } } user_ts+=160; } for (i=0;i<channels;i++){ close(filefd[i]); rtp_session_destroy(session[i]); } session_set_destroy(set); ortp_free(p_channel_id); ortp_free(filename); ortp_exit(); ortp_global_stats_display(); return 0; }
int main(int argc, char *argv[]) { RtpSession *session[STREAMS_COUNT]; int i; int filefd[STREAMS_COUNT]; int port; uint32_t user_ts=0; int channels; SessionSet *set; char *filename; if (argc<4){ printf("%s",help); return -1; } channels=atoi(argv[3]); if (channels==0){ printf("%s",help); return -1; } ortp_init(); ortp_scheduler_init(); port=atoi(argv[2]); recvbuf=ortp_malloc(160); for (i=0;i<channels;i++){ session[i]=rtp_session_new(RTP_SESSION_RECVONLY); rtp_session_set_scheduling_mode(session[i],1); rtp_session_set_blocking_mode(session[i],0); rtp_session_set_local_addr(session[i],"0.0.0.0",port,port+1); rtp_session_set_payload_type(session[i],0); rtp_session_enable_adaptive_jitter_compensation(session[i], TRUE); rtp_session_set_recv_buf_size(session[i],256); port+=2; } filename=ortp_malloc(strlen(argv[1])+15); for (i=0;i<channels;i++){ sprintf(filename,"%s%4.4d.dat",argv[1],i); #ifndef _WIN32 filefd[i]=open(filename,O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); #else filefd[i]=open(filename,_O_BINARY | O_WRONLY | O_CREAT | O_TRUNC); #endif if (filefd[i]<0) ortp_error("Could not open %s for writing: %s",filename,strerror(errno)); } signal(SIGINT,stophandler); /* create a set */ set=session_set_new(); while(runcond) { int k; for (k=0;k<channels;k++){ /* add the session to the set */ session_set_set(set,session[k]); //printf("session_set_set %d\n", k); } /* and then suspend the process by selecting() */ k=session_set_select(set,NULL,NULL); if (k==0) printf("warning: session_set_select() is returning 0...\n"); for (k=0;k<channels;k++){ if (session_set_is_set(set,session[k])){ rtp2disk(session[k],user_ts,filefd[k]); //printf("session_set_is_set %d\n", k); } else { //printf("warning: session %i is not set !\n",k); } } user_ts+=160; } printf("Exiting\n"); for (i=0;i<channels;i++){ close(filefd[i]); rtp_session_destroy(session[i]); } session_set_destroy(set); ortp_free(filename); ortp_exit(); ortp_global_stats_display(); ortp_free(recvbuf); return 0; }
OrtpLossRateEstimator * ortp_loss_rate_estimator_new(int min_packet_count_interval, uint64_t min_time_ms_interval, RtpSession *session){ OrtpLossRateEstimator *obj=ortp_malloc(sizeof(OrtpLossRateEstimator)); ortp_loss_rate_estimator_init(obj,min_packet_count_interval, min_time_ms_interval, session); return obj; }