don't play any sounds when using the quiet option (bug #4348)
[asterisk/asterisk.git] / apps / app_chanspy.c
index c7ea3aa..292a151 100755 (executable)
  * the GNU General Public License
  */
 
-#include <asterisk/file.h>
-#include <asterisk/logger.h>
-#include <asterisk/channel.h>
-#include <asterisk/features.h>
-#include <asterisk/options.h>
-#include <asterisk/app.h>
-#include <asterisk/utils.h>
-#include <asterisk/say.h>
-#include <asterisk/pbx.h>
-#include <asterisk/translate.h>
-#include <asterisk/module.h>
-#include <asterisk/lock.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 #include <ctype.h>
 
+#include "asterisk.h"
+#include "asterisk/file.h"
+#include "asterisk/logger.h"
+#include "asterisk/channel.h"
+#include "asterisk/features.h"
+#include "asterisk/options.h"
+#include "asterisk/app.h"
+#include "asterisk/utils.h"
+#include "asterisk/say.h"
+#include "asterisk/pbx.h"
+#include "asterisk/translate.h"
+#include "asterisk/module.h"
+#include "asterisk/lock.h"
+
+#define AST_MONITOR_DIR AST_SPOOL_DIR "/monitor"
+
 AST_MUTEX_DEFINE_STATIC(modlock);
 
 #define ast_fit_in_short(in) (in < -32768 ? -32768 : in > 32767 ? 32767 : in)
@@ -44,7 +48,8 @@ static char *desc = "   Chanspy([<scanspec>][|<options>])\n\n"
 " - q: quiet, don't announce channels beep, etc.\n"
 " - b: bridged, only spy on channels involved in a bridged call.\n"
 " - v([-4..4]): adjust the initial volume. (negative is quieter)\n"
-" - g(grp): enforce group.  Match only calls where their ${SPYGROUP} is 'grp'.\n\n"
+" - g(grp): enforce group.  Match only calls where their ${SPYGROUP} is 'grp'.\n"
+" - r[(basename)]: Record session to monitor spool dir (with optional basename, default is 'chanspy')\n\n"
 "If <scanspec> is specified, only channel names *beginning* with that string will be scanned.\n"
 "('all' or an empty string are also both valid <scanspec>)\n\n"
 "While Spying:\n\n"
@@ -58,12 +63,14 @@ static char *desc = "   Chanspy([<scanspec>][|<options>])\n\n"
 #define OPTION_BRIDGED   (1 << 1)      /* Only look at bridged calls */
 #define OPTION_VOLUME    (1 << 2)      /* Specify initial volume */
 #define OPTION_GROUP     (1 << 3)   /* Only look at channels in group */
+#define OPTION_RECORD    (1 << 4)   /* Record */
 
 AST_DECLARE_OPTIONS(chanspy_opts,{
        ['q'] = { OPTION_QUIET },
        ['b'] = { OPTION_BRIDGED },
        ['v'] = { OPTION_VOLUME, 1 },
        ['g'] = { OPTION_GROUP, 2 },
+       ['r'] = { OPTION_RECORD, 3 },
 });
 
 STANDARD_LOCAL_USER;
@@ -93,10 +100,10 @@ struct chanspy_translation_helper {
        int samples;
        int rsamples;
        int volfactor;
+       int fd;
 };
 
 /* Prototypes */
-static struct ast_channel *local_get_channel_by_name(char *name);
 static struct ast_channel *local_get_channel_begin_name(char *name);
 static struct ast_channel *local_channel_walk(struct ast_channel *chan);
 static void spy_release(struct ast_channel *chan, void *data);
@@ -106,10 +113,11 @@ static void ast_flush_spy_queue(struct ast_channel_spy *spy);
 static int spy_generate(struct ast_channel *chan, void *data, int len, int samples);
 static void start_spying(struct ast_channel *chan, struct ast_channel *spychan, struct ast_channel_spy *spy);
 static void stop_spying(struct ast_channel *chan, struct ast_channel_spy *spy);
-static int channel_spy(struct ast_channel *chan, struct ast_channel *spyee, int *volfactor);
+static int channel_spy(struct ast_channel *chan, struct ast_channel *spyee, int *volfactor, int fd);
 static int chanspy_exec(struct ast_channel *chan, void *data);
 
 
+#if 0
 static struct ast_channel *local_get_channel_by_name(char *name) 
 {
        struct ast_channel *ret;
@@ -121,6 +129,7 @@ static struct ast_channel *local_get_channel_by_name(char *name)
 
        return ret;
 }
+#endif
 
 static struct ast_channel *local_channel_walk(struct ast_channel *chan) 
 {
@@ -250,13 +259,13 @@ static int spy_generate(struct ast_channel *chan, void *data, int len, int sampl
                if (f0 && csth->fmt0 && csth->fmt0 != f0->subclass) {
                        ast_translator_free_path(csth->trans0);
                        csth->trans0 = NULL;
-                       csth->fmt0 = csth->fmt0;
+                       csth->fmt0 = f0->subclass;
                }
 
                if (f1 && csth->fmt1 && csth->fmt1 != f1->subclass) {
                        ast_translator_free_path(csth->trans1);
                        csth->trans1 = NULL;
-                       csth->fmt1 = csth->fmt1;
+                       csth->fmt1 = f1->subclass;
                }
        
                if (!csth->fmt0 && f0) {
@@ -306,6 +315,20 @@ static int spy_generate(struct ast_channel *chan, void *data, int len, int sampl
                                        return 0;
                                }
                        } else {
+                               if (csth->len0 < f0->datalen) {
+                                       if (!csth->len0) {
+                                               if (!(csth->buf0 = malloc(f0->datalen * 2))) {
+                                                       csth->spy.status = CHANSPY_DONE;
+                                                       return -1;
+                                               }
+                                       } else {
+                                               if (!realloc(csth->buf0, f0->datalen * 2)) {
+                                                       csth->spy.status = CHANSPY_DONE;
+                                                       return -1;
+                                               }
+                                       }
+                                       csth->len0 = f0->datalen;
+                               }
                                memcpy(csth->buf0, f0->data, f0->datalen);
                                maxsamp = f0->samples;
                        }
@@ -316,12 +339,12 @@ static int spy_generate(struct ast_channel *chan, void *data, int len, int sampl
                                if ((f = ast_translate(csth->trans1, f1, 0))) {
                                        if (csth->len1 < f->datalen) {
                                                if (!csth->len1) {
-                                                       if (!(csth->buf1 = malloc(f->datalen))) {
+                                                       if (!(csth->buf1 = malloc(f->datalen * 2))) {
                                                                csth->spy.status = CHANSPY_DONE;
                                                                return -1;
                                                        }
                                                } else {
-                                                       if (!realloc(csth->buf1, f->datalen)) {
+                                                       if (!realloc(csth->buf1, f->datalen * 2)) {
                                                                csth->spy.status = CHANSPY_DONE;
                                                                return -1;
                                                        }
@@ -338,6 +361,20 @@ static int spy_generate(struct ast_channel *chan, void *data, int len, int sampl
                                        return 0;
                                }
                        } else {
+                               if (csth->len1 < f1->datalen) {
+                                       if (!csth->len1) {
+                                               if (!(csth->buf1 = malloc(f1->datalen * 2))) {
+                                                       csth->spy.status = CHANSPY_DONE;
+                                                       return -1;
+                                               }
+                                       } else {
+                                               if (!realloc(csth->buf1, f1->datalen * 2)) {
+                                                       csth->spy.status = CHANSPY_DONE;
+                                                       return -1;
+                                               }
+                                       }
+                                       csth->len1 = f1->datalen;
+                               }
                                memcpy(csth->buf1, f1->data, f1->datalen);
                                if (f1->samples > maxsamp) {
                                        maxsamp = f1->samples;
@@ -405,6 +442,9 @@ static int spy_generate(struct ast_channel *chan, void *data, int len, int sampl
                        csth->spy.status = CHANSPY_DONE;
                        return -1;
                }
+               if (csth->fd) {
+                       write(csth->fd, csth->frame.data, csth->frame.datalen);
+               }
 
                if (f0) {
                        ast_frfree(f0);
@@ -489,7 +529,7 @@ static void stop_spying(struct ast_channel *chan, struct ast_channel_spy *spy)
 
 }
 
-static int channel_spy(struct ast_channel *chan, struct ast_channel *spyee, int *volfactor) 
+static int channel_spy(struct ast_channel *chan, struct ast_channel *spyee, int *volfactor, int fd) 
 {
        struct chanspy_translation_helper csth;
        int running = 1, res = 0, x = 0;
@@ -511,6 +551,9 @@ static int channel_spy(struct ast_channel *chan, struct ast_channel *spyee, int
                csth.frame.subclass = AST_FORMAT_SLINEAR;
                csth.frame.datalen = 320;
                csth.frame.samples = 160;
+               if (fd) {
+                       csth.fd = fd;
+               }
                start_spying(spyee, chan, &csth.spy);
                ast_activate_generator(chan, &spygen, &csth);
 
@@ -587,7 +630,8 @@ static int chanspy_exec(struct ast_channel *chan, void *data)
                *options = NULL,
                *spec = NULL,
                *argv[5],
-               *mygroup = NULL;
+               *mygroup = NULL,
+               *recbase = NULL;
        int res = -1,
                volfactor = 0,
                silent = 0,
@@ -596,7 +640,10 @@ static int chanspy_exec(struct ast_channel *chan, void *data)
                chosen = 0,
                count=0,
                waitms = 100,
-               num = 0;
+               num = 0,
+               oldrf = 0,
+               oldwf = 0,
+               fd = 0;
        struct ast_flags flags;
 
 
@@ -605,6 +652,8 @@ static int chanspy_exec(struct ast_channel *chan, void *data)
                return -1;
        }
 
+       oldrf = chan->readformat;
+       oldwf = chan->writeformat;
        if (ast_set_read_format(chan, AST_FORMAT_SLINEAR) < 0) {
                ast_log(LOG_ERROR, "Could Not Set Read Format.\n");
                return -1;
@@ -632,11 +681,16 @@ static int chanspy_exec(struct ast_channel *chan, void *data)
        }
        
        if (options) {
-               char *opts[2];
+               char *opts[3];
                ast_parseoptions(chanspy_opts, &flags, opts, options);
                if (ast_test_flag(&flags, OPTION_GROUP)) {
                        mygroup = opts[1];
                }
+               if (ast_test_flag(&flags, OPTION_RECORD)) {
+                       if (!(recbase = opts[2])) {
+                               recbase = "chanspy";
+                       }
+               }
                silent = ast_test_flag(&flags, OPTION_QUIET);
                bronly = ast_test_flag(&flags, OPTION_BRIDGED);
                if (ast_test_flag(&flags, OPTION_VOLUME) && opts[1]) {
@@ -648,21 +702,31 @@ static int chanspy_exec(struct ast_channel *chan, void *data)
                }
        }
 
+       if (recbase) {
+               char filename[512];
+               snprintf(filename,sizeof(filename),"%s/%s.%ld.raw",AST_MONITOR_DIR, recbase, time(NULL));
+               if ((fd = open(filename, O_CREAT | O_WRONLY, O_TRUNC)) <= 0) {
+                       ast_log(LOG_WARNING, "Cannot open %s for recording\n", filename);
+                       fd = 0;
+               }
+       }
 
        for(;;) {
-               res = ast_streamfile(chan, "beep", chan->language);
-               if (!res)
-                       res = ast_waitstream(chan, "");
-               if (res < 0) {
-                       ast_clear_flag(chan, AST_FLAG_SPYING);
-                       ALL_DONE(u, -1);
-               }                       
+               if (!silent) {
+                       res = ast_streamfile(chan, "beep", chan->language);
+                       if (!res)
+                               res = ast_waitstream(chan, "");
+                       if (res < 0) {
+                               ast_clear_flag(chan, AST_FLAG_SPYING);
+                               break;
+                       }
+               }
 
                count = 0;
                res = ast_waitfordigit(chan, waitms);
                if (res < 0) {
                        ast_clear_flag(chan, AST_FLAG_SPYING);
-                       ALL_DONE(u, -1);
+                       break;
                }
                                
                peer = local_channel_walk(NULL);
@@ -714,10 +778,9 @@ static int chanspy_exec(struct ast_channel *chan, void *data)
                                                }
                                                count++;
                                                prev = peer;
-                                               res = channel_spy(chan, peer, &volfactor);
+                                               res = channel_spy(chan, peer, &volfactor, fd);
                                                if (res == -1) {
-                                                       ast_clear_flag(chan, AST_FLAG_SPYING);
-                                                       ALL_DONE(u, -1);
+                                                       break;
                                                } else if (res > 1 && spec) {
                                                        snprintf(name, AST_NAME_STRLEN, "%s/%d", spec, res);
                                                        if ((peer = local_get_channel_begin_name(name))) {
@@ -736,6 +799,18 @@ static int chanspy_exec(struct ast_channel *chan, void *data)
        }
        
 
+       if (fd > 0) {
+               close(fd);
+       }
+
+       if (oldrf && ast_set_read_format(chan, oldrf) < 0) {
+               ast_log(LOG_ERROR, "Could Not Set Read Format.\n");
+       }
+       
+       if (oldwf && ast_set_write_format(chan, oldwf) < 0) {
+               ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
+       }
+
        ast_clear_flag(chan, AST_FLAG_SPYING);
        ALL_DONE(u, res);
 }