00001 /* 00002 * SpanDSP - a series of DSP components for telephony 00003 * 00004 * t38_core.h - An implementation of T.38, less the packet exchange part 00005 * 00006 * Written by Steve Underwood <steveu@coppice.org> 00007 * 00008 * Copyright (C) 2005 Steve Underwood 00009 * 00010 * All rights reserved. 00011 * 00012 * This program is free software; you can redistribute it and/or modify 00013 * it under the terms of the GNU Lesser General Public License version 2.1, 00014 * as published by the Free Software Foundation. 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Lesser General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU Lesser General Public 00022 * License along with this program; if not, write to the Free Software 00023 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00024 */ 00025 00026 /*! \file */ 00027 00028 #if !defined(_SPANDSP_T38_CORE_H_) 00029 #define _SPANDSP_T38_CORE_H_ 00030 00031 /*! \page t38_core_page T.38 real time FAX over IP message handling 00032 There are two ITU recommendations which address sending FAXes over IP networks. T.37 specifies a 00033 method of encapsulating FAX images in e-mails, and transporting them to the recipient (an e-mail 00034 box, or another FAX machine) in a store-and-forward manner. T.38 defines a protocol for 00035 transmitting a FAX across an IP network in real time. The core T.38 modules implements the basic 00036 message handling for the T.38, real time, FAX over IP (FoIP) protocol. 00037 00038 The T.38 protocol can operate between: 00039 - Internet-aware FAX terminals, which connect directly to an IP network. The T.38 terminal module 00040 extends this module to provide a complete T.38 terminal. 00041 - FAX gateways, which allow traditional PSTN FAX terminals to communicate through the Internet. 00042 The T.38 gateway module extends this module to provide a T.38 gateway. 00043 - A combination of terminals and gateways. 00044 00045 T.38 is the only standardised protocol which exists for real-time FoIP. Reliably transporting a 00046 FAX between PSTN FAX terminals, through an IP network, requires use of the T.38 protocol at FAX 00047 gateways. VoIP connections are not robust for modem use, including FAX modem use. Most use low 00048 bit rate codecs, which cannot convey the modem signals accurately. Even when high bit rate 00049 codecs are used, VoIP connections suffer dropouts and timing adjustments, which modems cannot 00050 tolerate. In a LAN environment the dropout rate may be very low, but the timing adjustments which 00051 occur in VoIP connections still make modem operation unreliable. T.38 FAX gateways deal with the 00052 delays, timing jitter, and packet loss experienced in packet networks, and isolate the PSTN FAX 00053 terminals from these as far as possible. In addition, by sending FAXes as image data, rather than 00054 digitised audio, they reduce the required bandwidth of the IP network. 00055 00056 \section t38_core_page_sec_1 What does it do? 00057 00058 \section t38_core_page_sec_2 How does it work? 00059 00060 Timing differences and jitter between two T.38 entities can be a serious problem, if one of those 00061 entities is a PSTN gateway. 00062 00063 Flow control for non-ECM image data takes advantage of several features of the T.30 specification. 00064 First, an unspecified number of 0xFF octets may be sent at the start of transmission. This means we 00065 can add endless extra 0xFF bytes at this point, without breaking the T.30 spec. In practice, we 00066 cannot add too many, or we will affect the timing tolerance of the T.30 protocol by delaying the 00067 response at the end of each image. Secondly, just before an end of line (EOL) marker we can pad 00068 with zero bits. Again, the number is limited only by need to avoid upsetting the timing of the 00069 step following the non-ECM data. 00070 */ 00071 00072 /*! T.38 indicator types */ 00073 enum t30_indicator_types_e 00074 { 00075 T38_IND_NO_SIGNAL = 0, 00076 T38_IND_CNG, 00077 T38_IND_CED, 00078 T38_IND_V21_PREAMBLE, 00079 T38_IND_V27TER_2400_TRAINING, 00080 T38_IND_V27TER_4800_TRAINING, 00081 T38_IND_V29_7200_TRAINING, 00082 T38_IND_V29_9600_TRAINING, 00083 T38_IND_V17_7200_SHORT_TRAINING, 00084 T38_IND_V17_7200_LONG_TRAINING, 00085 T38_IND_V17_9600_SHORT_TRAINING, 00086 T38_IND_V17_9600_LONG_TRAINING, 00087 T38_IND_V17_12000_SHORT_TRAINING, 00088 T38_IND_V17_12000_LONG_TRAINING, 00089 T38_IND_V17_14400_SHORT_TRAINING, 00090 T38_IND_V17_14400_LONG_TRAINING, 00091 T38_IND_V8_ANSAM, 00092 T38_IND_V8_SIGNAL, 00093 T38_IND_V34_CNTL_CHANNEL_1200, 00094 T38_IND_V34_PRI_CHANNEL, 00095 T38_IND_V34_CC_RETRAIN, 00096 T38_IND_V33_12000_TRAINING, 00097 T38_IND_V33_14400_TRAINING 00098 }; 00099 00100 /*! T.38 data types */ 00101 enum t38_data_types_e 00102 { 00103 T38_DATA_NONE = -1, 00104 T38_DATA_V21 = 0, 00105 T38_DATA_V27TER_2400, 00106 T38_DATA_V27TER_4800, 00107 T38_DATA_V29_7200, 00108 T38_DATA_V29_9600, 00109 T38_DATA_V17_7200, 00110 T38_DATA_V17_9600, 00111 T38_DATA_V17_12000, 00112 T38_DATA_V17_14400, 00113 T38_DATA_V8, 00114 T38_DATA_V34_PRI_RATE, 00115 T38_DATA_V34_CC_1200, 00116 T38_DATA_V34_PRI_CH, 00117 T38_DATA_V33_12000, 00118 T38_DATA_V33_14400 00119 }; 00120 00121 /*! T.38 data field types */ 00122 enum t38_field_types_e 00123 { 00124 T38_FIELD_HDLC_DATA = 0, 00125 T38_FIELD_HDLC_SIG_END, 00126 T38_FIELD_HDLC_FCS_OK, 00127 T38_FIELD_HDLC_FCS_BAD, 00128 T38_FIELD_HDLC_FCS_OK_SIG_END, 00129 T38_FIELD_HDLC_FCS_BAD_SIG_END, 00130 T38_FIELD_T4_NON_ECM_DATA, 00131 T38_FIELD_T4_NON_ECM_SIG_END, 00132 T38_FIELD_CM_MESSAGE, 00133 T38_FIELD_JM_MESSAGE, 00134 T38_FIELD_CI_MESSAGE, 00135 T38_FIELD_V34RATE 00136 }; 00137 00138 /*! T.38 field classes */ 00139 enum t38_field_classes_e 00140 { 00141 T38_FIELD_CLASS_NONE = 0, 00142 T38_FIELD_CLASS_HDLC, 00143 T38_FIELD_CLASS_NON_ECM 00144 }; 00145 00146 /*! T.38 message types */ 00147 enum t38_message_types_e 00148 { 00149 T38_TYPE_OF_MSG_T30_INDICATOR = 0, 00150 T38_TYPE_OF_MSG_T30_DATA 00151 }; 00152 00153 /*! T.38 transport types */ 00154 enum t38_transport_types_e 00155 { 00156 T38_TRANSPORT_UDPTL = 0, 00157 T38_TRANSPORT_RTP, 00158 T38_TRANSPORT_TCP, 00159 T38_TRANSPORT_TCP_TPKT 00160 }; 00161 00162 /*! T.38 TCF management types */ 00163 enum t38_data_rate_management_types_e 00164 { 00165 T38_DATA_RATE_MANAGEMENT_LOCAL_TCF = 1, 00166 T38_DATA_RATE_MANAGEMENT_TRANSFERRED_TCF = 2 00167 }; 00168 00169 /*! T.38 Packet categories used for setting the redundancy level and packet repeat 00170 counts on a packet by packet basis. */ 00171 enum t38_packet_categories_e 00172 { 00173 /*! \brief Indicator packet */ 00174 T38_PACKET_CATEGORY_INDICATOR = 0, 00175 /*! \brief Control data packet */ 00176 T38_PACKET_CATEGORY_CONTROL_DATA = 1, 00177 /*! \brief Terminating control data packet */ 00178 T38_PACKET_CATEGORY_CONTROL_DATA_END = 2, 00179 /*! \brief Image data packet */ 00180 T38_PACKET_CATEGORY_IMAGE_DATA = 3, 00181 /*! \brief Terminating image data packet */ 00182 T38_PACKET_CATEGORY_IMAGE_DATA_END = 4 00183 }; 00184 00185 #define T38_RX_BUF_LEN 2048 00186 #define T38_TX_BUF_LEN 16384 00187 00188 /*! T.38 data field */ 00189 typedef struct 00190 { 00191 /*! Field type */ 00192 int field_type; 00193 /*! Field contents */ 00194 const uint8_t *field; 00195 /*! Field length */ 00196 int field_len; 00197 } t38_data_field_t; 00198 00199 /*! 00200 Core T.38 state, common to all modes of T.38. 00201 */ 00202 typedef struct t38_core_state_s t38_core_state_t; 00203 00204 typedef int (*t38_tx_packet_handler_t)(t38_core_state_t *s, void *user_data, const uint8_t *buf, int len, int count); 00205 00206 typedef int (*t38_rx_indicator_handler_t)(t38_core_state_t *s, void *user_data, int indicator); 00207 typedef int (*t38_rx_data_handler_t)(t38_core_state_t *s, void *user_data, int data_type, int field_type, const uint8_t *buf, int len); 00208 typedef int (*t38_rx_missing_handler_t)(t38_core_state_t *s, void *user_data, int rx_seq_no, int expected_seq_no); 00209 00210 #if defined(__cplusplus) 00211 extern "C" 00212 { 00213 #endif 00214 00215 /*! \brief Convert the code for an indicator to a short text name. 00216 \param indicator The type of indicator. 00217 \return A pointer to a short text name for the indicator. */ 00218 SPAN_DECLARE(const char *) t38_indicator_to_str(int indicator); 00219 00220 /*! \brief Convert the code for a type of data to a short text name. 00221 \param data_type The data type. 00222 \return A pointer to a short text name for the data type. */ 00223 SPAN_DECLARE(const char *) t38_data_type_to_str(int data_type); 00224 00225 /*! \brief Convert the code for a type of data field to a short text name. 00226 \param field_type The field type. 00227 \return A pointer to a short text name for the field type. */ 00228 SPAN_DECLARE(const char *) t38_field_type_to_str(int field_type); 00229 00230 /*! \brief Convert the code for a CM profile code to text description. 00231 \param profile The profile code from a CM message. 00232 \return A pointer to a short text description of the profile. */ 00233 SPAN_DECLARE(const char *) t38_cm_profile_to_str(int profile); 00234 00235 /*! \brief Convert a JM message code to text description. 00236 \param data The data field of the message. 00237 \param len The length of the data field. 00238 \return A pointer to a short text description of the profile. */ 00239 SPAN_DECLARE(const char *) t38_jm_to_str(const uint8_t *data, int len); 00240 00241 /*! \brief Convert a V34rate message to an actual bit rate. 00242 \param data The data field of the message. 00243 \param len The length of the data field. 00244 \return The bit rate, or -1 for a bad message. */ 00245 SPAN_DECLARE(int) t38_v34rate_to_bps(const uint8_t *data, int len); 00246 00247 /*! \brief Send an indicator packet 00248 \param s The T.38 context. 00249 \param indicator The indicator to send. 00250 \return The delay to allow after this indicator is sent. */ 00251 SPAN_DECLARE(int) t38_core_send_indicator(t38_core_state_t *s, int indicator); 00252 00253 /*! \brief Find the delay to allow for HDLC flags after sending an indicator 00254 \param s The T.38 context. 00255 \param indicator The indicator to check. 00256 \return The delay to allow for initial HDLC flags after this indicator is sent. */ 00257 SPAN_DECLARE(int) t38_core_send_flags_delay(t38_core_state_t *s, int indicator); 00258 00259 /*! \brief Find the delay to allow for modem training after sending an indicator 00260 \param s The T.38 context. 00261 \param indicator The indicator to check. 00262 \return The delay to allow for modem training after this indicator is sent. */ 00263 SPAN_DECLARE(int) t38_core_send_training_delay(t38_core_state_t *s, int indicator); 00264 00265 /*! \brief Send a data packet 00266 \param s The T.38 context. 00267 \param data_type The packet's data type. 00268 \param field_type The packet's field type. 00269 \param field The message data content for the packet. 00270 \param field_len The length of the message data, in bytes. 00271 \param category The category of the packet being sent. This should be one of the values defined for t38_packet_categories_e. 00272 \return 0 for OK, else -1 */ 00273 SPAN_DECLARE(int) t38_core_send_data(t38_core_state_t *s, int data_type, int field_type, const uint8_t field[], int field_len, int category); 00274 00275 /*! \brief Send a data packet 00276 \param s The T.38 context. 00277 \param data_type The packet's data type. 00278 \param field The list of fields. 00279 \param fields The number of fields in the list. 00280 \param category The category of the packet being sent. This should be one of the values defined for t38_packet_categories_e. 00281 \return 0 for OK, else -1 */ 00282 SPAN_DECLARE(int) t38_core_send_data_multi_field(t38_core_state_t *s, int data_type, const t38_data_field_t field[], int fields, int category); 00283 00284 /*! \brief Process a received T.38 IFP packet from an unreliable packet stream (e.g. UDPTL or RTP). This processing includes 00285 packet sequence number checking, missing packet recovery, and skipping repeat packets. 00286 \param s The T.38 context. 00287 \param buf The packet contents. 00288 \param len The length of the packet contents. 00289 \param seq_no The packet sequence number. 00290 \return 0 for OK, else -1. */ 00291 SPAN_DECLARE_NONSTD(int) t38_core_rx_ifp_packet(t38_core_state_t *s, const uint8_t *buf, int len, uint16_t seq_no); 00292 00293 /*! \brief Process a received T.38 IFP packet from a reliable stream (e.g. TCP). 00294 \param s The T.38 context. 00295 \param buf The packet contents. 00296 \param len The length of the packet contents. 00297 \param seq_no The packet sequence number, used for logging purposes. 00298 \return The length of the packet processed, or -1 if there is an error in the packet, or too few bytes of data to complete it. */ 00299 SPAN_DECLARE_NONSTD(int) t38_core_rx_ifp_stream(t38_core_state_t *s, const uint8_t *buf, int len, uint16_t log_seq_no); 00300 00301 /*! Set the method to be used for data rate management, as per the T.38 spec. 00302 \param s The T.38 context. 00303 \param method 1 for pass TCF across the T.38 link, 2 for handle TCF locally. 00304 */ 00305 SPAN_DECLARE(void) t38_set_data_rate_management_method(t38_core_state_t *s, int method); 00306 00307 /*! Set the data transport protocol. 00308 \param s The T.38 context. 00309 \param data_transport_protocol UDPTL, RTP or TPKT. 00310 */ 00311 SPAN_DECLARE(void) t38_set_data_transport_protocol(t38_core_state_t *s, int data_transport_protocol); 00312 00313 /*! Set the non-ECM fill bit removal mode. 00314 \param s The T.38 context. 00315 \param fill_bit_removal TRUE to remove fill bits across the T.38 link, else FALSE. 00316 */ 00317 SPAN_DECLARE(void) t38_set_fill_bit_removal(t38_core_state_t *s, int fill_bit_removal); 00318 00319 /*! Set the MMR transcoding mode. 00320 \param s The T.38 context. 00321 \param mmr_transcoding TRUE to transcode to MMR across the T.38 link, else FALSE. 00322 */ 00323 SPAN_DECLARE(void) t38_set_mmr_transcoding(t38_core_state_t *s, int mmr_transcoding); 00324 00325 /*! Set the JBIG transcoding mode. 00326 \param s The T.38 context. 00327 \param jbig_transcoding TRUE to transcode to JBIG across the T.38 link, else FALSE. 00328 */ 00329 SPAN_DECLARE(void) t38_set_jbig_transcoding(t38_core_state_t *s, int jbig_transcoding); 00330 00331 /*! Set the maximum buffer size for received data at the far end. 00332 \param s The T.38 context. 00333 \param max_buffer_size The maximum buffer size. 00334 */ 00335 SPAN_DECLARE(void) t38_set_max_buffer_size(t38_core_state_t *s, int max_buffer_size); 00336 00337 /*! Set the maximum size of an IFP packet that is acceptable by the far end. 00338 \param s The T.38 context. 00339 \param max_datagram_size The maximum IFP packet length, in bytes. 00340 */ 00341 SPAN_DECLARE(void) t38_set_max_datagram_size(t38_core_state_t *s, int max_datagram_size); 00342 00343 /*! \brief Send a data packet 00344 \param s The T.38 context. 00345 \param category The category of the packet being sent. This should be one of the values defined for t38_packet_categories_e. 00346 \param setting The repeat count for the category. This should be at least one for all categories other an indicator packets. 00347 Zero is valid for indicator packets, as it suppresses the sending of indicator packets, as an application using 00348 TCP for the transport would require. As the setting is passed through to the transmission channel, additional 00349 information may be encoded in it, such as the redundancy depth for the particular packet category. */ 00350 SPAN_DECLARE(void) t38_set_redundancy_control(t38_core_state_t *s, int category, int setting); 00351 00352 SPAN_DECLARE(void) t38_set_pace_transmission(t38_core_state_t *s, int pace_transmission); 00353 00354 SPAN_DECLARE(void) t38_set_fastest_image_data_rate(t38_core_state_t *s, int max_rate); 00355 00356 SPAN_DECLARE(int) t38_get_fastest_image_data_rate(t38_core_state_t *s); 00357 00358 /*! Set the T.38 version to be emulated. 00359 \param s The T.38 context. 00360 \param t38_version Version number, as in the T.38 spec. 00361 */ 00362 SPAN_DECLARE(void) t38_set_t38_version(t38_core_state_t *s, int t38_version); 00363 00364 /*! Set the sequence number handling option. 00365 \param s The T.38 context. 00366 \param check TRUE to check sequence numbers, and handle gaps reasonably. FALSE 00367 for no sequence number processing (e.g. for TPKT over TCP transport). 00368 */ 00369 SPAN_DECLARE(void) t38_set_sequence_number_handling(t38_core_state_t *s, int check); 00370 00371 /*! Set the TEP handling option. 00372 \param s The T.38 context. 00373 \param allow_for_tep TRUE to allow for TEP playout, else FALSE. 00374 */ 00375 SPAN_DECLARE(void) t38_set_tep_handling(t38_core_state_t *s, int allow_for_tep); 00376 00377 /*! Get a pointer to the logging context associated with a T.38 context. 00378 \brief Get a pointer to the logging context associated with a T.38 context. 00379 \param s The T.38 context. 00380 \return A pointer to the logging context, or NULL. 00381 */ 00382 SPAN_DECLARE(logging_state_t *) t38_core_get_logging_state(t38_core_state_t *s); 00383 00384 /*! Restart a T.38 core context. 00385 \brief Restart a T.38 core context. 00386 \param s The T.38 context. 00387 \return 0 for OK, else -1. */ 00388 SPAN_DECLARE(int) t38_core_restart(t38_core_state_t *s); 00389 00390 /*! Initialise a T.38 core context. 00391 \brief Initialise a T.38 core context. 00392 \param s The T.38 context. 00393 \param rx_indicator_handler Receive indicator handling routine. 00394 \param rx_data_handler Receive data packet handling routine. 00395 \param rx_rx_missing_handler Missing receive packet handling routine. 00396 \param rx_packet_user_data An opaque pointer passed to the rx packet handling routines. 00397 \param tx_packet_handler Packet transmit handling routine. 00398 \param tx_packet_user_data An opaque pointer passed to the tx_packet_handler. 00399 \return A pointer to the T.38 context, or NULL if there was a problem. */ 00400 SPAN_DECLARE(t38_core_state_t *) t38_core_init(t38_core_state_t *s, 00401 t38_rx_indicator_handler_t rx_indicator_handler, 00402 t38_rx_data_handler_t rx_data_handler, 00403 t38_rx_missing_handler_t rx_missing_handler, 00404 void *rx_user_data, 00405 t38_tx_packet_handler_t tx_packet_handler, 00406 void *tx_packet_user_data); 00407 00408 /*! Release a signaling tone transmitter context. 00409 \brief Release a signaling tone transmitter context. 00410 \param s The T.38 context. 00411 \return 0 for OK */ 00412 SPAN_DECLARE(int) t38_core_release(t38_core_state_t *s); 00413 00414 /*! Free a signaling tone transmitter context. 00415 \brief Free a signaling tone transmitter context. 00416 \param s The T.38 context. 00417 \return 0 for OK */ 00418 SPAN_DECLARE(int) t38_core_free(t38_core_state_t *s); 00419 00420 #if defined(__cplusplus) 00421 } 00422 #endif 00423 00424 #endif 00425 /*- End of file ------------------------------------------------------------*/
1.6.1