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 ps->f.delivery.tv_sec = 0;
00127 ps->f.delivery.tv_usec = 0;
00128
ast_write(chan, &ps->f);
00129
00130 ps->pos += x;
00131
if (pi->duration && ps->pos >= pi->duration * 8) {
00132 ps->pos = 0;
00133 ps->npos++;
00134
if (ps->npos >= ps->nitems) {
00135
if (ps->reppos == -1)
00136
return -1;
00137 ps->npos = ps->reppos;
00138 }
00139 }
00140
return 0;
00141 }
00142
00143
static struct ast_generator playtones = {
00144 alloc: playtones_alloc,
00145 release: playtones_release,
00146 generate: playtones_generator,
00147 };
00148
00149 int ast_playtones_start(
struct ast_channel *chan,
int vol,
const char *playlst,
int interruptible)
00150 {
00151
char *
s, *data = ast_strdupa(playlst);
00152
struct playtones_def d = { vol, -1, 0, 1, NULL};
00153
char *stringp=NULL;
00154
char *separator;
00155
if (!data)
00156
return -1;
00157
if (vol < 1)
00158 d.
vol = 8192;
00159
00160 d.
interruptible = interruptible;
00161
00162 stringp=data;
00163
00164
00165
if (strchr(stringp,
'|'))
00166 separator =
"|";
00167
else
00168 separator =
",";
00169
s = strsep(&stringp,separator);
00170
while(
s && *
s) {
00171
int freq1, freq2, time, modulate=0;
00172
00173
if (
s[0]==
'!')
00174
s++;
00175
else if (d.
reppos == -1)
00176 d.
reppos = d.
nitems;
00177
if (sscanf(
s,
"%d+%d/%d", &freq1, &freq2, &time) == 3) {
00178
00179 }
else if (sscanf(
s,
"%d+%d", &freq1, &freq2) == 2) {
00180
00181 time = 0;
00182 }
else if (sscanf(
s,
"%d*%d/%d", &freq1, &freq2, &time) == 3) {
00183
00184 modulate = 1;
00185 }
else if (sscanf(
s,
"%d*%d", &freq1, &freq2) == 2) {
00186
00187 time = 0;
00188 modulate = 1;
00189 }
else if (sscanf(
s,
"%d/%d", &freq1, &time) == 2) {
00190
00191 freq2 = 0;
00192 }
else if (sscanf(
s,
"%d", &freq1) == 1) {
00193
00194 freq2 = 0;
00195 time = 0;
00196 }
else {
00197
ast_log(
LOG_WARNING,
"%s: tone component '%s' of '%s' is no good\n",chan->
name,
s,playlst);
00198
return -1;
00199 }
00200
00201 d.
items =
realloc(d.
items,(d.
nitems+1)*
sizeof(
struct playtones_item));
00202
if (d.
items == NULL)
00203
return -1;
00204 d.
items[d.
nitems].
freq1 = freq1;
00205 d.
items[d.
nitems].
freq2 = freq2;
00206 d.
items[d.
nitems].
duration = time;
00207 d.
items[d.
nitems].
modulate = modulate;
00208 d.
nitems++;
00209
00210
s = strsep(&stringp,separator);
00211 }
00212
00213
if (
ast_activate_generator(chan, &playtones, &d)) {
00214
free(d.
items);
00215
return -1;
00216 }
00217
return 0;
00218 }
00219
00220 void ast_playtones_stop(
struct ast_channel *chan)
00221 {
00222
ast_deactivate_generator(chan);
00223 }
00224
00225
00226
00227 struct tone_zone *
tone_zones;
00228
static struct tone_zone *current_tonezone;
00229
00230
00231
00232
AST_MUTEX_DEFINE_EXPORTED(tzlock);
00233
00234
00235 int ast_set_indication_country(
const char *country)
00236 {
00237
if (country) {
00238
struct tone_zone *z =
ast_get_indication_zone(country);
00239
if (z) {
00240
if (
option_verbose > 2)
00241
ast_verbose(
VERBOSE_PREFIX_3 "Setting default indication country to '%s'\n",country);
00242 current_tonezone = z;
00243
return 0;
00244 }
00245 }
00246
return 1;
00247 }
00248
00249
00250 struct tone_zone *
ast_get_indication_zone(
const char *country)
00251 {
00252
struct tone_zone *tz;
00253
int alias_loop = 0;
00254
00255
00256
if (country == NULL && current_tonezone)
00257
return current_tonezone;
00258
if (country == NULL &&
tone_zones)
00259
return tone_zones;
00260
if (country == NULL)
00261
return 0;
00262
00263
if (
ast_mutex_lock(&
tzlock)) {
00264
ast_log(
LOG_WARNING,
"Unable to lock tone_zones list\n");
00265
return 0;
00266 }
00267
do {
00268
for (tz=
tone_zones; tz; tz=tz->
next) {
00269
if (strcasecmp(country,tz->country)==0) {
00270
00271
if (tz->alias && tz->alias[0]) {
00272 country = tz->alias;
00273
break;
00274 }
00275
ast_mutex_unlock(&
tzlock);
00276
return tz;
00277 }
00278 }
00279 }
while (++alias_loop<20 && tz);
00280
ast_mutex_unlock(&
tzlock);
00281
if (alias_loop==20)
00282
ast_log(
LOG_NOTICE,
"Alias loop for '%s' forcefull broken\n",country);
00283
00284
return 0;
00285 }
00286
00287
00288 struct tone_zone_sound *
ast_get_indication_tone(
const struct tone_zone *zone,
const char *indication)
00289 {
00290
struct tone_zone_sound *ts;
00291
00292
00293
if (zone == NULL && current_tonezone)
00294 zone = current_tonezone;
00295
if (zone == NULL &&
tone_zones)
00296 zone =
tone_zones;
00297
if (zone == NULL)
00298
return 0;
00299
00300
if (
ast_mutex_lock(&
tzlock)) {
00301
ast_log(
LOG_WARNING,
"Unable to lock tone_zones list\n");
00302
return 0;
00303 }
00304
for (ts=zone->
tones; ts; ts=ts->
next) {
00305
if (strcasecmp(indication,ts->name)==0) {
00306
00307
ast_mutex_unlock(&
tzlock);
00308
return ts;
00309 }
00310 }
00311
00312
ast_mutex_unlock(&
tzlock);
00313
return 0;
00314 }
00315
00316
00317
static inline void free_zone(
struct tone_zone* zone)
00318 {
00319
while (zone->
tones) {
00320
struct tone_zone_sound *tmp = zone->
tones->
next;
00321
free((
void*)zone->
tones->
name);
00322
free((
void*)zone->
tones->
data);
00323
free(zone->
tones);
00324 zone->
tones = tmp;
00325 }
00326
if (zone->
ringcadance)
00327
free((
void*)zone->
ringcadance);
00328
free(zone);
00329 }
00330
00331
00332
00333
00334 int ast_register_indication_country(
struct tone_zone *zone)
00335 {
00336
struct tone_zone *tz,*pz;
00337
00338
if (
ast_mutex_lock(&
tzlock)) {
00339
ast_log(
LOG_WARNING,
"Unable to lock tone_zones list\n");
00340
return -1;
00341 }
00342
for (pz=NULL,tz=
tone_zones; tz; pz=tz,tz=tz->
next) {
00343
if (strcasecmp(zone->
country,tz->country)==0) {
00344
00345 zone->
next = tz->
next;
00346
if (pz)
00347 pz->
next = zone;
00348
else
00349
tone_zones = zone;
00350
00351
if (tz == current_tonezone)
00352 current_tonezone = zone;
00353
00354 free_zone(tz);
00355
ast_mutex_unlock(&
tzlock);
00356
return 0;
00357 }
00358 }
00359
00360 zone->
next = NULL;
00361
if (pz)
00362 pz->
next = zone;
00363
else
00364
tone_zones = zone;
00365
ast_mutex_unlock(&
tzlock);
00366
00367
if (
option_verbose > 2)
00368
ast_verbose(
VERBOSE_PREFIX_3 "Registered indication country '%s'\n",zone->country);
00369
return 0;
00370 }
00371
00372
00373
00374 int ast_unregister_indication_country(
const char *country)
00375 {
00376
struct tone_zone *tz, *pz = NULL, *tmp;
00377
int res = -1;
00378
00379
if (
ast_mutex_lock(&
tzlock)) {
00380
ast_log(
LOG_WARNING,
"Unable to lock tone_zones list\n");
00381
return -1;
00382 }
00383 tz =
tone_zones;
00384
while (tz) {
00385
if (country==NULL ||
00386 (strcasecmp(country, tz->country)==0 ||
00387 strcasecmp(country, tz->alias)==0)) {
00388
00389 tmp = tz->
next;
00390
if (pz)
00391 pz->
next = tmp;
00392
else
00393
tone_zones = tmp;
00394
00395
if (tz == current_tonezone) {
00396
ast_log(
LOG_NOTICE,
"Removed default indication country '%s'\n",tz->country);
00397 current_tonezone = NULL;
00398 }
00399
if (
option_verbose > 2)
00400
ast_verbose(
VERBOSE_PREFIX_3 "Unregistered indication country '%s'\n",tz->country);
00401 free_zone(tz);
00402
if (
tone_zones == tz)
00403
tone_zones = tmp;
00404 tz = tmp;
00405 res = 0;
00406 }
00407
else {
00408
00409 pz = tz;
00410 tz = tz->
next;
00411 }
00412 }
00413
ast_mutex_unlock(&
tzlock);
00414
return res;
00415 }
00416
00417
00418
00419 int ast_register_indication(
struct tone_zone *zone,
const char *indication,
const char *tonelist)
00420 {
00421
struct tone_zone_sound *ts,*ps;
00422
00423
00424
if (zone->
alias[0])
00425
return -1;
00426
00427
if (
ast_mutex_lock(&
tzlock)) {
00428
ast_log(
LOG_WARNING,
"Unable to lock tone_zones list\n");
00429
return -2;
00430 }
00431
for (ps=NULL,ts=zone->
tones; ts; ps=ts,ts=ts->
next) {
00432
if (strcasecmp(indication,ts->name)==0) {
00433
00434
free((
void*)ts->name);
00435
free((
void*)ts->data);
00436
break;
00437 }
00438 }
00439
if (!ts) {
00440
00441 ts =
malloc(
sizeof(
struct tone_zone_sound));
00442
if (!ts) {
00443
ast_log(
LOG_WARNING,
"Out of memory\n");
00444
ast_mutex_unlock(&
tzlock);
00445
return -2;
00446 }
00447 ts->next = NULL;
00448 }
00449 ts->name =
strdup(indication);
00450 ts->data =
strdup(tonelist);
00451
if (ts->name==NULL || ts->data==NULL) {
00452
ast_log(
LOG_WARNING,
"Out of memory\n");
00453
ast_mutex_unlock(&
tzlock);
00454
return -2;
00455 }
00456
if (ps)
00457 ps->next = ts;
00458
else
00459 zone->
tones = ts;
00460
ast_mutex_unlock(&
tzlock);
00461
return 0;
00462 }
00463
00464
00465 int ast_unregister_indication(
struct tone_zone *zone,
const char *indication)
00466 {
00467
struct tone_zone_sound *ts,*ps = NULL, *tmp;
00468
int res = -1;
00469
00470
00471
if (zone->
alias[0])
00472
return -1;
00473
00474
if (
ast_mutex_lock(&
tzlock)) {
00475
ast_log(
LOG_WARNING,
"Unable to lock tone_zones list\n");
00476
return -1;
00477 }
00478 ts = zone->
tones;
00479
while (ts) {
00480
if (strcasecmp(indication,ts->name)==0) {
00481
00482 tmp = ts->
next;
00483
if (ps)
00484 ps->
next = tmp;
00485
else
00486 zone->
tones = tmp;
00487
free((
void*)ts->name);
00488
free((
void*)ts->data);
00489
free(ts);
00490 ts = tmp;
00491 res = 0;
00492 }
00493
else {
00494
00495 ps = ts;
00496 ts = ts->next;
00497 }
00498 }
00499
00500
ast_mutex_unlock(&
tzlock);
00501
return res;
00502 }