2001-04-13 02:12:18 +00:00
|
|
|
/*
|
|
|
|
* Asterisk -- A telephony toolkit for Linux.
|
|
|
|
*
|
|
|
|
* Trivial application to record a sound file
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001, Linux Support Services, Inc.
|
|
|
|
*
|
|
|
|
* Matthew Fredrickson <creslin@linux-support.net>
|
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
|
|
|
* the GNU General Public License
|
|
|
|
*/
|
|
|
|
|
2002-06-19 02:35:40 +00:00
|
|
|
#include <asterisk/lock.h>
|
2001-04-13 02:12:18 +00:00
|
|
|
#include <asterisk/file.h>
|
|
|
|
#include <asterisk/logger.h>
|
|
|
|
#include <asterisk/channel.h>
|
|
|
|
#include <asterisk/pbx.h>
|
|
|
|
#include <asterisk/module.h>
|
|
|
|
#include <asterisk/translate.h>
|
2003-04-28 05:07:52 +00:00
|
|
|
#include <asterisk/dsp.h>
|
2001-04-13 02:12:18 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
static char *tdesc = "Trivial Record Application";
|
|
|
|
|
|
|
|
static char *app = "Record";
|
|
|
|
|
|
|
|
static char *synopsis = "Record to a file";
|
|
|
|
|
|
|
|
static char *descrip =
|
2004-06-30 16:04:28 +00:00
|
|
|
" Record(filename:format|silence[|maxduration][|option])\n\n"
|
|
|
|
"Records from the channel into a given filename. If the file exists it will\n"
|
|
|
|
"be overwritten.\n"
|
2004-02-25 22:31:51 +00:00
|
|
|
"- 'format' is the format of the file type to be recorded (wav, gsm, etc).\n"
|
2004-06-30 16:04:28 +00:00
|
|
|
"- 'silence' is the number of seconds of silence to allow before returning.\n"
|
|
|
|
"- 'maxduration' is the maximum recording duration in seconds. If missing\n"
|
|
|
|
"or 0 there is no maximum.\n"
|
|
|
|
"- 'option' may be 'skip' to return immediately if the line is not up,\n"
|
|
|
|
"or 'noanswer' to attempt to record even if the line is not up.\n\n"
|
2004-01-30 05:13:25 +00:00
|
|
|
"If filename contains '%d', these characters will be replaced with a number\n"
|
|
|
|
"incremented by one each time the file is recorded. \n\n"
|
2004-02-25 22:31:51 +00:00
|
|
|
"Formats: g723, g729, gsm, h263, ulaw, alaw, vox, wav, WAV\n\n"
|
2004-05-25 15:53:28 +00:00
|
|
|
"User can press '#' to terminate the recording and continue to the next priority.\n\n"
|
2001-07-26 16:36:23 +00:00
|
|
|
"Returns -1 when the user hangs up.\n";
|
2001-04-13 02:12:18 +00:00
|
|
|
|
|
|
|
STANDARD_LOCAL_USER;
|
|
|
|
|
|
|
|
LOCAL_USER_DECL;
|
|
|
|
|
|
|
|
static int record_exec(struct ast_channel *chan, void *data)
|
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
int count = 0;
|
|
|
|
int percentflag = 0;
|
|
|
|
char fil[256];
|
|
|
|
char tmp[256];
|
|
|
|
char ext[10];
|
|
|
|
char * vdata; /* Used so I don't have to typecast every use of *data */
|
|
|
|
int i = 0;
|
|
|
|
int j = 0;
|
2003-04-28 05:07:52 +00:00
|
|
|
|
2001-04-13 02:12:18 +00:00
|
|
|
struct ast_filestream *s = '\0';
|
|
|
|
struct localuser *u;
|
2002-06-19 02:35:40 +00:00
|
|
|
struct ast_frame *f = NULL;
|
2001-04-13 02:12:18 +00:00
|
|
|
|
2003-10-28 14:07:19 +00:00
|
|
|
struct ast_dsp *sildet = NULL; /* silence detector dsp */
|
2003-04-28 05:07:52 +00:00
|
|
|
int totalsilence = 0;
|
|
|
|
int dspsilence = 0;
|
|
|
|
int silence = 0; /* amount of silence to allow */
|
|
|
|
int gotsilence = 0; /* did we timeout for silence? */
|
|
|
|
char silencestr[5];
|
2004-06-30 16:04:28 +00:00
|
|
|
char durationstr[8];
|
|
|
|
int maxduration = 0; /* max duration of recording */
|
|
|
|
int gottimeout = 0; /* did we timeout for maxduration exceeded? */
|
|
|
|
time_t timeout = 0;
|
|
|
|
char option[16];
|
|
|
|
int option_skip = 0;
|
|
|
|
int option_noanswer = 0;
|
2003-11-23 22:14:32 +00:00
|
|
|
int rfmt = 0;
|
2003-04-28 05:07:52 +00:00
|
|
|
|
2001-04-13 02:12:18 +00:00
|
|
|
vdata = data; /* explained above */
|
|
|
|
|
|
|
|
/* The next few lines of code parse out the filename and header from the input string */
|
|
|
|
if (!vdata) { /* no data implies no filename or anything is present */
|
|
|
|
ast_log(LOG_WARNING, "Record requires an argument (filename)\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-03-28 03:28:03 +00:00
|
|
|
for (; vdata[i] && (vdata[i] != ':') && (vdata[i] != '|'); i++ ) {
|
2001-04-13 02:12:18 +00:00
|
|
|
if ((vdata[i] == '%') && (vdata[i+1] == 'd')) {
|
|
|
|
percentflag = 1; /* the wildcard is used */
|
|
|
|
}
|
2004-06-30 16:04:28 +00:00
|
|
|
if (j < sizeof(fil) - 1)
|
|
|
|
fil[j++] = vdata[i];
|
2001-04-13 02:12:18 +00:00
|
|
|
}
|
2004-06-30 16:04:28 +00:00
|
|
|
fil[j] = '\0';
|
2001-04-13 02:12:18 +00:00
|
|
|
|
2004-06-30 16:04:28 +00:00
|
|
|
if (vdata[i] != ':') {
|
|
|
|
ast_log(LOG_WARNING, "No extension found\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
|
|
|
|
j = 0;
|
|
|
|
for (; vdata[i] && (vdata[i] != '|'); i++)
|
|
|
|
if (j < sizeof(ext) - 1)
|
|
|
|
ext[j++] = vdata[i];
|
2001-04-13 02:12:18 +00:00
|
|
|
ext[j] = '\0';
|
2003-04-28 05:07:52 +00:00
|
|
|
|
2004-06-30 16:04:28 +00:00
|
|
|
if (vdata[i] == '|')
|
|
|
|
i++;
|
|
|
|
|
|
|
|
j = 0;
|
|
|
|
for (; vdata[i] && (vdata[i] != '|'); i++)
|
|
|
|
if (j < sizeof(silencestr) - 1)
|
|
|
|
silencestr[j++] = vdata[i];
|
|
|
|
silencestr[j] = '\0';
|
2003-04-28 05:07:52 +00:00
|
|
|
|
2004-06-30 16:04:28 +00:00
|
|
|
if (j > 0) {
|
2003-04-28 05:07:52 +00:00
|
|
|
silence = atoi(silencestr);
|
|
|
|
if (silence > 0)
|
|
|
|
silence *= 1000;
|
|
|
|
}
|
|
|
|
|
2004-06-30 16:04:28 +00:00
|
|
|
if (vdata[i] == '|')
|
|
|
|
i++;
|
|
|
|
|
|
|
|
j = 0;
|
|
|
|
for (; vdata[i] && (vdata[i] != '|'); i++)
|
|
|
|
if (j < sizeof(durationstr) - 1)
|
|
|
|
durationstr[j++] = vdata[i];
|
|
|
|
durationstr[j] = '\0';
|
|
|
|
|
|
|
|
if (j > 0)
|
|
|
|
maxduration = atoi(durationstr);
|
|
|
|
|
|
|
|
if (vdata[i] == '|')
|
|
|
|
i++;
|
|
|
|
|
|
|
|
j = 0;
|
|
|
|
for (; vdata[i] && (vdata[i] != '|'); i++)
|
|
|
|
if (j < sizeof(option) - 1)
|
|
|
|
option[j++] = vdata[i];
|
|
|
|
option[j] = '\0';
|
|
|
|
|
|
|
|
if (!strcasecmp(option, "skip"))
|
|
|
|
option_skip = 1;
|
|
|
|
if (!strcasecmp(option, "noanswer"))
|
|
|
|
option_noanswer = 1;
|
|
|
|
|
2001-04-13 02:12:18 +00:00
|
|
|
/* done parsing */
|
2004-06-30 16:04:28 +00:00
|
|
|
|
2001-04-13 02:12:18 +00:00
|
|
|
|
|
|
|
/* these are to allow the use of the %d in the config file for a wild card of sort to
|
|
|
|
create a new file with the inputed name scheme */
|
|
|
|
if (percentflag) {
|
|
|
|
do {
|
2004-07-14 07:34:34 +00:00
|
|
|
snprintf(tmp, sizeof(tmp), fil, count);
|
2001-04-13 02:12:18 +00:00
|
|
|
count++;
|
|
|
|
} while ( ast_fileexists(tmp, ext, chan->language) != -1 );
|
2004-03-22 04:04:07 +00:00
|
|
|
pbx_builtin_setvar_helper(chan, "RECORDED_FILE", tmp);
|
2001-04-13 02:12:18 +00:00
|
|
|
} else
|
2004-06-30 16:04:28 +00:00
|
|
|
strncpy(tmp, fil, sizeof(tmp)-1);
|
2001-04-13 02:12:18 +00:00
|
|
|
/* end of routine mentioned */
|
|
|
|
|
|
|
|
LOCAL_USER_ADD(u);
|
|
|
|
|
2002-07-12 09:03:50 +00:00
|
|
|
if (chan->_state != AST_STATE_UP) {
|
2004-06-30 16:04:28 +00:00
|
|
|
if (option_skip) {
|
|
|
|
/* At the user's option, skip if the line is not up */
|
|
|
|
LOCAL_USER_REMOVE(u);
|
|
|
|
return 0;
|
|
|
|
} else if (!option_noanswer) {
|
|
|
|
/* Otherwise answer unless we're supposed to record while on-hook */
|
|
|
|
res = ast_answer(chan);
|
|
|
|
}
|
2001-04-13 02:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!res) {
|
|
|
|
/* Some code to play a nice little beep to signify the start of the record operation */
|
|
|
|
res = ast_streamfile(chan, "beep", chan->language);
|
|
|
|
if (!res) {
|
|
|
|
res = ast_waitstream(chan, "");
|
|
|
|
} else {
|
|
|
|
ast_log(LOG_WARNING, "ast_streamfile failed on %s\n", chan->name);
|
|
|
|
}
|
|
|
|
ast_stopstream(chan);
|
2003-04-28 05:07:52 +00:00
|
|
|
|
2001-04-13 02:12:18 +00:00
|
|
|
/* The end of beep code. Now the recording starts */
|
2003-04-28 05:07:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (silence > 0) {
|
|
|
|
rfmt = chan->readformat;
|
2004-04-06 22:17:32 +00:00
|
|
|
res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
|
2003-04-28 05:07:52 +00:00
|
|
|
if (res < 0) {
|
|
|
|
ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sildet = ast_dsp_new();
|
|
|
|
if (!sildet) {
|
|
|
|
ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ast_dsp_set_threshold(sildet, 256);
|
|
|
|
}
|
|
|
|
|
2001-04-13 02:12:18 +00:00
|
|
|
s = ast_writefile( tmp, ext, NULL, O_CREAT|O_TRUNC|O_WRONLY , 0, 0644);
|
|
|
|
|
|
|
|
if (s) {
|
2004-06-30 16:04:28 +00:00
|
|
|
if (maxduration > 0)
|
|
|
|
timeout = time(NULL) + (time_t)maxduration;
|
|
|
|
|
2002-06-19 02:35:40 +00:00
|
|
|
while (ast_waitfor(chan, -1) > -1) {
|
2004-06-30 16:04:28 +00:00
|
|
|
if (maxduration > 0 && time(NULL) > timeout) {
|
|
|
|
gottimeout = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-06-19 02:35:40 +00:00
|
|
|
f = ast_read(chan);
|
|
|
|
if (!f) {
|
|
|
|
res = -1;
|
|
|
|
break;
|
|
|
|
}
|
2001-04-13 02:12:18 +00:00
|
|
|
if (f->frametype == AST_FRAME_VOICE) {
|
|
|
|
res = ast_writestream(s, f);
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
ast_log(LOG_WARNING, "Problem writing frame\n");
|
|
|
|
break;
|
|
|
|
}
|
2003-04-28 05:07:52 +00:00
|
|
|
|
|
|
|
if (silence > 0) {
|
|
|
|
dspsilence = 0;
|
|
|
|
ast_dsp_silence(sildet, f, &dspsilence);
|
|
|
|
if (dspsilence) {
|
|
|
|
totalsilence = dspsilence;
|
|
|
|
} else {
|
|
|
|
totalsilence = 0;
|
|
|
|
}
|
|
|
|
if (totalsilence > silence) {
|
|
|
|
/* Ended happily with silence */
|
|
|
|
ast_frfree(f);
|
|
|
|
gotsilence = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-04-13 02:12:18 +00:00
|
|
|
}
|
2003-06-29 03:24:39 +00:00
|
|
|
if (f->frametype == AST_FRAME_VIDEO) {
|
|
|
|
res = ast_writestream(s, f);
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
ast_log(LOG_WARNING, "Problem writing frame\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-07-26 16:36:23 +00:00
|
|
|
if ((f->frametype == AST_FRAME_DTMF) &&
|
|
|
|
(f->subclass == '#')) {
|
|
|
|
ast_frfree(f);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ast_frfree(f);
|
2001-04-13 02:12:18 +00:00
|
|
|
}
|
|
|
|
if (!f) {
|
|
|
|
ast_log(LOG_DEBUG, "Got hangup\n");
|
|
|
|
res = -1;
|
|
|
|
}
|
2003-04-28 05:07:52 +00:00
|
|
|
|
|
|
|
if (gotsilence) {
|
|
|
|
ast_stream_rewind(s, silence-1000);
|
|
|
|
ast_truncstream(s);
|
2004-06-30 16:04:28 +00:00
|
|
|
} else if (!gottimeout) {
|
2003-04-28 05:07:52 +00:00
|
|
|
/* Strip off the last 1/4 second of it */
|
|
|
|
ast_stream_rewind(s, 250);
|
|
|
|
ast_truncstream(s);
|
|
|
|
}
|
2001-04-13 02:12:18 +00:00
|
|
|
ast_closestream(s);
|
|
|
|
} else
|
|
|
|
ast_log(LOG_WARNING, "Could not create file %s\n", fil);
|
|
|
|
} else
|
|
|
|
ast_log(LOG_WARNING, "Could not answer channel '%s'\n", chan->name);
|
|
|
|
|
|
|
|
LOCAL_USER_REMOVE(u);
|
2003-04-28 05:07:52 +00:00
|
|
|
if (silence > 0) {
|
2004-04-06 22:17:32 +00:00
|
|
|
res = ast_set_read_format(chan, rfmt);
|
2003-04-28 05:07:52 +00:00
|
|
|
if (res)
|
|
|
|
ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", chan->name);
|
2003-10-28 14:07:19 +00:00
|
|
|
if (sildet)
|
|
|
|
ast_dsp_free(sildet);
|
2003-04-28 05:07:52 +00:00
|
|
|
}
|
2001-04-13 02:12:18 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int unload_module(void)
|
|
|
|
{
|
|
|
|
STANDARD_HANGUP_LOCALUSERS;
|
|
|
|
return ast_unregister_application(app);
|
|
|
|
}
|
|
|
|
|
|
|
|
int load_module(void)
|
|
|
|
{
|
|
|
|
return ast_register_application(app, record_exec, synopsis, descrip);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *description(void)
|
|
|
|
{
|
|
|
|
return tdesc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usecount(void)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
STANDARD_USECOUNT(res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *key()
|
|
|
|
{
|
|
|
|
return ASTERISK_GPL_KEY;
|
|
|
|
}
|