freeswitch/libs/freetdm/src
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
..
ftmod ftmod_misdn: Use a per-span I/O thread to handle B-channel data. 2012-09-11 14:31:27 -04:00
include FreeTDM: Add span start/stop callbacks to ftdm_io_interface. 2012-09-11 14:29:57 -04:00
isdn windows renaming to freetdm changes 2010-01-15 21:43:41 +00:00
ss7 Test 2008-04-11 17:11:44 +00:00
detect_dtmf.c we need to fix this build system it masks errors 2011-09-16 12:13:25 -05:00
detect_tones.c Linux renaming from openzap to freetdm 2010-01-15 19:22:49 +00:00
fsk.c start API cleanup by moving private headers and data structures into private/ include directory 2010-04-28 18:17:49 -04:00
ftdm_backtrace.c FreeTDM: Move custom backtrace code into ftdm_backtrace_walk() and helper functions. 2012-07-11 17:25:58 +02:00
ftdm_buffer.c Update a ton of copyright statements to make sure the dates are proper 2012-04-25 17:14:55 -05:00
ftdm_call_utils.c chlog: freetdm - SS7: Support for Transparent IAM 2011-05-17 17:27:05 -04:00
ftdm_callerid.c start API cleanup by moving private headers and data structures into private/ include directory 2010-04-28 18:17:49 -04:00
ftdm_config.c Update a ton of copyright statements to make sure the dates are proper 2012-04-25 17:14:55 -05:00
ftdm_cpu_monitor.c freetdm: Added support for hardware (native) R2 MF generation 2011-02-24 18:41:07 -05:00
ftdm_dso.c freetdm: Added support for hardware (native) R2 MF generation 2011-02-24 18:41:07 -05:00
ftdm_io.c FreeTDM: Add span start/stop callbacks to ftdm_io_interface. 2012-09-11 14:29:57 -04:00
ftdm_queue.c freetdm: - Extend ftdm interrupt object to notify which IO events are ready in the device 2012-05-07 15:13:26 -04:00
ftdm_sched.c stop sched thread quicker on global destroy 2012-04-02 15:33:45 -05:00
ftdm_state.c FreeTDM: Add gcc printf()-style format string checks to ftdm_log(), also add FTDM_(U)INT64_FMT and FTDM_TIME_FMT constants. 2012-07-11 22:44:08 +02:00
ftdm_threadmutex.c OPENZAP-183 --resolve 2012-05-09 09:20:10 -05:00
ftdm_variables.c freetdm: Removed unnecessary parameter copy 2011-02-25 12:20:32 -05:00
g711.c fix vim format line 2009-09-14 22:06:20 +00:00
hashtable.c start API cleanup by moving private headers and data structures into private/ include directory 2010-04-28 18:17:49 -04:00
hashtable_itr.c start API cleanup by moving private headers and data structures into private/ include directory 2010-04-28 18:17:49 -04:00
libteletone_detect.c Copyright date update. 2012-04-18 11:52:02 -05:00
libteletone_generate.c Copyright date update. 2012-04-18 11:52:02 -05:00
priserver.c Update a ton of copyright statements to make sure the dates are proper 2012-04-25 17:14:55 -05:00
sangoma_pri.c Update a ton of copyright statements to make sure the dates are proper 2012-04-25 17:14:55 -05:00
sangoma_pri.h Update a ton of copyright statements to make sure the dates are proper 2012-04-25 17:14:55 -05:00
testanalog.c freetdm: ISDN Changes to better match SIP-to-TDM states 2010-11-24 20:12:09 -05:00
testapp.c start API cleanup by moving private headers and data structures into private/ include directory 2010-04-28 18:17:49 -04:00
testcid.c start API cleanup by moving private headers and data structures into private/ include directory 2010-04-28 18:17:49 -04:00
testisdn.c more freetdm changes 2010-01-15 20:35:11 +00:00
testpri.c freetdm: check protocol status after configuring the signaling 2010-04-28 18:17:52 -04:00
testr2.c FreeTDM: Add gcc printf()-style format string checks to ftdm_log(), also add FTDM_(U)INT64_FMT and FTDM_TIME_FMT constants. 2012-07-11 22:44:08 +02:00
testtones.c start API cleanup by moving private headers and data structures into private/ include directory 2010-04-28 18:17:49 -04:00
uart.c start API cleanup by moving private headers and data structures into private/ include directory 2010-04-28 18:17:49 -04:00