00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "asterisk.h"
00034
00035 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00036
00037 #include <stdlib.h>
00038 #include <errno.h>
00039 #include <unistd.h>
00040 #include <string.h>
00041 #include <signal.h>
00042 #include <stdlib.h>
00043 #include <stdio.h>
00044 #include <sys/time.h>
00045 #include <sys/signal.h>
00046 #include <netinet/in.h>
00047 #include <sys/stat.h>
00048 #include <dirent.h>
00049 #include <unistd.h>
00050 #include <sys/ioctl.h>
00051 #ifdef SOLARIS
00052 #include <thread.h>
00053 #endif
00054
00055 #include "asterisk/lock.h"
00056 #include "asterisk/file.h"
00057 #include "asterisk/logger.h"
00058 #include "asterisk/channel.h"
00059 #include "asterisk/pbx.h"
00060 #include "asterisk/options.h"
00061 #include "asterisk/module.h"
00062 #include "asterisk/translate.h"
00063 #include "asterisk/say.h"
00064 #include "asterisk/musiconhold.h"
00065 #include "asterisk/config.h"
00066 #include "asterisk/utils.h"
00067 #include "asterisk/cli.h"
00068 #include "asterisk/stringfields.h"
00069 #include "asterisk/linkedlists.h"
00070
00071 #include "asterisk/dahdi_compat.h"
00072
00073 #define INITIAL_NUM_FILES 8
00074
00075 static char *app0 = "MusicOnHold";
00076 static char *app1 = "WaitMusicOnHold";
00077 static char *app2 = "SetMusicOnHold";
00078 static char *app3 = "StartMusicOnHold";
00079 static char *app4 = "StopMusicOnHold";
00080
00081 static char *synopsis0 = "Play Music On Hold indefinitely";
00082 static char *synopsis1 = "Wait, playing Music On Hold";
00083 static char *synopsis2 = "Set default Music On Hold class";
00084 static char *synopsis3 = "Play Music On Hold";
00085 static char *synopsis4 = "Stop Playing Music On Hold";
00086
00087 static char *descrip0 = "MusicOnHold(class): "
00088 "Plays hold music specified by class. If omitted, the default\n"
00089 "music source for the channel will be used. Set the default \n"
00090 "class with the SetMusicOnHold() application.\n"
00091 "Returns -1 on hangup.\n"
00092 "Never returns otherwise.\n";
00093
00094 static char *descrip1 = "WaitMusicOnHold(delay): "
00095 "Plays hold music specified number of seconds. Returns 0 when\n"
00096 "done, or -1 on hangup. If no hold music is available, the delay will\n"
00097 "still occur with no sound.\n";
00098
00099 static char *descrip2 = "SetMusicOnHold(class): "
00100 "Sets the default class for music on hold for a given channel. When\n"
00101 "music on hold is activated, this class will be used to select which\n"
00102 "music is played.\n";
00103
00104 static char *descrip3 = "StartMusicOnHold(class): "
00105 "Starts playing music on hold, uses default music class for channel.\n"
00106 "Starts playing music specified by class. If omitted, the default\n"
00107 "music source for the channel will be used. Always returns 0.\n";
00108
00109 static char *descrip4 = "StopMusicOnHold: "
00110 "Stops playing music on hold.\n";
00111
00112 static int respawn_time = 20;
00113
00114 struct moh_files_state {
00115 struct mohclass *class;
00116 int origwfmt;
00117 int samples;
00118 int sample_queue;
00119 int pos;
00120 int save_pos;
00121 char *save_pos_filename;
00122 };
00123
00124 #define MOH_QUIET (1 << 0)
00125 #define MOH_SINGLE (1 << 1)
00126 #define MOH_CUSTOM (1 << 2)
00127 #define MOH_RANDOMIZE (1 << 3)
00128
00129 struct mohclass {
00130 char name[MAX_MUSICCLASS];
00131 char dir[256];
00132 char args[256];
00133 char mode[80];
00134
00135 char **filearray;
00136
00137 int allowed_files;
00138
00139 int total_files;
00140 unsigned int flags;
00141
00142 int format;
00143
00144 int pid;
00145 time_t start;
00146 pthread_t thread;
00147
00148 int srcfd;
00149
00150 int pseudofd;
00151
00152 int inuse;
00153 unsigned int delete:1;
00154 AST_LIST_HEAD_NOLOCK(, mohdata) members;
00155 AST_LIST_ENTRY(mohclass) list;
00156 };
00157
00158 struct mohdata {
00159 int pipe[2];
00160 int origwfmt;
00161 struct mohclass *parent;
00162 struct ast_frame f;
00163 AST_LIST_ENTRY(mohdata) list;
00164 };
00165
00166 AST_LIST_HEAD_STATIC(mohclasses, mohclass);
00167
00168 #define LOCAL_MPG_123 "/usr/local/bin/mpg123"
00169 #define MPG_123 "/usr/bin/mpg123"
00170 #define MAX_MP3S 256
00171
00172 static int ast_moh_destroy_one(struct mohclass *moh);
00173 static int reload(void);
00174
00175 static void ast_moh_free_class(struct mohclass **mohclass)
00176 {
00177 struct mohdata *member;
00178 struct mohclass *class = *mohclass;
00179 int i;
00180
00181 while ((member = AST_LIST_REMOVE_HEAD(&class->members, list)))
00182 free(member);
00183
00184 if (class->thread) {
00185 pthread_cancel(class->thread);
00186 class->thread = 0;
00187 }
00188
00189 if (class->filearray) {
00190 for (i = 0; i < class->total_files; i++)
00191 free(class->filearray[i]);
00192 free(class->filearray);
00193 }
00194
00195 free(class);
00196 *mohclass = NULL;
00197 }
00198
00199
00200 static void moh_files_release(struct ast_channel *chan, void *data)
00201 {
00202 struct moh_files_state *state;
00203
00204 if (chan) {
00205 if ((state = chan->music_state)) {
00206 if (chan->stream) {
00207 ast_closestream(chan->stream);
00208 chan->stream = NULL;
00209 }
00210 if (option_verbose > 2)
00211 ast_verbose(VERBOSE_PREFIX_3 "Stopped music on hold on %s\n", chan->name);
00212
00213 if (state->origwfmt && ast_set_write_format(chan, state->origwfmt)) {
00214 ast_log(LOG_WARNING, "Unable to restore channel '%s' to format '%d'\n", chan->name, state->origwfmt);
00215 }
00216 state->save_pos = state->pos;
00217
00218 if (ast_atomic_dec_and_test(&state->class->inuse) && state->class->delete)
00219 ast_moh_destroy_one(state->class);
00220 }
00221 }
00222 }
00223
00224
00225 static int ast_moh_files_next(struct ast_channel *chan)
00226 {
00227 struct moh_files_state *state = chan->music_state;
00228 int tries;
00229
00230
00231 if (chan->stream) {
00232 ast_closestream(chan->stream);
00233 chan->stream = NULL;
00234 }
00235
00236 if (!state->class->total_files) {
00237 ast_log(LOG_WARNING, "No files available for class '%s'\n", state->class->name);
00238 return -1;
00239 }
00240
00241
00242 if (state->save_pos >= 0 && state->save_pos < state->class->total_files && state->class->filearray[state->save_pos] == state->save_pos_filename) {
00243 state->pos = state->save_pos;
00244 state->save_pos = -1;
00245 } else if (ast_test_flag(state->class, MOH_RANDOMIZE)) {
00246
00247 for (tries = 0; tries < 20; tries++) {
00248 state->pos = ast_random() % state->class->total_files;
00249 if (ast_fileexists(state->class->filearray[state->pos], NULL, NULL) > 0)
00250 break;
00251 }
00252 state->save_pos = -1;
00253 state->samples = 0;
00254 } else {
00255
00256 state->pos++;
00257 state->pos %= state->class->total_files;
00258 state->save_pos = -1;
00259 state->samples = 0;
00260 }
00261
00262 if (!ast_openstream_full(chan, state->class->filearray[state->pos], chan->language, 1)) {
00263 ast_log(LOG_WARNING, "Unable to open file '%s': %s\n", state->class->filearray[state->pos], strerror(errno));
00264 state->pos++;
00265 state->pos %= state->class->total_files;
00266 return -1;
00267 }
00268
00269
00270 state->save_pos_filename = state->class->filearray[state->pos];
00271
00272 if (option_debug)
00273 ast_log(LOG_DEBUG, "%s Opened file %d '%s'\n", chan->name, state->pos, state->class->filearray[state->pos]);
00274
00275 if (state->samples)
00276 ast_seekstream(chan->stream, state->samples, SEEK_SET);
00277
00278 return 0;
00279 }
00280
00281
00282 static struct ast_frame *moh_files_readframe(struct ast_channel *chan)
00283 {
00284 struct ast_frame *f = NULL;
00285
00286 if (!(chan->stream && (f = ast_readframe(chan->stream)))) {
00287 if (!ast_moh_files_next(chan))
00288 f = ast_readframe(chan->stream);
00289 }
00290
00291 return f;
00292 }
00293
00294 static int moh_files_generator(struct ast_channel *chan, void *data, int len, int samples)
00295 {
00296 struct moh_files_state *state = chan->music_state;
00297 struct ast_frame *f = NULL;
00298 int res = 0;
00299
00300 state->sample_queue += samples;
00301
00302 while (state->sample_queue > 0) {
00303 if ((f = moh_files_readframe(chan))) {
00304 state->samples += f->samples;
00305 state->sample_queue -= f->samples;
00306 res = ast_write(chan, f);
00307 ast_frfree(f);
00308 if (res < 0) {
00309 ast_log(LOG_WARNING, "Failed to write frame to '%s': %s\n", chan->name, strerror(errno));
00310 return -1;
00311 }
00312 } else
00313 return -1;
00314 }
00315 return res;
00316 }
00317
00318
00319 static void *moh_files_alloc(struct ast_channel *chan, void *params)
00320 {
00321 struct moh_files_state *state;
00322 struct mohclass *class = params;
00323
00324 if (!chan->music_state && (state = ast_calloc(1, sizeof(*state)))) {
00325 chan->music_state = state;
00326 state->class = class;
00327 state->save_pos = -1;
00328 } else
00329 state = chan->music_state;
00330
00331 if (state) {
00332 if (state->class != class) {
00333
00334 memset(state, 0, sizeof(*state));
00335 state->class = class;
00336 if (ast_test_flag(state->class, MOH_RANDOMIZE) && class->total_files)
00337 state->pos = ast_random() % class->total_files;
00338 }
00339
00340 state->origwfmt = chan->writeformat;
00341
00342 if (option_verbose > 2)
00343 ast_verbose(VERBOSE_PREFIX_3 "Started music on hold, class '%s', on %s\n", class->name, chan->name);
00344 }
00345
00346 return chan->music_state;
00347 }
00348
00349 static struct ast_generator moh_file_stream =
00350 {
00351 alloc: moh_files_alloc,
00352 release: moh_files_release,
00353 generate: moh_files_generator,
00354 };
00355
00356 static int spawn_mp3(struct mohclass *class)
00357 {
00358 int fds[2];
00359 int files = 0;
00360 char fns[MAX_MP3S][80];
00361 char *argv[MAX_MP3S + 50];
00362 char xargs[256];
00363 char *argptr;
00364 int argc = 0;
00365 DIR *dir = NULL;
00366 struct dirent *de;
00367 sigset_t signal_set, old_set;
00368
00369
00370 if (!strcasecmp(class->dir, "nodir")) {
00371 files = 1;
00372 } else {
00373 dir = opendir(class->dir);
00374 if (!dir && strncasecmp(class->dir, "http://", 7)) {
00375 ast_log(LOG_WARNING, "%s is not a valid directory\n", class->dir);
00376 return -1;
00377 }
00378 }
00379
00380 if (!ast_test_flag(class, MOH_CUSTOM)) {
00381 argv[argc++] = "mpg123";
00382 argv[argc++] = "-q";
00383 argv[argc++] = "-s";
00384 argv[argc++] = "--mono";
00385 argv[argc++] = "-r";
00386 argv[argc++] = "8000";
00387
00388 if (!ast_test_flag(class, MOH_SINGLE)) {
00389 argv[argc++] = "-b";
00390 argv[argc++] = "2048";
00391 }
00392
00393 argv[argc++] = "-f";
00394
00395 if (ast_test_flag(class, MOH_QUIET))
00396 argv[argc++] = "4096";
00397 else
00398 argv[argc++] = "8192";
00399
00400
00401 ast_copy_string(xargs, class->args, sizeof(xargs));
00402 argptr = xargs;
00403 while (!ast_strlen_zero(argptr)) {
00404 argv[argc++] = argptr;
00405 strsep(&argptr, ",");
00406 }
00407 } else {
00408
00409 ast_copy_string(xargs, class->args, sizeof(xargs));
00410 argptr = xargs;
00411 while (!ast_strlen_zero(argptr)) {
00412 argv[argc++] = argptr;
00413 strsep(&argptr, " ");
00414 }
00415 }
00416
00417 if (!strncasecmp(class->dir, "http://", 7)) {
00418 ast_copy_string(fns[files], class->dir, sizeof(fns[files]));
00419 argv[argc++] = fns[files];
00420 files++;
00421 } else if (dir) {
00422 while ((de = readdir(dir)) && (files < MAX_MP3S)) {
00423 if ((strlen(de->d_name) > 3) &&
00424 ((ast_test_flag(class, MOH_CUSTOM) &&
00425 (!strcasecmp(de->d_name + strlen(de->d_name) - 4, ".raw") ||
00426 !strcasecmp(de->d_name + strlen(de->d_name) - 4, ".sln"))) ||
00427 !strcasecmp(de->d_name + strlen(de->d_name) - 4, ".mp3"))) {
00428 ast_copy_string(fns[files], de->d_name, sizeof(fns[files]));
00429 argv[argc++] = fns[files];
00430 files++;
00431 }
00432 }
00433 }
00434 argv[argc] = NULL;
00435 if (dir) {
00436 closedir(dir);
00437 }
00438 if (pipe(fds)) {
00439 ast_log(LOG_WARNING, "Pipe failed\n");
00440 return -1;
00441 }
00442 if (!files) {
00443 ast_log(LOG_WARNING, "Found no files in '%s'\n", class->dir);
00444 close(fds[0]);
00445 close(fds[1]);
00446 return -1;
00447 }
00448 if (!strncasecmp(class->dir, "http://", 7) && time(NULL) - class->start < respawn_time) {
00449 sleep(respawn_time - (time(NULL) - class->start));
00450 }
00451
00452
00453 sigfillset(&signal_set);
00454 pthread_sigmask(SIG_BLOCK, &signal_set, &old_set);
00455
00456 time(&class->start);
00457 class->pid = fork();
00458 if (class->pid < 0) {
00459 close(fds[0]);
00460 close(fds[1]);
00461 ast_log(LOG_WARNING, "Fork failed: %s\n", strerror(errno));
00462 return -1;
00463 }
00464 if (!class->pid) {
00465 int x;
00466
00467 if (ast_opt_high_priority)
00468 ast_set_priority(0);
00469
00470
00471 signal(SIGPIPE, SIG_DFL);
00472 pthread_sigmask(SIG_UNBLOCK, &signal_set, NULL);
00473
00474 close(fds[0]);
00475
00476 dup2(fds[1], STDOUT_FILENO);
00477
00478 for (x=3;x<8192;x++) {
00479 if (-1 != fcntl(x, F_GETFL)) {
00480 close(x);
00481 }
00482 }
00483
00484 if (strcasecmp(class->dir, "nodir") && chdir(class->dir) < 0) {
00485 ast_log(LOG_WARNING, "chdir() failed: %s\n", strerror(errno));
00486 _exit(1);
00487 }
00488 if (ast_test_flag(class, MOH_CUSTOM)) {
00489 execv(argv[0], argv);
00490 } else {
00491
00492 execv(LOCAL_MPG_123, argv);
00493
00494 execv(MPG_123, argv);
00495
00496 execvp("mpg123", argv);
00497 }
00498 ast_log(LOG_WARNING, "Exec failed: %s\n", strerror(errno));
00499 close(fds[1]);
00500 _exit(1);
00501 } else {
00502
00503 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
00504 close(fds[1]);
00505 }
00506 return fds[0];
00507 }
00508
00509 static void *monmp3thread(void *data)
00510 {
00511 #define MOH_MS_INTERVAL 100
00512
00513 struct mohclass *class = data;
00514 struct mohdata *moh;
00515 char buf[8192];
00516 short sbuf[8192];
00517 int res, res2;
00518 int len;
00519 struct timeval tv, tv_tmp;
00520
00521 tv.tv_sec = 0;
00522 tv.tv_usec = 0;
00523 for(;;) {
00524 pthread_testcancel();
00525
00526 if (class->srcfd < 0) {
00527 if ((class->srcfd = spawn_mp3(class)) < 0) {
00528 ast_log(LOG_WARNING, "Unable to spawn mp3player\n");
00529
00530 sleep(500);
00531 pthread_testcancel();
00532 }
00533 }
00534 if (class->pseudofd > -1) {
00535 #ifdef SOLARIS
00536 thr_yield();
00537 #endif
00538
00539 res = read(class->pseudofd, buf, sizeof(buf));
00540 pthread_testcancel();
00541 } else {
00542 long delta;
00543
00544 tv_tmp = ast_tvnow();
00545 if (ast_tvzero(tv))
00546 tv = tv_tmp;
00547 delta = ast_tvdiff_ms(tv_tmp, tv);
00548 if (delta < MOH_MS_INTERVAL) {
00549 tv = ast_tvadd(tv, ast_samp2tv(MOH_MS_INTERVAL, 1000));
00550 usleep(1000 * (MOH_MS_INTERVAL - delta));
00551 pthread_testcancel();
00552 } else {
00553 ast_log(LOG_NOTICE, "Request to schedule in the past?!?!\n");
00554 tv = tv_tmp;
00555 }
00556 res = 8 * MOH_MS_INTERVAL;
00557 }
00558 if (AST_LIST_EMPTY(&class->members))
00559 continue;
00560
00561 len = ast_codec_get_len(class->format, res);
00562
00563 if ((res2 = read(class->srcfd, sbuf, len)) != len) {
00564 if (!res2) {
00565 close(class->srcfd);
00566 class->srcfd = -1;
00567 pthread_testcancel();
00568 if (class->pid > 1) {
00569 kill(class->pid, SIGHUP);
00570 usleep(100000);
00571 kill(class->pid, SIGTERM);
00572 usleep(100000);
00573 kill(class->pid, SIGKILL);
00574 class->pid = 0;
00575 }
00576 } else
00577 ast_log(LOG_DEBUG, "Read %d bytes of audio while expecting %d\n", res2, len);
00578 continue;
00579 }
00580 pthread_testcancel();
00581 AST_LIST_LOCK(&mohclasses);
00582 AST_LIST_TRAVERSE(&class->members, moh, list) {
00583
00584 if ((res = write(moh->pipe[1], sbuf, res2)) != res2) {
00585 if (option_debug)
00586 ast_log(LOG_DEBUG, "Only wrote %d of %d bytes to pipe\n", res, res2);
00587 }
00588 }
00589 AST_LIST_UNLOCK(&mohclasses);
00590 }
00591 return NULL;
00592 }
00593
00594 static int moh0_exec(struct ast_channel *chan, void *data)
00595 {
00596 if (ast_moh_start(chan, data, NULL)) {
00597 ast_log(LOG_WARNING, "Unable to start music on hold (class '%s') on channel %s\n", (char *)data, chan->name);
00598 return 0;
00599 }
00600 while (!ast_safe_sleep(chan, 10000));
00601 ast_moh_stop(chan);
00602 return -1;
00603 }
00604
00605 static int moh1_exec(struct ast_channel *chan, void *data)
00606 {
00607 int res;
00608 if (!data || !atoi(data)) {
00609 ast_log(LOG_WARNING, "WaitMusicOnHold requires an argument (number of seconds to wait)\n");
00610 return -1;
00611 }
00612 if (ast_moh_start(chan, NULL, NULL)) {
00613 ast_log(LOG_WARNING, "Unable to start music on hold for %d seconds on channel %s\n", atoi(data), chan->name);
00614 return 0;
00615 }
00616 res = ast_safe_sleep(chan, atoi(data) * 1000);
00617 ast_moh_stop(chan);
00618 return res;
00619 }
00620
00621 static int moh2_exec(struct ast_channel *chan, void *data)
00622 {
00623 if (ast_strlen_zero(data)) {
00624 ast_log(LOG_WARNING, "SetMusicOnHold requires an argument (class)\n");
00625 return -1;
00626 }
00627 ast_string_field_set(chan, musicclass, data);
00628 return 0;
00629 }
00630
00631 static int moh3_exec(struct ast_channel *chan, void *data)
00632 {
00633 char *class = NULL;
00634 if (data && strlen(data))
00635 class = data;
00636 if (ast_moh_start(chan, class, NULL))
00637 ast_log(LOG_NOTICE, "Unable to start music on hold class '%s' on channel %s\n", class ? class : "default", chan->name);
00638
00639 return 0;
00640 }
00641
00642 static int moh4_exec(struct ast_channel *chan, void *data)
00643 {
00644 ast_moh_stop(chan);
00645
00646 return 0;
00647 }
00648
00649
00650 static struct mohclass *get_mohbyname(const char *name, int warn)
00651 {
00652 struct mohclass *moh = NULL;
00653
00654 AST_LIST_TRAVERSE(&mohclasses, moh, list) {
00655 if (!strcasecmp(name, moh->name))
00656 break;
00657 }
00658
00659 if (!moh && warn)
00660 ast_log(LOG_WARNING, "Music on Hold class '%s' not found\n", name);
00661
00662 return moh;
00663 }
00664
00665 static struct mohdata *mohalloc(struct mohclass *cl)
00666 {
00667 struct mohdata *moh;
00668 long flags;
00669
00670 if (!(moh = ast_calloc(1, sizeof(*moh))))
00671 return NULL;
00672
00673 if (pipe(moh->pipe)) {
00674 ast_log(LOG_WARNING, "Failed to create pipe: %s\n", strerror(errno));
00675 free(moh);
00676 return NULL;
00677 }
00678
00679
00680 flags = fcntl(moh->pipe[0], F_GETFL);
00681 fcntl(moh->pipe[0], F_SETFL, flags | O_NONBLOCK);
00682 flags = fcntl(moh->pipe[1], F_GETFL);
00683 fcntl(moh->pipe[1], F_SETFL, flags | O_NONBLOCK);
00684
00685 moh->f.frametype = AST_FRAME_VOICE;
00686 moh->f.subclass = cl->format;
00687 moh->f.offset = AST_FRIENDLY_OFFSET;
00688
00689 moh->parent = cl;
00690
00691 AST_LIST_LOCK(&mohclasses);
00692 AST_LIST_INSERT_HEAD(&cl->members, moh, list);
00693 AST_LIST_UNLOCK(&mohclasses);
00694
00695 return moh;
00696 }
00697
00698 static void moh_release(struct ast_channel *chan, void *data)
00699 {
00700 struct mohdata *moh = data;
00701 int oldwfmt;
00702
00703 AST_LIST_LOCK(&mohclasses);
00704 AST_LIST_REMOVE(&moh->parent->members, moh, list);
00705 AST_LIST_UNLOCK(&mohclasses);
00706
00707 close(moh->pipe[0]);
00708 close(moh->pipe[1]);
00709 oldwfmt = moh->origwfmt;
00710 if (moh->parent->delete && ast_atomic_dec_and_test(&moh->parent->inuse))
00711 ast_moh_destroy_one(moh->parent);
00712 free(moh);
00713 if (chan) {
00714 if (oldwfmt && ast_set_write_format(chan, oldwfmt))
00715 ast_log(LOG_WARNING, "Unable to restore channel '%s' to format %s\n", chan->name, ast_getformatname(oldwfmt));
00716 if (option_verbose > 2)
00717 ast_verbose(VERBOSE_PREFIX_3 "Stopped music on hold on %s\n", chan->name);
00718 }
00719 }
00720
00721 static void *moh_alloc(struct ast_channel *chan, void *params)
00722 {
00723 struct mohdata *res;
00724 struct mohclass *class = params;
00725
00726 if ((res = mohalloc(class))) {
00727 res->origwfmt = chan->writeformat;
00728 if (ast_set_write_format(chan, class->format)) {
00729 ast_log(LOG_WARNING, "Unable to set channel '%s' to format '%s'\n", chan->name, ast_codec2str(class->format));
00730 moh_release(NULL, res);
00731 res = NULL;
00732 }
00733 if (option_verbose > 2)
00734 ast_verbose(VERBOSE_PREFIX_3 "Started music on hold, class '%s', on channel '%s'\n", class->name, chan->name);
00735 }
00736 return res;
00737 }
00738
00739 static int moh_generate(struct ast_channel *chan, void *data, int len, int samples)
00740 {
00741 struct mohdata *moh = data;
00742 short buf[1280 + AST_FRIENDLY_OFFSET / 2];
00743 int res;
00744
00745 if (!moh->parent->pid && moh->parent->inuse == 0) {
00746 return -1;
00747 }
00748
00749 len = ast_codec_get_len(moh->parent->format, samples);
00750
00751 if (len > sizeof(buf) - AST_FRIENDLY_OFFSET) {
00752 ast_log(LOG_WARNING, "Only doing %d of %d requested bytes on %s\n", (int)sizeof(buf), len, chan->name);
00753 len = sizeof(buf) - AST_FRIENDLY_OFFSET;
00754 }
00755 res = read(moh->pipe[0], buf + AST_FRIENDLY_OFFSET/2, len);
00756 if (res <= 0)
00757 return 0;
00758
00759 moh->f.datalen = res;
00760 moh->f.data = buf + AST_FRIENDLY_OFFSET / 2;
00761 moh->f.samples = ast_codec_get_samples(&moh->f);
00762
00763 if (ast_write(chan, &moh->f) < 0) {
00764 ast_log(LOG_WARNING, "Failed to write frame to '%s': %s\n", chan->name, strerror(errno));
00765 return -1;
00766 }
00767
00768 return 0;
00769 }
00770
00771 static struct ast_generator mohgen =
00772 {
00773 alloc: moh_alloc,
00774 release: moh_release,
00775 generate: moh_generate,
00776 };
00777
00778 static int moh_add_file(struct mohclass *class, const char *filepath)
00779 {
00780 if (!class->allowed_files) {
00781 if (!(class->filearray = ast_calloc(1, INITIAL_NUM_FILES * sizeof(*class->filearray))))
00782 return -1;
00783 class->allowed_files = INITIAL_NUM_FILES;
00784 } else if (class->total_files == class->allowed_files) {
00785 if (!(class->filearray = ast_realloc(class->filearray, class->allowed_files * sizeof(*class->filearray) * 2))) {
00786 class->allowed_files = 0;
00787 class->total_files = 0;
00788 return -1;
00789 }
00790 class->allowed_files *= 2;
00791 }
00792
00793 if (!(class->filearray[class->total_files] = ast_strdup(filepath)))
00794 return -1;
00795
00796 class->total_files++;
00797
00798 return 0;
00799 }
00800
00801 static int moh_scan_files(struct mohclass *class) {
00802
00803 DIR *files_DIR;
00804 struct dirent *files_dirent;
00805 char path[PATH_MAX];
00806 char filepath[PATH_MAX];
00807 char *ext;
00808 struct stat statbuf;
00809 int dirnamelen;
00810 int i;
00811
00812 files_DIR = opendir(class->dir);
00813 if (!files_DIR) {
00814 ast_log(LOG_WARNING, "Cannot open dir %s or dir does not exist\n", class->dir);
00815 return -1;
00816 }
00817
00818 for (i = 0; i < class->total_files; i++)
00819 free(class->filearray[i]);
00820
00821 class->total_files = 0;
00822 dirnamelen = strlen(class->dir) + 2;
00823 if (!getcwd(path, sizeof(path))) {
00824 ast_log(LOG_WARNING, "getcwd() failed: %s\n", strerror(errno));
00825 return -1;
00826 }
00827 if (chdir(class->dir) < 0) {
00828 ast_log(LOG_WARNING, "chdir() failed: %s\n", strerror(errno));
00829 return -1;
00830 }
00831 while ((files_dirent = readdir(files_DIR))) {
00832
00833 if ((strlen(files_dirent->d_name) < 4))
00834 continue;
00835
00836
00837 if (files_dirent->d_name[0] == '.')
00838 continue;
00839
00840
00841 if (!strchr(files_dirent->d_name, '.'))
00842 continue;
00843
00844 snprintf(filepath, sizeof(filepath), "%s/%s", class->dir, files_dirent->d_name);
00845
00846 if (stat(filepath, &statbuf))
00847 continue;
00848
00849 if (!S_ISREG(statbuf.st_mode))
00850 continue;
00851
00852 if ((ext = strrchr(filepath, '.'))) {
00853 *ext = '\0';
00854 ext++;
00855 }
00856
00857
00858 for (i = 0; i < class->total_files; i++)
00859 if (!strcmp(filepath, class->filearray[i]))
00860 break;
00861
00862 if (i == class->total_files) {
00863 if (moh_add_file(class, filepath))
00864 break;
00865 }
00866 }
00867
00868 closedir(files_DIR);
00869 if (chdir(path) < 0) {
00870 ast_log(LOG_WARNING, "chdir() failed: %s\n", strerror(errno));
00871 return -1;
00872 }
00873 return class->total_files;
00874 }
00875
00876 static int moh_register(struct mohclass *moh, int reload)
00877 {
00878 #ifdef HAVE_DAHDI
00879 int x;
00880 #endif
00881 struct mohclass *mohclass = NULL;
00882 int res = 0;
00883
00884 AST_LIST_LOCK(&mohclasses);
00885 if ((mohclass = get_mohbyname(moh->name, 0))) {
00886 if (!mohclass->delete) {
00887 ast_log(LOG_WARNING, "Music on Hold class '%s' already exists\n", moh->name);
00888 free(moh);
00889 AST_LIST_UNLOCK(&mohclasses);
00890 return -1;
00891 }
00892 }
00893 AST_LIST_UNLOCK(&mohclasses);
00894
00895 time(&moh->start);
00896 moh->start -= respawn_time;
00897
00898 if (!strcasecmp(moh->mode, "files")) {
00899 res = moh_scan_files(moh);
00900 if (res <= 0) {
00901 if (res == 0) {
00902 if (option_verbose > 2)
00903 ast_verbose(VERBOSE_PREFIX_3 "Files not found in %s for moh class:%s\n", moh->dir, moh->name);
00904 }
00905 ast_moh_free_class(&moh);
00906 return -1;
00907 }
00908 if (strchr(moh->args, 'r'))
00909 ast_set_flag(moh, MOH_RANDOMIZE);
00910 } else if (!strcasecmp(moh->mode, "mp3") || !strcasecmp(moh->mode, "mp3nb") || !strcasecmp(moh->mode, "quietmp3") || !strcasecmp(moh->mode, "quietmp3nb") || !strcasecmp(moh->mode, "httpmp3") || !strcasecmp(moh->mode, "custom")) {
00911
00912 if (!strcasecmp(moh->mode, "custom"))
00913 ast_set_flag(moh, MOH_CUSTOM);
00914 else if (!strcasecmp(moh->mode, "mp3nb"))
00915 ast_set_flag(moh, MOH_SINGLE);
00916 else if (!strcasecmp(moh->mode, "quietmp3nb"))
00917 ast_set_flag(moh, MOH_SINGLE | MOH_QUIET);
00918 else if (!strcasecmp(moh->mode, "quietmp3"))
00919 ast_set_flag(moh, MOH_QUIET);
00920
00921 moh->srcfd = -1;
00922 #ifdef HAVE_DAHDI
00923
00924
00925 #ifdef HAVE_ZAPTEL
00926 moh->pseudofd = open("/dev/zap/pseudo", O_RDONLY);
00927 #else
00928 moh->pseudofd = open("/dev/dahdi/pseudo", O_RDONLY);
00929 #endif
00930 if (moh->pseudofd < 0) {
00931 ast_log(LOG_WARNING, "Unable to open pseudo channel for timing... Sound may be choppy.\n");
00932 } else {
00933 x = 320;
00934 ioctl(moh->pseudofd, DAHDI_SET_BLOCKSIZE, &x);
00935 }
00936 #else
00937 moh->pseudofd = -1;
00938 #endif
00939 if (ast_pthread_create_background(&moh->thread, NULL, monmp3thread, moh)) {
00940 ast_log(LOG_WARNING, "Unable to create moh...\n");
00941 if (moh->pseudofd > -1)
00942 close(moh->pseudofd);
00943 ast_moh_free_class(&moh);
00944 return -1;
00945 }
00946 } else {
00947 ast_log(LOG_WARNING, "Don't know how to do a mode '%s' music on hold\n", moh->mode);
00948 ast_moh_free_class(&moh);
00949 return -1;
00950 }
00951
00952 AST_LIST_LOCK(&mohclasses);
00953 AST_LIST_INSERT_HEAD(&mohclasses, moh, list);
00954 AST_LIST_UNLOCK(&mohclasses);
00955
00956 return 0;
00957 }
00958
00959 static void local_ast_moh_cleanup(struct ast_channel *chan)
00960 {
00961 if (chan->music_state) {
00962 free(chan->music_state);
00963 chan->music_state = NULL;
00964 }
00965 }
00966
00967 static int local_ast_moh_start(struct ast_channel *chan, const char *mclass, const char *interpclass)
00968 {
00969 struct mohclass *mohclass = NULL;
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982 AST_LIST_LOCK(&mohclasses);
00983 if (!ast_strlen_zero(chan->musicclass))
00984 mohclass = get_mohbyname(chan->musicclass, 1);
00985 if (!mohclass && !ast_strlen_zero(mclass))
00986 mohclass = get_mohbyname(mclass, 1);
00987 if (!mohclass && !ast_strlen_zero(interpclass))
00988 mohclass = get_mohbyname(interpclass, 1);
00989 if (!mohclass)
00990 mohclass = get_mohbyname("default", 1);
00991 if (mohclass)
00992 ast_atomic_fetchadd_int(&mohclass->inuse, +1);
00993 AST_LIST_UNLOCK(&mohclasses);
00994
00995 if (!mohclass)
00996 return -1;
00997
00998 ast_set_flag(chan, AST_FLAG_MOH);
00999 if (mohclass->total_files) {
01000 return ast_activate_generator(chan, &moh_file_stream, mohclass);
01001 } else
01002 return ast_activate_generator(chan, &mohgen, mohclass);
01003 }
01004
01005 static void local_ast_moh_stop(struct ast_channel *chan)
01006 {
01007 ast_clear_flag(chan, AST_FLAG_MOH);
01008 ast_deactivate_generator(chan);
01009
01010 if (chan->music_state) {
01011 if (chan->stream) {
01012 ast_closestream(chan->stream);
01013 chan->stream = NULL;
01014 }
01015 }
01016 }
01017
01018 static struct mohclass *moh_class_malloc(void)
01019 {
01020 struct mohclass *class;
01021
01022 if ((class = ast_calloc(1, sizeof(*class))))
01023 class->format = AST_FORMAT_SLINEAR;
01024
01025 return class;
01026 }
01027
01028 static int load_moh_classes(int reload)
01029 {
01030 struct ast_config *cfg;
01031 struct ast_variable *var;
01032 struct mohclass *class;
01033 char *data;
01034 char *args;
01035 char *cat;
01036 int numclasses = 0;
01037 static int dep_warning = 0;
01038
01039 cfg = ast_config_load("musiconhold.conf");
01040
01041 if (!cfg)
01042 return 0;
01043
01044 if (reload) {
01045 AST_LIST_LOCK(&mohclasses);
01046 AST_LIST_TRAVERSE(&mohclasses, class, list)
01047 class->delete = 1;
01048 AST_LIST_UNLOCK(&mohclasses);
01049 }
01050
01051 cat = ast_category_browse(cfg, NULL);
01052 for (; cat; cat = ast_category_browse(cfg, cat)) {
01053 if (strcasecmp(cat, "classes") && strcasecmp(cat, "moh_files")) {
01054 if (!(class = moh_class_malloc())) {
01055 break;
01056 }
01057 ast_copy_string(class->name, cat, sizeof(class->name));
01058 var = ast_variable_browse(cfg, cat);
01059 while (var) {
01060 if (!strcasecmp(var->name, "mode"))
01061 ast_copy_string(class->mode, var->value, sizeof(class->mode));
01062 else if (!strcasecmp(var->name, "directory"))
01063 ast_copy_string(class->dir, var->value, sizeof(class->dir));
01064 else if (!strcasecmp(var->name, "application"))
01065 ast_copy_string(class->args, var->value, sizeof(class->args));
01066 else if (!strcasecmp(var->name, "random"))
01067 ast_set2_flag(class, ast_true(var->value), MOH_RANDOMIZE);
01068 else if (!strcasecmp(var->name, "format")) {
01069 class->format = ast_getformatbyname(var->value);
01070 if (!class->format) {
01071 ast_log(LOG_WARNING, "Unknown format '%s' -- defaulting to SLIN\n", var->value);
01072 class->format = AST_FORMAT_SLINEAR;
01073 }
01074 }
01075 var = var->next;
01076 }
01077
01078 if (ast_strlen_zero(class->dir)) {
01079 if (!strcasecmp(class->mode, "custom")) {
01080 strcpy(class->dir, "nodir");
01081 } else {
01082 ast_log(LOG_WARNING, "A directory must be specified for class '%s'!\n", class->name);
01083 free(class);
01084 continue;
01085 }
01086 }
01087 if (ast_strlen_zero(class->mode)) {
01088 ast_log(LOG_WARNING, "A mode must be specified for class '%s'!\n", class->name);
01089 free(class);
01090 continue;
01091 }
01092 if (ast_strlen_zero(class->args) && !strcasecmp(class->mode, "custom")) {
01093 ast_log(LOG_WARNING, "An application must be specified for class '%s'!\n", class->name);
01094 free(class);
01095 continue;
01096 }
01097
01098
01099 moh_register(class, reload);
01100
01101 numclasses++;
01102 }
01103 }
01104
01105
01106
01107 var = ast_variable_browse(cfg, "classes");
01108 while (var) {
01109 if (!dep_warning) {
01110 ast_log(LOG_WARNING, "The old musiconhold.conf syntax has been deprecated! Please refer to the sample configuration for information on the new syntax.\n");
01111 dep_warning = 1;
01112 }
01113 data = strchr(var->value, ':');
01114 if (data) {
01115 *data++ = '\0';
01116 args = strchr(data, ',');
01117 if (args)
01118 *args++ = '\0';
01119 if (!(get_mohbyname(var->name, 0))) {
01120 if (!(class = moh_class_malloc())) {
01121 break;
01122 }
01123
01124 ast_copy_string(class->name, var->name, sizeof(class->name));
01125 ast_copy_string(class->dir, data, sizeof(class->dir));
01126 ast_copy_string(class->mode, var->value, sizeof(class->mode));
01127 if (args)
01128 ast_copy_string(class->args, args, sizeof(class->args));
01129
01130 moh_register(class, reload);
01131 numclasses++;
01132 }
01133 }
01134 var = var->next;
01135 }
01136 var = ast_variable_browse(cfg, "moh_files");
01137 while (var) {
01138 if (!dep_warning) {
01139 ast_log(LOG_WARNING, "The old musiconhold.conf syntax has been deprecated! Please refer to the sample configuration for information on the new syntax.\n");
01140 dep_warning = 1;
01141 }
01142 if (!(get_mohbyname(var->name, 0))) {
01143 args = strchr(var->value, ',');
01144 if (args)
01145 *args++ = '\0';
01146 if (!(class = moh_class_malloc())) {
01147 break;
01148 }
01149
01150 ast_copy_string(class->name, var->name, sizeof(class->name));
01151 ast_copy_string(class->dir, var->value, sizeof(class->dir));
01152 strcpy(class->mode, "files");
01153 if (args)
01154 ast_copy_string(class->args, args, sizeof(class->args));
01155
01156 moh_register(class, reload);
01157 numclasses++;
01158 }
01159 var = var->next;
01160 }
01161
01162 ast_config_destroy(cfg);
01163
01164 return numclasses;
01165 }
01166
01167 static int ast_moh_destroy_one(struct mohclass *moh)
01168 {
01169 char buff[8192];
01170 int bytes, tbytes = 0, stime = 0, pid = 0;
01171
01172 if (moh) {
01173 if (moh->pid > 1) {
01174 ast_log(LOG_DEBUG, "killing %d!\n", moh->pid);
01175 stime = time(NULL) + 2;
01176 pid = moh->pid;
01177 moh->pid = 0;
01178
01179
01180
01181 kill(pid, SIGHUP);
01182 usleep(100000);
01183 kill(pid, SIGTERM);
01184 usleep(100000);
01185 kill(pid, SIGKILL);
01186 while ((ast_wait_for_input(moh->srcfd, 100) > 0) && (bytes = read(moh->srcfd, buff, 8192)) && time(NULL) < stime)
01187 tbytes = tbytes + bytes;
01188 ast_log(LOG_DEBUG, "mpg123 pid %d and child died after %d bytes read\n", pid, tbytes);
01189 close(moh->srcfd);
01190 }
01191 ast_moh_free_class(&moh);
01192 }
01193
01194 return 0;
01195 }
01196
01197 static void ast_moh_destroy(void)
01198 {
01199 struct mohclass *moh;
01200
01201 if (option_verbose > 1)
01202 ast_verbose(VERBOSE_PREFIX_2 "Destroying musiconhold processes\n");
01203
01204 AST_LIST_LOCK(&mohclasses);
01205 while ((moh = AST_LIST_REMOVE_HEAD(&mohclasses, list))) {
01206 ast_moh_destroy_one(moh);
01207 }
01208 AST_LIST_UNLOCK(&mohclasses);
01209 }
01210
01211 static int moh_cli(int fd, int argc, char *argv[])
01212 {
01213 reload();
01214 return 0;
01215 }
01216
01217 static int cli_files_show(int fd, int argc, char *argv[])
01218 {
01219 int i;
01220 struct mohclass *class;
01221
01222 AST_LIST_LOCK(&mohclasses);
01223 AST_LIST_TRAVERSE(&mohclasses, class, list) {
01224 if (!class->total_files)
01225 continue;
01226
01227 ast_cli(fd, "Class: %s\n", class->name);
01228 for (i = 0; i < class->total_files; i++)
01229 ast_cli(fd, "\tFile: %s\n", class->filearray[i]);
01230 }
01231 AST_LIST_UNLOCK(&mohclasses);
01232
01233 return 0;
01234 }
01235
01236 static int moh_classes_show(int fd, int argc, char *argv[])
01237 {
01238 struct mohclass *class;
01239
01240 AST_LIST_LOCK(&mohclasses);
01241 AST_LIST_TRAVERSE(&mohclasses, class, list) {
01242 ast_cli(fd, "Class: %s\n", class->name);
01243 ast_cli(fd, "\tMode: %s\n", S_OR(class->mode, "<none>"));
01244 ast_cli(fd, "\tDirectory: %s\n", S_OR(class->dir, "<none>"));
01245 ast_cli(fd, "\tUse Count: %d\n", class->inuse);
01246 if (ast_test_flag(class, MOH_CUSTOM))
01247 ast_cli(fd, "\tApplication: %s\n", S_OR(class->args, "<none>"));
01248 if (strcasecmp(class->mode, "files"))
01249 ast_cli(fd, "\tFormat: %s\n", ast_getformatname(class->format));
01250 }
01251 AST_LIST_UNLOCK(&mohclasses);
01252
01253 return 0;
01254 }
01255
01256 static struct ast_cli_entry cli_moh_classes_show_deprecated = {
01257 { "moh", "classes", "show"},
01258 moh_classes_show, NULL,
01259 NULL };
01260
01261 static struct ast_cli_entry cli_moh_files_show_deprecated = {
01262 { "moh", "files", "show"},
01263 cli_files_show, NULL,
01264 NULL };
01265
01266 static struct ast_cli_entry cli_moh[] = {
01267 { { "moh", "reload"},
01268 moh_cli, "Music On Hold",
01269 "Usage: moh reload\n Rereads configuration\n" },
01270
01271 { { "moh", "show", "classes"},
01272 moh_classes_show, "List MOH classes",
01273 "Usage: moh show classes\n Lists all MOH classes\n", NULL, &cli_moh_classes_show_deprecated },
01274
01275 { { "moh", "show", "files"},
01276 cli_files_show, "List MOH file-based classes",
01277 "Usage: moh show files\n Lists all loaded file-based MOH classes and their files\n", NULL, &cli_moh_files_show_deprecated },
01278 };
01279
01280 static int init_classes(int reload)
01281 {
01282 struct mohclass *moh;
01283
01284 if (!load_moh_classes(reload))
01285 return 0;
01286
01287 AST_LIST_LOCK(&mohclasses);
01288 AST_LIST_TRAVERSE_SAFE_BEGIN(&mohclasses, moh, list) {
01289 if (reload && moh->delete) {
01290 AST_LIST_REMOVE_CURRENT(&mohclasses, list);
01291 if (!moh->inuse)
01292 ast_moh_destroy_one(moh);
01293 }
01294 }
01295 AST_LIST_TRAVERSE_SAFE_END
01296 AST_LIST_UNLOCK(&mohclasses);
01297
01298 return 1;
01299 }
01300
01301 static int load_module(void)
01302 {
01303 int res;
01304
01305 res = ast_register_application(app0, moh0_exec, synopsis0, descrip0);
01306 ast_register_atexit(ast_moh_destroy);
01307 ast_cli_register_multiple(cli_moh, sizeof(cli_moh) / sizeof(struct ast_cli_entry));
01308 if (!res)
01309 res = ast_register_application(app1, moh1_exec, synopsis1, descrip1);
01310 if (!res)
01311 res = ast_register_application(app2, moh2_exec, synopsis2, descrip2);
01312 if (!res)
01313 res = ast_register_application(app3, moh3_exec, synopsis3, descrip3);
01314 if (!res)
01315 res = ast_register_application(app4, moh4_exec, synopsis4, descrip4);
01316
01317 if (!init_classes(0)) {
01318 ast_log(LOG_WARNING, "No music on hold classes configured, disabling music on hold.\n");
01319 } else {
01320 ast_install_music_functions(local_ast_moh_start, local_ast_moh_stop, local_ast_moh_cleanup);
01321 }
01322
01323 return 0;
01324 }
01325
01326 static int reload(void)
01327 {
01328 if (init_classes(1))
01329 ast_install_music_functions(local_ast_moh_start, local_ast_moh_stop, local_ast_moh_cleanup);
01330
01331 return 0;
01332 }
01333
01334 static int unload_module(void)
01335 {
01336 int res = 0;
01337 struct mohclass *class = NULL;
01338
01339 AST_LIST_LOCK(&mohclasses);
01340 AST_LIST_TRAVERSE(&mohclasses, class, list) {
01341 if (class->inuse > 0) {
01342 res = -1;
01343 break;
01344 }
01345 }
01346 AST_LIST_UNLOCK(&mohclasses);
01347 if (res < 0) {
01348 ast_log(LOG_WARNING, "Unable to unload res_musiconhold due to active MOH channels\n");
01349 return res;
01350 }
01351
01352 ast_uninstall_music_functions();
01353 ast_moh_destroy();
01354 res = ast_unregister_application(app0);
01355 res |= ast_unregister_application(app1);
01356 res |= ast_unregister_application(app2);
01357 res |= ast_unregister_application(app3);
01358 res |= ast_unregister_application(app4);
01359 ast_cli_unregister_multiple(cli_moh, sizeof(cli_moh) / sizeof(struct ast_cli_entry));
01360 return res;
01361 }
01362
01363 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Music On Hold Resource",
01364 .load = load_module,
01365 .unload = unload_module,
01366 .reload = reload,
01367 );