[Freeswitch-svn] [commit] r9431 - freeswitch/trunk/src/mod/applications/mod_fax

Freeswitch SVN agx at freeswitch.org
Wed Sep 3 19:16:23 EDT 2008


Author: agx
Date: Wed Sep  3 19:16:23 2008
New Revision: 9431

Modified:
   freeswitch/trunk/src/mod/applications/mod_fax/mod_fax.c

Log:
improved mod_fax

Modified: freeswitch/trunk/src/mod/applications/mod_fax/mod_fax.c
==============================================================================
--- freeswitch/trunk/src/mod/applications/mod_fax/mod_fax.c	(original)
+++ freeswitch/trunk/src/mod/applications/mod_fax/mod_fax.c	Wed Sep  3 19:16:23 2008
@@ -38,11 +38,43 @@
 SWITCH_MODULE_LOAD_FUNCTION(mod_fax_load);
 SWITCH_MODULE_DEFINITION(mod_fax, mod_fax_load, NULL, NULL);
 
+/*
+ * output spandsp lowlevel messages
+ */
+static void span_message(int level, const char *msg)
+{
+	if (msg==NULL) return;
+	int fs_log_level = SWITCH_LOG_NOTICE;
+    // TODO: verify all the span_log_levels available
+	if (level == SPAN_LOG_ERROR)
+		fs_log_level = SWITCH_LOG_ERROR;
+	else if (level == SPAN_LOG_WARNING)
+		fs_log_level = SWITCH_LOG_WARNING;
+	else
+		fs_log_level = SWITCH_LOG_DEBUG;
+    switch_log_printf(SWITCH_CHANNEL_LOG, fs_log_level, "%s", msg );
+}
+
+/*
+ * This function is called when the negotiation is completed
+ */
+
+static int phase_b_handler(t30_state_t *s, void *user_data, int result)
+{
+    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "MARK: Entering phase D\n");
+	return T30_ERR_OK;
+}
+
+/*
+ * This function is called when the fax has finished his job
+ */
+
 static void phase_e_handler(t30_state_t *s, void *user_data, int result)
 {
     t30_stats_t t;
     const char *local_ident = NULL;
     const char *far_ident = NULL;
+    switch_channel_t *chan = (struct switch_channel_t *) user_data;
     
     if (result == T30_ERR_OK)
         {
@@ -63,15 +95,38 @@
             switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Image resolution:  %i x %i\n", t.x_resolution, t.y_resolution);
             switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Transfer Rate:     %i\n", t.bit_rate);
             switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "==============================================================================\n");
+
         }
     else
         {
             switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "==============================================================================\n");
             switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Fax receive not successful - result (%d) %s.\n", result, t30_completion_code_to_str(result));
             switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "==============================================================================\n");
+
         }
+
+    //TODO: remove the assert once this has been tested
+    assert(user_data != NULL);
+    //TODO: is the buffer too little?
+    switch_channel_set_variable(chan, "FAX_REMOTESTATIONID", far_ident);
+    char buf[128];
+    snprintf(buf, sizeof(buf), "%d", t.pages_transferred);
+    switch_channel_set_variable(chan, "FAX_PAGES", buf);
+    snprintf(buf, sizeof(buf), "%dx%d", t.x_resolution, t.y_resolution);
+    switch_channel_set_variable(chan, "FAX_SIZE", buf);
+    snprintf(buf, sizeof(buf), "%d", t.bit_rate);
+    switch_channel_set_variable(chan, "FAX_SPEED", buf);
+    snprintf(buf, sizeof(buf), "%d", result);
+    switch_channel_set_variable(chan, "FAX_RESULT", buf);
+    snprintf(buf, sizeof(buf), "%s", t30_completion_code_to_str(result));
+    switch_channel_set_variable(chan, "FAX_ERROR", buf);
 }
 
+
+/*
+ * This function is called whenever a single new page has been received
+ */
+
 static int phase_d_handler(t30_state_t *s, void *user_data, int result)
 {
     t30_stats_t t;
@@ -86,7 +141,8 @@
             switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Transfer Rate:      %i\n", t.bit_rate);
             switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Bad rows            %i\n", t.bad_rows);
             switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Longest bad row run %i\n", t.longest_bad_row_run);
-            switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Compression type    %i\n", t.encoding);
+            switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Compression type    %i %s\n", t.encoding,
+                t4_encoding_to_str(t.encoding));
             switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Image size (bytes)  %i\n", t.image_size);
             switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "==============================================================================\n");
         }
@@ -106,15 +162,39 @@
     int16_t buf[512];
     int tx = 0;
     int calling_party = FALSE;
+    /* Channels variable parsing */
     char *file_name = NULL;
+    const char *fax_local_debug = NULL;
+    int debug = FALSE;
+    const char *fax_local_number = NULL;
+    const char *fax_local_name = NULL;
+    const char *fax_local_subname = NULL;
+    const char *fax_local_ecm = NULL;
+    int fax_local_disable_ecm;
+    const char *fax_local_v17 = NULL;
 
-    file_name = switch_core_session_strdup(session, data);
-
+    // make sure we have a valid channel when starting the FAX application
     channel = switch_core_session_get_channel(session);
 	assert(channel != NULL);	
 
+	/* reset output variables */
+    switch_channel_set_variable(channel, "FAX_REMOTESTATIONID", "unknown");
+    switch_channel_set_variable(channel, "FAX_PAGES",   "0");
+    switch_channel_set_variable(channel, "FAX_SIZE",    "0");
+    switch_channel_set_variable(channel, "FAX_SPEED",   "0");
+    switch_channel_set_variable(channel, "FAX_RESULT",  "1");
+    switch_channel_set_variable(channel, "FAX_ERROR",   "fax not received yet");
+
+    // Answer the call, otherwise we're not getting incoming audio
 	switch_channel_answer(channel);
 
+    // TODO: could be a good idea to disable ECHOCAN on ZAP channels and to reset volumes too
+
+    /* We store the original channel codec before switching both
+     * legs of the calls to a linear 16 bit codec that is the one
+     * used internally by spandsp and FS will do the transcoding
+     * from G.711 or any other original codec
+     */
     orig_read_codec = switch_core_session_get_read_codec(session);
 
     if (switch_core_codec_init(&read_codec, 
@@ -147,31 +227,102 @@
         goto done;
     }
 
+    /* SpanDSP initialization */
+    //TODO: check spandsp code to see if we need to clear the fax structure
+    memset(&fax, 0, sizeof(fax));
     fax_init(&fax, calling_party);
-    t30_set_tx_ident(&fax.t30, "test");
-    t30_set_tx_page_header_info(&fax.t30, "test");
+    //TODO: fax_init return NULL in case of failed initialization
+
+    //TODO: did i ported this from app_TXfax.c and it has no use there?
+    fax_set_transmit_on_idle(&fax, TRUE);
+
+    //TODO: set spanlog debug to be outputted somewhere 
+    span_log_set_message_handler(&fax.logging, span_message);
+    span_log_set_message_handler(&fax.t30.logging, span_message);
+
+    /*
+     * Enable options based on channel variables and input parameters
+     */
+
+    /* file_name - Sets the TIFF filename where do you want to save the fax */
+    file_name = switch_core_session_strdup(session, data);
+    //TODO: check file_name is not NULL ?
     t30_set_rx_file(&fax.t30, file_name, -1);
 
-    t30_set_phase_d_handler(&fax.t30, phase_d_handler, NULL);
-    t30_set_phase_e_handler(&fax.t30, phase_e_handler, NULL);
+    /* FAX_DEBUG - enable extra debugging if defined */
+    fax_local_debug = switch_channel_get_variable(channel, "FAX_DEBUG");
+    debug = (fax_local_debug == NULL) ? FALSE : TRUE;
+    if (debug) {
+        span_log_set_level(&fax.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW);
+        span_log_set_level(&fax.t30.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW);
+    }
 
-    t30_set_ecm_capability(&fax.t30, TRUE);
-    t30_set_supported_compressions(&fax.t30, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
+    /* FAX_LOCAL_NUMBER - Set the receiving station phone number */
+    fax_local_number = switch_channel_get_variable(channel, "FAX_LOCAL_NUMBER");
+    if (fax_local_number==NULL) {
+        fax_local_number="";
+    }
+    t30_set_tx_ident(&fax.t30, fax_local_number);
 
-    t30_set_supported_image_sizes(&fax.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(&fax.t30, T30_SUPPORT_STANDARD_RESOLUTION | T30_SUPPORT_FINE_RESOLUTION | T30_SUPPORT_SUPERFINE_RESOLUTION
-                                  | T30_SUPPORT_R8_RESOLUTION | T30_SUPPORT_R16_RESOLUTION);
+    /* FAX_LOCAL_NAME - Set the receiving station ID name (string) */
+    fax_local_name = switch_channel_get_variable(channel, "FAX_LOCAL_NAME");
+    if (fax_local_name==NULL) {
+        fax_local_name="";
+    }
+    t30_set_tx_page_header_info(&fax.t30, fax_local_name);
+
+    /* FAX_LOCAL_SUBNAME - Set the receiving station ID sub name */
+    fax_local_subname = switch_channel_get_variable(channel, "FAX_LOCAL_SUBNAME");
+    if (fax_local_subname==NULL) {
+        fax_local_subname="";
+    }
+	t30_set_tx_sub_address(&fax.t30, fax_local_subname);
 
+    /* FAX_DISABLE_ECM - Set if you want ECM on or OFF */
+    fax_local_ecm = switch_channel_get_variable(channel, "FAX_DISABLE_ECM");
+    if (fax_local_ecm == NULL ) {
+        t30_set_ecm_capability(&fax.t30, TRUE);
+		t30_set_supported_compressions(&fax.t30, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
+    } else {
+        t30_set_ecm_capability(&fax.t30, FALSE);
+	}
+
+    /* FAX_DISABLE_V17 - set if you want 9600 or V17 (14.400) */
+    fax_local_v17 = switch_channel_get_variable(channel, "FAX_DISABLE_V17");
+    if (fax_local_v17 == NULL) {
+		t30_set_supported_modems(&fax.t30, T30_SUPPORT_V29 | T30_SUPPORT_V27TER | T30_SUPPORT_V17 );
+    } else {
+		t30_set_supported_modems(&fax.t30, T30_SUPPORT_V29 | T30_SUPPORT_V27TER);
+    }
+
+	/* Support for different image sizes && resolutions */
+	t30_set_supported_image_sizes(&fax.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(&fax.t30, T30_SUPPORT_STANDARD_RESOLUTION | T30_SUPPORT_FINE_RESOLUTION | T30_SUPPORT_SUPERFINE_RESOLUTION
+			| T30_SUPPORT_R8_RESOLUTION | T30_SUPPORT_R16_RESOLUTION);
+
+    /* set phase handlers callbaks */
+	t30_set_phase_b_handler(&fax.t30, phase_b_handler, NULL);
+    t30_set_phase_d_handler(&fax.t30, phase_d_handler, NULL);
+    t30_set_phase_e_handler(&fax.t30, phase_e_handler, channel);
+
+    // TODO: ?? what this does ??
     write_frame.codec = &write_codec;
     write_frame.data = buf;
 
+    /*
+     * now we enter a loop where we read audio frames to the channels and will pass it to spandsp
+     * and if there is some outgoing frame we'll send it back to the calling fax machine
+     */
     while(switch_channel_ready(channel)) {
+
+        // read new audio frame from the channel
         status = switch_core_session_read_frame(session, &read_frame, -1, 0);
         if (!SWITCH_READ_ACCEPTABLE(status)) {
             goto done;
         }
 
+        // pass the new incoming audio frame to the fax_rx application
         fax_rx(&fax, (int16_t *)read_frame->data, read_frame->samples);
         if ((tx = fax_tx(&fax, (int16_t *) &buf, write_codec.implementation->samples_per_frame)) < 0) {
             switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fax Error\n");
@@ -192,9 +343,11 @@
 
  done:
 
+    // shutdown spandsp so it can create our tiff
     t30_terminate(&fax.t30);
     fax_release(&fax);
     
+    // restore the original codecs over the channels
     if (read_codec.implementation) {
         switch_core_codec_destroy(&read_codec);
     }
@@ -217,6 +370,10 @@
     *module_interface = switch_loadable_module_create_module_interface(pool, modname);
     
     SWITCH_ADD_APP(app_interface, "rxfax", "Trivial FAX Receive Application", "Trivial FAX Receive Application", rxfax_function, "", SAF_NONE);
+
+    /* TODO: its important to debug the exact spandsp used
+	"RxFax using spandsp %i %i\n", SPANDSP_RELEASE_DATE, SPANDSP_RELEASE_TIME 
+    */
     
 	/* indicate that the module should continue to be loaded */
 	return SWITCH_STATUS_SUCCESS;



More information about the Freeswitch-svn mailing list