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 #include <string.h>
00027 #include <stdio.h>
00028 #include <signal.h>
00029 #include <stdlib.h>
00030 #include <unistd.h>
00031 #include <fcntl.h>
00032 #include <sys/time.h>
00033 #include <sys/socket.h>
00034
00035 #include "asterisk.h"
00036
00037 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 24019 $")
00038
00039 #include "asterisk/lock.h"
00040 #include "asterisk/file.h"
00041 #include "asterisk/logger.h"
00042 #include "asterisk/channel.h"
00043 #include "asterisk/frame.h"
00044 #include "asterisk/pbx.h"
00045 #include "asterisk/module.h"
00046 #include "asterisk/translate.h"
00047 #include "asterisk/options.h"
00048
00049 #define LOCAL_NBSCAT "/usr/local/bin/nbscat8k"
00050 #define NBSCAT "/usr/bin/nbscat8k"
00051
00052 #ifndef AF_LOCAL
00053 #define AF_LOCAL AF_UNIX
00054 #endif
00055
00056 static char *tdesc = "Silly NBS Stream Application";
00057
00058 static char *app = "NBScat";
00059
00060 static char *synopsis = "Play an NBS local stream";
00061
00062 static char *descrip =
00063 " NBScat: Executes nbscat to listen to the local NBS stream.\n"
00064 "User can exit by pressing any key\n.";
00065
00066 STANDARD_LOCAL_USER;
00067
00068 LOCAL_USER_DECL;
00069
00070 static int NBScatplay(int fd)
00071 {
00072 int res;
00073 int x;
00074 res = fork();
00075 if (res < 0)
00076 ast_log(LOG_WARNING, "Fork failed\n");
00077 if (res)
00078 return res;
00079 if (option_highpriority)
00080 ast_set_priority(0);
00081
00082 dup2(fd, STDOUT_FILENO);
00083 for (x=0;x<256;x++) {
00084 if (x != STDOUT_FILENO)
00085 close(x);
00086 }
00087
00088 execl(NBSCAT, "nbscat8k", "-d", (char *)NULL);
00089 execl(LOCAL_NBSCAT, "nbscat8k", "-d", (char *)NULL);
00090 ast_log(LOG_WARNING, "Execute of nbscat8k failed\n");
00091 return -1;
00092 }
00093
00094 static int timed_read(int fd, void *data, int datalen)
00095 {
00096 int res;
00097 struct pollfd fds[1];
00098 fds[0].fd = fd;
00099 fds[0].events = POLLIN;
00100 res = poll(fds, 1, 2000);
00101 if (res < 1) {
00102 ast_log(LOG_NOTICE, "Selected timed out/errored out with %d\n", res);
00103 return -1;
00104 }
00105 return read(fd, data, datalen);
00106
00107 }
00108
00109 static int NBScat_exec(struct ast_channel *chan, void *data)
00110 {
00111 int res=0;
00112 struct localuser *u;
00113 int fds[2];
00114 int ms = -1;
00115 int pid = -1;
00116 int owriteformat;
00117 struct timeval next;
00118 struct ast_frame *f;
00119 struct myframe {
00120 struct ast_frame f;
00121 char offset[AST_FRIENDLY_OFFSET];
00122 short frdata[160];
00123 } myf;
00124
00125 LOCAL_USER_ADD(u);
00126
00127 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds)) {
00128 ast_log(LOG_WARNING, "Unable to create socketpair\n");
00129 LOCAL_USER_REMOVE(u);
00130 return -1;
00131 }
00132
00133 ast_stopstream(chan);
00134
00135 owriteformat = chan->writeformat;
00136 res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
00137 if (res < 0) {
00138 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
00139 LOCAL_USER_REMOVE(u);
00140 return -1;
00141 }
00142
00143 res = NBScatplay(fds[1]);
00144
00145 next = ast_tvnow();
00146 next.tv_sec += 1;
00147 if (res >= 0) {
00148 pid = res;
00149
00150
00151 for (;;) {
00152 ms = ast_tvdiff_ms(next, ast_tvnow());
00153 if (ms <= 0) {
00154 res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata));
00155 if (res > 0) {
00156 myf.f.frametype = AST_FRAME_VOICE;
00157 myf.f.subclass = AST_FORMAT_SLINEAR;
00158 myf.f.datalen = res;
00159 myf.f.samples = res / 2;
00160 myf.f.mallocd = 0;
00161 myf.f.offset = AST_FRIENDLY_OFFSET;
00162 myf.f.src = __PRETTY_FUNCTION__;
00163 myf.f.delivery.tv_sec = 0;
00164 myf.f.delivery.tv_usec = 0;
00165 myf.f.data = myf.frdata;
00166 if (ast_write(chan, &myf.f) < 0) {
00167 res = -1;
00168 break;
00169 }
00170 } else {
00171 ast_log(LOG_DEBUG, "No more mp3\n");
00172 res = 0;
00173 break;
00174 }
00175 next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
00176 } else {
00177 ms = ast_waitfor(chan, ms);
00178 if (ms < 0) {
00179 ast_log(LOG_DEBUG, "Hangup detected\n");
00180 res = -1;
00181 break;
00182 }
00183 if (ms) {
00184 f = ast_read(chan);
00185 if (!f) {
00186 ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
00187 res = -1;
00188 break;
00189 }
00190 if (f->frametype == AST_FRAME_DTMF) {
00191 ast_log(LOG_DEBUG, "User pressed a key\n");
00192 ast_frfree(f);
00193 res = 0;
00194 break;
00195 }
00196 ast_frfree(f);
00197 }
00198 }
00199 }
00200 }
00201 close(fds[0]);
00202 close(fds[1]);
00203
00204 if (pid > -1)
00205 kill(pid, SIGKILL);
00206 if (!res && owriteformat)
00207 ast_set_write_format(chan, owriteformat);
00208
00209 LOCAL_USER_REMOVE(u);
00210
00211 return res;
00212 }
00213
00214 int unload_module(void)
00215 {
00216 int res;
00217
00218 res = ast_unregister_application(app);
00219
00220 STANDARD_HANGUP_LOCALUSERS;
00221
00222 return res;
00223 }
00224
00225 int load_module(void)
00226 {
00227 return ast_register_application(app, NBScat_exec, synopsis, descrip);
00228 }
00229
00230 char *description(void)
00231 {
00232 return tdesc;
00233 }
00234
00235 int usecount(void)
00236 {
00237 int res;
00238 STANDARD_USECOUNT(res);
00239 return res;
00240 }
00241
00242 char *key()
00243 {
00244 return ASTERISK_GPL_KEY;
00245 }