Make ast_channel_walk become ast_channel_walk_locked
[asterisk/asterisk.git] / res / res_monitor.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <errno.h>
4 #include <string.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <libgen.h>             //dirname()
8
9 #include <asterisk/lock.h>
10 #include <asterisk/channel.h>
11 #include <asterisk/logger.h>
12 #include <asterisk/file.h>
13 #include <asterisk/pbx.h>
14 #include <asterisk/module.h>
15 #include <asterisk/manager.h>
16 #include <asterisk/cli.h>
17 #include <asterisk/monitor.h>
18
19 #define AST_MONITOR_DIR INSTALL_PREFIX "/var/spool/asterisk/monitor"
20
21 static ast_mutex_t monitorlock = AST_MUTEX_INITIALIZER;
22
23 static unsigned long seq = 0;
24
25 static char *monitor_synopsis = "Monitor a channel";
26
27 static char *monitor_descrip = "Monitor([file_format|[fname_base]|[options]]):\n"
28         "Used to start monitoring a channel. The channel's input and output\n"
29         "voice packets are logged to files until the channel hangs up or\n"
30         "monitoring is stopped by the StopMonitor application.\n"
31         "       file_format -- optional, if not set, defaults to \"wav\"\n"
32         "       fname_base -- if set, changes the filename used to the one specified.\n"
33         "       options:\n"
34         "               'm' - when the recording ends mix the two leg files into one using 'soxmix' utility which has to be installed on the system.\n";
35
36 static char *stopmonitor_synopsis = "Stop monitoring a channel";
37
38 static char *stopmonitor_descrip = "StopMonitor\n"
39         "Stops monitoring a channel. Has no effect if the channel is not monitored\n";
40
41 static char *changemonitor_synopsis = "Change monitoring filename of a channel";
42
43 static char *changemonitor_descrip = "ChangeMonitor\n"
44         "Changes monitoring filename of a channel. Has no effect if the channel is not monitored\n"
45         "The option string may contain the following:\n"
46         "       filename_base -- if set, changes the filename used to the one specified.\n";
47
48 /* Start monitoring a channel */
49 int ast_monitor_start(  struct ast_channel *chan, const char *format_spec,
50                                                 const char *fname_base, int need_lock )
51 {
52         int res = 0;
53         char tmp[256];
54
55         if( need_lock ) {
56                 if (ast_mutex_lock(&chan->lock)) {
57                         ast_log(LOG_WARNING, "Unable to lock channel\n");
58                         return -1;
59                 }
60         }
61
62         if( !(chan->monitor) ) {
63                 struct ast_channel_monitor *monitor;
64                 char *channel_name, *p;
65
66                 /* Create monitoring directory if needed */
67                 if( mkdir( AST_MONITOR_DIR, 0770 ) < 0 ) {
68                         if( errno != EEXIST ) {
69                                 ast_log(LOG_WARNING, "Unable to create audio monitor directory: %s\n",
70                                                 strerror( errno ) );
71                         }
72                 }
73
74                 monitor = malloc( sizeof( struct ast_channel_monitor ) );
75                 memset( monitor, 0, sizeof( struct ast_channel_monitor ) );
76
77                 /* Determine file names */
78                 if( fname_base && strlen( fname_base ) ) {
79                         int directory = strchr(fname_base, '/') ? 1 : 0;
80                         /* try creating the directory just in case it doesn't exist */
81                         if (directory) {
82                                 char *name = strdup(fname_base);
83                                 snprintf(tmp, sizeof(tmp), "mkdir -p %s",dirname(name));
84                                 free(name);
85                                 system(tmp);
86                         }
87                         snprintf(       monitor->read_filename, FILENAME_MAX, "%s/%s-in",
88                                                 directory ? "" : AST_MONITOR_DIR, fname_base );
89                         snprintf(       monitor->write_filename, FILENAME_MAX, "%s/%s-out",
90                                                 directory ? "" : AST_MONITOR_DIR, fname_base );
91                         strncpy(monitor->filename_base, fname_base, sizeof(monitor->filename_base) - 1);
92                 } else {
93                         ast_mutex_lock( &monitorlock );
94                         snprintf(       monitor->read_filename, FILENAME_MAX, "%s/audio-in-%ld",
95                                                 AST_MONITOR_DIR, seq );
96                         snprintf(       monitor->write_filename, FILENAME_MAX, "%s/audio-out-%ld",
97                                                 AST_MONITOR_DIR, seq );
98                         seq++;
99                         ast_mutex_unlock( &monitorlock );
100
101                         channel_name = strdup( chan->name );
102                         while( ( p = strchr( channel_name, '/' ) ) ) {
103                                 *p = '-';
104                         }
105                         snprintf(       monitor->filename_base, FILENAME_MAX, "%s/%s",
106                                                 AST_MONITOR_DIR, channel_name );
107                         monitor->filename_changed = 1;
108                         free( channel_name );
109                 }
110
111                 monitor->stop = ast_monitor_stop;
112
113                 // Determine file format
114                 if( format_spec && strlen( format_spec ) ) {
115                         monitor->format = strdup( format_spec );
116                 } else {
117                         monitor->format = strdup( "wav" );
118                 }
119                 
120                 // open files
121                 if( ast_fileexists( monitor->read_filename, NULL, NULL ) > 0 ) {
122                         ast_filedelete( monitor->read_filename, NULL );
123                 }
124                 if( !(monitor->read_stream = ast_writefile(     monitor->read_filename,
125                                                 monitor->format, NULL,
126                                                 O_CREAT|O_TRUNC|O_WRONLY, 0, 0644 ) ) ) {
127                         ast_log(        LOG_WARNING, "Could not create file %s\n",
128                                                 monitor->read_filename );
129                         free( monitor );
130                         ast_mutex_unlock(&chan->lock);
131                         return -1;
132                 }
133                 if( ast_fileexists( monitor->write_filename, NULL, NULL ) > 0 ) {
134                         ast_filedelete( monitor->write_filename, NULL );
135                 }
136                 if( !(monitor->write_stream = ast_writefile( monitor->write_filename,
137                                                 monitor->format, NULL,
138                                                 O_CREAT|O_TRUNC|O_WRONLY, 0, 0644 ) ) ) {
139                         ast_log(        LOG_WARNING, "Could not create file %s\n",
140                                                 monitor->write_filename );
141                         ast_closestream( monitor->read_stream );
142                         free( monitor );
143                         ast_mutex_unlock(&chan->lock);
144                         return -1;
145                 }
146                 chan->monitor = monitor;
147         } else {
148                 ast_log(        LOG_DEBUG,"Cannot start monitoring %s, already monitored\n",
149                                         chan->name );
150                 res = -1;
151         }
152
153         if( need_lock ) {
154                 ast_mutex_unlock(&chan->lock);
155         }
156         return res;
157 }
158
159 /* Stop monitoring a channel */
160 int ast_monitor_stop( struct ast_channel *chan, int need_lock )
161 {
162         if(need_lock) {
163                 if(ast_mutex_lock(&chan->lock)) {
164                         ast_log(LOG_WARNING, "Unable to lock channel\n");
165                         return -1;
166                 }
167         }
168
169         if(chan->monitor) {
170                 char filename[ FILENAME_MAX ];
171
172                 if(chan->monitor->read_stream) {
173                         ast_closestream( chan->monitor->read_stream );
174                 }
175                 if(chan->monitor->write_stream) {
176                         ast_closestream( chan->monitor->write_stream );
177                 }
178
179                 if(chan->monitor->filename_changed&&strlen(chan->monitor->filename_base)) {
180                         if( ast_fileexists(chan->monitor->read_filename,NULL,NULL) > 0 ) {
181                                 snprintf(       filename, FILENAME_MAX, "%s-in",
182                                                         chan->monitor->filename_base );
183                                 if(ast_fileexists( filename, NULL, NULL ) > 0) {
184                                         ast_filedelete( filename, NULL );
185                                 }
186                                 ast_filerename( chan->monitor->read_filename, filename,
187                                                                 chan->monitor->format );
188                         } else {
189                                 ast_log(        LOG_WARNING, "File %s not found\n",
190                                                         chan->monitor->read_filename );
191                         }
192
193                         if(ast_fileexists(chan->monitor->write_filename,NULL,NULL) > 0) {
194                                 snprintf(       filename, FILENAME_MAX, "%s-out",
195                                                         chan->monitor->filename_base );
196                                 if( ast_fileexists( filename, NULL, NULL ) > 0 ) {
197                                         ast_filedelete( filename, NULL );
198                                 }
199                                 ast_filerename( chan->monitor->write_filename, filename,
200                                                                 chan->monitor->format );
201                         } else {
202                                 ast_log(        LOG_WARNING, "File %s not found\n",
203                                                         chan->monitor->write_filename );
204                         }
205                 }
206                 if (chan->monitor->joinfiles && strlen(chan->monitor->filename_base)) {
207                         char tmp[1024];
208                         char *format = !strcasecmp(chan->monitor->format,"wav49") ? "WAV" : chan->monitor->format;
209                         char *name = chan->monitor->filename_base;
210                         int directory = strchr(name, '/') ? 1 : 0;
211                         char *dir = directory ? "" : AST_MONITOR_DIR;
212                         snprintf(tmp, sizeof(tmp), "nice -n 19 soxmix %s/%s-in.%s %s/%s-out.%s %s/%s.%s && rm -rf %s/%s-* &", dir, name, format, dir, name, format, dir, name, format, dir, name);
213 #if 0
214                         ast_verbose("executing %s\n",tmp);
215 #endif
216                         if (system(tmp) == -1)
217                                 ast_log(LOG_WARNING, "You might not have the soxmix installed and available in the path, please check.\n");
218                 }
219                 free( chan->monitor->format );
220                 free( chan->monitor );
221                 chan->monitor = NULL;
222         }
223
224         if( need_lock ) {
225                 ast_mutex_unlock(&chan->lock);
226         }
227         return 0;
228 }
229
230 /* Change monitoring filename of a channel */
231 int ast_monitor_change_fname(   struct ast_channel *chan,
232                                                                 const char *fname_base, int need_lock )
233 {
234         char tmp[256];
235         if( (!fname_base) || (!strlen(fname_base)) ) {
236                 ast_log(        LOG_WARNING,
237                                         "Cannot change monitor filename of channel %s to null",
238                                         chan->name );
239                 return -1;
240         }
241         
242         if( need_lock ) {
243                 if (ast_mutex_lock(&chan->lock)) {
244                         ast_log(LOG_WARNING, "Unable to lock channel\n");
245                         return -1;
246                 }
247         }
248
249         if( chan->monitor ) {
250                 int directory = strchr(fname_base, '/') ? 1 : 0;
251                 /* try creating the directory just in case it doesn't exist */
252                 if (directory) {
253                         char *name = strdup(fname_base);
254                         snprintf(tmp, sizeof(tmp), "mkdir -p %s",dirname(name));
255                         free(name);
256                         system(tmp);
257                 }
258
259                 snprintf(       chan->monitor->filename_base, FILENAME_MAX, "%s/%s",
260                                         directory ? "" : AST_MONITOR_DIR, fname_base );
261         } else {
262                 ast_log(        LOG_WARNING,
263                                         "Cannot change monitor filename of channel %s to %s, monitoring not started",
264                                         chan->name, fname_base );
265                                 
266         }
267
268         if( need_lock ) {
269                 ast_mutex_unlock(&chan->lock);
270         }
271         return 0;
272 }
273
274 static int start_monitor_exec(struct ast_channel *chan, void *data)
275 {
276         char *arg = NULL;
277         char *format = NULL;
278         char *fname_base = NULL;
279         char *options = NULL;
280         int joinfiles = 0;
281         int res;
282         
283         /* Parse arguments. */
284         if( data && strlen((char*)data) ) {
285                 arg = strdup( (char*)data );
286                 format = arg;
287                 fname_base = strchr( arg, '|' );
288                 if( fname_base ) {
289                         *fname_base = 0;
290                         fname_base++;
291                         if ((options = strchr(fname_base, '|'))) {
292                                 *options = 0;
293                                 options++;
294                                 if (strchr(options, 'm'))
295                                         joinfiles = 1;
296                         }
297                 }
298                 
299         }
300
301         res = ast_monitor_start( chan, format, fname_base, 1 );
302         if( res < 0 ) {
303                 res = ast_monitor_change_fname( chan, fname_base, 1 );
304         }
305         ast_monitor_setjoinfiles(chan, joinfiles);
306
307         if( arg ) {
308                 free( arg );
309         }
310
311         return res;
312 }
313
314 static int stop_monitor_exec(struct ast_channel *chan, void *data)
315 {
316         return ast_monitor_stop( chan, 1 );
317 }
318
319 static int change_monitor_exec(struct ast_channel *chan, void *data)
320 {
321         return ast_monitor_change_fname( chan, (const char*)data, 1 );
322 }
323
324 static int start_monitor_action(struct mansession *s, struct message *m)
325 {
326         struct ast_channel *c = NULL;
327         char *name = astman_get_header(m, "Channel");
328         char *fname = astman_get_header(m, "File");
329         char *format = astman_get_header(m, "Format");
330         char *d;
331         
332         if((!name)||(!strlen(name))) {
333                 astman_send_error(s, m, "No channel specified");
334                 return 0;
335         }
336         c = ast_channel_walk_locked(NULL);
337         while(c) {
338                 if (!strcasecmp(c->name, name)) {
339                         break;
340                 }
341                 ast_mutex_unlock(&c->lock);
342                 c = ast_channel_walk_locked(c);
343         }
344         if (!c) {
345                 astman_send_error(s, m, "No such channel");
346                 return 0;
347         }
348      
349         if( (!fname) || (!strlen(fname)) ) {
350                 // No filename base specified, default to channel name as per CLI
351                 fname = malloc (FILENAME_MAX);
352                 memset( fname, 0, FILENAME_MAX);
353                 strncpy( fname, c->name, FILENAME_MAX-1);
354                 // Channels have the format technology/channel_name - have to replace that / 
355                 if( (d=strchr( fname, '/')) ) *d='-';
356         }
357         
358         if( ast_monitor_start( c, format, fname, 1 ) ) {
359                 if( ast_monitor_change_fname( c, fname, 1 ) ) {
360                         astman_send_error(s, m, "Could not start monitoring channel");
361                         return 0;
362                 }
363         }
364         ast_mutex_unlock(&c->lock);
365         astman_send_ack(s, m, "Started monitoring channel");
366         return 0;
367 }
368
369 static int stop_monitor_action(struct mansession *s, struct message *m)
370 {
371         struct ast_channel *c = NULL;
372         char *name = astman_get_header(m, "Channel");
373         int res;
374         if((!name)||(!strlen(name))) {
375                 astman_send_error(s, m, "No channel specified");
376                 return 0;
377         }
378         c = ast_channel_walk_locked(NULL);
379         while(c) {
380                 if (!strcasecmp(c->name, name)) {
381                         break;
382                 }
383                 ast_mutex_unlock(&c->lock);
384                 c = ast_channel_walk_locked(c);
385         }
386         if (!c) {
387                 astman_send_error(s, m, "No such channel");
388                 return 0;
389         }
390         res = ast_monitor_stop( c, 1 );
391         ast_mutex_unlock(&c->lock);
392         if( res ) {
393                 astman_send_error(s, m, "Could not stop monitoring channel");
394                 return 0;
395         }
396         astman_send_ack(s, m, "Stopped monitoring channel");
397         return 0;
398 }
399
400 static int change_monitor_action(struct mansession *s, struct message *m)
401 {
402         struct ast_channel *c = NULL;
403         char *name = astman_get_header(m, "Channel");
404         char *fname = astman_get_header(m, "File");
405         if((!name) || (!strlen(name))) {
406                 astman_send_error(s, m, "No channel specified");
407                 return 0;
408         }
409         if ((!fname)||(!strlen(fname))) {
410                 astman_send_error(s, m, "No filename specified");
411                 return 0;
412         }
413         c = ast_channel_walk_locked(NULL);
414         while(c) {
415                 if (!strcasecmp(c->name, name)) {
416                         break;
417                 }
418                 ast_mutex_unlock(&c->lock);
419                 c = ast_channel_walk_locked(c);
420         }
421         if (!c) {
422                 astman_send_error(s, m, "No such channel");
423                 return 0;
424         }
425         if( ast_monitor_change_fname( c, fname, 1 ) ) {
426                 astman_send_error(s, m, "Could not change monitored filename of channel");
427                 return 0;
428         }
429         astman_send_ack(s, m, "Stopped monitoring channel");
430         return 0;
431 }
432
433 void ast_monitor_setjoinfiles(struct ast_channel *chan, int turnon)
434 {
435         if (chan->monitor)
436                 chan->monitor->joinfiles = turnon;
437 }
438
439 int load_module(void)
440 {
441         ast_register_application( "Monitor", start_monitor_exec, monitor_synopsis, monitor_descrip );
442         ast_register_application( "StopMonitor", stop_monitor_exec, stopmonitor_synopsis, stopmonitor_descrip );
443         ast_register_application( "ChangeMonitor", change_monitor_exec, changemonitor_synopsis, changemonitor_descrip );
444         ast_manager_register( "Monitor", EVENT_FLAG_CALL, start_monitor_action, monitor_synopsis );
445         ast_manager_register( "StopMonitor", EVENT_FLAG_CALL, stop_monitor_action, stopmonitor_synopsis );
446         ast_manager_register( "ChangeMonitor", EVENT_FLAG_CALL, change_monitor_action, changemonitor_synopsis );
447
448         return 0;
449 }
450
451 int unload_module(void)
452 {
453         ast_unregister_application( "Monitor" );
454         ast_unregister_application( "StopMonitor" );
455         return 0;
456 }
457
458 char *description(void)
459 {
460         return "Call Monitoring Resource";
461 }
462
463 int usecount(void)
464 {
465         /* Never allow monitor to be unloaded because it will
466            unresolve needed symbols in the channel */
467 #if 0
468         int res;
469         STANDARD_USECOUNT(res);
470         return res;
471 #else
472         return 1;
473 #endif
474 }
475
476 char *key()
477 {
478         return ASTERISK_GPL_KEY;
479 }