2bb3e440e3f3267e324bead50ff632746a0c7934
[asterisk/asterisk.git] / main / indications.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2002, Pauline Middelink
5  *
6  *
7  * See http://www.asterisk.org for more information about
8  * the Asterisk project. Please do not directly contact
9  * any of the maintainers of this project for assistance;
10  * the project provides a web site, mailing lists and IRC
11  * channels for your use.
12  *
13  * This program is free software, distributed under the terms of
14  * the GNU General Public License Version 2. See the LICENSE file
15  * at the top of the source tree.
16  */
17
18 /*! \file
19  *
20  * \brief Tone Management
21  * 
22  * \author Pauline Middelink <middelink@polyware.nl>
23  *
24  * This set of function allow us to play a list of tones on a channel.
25  * Each element has two frequencies, which are mixed together and a
26  * duration. For silence both frequencies can be set to 0.
27  * The playtones can be given as a comma separated string.
28  *
29  */
30
31 #include "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34
35 #include <math.h>
36
37 #include "asterisk/lock.h"
38 #include "asterisk/linkedlists.h"
39 #include "asterisk/indications.h"
40 #include "asterisk/frame.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/utils.h"
43
44 static int midi_tohz[128] = {
45                         8,8,9,9,10,10,11,12,12,13,14,
46                         15,16,17,18,19,20,21,23,24,25,
47                         27,29,30,32,34,36,38,41,43,46,
48                         48,51,55,58,61,65,69,73,77,82,
49                         87,92,97,103,110,116,123,130,138,146,
50                         155,164,174,184,195,207,220,233,246,261,
51                         277,293,311,329,349,369,391,415,440,466,
52                         493,523,554,587,622,659,698,739,783,830,
53                         880,932,987,1046,1108,1174,1244,1318,1396,1479,
54                         1567,1661,1760,1864,1975,2093,2217,2349,2489,2637,
55                         2793,2959,3135,3322,3520,3729,3951,4186,4434,4698,
56                         4978,5274,5587,5919,6271,6644,7040,7458,7902,8372,
57                         8869,9397,9956,10548,11175,11839,12543
58                         };
59
60 struct playtones_item {
61         int fac1;
62         int init_v2_1;
63         int init_v3_1;
64         int fac2;
65         int init_v2_2;
66         int init_v3_2;
67         int modulate;
68         int duration;
69 };
70
71 struct playtones_def {
72         int vol;
73         int reppos;
74         int nitems;
75         int interruptible;
76         struct playtones_item *items;
77 };
78
79 struct playtones_state {
80         int vol;
81         int v1_1;
82         int v2_1;
83         int v3_1;
84         int v1_2;
85         int v2_2;
86         int v3_2;
87         int reppos;
88         int nitems;
89         struct playtones_item *items;
90         int npos;
91         int oldnpos;
92         int pos;
93         int origwfmt;
94         struct ast_frame f;
95         unsigned char offset[AST_FRIENDLY_OFFSET];
96         short data[4000];
97 };
98
99 static void playtones_release(struct ast_channel *chan, void *params)
100 {
101         struct playtones_state *ps = params;
102
103         if (chan)
104                 ast_set_write_format(chan, ps->origwfmt);
105         if (ps->items)
106                 ast_free(ps->items);
107
108         ast_free(ps);
109 }
110
111 static void * playtones_alloc(struct ast_channel *chan, void *params)
112 {
113         struct playtones_def *pd = params;
114         struct playtones_state *ps = NULL;
115
116         if (!(ps = ast_calloc(1, sizeof(*ps))))
117                 return NULL;
118
119         ps->origwfmt = chan->writeformat;
120
121         if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
122                 ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", chan->name);
123                 playtones_release(NULL, ps);
124                 ps = NULL;
125         } else {
126                 ps->vol = pd->vol;
127                 ps->reppos = pd->reppos;
128                 ps->nitems = pd->nitems;
129                 ps->items = pd->items;
130                 ps->oldnpos = -1;
131         }
132
133         /* Let interrupts interrupt :) */
134         if (pd->interruptible)
135                 ast_set_flag(chan, AST_FLAG_WRITE_INT);
136         else
137                 ast_clear_flag(chan, AST_FLAG_WRITE_INT);
138
139         return ps;
140 }
141
142 static int playtones_generator(struct ast_channel *chan, void *data, int len, int samples)
143 {
144         struct playtones_state *ps = data;
145         struct playtones_item *pi;
146         int x;
147         /* we need to prepare a frame with 16 * timelen samples as we're 
148          * generating SLIN audio
149          */
150         len = samples * 2;
151         if (len > sizeof(ps->data) / 2 - 1) {
152                 ast_log(LOG_WARNING, "Can't generate that much data!\n");
153                 return -1;
154         }
155         memset(&ps->f, 0, sizeof(ps->f));
156
157         pi = &ps->items[ps->npos];
158         if (ps->oldnpos != ps->npos) {
159                 /* Load new parameters */
160                 ps->v1_1 = 0;
161                 ps->v2_1 = pi->init_v2_1;
162                 ps->v3_1 = pi->init_v3_1;
163                 ps->v1_2 = 0;
164                 ps->v2_2 = pi->init_v2_2;
165                 ps->v3_2 = pi->init_v3_2;
166                 ps->oldnpos = ps->npos;
167         }
168         for (x = 0; x < len/2; x++) {
169                 ps->v1_1 = ps->v2_1;
170                 ps->v2_1 = ps->v3_1;
171                 ps->v3_1 = (pi->fac1 * ps->v2_1 >> 15) - ps->v1_1;
172                 
173                 ps->v1_2 = ps->v2_2;
174                 ps->v2_2 = ps->v3_2;
175                 ps->v3_2 = (pi->fac2 * ps->v2_2 >> 15) - ps->v1_2;
176                 if (pi->modulate) {
177                         int p;
178                         p = ps->v3_2 - 32768;
179                         if (p < 0) p = -p;
180                         p = ((p * 9) / 10) + 1;
181                         ps->data[x] = (ps->v3_1 * p) >> 15;
182                 } else
183                         ps->data[x] = ps->v3_1 + ps->v3_2; 
184         }
185         
186         ps->f.frametype = AST_FRAME_VOICE;
187         ps->f.subclass = AST_FORMAT_SLINEAR;
188         ps->f.datalen = len;
189         ps->f.samples = samples;
190         ps->f.offset = AST_FRIENDLY_OFFSET;
191         ps->f.data = ps->data;
192         ps->f.delivery.tv_sec = 0;
193         ps->f.delivery.tv_usec = 0;
194         ast_write(chan, &ps->f);
195
196         ps->pos += x;
197         if (pi->duration && ps->pos >= pi->duration * 8) {      /* item finished? */
198                 ps->pos = 0;                                    /* start new item */
199                 ps->npos++;
200                 if (ps->npos >= ps->nitems) {                   /* last item? */
201                         if (ps->reppos == -1)                   /* repeat set? */
202                                 return -1;
203                         ps->npos = ps->reppos;                  /* redo from top */
204                 }
205         }
206         return 0;
207 }
208
209 static struct ast_generator playtones = {
210         alloc: playtones_alloc,
211         release: playtones_release,
212         generate: playtones_generator,
213 };
214
215 int ast_playtones_start(struct ast_channel *chan, int vol, const char *playlst, int interruptible)
216 {
217         char *s, *data = ast_strdupa(playlst); /* cute */
218         struct playtones_def d = { vol, -1, 0, 1, NULL};
219         char *stringp;
220         char *separator;
221         
222         if (vol < 1)
223                 d.vol = 7219; /* Default to -8db */
224
225         d.interruptible = interruptible;
226         
227         stringp=data;
228         /* the stringp/data is not null here */
229         /* check if the data is separated with '|' or with ',' by default */
230         if (strchr(stringp,'|'))
231                 separator = "|";
232         else
233                 separator = ",";
234         s = strsep(&stringp,separator);
235         while (s && *s) {
236                 int freq1, freq2, time, modulate = 0, midinote = 0;
237
238                 if (s[0]=='!')
239                         s++;
240                 else if (d.reppos == -1)
241                         d.reppos = d.nitems;
242                 if (sscanf(s, "%d+%d/%d", &freq1, &freq2, &time) == 3) {
243                         /* f1+f2/time format */
244                 } else if (sscanf(s, "%d+%d", &freq1, &freq2) == 2) {
245                         /* f1+f2 format */
246                         time = 0;
247                 } else if (sscanf(s, "%d*%d/%d", &freq1, &freq2, &time) == 3) {
248                         /* f1*f2/time format */
249                         modulate = 1;
250                 } else if (sscanf(s, "%d*%d", &freq1, &freq2) == 2) {
251                         /* f1*f2 format */
252                         time = 0;
253                         modulate = 1;
254                 } else if (sscanf(s, "%d/%d", &freq1, &time) == 2) {
255                         /* f1/time format */
256                         freq2 = 0;
257                 } else if (sscanf(s, "%d", &freq1) == 1) {
258                         /* f1 format */
259                         freq2 = 0;
260                         time = 0;
261                 } else if (sscanf(s, "M%d+M%d/%d", &freq1, &freq2, &time) == 3) {
262                         /* Mf1+Mf2/time format */
263                         midinote = 1;
264                 } else if (sscanf(s, "M%d+M%d", &freq1, &freq2) == 2) {
265                         /* Mf1+Mf2 format */
266                         time = 0;
267                         midinote = 1;
268                 } else if (sscanf(s, "M%d*M%d/%d", &freq1, &freq2, &time) == 3) {
269                         /* Mf1*Mf2/time format */
270                         modulate = 1;
271                         midinote = 1;
272                 } else if (sscanf(s, "M%d*M%d", &freq1, &freq2) == 2) {
273                         /* Mf1*Mf2 format */
274                         time = 0;
275                         modulate = 1;
276                         midinote = 1;
277                 } else if (sscanf(s, "M%d/%d", &freq1, &time) == 2) {
278                         /* Mf1/time format */
279                         freq2 = -1;
280                         midinote = 1;
281                 } else if (sscanf(s, "M%d", &freq1) == 1) {
282                         /* Mf1 format */
283                         freq2 = -1;
284                         time = 0;
285                         midinote = 1;
286                 } else {
287                         ast_log(LOG_WARNING,"%s: tone component '%s' of '%s' is no good\n",chan->name,s,playlst);
288                         return -1;
289                 }
290
291                 if (midinote) {
292                         /* midi notes must be between 0 and 127 */
293                         if ((freq1 >= 0) && (freq1 <= 127))
294                                 freq1 = midi_tohz[freq1];
295                         else
296                                 freq1 = 0;
297
298                         if ((freq2 >= 0) && (freq2 <= 127))
299                                 freq2 = midi_tohz[freq2];
300                         else
301                                 freq2 = 0;
302                 }
303
304                 if (!(d.items = ast_realloc(d.items, (d.nitems + 1) * sizeof(*d.items)))) {
305                         return -1;
306                 }
307                 d.items[d.nitems].fac1 = 2.0 * cos(2.0 * M_PI * (freq1 / 8000.0)) * 32768.0;
308                 d.items[d.nitems].init_v2_1 = sin(-4.0 * M_PI * (freq1 / 8000.0)) * d.vol;
309                 d.items[d.nitems].init_v3_1 = sin(-2.0 * M_PI * (freq1 / 8000.0)) * d.vol;
310
311                 d.items[d.nitems].fac2 = 2.0 * cos(2.0 * M_PI * (freq2 / 8000.0)) * 32768.0;
312                 d.items[d.nitems].init_v2_2 = sin(-4.0 * M_PI * (freq2 / 8000.0)) * d.vol;
313                 d.items[d.nitems].init_v3_2 = sin(-2.0 * M_PI * (freq2 / 8000.0)) * d.vol;
314                 d.items[d.nitems].duration = time;
315                 d.items[d.nitems].modulate = modulate;
316                 d.nitems++;
317
318                 s = strsep(&stringp,separator);
319         }
320
321         if (ast_activate_generator(chan, &playtones, &d)) {
322                 ast_free(d.items);
323                 return -1;
324         }
325         return 0;
326 }
327
328 void ast_playtones_stop(struct ast_channel *chan)
329 {
330         ast_deactivate_generator(chan);
331 }
332
333 /*--------------------------------------------*/
334
335 static AST_RWLIST_HEAD_STATIC(tone_zones, ind_tone_zone);
336 static struct ind_tone_zone *current_tonezone;
337
338 struct ind_tone_zone *ast_walk_indications(const struct ind_tone_zone *cur)
339 {
340         struct ind_tone_zone *tz = NULL;
341
342         AST_RWLIST_RDLOCK(&tone_zones);
343         /* If cur is not NULL, then we have to iterate through - otherwise just return the first entry */
344         if (cur) {
345                 AST_RWLIST_TRAVERSE(&tone_zones, tz, list) {
346                         if (tz == cur)
347                                 break;
348                 }
349                 tz = AST_RWLIST_NEXT(tz, list);
350         } else {
351                 tz = AST_RWLIST_FIRST(&tone_zones);
352         }
353         AST_RWLIST_UNLOCK(&tone_zones);
354
355         return tz;
356 }
357
358 /* Set global indication country */
359 int ast_set_indication_country(const char *country)
360 {
361         struct ind_tone_zone *zone = NULL;
362
363         /* If no country is specified or we are unable to find the zone, then return not found */
364         if (!country || !(zone = ast_get_indication_zone(country)))
365                 return 1;
366         
367         ast_verb(3, "Setting default indication country to '%s'\n", country);
368
369         /* Protect the current tonezone using the tone_zones lock as well */
370         AST_RWLIST_WRLOCK(&tone_zones);
371         current_tonezone = zone;
372         AST_RWLIST_UNLOCK(&tone_zones);
373
374         /* Zone was found */
375         return 0;
376 }
377
378 /* locate tone_zone, given the country. if country == NULL, use the default country */
379 struct ind_tone_zone *ast_get_indication_zone(const char *country)
380 {
381         struct ind_tone_zone *tz = NULL;
382         int alias_loop = 0;
383
384         AST_RWLIST_RDLOCK(&tone_zones);
385
386         if (!country) {
387                 if (current_tonezone)
388                         tz = current_tonezone;
389                 else
390                         tz = AST_LIST_FIRST(&tone_zones);
391         } else {
392                 do {
393                         AST_RWLIST_TRAVERSE(&tone_zones, tz, list) {
394                                 if (!strcasecmp(tz->country, country))
395                                         break;
396                         }
397                         if (!tz)
398                                 break;
399                         /* If this is an alias then we have to search yet again otherwise we have found the zonezone */
400                         if (tz->alias && tz->alias[0])
401                                 country = tz->alias;
402                         else
403                                 break;
404                 } while ((++alias_loop < 20) && tz);
405         }
406
407         AST_RWLIST_UNLOCK(&tone_zones);
408
409         /* If we reached the maximum loops to find the proper country via alias, print out a notice */
410         if (alias_loop == 20)
411                 ast_log(LOG_NOTICE, "Alias loop for '%s' is bonkers\n", country);
412
413         return tz;
414 }
415
416 /* locate a tone_zone_sound, given the tone_zone. if tone_zone == NULL, use the default tone_zone */
417 struct ind_tone_zone_sound *ast_get_indication_tone(const struct ind_tone_zone *zone, const char *indication)
418 {
419         struct ind_tone_zone_sound *ts = NULL;
420
421         AST_RWLIST_RDLOCK(&tone_zones);
422
423         /* If no zone is already specified we need to try to pick one */
424         if (!zone) {
425                 if (current_tonezone) {
426                         zone = current_tonezone;
427                 } else if (!(zone = AST_LIST_FIRST(&tone_zones))) {
428                         /* No zone has been found ;( */
429                         AST_RWLIST_UNLOCK(&tone_zones);
430                         return NULL;
431                 }
432         }
433
434         /* Look through list of tones in the zone searching for the right one */
435         for (ts = zone->tones; ts; ts = ts->next) {
436                 if (!strcasecmp(ts->name, indication))
437                         break;
438         }
439
440         AST_RWLIST_UNLOCK(&tone_zones);
441
442         return ts;
443 }
444
445 /* helper function to delete a tone_zone in its entirety */
446 static inline void free_zone(struct ind_tone_zone* zone)
447 {
448         while (zone->tones) {
449                 struct ind_tone_zone_sound *tmp = zone->tones->next;
450                 ast_free((void *)zone->tones->name);
451                 ast_free((void *)zone->tones->data);
452                 ast_free(zone->tones);
453                 zone->tones = tmp;
454         }
455
456         if (zone->ringcadence)
457                 ast_free(zone->ringcadence);
458
459         ast_free(zone);
460 }
461
462 /*--------------------------------------------*/
463
464 /* add a new country, if country exists, it will be replaced. */
465 int ast_register_indication_country(struct ind_tone_zone *zone)
466 {
467         struct ind_tone_zone *tz = NULL;
468
469         AST_RWLIST_WRLOCK(&tone_zones);
470         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&tone_zones, tz, list) {
471                 /* If this is not the same zone, then just continue to the next entry */
472                 if (strcasecmp(zone->country, tz->country))
473                         continue;
474                 /* If this zone we are going to remove is the current default then make the new zone the default */
475                 if (tz == current_tonezone)
476                         current_tonezone = zone;
477                 /* Remove from the linked list */
478                 AST_RWLIST_REMOVE_CURRENT(list);
479                 /* Finally free the zone itself */
480                 free_zone(tz);
481                 break;
482         }
483         AST_RWLIST_TRAVERSE_SAFE_END;
484
485         /* Add zone to the list */
486         AST_RWLIST_INSERT_TAIL(&tone_zones, zone, list);
487
488         /* It's all over. */
489         AST_RWLIST_UNLOCK(&tone_zones);
490
491         ast_verb(3, "Registered indication country '%s'\n", zone->country);
492
493         return 0;
494 }
495
496 /* remove an existing country and all its indications, country must exist.
497  * Also, all countries which are an alias for the specified country are removed. */
498 int ast_unregister_indication_country(const char *country)
499 {
500         struct ind_tone_zone *tz = NULL;
501         int res = -1;
502
503         AST_RWLIST_WRLOCK(&tone_zones);
504         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&tone_zones, tz, list) {
505                 if (country && (strcasecmp(country, tz->country) && strcasecmp(country, tz->alias)))
506                         continue;
507                 /* If this tonezone is the current default then unset it */
508                 if (tz == current_tonezone) {
509                         ast_log(LOG_NOTICE,"Removed default indication country '%s'\n", tz->country);
510                         current_tonezone = NULL;
511                 }
512                 /* Remove from the list */
513                 AST_RWLIST_REMOVE_CURRENT(list);
514                 ast_verb(3, "Unregistered indication country '%s'\n", tz->country);
515                 free_zone(tz);
516                 res = 0;
517         }
518         AST_RWLIST_TRAVERSE_SAFE_END;
519         AST_RWLIST_UNLOCK(&tone_zones);
520
521         return res;
522 }
523
524 /* add a new indication to a tone_zone. tone_zone must exist. if the indication already
525  * exists, it will be replaced. */
526 int ast_register_indication(struct ind_tone_zone *zone, const char *indication, const char *tonelist)
527 {
528         struct ind_tone_zone_sound *ts, *ps;
529
530         /* is it an alias? stop */
531         if (zone->alias[0])
532                 return -1;
533
534         AST_RWLIST_WRLOCK(&tone_zones);
535         for (ps=NULL,ts=zone->tones; ts; ps=ts,ts=ts->next) {
536                 if (!strcasecmp(indication,ts->name)) {
537                         /* indication already there, replace */
538                         ast_free((void*)ts->name);
539                         ast_free((void*)ts->data);
540                         break;
541                 }
542         }
543         if (!ts) {
544                 /* not there, we have to add */
545                 if (!(ts = ast_malloc(sizeof(*ts)))) {
546                         AST_RWLIST_UNLOCK(&tone_zones);
547                         return -2;
548                 }
549                 ts->next = NULL;
550         }
551         if (!(ts->name = ast_strdup(indication)) || !(ts->data = ast_strdup(tonelist))) {
552                 AST_RWLIST_UNLOCK(&tone_zones);
553                 return -2;
554         }
555         if (ps)
556                 ps->next = ts;
557         else
558                 zone->tones = ts;
559         AST_RWLIST_UNLOCK(&tone_zones);
560         return 0;
561 }
562
563 /* remove an existing country's indication. Both country and indication must exist */
564 int ast_unregister_indication(struct ind_tone_zone *zone, const char *indication)
565 {
566         struct ind_tone_zone_sound *ts,*ps = NULL, *tmp;
567         int res = -1;
568
569         /* is it an alias? stop */
570         if (zone->alias[0])
571                 return -1;
572
573         AST_RWLIST_WRLOCK(&tone_zones);
574         ts = zone->tones;
575         while (ts) {
576                 if (!strcasecmp(indication,ts->name)) {
577                         /* indication found */
578                         tmp = ts->next;
579                         if (ps)
580                                 ps->next = tmp;
581                         else
582                                 zone->tones = tmp;
583                         ast_free((void*)ts->name);
584                         ast_free((void*)ts->data);
585                         ast_free(ts);
586                         ts = tmp;
587                         res = 0;
588                 }
589                 else {
590                         /* next zone please */
591                         ps = ts;
592                         ts = ts->next;
593                 }
594         }
595         /* indication not found, goodbye */
596         AST_RWLIST_UNLOCK(&tone_zones);
597         return res;
598 }