00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 #include <pthread.h>
00022 #include <string.h>
00023 #include <math.h>
00024 #include <asterisk/indications.h>
00025 #include <asterisk/frame.h>
00026 #include <asterisk/options.h>
00027 #include <asterisk/channel.h>
00028 #include <asterisk/logger.h>
00029
00030 struct playtones_item {
00031 int freq1;
00032 int freq2;
00033 int duration;
00034 int modulate;
00035 };
00036
00037 struct playtones_def {
00038 int vol;
00039 int reppos;
00040 int nitems;
00041 int interruptible;
00042 struct playtones_item *items;
00043 };
00044
00045 struct playtones_state {
00046 int vol;
00047 int reppos;
00048 int nitems;
00049 struct playtones_item *items;
00050 int npos;
00051 int pos;
00052 int origwfmt;
00053 struct ast_frame f;
00054 short data[4000];
00055 };
00056
00057 static void playtones_release(struct ast_channel *chan, void *params)
00058 {
00059 struct playtones_state *ps = params;
00060 if (chan) {
00061 ast_set_write_format(chan, ps->origwfmt);
00062 }
00063 if (ps->items) free(ps->items);
00064 free(ps);
00065 }
00066
00067 static void * playtones_alloc(struct ast_channel *chan, void *params)
00068 {
00069 struct playtones_def *pd = params;
00070 struct playtones_state *ps = malloc(sizeof(struct playtones_state));
00071 if (!ps)
00072 return NULL;
00073 memset(ps, 0, sizeof(struct playtones_state));
00074 ps->origwfmt = chan->writeformat;
00075 if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
00076 ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", chan->name);
00077 playtones_release(NULL, ps);
00078 ps = NULL;
00079 } else {
00080 ps->vol = pd->vol;
00081 ps->reppos = pd->reppos;
00082 ps->nitems = pd->nitems;
00083 ps->items = pd->items;
00084 }
00085
00086 chan->writeinterrupt = pd->interruptible;
00087 return ps;
00088 }
00089
00090 static int playtones_generator(struct ast_channel *chan, void *data, int len, int samples)
00091 {
00092 struct playtones_state *ps = data;
00093 struct playtones_item *pi;
00094 int x;
00095
00096
00097
00098 len = samples * 2;
00099 if (len > sizeof(ps->data) / 2 - 1) {
00100 ast_log(LOG_WARNING, "Can't generate that much data!\n");
00101 return -1;
00102 }
00103 memset(&ps->f, 0, sizeof(ps->f));
00104
00105 pi = &ps->items[ps->npos];
00106 for (x=0;x<len/2;x++) {
00107 if (pi->modulate)
00108
00109 ps->data[x] = ps->vol * 2 * (
00110 sin((pi->freq1 * 2.0 * M_PI / 8000.0) * (ps->pos + x)) *
00111 (0.9 * fabs(sin((pi->freq2 * 2.0 * M_PI / 8000.0) * (ps->pos + x))) + 0.1)
00112 );
00113 else
00114
00115 ps->data[x] = ps->vol * (
00116 sin((pi->freq1 * 2.0 * M_PI / 8000.0) * (ps->pos + x)) +
00117 sin((pi->freq2 * 2.0 * M_PI / 8000.0) * (ps->pos + x))
00118 );
00119 }
00120 ps->f.frametype = AST_FRAME_VOICE;
00121 ps->f.subclass = AST_FORMAT_SLINEAR;
00122 ps->f.datalen = len;
00123 ps->f.samples = samples;
00124 ps->f.offset = AST_FRIENDLY_OFFSET;
00125 ps->f.data = ps->data;
00126 ast_write(chan, &ps->f);
00127
00128 ps->pos += x;
00129 if (pi->duration && ps->pos >= pi->duration * 8) {
00130 ps->pos = 0;
00131 ps->npos++;
00132 if (ps->npos >= ps->nitems) {
00133 if (ps->reppos == -1)
00134 return -1;
00135 ps->npos = ps->reppos;
00136 }
00137 }
00138 return 0;
00139 }
00140
00141 static struct ast_generator playtones = {
00142 alloc: playtones_alloc,
00143 release: playtones_release,
00144 generate: playtones_generator,
00145 };
00146
00147 int ast_playtones_start(struct ast_channel *chan, int vol, const char *playlst, int interruptible)
00148 {
00149 char *s, *data = ast_strdupa(playlst);
00150 struct playtones_def d = { vol, -1, 0, 1, NULL};
00151 char *stringp=NULL;
00152 char *separator;
00153 if (!data)
00154 return -1;
00155 if (vol < 1)
00156 d.vol = 8192;
00157
00158 d.interruptible = interruptible;
00159
00160 stringp=data;
00161
00162
00163 if (strchr(stringp,'|'))
00164 separator = "|";
00165 else
00166 separator = ",";
00167 s = strsep(&stringp,separator);
00168 while(s && *s) {
00169 int freq1, freq2, time, modulate=0;
00170
00171 if (s[0]=='!')
00172 s++;
00173 else if (d.reppos == -1)
00174 d.reppos = d.nitems;
00175 if (sscanf(s, "%d+%d/%d", &freq1, &freq2, &time) == 3) {
00176
00177 } else if (sscanf(s, "%d+%d", &freq1, &freq2) == 2) {
00178
00179 time = 0;
00180 } else if (sscanf(s, "%d*%d/%d", &freq1, &freq2, &time) == 3) {
00181
00182 modulate = 1;
00183 } else if (sscanf(s, "%d*%d", &freq1, &freq2) == 2) {
00184
00185 time = 0;
00186 modulate = 1;
00187 } else if (sscanf(s, "%d/%d", &freq1, &time) == 2) {
00188
00189 freq2 = 0;
00190 } else if (sscanf(s, "%d", &freq1) == 1) {
00191
00192 freq2 = 0;
00193 time = 0;
00194 } else {
00195 ast_log(LOG_WARNING,"%s: tone component '%s' of '%s' is no good\n",chan->name,s,playlst);
00196 return -1;
00197 }
00198
00199 d.items = realloc(d.items,(d.nitems+1)*sizeof(struct playtones_item));
00200 if (d.items == NULL)
00201 return -1;
00202 d.items[d.nitems].freq1 = freq1;
00203 d.items[d.nitems].freq2 = freq2;
00204 d.items[d.nitems].duration = time;
00205 d.items[d.nitems].modulate = modulate;
00206 d.nitems++;
00207
00208 s = strsep(&stringp,separator);
00209 }
00210
00211 if (ast_activate_generator(chan, &playtones, &d)) {
00212 free(d.items);
00213 return -1;
00214 }
00215 return 0;
00216 }
00217
00218 void ast_playtones_stop(struct ast_channel *chan)
00219 {
00220 ast_deactivate_generator(chan);
00221 }
00222
00223
00224
00225 struct tone_zone *tone_zones;
00226 static struct tone_zone *current_tonezone;
00227
00228
00229
00230 ast_mutex_t tzlock = AST_MUTEX_INITIALIZER;
00231
00232
00233 int ast_set_indication_country(const char *country)
00234 {
00235 if (country) {
00236 struct tone_zone *z = ast_get_indication_zone(country);
00237 if (z) {
00238 if (option_verbose > 2)
00239 ast_verbose(VERBOSE_PREFIX_3 "Setting default indication country to '%s'\n",country);
00240 current_tonezone = z;
00241 return 0;
00242 }
00243 }
00244 return 1;
00245 }
00246
00247
00248 struct tone_zone *ast_get_indication_zone(const char *country)
00249 {
00250 struct tone_zone *tz;
00251 int alias_loop = 0;
00252
00253
00254 if (country == NULL && current_tonezone)
00255 return current_tonezone;
00256 if (country == NULL && tone_zones)
00257 return tone_zones;
00258 if (country == NULL)
00259 return 0;
00260
00261 if (ast_mutex_lock(&tzlock)) {
00262 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
00263 return 0;
00264 }
00265 do {
00266 for (tz=tone_zones; tz; tz=tz->next) {
00267 if (strcasecmp(country,tz->country)==0) {
00268
00269 if (tz->alias && tz->alias[0]) {
00270 country = tz->alias;
00271 break;
00272 }
00273 ast_mutex_unlock(&tzlock);
00274 return tz;
00275 }
00276 }
00277 } while (++alias_loop<20 && tz);
00278 ast_mutex_unlock(&tzlock);
00279 if (alias_loop==20)
00280 ast_log(LOG_NOTICE,"Alias loop for '%s' forcefull broken\n",country);
00281
00282 return 0;
00283 }
00284
00285
00286 struct tone_zone_sound *ast_get_indication_tone(const struct tone_zone *zone, const char *indication)
00287 {
00288 struct tone_zone_sound *ts;
00289
00290
00291 if (zone == NULL && current_tonezone)
00292 zone = current_tonezone;
00293 if (zone == NULL && tone_zones)
00294 zone = tone_zones;
00295 if (zone == NULL)
00296 return 0;
00297
00298 if (ast_mutex_lock(&tzlock)) {
00299 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
00300 return 0;
00301 }
00302 for (ts=zone->tones; ts; ts=ts->next) {
00303 if (strcasecmp(indication,ts->name)==0) {
00304
00305 ast_mutex_unlock(&tzlock);
00306 return ts;
00307 }
00308 }
00309
00310 ast_mutex_unlock(&tzlock);
00311 return 0;
00312 }
00313
00314
00315 static inline void free_zone(struct tone_zone* zone)
00316 {
00317 while (zone->tones) {
00318 struct tone_zone_sound *tmp = zone->tones->next;
00319 free((void*)zone->tones->name);
00320 free((void*)zone->tones->data);
00321 free(zone->tones);
00322 zone->tones = tmp;
00323 }
00324 free(zone);
00325 }
00326
00327
00328
00329
00330 int ast_register_indication_country(struct tone_zone *zone)
00331 {
00332 struct tone_zone *tz,*pz;
00333
00334 if (ast_mutex_lock(&tzlock)) {
00335 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
00336 return -1;
00337 }
00338 for (pz=NULL,tz=tone_zones; tz; pz=tz,tz=tz->next) {
00339 if (strcasecmp(zone->country,tz->country)==0) {
00340
00341 zone->next = tz->next;
00342 if (pz)
00343 pz->next = zone;
00344 else
00345 tone_zones = zone;
00346
00347 if (tz == current_tonezone)
00348 current_tonezone = zone;
00349
00350 free_zone(tz);
00351 ast_mutex_unlock(&tzlock);
00352 return 0;
00353 }
00354 }
00355
00356 zone->next = NULL;
00357 if (pz)
00358 pz->next = zone;
00359 else
00360 tone_zones = zone;
00361 ast_mutex_unlock(&tzlock);
00362
00363 if (option_verbose > 2)
00364 ast_verbose(VERBOSE_PREFIX_3 "Registered indication country '%s'\n",zone->country);
00365 return 0;
00366 }
00367
00368
00369
00370 int ast_unregister_indication_country(const char *country)
00371 {
00372 struct tone_zone *tz, *pz = NULL, *tmp;
00373 int res = -1;
00374
00375 if (ast_mutex_lock(&tzlock)) {
00376 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
00377 return -1;
00378 }
00379 tz = tone_zones;
00380 while (tz) {
00381 if (country==NULL ||
00382 (strcasecmp(country, tz->country)==0 ||
00383 strcasecmp(country, tz->alias)==0)) {
00384
00385 tmp = tz->next;
00386 if (pz)
00387 pz->next = tmp;
00388 else
00389 tone_zones = tmp;
00390
00391 if (tz == current_tonezone) {
00392 ast_log(LOG_NOTICE,"Removed default indication country '%s'\n",tz->country);
00393 current_tonezone = NULL;
00394 }
00395 if (option_verbose > 2)
00396 ast_verbose(VERBOSE_PREFIX_3 "Unregistered indication country '%s'\n",tz->country);
00397 free_zone(tz);
00398 tz = tmp;
00399 res = 0;
00400 }
00401 else {
00402
00403 pz = tz;
00404 tz = tz->next;
00405 }
00406 }
00407 ast_mutex_unlock(&tzlock);
00408 return res;
00409 }
00410
00411
00412
00413 int ast_register_indication(struct tone_zone *zone, const char *indication, const char *tonelist)
00414 {
00415 struct tone_zone_sound *ts,*ps;
00416
00417
00418 if (zone->alias[0])
00419 return -1;
00420
00421 if (ast_mutex_lock(&tzlock)) {
00422 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
00423 return -2;
00424 }
00425 for (ps=NULL,ts=zone->tones; ts; ps=ts,ts=ts->next) {
00426 if (strcasecmp(indication,ts->name)==0) {
00427
00428 free((void*)ts->name);
00429 free((void*)ts->data);
00430 break;
00431 }
00432 }
00433 if (!ts) {
00434
00435 ts = malloc(sizeof(struct tone_zone_sound));
00436 if (!ts) {
00437 ast_log(LOG_WARNING, "Out of memory\n");
00438 ast_mutex_unlock(&tzlock);
00439 return -2;
00440 }
00441 ts->next = NULL;
00442 }
00443 ts->name = strdup(indication);
00444 ts->data = strdup(tonelist);
00445 if (ts->name==NULL || ts->data==NULL) {
00446 ast_log(LOG_WARNING, "Out of memory\n");
00447 ast_mutex_unlock(&tzlock);
00448 return -2;
00449 }
00450 if (ps)
00451 ps->next = ts;
00452 else
00453 zone->tones = ts;
00454 ast_mutex_unlock(&tzlock);
00455 return 0;
00456 }
00457
00458
00459 int ast_unregister_indication(struct tone_zone *zone, const char *indication)
00460 {
00461 struct tone_zone_sound *ts,*ps = NULL, *tmp;
00462 int res = -1;
00463
00464
00465 if (zone->alias[0])
00466 return -1;
00467
00468 if (ast_mutex_lock(&tzlock)) {
00469 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
00470 return -1;
00471 }
00472 ts = zone->tones;
00473 while (ts) {
00474 if (strcasecmp(indication,ts->name)==0) {
00475
00476 tmp = ts->next;
00477 if (ps)
00478 ps->next = tmp;
00479 else
00480 zone->tones = tmp;
00481 free((void*)ts->name);
00482 free((void*)ts->data);
00483 free(ts);
00484 ts = tmp;
00485 res = 0;
00486 }
00487 else {
00488
00489 ps = ts;
00490 ts = ts->next;
00491 }
00492 }
00493
00494 ast_mutex_unlock(&tzlock);
00495 return res;
00496 }