2 * iaxclient: a cross-platform IAX softphone library
5 * Copyright (C) 2003-2006, Horizon Wimba, Inc.
6 * Copyright (C) 2007, Wimba, Inc.
9 * Steve Kann <stevek@stevek.com>
10 * Michael Van Donselaar <mvand@vandonselaar.org>
11 * Shawn Lawrence <shawn.lawrence@terracecomm.com>
12 * Frik Strecker <frik@gatherworks.com>
13 * Mihai Balea <mihai AT hates DOT ms>
14 * Peter Grayson <jpgrayson@gmail.com>
15 * Bill Cholewka <bcholew@gmail.com>
16 * Erik Bunce <kde@bunce.us>
18 * This program is free software, distributed under the terms of
19 * the GNU Lesser (Library) General Public License.
24 #if defined(WIN32) || defined(_WIN32_WCE)
28 /* Win32 has _vsnprintf instead of vsnprintf */
31 # define vsnprintf _vsnprintf
35 #include "iaxclient_lib.h"
36 #include "audio_portaudio.h"
37 #include "audio_encode.h"
41 #include "iax-client.h"
42 #include "jitterbuf.h"
47 #include "audio_alsa.h"
50 #define IAXC_ERROR IAXC_TEXT_TYPE_ERROR
51 #define IAXC_STATUS IAXC_TEXT_TYPE_STATUS
52 #define IAXC_NOTICE IAXC_TEXT_TYPE_NOTICE
54 #define DEFAULT_CALLERID_NAME "Not Available"
55 #define DEFAULT_CALLERID_NUMBER "7005551212"
59 /* global test mode flag */
62 /* configurable jitterbuffer options */
63 static long jb_target_extra = -1;
65 struct iaxc_registration
67 struct iax_session *session;
74 struct iaxc_registration *next;
77 static int next_registration_id = 0;
78 static struct iaxc_registration *registrations = NULL;
80 static struct iaxc_audio_driver audio_driver;
82 static int audio_format_capability;
83 static int audio_format_preferred;
85 // Audio callback behavior
86 // By default apps should let iaxclient handle audio
87 static unsigned int audio_prefs = 0;
89 void * post_event_handle = NULL;
90 int post_event_id = 0;
92 static int minimum_outgoing_framesize = 160; /* 20ms */
94 static MUTEX iaxc_lock;
95 static MUTEX event_queue_lock;
97 static int iaxci_bound_port = -1;
99 // default to use port 4569 unless set by iaxc_set_preferred_source_udp_port
100 static int source_udp_port = IAX_DEFAULT_PORTNO;
102 int iaxci_audio_output_mode = 0; // Normal
104 int selected_call; // XXX to be protected by mutex?
105 struct iaxc_call* calls;
106 int max_calls; // number of calls for this library session
108 static void service_network();
109 static int service_audio();
111 /* external global networking replacements */
112 static iaxc_sendto_t iaxc_sendto = (iaxc_sendto_t)sendto;
113 static iaxc_recvfrom_t iaxc_recvfrom = (iaxc_recvfrom_t)recvfrom;
116 static THREAD main_proc_thread;
117 #if defined(WIN32) || defined(_WIN32_WCE)
118 static THREADID main_proc_thread_id;
121 /* 0 running, 1 should quit, -1 not running */
122 static int main_proc_thread_flag = -1;
124 static iaxc_event_callback_t iaxc_event_callback = NULL;
126 // Internal queue of events, waiting to be posted once the library
128 static iaxc_event *event_queue = NULL;
131 static void get_iaxc_lock()
133 MUTEXLOCK(&iaxc_lock);
138 return MUTEXTRYLOCK(&iaxc_lock);
141 // Unlock the library and post any events that were queued in the meantime
144 iaxc_event *prev, *event;
146 MUTEXLOCK(&event_queue_lock);
149 MUTEXUNLOCK(&event_queue_lock);
151 MUTEXUNLOCK(&iaxc_lock);
155 iaxci_post_event(*event);
162 EXPORT void iaxc_set_audio_output(int mode)
164 iaxci_audio_output_mode = mode;
167 long iaxci_usecdiff(struct timeval * t0, struct timeval * t1)
169 return (t0->tv_sec - t1->tv_sec) * 1000000L + t0->tv_usec - t1->tv_usec;
172 long iaxci_msecdiff(struct timeval * t0, struct timeval * t1)
174 return iaxci_usecdiff(t0, t1) / 1000L;
177 EXPORT void iaxc_set_event_callback(iaxc_event_callback_t func)
179 iaxc_event_callback = func;
182 EXPORT int iaxc_set_event_callpost(void *handle, int id)
184 post_event_handle = handle;
186 iaxc_event_callback = iaxci_post_event_callback;
190 EXPORT void iaxc_free_event(iaxc_event *e)
195 EXPORT struct iaxc_ev_levels *iaxc_get_event_levels(iaxc_event *e)
197 return &e->ev.levels;
200 EXPORT struct iaxc_ev_text *iaxc_get_event_text(iaxc_event *e)
205 EXPORT struct iaxc_ev_call_state *iaxc_get_event_state(iaxc_event *e)
210 // Messaging functions
211 static void default_message_callback(const char * message)
213 //fprintf(stderr, "IAXCLIENT: %s\n", message);
216 // Post Events back to clients
217 void iaxci_post_event(iaxc_event e)
221 iaxci_usermsg(IAXC_ERROR,
222 "Error: something posted to us an invalid event");
226 if ( MUTEXTRYLOCK(&iaxc_lock) )
230 /* We could not obtain the lock. Queue the event. */
231 MUTEXLOCK(&event_queue_lock);
235 tail = &(*tail)->next;
236 *tail = (iaxc_event *)malloc(sizeof(iaxc_event));
237 memcpy(*tail, &e, sizeof(iaxc_event));
238 MUTEXUNLOCK(&event_queue_lock);
242 /* TODO: This is not the best. Since we were able to get the
243 * lock, we decide that it is okay to go ahead and do the
244 * callback to the application. This is really nasty because
245 * it gives the appearance of serialized callbacks, but in
246 * reality, we could callback an application multiple times
247 * simultaneously. So, as things stand, an application must
248 * do some locking in their callback function to make it
249 * reentrant. Barf. More ideally, iaxclient would guarantee
250 * serialized callbacks to the application.
252 MUTEXUNLOCK(&iaxc_lock);
254 if ( iaxc_event_callback )
258 rv = iaxc_event_callback(e);
260 if ( e.type == IAXC_EVENT_VIDEO )
262 /* We can free the frame data once it is off the
263 * event queue and has been processed by the client.
265 free(e.ev.video.data);
267 else if ( e.type == IAXC_EVENT_AUDIO )
269 free(e.ev.audio.data);
273 default_message_callback(
274 "IAXCLIENT: BIG PROBLEM, event callback returned failure!");
275 // > 0 means processed
279 // else, fall through to "defaults"
284 case IAXC_EVENT_TEXT:
285 default_message_callback(e.ev.text.message);
286 // others we just ignore too
292 void iaxci_usermsg(int type, const char *fmt, ...)
297 e.type = IAXC_EVENT_TEXT;
298 e.ev.text.type = type;
299 e.ev.text.callNo = -1;
301 vsnprintf(e.ev.text.message, IAXC_EVENT_BUFSIZ, fmt, args);
308 void iaxci_do_levels_callback(float input, float output)
311 e.type = IAXC_EVENT_LEVELS;
312 e.ev.levels.input = input;
313 e.ev.levels.output = output;
317 void iaxci_do_state_callback(int callNo)
320 if ( callNo < 0 || callNo >= max_calls )
322 e.type = IAXC_EVENT_STATE;
323 e.ev.call.callNo = callNo;
324 e.ev.call.state = calls[callNo].state;
325 e.ev.call.format = calls[callNo].format;
326 e.ev.call.vformat = calls[callNo].vformat;
327 strncpy(e.ev.call.remote, calls[callNo].remote, IAXC_EVENT_BUFSIZ);
328 strncpy(e.ev.call.remote_name, calls[callNo].remote_name, IAXC_EVENT_BUFSIZ);
329 strncpy(e.ev.call.local, calls[callNo].local, IAXC_EVENT_BUFSIZ);
330 strncpy(e.ev.call.local_context, calls[callNo].local_context, IAXC_EVENT_BUFSIZ);
334 void iaxci_do_registration_callback(int id, int reply, int msgcount)
337 e.type = IAXC_EVENT_REGISTRATION;
339 e.ev.reg.reply = reply;
340 e.ev.reg.msgcount = msgcount;
344 void iaxci_do_audio_callback(int callNo, unsigned int ts, int source,
345 int encoded, int format, int size, unsigned char *data)
349 e.type = IAXC_EVENT_AUDIO;
351 e.ev.audio.encoded = encoded;
352 assert(source == IAXC_SOURCE_REMOTE || source == IAXC_SOURCE_LOCAL);
353 e.ev.audio.source = source;
354 e.ev.audio.size = size;
355 e.ev.audio.callNo = callNo;
356 e.ev.audio.format = format;
358 e.ev.audio.data = (unsigned char *)malloc(size);
360 if ( !e.ev.audio.data )
362 iaxci_usermsg(IAXC_ERROR,
363 "failed to allocate memory for audio event");
367 memcpy(e.ev.audio.data, data, size);
372 void iaxci_do_dtmf_callback(int callNo, char digit)
375 e.type = IAXC_EVENT_DTMF;
376 e.ev.dtmf.callNo = callNo;
377 e.ev.dtmf.digit = digit;
381 static int iaxc_remove_registration_by_id(int id)
383 struct iaxc_registration *curr, *prev;
385 for ( prev = NULL, curr = registrations; curr != NULL;
386 prev = curr, curr = curr->next )
388 if ( curr->id == id )
391 if ( curr->session != NULL )
392 iax_destroy( curr->session );
394 prev->next = curr->next;
396 registrations = curr->next;
404 EXPORT int iaxc_first_free_call()
407 for ( i = 0; i < max_calls; i++ )
408 if ( calls[i].state == IAXC_CALL_STATE_FREE )
415 static void iaxc_clear_call(int toDump)
417 // XXX libiax should handle cleanup, I think..
418 calls[toDump].state = IAXC_CALL_STATE_FREE;
419 calls[toDump].format = 0;
420 calls[toDump].vformat = 0;
421 calls[toDump].session = NULL;
422 iaxci_do_state_callback(toDump);
426 /* XXX Locking?? Start/stop audio?? */
427 EXPORT int iaxc_select_call(int callNo)
429 // continue if already selected?
430 //if ( callNo == selected_call ) return;
432 if ( callNo >= max_calls )
434 iaxci_usermsg(IAXC_ERROR, "Error: tried to select out_of_range call %d", callNo);
438 // callNo < 0 means no call selected (i.e. all on hold)
441 if ( selected_call >= 0 )
443 calls[selected_call].state &= ~IAXC_CALL_STATE_SELECTED;
445 selected_call = callNo;
449 // de-select and notify the old call if not also the new call
450 if ( callNo != selected_call )
452 if ( selected_call >= 0 )
454 calls[selected_call].state &= ~IAXC_CALL_STATE_SELECTED;
455 iaxci_do_state_callback(selected_call);
457 selected_call = callNo;
458 calls[selected_call].state |= IAXC_CALL_STATE_SELECTED;
461 // if it's an incoming call, and ringing, answer it.
462 if ( !(calls[selected_call].state & IAXC_CALL_STATE_OUTGOING) &&
463 (calls[selected_call].state & IAXC_CALL_STATE_RINGING) )
465 iaxc_answer_call(selected_call);
468 // otherwise just update state (answer does this for us)
469 iaxci_do_state_callback(selected_call);
475 /* external API accessor */
476 EXPORT int iaxc_selected_call()
478 return selected_call;
481 EXPORT void iaxc_set_networking(iaxc_sendto_t st, iaxc_recvfrom_t rf)
487 EXPORT void iaxc_set_jb_target_extra( long value )
489 /* store in jb_target_extra, a static global */
490 jb_target_extra = value;
493 static void jb_errf(const char *fmt, ...)
499 vsnprintf(buf, 1024, fmt, args);
502 iaxci_usermsg(IAXC_ERROR, buf);
505 static void jb_warnf(const char *fmt, ...)
511 vsnprintf(buf, 1024, fmt, args);
514 iaxci_usermsg(IAXC_NOTICE, buf);
518 static void jb_dbgf(const char *fmt, ...)
523 vfprintf(stderr, fmt, args);
528 static void setup_jb_output()
531 jb_setoutput(jb_errf, jb_warnf, jb_dbgf);
533 jb_setoutput(jb_errf, jb_warnf, NULL);
537 // Note: Must be called before iaxc_initialize()
538 EXPORT void iaxc_set_preferred_source_udp_port(int port)
540 source_udp_port = port;
543 /* For "slow" systems. See iax.c code */
544 EXPORT int iaxc_video_bypass_jitter(int mode)
547 * 1. When switching from jitter to no-jitter the buffer must be
548 * flushed of queued video packet and must be sent a key-frame
549 * to redraw the screen (partially done).
550 * 2. When switching from no-jitter to jitter we must drop all
551 * enqueued events prior the mode change (must be touched
552 * iax_sched_del and iax_get_event).
554 return iax_video_bypass_jitter(calls[selected_call].session,mode);
557 EXPORT int iaxc_get_bind_port()
559 return iaxci_bound_port;
562 EXPORT int iaxc_initialize(int num_calls)
571 MUTEXINIT(&iaxc_lock);
572 MUTEXINIT(&event_queue_lock);
574 iaxc_set_audio_prefs(0);
576 if ( iaxc_recvfrom != (iaxc_recvfrom_t)recvfrom )
577 iax_set_networking(iaxc_sendto, iaxc_recvfrom);
579 /* Note that iax_init() only sets up the receive port when the
580 * sendto/recvfrom functions have not been replaced. We need
581 * to call iaxc_init in either case because there is other
582 * initialization beyond the socket setup that needs to be done.
584 if ( (port = iax_init(source_udp_port)) < 0 )
586 iaxci_usermsg(IAXC_ERROR,
587 "Fatal error: failed to initialize iax with port %d",
592 if ( iaxc_recvfrom == (iaxc_recvfrom_t)recvfrom )
593 iaxci_bound_port = port;
595 iaxci_bound_port = -1;
597 /* tweak the jitterbuffer settings */
598 iax_set_jb_target_extra( jb_target_extra );
600 max_calls = num_calls;
601 /* initialize calls */
602 if ( max_calls <= 0 )
603 max_calls = 1; /* 0 == Default? */
605 /* calloc zeroes for us */
606 calls = (struct iaxc_call *)calloc(sizeof(struct iaxc_call), max_calls);
609 iaxci_usermsg(IAXC_ERROR, "Fatal error: can't allocate memory");
615 for ( i = 0; i < max_calls; i++ )
617 strncpy(calls[i].callerid_name, DEFAULT_CALLERID_NAME, IAXC_EVENT_BUFSIZ);
618 strncpy(calls[i].callerid_number, DEFAULT_CALLERID_NUMBER, IAXC_EVENT_BUFSIZ);
624 if ( pa_initialize(&audio_driver, 8000) )
626 iaxci_usermsg(IAXC_ERROR, "failed pa_initialize");
631 /* TODO: It is unknown whether this stuff for direct access to
632 * alsa should be left in iaxclient. We're leaving it in here for
633 * the time being, but unless it becomes clear that someone cares
634 * about having it, it will be removed. Also note that portaudio
635 * is capable of using alsa. This is another reason why this
636 * direct alsa access may be unneeded.
638 if ( alsa_initialize(&audio_driver, 8000) )
642 if ( openal_initialize(&audio_driver, 8000) )
644 iaxci_usermsg(IAXC_ERROR, "failed openal_initialize");
650 if ( video_initialize() )
651 iaxci_usermsg(IAXC_ERROR,
652 "iaxc_initialize: cannot initialize video!\n");
655 /* Default audio format capabilities */
656 audio_format_capability =
663 audio_format_preferred = IAXC_FORMAT_SPEEX;
668 EXPORT void iaxc_shutdown()
670 iaxc_dump_all_calls();
676 audio_driver.destroy(&audio_driver);
682 /* destroy enocders and decoders for all existing calls */
686 for ( i=0 ; i<max_calls ; i++ )
688 if ( calls[i].encoder )
689 calls[i].encoder->destroy(calls[i].encoder);
690 if ( calls[i].decoder )
691 calls[i].decoder->destroy(calls[i].decoder);
692 if ( calls[i].vencoder )
693 calls[i].vencoder->destroy(calls[i].vencoder);
694 if ( calls[i].vdecoder )
695 calls[i].vdecoder->destroy(calls[i].vdecoder);
702 //closesocket(iax_get_fd()); //fd:
707 MUTEXDESTROY(&event_queue_lock);
708 MUTEXDESTROY(&iaxc_lock);
712 EXPORT void iaxc_set_formats(int preferred, int allowed)
714 audio_format_capability = allowed;
715 audio_format_preferred = preferred;
718 EXPORT void iaxc_set_min_outgoing_framesize(int samples)
720 minimum_outgoing_framesize = samples;
723 EXPORT void iaxc_set_callerid(const char * name, const char * number)
727 for ( i = 0; i < max_calls; i++ )
729 strncpy(calls[i].callerid_name, name, IAXC_EVENT_BUFSIZ);
730 strncpy(calls[i].callerid_number, number, IAXC_EVENT_BUFSIZ);
734 static void iaxc_note_activity(int callNo)
738 calls[callNo].last_activity = iax_tvnow();
741 static void iaxc_refresh_registrations()
743 struct iaxc_registration *cur;
748 for ( cur = registrations; cur != NULL; cur = cur->next )
750 // If there is less than three seconds before the registration is about
751 // to expire, renew it.
752 if ( iaxci_usecdiff(&now, &cur->last) > (cur->refresh - 3) * 1000 *1000 )
754 if ( cur->session != NULL )
756 iax_destroy( cur->session );
758 cur->session = iax_session_new();
761 iaxci_usermsg(IAXC_ERROR, "Can't make new registration session");
764 iax_register(cur->session, cur->host, cur->user, cur->pass, cur->refresh);
770 #define LOOP_SLEEP 5 // In ms
771 static THREADFUNCDECL(main_proc_thread_func)
773 static int refresh_registration_count = 0;
777 /* Increase Priority */
778 iaxci_prioboostbegin();
780 while ( !main_proc_thread_flag )
788 // Check registration refresh once a second
789 if ( refresh_registration_count++ > 1000/LOOP_SLEEP )
791 iaxc_refresh_registrations();
792 refresh_registration_count = 0;
797 iaxc_millisleep(LOOP_SLEEP);
800 /* Decrease priority */
801 iaxci_prioboostend();
803 main_proc_thread_flag = -1;
808 EXPORT int iaxc_start_processing_thread()
810 main_proc_thread_flag = 0;
812 if ( THREADCREATE(main_proc_thread_func, NULL, main_proc_thread,
813 main_proc_thread_id) == THREADCREATE_ERROR)
819 EXPORT int iaxc_stop_processing_thread()
821 if ( main_proc_thread_flag >= 0 )
823 main_proc_thread_flag = 1;
824 THREADJOIN(main_proc_thread);
830 static int service_audio()
832 /* TODO: maybe we shouldn't allocate 8kB on the stack here. */
835 int want_send_audio =
836 selected_call >= 0 &&
837 ((calls[selected_call].state & IAXC_CALL_STATE_OUTGOING) ||
838 (calls[selected_call].state & IAXC_CALL_STATE_COMPLETE))
839 && !(audio_prefs & IAXC_AUDIO_PREF_SEND_DISABLE);
841 int want_local_audio =
842 (audio_prefs & IAXC_AUDIO_PREF_RECV_LOCAL_RAW) ||
843 (audio_prefs & IAXC_AUDIO_PREF_RECV_LOCAL_ENCODED);
845 if ( want_local_audio || want_send_audio )
852 audio_driver.start(&audio_driver);
854 /* use codec minimum if higher */
855 cmin = want_send_audio && calls[selected_call].encoder ?
856 calls[selected_call].encoder->minimum_frame_size :
859 to_read = cmin > minimum_outgoing_framesize ?
860 cmin : minimum_outgoing_framesize;
862 /* Round up to the next multiple */
863 if ( to_read % cmin )
864 to_read += cmin - (to_read % cmin);
866 if ( to_read > (int)(sizeof(buf) / sizeof(short)) )
869 "internal error: to_read > sizeof(buf)\n");
873 /* Currently pa gives us either all the bits we ask for or none */
874 if ( audio_driver.input(&audio_driver, buf, &to_read) )
876 iaxci_usermsg(IAXC_ERROR, "ERROR reading audio\n");
880 /* Frame was not available */
884 if ( audio_prefs & IAXC_AUDIO_PREF_RECV_LOCAL_RAW )
885 iaxci_do_audio_callback(selected_call, 0,
886 IAXC_SOURCE_LOCAL, 0, 0,
887 to_read * 2, (unsigned char *)buf);
889 if ( want_send_audio )
890 audio_send_encoded_audio(&calls[selected_call],
892 calls[selected_call].format &
893 IAXC_AUDIO_FORMAT_MASK,
901 audio_driver.stop(&audio_driver);
905 Q: Why do we continuously send IAXC_EVENT_LEVELS events
906 when there is no selected call?
908 A: So that certain users of iaxclient do not have to
909 reset their vu meters when a call ends -- they can just
910 count on getting level callbacks. This is a bit of a hack
911 so any applications relying on this behavior should maybe
915 iaxci_do_levels_callback(AUDIO_ENCODE_SILENCE_DB,
916 AUDIO_ENCODE_SILENCE_DB);
922 /* handle IAX text events */
923 static void handle_text_event(struct iax_event *e, int callNo)
931 memset(&ev, 0, sizeof(iaxc_event));
932 ev.type = IAXC_EVENT_TEXT;
933 ev.ev.text.type = IAXC_TEXT_TYPE_IAX;
934 ev.ev.text.callNo = callNo;
936 len = e->datalen <= IAXC_EVENT_BUFSIZ - 1 ? e->datalen : IAXC_EVENT_BUFSIZ - 1;
937 strncpy(ev.ev.text.message, (char *) e->data, len);
938 iaxci_post_event(ev);
941 /* handle IAX URL events */
942 void handle_url_event( struct iax_event *e, int callNo )
949 ev.ev.url.callNo = callNo;
950 ev.type = IAXC_EVENT_URL;
951 strcpy( ev.ev.url.url, "" );
953 switch ( e->subclass )
956 ev.ev.url.type = IAXC_URL_URL;
959 if ( e->datalen > IAXC_EVENT_BUFSIZ )
961 fprintf( stderr, "ERROR: URL too long %d > %d\n",
962 e->datalen, IAXC_EVENT_BUFSIZ );
965 strncpy( ev.ev.url.url, (char *) e->data, e->datalen );
968 /* fprintf( stderr, "URL:%s\n", ev.ev.url.url ); */
970 case AST_HTML_LINKURL:
971 ev.ev.url.type = IAXC_URL_LINKURL;
972 /* fprintf( stderr, "LINKURL event\n" ); */
974 case AST_HTML_LDCOMPLETE:
975 ev.ev.url.type = IAXC_URL_LDCOMPLETE;
976 /* fprintf( stderr, "LDCOMPLETE event\n" ); */
978 case AST_HTML_UNLINK:
979 ev.ev.url.type = IAXC_URL_UNLINK;
980 /* fprintf( stderr, "UNLINK event\n" ); */
982 case AST_HTML_LINKREJECT:
983 ev.ev.url.type = IAXC_URL_LINKREJECT;
984 /* fprintf( stderr, "LINKREJECT event\n" ); */
987 fprintf( stderr, "Unknown URL event %d\n", e->subclass );
990 iaxci_post_event( ev );
993 /* DANGER: bad things can happen if iaxc_netstat != iax_netstat.. */
994 EXPORT int iaxc_get_netstats(int call, int *rtt, struct iaxc_netstat *local,
995 struct iaxc_netstat *remote)
997 return iax_get_netstats(calls[call].session, rtt,
998 (struct iax_netstat *)local,
999 (struct iax_netstat *)remote);
1002 /* handle IAX text events */
1003 static void generate_netstat_event(int callNo)
1010 ev.type = IAXC_EVENT_NETSTAT;
1011 ev.ev.netstats.callNo = callNo;
1013 /* only post the event if the session is valid, etc */
1014 if ( !iaxc_get_netstats(callNo, &ev.ev.netstats.rtt,
1015 &ev.ev.netstats.local, &ev.ev.netstats.remote))
1016 iaxci_post_event(ev);
1019 static void handle_audio_event(struct iax_event *e, int callNo)
1021 int total_consumed = 0;
1023 const int fr_samples = sizeof(fr) / sizeof(short);
1024 int samples, format;
1026 int cycles_max = 100; //fd:
1028 struct iaxc_call *call;
1033 call = &calls[callNo];
1035 if ( callNo != selected_call )
1037 /* drop audio for unselected call? */
1041 samples = fr_samples;
1042 format = call->format & IAXC_AUDIO_FORMAT_MASK;
1048 int mainbuf_delta = fr_samples - samples;
1050 bytes_decoded = audio_decode_audio(call,
1052 e->data + total_consumed,
1053 e->datalen - total_consumed,
1057 if ( bytes_decoded < 0 )
1059 iaxci_usermsg(IAXC_STATUS,
1060 "Bad or incomplete voice packet. Unable to decode. dropping");
1064 /* Pass encoded audio back to the app if required */
1065 if ( audio_prefs & IAXC_AUDIO_PREF_RECV_REMOTE_ENCODED )
1066 iaxci_do_audio_callback(callNo, e->ts, IAXC_SOURCE_REMOTE,
1067 1, format & IAXC_AUDIO_FORMAT_MASK,
1068 e->datalen - total_consumed,
1069 e->data + total_consumed);
1072 //fd: start: for some reason it loops here. Try to avoid it
1074 if ( cycles_max < 0 )
1080 total_consumed += bytes_decoded;
1081 if ( audio_prefs & IAXC_AUDIO_PREF_RECV_REMOTE_RAW )
1083 // audio_decode_audio returns the number of samples.
1084 // We are using 16 bit samples, so we need to double
1085 // the number to obtain the size in bytes.
1086 // format will also be 0 since this is raw audio
1087 int size = (fr_samples - samples - mainbuf_delta) * 2;
1088 iaxci_do_audio_callback(callNo, e->ts, IAXC_SOURCE_REMOTE,
1089 0, 0, size, (unsigned char *)fr);
1092 if ( iaxci_audio_output_mode )
1096 audio_driver.output(&audio_driver, fr,
1097 fr_samples - samples - mainbuf_delta);
1099 } while ( total_consumed < e->datalen );
1103 static void handle_video_event(struct iax_event *e, int callNo)
1105 struct iaxc_call *call;
1110 if ( e->datalen == 0 )
1112 iaxci_usermsg(IAXC_STATUS, "Received 0-size packet. Unable to decode.");
1116 call = &calls[callNo];
1118 if ( callNo != selected_call )
1120 /* drop video for unselected call? */
1124 if ( call->vformat )
1126 if ( video_recv_video(call, selected_call, e->data,
1127 e->datalen, e->ts, call->vformat) < 0 )
1129 iaxci_usermsg(IAXC_STATUS,
1130 "Bad or incomplete video packet. Unable to decode.");
1135 #endif /* USE_VIDEO */
1137 static void iaxc_handle_network_event(struct iax_event *e, int callNo)
1142 iaxc_note_activity(callNo);
1146 case IAX_EVENT_NULL:
1148 case IAX_EVENT_HANGUP:
1149 iaxci_usermsg(IAXC_STATUS, "Call disconnected by remote");
1150 // XXX does the session go away now?
1151 iaxc_clear_call(callNo);
1153 case IAX_EVENT_REJECT:
1154 iaxci_usermsg(IAXC_STATUS, "Call rejected by remote");
1155 iaxc_clear_call(callNo);
1157 case IAX_EVENT_ACCEPT:
1158 calls[callNo].format = e->ies.format & IAXC_AUDIO_FORMAT_MASK;
1159 calls[callNo].vformat = e->ies.format & IAXC_VIDEO_FORMAT_MASK;
1161 if ( !(e->ies.format & IAXC_VIDEO_FORMAT_MASK) )
1163 iaxci_usermsg(IAXC_NOTICE,
1164 "Failed video codec negotiation.");
1167 iaxci_usermsg(IAXC_STATUS,"Call %d accepted", callNo);
1169 case IAX_EVENT_ANSWER:
1170 calls[callNo].state &= ~IAXC_CALL_STATE_RINGING;
1171 calls[callNo].state |= IAXC_CALL_STATE_COMPLETE;
1172 iaxci_do_state_callback(callNo);
1173 iaxci_usermsg(IAXC_STATUS,"Call %d answered", callNo);
1174 //iaxc_answer_call(callNo);
1177 case IAX_EVENT_BUSY:
1178 calls[callNo].state &= ~IAXC_CALL_STATE_RINGING;
1179 calls[callNo].state |= IAXC_CALL_STATE_BUSY;
1180 iaxci_do_state_callback(callNo);
1181 iaxci_usermsg(IAXC_STATUS, "Call %d busy", callNo);
1183 case IAX_EVENT_VOICE:
1184 handle_audio_event(e, callNo);
1185 if ( (calls[callNo].state & IAXC_CALL_STATE_OUTGOING) &&
1186 (calls[callNo].state & IAXC_CALL_STATE_RINGING) )
1188 calls[callNo].state &= ~IAXC_CALL_STATE_RINGING;
1189 calls[callNo].state |= IAXC_CALL_STATE_COMPLETE;
1190 iaxci_do_state_callback(callNo);
1191 iaxci_usermsg(IAXC_STATUS,"Call %d progress",
1196 case IAX_EVENT_VIDEO:
1197 handle_video_event(e, callNo);
1200 case IAX_EVENT_TEXT:
1201 handle_text_event(e, callNo);
1203 case IAX_EVENT_RINGA:
1204 calls[callNo].state |= IAXC_CALL_STATE_RINGING;
1205 iaxci_do_state_callback(callNo);
1206 iaxci_usermsg(IAXC_STATUS,"Call %d ringing", callNo);
1208 case IAX_EVENT_PONG:
1209 generate_netstat_event(callNo);
1212 handle_url_event(e, callNo);
1217 case IAX_EVENT_TIMEOUT:
1218 iax_hangup(e->session, "Call timed out");
1219 iaxci_usermsg(IAXC_STATUS, "Call %d timed out.", callNo);
1220 iaxc_clear_call(callNo);
1222 case IAX_EVENT_TRANSFER:
1223 calls[callNo].state |= IAXC_CALL_STATE_TRANSFER;
1224 iaxci_do_state_callback(callNo);
1225 iaxci_usermsg(IAXC_STATUS,"Call %d transfer released", callNo);
1227 case IAX_EVENT_DTMF:
1228 iaxci_do_dtmf_callback(callNo,e->subclass);
1229 iaxci_usermsg(IAXC_STATUS, "DTMF digit %c received", e->subclass);
1232 iaxci_usermsg(IAXC_STATUS, "Unknown event: %d for call %d", e->etype, callNo);
1237 EXPORT int iaxc_unregister( int id )
1241 count = iaxc_remove_registration_by_id(id);
1246 EXPORT int iaxc_register(const char * user, const char * pass, const char * host)
1248 return iaxc_register_ex(user, pass, host, 60);
1251 EXPORT int iaxc_register_ex(const char * user, const char * pass, const char * host, int refresh)
1253 struct iaxc_registration *newreg;
1255 newreg = (struct iaxc_registration *)malloc(sizeof (struct iaxc_registration));
1258 iaxci_usermsg(IAXC_ERROR, "Can't make new registration");
1263 newreg->session = iax_session_new();
1264 if ( !newreg->session )
1266 iaxci_usermsg(IAXC_ERROR, "Can't make new registration session");
1271 newreg->last = iax_tvnow();
1272 newreg->refresh = refresh;
1274 strncpy(newreg->host, host, 256);
1275 strncpy(newreg->user, user, 256);
1276 strncpy(newreg->pass, pass, 256);
1278 /* send out the initial registration with refresh seconds */
1279 iax_register(newreg->session, host, user, pass, refresh);
1281 /* add it to the list; */
1282 newreg->id = ++next_registration_id;
1283 newreg->next = registrations;
1284 registrations = newreg;
1290 static void codec_destroy( int callNo )
1292 if ( calls[callNo].encoder )
1294 calls[callNo].encoder->destroy( calls[callNo].encoder );
1295 calls[callNo].encoder = NULL;
1297 if ( calls[callNo].decoder )
1299 calls[callNo].decoder->destroy( calls[callNo].decoder );
1300 calls[callNo].decoder = NULL;
1302 if ( calls[callNo].vdecoder )
1304 calls[callNo].vdecoder->destroy(calls[callNo].vdecoder);
1305 calls[callNo].vdecoder = NULL;
1307 if ( calls[callNo].vencoder )
1309 calls[callNo].vencoder->destroy(calls[callNo].vencoder);
1310 calls[callNo].vencoder = NULL;
1314 EXPORT int iaxc_call(const char * num)
1316 return iaxc_call_ex(num, NULL, NULL, 1);
1319 EXPORT int iaxc_call_ex(const char *num, const char* callerid_name, const char* callerid_number, int video)
1321 int video_format_capability = 0;
1322 int video_format_preferred = 0;
1324 struct iax_session *newsession;
1325 char *ext = strstr(num, "/");
1329 // if no call is selected, get a new appearance
1330 if ( selected_call < 0 )
1332 callNo = iaxc_first_free_call();
1335 // use selected call if not active, otherwise, get a new appearance
1336 if ( calls[selected_call].state & IAXC_CALL_STATE_ACTIVE )
1338 callNo = iaxc_first_free_call();
1341 callNo = selected_call;
1347 iaxci_usermsg(IAXC_STATUS, "No free call appearances");
1348 goto iaxc_call_bail;
1351 newsession = iax_session_new();
1354 iaxci_usermsg(IAXC_ERROR, "Can't make new session");
1355 goto iaxc_call_bail;
1358 calls[callNo].session = newsession;
1360 codec_destroy( callNo );
1364 strncpy(calls[callNo].remote_name, num, IAXC_EVENT_BUFSIZ);
1365 strncpy(calls[callNo].remote, ++ext, IAXC_EVENT_BUFSIZ);
1368 strncpy(calls[callNo].remote_name, num, IAXC_EVENT_BUFSIZ);
1369 strncpy(calls[callNo].remote, "" , IAXC_EVENT_BUFSIZ);
1372 if ( callerid_number != NULL )
1373 strncpy(calls[callNo].callerid_number, callerid_number, IAXC_EVENT_BUFSIZ);
1375 if ( callerid_name != NULL )
1376 strncpy(calls[callNo].callerid_name, callerid_name, IAXC_EVENT_BUFSIZ);
1378 strncpy(calls[callNo].local , calls[callNo].callerid_name, IAXC_EVENT_BUFSIZ);
1379 strncpy(calls[callNo].local_context, "default", IAXC_EVENT_BUFSIZ);
1381 calls[callNo].state = IAXC_CALL_STATE_ACTIVE | IAXC_CALL_STATE_OUTGOING;
1383 /* reset activity and ping "timers" */
1384 iaxc_note_activity(callNo);
1385 calls[callNo].last_ping = calls[callNo].last_activity;
1389 iaxc_video_format_get_cap(&video_format_preferred, &video_format_capability);
1392 iaxci_usermsg(IAXC_NOTICE, "Originating an %s call",
1393 video_format_preferred ? "audio+video" : "audio only");
1394 iax_call(calls[callNo].session, calls[callNo].callerid_number,
1395 calls[callNo].callerid_name, num, NULL, 0,
1396 audio_format_preferred | video_format_preferred,
1397 audio_format_capability | video_format_capability);
1399 // does state stuff also
1400 iaxc_select_call(callNo);
1408 EXPORT void iaxc_send_busy_on_incoming_call(int callNo)
1413 iax_busy(calls[callNo].session);
1416 EXPORT void iaxc_answer_call(int callNo)
1421 calls[callNo].state |= IAXC_CALL_STATE_COMPLETE;
1422 calls[callNo].state &= ~IAXC_CALL_STATE_RINGING;
1423 iax_answer(calls[callNo].session);
1424 iaxci_do_state_callback(callNo);
1427 EXPORT void iaxc_blind_transfer_call(int callNo, const char * dest_extension)
1429 if ( callNo < 0 || !(calls[callNo].state & IAXC_CALL_STATE_ACTIVE) )
1432 iax_transfer(calls[callNo].session, dest_extension);
1435 EXPORT void iaxc_setup_call_transfer(int sourceCallNo, int targetCallNo)
1437 if ( sourceCallNo < 0 || targetCallNo < 0 ||
1438 !(calls[sourceCallNo].state & IAXC_CALL_STATE_ACTIVE) ||
1439 !(calls[targetCallNo].state & IAXC_CALL_STATE_ACTIVE) )
1442 iax_setup_transfer(calls[sourceCallNo].session, calls[targetCallNo].session);
1445 static void iaxc_dump_one_call(int callNo)
1449 if ( calls[callNo].state == IAXC_CALL_STATE_FREE )
1452 iax_hangup(calls[callNo].session,"Dumped Call");
1453 iaxci_usermsg(IAXC_STATUS, "Hanging up call %d", callNo);
1454 iaxc_clear_call(callNo);
1457 EXPORT void iaxc_dump_all_calls(void)
1461 for ( callNo = 0; callNo < max_calls; callNo++ )
1462 iaxc_dump_one_call(callNo);
1467 EXPORT void iaxc_dump_call_number( int callNo )
1469 if ( ( callNo >= 0 ) && ( callNo < max_calls ) )
1472 iaxc_dump_one_call(callNo);
1477 EXPORT void iaxc_dump_call(void)
1479 if ( selected_call >= 0 )
1482 iaxc_dump_one_call(selected_call);
1487 EXPORT void iaxc_reject_call(void)
1489 if ( selected_call >= 0 )
1491 iaxc_reject_call_number(selected_call);
1495 EXPORT void iaxc_reject_call_number( int callNo )
1497 if ( ( callNo >= 0 ) && ( callNo < max_calls ) )
1500 iax_reject(calls[callNo].session, "Call rejected manually.");
1501 iaxc_clear_call(callNo);
1506 EXPORT void iaxc_send_dtmf(char digit)
1508 if ( selected_call >= 0 )
1511 if ( calls[selected_call].state & IAXC_CALL_STATE_ACTIVE )
1512 iax_send_dtmf(calls[selected_call].session,digit);
1517 EXPORT void iaxc_send_text(const char * text)
1519 if ( selected_call >= 0 )
1522 if ( calls[selected_call].state & IAXC_CALL_STATE_ACTIVE )
1523 iax_send_text(calls[selected_call].session, text);
1528 EXPORT void iaxc_send_text_call(int callNo, const char * text)
1530 if ( callNo < 0 || !(calls[callNo].state & IAXC_CALL_STATE_ACTIVE) )
1534 if ( calls[callNo].state & IAXC_CALL_STATE_ACTIVE )
1535 iax_send_text(calls[callNo].session, text);
1539 EXPORT void iaxc_send_url(const char * url, int link)
1541 if ( selected_call >= 0 )
1544 if ( calls[selected_call].state & IAXC_CALL_STATE_ACTIVE )
1545 iax_send_url(calls[selected_call].session, url, link);
1550 static int iaxc_find_call_by_session(struct iax_session *session)
1553 for ( i = 0; i < max_calls; i++ )
1554 if ( calls[i].session == session )
1559 static struct iaxc_registration *iaxc_find_registration_by_session(
1560 struct iax_session *session)
1562 struct iaxc_registration *reg;
1563 for (reg = registrations; reg != NULL; reg = reg->next)
1564 if ( reg->session == session )
1569 static void iaxc_handle_regreply(struct iax_event *e, struct iaxc_registration *reg)
1571 iaxci_do_registration_callback(reg->id, e->etype, e->ies.msgcount);
1573 // XXX I think the session is no longer valid.. at least, that's
1574 // what miniphone does, and re-using the session doesn't seem to
1576 iax_destroy(reg->session);
1577 reg->session = NULL;
1579 if ( e->etype == IAX_EVENT_REGREJ )
1581 // we were rejected, so end the registration
1582 iaxc_remove_registration_by_id(reg->id);
1586 /* this is what asterisk does */
1587 static int iaxc_choose_codec(int formats)
1590 static int codecs[] =
1594 IAXC_FORMAT_SLINEAR,
1604 /* To negotiate video codec */
1609 IAXC_FORMAT_H263_PLUS,
1614 for ( i = 0; i < (int)(sizeof(codecs) / sizeof(int)); i++ )
1615 if ( codecs[i] & formats )
1620 static void iaxc_handle_connect(struct iax_event * e)
1623 int video_format_capability;
1624 int video_format_preferred;
1626 int video_format = 0;
1630 callno = iaxc_first_free_call();
1634 iaxci_usermsg(IAXC_STATUS,
1635 "%i \n Incoming Call, but no appearances",
1637 // XXX Reject this call!, or just ignore?
1638 //iax_reject(e->session, "Too many calls, we're busy!");
1639 iax_accept(e->session, audio_format_preferred & e->ies.capability);
1640 iax_busy(e->session);
1644 /* negotiate codec */
1645 /* first, try _their_ preferred format */
1646 format = audio_format_capability & e->ies.format;
1649 /* then, try our preferred format */
1650 format = audio_format_preferred & e->ies.capability;
1655 /* finally, see if we have one in common */
1656 format = audio_format_capability & e->ies.capability;
1658 /* now choose amongst these, if we got one */
1661 format = iaxc_choose_codec(format);
1667 iax_reject(e->session, "Could not negotiate common codec");
1672 iaxc_video_format_get_cap(&video_format_preferred,
1673 &video_format_capability);
1675 /* first, see if they even want video */
1676 video_format = (e->ies.format & IAXC_VIDEO_FORMAT_MASK);
1680 /* next, try _their_ preferred format */
1681 video_format &= video_format_capability;
1683 if ( !video_format )
1685 /* then, try our preferred format */
1686 video_format = video_format_preferred &
1687 (e->ies.capability & IAXC_VIDEO_FORMAT_MASK);
1690 if ( !video_format )
1692 /* finally, see if we have one in common */
1693 video_format = video_format_capability &
1694 (e->ies.capability & IAXC_VIDEO_FORMAT_MASK);
1696 /* now choose amongst these, if we got one */
1699 video_format = iaxc_choose_codec(video_format);
1703 /* All video negotiations failed, then warn */
1704 if ( !video_format )
1706 iaxci_usermsg(IAXC_NOTICE,
1707 "Notice: could not negotiate common video codec");
1708 iaxci_usermsg(IAXC_NOTICE,
1709 "Notice: switching to audio-only call");
1712 #endif /* USE_VIDEO */
1714 calls[callno].vformat = video_format;
1715 calls[callno].format = format;
1717 if ( e->ies.called_number )
1718 strncpy(calls[callno].local, e->ies.called_number,
1721 strncpy(calls[callno].local, "unknown",
1724 if ( e->ies.called_context )
1725 strncpy(calls[callno].local_context, e->ies.called_context,
1728 strncpy(calls[callno].local_context, "",
1731 if ( e->ies.calling_number )
1732 strncpy(calls[callno].remote, e->ies.calling_number,
1735 strncpy(calls[callno].remote, "unknown",
1738 if ( e->ies.calling_name )
1739 strncpy(calls[callno].remote_name, e->ies.calling_name,
1742 strncpy(calls[callno].remote_name, "unknown",
1745 iaxc_note_activity(callno);
1746 iaxci_usermsg(IAXC_STATUS, "Call from (%s)", calls[callno].remote);
1748 codec_destroy( callno );
1750 calls[callno].session = e->session;
1751 calls[callno].state = IAXC_CALL_STATE_ACTIVE|IAXC_CALL_STATE_RINGING;
1753 iax_accept(calls[callno].session, format | video_format);
1754 iax_ring_announce(calls[callno].session);
1756 iaxci_do_state_callback(callno);
1758 iaxci_usermsg(IAXC_STATUS, "Incoming call on line %d", callno);
1761 static void service_network()
1763 struct iax_event *e = 0;
1765 struct iaxc_registration *reg;
1767 while ( (e = iax_get_event(0)) )
1770 iaxc_millisleep(0); //fd:
1772 // first, see if this is an event for one of our calls.
1773 callNo = iaxc_find_call_by_session(e->session);
1774 if ( e->etype == IAX_EVENT_NULL )
1776 // Should we do something here?
1777 // Right now we do nothing, just go with the flow
1778 // and let the event be deallocated.
1779 } else if ( callNo >= 0 )
1781 iaxc_handle_network_event(e, callNo);
1782 } else if ( (reg = iaxc_find_registration_by_session(e->session)) != NULL )
1784 iaxc_handle_regreply(e,reg);
1785 } else if ( e->etype == IAX_EVENT_REGACK || e->etype == IAX_EVENT_REGREJ )
1787 iaxci_usermsg(IAXC_ERROR, "Unexpected registration reply");
1788 } else if ( e->etype == IAX_EVENT_REGREQ )
1790 iaxci_usermsg(IAXC_ERROR,
1791 "Registration requested by someone, but we don't understand!");
1792 } else if ( e->etype == IAX_EVENT_CONNECT )
1794 iaxc_handle_connect(e);
1795 } else if ( e->etype == IAX_EVENT_TIMEOUT )
1797 iaxci_usermsg(IAXC_STATUS,
1798 "Timeout for a non-existant session. Dropping",
1802 iaxci_usermsg(IAXC_ERROR,
1803 "Event (type %d) for a non-existant session. Dropping",
1810 EXPORT int iaxc_audio_devices_get(struct iaxc_audio_device **devs, int *nDevs,
1811 int *input, int *output, int *ring)
1816 *devs = audio_driver.devices;
1817 *nDevs = audio_driver.nDevices;
1818 audio_driver.selected_devices(&audio_driver, input, output, ring);
1822 EXPORT int iaxc_audio_devices_set(int input, int output, int ring)
1830 ret = audio_driver.select_devices(&audio_driver, input, output, ring);
1835 EXPORT float iaxc_input_level_get()
1840 return audio_driver.input_level_get(&audio_driver);
1843 EXPORT float iaxc_output_level_get()
1848 return audio_driver.output_level_get(&audio_driver);
1851 EXPORT int iaxc_input_level_set(float level)
1856 return audio_driver.input_level_set(&audio_driver, level);
1859 EXPORT int iaxc_output_level_set(float level)
1864 return audio_driver.output_level_set(&audio_driver, level);
1867 EXPORT int iaxc_play_sound(struct iaxc_sound *s, int ring)
1875 ret = audio_driver.play_sound(s,ring);
1880 EXPORT int iaxc_stop_sound(int id)
1888 ret = audio_driver.stop_sound(id);
1893 EXPORT int iaxc_quelch(int callNo, int MOH)
1895 struct iax_session *session = calls[callNo].session;
1899 return iax_quelch_moh(session, MOH);
1902 EXPORT int iaxc_unquelch(int call)
1904 return iax_unquelch(calls[call].session);
1907 EXPORT int iaxc_mic_boost_get( void )
1909 return audio_driver.mic_boost_get( &audio_driver ) ;
1912 EXPORT int iaxc_mic_boost_set( int enable )
1914 return audio_driver.mic_boost_set( &audio_driver, enable ) ;
1917 EXPORT char* iaxc_version(char * ver)
1922 strncpy(ver, LIBVER, IAXC_EVENT_BUFSIZ);
1926 EXPORT unsigned int iaxc_get_audio_prefs(void)
1931 EXPORT int iaxc_set_audio_prefs(unsigned int prefs)
1933 unsigned int prefs_mask =
1934 IAXC_AUDIO_PREF_RECV_LOCAL_RAW |
1935 IAXC_AUDIO_PREF_RECV_LOCAL_ENCODED |
1936 IAXC_AUDIO_PREF_RECV_REMOTE_RAW |
1937 IAXC_AUDIO_PREF_RECV_REMOTE_ENCODED |
1938 IAXC_AUDIO_PREF_SEND_DISABLE;
1940 if ( prefs & ~prefs_mask )
1943 audio_prefs = prefs;
1947 EXPORT void iaxc_set_test_mode(int tm)
1952 EXPORT int iaxc_push_audio(void *data, unsigned int size, unsigned int samples)
1954 struct iaxc_call *call;
1956 if ( selected_call < 0 )
1959 call = &calls[selected_call];
1961 if ( audio_prefs & IAXC_AUDIO_PREF_SEND_DISABLE )
1964 //fprintf(stderr, "iaxc_push_audio: sending audio size %d\n", size);
1966 if ( iax_send_voice(call->session, call->format, data, size, samples) == -1 )
1968 fprintf(stderr, "iaxc_push_audio: failed to send audio frame of size %d on call %d\n", size, selected_call);
1975 void iaxc_debug_iax_set(int enable)
1977 #ifdef DEBUG_SUPPORT
1981 iax_disable_debug();