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