Debian Codegraph @

3rdparty/iaxclient/lib/iaxclient_lib.c

raw symbols SCIP
1 /*
2 * iaxclient: a cross-platform IAX softphone library
3 *
4 * Copyrights:
5 * Copyright (C) 2003-2006, Horizon Wimba, Inc.
6 * Copyright (C) 2007, Wimba, Inc.
7 *
8 * Contributors:
9 * Steve Kann <[email protected]>
10 * Michael Van Donselaar <[email protected]>
11 * Shawn Lawrence <[email protected]>
12 * Frik Strecker <[email protected]>
13 * Mihai Balea <mihai AT hates DOT ms>
14 * Peter Grayson <[email protected]>
15 * Bill Cholewka <[email protected]>
16 * Erik Bunce <[email protected]>
17 *
18 * This program is free software, distributed under the terms of
19 * the GNU Lesser (Library) General Public License.
20 */
21
22 #include <assert.h>
23
24 #if defined(WIN32) || defined(_WIN32_WCE)
25 #include <stdlib.h>
26 #endif
27
28 /* Win32 has _vsnprintf instead of vsnprintf */
29 #if ! HAVE_VSNPRINTF
30 # if HAVE__VSNPRINTF
31 # define vsnprintf _vsnprintf
32 # endif
33 #endif
34
35 #include "iaxclient_lib.h"
36 #include "audio_portaudio.h"
37 #include "audio_encode.h"
38 #ifdef USE_VIDEO
39 #include "video.h"
40 #endif
41 #include "iax-client.h"
42 #include "jitterbuf.h"
43
44 #include <stdarg.h>
45
46 #ifdef AUDIO_ALSA
47 #include "audio_alsa.h"
48 #endif
49
50 #ifdef AUDIO_OPENAL
51 #include "audio_openal.h"
52 #endif
53
54 #define IAXC_ERROR IAXC_TEXT_TYPE_ERROR
55 #define IAXC_STATUS IAXC_TEXT_TYPE_STATUS
56 #define IAXC_NOTICE IAXC_TEXT_TYPE_NOTICE
57
58 #define DEFAULT_CALLERID_NAME "Not Available"
59 #define DEFAULT_CALLERID_NUMBER "7005551212"
60
61 #undef JB_DEBUGGING
62
63 /* global test mode flag */
64 int test_mode = 0;
65
66 /* configurable jitterbuffer options */
67 static long jb_target_extra = -1;
68
69 struct iaxc_registration
70 {
71 struct iax_session *session;
72 struct timeval last;
73 char host[256];
74 char user[256];
75 char pass[256];
76 long refresh;
77 int id;
78 struct iaxc_registration *next;
79 };
80
81 static int next_registration_id = 0;
82 static struct iaxc_registration *registrations = NULL;
83
84 static struct iaxc_audio_driver audio_driver;
85
86 static int audio_format_capability;
87 static int audio_format_preferred;
88
89 // Audio callback behavior
90 // By default apps should let iaxclient handle audio
91 static unsigned int audio_prefs = 0;
92
93 void * post_event_handle = NULL;
94 int post_event_id = 0;
95
96 static int minimum_outgoing_framesize = 160; /* 20ms */
97
98 static MUTEX iaxc_lock;
99 static MUTEX event_queue_lock;
100
101 static int iaxci_bound_port = -1;
102
103 // default to use port 4569 unless set by iaxc_set_preferred_source_udp_port
104 static int source_udp_port = IAX_DEFAULT_PORTNO;
105
106 int iaxci_audio_output_mode = 0; // Normal
107
108 int selected_call; // XXX to be protected by mutex?
109 struct iaxc_call* calls;
110 int max_calls; // number of calls for this library session
111
112 static void service_network();
113 static int service_audio();
114
115 /* external global networking replacements */
116 static iaxc_sendto_t iaxc_sendto = (iaxc_sendto_t)sendto;
117 static iaxc_recvfrom_t iaxc_recvfrom = (iaxc_recvfrom_t)recvfrom;
118
119
120 static THREAD main_proc_thread;
121 #if defined(WIN32) || defined(_WIN32_WCE)
122 static THREADID main_proc_thread_id;
123 #endif
124
125 /* 0 running, 1 should quit, -1 not running */
126 static int main_proc_thread_flag = -1;
127
128 static iaxc_event_callback_t iaxc_event_callback = NULL;
129
130 // Internal queue of events, waiting to be posted once the library
131 // lock is released.
132 static iaxc_event *event_queue = NULL;
133
134 // Lock the library
135 static void get_iaxc_lock()
136 {
137 MUTEXLOCK(&iaxc_lock);
138 }
139
140 int try_iaxc_lock()
141 {
142 return MUTEXTRYLOCK(&iaxc_lock);
143 }
144
145 // Unlock the library and post any events that were queued in the meantime
146 void put_iaxc_lock()
147 {
148 iaxc_event *prev, *event;
149
150 MUTEXLOCK(&event_queue_lock);
151 event = event_queue;
152 event_queue = NULL;
153 MUTEXUNLOCK(&event_queue_lock);
154
155 MUTEXUNLOCK(&iaxc_lock);
156
157 while (event)
158 {
159 iaxci_post_event(*event);
160 prev = event;
161 event = event->next;
162 free(prev);
163 }
164 }
165
166 EXPORT void iaxc_set_audio_output(int mode)
167 {
168 iaxci_audio_output_mode = mode;
169 }
170
171 long iaxci_usecdiff(struct timeval * t0, struct timeval * t1)
172 {
173 return (t0->tv_sec - t1->tv_sec) * 1000000L + t0->tv_usec - t1->tv_usec;
174 }
175
176 long iaxci_msecdiff(struct timeval * t0, struct timeval * t1)
177 {
178 return iaxci_usecdiff(t0, t1) / 1000L;
179 }
180
181 EXPORT void iaxc_set_event_callback(iaxc_event_callback_t func)
182 {
183 iaxc_event_callback = func;
184 }
185
186 EXPORT int iaxc_set_event_callpost(void *handle, int id)
187 {
188 post_event_handle = handle;
189 post_event_id = id;
190 iaxc_event_callback = iaxci_post_event_callback;
191 return 0;
192 }
193
194 EXPORT void iaxc_free_event(iaxc_event *e)
195 {
196 free(e);
197 }
198
199 EXPORT struct iaxc_ev_levels *iaxc_get_event_levels(iaxc_event *e)
200 {
201 return &e->ev.levels;
202 }
203
204 EXPORT struct iaxc_ev_text *iaxc_get_event_text(iaxc_event *e)
205 {
206 return &e->ev.text;
207 }
208
209 EXPORT struct iaxc_ev_call_state *iaxc_get_event_state(iaxc_event *e)
210 {
211 return &e->ev.call;
212 }
213
214 // Messaging functions
215 static void default_message_callback(const char * message)
216 {
217 //fprintf(stderr, "IAXCLIENT: %s\n", message);
218 }
219
220 // Post Events back to clients
221 void iaxci_post_event(iaxc_event e)
222 {
223 if ( e.type == 0 )
224 {
225 iaxci_usermsg(IAXC_ERROR,
226 "Error: something posted to us an invalid event");
227 return;
228 }
229
230 if ( MUTEXTRYLOCK(&iaxc_lock) )
231 {
232 iaxc_event **tail;
233
234 /* We could not obtain the lock. Queue the event. */
235 MUTEXLOCK(&event_queue_lock);
236 tail = &event_queue;
237 e.next = NULL;
238 while ( *tail )
239 tail = &(*tail)->next;
240 *tail = (iaxc_event *)malloc(sizeof(iaxc_event));
241 memcpy(*tail, &e, sizeof(iaxc_event));
242 MUTEXUNLOCK(&event_queue_lock);
243 return;
244 }
245
246 /* TODO: This is not the best. Since we were able to get the
247 * lock, we decide that it is okay to go ahead and do the
248 * callback to the application. This is really nasty because
249 * it gives the appearance of serialized callbacks, but in
250 * reality, we could callback an application multiple times
251 * simultaneously. So, as things stand, an application must
252 * do some locking in their callback function to make it
253 * reentrant. Barf. More ideally, iaxclient would guarantee
254 * serialized callbacks to the application.
255 */
256 MUTEXUNLOCK(&iaxc_lock);
257
258 if ( iaxc_event_callback )
259 {
260 int rv;
261
262 rv = iaxc_event_callback(e);
263
264 if ( e.type == IAXC_EVENT_VIDEO )
265 {
266 /* We can free the frame data once it is off the
267 * event queue and has been processed by the client.
268 */
269 free(e.ev.video.data);
270 }
271 else if ( e.type == IAXC_EVENT_AUDIO )
272 {
273 free(e.ev.audio.data);
274 }
275
276 if ( rv < 0 )
277 default_message_callback(
278 "IAXCLIENT: BIG PROBLEM, event callback returned failure!");
279 // > 0 means processed
280 if ( rv > 0 )
281 return;
282
283 // else, fall through to "defaults"
284 }
285
286 switch ( e.type )
287 {
288 case IAXC_EVENT_TEXT:
289 default_message_callback(e.ev.text.message);
290 // others we just ignore too
291 return;
292 }
293 }
294
295
296 void iaxci_usermsg(int type, const char *fmt, ...)
297 {
298 va_list args;
299 iaxc_event e;
300
301 e.type = IAXC_EVENT_TEXT;
302 e.ev.text.type = type;
303 e.ev.text.callNo = -1;
304 va_start(args, fmt);
305 vsnprintf(e.ev.text.message, IAXC_EVENT_BUFSIZ - 1, fmt, args);
306 e.ev.text.message[IAXC_EVENT_BUFSIZ - 1] = '\0';
307 va_end(args);
308
309 iaxci_post_event(e);
310 }
311
312
313 void iaxci_do_levels_callback(float input, float output)
314 {
315 iaxc_event e;
316 e.type = IAXC_EVENT_LEVELS;
317 e.ev.levels.input = input;
318 e.ev.levels.output = output;
319 iaxci_post_event(e);
320 }
321
322 void iaxci_do_state_callback(int callNo)
323 {
324 iaxc_event e;
325 if ( callNo < 0 || callNo >= max_calls )
326 return;
327
328 e.type = IAXC_EVENT_STATE;
329 e.ev.call.callNo = callNo;
330 e.ev.call.state = calls[callNo].state;
331 e.ev.call.format = calls[callNo].format;
332 e.ev.call.vformat = calls[callNo].vformat;
333 strncpy(e.ev.call.remote, calls[callNo].remote, IAXC_EVENT_BUFSIZ - 1);
334 e.ev.call.remote[IAXC_EVENT_BUFSIZ - 1] = '\0';
335 strncpy(e.ev.call.remote_name, calls[callNo].remote_name, IAXC_EVENT_BUFSIZ - 1);
336 e.ev.call.remote_name[IAXC_EVENT_BUFSIZ - 1] = '\0';
337 strncpy(e.ev.call.local, calls[callNo].local, IAXC_EVENT_BUFSIZ - 1);
338 e.ev.call.local[IAXC_EVENT_BUFSIZ - 1] = '\0';
339 strncpy(e.ev.call.local_context, calls[callNo].local_context, IAXC_EVENT_BUFSIZ - 1);
340 e.ev.call.local_context[IAXC_EVENT_BUFSIZ - 1] = '\0';
341 iaxci_post_event(e);
342 }
343
344 void iaxci_do_registration_callback(int id, int reply, int msgcount)
345 {
346 iaxc_event e;
347 e.type = IAXC_EVENT_REGISTRATION;
348 e.ev.reg.id = id;
349 e.ev.reg.reply = reply;
350 e.ev.reg.msgcount = msgcount;
351 iaxci_post_event(e);
352 }
353
354 void iaxci_do_audio_callback(int callNo, unsigned int ts, int source,
355 int encoded, int format, int size, unsigned char *data)
356 {
357 iaxc_event e;
358
359 e.type = IAXC_EVENT_AUDIO;
360 e.ev.audio.ts = ts;
361 e.ev.audio.encoded = encoded;
362 assert(source == IAXC_SOURCE_REMOTE || source == IAXC_SOURCE_LOCAL);
363 e.ev.audio.source = source;
364 e.ev.audio.size = size;
365 e.ev.audio.callNo = callNo;
366 e.ev.audio.format = format;
367
368 e.ev.audio.data = (unsigned char *)malloc(size);
369
370 if ( !e.ev.audio.data )
371 {
372 iaxci_usermsg(IAXC_ERROR,
373 "failed to allocate memory for audio event");
374 return;
375 }
376
377 memcpy(e.ev.audio.data, data, size);
378
379 iaxci_post_event(e);
380 }
381
382 void iaxci_do_dtmf_callback(int callNo, char digit)
383 {
384 iaxc_event e;
385 e.type = IAXC_EVENT_DTMF;
386 e.ev.dtmf.callNo = callNo;
387 e.ev.dtmf.digit = digit;
388 iaxci_post_event(e);
389 }
390
391 static int iaxc_remove_registration_by_id(int id)
392 {
393 struct iaxc_registration *curr, *prev;
394 int count = 0;
395 for ( prev = NULL, curr = registrations; curr != NULL;
396 prev = curr, curr = curr->next )
397 {
398 if ( curr->id == id )
399 {
400 count++;
401 if ( curr->session != NULL )
402 iax_destroy( curr->session );
403 if ( prev != NULL )
404 prev->next = curr->next;
405 else
406 registrations = curr->next;
407 free( curr );
408 break;
409 }
410 }
411 return count;
412 }
413
414 EXPORT int iaxc_first_free_call()
415 {
416 int i;
417 for ( i = 0; i < max_calls; i++ )
418 if ( calls[i].state == IAXC_CALL_STATE_FREE )
419 return i;
420
421 return -1;
422 }
423
424
425 static void iaxc_clear_call(int toDump)
426 {
427 // XXX libiax should handle cleanup, I think..
428 calls[toDump].state = IAXC_CALL_STATE_FREE;
429 calls[toDump].format = 0;
430 calls[toDump].vformat = 0;
431 calls[toDump].session = NULL;
432 iaxci_do_state_callback(toDump);
433 }
434
435 /* select a call. */
436 /* XXX Locking?? Start/stop audio?? */
437 EXPORT int iaxc_select_call(int callNo)
438 {
439 // continue if already selected?
440 //if ( callNo == selected_call ) return;
441
442 if ( callNo >= max_calls )
443 {
444 iaxci_usermsg(IAXC_ERROR, "Error: tried to select out_of_range call %d", callNo);
445 return -1;
446 }
447
448 // callNo < 0 means no call selected (i.e. all on hold)
449 if ( callNo < 0 )
450 {
451 if ( selected_call >= 0 )
452 {
453 calls[selected_call].state &= ~IAXC_CALL_STATE_SELECTED;
454 }
455 selected_call = callNo;
456 return 0;
457 }
458
459 // de-select and notify the old call if not also the new call
460 if ( callNo != selected_call )
461 {
462 if ( selected_call >= 0 )
463 {
464 calls[selected_call].state &= ~IAXC_CALL_STATE_SELECTED;
465 iaxci_do_state_callback(selected_call);
466 }
467 selected_call = callNo;
468 calls[selected_call].state |= IAXC_CALL_STATE_SELECTED;
469 }
470
471 // if it's an incoming call, and ringing, answer it.
472 if ( !(calls[selected_call].state & IAXC_CALL_STATE_OUTGOING) &&
473 (calls[selected_call].state & IAXC_CALL_STATE_RINGING) )
474 {
475 iaxc_answer_call(selected_call);
476 } else
477 {
478 // otherwise just update state (answer does this for us)
479 iaxci_do_state_callback(selected_call);
480 }
481
482 return 0;
483 }
484
485 /* external API accessor */
486 EXPORT int iaxc_selected_call()
487 {
488 return selected_call;
489 }
490
491 EXPORT void iaxc_set_networking(iaxc_sendto_t st, iaxc_recvfrom_t rf)
492 {
493 iaxc_sendto = st;
494 iaxc_recvfrom = rf;
495 }
496
497 EXPORT void iaxc_set_jb_target_extra( long value )
498 {
499 /* store in jb_target_extra, a static global */
500 jb_target_extra = value;
501 }
502
503 static void jb_errf(const char *fmt, ...)
504 {
505 va_list args;
506 char buf[1024];
507
508 va_start(args, fmt);
509 vsnprintf(buf, 1024, fmt, args);
510 va_end(args);
511
512 iaxci_usermsg(IAXC_ERROR, buf);
513 }
514
515 static void jb_warnf(const char *fmt, ...)
516 {
517 va_list args;
518 char buf[1024];
519
520 va_start(args, fmt);
521 vsnprintf(buf, 1024, fmt, args);
522 va_end(args);
523
524 iaxci_usermsg(IAXC_NOTICE, buf);
525 }
526
527 #ifdef JB_DEBUGGING
528 static void jb_dbgf(const char *fmt, ...)
529 {
530 va_list args;
531
532 va_start(args, fmt);
533 vfprintf(stderr, fmt, args);
534 va_end(args);
535 }
536 #endif
537
538 static void setup_jb_output()
539 {
540 #ifdef JB_DEBUGGING
541 jb_setoutput(jb_errf, jb_warnf, jb_dbgf);
542 #else
543 jb_setoutput(jb_errf, jb_warnf, NULL);
544 #endif
545 }
546
547 // Note: Must be called before iaxc_initialize()
548 EXPORT void iaxc_set_preferred_source_udp_port(int port)
549 {
550 source_udp_port = port;
551 }
552
553 /* For "slow" systems. See iax.c code */
554 EXPORT int iaxc_video_bypass_jitter(int mode)
555 {
556 /* TODO:
557 * 1. When switching from jitter to no-jitter the buffer must be
558 * flushed of queued video packet and must be sent a key-frame
559 * to redraw the screen (partially done).
560 * 2. When switching from no-jitter to jitter we must drop all
561 * enqueued events prior the mode change (must be touched
562 * iax_sched_del and iax_get_event).
563 */
564 return iax_video_bypass_jitter(calls[selected_call].session,mode);
565 }
566
567 EXPORT int iaxc_get_bind_port()
568 {
569 return iaxci_bound_port;
570 }
571
572 EXPORT int iaxc_initialize(int num_calls)
573 {
574 int i;
575 int port;
576
577 os_init();
578
579 setup_jb_output();
580
581 MUTEXINIT(&iaxc_lock);
582 MUTEXINIT(&event_queue_lock);
583
584 iaxc_set_audio_prefs(0);
585
586 if ( iaxc_recvfrom != (iaxc_recvfrom_t)recvfrom )
587 iax_set_networking(iaxc_sendto, iaxc_recvfrom);
588
589 /* Note that iax_init() only sets up the receive port when the
590 * sendto/recvfrom functions have not been replaced. We need
591 * to call iaxc_init in either case because there is other
592 * initialization beyond the socket setup that needs to be done.
593 */
594 if ( (port = iax_init(source_udp_port)) < 0 )
595 {
596 iaxci_usermsg(IAXC_ERROR,
597 "Fatal error: failed to initialize iax with port %d",
598 port);
599 return -1;
600 }
601
602 if ( iaxc_recvfrom == (iaxc_recvfrom_t)recvfrom )
603 iaxci_bound_port = port;
604 else
605 iaxci_bound_port = -1;
606
607 /* tweak the jitterbuffer settings */
608 iax_set_jb_target_extra( jb_target_extra );
609
610 max_calls = num_calls;
611 /* initialize calls */
612 if ( max_calls <= 0 )
613 max_calls = 1; /* 0 == Default? */
614
615 /* calloc zeroes for us */
616 calls = (struct iaxc_call *)calloc(sizeof(struct iaxc_call), max_calls);
617 if ( !calls )
618 {
619 iaxci_usermsg(IAXC_ERROR, "Fatal error: can't allocate memory");
620 return -1;
621 }
622
623 selected_call = -1;
624
625 for ( i = 0; i < max_calls; i++ )
626 {
627 strncpy(calls[i].callerid_name, DEFAULT_CALLERID_NAME, IAXC_EVENT_BUFSIZ - 1);
628 calls[i].callerid_name[IAXC_EVENT_BUFSIZ - 1] = '\0';
629 strncpy(calls[i].callerid_number, DEFAULT_CALLERID_NUMBER, IAXC_EVENT_BUFSIZ - 1);
630 calls[i].callerid_number[IAXC_EVENT_BUFSIZ - 1] = '\0';
631 }
632
633 if ( !test_mode )
634 {
635 #ifdef AUDIO_PA
636 if ( pa_initialize(&audio_driver, 8000) )
637 {
638 iaxci_usermsg(IAXC_ERROR, "failed pa_initialize");
639 return -1;
640 }
641 #endif
642 #ifdef AUDIO_ALSA
643 /* TODO: It is unknown whether this stuff for direct access to
644 * alsa should be left in iaxclient. We're leaving it in here for
645 * the time being, but unless it becomes clear that someone cares
646 * about having it, it will be removed. Also note that portaudio
647 * is capable of using alsa. This is another reason why this
648 * direct alsa access may be unneeded.
649 */
650 if ( alsa_initialize(&audio_driver, 8000) )
651 return -1;
652 #endif
653 #ifdef AUDIO_OPENAL
654 if ( openal_initialize(&audio_driver, 8000) )
655 {
656 iaxci_usermsg(IAXC_ERROR, "failed openal_initialize");
657 return -1;
658 }
659 #endif
660 }
661 #ifdef USE_VIDEO
662 if ( video_initialize() )
663 iaxci_usermsg(IAXC_ERROR,
664 "iaxc_initialize: cannot initialize video!\n");
665 #endif
666
667 /* Default audio format capabilities */
668 audio_format_capability =
669 IAXC_FORMAT_ULAW |
670 IAXC_FORMAT_ALAW |
671 #ifdef CODEC_GSM
672 IAXC_FORMAT_GSM |
673 #endif
674 IAXC_FORMAT_SPEEX;
675 audio_format_preferred = IAXC_FORMAT_SPEEX;
676
677 return 0;
678 }
679
680 EXPORT void iaxc_shutdown()
681 {
682 iaxc_dump_all_calls();
683
684 get_iaxc_lock();
685
686 if ( !test_mode )
687 {
688 audio_driver.destroy(&audio_driver);
689 #ifdef USE_VIDEO
690 video_destroy();
691 #endif
692 }
693
694 /* destroy enocders and decoders for all existing calls */
695 if ( calls )
696 {
697 int i;
698 for ( i=0 ; i<max_calls ; i++ )
699 {
700 if ( calls[i].encoder )
701 calls[i].encoder->destroy(calls[i].encoder);
702 if ( calls[i].decoder )
703 calls[i].decoder->destroy(calls[i].decoder);
704 if ( calls[i].vencoder )
705 calls[i].vencoder->destroy(calls[i].vencoder);
706 if ( calls[i].vdecoder )
707 calls[i].vdecoder->destroy(calls[i].vdecoder);
708 }
709 free(calls);
710 calls = NULL;
711 }
712 put_iaxc_lock();
713 #ifdef WIN32
714 //closesocket(iax_get_fd()); //fd:
715 #endif
716
717 free(calls);
718
719 MUTEXDESTROY(&event_queue_lock);
720 MUTEXDESTROY(&iaxc_lock);
721 }
722
723
724 EXPORT void iaxc_set_formats(int preferred, int allowed)
725 {
726 audio_format_capability = allowed;
727 audio_format_preferred = preferred;
728 }
729
730 EXPORT void iaxc_set_min_outgoing_framesize(int samples)
731 {
732 minimum_outgoing_framesize = samples;
733 }
734
735 EXPORT void iaxc_set_callerid(const char * name, const char * number)
736 {
737 int i;
738
739 for ( i = 0; i < max_calls; i++ )
740 {
741 strncpy(calls[i].callerid_name, name, IAXC_EVENT_BUFSIZ - 1);
742 calls[i].callerid_name[IAXC_EVENT_BUFSIZ - 1] = '\0';
743 strncpy(calls[i].callerid_number, number, IAXC_EVENT_BUFSIZ - 1);
744 calls[i].callerid_number[IAXC_EVENT_BUFSIZ - 1] = '\0';
745 }
746 }
747
748 static void iaxc_note_activity(int callNo)
749 {
750 if ( callNo < 0 )
751 return;
752 calls[callNo].last_activity = iax_tvnow();
753 }
754
755 static void iaxc_refresh_registrations()
756 {
757 struct iaxc_registration *cur;
758 struct timeval now;
759
760 now = iax_tvnow();
761
762 for ( cur = registrations; cur != NULL; cur = cur->next )
763 {
764 // If there is less than three seconds before the registration is about
765 // to expire, renew it.
766 if ( iaxci_usecdiff(&now, &cur->last) > (cur->refresh - 3) * 1000 *1000 )
767 {
768 if ( cur->session != NULL )
769 {
770 iax_destroy( cur->session );
771 }
772 cur->session = iax_session_new();
773 if ( !cur->session )
774 {
775 iaxci_usermsg(IAXC_ERROR, "Can't make new registration session");
776 return;
777 }
778 iax_register(cur->session, cur->host, cur->user, cur->pass, cur->refresh);
779 cur->last = now;
780 }
781 }
782 }
783
784 #define LOOP_SLEEP 5 // In ms
785 static THREADFUNCDECL(main_proc_thread_func)
786 {
787 static int refresh_registration_count = 0;
788
789 THREADFUNCRET(ret);
790
791 /* Increase Priority */
792 iaxci_prioboostbegin();
793
794 while ( !main_proc_thread_flag )
795 {
796 get_iaxc_lock();
797
798 service_network();
799 if ( !test_mode )
800 service_audio();
801
802 // Check registration refresh once a second
803 if ( refresh_registration_count++ > 1000/LOOP_SLEEP )
804 {
805 iaxc_refresh_registrations();
806 refresh_registration_count = 0;
807 }
808
809 put_iaxc_lock();
810
811 iaxc_millisleep(LOOP_SLEEP);
812 }
813
814 /* Decrease priority */
815 iaxci_prioboostend();
816
817 main_proc_thread_flag = -1;
818
819 return ret;
820 }
821
822 EXPORT int iaxc_start_processing_thread()
823 {
824 main_proc_thread_flag = 0;
825
826 if ( THREADCREATE(main_proc_thread_func, NULL, main_proc_thread,
827 main_proc_thread_id) == THREADCREATE_ERROR)
828 return -1;
829
830 return 0;
831 }
832
833 EXPORT int iaxc_stop_processing_thread()
834 {
835 if ( main_proc_thread_flag >= 0 )
836 {
837 main_proc_thread_flag = 1;
838 THREADJOIN(main_proc_thread);
839 }
840
841 return 0;
842 }
843
844 static int service_audio()
845 {
846 /* TODO: maybe we shouldn't allocate 8kB on the stack here. */
847 short buf [4096];
848
849 int want_send_audio =
850 selected_call >= 0 &&
851 ((calls[selected_call].state & IAXC_CALL_STATE_OUTGOING) ||
852 (calls[selected_call].state & IAXC_CALL_STATE_COMPLETE))
853 && !(audio_prefs & IAXC_AUDIO_PREF_SEND_DISABLE);
854
855 int want_local_audio =
856 (audio_prefs & IAXC_AUDIO_PREF_RECV_LOCAL_RAW) ||
857 (audio_prefs & IAXC_AUDIO_PREF_RECV_LOCAL_ENCODED);
858
859 if ( want_local_audio || want_send_audio )
860 {
861 for ( ;; )
862 {
863 int to_read;
864 int cmin;
865
866 audio_driver.start(&audio_driver);
867
868 /* use codec minimum if higher */
869 cmin = want_send_audio && calls[selected_call].encoder ?
870 calls[selected_call].encoder->minimum_frame_size :
871 1;
872
873 to_read = cmin > minimum_outgoing_framesize ?
874 cmin : minimum_outgoing_framesize;
875
876 /* Round up to the next multiple */
877 if ( to_read % cmin )
878 to_read += cmin - (to_read % cmin);
879
880 if ( to_read > (int)(sizeof(buf) / sizeof(short)) )
881 {
882 fprintf(stderr,
883 "internal error: to_read > sizeof(buf)\n");
884 exit(1);
885 }
886
887 /* Currently pa gives us either all the bits we ask for or none */
888 if ( audio_driver.input(&audio_driver, buf, &to_read) )
889 {
890 iaxci_usermsg(IAXC_ERROR, "ERROR reading audio\n");
891 break;
892 }
893
894 /* Frame was not available */
895 if ( !to_read )
896 break;
897
898 if ( audio_prefs & IAXC_AUDIO_PREF_RECV_LOCAL_RAW )
899 iaxci_do_audio_callback(selected_call, 0,
900 IAXC_SOURCE_LOCAL, 0, 0,
901 to_read * 2, (unsigned char *)buf);
902
903 if ( want_send_audio )
904 audio_send_encoded_audio(&calls[selected_call],
905 selected_call, buf,
906 calls[selected_call].format &
907 IAXC_AUDIO_FORMAT_MASK,
908 to_read);
909 }
910 }
911 else
912 {
913 static int i = 0;
914
915 audio_driver.stop(&audio_driver);
916
917 /*!
918 \deprecated
919 Q: Why do we continuously send IAXC_EVENT_LEVELS events
920 when there is no selected call?
921
922 A: So that certain users of iaxclient do not have to
923 reset their vu meters when a call ends -- they can just
924 count on getting level callbacks. This is a bit of a hack
925 so any applications relying on this behavior should maybe
926 be changed.
927 */
928 if ( i++ % 50 == 0 )
929 iaxci_do_levels_callback(AUDIO_ENCODE_SILENCE_DB,
930 AUDIO_ENCODE_SILENCE_DB);
931 }
932
933 return 0;
934 }
935
936 /* handle IAX text events */
937 static void handle_text_event(struct iax_event *e, int callNo)
938 {
939 iaxc_event ev;
940 int len;
941
942 if ( callNo < 0 )
943 return;
944
945 memset(&ev, 0, sizeof(iaxc_event));
946 ev.type = IAXC_EVENT_TEXT;
947 ev.ev.text.type = IAXC_TEXT_TYPE_IAX;
948 ev.ev.text.callNo = callNo;
949
950 len = e->datalen <= IAXC_EVENT_BUFSIZ - 1 ? e->datalen : IAXC_EVENT_BUFSIZ - 1;
951 strncpy(ev.ev.text.message, (char *) e->data, len);
952 iaxci_post_event(ev);
953 }
954
955 /* handle IAX URL events */
956 void handle_url_event( struct iax_event *e, int callNo )
957 {
958 iaxc_event ev;
959
960 if ( callNo < 0 )
961 return;
962
963 ev.ev.url.callNo = callNo;
964 ev.type = IAXC_EVENT_URL;
965 strcpy( ev.ev.url.url, "" );
966
967 switch ( e->subclass )
968 {
969 case AST_HTML_URL:
970 ev.ev.url.type = IAXC_URL_URL;
971 if ( e->datalen )
972 {
973 if ( e->datalen > IAXC_EVENT_BUFSIZ )
974 {
975 fprintf( stderr, "ERROR: URL too long %d > %d\n",
976 e->datalen, IAXC_EVENT_BUFSIZ );
977 } else
978 {
979 strncpy( ev.ev.url.url, (char *) e->data, e->datalen );
980 }
981 }
982 /* fprintf( stderr, "URL:%s\n", ev.ev.url.url ); */
983 break;
984 case AST_HTML_LINKURL:
985 ev.ev.url.type = IAXC_URL_LINKURL;
986 /* fprintf( stderr, "LINKURL event\n" ); */
987 break;
988 case AST_HTML_LDCOMPLETE:
989 ev.ev.url.type = IAXC_URL_LDCOMPLETE;
990 /* fprintf( stderr, "LDCOMPLETE event\n" ); */
991 break;
992 case AST_HTML_UNLINK:
993 ev.ev.url.type = IAXC_URL_UNLINK;
994 /* fprintf( stderr, "UNLINK event\n" ); */
995 break;
996 case AST_HTML_LINKREJECT:
997 ev.ev.url.type = IAXC_URL_LINKREJECT;
998 /* fprintf( stderr, "LINKREJECT event\n" ); */
999 break;
1000 default:
1001 fprintf( stderr, "Unknown URL event %d\n", e->subclass );
1002 break;
1003 }
1004 iaxci_post_event( ev );
1005 }
1006
1007 /* DANGER: bad things can happen if iaxc_netstat != iax_netstat.. */
1008 EXPORT int iaxc_get_netstats(int call, int *rtt, struct iaxc_netstat *local,
1009 struct iaxc_netstat *remote)
1010 {
1011 return iax_get_netstats(calls[call].session, rtt,
1012 (struct iax_netstat *)local,
1013 (struct iax_netstat *)remote);
1014 }
1015
1016 /* handle IAX text events */
1017 static void generate_netstat_event(int callNo)
1018 {
1019 iaxc_event ev;
1020
1021 if ( callNo < 0 )
1022 return;
1023
1024 ev.type = IAXC_EVENT_NETSTAT;
1025 ev.ev.netstats.callNo = callNo;
1026
1027 /* only post the event if the session is valid, etc */
1028 if ( !iaxc_get_netstats(callNo, &ev.ev.netstats.rtt,
1029 &ev.ev.netstats.local, &ev.ev.netstats.remote))
1030 iaxci_post_event(ev);
1031 }
1032
1033 static void handle_audio_event(struct iax_event *e, int callNo)
1034 {
1035 int total_consumed = 0;
1036 short fr[4096];
1037 const int fr_samples = sizeof(fr) / sizeof(short);
1038 int samples, format;
1039 #ifdef WIN32
1040 int cycles_max = 100; //fd:
1041 #endif
1042 struct iaxc_call *call;
1043
1044 if ( callNo < 0 )
1045 return;
1046
1047 call = &calls[callNo];
1048
1049 if ( callNo != selected_call )
1050 {
1051 /* drop audio for unselected call? */
1052 return;
1053 }
1054
1055 samples = fr_samples;
1056 format = call->format & IAXC_AUDIO_FORMAT_MASK;
1057
1058 do
1059 {
1060 int bytes_decoded;
1061
1062 int mainbuf_delta = fr_samples - samples;
1063
1064 bytes_decoded = audio_decode_audio(call,
1065 fr,
1066 e->data + total_consumed,
1067 e->datalen - total_consumed,
1068 format,
1069 &samples);
1070
1071 if ( bytes_decoded < 0 )
1072 {
1073 iaxci_usermsg(IAXC_STATUS,
1074 "Bad or incomplete voice packet. Unable to decode. dropping");
1075 return;
1076 }
1077
1078 /* Pass encoded audio back to the app if required */
1079 if ( audio_prefs & IAXC_AUDIO_PREF_RECV_REMOTE_ENCODED )
1080 iaxci_do_audio_callback(callNo, e->ts, IAXC_SOURCE_REMOTE,
1081 1, format & IAXC_AUDIO_FORMAT_MASK,
1082 e->datalen - total_consumed,
1083 e->data + total_consumed);
1084
1085 #ifdef WIN32
1086 //fd: start: for some reason it loops here. Try to avoid it
1087 cycles_max--;
1088 if ( cycles_max < 0 )
1089 {
1090 iaxc_millisleep(0);
1091 }
1092 //fd: end
1093 #endif
1094 total_consumed += bytes_decoded;
1095 if ( audio_prefs & IAXC_AUDIO_PREF_RECV_REMOTE_RAW )
1096 {
1097 // audio_decode_audio returns the number of samples.
1098 // We are using 16 bit samples, so we need to double
1099 // the number to obtain the size in bytes.
1100 // format will also be 0 since this is raw audio
1101 int size = (fr_samples - samples - mainbuf_delta) * 2;
1102 iaxci_do_audio_callback(callNo, e->ts, IAXC_SOURCE_REMOTE,
1103 0, 0, size, (unsigned char *)fr);
1104 }
1105
1106 if ( iaxci_audio_output_mode )
1107 continue;
1108
1109 if ( !test_mode )
1110 audio_driver.output(&audio_driver, fr,
1111 fr_samples - samples - mainbuf_delta);
1112
1113 } while ( total_consumed < e->datalen );
1114 }
1115
1116 #ifdef USE_VIDEO
1117 static void handle_video_event(struct iax_event *e, int callNo)
1118 {
1119 struct iaxc_call *call;
1120
1121 if ( callNo < 0 )
1122 return;
1123
1124 if ( e->datalen == 0 )
1125 {
1126 iaxci_usermsg(IAXC_STATUS, "Received 0-size packet. Unable to decode.");
1127 return;
1128 }
1129
1130 call = &calls[callNo];
1131
1132 if ( callNo != selected_call )
1133 {
1134 /* drop video for unselected call? */
1135 return;
1136 }
1137
1138 if ( call->vformat )
1139 {
1140 if ( video_recv_video(call, selected_call, e->data,
1141 e->datalen, e->ts, call->vformat) < 0 )
1142 {
1143 iaxci_usermsg(IAXC_STATUS,
1144 "Bad or incomplete video packet. Unable to decode.");
1145 return;
1146 }
1147 }
1148 }
1149 #endif /* USE_VIDEO */
1150
1151 static void iaxc_handle_network_event(struct iax_event *e, int callNo)
1152 {
1153 if ( callNo < 0 )
1154 return;
1155
1156 iaxc_note_activity(callNo);
1157
1158 switch ( e->etype )
1159 {
1160 case IAX_EVENT_NULL:
1161 break;
1162 case IAX_EVENT_HANGUP:
1163 iaxci_usermsg(IAXC_STATUS, "Call disconnected by remote");
1164 // XXX does the session go away now?
1165 iaxc_clear_call(callNo);
1166 break;
1167 case IAX_EVENT_REJECT:
1168 iaxci_usermsg(IAXC_STATUS, "Call rejected by remote");
1169 iaxc_clear_call(callNo);
1170 break;
1171 case IAX_EVENT_ACCEPT:
1172 calls[callNo].format = e->ies.format & IAXC_AUDIO_FORMAT_MASK;
1173 calls[callNo].vformat = e->ies.format & IAXC_VIDEO_FORMAT_MASK;
1174 #ifdef USE_VIDEO
1175 if ( !(e->ies.format & IAXC_VIDEO_FORMAT_MASK) )
1176 {
1177 iaxci_usermsg(IAXC_NOTICE,
1178 "Failed video codec negotiation.");
1179 }
1180 #endif
1181 iaxci_usermsg(IAXC_STATUS,"Call %d accepted", callNo);
1182 break;
1183 case IAX_EVENT_ANSWER:
1184 calls[callNo].state &= ~IAXC_CALL_STATE_RINGING;
1185 calls[callNo].state |= IAXC_CALL_STATE_COMPLETE;
1186 iaxci_do_state_callback(callNo);
1187 iaxci_usermsg(IAXC_STATUS,"Call %d answered", callNo);
1188 //iaxc_answer_call(callNo);
1189 // notify the user?
1190 break;
1191 case IAX_EVENT_BUSY:
1192 calls[callNo].state &= ~IAXC_CALL_STATE_RINGING;
1193 calls[callNo].state |= IAXC_CALL_STATE_BUSY;
1194 iaxci_do_state_callback(callNo);
1195 iaxci_usermsg(IAXC_STATUS, "Call %d busy", callNo);
1196 break;
1197 case IAX_EVENT_VOICE:
1198 handle_audio_event(e, callNo);
1199 if ( (calls[callNo].state & IAXC_CALL_STATE_OUTGOING) &&
1200 (calls[callNo].state & IAXC_CALL_STATE_RINGING) )
1201 {
1202 calls[callNo].state &= ~IAXC_CALL_STATE_RINGING;
1203 calls[callNo].state |= IAXC_CALL_STATE_COMPLETE;
1204 iaxci_do_state_callback(callNo);
1205 iaxci_usermsg(IAXC_STATUS,"Call %d progress",
1206 callNo);
1207 }
1208 break;
1209 #ifdef USE_VIDEO
1210 case IAX_EVENT_VIDEO:
1211 handle_video_event(e, callNo);
1212 break;
1213 #endif
1214 case IAX_EVENT_TEXT:
1215 handle_text_event(e, callNo);
1216 break;
1217 case IAX_EVENT_RINGA:
1218 calls[callNo].state |= IAXC_CALL_STATE_RINGING;
1219 iaxci_do_state_callback(callNo);
1220 iaxci_usermsg(IAXC_STATUS,"Call %d ringing", callNo);
1221 break;
1222 case IAX_EVENT_PONG:
1223 generate_netstat_event(callNo);
1224 break;
1225 case IAX_EVENT_URL:
1226 handle_url_event(e, callNo);
1227 break;
1228 case IAX_EVENT_CNG:
1229 /* ignore? */
1230 break;
1231 case IAX_EVENT_TIMEOUT:
1232 iax_hangup(e->session, "Call timed out");
1233 iaxci_usermsg(IAXC_STATUS, "Call %d timed out.", callNo);
1234 iaxc_clear_call(callNo);
1235 break;
1236 case IAX_EVENT_TRANSFER:
1237 calls[callNo].state |= IAXC_CALL_STATE_TRANSFER;
1238 iaxci_do_state_callback(callNo);
1239 iaxci_usermsg(IAXC_STATUS,"Call %d transfer released", callNo);
1240 break;
1241 case IAX_EVENT_DTMF:
1242 iaxci_do_dtmf_callback(callNo,e->subclass);
1243 iaxci_usermsg(IAXC_STATUS, "DTMF digit %c received", e->subclass);
1244 break;
1245 default:
1246 iaxci_usermsg(IAXC_STATUS, "Unknown event: %d for call %d", e->etype, callNo);
1247 break;
1248 }
1249 }
1250
1251 EXPORT int iaxc_unregister( int id )
1252 {
1253 int count = 0;
1254 get_iaxc_lock();
1255 count = iaxc_remove_registration_by_id(id);
1256 put_iaxc_lock();
1257 return count;
1258 }
1259
1260 EXPORT int iaxc_register(const char * user, const char * pass, const char * host)
1261 {
1262 return iaxc_register_ex(user, pass, host, 60);
1263 }
1264
1265 EXPORT int iaxc_register_ex(const char * user, const char * pass, const char * host, int refresh)
1266 {
1267 struct iaxc_registration *newreg;
1268
1269 newreg = (struct iaxc_registration *)malloc(sizeof (struct iaxc_registration));
1270 if ( !newreg )
1271 {
1272 iaxci_usermsg(IAXC_ERROR, "Can't make new registration");
1273 return -1;
1274 }
1275
1276 get_iaxc_lock();
1277 newreg->session = iax_session_new();
1278 if ( !newreg->session )
1279 {
1280 iaxci_usermsg(IAXC_ERROR, "Can't make new registration session");
1281 put_iaxc_lock();
1282 return -1;
1283 }
1284
1285 newreg->last = iax_tvnow();
1286 newreg->refresh = refresh;
1287
1288 strncpy(newreg->host, host, 255);
1289 newreg->host[255] = '\0';
1290 strncpy(newreg->user, user, 255);
1291 newreg->user[255] = '\0';
1292 strncpy(newreg->pass, pass, 255);
1293 newreg->pass[255] = '\0';
1294
1295 /* send out the initial registration with refresh seconds */
1296 iax_register(newreg->session, host, user, pass, refresh);
1297
1298 /* add it to the list; */
1299 newreg->id = ++next_registration_id;
1300 newreg->next = registrations;
1301 registrations = newreg;
1302
1303 put_iaxc_lock();
1304 return newreg->id;
1305 }
1306
1307 static void codec_destroy( int callNo )
1308 {
1309 if ( calls[callNo].encoder )
1310 {
1311 calls[callNo].encoder->destroy( calls[callNo].encoder );
1312 calls[callNo].encoder = NULL;
1313 }
1314 if ( calls[callNo].decoder )
1315 {
1316 calls[callNo].decoder->destroy( calls[callNo].decoder );
1317 calls[callNo].decoder = NULL;
1318 }
1319 if ( calls[callNo].vdecoder )
1320 {
1321 calls[callNo].vdecoder->destroy(calls[callNo].vdecoder);
1322 calls[callNo].vdecoder = NULL;
1323 }
1324 if ( calls[callNo].vencoder )
1325 {
1326 calls[callNo].vencoder->destroy(calls[callNo].vencoder);
1327 calls[callNo].vencoder = NULL;
1328 }
1329 }
1330
1331 EXPORT int iaxc_call(const char * num)
1332 {
1333 return iaxc_call_ex(num, NULL, NULL, 1);
1334 }
1335
1336 EXPORT int iaxc_call_ex(const char *num, const char* callerid_name, const char* callerid_number, int video)
1337 {
1338 int video_format_capability = 0;
1339 int video_format_preferred = 0;
1340 int callNo = -1;
1341 struct iax_session *newsession;
1342 char *ext = strstr(num, "/");
1343
1344 get_iaxc_lock();
1345
1346 // if no call is selected, get a new appearance
1347 if ( selected_call < 0 )
1348 {
1349 callNo = iaxc_first_free_call();
1350 } else
1351 {
1352 // use selected call if not active, otherwise, get a new appearance
1353 if ( calls[selected_call].state & IAXC_CALL_STATE_ACTIVE )
1354 {
1355 callNo = iaxc_first_free_call();
1356 } else
1357 {
1358 callNo = selected_call;
1359 }
1360 }
1361
1362 if ( callNo < 0 )
1363 {
1364 iaxci_usermsg(IAXC_STATUS, "No free call appearances");
1365 goto iaxc_call_bail;
1366 }
1367
1368 newsession = iax_session_new();
1369 if ( !newsession )
1370 {
1371 iaxci_usermsg(IAXC_ERROR, "Can't make new session");
1372 goto iaxc_call_bail;
1373 }
1374
1375 calls[callNo].session = newsession;
1376
1377 codec_destroy( callNo );
1378
1379 if ( ext )
1380 {
1381 strncpy(calls[callNo].remote_name, num, IAXC_EVENT_BUFSIZ - 1);
1382 strncpy(calls[callNo].remote, ++ext, IAXC_EVENT_BUFSIZ - 1);
1383 } else
1384 {
1385 strncpy(calls[callNo].remote_name, num, IAXC_EVENT_BUFSIZ - 1);
1386 strncpy(calls[callNo].remote, "" , IAXC_EVENT_BUFSIZ - 1);
1387 }
1388 calls[callNo].remote_name[IAXC_EVENT_BUFSIZ - 1] = '\0';
1389 calls[callNo].remote[IAXC_EVENT_BUFSIZ - 1] = '\0';
1390
1391 if ( callerid_number != NULL ) {
1392 strncpy(calls[callNo].callerid_number, callerid_number, IAXC_EVENT_BUFSIZ - 1);
1393 calls[callNo].callerid_number[IAXC_EVENT_BUFSIZ - 1] = '\0';
1394 }
1395
1396 if ( callerid_name != NULL ) {
1397 strncpy(calls[callNo].callerid_name, callerid_name, IAXC_EVENT_BUFSIZ - 1);
1398 calls[callNo].callerid_name[IAXC_EVENT_BUFSIZ - 1] = '\0';
1399 }
1400
1401 strncpy(calls[callNo].local , calls[callNo].callerid_name, IAXC_EVENT_BUFSIZ - 1);
1402 calls[callNo].local[IAXC_EVENT_BUFSIZ - 1] = '\0';
1403
1404 strncpy(calls[callNo].local_context, "default", IAXC_EVENT_BUFSIZ - 1);
1405 calls[callNo].local_context[IAXC_EVENT_BUFSIZ - 1] = '\0';
1406
1407 calls[callNo].state = IAXC_CALL_STATE_ACTIVE | IAXC_CALL_STATE_OUTGOING;
1408
1409 /* reset activity and ping "timers" */
1410 iaxc_note_activity(callNo);
1411 calls[callNo].last_ping = calls[callNo].last_activity;
1412
1413 #ifdef USE_VIDEO
1414 if ( video )
1415 iaxc_video_format_get_cap(&video_format_preferred, &video_format_capability);
1416 #endif
1417
1418 iaxci_usermsg(IAXC_NOTICE, "Originating an %s call",
1419 video_format_preferred ? "audio+video" : "audio only");
1420 iax_call(calls[callNo].session, calls[callNo].callerid_number,
1421 calls[callNo].callerid_name, num, NULL, 0,
1422 audio_format_preferred | video_format_preferred,
1423 audio_format_capability | video_format_capability);
1424
1425 // does state stuff also
1426 iaxc_select_call(callNo);
1427
1428 iaxc_call_bail:
1429 put_iaxc_lock();
1430
1431 return callNo;
1432 }
1433
1434 EXPORT void iaxc_send_busy_on_incoming_call(int callNo)
1435 {
1436 if ( callNo < 0 )
1437 return;
1438
1439 iax_busy(calls[callNo].session);
1440 }
1441
1442 EXPORT void iaxc_answer_call(int callNo)
1443 {
1444 if ( callNo < 0 )
1445 return;
1446
1447 calls[callNo].state |= IAXC_CALL_STATE_COMPLETE;
1448 calls[callNo].state &= ~IAXC_CALL_STATE_RINGING;
1449 iax_answer(calls[callNo].session);
1450 iaxci_do_state_callback(callNo);
1451 }
1452
1453 EXPORT void iaxc_blind_transfer_call(int callNo, const char * dest_extension)
1454 {
1455 if ( callNo < 0 || !(calls[callNo].state & IAXC_CALL_STATE_ACTIVE) )
1456 return;
1457
1458 iax_transfer(calls[callNo].session, dest_extension);
1459 }
1460
1461 EXPORT void iaxc_setup_call_transfer(int sourceCallNo, int targetCallNo)
1462 {
1463 if ( sourceCallNo < 0 || targetCallNo < 0 ||
1464 !(calls[sourceCallNo].state & IAXC_CALL_STATE_ACTIVE) ||
1465 !(calls[targetCallNo].state & IAXC_CALL_STATE_ACTIVE) )
1466 return;
1467
1468 iax_setup_transfer(calls[sourceCallNo].session, calls[targetCallNo].session);
1469 }
1470
1471 static void iaxc_dump_one_call(int callNo)
1472 {
1473 if ( callNo < 0 )
1474 return;
1475 if ( calls[callNo].state == IAXC_CALL_STATE_FREE )
1476 return;
1477
1478 iax_hangup(calls[callNo].session,"Dumped Call");
1479 iaxci_usermsg(IAXC_STATUS, "Hanging up call %d", callNo);
1480 iaxc_clear_call(callNo);
1481 }
1482
1483 EXPORT void iaxc_dump_all_calls(void)
1484 {
1485 int callNo;
1486 get_iaxc_lock();
1487 for ( callNo = 0; callNo < max_calls; callNo++ )
1488 iaxc_dump_one_call(callNo);
1489 put_iaxc_lock();
1490 }
1491
1492
1493 EXPORT void iaxc_dump_call_number( int callNo )
1494 {
1495 if ( ( callNo >= 0 ) && ( callNo < max_calls ) )
1496 {
1497 get_iaxc_lock();
1498 iaxc_dump_one_call(callNo);
1499 put_iaxc_lock();
1500 }
1501 }
1502
1503 EXPORT void iaxc_dump_call(void)
1504 {
1505 if ( selected_call >= 0 )
1506 {
1507 get_iaxc_lock();
1508 iaxc_dump_one_call(selected_call);
1509 put_iaxc_lock();
1510 }
1511 }
1512
1513 EXPORT void iaxc_reject_call(void)
1514 {
1515 if ( selected_call >= 0 )
1516 {
1517 iaxc_reject_call_number(selected_call);
1518 }
1519 }
1520
1521 EXPORT void iaxc_reject_call_number( int callNo )
1522 {
1523 if ( ( callNo >= 0 ) && ( callNo < max_calls ) )
1524 {
1525 get_iaxc_lock();
1526 iax_reject(calls[callNo].session, "Call rejected manually.");
1527 iaxc_clear_call(callNo);
1528 put_iaxc_lock();
1529 }
1530 }
1531
1532 EXPORT void iaxc_send_dtmf(char digit)
1533 {
1534 if ( selected_call >= 0 )
1535 {
1536 get_iaxc_lock();
1537 if ( calls[selected_call].state & IAXC_CALL_STATE_ACTIVE )
1538 iax_send_dtmf(calls[selected_call].session,digit);
1539 put_iaxc_lock();
1540 }
1541 }
1542
1543 EXPORT void iaxc_send_text(const char * text)
1544 {
1545 if ( selected_call >= 0 )
1546 {
1547 get_iaxc_lock();
1548 if ( calls[selected_call].state & IAXC_CALL_STATE_ACTIVE )
1549 iax_send_text(calls[selected_call].session, text);
1550 put_iaxc_lock();
1551 }
1552 }
1553
1554 EXPORT void iaxc_send_text_call(int callNo, const char * text)
1555 {
1556 if ( callNo < 0 || !(calls[callNo].state & IAXC_CALL_STATE_ACTIVE) )
1557 return;
1558
1559 get_iaxc_lock();
1560 if ( calls[callNo].state & IAXC_CALL_STATE_ACTIVE )
1561 iax_send_text(calls[callNo].session, text);
1562 put_iaxc_lock();
1563 }
1564
1565 EXPORT void iaxc_send_url(const char * url, int link)
1566 {
1567 if ( selected_call >= 0 )
1568 {
1569 get_iaxc_lock();
1570 if ( calls[selected_call].state & IAXC_CALL_STATE_ACTIVE )
1571 iax_send_url(calls[selected_call].session, url, link);
1572 put_iaxc_lock();
1573 }
1574 }
1575
1576 static int iaxc_find_call_by_session(struct iax_session *session)
1577 {
1578 int i;
1579 for ( i = 0; i < max_calls; i++ )
1580 if ( calls[i].session == session )
1581 return i;
1582 return -1;
1583 }
1584
1585 static struct iaxc_registration *iaxc_find_registration_by_session(
1586 struct iax_session *session)
1587 {
1588 struct iaxc_registration *reg;
1589 for (reg = registrations; reg != NULL; reg = reg->next)
1590 if ( reg->session == session )
1591 break;
1592 return reg;
1593 }
1594
1595 static void iaxc_handle_regreply(struct iax_event *e, struct iaxc_registration *reg)
1596 {
1597 iaxci_do_registration_callback(reg->id, e->etype, e->ies.msgcount);
1598
1599 // XXX I think the session is no longer valid.. at least, that's
1600 // what miniphone does, and re-using the session doesn't seem to
1601 // work!
1602 iax_destroy(reg->session);
1603 reg->session = NULL;
1604
1605 if ( e->etype == IAX_EVENT_REGREJ )
1606 {
1607 // we were rejected, so end the registration
1608 iaxc_remove_registration_by_id(reg->id);
1609 }
1610 }
1611
1612 /* this is what asterisk does */
1613 static int iaxc_choose_codec(int formats)
1614 {
1615 int i;
1616 static int codecs[] =
1617 {
1618 IAXC_FORMAT_ULAW,
1619 IAXC_FORMAT_ALAW,
1620 IAXC_FORMAT_SLINEAR,
1621 IAXC_FORMAT_G726,
1622 IAXC_FORMAT_ADPCM,
1623 IAXC_FORMAT_GSM,
1624 IAXC_FORMAT_ILBC,
1625 IAXC_FORMAT_SPEEX,
1626 IAXC_FORMAT_LPC10,
1627 IAXC_FORMAT_G729A,
1628 IAXC_FORMAT_G723_1,
1629
1630 /* To negotiate video codec */
1631 IAXC_FORMAT_JPEG,
1632 IAXC_FORMAT_PNG,
1633 IAXC_FORMAT_H261,
1634 IAXC_FORMAT_H263,
1635 IAXC_FORMAT_H263_PLUS,
1636 IAXC_FORMAT_MPEG4,
1637 IAXC_FORMAT_H264,
1638 IAXC_FORMAT_THEORA,
1639 };
1640 for ( i = 0; i < (int)(sizeof(codecs) / sizeof(int)); i++ )
1641 if ( codecs[i] & formats )
1642 return codecs[i];
1643 return 0;
1644 }
1645
1646 static void iaxc_handle_connect(struct iax_event * e)
1647 {
1648 #ifdef USE_VIDEO
1649 int video_format_capability;
1650 int video_format_preferred;
1651 #endif
1652 int video_format = 0;
1653 int format = 0;
1654 int callno;
1655
1656 callno = iaxc_first_free_call();
1657
1658 if ( callno < 0 )
1659 {
1660 iaxci_usermsg(IAXC_STATUS,
1661 "%i \n Incoming Call, but no appearances",
1662 callno);
1663 // XXX Reject this call!, or just ignore?
1664 //iax_reject(e->session, "Too many calls, we're busy!");
1665 iax_accept(e->session, audio_format_preferred & e->ies.capability);
1666 iax_busy(e->session);
1667 return;
1668 }
1669
1670 /* negotiate codec */
1671 /* first, try _their_ preferred format */
1672 format = audio_format_capability & e->ies.format;
1673 if ( !format )
1674 {
1675 /* then, try our preferred format */
1676 format = audio_format_preferred & e->ies.capability;
1677 }
1678
1679 if ( !format )
1680 {
1681 /* finally, see if we have one in common */
1682 format = audio_format_capability & e->ies.capability;
1683
1684 /* now choose amongst these, if we got one */
1685 if ( format )
1686 {
1687 format = iaxc_choose_codec(format);
1688 }
1689 }
1690
1691 if ( !format )
1692 {
1693 iax_reject(e->session, "Could not negotiate common codec");
1694 return;
1695 }
1696
1697 #ifdef USE_VIDEO
1698 iaxc_video_format_get_cap(&video_format_preferred,
1699 &video_format_capability);
1700
1701 /* first, see if they even want video */
1702 video_format = (e->ies.format & IAXC_VIDEO_FORMAT_MASK);
1703
1704 if ( video_format )
1705 {
1706 /* next, try _their_ preferred format */
1707 video_format &= video_format_capability;
1708
1709 if ( !video_format )
1710 {
1711 /* then, try our preferred format */
1712 video_format = video_format_preferred &
1713 (e->ies.capability & IAXC_VIDEO_FORMAT_MASK);
1714 }
1715
1716 if ( !video_format )
1717 {
1718 /* finally, see if we have one in common */
1719 video_format = video_format_capability &
1720 (e->ies.capability & IAXC_VIDEO_FORMAT_MASK);
1721
1722 /* now choose amongst these, if we got one */
1723 if ( video_format )
1724 {
1725 video_format = iaxc_choose_codec(video_format);
1726 }
1727 }
1728
1729 /* All video negotiations failed, then warn */
1730 if ( !video_format )
1731 {
1732 iaxci_usermsg(IAXC_NOTICE,
1733 "Notice: could not negotiate common video codec");
1734 iaxci_usermsg(IAXC_NOTICE,
1735 "Notice: switching to audio-only call");
1736 }
1737 }
1738 #endif /* USE_VIDEO */
1739
1740 calls[callno].vformat = video_format;
1741 calls[callno].format = format;
1742
1743 if ( e->ies.called_number )
1744 strncpy(calls[callno].local, e->ies.called_number,
1745 IAXC_EVENT_BUFSIZ - 1);
1746 else
1747 strncpy(calls[callno].local, "unknown",
1748 IAXC_EVENT_BUFSIZ - 1);
1749 calls[callno].local[IAXC_EVENT_BUFSIZ - 1] = '\0';
1750
1751 if ( e->ies.called_context )
1752 strncpy(calls[callno].local_context, e->ies.called_context,
1753 IAXC_EVENT_BUFSIZ - 1);
1754 else
1755 strncpy(calls[callno].local_context, "",
1756 IAXC_EVENT_BUFSIZ - 1);
1757 calls[callno].local_context[IAXC_EVENT_BUFSIZ - 1] = '\0';
1758
1759 if ( e->ies.calling_number )
1760 strncpy(calls[callno].remote, e->ies.calling_number,
1761 IAXC_EVENT_BUFSIZ - 1);
1762 else
1763 strncpy(calls[callno].remote, "unknown",
1764 IAXC_EVENT_BUFSIZ - 1);
1765 calls[callno].remote[IAXC_EVENT_BUFSIZ - 1] = '\0';
1766
1767 if ( e->ies.calling_name )
1768 strncpy(calls[callno].remote_name, e->ies.calling_name,
1769 IAXC_EVENT_BUFSIZ - 1);
1770 else
1771 strncpy(calls[callno].remote_name, "unknown",
1772 IAXC_EVENT_BUFSIZ - 1);
1773 calls[callno].remote_name[IAXC_EVENT_BUFSIZ - 1] = '\0';
1774
1775 iaxc_note_activity(callno);
1776 iaxci_usermsg(IAXC_STATUS, "Call from (%s)", calls[callno].remote);
1777
1778 codec_destroy( callno );
1779
1780 calls[callno].session = e->session;
1781 calls[callno].state = IAXC_CALL_STATE_ACTIVE|IAXC_CALL_STATE_RINGING;
1782
1783 iax_accept(calls[callno].session, format | video_format);
1784 iax_ring_announce(calls[callno].session);
1785
1786 iaxci_do_state_callback(callno);
1787
1788 iaxci_usermsg(IAXC_STATUS, "Incoming call on line %d", callno);
1789 }
1790
1791 static void service_network()
1792 {
1793 struct iax_event *e = 0;
1794 int callNo;
1795 struct iaxc_registration *reg;
1796
1797 while ( (e = iax_get_event(0)) )
1798 {
1799 #ifdef WIN32
1800 iaxc_millisleep(0); //fd:
1801 #endif
1802 // first, see if this is an event for one of our calls.
1803 callNo = iaxc_find_call_by_session(e->session);
1804 if ( e->etype == IAX_EVENT_NULL )
1805 {
1806 // Should we do something here?
1807 // Right now we do nothing, just go with the flow
1808 // and let the event be deallocated.
1809 } else if ( callNo >= 0 )
1810 {
1811 iaxc_handle_network_event(e, callNo);
1812 } else if ( (reg = iaxc_find_registration_by_session(e->session)) != NULL )
1813 {
1814 iaxc_handle_regreply(e,reg);
1815 } else if ( e->etype == IAX_EVENT_REGACK || e->etype == IAX_EVENT_REGREJ )
1816 {
1817 iaxci_usermsg(IAXC_ERROR, "Unexpected registration reply");
1818 } else if ( e->etype == IAX_EVENT_REGREQ )
1819 {
1820 iaxci_usermsg(IAXC_ERROR,
1821 "Registration requested by someone, but we don't understand!");
1822 } else if ( e->etype == IAX_EVENT_CONNECT )
1823 {
1824 iaxc_handle_connect(e);
1825 } else if ( e->etype == IAX_EVENT_TIMEOUT )
1826 {
1827 iaxci_usermsg(IAXC_STATUS,
1828 "Timeout for a non-existant session. Dropping",
1829 e->etype);
1830 } else
1831 {
1832 iaxci_usermsg(IAXC_ERROR,
1833 "Event (type %d) for a non-existant session. Dropping",
1834 e->etype);
1835 }
1836 iax_event_free(e);
1837 }
1838 }
1839
1840 EXPORT int iaxc_audio_devices_get(struct iaxc_audio_device **devs, int *nDevs,
1841 int *input, int *output, int *ring)
1842 {
1843 if ( test_mode )
1844 return 0;
1845
1846 *devs = audio_driver.devices;
1847 *nDevs = audio_driver.nDevices;
1848 audio_driver.selected_devices(&audio_driver, input, output, ring);
1849 return 0;
1850 }
1851
1852 EXPORT int iaxc_audio_devices_set(int input, int output, int ring)
1853 {
1854 int ret;
1855
1856 if ( test_mode )
1857 return 0;
1858
1859 get_iaxc_lock();
1860 ret = audio_driver.select_devices(&audio_driver, input, output, ring);
1861 put_iaxc_lock();
1862 return ret;
1863 }
1864
1865 EXPORT float iaxc_input_level_get()
1866 {
1867 if ( test_mode )
1868 return 0;
1869
1870 return audio_driver.input_level_get(&audio_driver);
1871 }
1872
1873 EXPORT float iaxc_output_level_get()
1874 {
1875 if ( test_mode )
1876 return 0;
1877
1878 return audio_driver.output_level_get(&audio_driver);
1879 }
1880
1881 EXPORT int iaxc_input_level_set(float level)
1882 {
1883 if ( test_mode )
1884 return 0;
1885
1886 return audio_driver.input_level_set(&audio_driver, level);
1887 }
1888
1889 EXPORT int iaxc_output_level_set(float level)
1890 {
1891 if ( test_mode )
1892 return 0;
1893
1894 return audio_driver.output_level_set(&audio_driver, level);
1895 }
1896
1897 EXPORT int iaxc_play_sound(struct iaxc_sound *s, int ring)
1898 {
1899 int ret;
1900
1901 if ( test_mode )
1902 return 0;
1903
1904 get_iaxc_lock();
1905 ret = audio_driver.play_sound(s,ring);
1906 put_iaxc_lock();
1907 return ret;
1908 }
1909
1910 EXPORT int iaxc_stop_sound(int id)
1911 {
1912 int ret;
1913
1914 if ( test_mode )
1915 return 0;
1916
1917 get_iaxc_lock();
1918 ret = audio_driver.stop_sound(id);
1919 put_iaxc_lock();
1920 return ret;
1921 }
1922
1923 EXPORT int iaxc_quelch(int callNo, int MOH)
1924 {
1925 struct iax_session *session = calls[callNo].session;
1926 if ( !session )
1927 return -1;
1928
1929 return iax_quelch_moh(session, MOH);
1930 }
1931
1932 EXPORT int iaxc_unquelch(int call)
1933 {
1934 return iax_unquelch(calls[call].session);
1935 }
1936
1937 EXPORT int iaxc_mic_boost_get( void )
1938 {
1939 return audio_driver.mic_boost_get( &audio_driver ) ;
1940 }
1941
1942 EXPORT int iaxc_mic_boost_set( int enable )
1943 {
1944 return audio_driver.mic_boost_set( &audio_driver, enable ) ;
1945 }
1946
1947 EXPORT char* iaxc_version(char * ver)
1948 {
1949 #ifndef LIBVER
1950 #define LIBVER ""
1951 #endif
1952 strncpy(ver, LIBVER, IAXC_EVENT_BUFSIZ);
1953 return ver;
1954 }
1955
1956 EXPORT unsigned int iaxc_get_audio_prefs(void)
1957 {
1958 return audio_prefs;
1959 }
1960
1961 EXPORT int iaxc_set_audio_prefs(unsigned int prefs)
1962 {
1963 unsigned int prefs_mask =
1964 IAXC_AUDIO_PREF_RECV_LOCAL_RAW |
1965 IAXC_AUDIO_PREF_RECV_LOCAL_ENCODED |
1966 IAXC_AUDIO_PREF_RECV_REMOTE_RAW |
1967 IAXC_AUDIO_PREF_RECV_REMOTE_ENCODED |
1968 IAXC_AUDIO_PREF_SEND_DISABLE;
1969
1970 if ( prefs & ~prefs_mask )
1971 return -1;
1972
1973 audio_prefs = prefs;
1974 return 0;
1975 }
1976
1977 EXPORT void iaxc_set_test_mode(int tm)
1978 {
1979 test_mode = tm;
1980 }
1981
1982 EXPORT int iaxc_push_audio(void *data, unsigned int size, unsigned int samples)
1983 {
1984 struct iaxc_call *call;
1985
1986 if ( selected_call < 0 )
1987 return -1;
1988
1989 call = &calls[selected_call];
1990
1991 if ( audio_prefs & IAXC_AUDIO_PREF_SEND_DISABLE )
1992 return 0;
1993
1994 //fprintf(stderr, "iaxc_push_audio: sending audio size %d\n", size);
1995
1996 if ( iax_send_voice(call->session, call->format, data, size, samples) == -1 )
1997 {
1998 fprintf(stderr, "iaxc_push_audio: failed to send audio frame of size %d on call %d\n", size, selected_call);
1999 return -1;
2000 }
2001
2002 return 0;
2003 }
2004
2005 void iaxc_debug_iax_set(int enable)
2006 {
2007 #ifdef DEBUG_SUPPORT
2008 if (enable)
2009 iax_enable_debug();
2010 else
2011 iax_disable_debug();
2012 #endif
2013 }
2014