freeswitch/libs
Stefan Knoblich 705543c8b4 ftmod_misdn: Use a per-span I/O thread to handle B-channel data.
Move the B-channel message handling into a per-span I/O thread,
to solve most of the problems caused by the intermixed data + control
socket interface of mISDN, missing write poll() support on
mISDN B-channels and the FreeTDM I/O model. This eliminates most of
the audio problems (except for a few minor glitches).

A unix stream socket pair is used as a bi-directional pipe replacement
(the pipe code is still included in this commit, but will be removed later),
with the RX and TX buffer sizes carefully tuned to avoid excessive buffering
(= latency) and a deadlock situation between the write() call in ftdm_write()
and the code in misdn_span_run() that needs a minimum amount of data in the
TX buffer, before sending out a PH_DATA_REQ to the mISDN socket
(see misdn_span_run() comments for more details).

The minimum size for pipes is PAGE_SIZE (4k), which is ~500 ms worth of
audio. A socket pair RX/TX buffer size of 3k, seems to hold a maximum
amount of around 500 bytes data in practice, giving us a much lower
maximum latency than a unix pipe. (The socket pair might be replaced by a
custom ring buffer / fifo data structure to get even more fine grained
control of the maximum latency.)

The newly introduced span_start / span_stop callbacks in
ftdm_io_interface_t are used to start / stop the I/O thread. The callback
functions will wait up to 10 seconds for the thread to successfully
start up or shut down (using a mutex + condition var).

NOTE: Using any of the locking ftdm_span_() functions in the I/O will cause
      a deadlock between the I/O thread (trying to lock span->mutex) and the
      thread calling ftdm_start()/_stop() (holding the span->mutex).
      (The I/O thread currently uses direct span member access to avoid this.)

The I/O thread uses the epoll(7) family of functions for event handling.
An epoll context is created on startup and all B-channel sockets are
registered (READ, PRI and ERR). Before entering the event loop,
the I/O thread will send a signal on the condition variable, to
indicate it has completed the startup procedure.

Incoming b-channel and command pipe events are handled by the event loop.
Payload of incoming PH_DATA_IND frames (= audio data) is sent to the
rx_audio_pipe_in end of the b-channel's socket pair and, if enough data is
available, a PH_DATA_REQ of the same size is sent to the b-channel mISDN socket
to transmit audio.

A MISDN_CMD_STOP command on the event pipe will wake up the I/O thread and
cause it to shut down. All b-channels will be unregistered from the epoll context
and the epoll fd closed. The I/O thread terminates itself after signalling the
successfull shutdown on the condition variable.

TODOs:
    - Move D-Channel into I/O thread too

    - Custom FIFO/ring buffer for data (even lower latency)

    - Improve epoll() code (per-channel struct w/ callback, for epfd.data.ptr)

    - Use mISDN DSP for audio (e.g. tone generator, dtmf detector, echo cancel)

    - Use a per-port / span control socket to execute channel commands
      synchronously, or add misdn_commands (queue?) that can be used that way

    - Name I/O threads 'mISDN-%SPAN_NAME%', e.g. 'mISDN-M_BRI1'
      (= add ftdm_thread_set_namef(thread, fmt, ...) / ftdm_thread_set_name(thread, name))

TL;DR: "tweak", solves "booboo" with audio

Signed-off-by: Stefan Knoblich <stkn@openisdn.net>
2012-09-11 14:31:27 -04:00
..
apr Return const char* from apr_pool_tag 2012-06-24 08:15:16 +00:00
apr-util unimrcp vs2010 upgrade with revert of incorrect 2008 changes - still not working 2011-06-16 23:26:53 -05:00
broadvoice
curl FS-3803 --resolve 2012-01-12 14:17:23 -06:00
esl FS-4543 --resolve thanks Peter 2012-09-11 14:29:58 -04:00
freetdm ftmod_misdn: Use a per-span I/O thread to handle B-channel data. 2012-09-11 14:31:27 -04:00
iksemel add pkg-config macro 2012-01-11 20:14:17 -05:00
ilbc fix iLBC under windows 2011-02-03 23:46:19 -06:00
js http://jira.freeswitch.org/browse/FS-2976 2011-01-13 09:55:55 -06:00
ldns attempt to fix ldns install-sh correctly 2012-04-17 23:05:48 -04:00
libcodec2 [3/6] codec2: Rename AUTOMAKE_OPTS to AUTOMAKE_OPTIONS 2011-11-04 14:20:13 +01:00
libdingaling add video support to lib/mod.dingaling this needs testing, google voice won't work with video calls so disable video codecs if you use that 2012-06-22 18:15:06 -05:00
libedit fs_cli: make enhanced fs_cli features work on all TERMs 2012-01-31 18:42:47 +00:00
libg722_1 spelling fix 2012-05-12 18:53:00 +00:00
libnatpmp FS-4071 --resolve 2012-04-04 16:18:53 -05:00
libscgi frick 2012-07-14 20:23:13 -05:00
libsndfile FS-4114 --resolve this bug is actually in libsndfile, if its not fixed in the latest version we probably should report it upstream 2012-04-16 11:55:52 -05:00
libteletone Update a ton of copyright statements to make sure the dates are proper 2012-04-25 17:14:55 -05:00
libwebsockets stub for mod_html5 2012-09-11 14:29:56 -04:00
libzrtp Update AUTHORS 2012-06-25 08:51:49 +00:00
miniupnpc
openzap abstract out originate_signal_bond to a function to avoid confustion and regressions 2012-05-29 13:10:15 -05:00
pcre FS-3766 --resolve 2011-12-16 07:35:05 -05:00
portaudio FS-3472 --resolve 2011-12-15 21:23:19 -05:00
silk [2/6] silk: Rename AUTOMAKE_OPTS to AUTOMAKE_OPTIONS 2011-11-04 14:19:10 +01:00
sofia-sip FS-4079 FS-4540 please update to this version 2012-09-11 14:29:57 -04:00
spandsp A line got deleted somewhere along the line. I didn't do it, honest. 2012-09-11 14:30:50 -04:00
speex
sqlite Fix warnings in sqlite 2012-06-24 08:34:52 +00:00
srtp [6/6] srtp: Rename AUTOMAKE_OPTS to AUTOMAKE_OPTIONS 2011-11-04 14:22:28 +01:00
stfu FS-4249 --resolve 2012-05-25 07:54:11 -05:00
tiff-3.8.2
unimrcp FS-3648 --resolve 2011-12-15 16:55:10 -05:00
win32 FS-4219 --resolve 2012-09-11 14:31:27 -04:00
xmlrpc-c merge code from xml-rpc trunk - fix xml-rpc not terminating on windows on fs unload - only effect windows build 2011-01-11 09:40:59 -06:00
yaml
.gitignore FS-4526 --resolve 2012-09-11 14:29:58 -04:00