cc1c7e3b0adc28e1e7367f81d4e4874062bf8faf
[asterisk/asterisk.git] / indications.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Tone Management
5  * 
6  * Copyright (C) 2002, Pauline Middelink
7  *
8  * Pauline Middelink <middelink@polyware.nl>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  *
13  * This set of function allow us to play a list of tones on a channel.
14  * Each element has two frequencies, which are mixed together and a
15  * duration. For silence both frequencies can be set to 0.
16  * The playtones can be given as a comma separated string.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <math.h>                       /* For PI */
23
24 #include "asterisk.h"
25
26 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
27
28 #include "asterisk/indications.h"
29 #include "asterisk/frame.h"
30 #include "asterisk/options.h"
31 #include "asterisk/channel.h"
32 #include "asterisk/logger.h"
33 #include "asterisk/lock.h"
34 #include "asterisk/utils.h"
35
36 struct playtones_item {
37         int freq1;
38         int freq2;
39         int duration;
40         int modulate;
41 };
42
43 struct playtones_def {
44         int vol;
45         int reppos;
46         int nitems;
47         int interruptible;
48         struct playtones_item *items;
49 };
50
51 struct playtones_state {
52         int vol;
53         int reppos;
54         int nitems;
55         struct playtones_item *items;
56         int npos;
57         int pos;
58         int origwfmt;
59         struct ast_frame f;
60         unsigned char offset[AST_FRIENDLY_OFFSET];
61         short data[4000];
62 };
63
64 static void playtones_release(struct ast_channel *chan, void *params)
65 {
66         struct playtones_state *ps = params;
67         if (chan) {
68                 ast_set_write_format(chan, ps->origwfmt);
69         }
70         if (ps->items) free(ps->items);
71         free(ps);
72 }
73
74 static void * playtones_alloc(struct ast_channel *chan, void *params)
75 {
76         struct playtones_def *pd = params;
77         struct playtones_state *ps = malloc(sizeof(struct playtones_state));
78         if (!ps)
79                 return NULL;
80         memset(ps, 0, sizeof(struct playtones_state));
81         ps->origwfmt = chan->writeformat;
82         if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
83                 ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", chan->name);
84                 playtones_release(NULL, ps);
85                 ps = NULL;
86         } else {
87                 ps->vol = pd->vol;
88                 ps->reppos = pd->reppos;
89                 ps->nitems = pd->nitems;
90                 ps->items = pd->items;
91         }
92         /* Let interrupts interrupt :) */
93         if (pd->interruptible)
94                 ast_set_flag(chan, AST_FLAG_WRITE_INT);
95         else
96                 ast_clear_flag(chan, AST_FLAG_WRITE_INT);
97         return ps;
98 }
99
100 static int playtones_generator(struct ast_channel *chan, void *data, int len, int samples)
101 {
102         struct playtones_state *ps = data;
103         struct playtones_item *pi;
104         int x;
105         /* we need to prepare a frame with 16 * timelen samples as we're 
106          * generating SLIN audio
107          */
108         len = samples * 2;
109         if (len > sizeof(ps->data) / 2 - 1) {
110                 ast_log(LOG_WARNING, "Can't generate that much data!\n");
111                 return -1;
112         }
113         memset(&ps->f, 0, sizeof(ps->f));
114
115         pi = &ps->items[ps->npos];
116         for (x=0;x<len/2;x++) {
117                 if (pi->modulate)
118                 /* Modulate 1st tone with 2nd, to 90% modulation depth */
119                 ps->data[x] = ps->vol * 2 * (
120                         sin((pi->freq1 * 2.0 * M_PI / 8000.0) * (ps->pos + x)) *
121                         (0.9 * fabs(sin((pi->freq2 * 2.0 * M_PI / 8000.0) * (ps->pos + x))) + 0.1)
122                         );
123                 else
124                         /* Add 2 tones together */
125                         ps->data[x] = ps->vol * (
126                                 sin((pi->freq1 * 2.0 * M_PI / 8000.0) * (ps->pos + x)) +
127                                 sin((pi->freq2 * 2.0 * M_PI / 8000.0) * (ps->pos + x))
128                         );
129         }
130         ps->f.frametype = AST_FRAME_VOICE;
131         ps->f.subclass = AST_FORMAT_SLINEAR;
132         ps->f.datalen = len;
133         ps->f.samples = samples;
134         ps->f.offset = AST_FRIENDLY_OFFSET;
135         ps->f.data = ps->data;
136         ps->f.delivery.tv_sec = 0;
137         ps->f.delivery.tv_usec = 0;
138         ast_write(chan, &ps->f);
139
140         ps->pos += x;
141         if (pi->duration && ps->pos >= pi->duration * 8) {      /* item finished? */
142                 ps->pos = 0;                                    /* start new item */
143                 ps->npos++;
144                 if (ps->npos >= ps->nitems) {                   /* last item? */
145                         if (ps->reppos == -1)                   /* repeat set? */
146                                 return -1;
147                         ps->npos = ps->reppos;                  /* redo from top */
148                 }
149         }
150         return 0;
151 }
152
153 static struct ast_generator playtones = {
154         alloc: playtones_alloc,
155         release: playtones_release,
156         generate: playtones_generator,
157 };
158
159 int ast_playtones_start(struct ast_channel *chan, int vol, const char *playlst, int interruptible)
160 {
161         char *s, *data = ast_strdupa(playlst); /* cute */
162         struct playtones_def d = { vol, -1, 0, 1, NULL};
163         char *stringp=NULL;
164         char *separator;
165         if (!data)
166                 return -1;
167         if (vol < 1)
168                 d.vol = 8192;
169
170         d.interruptible = interruptible;
171         
172         stringp=data;
173         /* the stringp/data is not null here */
174         /* check if the data is separated with '|' or with ',' by default */
175         if (strchr(stringp,'|'))
176                 separator = "|";
177         else
178                 separator = ",";
179         s = strsep(&stringp,separator);
180         while (s && *s) {
181                 int freq1, freq2, time, modulate=0;
182
183                 if (s[0]=='!')
184                         s++;
185                 else if (d.reppos == -1)
186                         d.reppos = d.nitems;
187                 if (sscanf(s, "%d+%d/%d", &freq1, &freq2, &time) == 3) {
188                         /* f1+f2/time format */
189                 } else if (sscanf(s, "%d+%d", &freq1, &freq2) == 2) {
190                         /* f1+f2 format */
191                         time = 0;
192                 } else if (sscanf(s, "%d*%d/%d", &freq1, &freq2, &time) == 3) {
193                         /* f1*f2/time format */
194                         modulate = 1;
195                 } else if (sscanf(s, "%d*%d", &freq1, &freq2) == 2) {
196                         /* f1*f2 format */
197                         time = 0;
198                         modulate = 1;
199                 } else if (sscanf(s, "%d/%d", &freq1, &time) == 2) {
200                         /* f1/time format */
201                         freq2 = 0;
202                 } else if (sscanf(s, "%d", &freq1) == 1) {
203                         /* f1 format */
204                         freq2 = 0;
205                         time = 0;
206                 } else {
207                         ast_log(LOG_WARNING,"%s: tone component '%s' of '%s' is no good\n",chan->name,s,playlst);
208                         return -1;
209                 }
210
211                 d.items = realloc(d.items,(d.nitems+1)*sizeof(struct playtones_item));
212                 if (d.items == NULL)
213                         return -1;
214                 d.items[d.nitems].freq1    = freq1;
215                 d.items[d.nitems].freq2    = freq2;
216                 d.items[d.nitems].duration = time;
217                 d.items[d.nitems].modulate = modulate;
218                 d.nitems++;
219
220                 s = strsep(&stringp,separator);
221         }
222
223         if (ast_activate_generator(chan, &playtones, &d)) {
224                 free(d.items);
225                 return -1;
226         }
227         return 0;
228 }
229
230 void ast_playtones_stop(struct ast_channel *chan)
231 {
232         ast_deactivate_generator(chan);
233 }
234
235 /*--------------------------------------------*/
236
237 struct tone_zone *tone_zones;
238 static struct tone_zone *current_tonezone;
239
240 /* Protect the tone_zones list (highly unlikely that two things would change
241  * it at the same time, but still! */
242 AST_MUTEX_DEFINE_EXPORTED(tzlock);
243
244 /* Set global indication country */
245 int ast_set_indication_country(const char *country)
246 {
247         if (country) {
248                 struct tone_zone *z = ast_get_indication_zone(country);
249                 if (z) {
250                         if (option_verbose > 2)
251                                 ast_verbose(VERBOSE_PREFIX_3 "Setting default indication country to '%s'\n",country);
252                         current_tonezone = z;
253                         return 0;
254                 }
255         }
256         return 1; /* not found */
257 }
258
259 /* locate tone_zone, given the country. if country == NULL, use the default country */
260 struct tone_zone *ast_get_indication_zone(const char *country)
261 {
262         struct tone_zone *tz;
263         int alias_loop = 0;
264
265         /* we need some tonezone, pick the first */
266         if (country == NULL && current_tonezone)
267                 return current_tonezone;        /* default country? */
268         if (country == NULL && tone_zones)
269                 return tone_zones;              /* any country? */
270         if (country == NULL)
271                 return 0;       /* not a single country insight */
272
273         if (ast_mutex_lock(&tzlock)) {
274                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
275                 return 0;
276         }
277         do {
278                 for (tz=tone_zones; tz; tz=tz->next) {
279                         if (strcasecmp(country,tz->country)==0) {
280                                 /* tone_zone found */
281                                 if (tz->alias && tz->alias[0]) {
282                                         country = tz->alias;
283                                         break;
284                                 }
285                                 ast_mutex_unlock(&tzlock);
286                                 return tz;
287                         }
288                 }
289         } while (++alias_loop<20 && tz);
290         ast_mutex_unlock(&tzlock);
291         if (alias_loop==20)
292                 ast_log(LOG_NOTICE,"Alias loop for '%s' forcefull broken\n",country);
293         /* nothing found, sorry */
294         return 0;
295 }
296
297 /* locate a tone_zone_sound, given the tone_zone. if tone_zone == NULL, use the default tone_zone */
298 struct tone_zone_sound *ast_get_indication_tone(const struct tone_zone *zone, const char *indication)
299 {
300         struct tone_zone_sound *ts;
301
302         /* we need some tonezone, pick the first */
303         if (zone == NULL && current_tonezone)
304                 zone = current_tonezone;        /* default country? */
305         if (zone == NULL && tone_zones)
306                 zone = tone_zones;              /* any country? */
307         if (zone == NULL)
308                 return 0;       /* not a single country insight */
309
310         if (ast_mutex_lock(&tzlock)) {
311                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
312                 return 0;
313         }
314         for (ts=zone->tones; ts; ts=ts->next) {
315                 if (strcasecmp(indication,ts->name)==0) {
316                         /* found indication! */
317                         ast_mutex_unlock(&tzlock);
318                         return ts;
319                 }
320         }
321         /* nothing found, sorry */
322         ast_mutex_unlock(&tzlock);
323         return 0;
324 }
325
326 /* helper function to delete a tone_zone in its entirety */
327 static inline void free_zone(struct tone_zone* zone)
328 {
329         while (zone->tones) {
330                 struct tone_zone_sound *tmp = zone->tones->next;
331                 free((void*)zone->tones->name);
332                 free((void*)zone->tones->data);
333                 free(zone->tones);
334                 zone->tones = tmp;
335         }
336         if (zone->ringcadance)
337                 free((void*)zone->ringcadance);
338         free(zone);
339 }
340
341 /*--------------------------------------------*/
342
343 /* add a new country, if country exists, it will be replaced. */
344 int ast_register_indication_country(struct tone_zone *zone)
345 {
346         struct tone_zone *tz,*pz;
347
348         if (ast_mutex_lock(&tzlock)) {
349                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
350                 return -1;
351         }
352         for (pz=NULL,tz=tone_zones; tz; pz=tz,tz=tz->next) {
353                 if (strcasecmp(zone->country,tz->country)==0) {
354                         /* tone_zone already there, replace */
355                         zone->next = tz->next;
356                         if (pz)
357                                 pz->next = zone;
358                         else
359                                 tone_zones = zone;
360                         /* if we are replacing the default zone, re-point it */
361                         if (tz == current_tonezone)
362                                 current_tonezone = zone;
363                         /* now free the previous zone */
364                         free_zone(tz);
365                         ast_mutex_unlock(&tzlock);
366                         return 0;
367                 }
368         }
369         /* country not there, add */
370         zone->next = NULL;
371         if (pz)
372                 pz->next = zone;
373         else
374                 tone_zones = zone;
375         ast_mutex_unlock(&tzlock);
376
377         if (option_verbose > 2)
378                 ast_verbose(VERBOSE_PREFIX_3 "Registered indication country '%s'\n",zone->country);
379         return 0;
380 }
381
382 /* remove an existing country and all its indications, country must exist.
383  * Also, all countries which are an alias for the specified country are removed. */
384 int ast_unregister_indication_country(const char *country)
385 {
386         struct tone_zone *tz, *pz = NULL, *tmp;
387         int res = -1;
388
389         if (ast_mutex_lock(&tzlock)) {
390                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
391                 return -1;
392         }
393         tz = tone_zones;
394         while (tz) {
395                 if (country==NULL ||
396                     (strcasecmp(country, tz->country)==0 ||
397                      strcasecmp(country, tz->alias)==0)) {
398                         /* tone_zone found, remove */
399                         tmp = tz->next;
400                         if (pz)
401                                 pz->next = tmp;
402                         else
403                                 tone_zones = tmp;
404                         /* if we are unregistering the default country, w'll notice */
405                         if (tz == current_tonezone) {
406                                 ast_log(LOG_NOTICE,"Removed default indication country '%s'\n",tz->country);
407                                 current_tonezone = NULL;
408                         }
409                         if (option_verbose > 2)
410                                 ast_verbose(VERBOSE_PREFIX_3 "Unregistered indication country '%s'\n",tz->country);
411                         free_zone(tz);
412                         if (tone_zones == tz)
413                                 tone_zones = tmp;
414                         tz = tmp;
415                         res = 0;
416                 }
417                 else {
418                         /* next zone please */
419                         pz = tz;
420                         tz = tz->next;
421                 }
422         }
423         ast_mutex_unlock(&tzlock);
424         return res;
425 }
426
427 /* add a new indication to a tone_zone. tone_zone must exist. if the indication already
428  * exists, it will be replaced. */
429 int ast_register_indication(struct tone_zone *zone, const char *indication, const char *tonelist)
430 {
431         struct tone_zone_sound *ts,*ps;
432
433         /* is it an alias? stop */
434         if (zone->alias[0])
435                 return -1;
436
437         if (ast_mutex_lock(&tzlock)) {
438                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
439                 return -2;
440         }
441         for (ps=NULL,ts=zone->tones; ts; ps=ts,ts=ts->next) {
442                 if (strcasecmp(indication,ts->name)==0) {
443                         /* indication already there, replace */
444                         free((void*)ts->name);
445                         free((void*)ts->data);
446                         break;
447                 }
448         }
449         if (!ts) {
450                 /* not there, we have to add */
451                 ts = malloc(sizeof(struct tone_zone_sound));
452                 if (!ts) {
453                         ast_log(LOG_WARNING, "Out of memory\n");
454                         ast_mutex_unlock(&tzlock);
455                         return -2;
456                 }
457                 ts->next = NULL;
458         }
459         ts->name = strdup(indication);
460         ts->data = strdup(tonelist);
461         if (ts->name==NULL || ts->data==NULL) {
462                 ast_log(LOG_WARNING, "Out of memory\n");
463                 ast_mutex_unlock(&tzlock);
464                 return -2;
465         }
466         if (ps)
467                 ps->next = ts;
468         else
469                 zone->tones = ts;
470         ast_mutex_unlock(&tzlock);
471         return 0;
472 }
473
474 /* remove an existing country's indication. Both country and indication must exist */
475 int ast_unregister_indication(struct tone_zone *zone, const char *indication)
476 {
477         struct tone_zone_sound *ts,*ps = NULL, *tmp;
478         int res = -1;
479
480         /* is it an alias? stop */
481         if (zone->alias[0])
482                 return -1;
483
484         if (ast_mutex_lock(&tzlock)) {
485                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
486                 return -1;
487         }
488         ts = zone->tones;
489         while (ts) {
490                 if (strcasecmp(indication,ts->name)==0) {
491                         /* indication found */
492                         tmp = ts->next;
493                         if (ps)
494                                 ps->next = tmp;
495                         else
496                                 zone->tones = tmp;
497                         free((void*)ts->name);
498                         free((void*)ts->data);
499                         free(ts);
500                         ts = tmp;
501                         res = 0;
502                 }
503                 else {
504                         /* next zone please */
505                         ps = ts;
506                         ts = ts->next;
507                 }
508         }
509         /* indication not found, goodbye */
510         ast_mutex_unlock(&tzlock);
511         return res;
512 }