NVFaxDetect
Created by: justin_newman,Last modification on Thu 22 of Nov, 2007 [17:51 UTC] by jq53333
Fax Detection for IAX/SIP/ZAP
Including non-silence and digit detection.Download
You can find the code and more instructions on the web.Synopsis
Detects fax sounds on all channel types (on IAX and SIP too)Description
NVFaxDetect([waitdur[|options[|sildur[|mindur[|maxdur]]]]])This application listens for fax tones (on IAX and SIP channels too) for waitdur seconds of time. In addition, it can be interrupted by digits, or non-silence. Audio is only monitored in the receive direction. If digits interrupt, they must be the start of a valid extension unless the option is included to ignore. If fax is detected, it will jump to the 'fax' extension. If a period of non-silence greater than 'mindur' ms, yet less than 'maxdur' ms is followed by silence at least 'sildur' ms then the app is aborted and jumps to the 'talk' extension. If all undetected, control will continue at the next priority.
Parameters
waitdur: Maximum number of seconds to wait (default=4)options:
'n': Attempt on-hook if unanswered (default=no)
'x': DTMF digits terminate without extension (default=no)
'd': Ignore DTMF digit detection (default=no)
'f': Ignore fax detection (default=no)
't': Ignore talk detection (default=no)
sildur: Silence ms after mindur/maxdur before aborting (default=1000)
mindur: Minimum non-silence ms needed (default=100)
maxdur: Maximum non-silence ms allowed (default=0/forever)
Return codes
Returns -1 on hangup, and 0 on successful completion with no exit conditions.Notes
This code is NOT included with Asterisk at this point, however it is free. To get it, please search for it on the web.This should only work on channels that are using ULAW/ALAW.
Requirements
- Asterisk development or stable
Sample Usage (extensions.conf)
[context-incoming]
; Answer and do some detection work
exten => s,1,Answer
exten => s,2,NVFaxDetect()
exten => s,3,Hangup
; If user presses "1", dial main line
exten => 1,1,Dial(SIP/5500)
exten => 1,2,Hangup
; If this is a fax, dial fax line
exten => fax,1,Dial(SIP/5501)
exten => fax,2,Hangup
; If user is talking, send him to Debra
exten => talk,1,Dial(SIP/5502)
exten => talk,2,Hangup
; Answer and do some detection work
exten => s,1,Answer
exten => s,2,NVFaxDetect()
exten => s,3,Hangup
; If user presses "1", dial main line
exten => 1,1,Dial(SIP/5500)
exten => 1,2,Hangup
; If this is a fax, dial fax line
exten => fax,1,Dial(SIP/5501)
exten => fax,2,Hangup
; If user is talking, send him to Debra
exten => talk,1,Dial(SIP/5502)
exten => talk,2,Hangup
Another Example (extensions.conf)
; ring extensions with fax detect
[macro-faxexten]
exten => s,1,Dial(${ARG1},17) ; wait 3 rings
exten => s,2,Answer
exten => s,3,Playtones(ring) ; play fake ring so caller doesn't wonder whats going on
exten => s,4,NVFaxDetect(4|dt) ; while playing ring sound, detect faxes for 4 seconds (goes to "fax" extension if detected)
exten => s,5,Goto(s-${DIALSTATUS},1) ; if no fax, branch on dialstatus
exten => s-NOANSWER,1,Voicemail(u${MACRO_EXTEN})
exten => s-NOANSWER,2,Hangup()
exten => s-BUSY,1,Voicemail(b${MACRO_EXTEN})
exten => s-BUSY,2,Hangup()
exten => _s-.,1,Goto(s-NOANSWER,1) ; everything else is treated as no answer
; If this is a fax, get it
exten => fax,1,StopPlaytones ; you must do this or it will play ring sounds over your fax
exten => fax,2,SetVar(FAXFILE=/var/spool/asterisk/fax/${DNID}_${UNIQUEID})
exten => fax,3,Goto(fax,s,1)
[fax]
exten => s,1,rxfax(${FAXFILE}.tif)
exten => h,1,DEADAGI(faxtoemail.agi|${FAXFILE}.tif) ; you need to download the faxtoemail script or something similar here.
[macro-faxexten]
exten => s,1,Dial(${ARG1},17) ; wait 3 rings
exten => s,2,Answer
exten => s,3,Playtones(ring) ; play fake ring so caller doesn't wonder whats going on
exten => s,4,NVFaxDetect(4|dt) ; while playing ring sound, detect faxes for 4 seconds (goes to "fax" extension if detected)
exten => s,5,Goto(s-${DIALSTATUS},1) ; if no fax, branch on dialstatus
exten => s-NOANSWER,1,Voicemail(u${MACRO_EXTEN})
exten => s-NOANSWER,2,Hangup()
exten => s-BUSY,1,Voicemail(b${MACRO_EXTEN})
exten => s-BUSY,2,Hangup()
exten => _s-.,1,Goto(s-NOANSWER,1) ; everything else is treated as no answer
; If this is a fax, get it
exten => fax,1,StopPlaytones ; you must do this or it will play ring sounds over your fax
exten => fax,2,SetVar(FAXFILE=/var/spool/asterisk/fax/${DNID}_${UNIQUEID})
exten => fax,3,Goto(fax,s,1)
[fax]
exten => s,1,rxfax(${FAXFILE}.tif)
exten => h,1,DEADAGI(faxtoemail.agi|${FAXFILE}.tif) ; you need to download the faxtoemail script or something similar here.
Installation
Easiest way to get up and running:
(1) Drop the code in your /usr/src/asterisk/apps directory
(2) Edit the Makefile in the apps directory. Add the following line:
APPS+=app_nv_faxdetect.so
(3) Go to /usr/src/asterisk and run "make", then run "make install"
If you encounter compiler errors like:
error: struct ast_channel has no member cid
Edit app_nv_faxdetect.c and enable the CALLERID_FIELD #define statement:
#define CALLERID_FIELD cid.cid_num
Comment out the other, existing definition.
(4) Start or restart Asterisk
(5) Type "show application nvfaxdetect" from the CLI and you should see it
(1) Drop the code in your /usr/src/asterisk/apps directory
(2) Edit the Makefile in the apps directory. Add the following line:
APPS+=app_nv_faxdetect.so
(3) Go to /usr/src/asterisk and run "make", then run "make install"
If you encounter compiler errors like:
error: struct ast_channel has no member cid
Edit app_nv_faxdetect.c and enable the CALLERID_FIELD #define statement:
#define CALLERID_FIELD cid.cid_num
Comment out the other, existing definition.
(4) Start or restart Asterisk
(5) Type "show application nvfaxdetect" from the CLI and you should see it
There may be a better way to do this; however, this is what I used to get it working under FreeBSD.
Now cd /usr/ports/distfiles
go ahead and extract the asterisk-1.2.7.1.tar.gz file with
tar -zxvf asterisk-1.2.7.1.tar.gz
cd asterisk-1.2.7.1/apps
vi Makefile
add the following to the list of modules to compile app_nv_backgrounddetect.so app_nv_faxdetectc.so
write the file
Then add the following before file.h in in both app_nv_backgrounddetect.c and app_nv_faxdetect.c
- include <stdio.h>
cd ../../..
(You should now be in /usr/ports/distfiles/ )
tar cf asterisk-1.2.7.1.tar asterisk-1.2.7.1
and then compress with
gzip asterisk-1.2.7.1.tar (if it exists, you will need to write over it)
Now cd /usr/ports/net/asterisk
rename the distinfo file to distinfo.old so it wont get used when you begin compiling.
make clean ; make deinstall ; make ; make install
asterisk -vvvgc
asterisk> show modules
You should see the two modules now.
-Greg
Future Improvements
We are finishing up outgoing fax detect. Look for the patch or new file there.See also
- NewmanTelOnAsterisk14 - Instructions to modify Newman Telecom modules for use on Asterisk 1.4.0
- NVBackgroundDetect - Detects fax, talk, DMTF, and other tones on IAX, SIP, ZAP, and other channels
- NVLineDetect - Detects answer, busy, congestion, dialtone, dead, and others on IAX, SIP, ZAP, and other channels
- NVFaxEmail - Integration of SpanDSP, RxFax, and e-mail; quick and simple to use without scripts
- NVMachineDetect - Answering machine, PBX, and talk detection
- Asterisk fax
- Asterisk Fax to email
- Asterisk spandsp - Sending and receiving fax directly with Asterisk
- Hylafax - Another fax solution
- T.38 - The present future for fax over IP (FoIP)
- Receiving and sending faxes with asterisk 1.4, NVFaxDetect, iaxmodem and hylafax


Comments
333Re. the 'Fax detected by no fax extension' issue
FWIW, the default zapata.conf in TrixBox 2.0 puts incoming Zap calls in the "from-zaptel" context. While "from-pstn" has the proper fax extension, from-zaptel does not. Once we added the line:
exten => fax,1,Goto(ext-fax,in_fax,1)
to from-zaptel, things started working.
333Detecting but no fax extension
2006-04-22 12:08:17 NOTICE15648: app_nv_faxdetect.c:216 nv_detectfax_exec: Redirecting SIP/gw02.uk.sipgate.net-4191b8c0 to fax extension
2006-04-22 12:08:17 WARNING15648: app_nv_faxdetect.c:224 nv_detectfax_exec: Fax detected, but no fax extension
This confuses me as I clearly have a fax extension
...........
Much later ...... I seem to have got over this with a version upgrade and building from source.
Now on to why rxfax is failing!
==John ff
333Not detecting fax
Have followed directions and installed as per above and I get this in the log
Mar 14 21:29:05 VERBOSE5228 logger.c: — Executing Goto("IAX2/FreeWorldTel-Out-3", "from-pstn-afthours|s|1") in new stack
Mar 14 21:29:05 VERBOSE5228 logger.c: — Goto (from-pstn-afthours,s,1)
Mar 14 21:29:05 DEBUG5228 pbx.c: Expression result is '0'
Mar 14 21:29:05 VERBOSE5228 logger.c: — Executing GotoIf("IAX2/FreeWorldTel-Out-3", "0?from-pstn-afthours-nofax|s|1:2") in new stack
Mar 14 21:29:05 VERBOSE5228 logger.c: — Goto (from-pstn-afthours,s,2)
Mar 14 21:29:05 VERBOSE5228 logger.c: — Executing Answer("IAX2/FreeWorldTel-Out-3", "") in new stack
Mar 14 21:29:05 VERBOSE5228 logger.c: — Executing PlayTones("IAX2/FreeWorldTel-Out-3", "ring") in new stack
Mar 14 21:29:05 DEBUG5228 channel.c: Scheduling timer at 160 sample intervals
Mar 14 21:29:05 VERBOSE5228 logger.c: — Executing NVFaxDetect("IAX2/FreeWorldTel-Out-3", "40") in new stack
Mar 14 21:29:05 DEBUG5228 app_nv_faxdetect.c: Preparing detect of fax (waitdur=40ms, sildur=1000ms, mindur=100ms, maxdur=-1ms)
Mar 14 21:29:05 DEBUG5059 chan_iax2.c: Ooh, voice format changed to 16
Mar 14 21:29:05 DEBUG5228 channel.c: Generator got voice, switching to phase locked mode
Mar 14 21:29:05 DEBUG5228 channel.c: Scheduling timer at 0 sample intervals
Mar 14 21:29:05 DEBUG5228 app_nv_faxdetect.c: Start of voice token!
Mar 14 21:29:06 DEBUG5228 app_nv_faxdetect.c: Found unqualified token of 0 ms
Mar 14 21:29:07 DEBUG5228 app_nv_faxdetect.c: Start of voice token!
Mar 14 21:29:08 DEBUG5228 app_nv_faxdetect.c: Found unqualified token of 0 ms
Mar 14 21:29:09 DEBUG5228 app_nv_faxdetect.c: Start of voice token!
Mar 14 21:29:10 DEBUG5228 app_nv_faxdetect.c: Found unqualified token of 0 ms
Mar 14 21:29:11 DEBUG5228 app_nv_faxdetect.c: Start of voice token!
Mar 14 21:29:12 DEBUG5228 app_nv_faxdetect.c: Found unqualified token of 0 ms
Mar 14 21:29:13 DEBUG5228 app_nv_faxdetect.c: Start of voice token!
Mar 14 21:29:14 DEBUG5228 app_nv_faxdetect.c: Found unqualified token of 0 ms
Mar 14 21:29:15 DEBUG5228 app_nv_faxdetect.c: Start of voice token!
Mar 14 21:29:16 DEBUG5228 app_nv_faxdetect.c: Found unqualified token of 20 ms
Mar 14 21:29:17 DEBUG5228 app_nv_faxdetect.c: Start of voice token!
Mar 14 21:29:18 DEBUG5228 app_nv_faxdetect.c: Found unqualified token of 0 ms
Mar 14 21:29:19 DEBUG5228 app_nv_faxdetect.c: Start of voice token!
Mar 14 21:29:20 DEBUG5228 app_nv_faxdetect.c: Found unqualified token of 0 ms
Mar 14 21:29:21 DEBUG5228 app_nv_faxdetect.c: Start of voice token!
Mar 14 21:29:22 DEBUG5228 app_nv_faxdetect.c: Found unqualified token of 19 ms
Mar 14 21:29:25 DEBUG5228 app_nv_faxdetect.c: Start of voice token!
Mar 14 21:29:26 DEBUG5228 app_nv_faxdetect.c: Found unqualified token of 0 ms
Mar 14 21:29:27 DEBUG5228 app_nv_faxdetect.c: Start of voice token!
Mar 14 21:29:28 DEBUG5228 app_nv_faxdetect.c: Found unqualified token of 0 ms
Mar 14 21:29:31 DEBUG5228 app_nv_faxdetect.c: Start of voice token!
Mar 14 21:29:32 DEBUG5228 app_nv_faxdetect.c: Found unqualified token of 0 ms
Mar 14 21:29:33 DEBUG5228 app_nv_faxdetect.c: Start of voice token!
Mar 14 21:29:34 DEBUG5228 app_nv_faxdetect.c: Found unqualified token of 0 ms
Mar 14 21:29:35 DEBUG5059 chan_iax2.c: Received VNAK: resending outstanding frames
Mar 14 21:29:37 DEBUG5228 app_nv_faxdetect.c: Start of voice token!
Mar 14 21:29:38 DEBUG5228 app_nv_faxdetect.c: Found unqualified token of 0 ms
an_iax2.c: Ooh, voice format changed to 16Mar 14 21:29:39 DEBUG5228 app_nv_faxdetect.c: Start of voice token!
Mar 14 21:29:40 DEBUG5228 app_nv_faxdetect.c: Found unqualified token of 0 ms
I even changed the detection time to 40MS!! Still won't pick-up. Any Ideas?
333Cannot Get Registered?
Been working with Asterisk for awhile now, but can't get the application to register. Seems to compile fine with make && make install, but will not show as registered. Any suggestions? Thanks!
333Re: Should be in CVS
333NVFaxDetect on Asterisk 1.2 (and/or AMP or @home Beta)
1.2 (and/or AMP or @home Beta), make the following changes:
1) Above the following line near the top, in both files:
#include <asterisk/lock.h>
Add:
#include <stdio.h>
2) In NVBackgroundDetect, to get rid of the trigraph warning, search for
"??)" and replace it with "?)".
3) Rebuild Asterisk from /usr/src/asterisk with "make && make install".
4) Restart Asterisk with "restart now" from the CLI.
The new release will have this modification.
Justin
333Simpler way to install on Debian (Sarge)
Install the official sarge asterisk packages : apt-get install asterisk asterisk-dev
Download app_nv_faxdetect.c, edit and replace "#define CALLERID_FIELD cid.cid_num" by "#define CALLERID_FIELD callerid"
Compile it using "gcc -D_GNU_SOURCE -shared -o app_nv_faxdetect.so app_nv_faxdetect.c"
Copy the resulting app_nv_faxdetect.so to /usr/lib/asterisk/modules/
Restart asterisk and enjoy :-)
333Re: Should be in CVS
333Should be in CVS
This app rocks!
Not everyone gets their faxes through a ZAP card, or can assign a DID just for fax.
Bjorn