123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- #include "precompile.h"
- #include "audiofax.h"
- #include "audiolog.h"
- #include "audiocontext.h"
- #define SPANDSP_EXPOSE_INTERNAL_STRUCTURES
- #include <spandsp.h>
- #include <spandsp/version.h>
- #define DEFAULT_IDENT "SpanDSP Fax Ident"
- #define DEFAULT_HEADER "SpanDSP Fax Header"
- static void spanfax_log_message(int level, const char *msg)
- {
- int log_level;
- switch (level) {
- case SPAN_LOG_NONE:
- return;
- case SPAN_LOG_ERROR:
- case SPAN_LOG_PROTOCOL_ERROR:
- log_level = AUDIO_LOG_LEVEL_ERROR;
- break;
- case SPAN_LOG_WARNING:
- case SPAN_LOG_PROTOCOL_WARNING:
- log_level = AUDIO_LOG_LEVEL_WARN;
- break;
- case SPAN_LOG_FLOW:
- case SPAN_LOG_FLOW_2:
- case SPAN_LOG_FLOW_3:
- default: /* SPAN_LOG_DEBUG, SPAN_LOG_DEBUG_2, SPAN_LOG_DEBUG_3 */
- log_level = AUDIO_LOG_LEVEL_DEBUG;
- break;
- }
- if (msg && strlen(msg)) {
- audio_log(log_level, msg);
- }
- }
- /*
- * Called at the end of the document
- */
- static void phase_e_handler(t30_state_t * s, void *user_data, int result)
- {
- audiofax_t *fax = (audiofax_t *)user_data;
- audiostream_raise_event(&fax->base, STREAM_EVT_FAX_END, result == T30_ERR_OK ? 0 : -1, 0);
- }
- static apr_status_t read_frame(void *self, audioframe_t *frame)
- {
- audiofax_t *fax = CONTAINING_RECORD(self, audiofax_t, base);
- int tx;
-
- frame->dtmf = 0;
- frame->size = 2 * FRAME_TIME * 8000 / 1000;
- tx = fax_tx(fax->fax_state, (short*)frame->buffer, frame->size>>1);
- return APR_SUCCESS;
- }
- static apr_status_t write_frame(void *self, const audioframe_t *frame)
- {
- audiofax_t *fax = CONTAINING_RECORD(self, audiofax_t, base);
- char tmp[SUGGEST_FRAME_SIZE];
- memcpy(tmp, frame->buffer, frame->size);
- fax_rx(fax->fax_state, (short*)&tmp[0], frame->size >> 1);
- return APR_SUCCESS;
- }
- static audiostream_vtbl_t g_stream_vtbl = {
- &read_frame,
- &write_frame,
- };
- apr_status_t audiofax_create(apr_pool_t *pool, audioengine_t *engine, audiofax_t **p_fax)
- {
- audiofax_t *fax;
- fax = apr_palloc(pool, sizeof(audiofax_t));
- memset(fax, 0, sizeof(audiofax_t));
- fax->ident = DEFAULT_IDENT;
- fax->header = DEFAULT_HEADER;
- fax->disable_v17 = FALSE;
- fax->tx_page_start = -1;
- fax->tx_page_end = -1;
- fax->fax_state = 0;
- fax->ecm = TRUE;
- audiostream_init(engine, &g_stream_vtbl, &fax->base);
- fax->base.direction = STREAM_DIR_BOTH;
- *p_fax = fax;
- return APR_SUCCESS;
- }
- void audiofax_destroy(audiofax_t *fax)
- {
- if (fax->file) {
- free(fax->file);
- fax->file = NULL;
- }
- if (fax->ident) {
- free(fax->ident);
- fax->ident = NULL;
- }
- if (fax->header) {
- free(fax->header);
- fax->header = NULL;
- }
- }
- apr_status_t audiofax_set_caller(audiofax_t *fax, int on)
- {
- fax->caller = !!on;
- return APR_SUCCESS;
- }
- apr_status_t audiofax_set_file(audiofax_t *fax, const char *file)
- {
- if (!file)
- return APR_EINVAL;
- if (fax->file)
- free(fax->file);
- fax->file = strdup(file);
- return APR_SUCCESS;
- }
- apr_status_t audiofax_set_ident(audiofax_t *fax, const char *ident)
- {
- if (!ident)
- return APR_EINVAL;
- if (fax->ident)
- free(fax->ident);
- fax->ident = strdup(ident);
- return APR_SUCCESS;
- }
- apr_status_t audiofax_set_header(audiofax_t *fax, const char *header)
- {
- if (!header)
- return APR_EINVAL;
- if (fax->header)
- free(fax->header);
- fax->header = strdup(header);
- return APR_SUCCESS;
- }
- apr_status_t audiofax_set_page_range(audiofax_t *fax, int start, int end)
- {
- fax->tx_page_start = start;
- fax->tx_page_end = end;
- return APR_SUCCESS;
- }
- apr_status_t audiofax_enable_v17(audiofax_t *fax, int on)
- {
- fax->disable_v17 = !on;
- return APR_SUCCESS;
- }
- apr_status_t audiofax_enable_ecm(audiofax_t *fax, int on)
- {
- fax->ecm = on;
- return APR_SUCCESS;
- }
- apr_status_t audiofax_init(audiofax_t *fax)
- {
- t30_state_t *t30;
-
- fax->fax_state = malloc(sizeof(fax_state_t));
- t30 = fax_get_t30_state(fax->fax_state);
- memset(fax->fax_state, 0, sizeof(fax_state_t));
- if (fax_init(fax->fax_state, fax->caller) == NULL)
- return APR_EGENERAL;
- fax_set_transmit_on_idle(fax->fax_state, TRUE);
-
- span_log_set_message_handler(&(((fax_state_t*)fax->fax_state)->logging), spanfax_log_message);
- span_log_set_message_handler(&t30->logging, spanfax_log_message);
- t30_set_tx_ident(t30, fax->ident ? fax->ident : DEFAULT_IDENT);
- t30_set_tx_page_header_info(t30, fax->header ? fax->header : DEFAULT_HEADER);
- t30_set_phase_e_handler(t30, phase_e_handler, fax);
- t30_set_supported_image_sizes(t30,
- T30_SUPPORT_US_LETTER_LENGTH | T30_SUPPORT_US_LEGAL_LENGTH | T30_SUPPORT_UNLIMITED_LENGTH
- | T30_SUPPORT_215MM_WIDTH | T30_SUPPORT_255MM_WIDTH | T30_SUPPORT_303MM_WIDTH);
- t30_set_supported_resolutions(t30,
- T30_SUPPORT_STANDARD_RESOLUTION | T30_SUPPORT_FINE_RESOLUTION | T30_SUPPORT_SUPERFINE_RESOLUTION
- | T30_SUPPORT_R8_RESOLUTION | T30_SUPPORT_R16_RESOLUTION);
- if (fax->disable_v17) {
- t30_set_supported_modems(t30, T30_SUPPORT_V29 | T30_SUPPORT_V27TER);
- } else {
- t30_set_supported_modems(t30, T30_SUPPORT_V29 | T30_SUPPORT_V27TER | T30_SUPPORT_V17);
- }
- if (fax->ecm) {
- t30_set_supported_compressions(t30, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
- t30_set_ecm_capability(t30, TRUE);
- } else {
- t30_set_supported_compressions(t30, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION);
- }
- if (fax->caller) {
- t30_set_tx_file(t30, fax->file, fax->tx_page_start, fax->tx_page_end);
- } else {
- t30_set_rx_file(t30, fax->file, -1);
- }
- return APR_SUCCESS;
- }
- apr_status_t audiofax_term(audiofax_t *fax)
- {
- if (fax->fax_state) {
- t30_state_t *t30;
- t30 = fax_get_t30_state(fax->fax_state);
- if (t30) {
- t30_terminate(t30);
- }
- fax_release(fax->fax_state);
- free(fax->fax_state);
- fax->fax_state = 0;
- }
- return APR_SUCCESS;
- }
|