#include #include #include #include #include #include #include #include #include #include #include #include #include //#include //File Control Definitions #include //POSIX Terminal Control Definitions //#include //#include /* . . . */ #define RTP_SENDBUF 160 #define RTP_RECVBUF 160 /* . . . */ #define RTP_STOP 1 #define RTP_CLOSED 2 #define RTP_ACTIVE 3 /* . . . */ typedef struct ua_core{ /* config */ int expiry; int localport; int calltimeout; char *proxy; char *outboundproxy; char *username; char *password; char *from; char *to; char *contact; char *localip; char *firewallip; char *transport; /* dynamic */ struct eXosip_t *context; //pthread_t notifythread; pthread_t rtp_thread; int running; int regid; int callid; int dialogid; int transactionid; int cmd; int remoteRtpPort; char *remoteIP; RtpSession* RecvSession; // RtpSession *SendSession; uint32_t send_ts; uint32_t recv_ts; int rtp_status; int sipua_status; } uacore; uacore g_core; /* . . . */ void Twr_RtpSessionsInit(uacore *core); void Twr_rtpSessionDestroy(uacore *core); void *ua_rtp_thread(void *arg); unsigned char Snack_Lin2Mulaw(short pcm_val); //Convert a linear PCM value to u-law short Snack_Mulaw2Lin(unsigned char u_val); //Convert a u-law value to 16-bit linear PCM void ua_stop(int signum) { g_core.running = 0; } /* . . . */ void Twr_RtpSessionsInit(uacore *core) { int jittcomp=50; core->RecvSession=rtp_session_new(RTP_SESSION_SENDRECV); rtp_session_set_scheduling_mode(core->RecvSession,1); rtp_session_set_blocking_mode(core->RecvSession,0); rtp_session_set_local_addr(core->RecvSession,"192.168.1.3",7088,7089); rtp_session_set_connected_mode(core->RecvSession,TRUE); rtp_session_set_remote_addr(core->RecvSession,core->remoteIP,core->remoteRtpPort); rtp_session_enable_adaptive_jitter_compensation(core->RecvSession,TRUE); rtp_session_set_jitter_compensation(core->RecvSession,jittcomp); rtp_session_set_payload_type(core->RecvSession,0); rtp_session_signal_connect(core->RecvSession,"ssrc_changed",(RtpCallback)rtp_session_reset,0); } void Twr_rtpSessionDestroy(uacore *core) { if (NULL != core->RecvSession) { rtp_session_destroy(core->RecvSession); core->RecvSession = NULL; } if (NULL != core->remoteIP) { free(core->remoteIP); core->remoteIP = NULL; } if (ttyusb0_active) ttyusb0_active = close(fd_ttyusb0); //Close the port } /* rtp loop */ void * ua_rtp_thread(void *arg) { uacore *core = (uacore *)arg; int recv_data, send_data; short buff_write_l16[RTP_RECVBUF]; unsigned char RtpRecvBuffer[RTP_RECVBUF]; short buff_read_l16[RTP_SENDBUF]; unsigned char RtpSendBuffer[RTP_SENDBUF]; int have_more; int rtpInd; int copy_rtp_status; while (core->running) { pthread_mutex_lock(&lock); copy_rtp_status = core->rtp_status; pthread_mutex_unlock(&lock); if(copy_rtp_status == RTP_ACTIVE) { have_more=1; while (have_more) { recv_data=rtp_session_recv_with_ts(core->RecvSession, RtpRecvBuffer, RTP_RECVBUF, core->recv_ts, &have_more); if (recv_data>0) { for (rtpInd = 0; rtpInd < recv_data; rtpInd++) buff_write_l16[rtpInd] = Snack_Mulaw2Lin(RtpRecvBuffer[rtpInd]); ssize_t ret = write(fd_ttyusb0, buff_write_l16, (RTP_RECVBUF*2)); } } core->recv_ts+=RTP_RECVBUF; if((send_data = read(fd_ttyusb0, buff_read_l16, (RTP_SENDBUF*2))) > 0) { for (rtpInd = 0; rtpInd < (send_data/2); rtpInd++) RtpSendBuffer[rtpInd] = Snack_Lin2Mulaw(~buff_read_l16[rtpInd]); rtp_session_send_with_ts( core->RecvSession, RtpSendBuffer, (send_data/2), core->send_ts); core->send_ts+=RTP_SENDBUF; } } else { if(copy_rtp_status == RTP_CLOSED) { core->send_ts = 0; core->recv_ts = 0; pthread_mutex_lock(&lock); Twr_rtpSessionDestroy(core); core->rtp_status = RTP_STOP; pthread_mutex_unlock(&lock); } } }//end while (core->running) pthread_detach(pthread_self()); return 0; } /* . . . */ /***************************** main *****************************/ int main(int argc, char *argv[]) { /* . . . */ ortp_init(); ortp_scheduler_init(); //ortp_set_log_level_mask(NULL, ORTP_DEBUG|ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR); g_core.rtp_status = RTP_STOP; /* start */ pthread_create(&g_core.rtp_thread, NULL, ua_rtp_thread, &g_core); /* . . . */ ortp_exit(); ortp_global_stats_display(); return 0; } /* . . . */