void IAXVoIPLink::init() { if (initDone_) return; for (int port = IAX_DEFAULT_PORTNO, nbTry = 0; nbTry < 3 ; port = rand() % 64000 + 1024, nbTry++) { if (iax_init(port) >= 0) { handlingEvents_ = true; evThread_.start(); initDone_ = true; break; } } }
EXPORT int iaxc_initialize(int num_calls) { printf("ESTOY IAXC 0\n"); int i; int port; os_init(); setup_jb_output(); MUTEXINIT(&iaxc_lock); MUTEXINIT(&event_queue_lock); iaxc_set_audio_prefs(0); if ( iaxc_recvfrom != (iaxc_recvfrom_t)recvfrom ) iax_set_networking(iaxc_sendto, iaxc_recvfrom); /* Note that iax_init() only sets up the receive port when the * sendto/recvfrom functions have not been replaced. We need * to call iaxc_init in either case because there is other * initialization beyond the socket setup that needs to be done. */ if ( (port = iax_init(source_udp_port)) < 0 ) { iaxci_usermsg(IAXC_ERROR, "Fatal error: failed to initialize iax with port %d", port); return -1; } if ( iaxc_recvfrom == (iaxc_recvfrom_t)recvfrom ) iaxci_bound_port = port; else iaxci_bound_port = -1; /* tweak the jitterbuffer settings */ iax_set_jb_target_extra( jb_target_extra ); max_calls = num_calls; /* initialize calls */ if ( max_calls <= 0 ) max_calls = 1; /* 0 == Default? */ /* calloc zeroes for us */ calls = (struct iaxc_call *)calloc(sizeof(struct iaxc_call), max_calls); if ( !calls ) { iaxci_usermsg(IAXC_ERROR, "Fatal error: can't allocate memory"); return -1; } selected_call = -1; for ( i = 0; i < max_calls; i++ ) { strncpy(calls[i].callerid_name, DEFAULT_CALLERID_NAME, IAXC_EVENT_BUFSIZ); strncpy(calls[i].callerid_number, DEFAULT_CALLERID_NUMBER, IAXC_EVENT_BUFSIZ); } printf("ESTOY IAXC\n"); if ( alsa_initialize(&audio_driver, 8000) ) { iaxci_usermsg(IAXC_ERROR, "failed alsa_initialize"); return -1; } #ifdef USE_VIDEO if ( video_initialize() ) iaxci_usermsg(IAXC_ERROR, "iaxc_initialize: cannot initialize video!\n"); #endif /* Default audio format capabilities */ audio_format_capability = IAXC_FORMAT_ULAW | IAXC_FORMAT_ALAW | #ifdef CODEC_GSM IAXC_FORMAT_GSM | #endif IAXC_FORMAT_SPEEX; audio_format_preferred = IAXC_FORMAT_SPEEX; return 0; }
EXPORT int iaxc_initialize(int num_calls) { int i; int port; os_init(); setup_jb_output(); MUTEXINIT(&iaxc_lock); MUTEXINIT(&event_queue_lock); iaxc_set_audio_prefs(0); if ( iaxc_recvfrom != (iaxc_recvfrom_t)recvfrom ) iax_set_networking(iaxc_sendto, iaxc_recvfrom); /* Note that iax_init() only sets up the receive port when the * sendto/recvfrom functions have not been replaced. We need * to call iaxc_init in either case because there is other * initialization beyond the socket setup that needs to be done. */ if ( (port = iax_init(source_udp_port)) < 0 ) { iaxci_usermsg(IAXC_ERROR, "Fatal error: failed to initialize iax with port %d", port); return -1; } if ( iaxc_recvfrom == (iaxc_recvfrom_t)recvfrom ) iaxci_bound_port = port; else iaxci_bound_port = -1; /* tweak the jitterbuffer settings */ iax_set_jb_target_extra( jb_target_extra ); max_calls = num_calls; /* initialize calls */ if ( max_calls <= 0 ) max_calls = 1; /* 0 == Default? */ /* calloc zeroes for us */ calls = (struct iaxc_call *)calloc(sizeof(struct iaxc_call), max_calls); if ( !calls ) { iaxci_usermsg(IAXC_ERROR, "Fatal error: can't allocate memory"); return -1; } selected_call = -1; for ( i = 0; i < max_calls; i++ ) { strncpy(calls[i].callerid_name, DEFAULT_CALLERID_NAME, IAXC_EVENT_BUFSIZ); strncpy(calls[i].callerid_number, DEFAULT_CALLERID_NUMBER, IAXC_EVENT_BUFSIZ); } if ( !test_mode ) { #ifndef AUDIO_ALSA if ( pa_initialize(&audio_driver, 8000) ) { iaxci_usermsg(IAXC_ERROR, "failed pa_initialize"); return -1; } #else /* TODO: It is unknown whether this stuff for direct access to * alsa should be left in iaxclient. We're leaving it in here for * the time being, but unless it becomes clear that someone cares * about having it, it will be removed. Also note that portaudio * is capable of using alsa. This is another reason why this * direct alsa access may be unneeded. */ if ( alsa_initialize(&audio_driver, 8000) ) return -1; #endif } #ifdef USE_VIDEO if ( video_initialize() ) iaxci_usermsg(IAXC_ERROR, "iaxc_initialize: cannot initialize video!\n"); #endif /* Default audio format capabilities */ audio_format_capability = IAXC_FORMAT_ULAW | IAXC_FORMAT_ALAW | #ifdef CODEC_GSM IAXC_FORMAT_GSM | #endif IAXC_FORMAT_SPEEX; audio_format_preferred = IAXC_FORMAT_SPEEX; return 0; }
int main(int argc, char *argv[]) { int port; int netfd; int c, h=0, m, regm; FILE *f; int fd = STDIN_FILENO; char rcmd[RBUFSIZE]; fd_set readfd; fd_set writefd; struct timeval timer; struct timeval *timerptr = NULL; gsm_frame fo; load_options(); if (!strlen(callerid)) gethostname(callerid, sizeof(callerid)); signal(SIGHUP, sighandler); signal(SIGINT, sighandler); if ( !(f = fdopen(fd, "w+"))) { fprintf(stderr, "Unable to create file on fd %d\n", fd); return -1; } if ( (audiofd = audio_setup(audiodev)) == -1) { fprintf(stderr, "Fatal error: failed to open sound device"); return -1; } if ( (port = iax_init(0) < 0)) { fprintf(stderr, "Fatal error: failed to initialize iax with port %d\n", port); return -1; } iax_set_formats(AST_FORMAT_GSM); netfd = iax_get_fd(); check_iax_register(); fprintf(f, "Text Based Telephony Client.\n\n"); issue_prompt(f); timer.tv_sec = 0; timer.tv_usec = 0; while(1) { FD_ZERO(&readfd); FD_ZERO(&writefd); FD_SET(fd, &readfd); if(fd > h) h = fd; if(answered_call && !writeonly) { FD_SET(audiofd, &readfd); if(audiofd > h) h = audiofd; } if (cursound > -1) { FD_SET(audiofd, &writefd); if (audiofd > h) h = audiofd; } FD_SET(netfd, &readfd); if(netfd > h) h = netfd; if ( (c = select(h+1, &readfd, &writefd, 0, timerptr)) >= 0) { if(FD_ISSET(fd, &readfd)) { if ( ( fgets(&*rcmd, 256, f))) { rcmd[strlen(rcmd)-1] = 0; parse_args(f, &*rcmd); } else fprintf(f, "Fatal error: failed to read data!\n"); issue_prompt(f); } if(answered_call) { if(FD_ISSET(audiofd, &readfd)) { static int ret, rlen = 0; static short rbuf[FRAME_SIZE]; if ( (ret = read(audiofd, rbuf + rlen, 2 * (FRAME_SIZE-rlen))) == -1) { puts("Failed to read audio."); return -1; } rlen += ret/2; if(rlen == FRAME_SIZE) { rlen = 0; if(!most_recent_answer->gsmout) most_recent_answer->gsmout = gsm_create(); gsm_encode(most_recent_answer->gsmout, rbuf, fo); if(iax_send_voice(most_recent_answer->session, AST_FORMAT_GSM, (char *)fo, sizeof(fo)) == -1) puts("Failed to send voice!"); } } } do_iax_event(f); m = iax_time_to_next_event(); if(m > -1) { timerptr = &timer; timer.tv_sec = m /1000; timer.tv_usec = (m % 1000) * 1000; } else timerptr = 0; regm = check_iax_timeout(); if (!timerptr || (m > regm)) { timerptr = &timer; timer.tv_sec = regm /1000; timer.tv_usec = (regm % 1000) * 1000; } if (FD_ISSET(audiofd, &writefd)) { send_sound(audiofd); } } else { if(errno == EINTR) continue; fprintf(stderr, "Fatal error in select(): %s\n", strerror(errno)); return -1; } } return 0; }