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