app_festival.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Connect to festival
  5. *
  6. * Copyright (C) 2002, Christos Ricudis
  7. *
  8. * Christos Ricudis <ricudis@itc.auth.gr>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <sys/types.h>
  14. #include <asterisk/file.h>
  15. #include <asterisk/logger.h>
  16. #include <asterisk/channel.h>
  17. #include <asterisk/pbx.h>
  18. #include <asterisk/module.h>
  19. #include <asterisk/md5.h>
  20. #include <asterisk/config.h>
  21. #include <asterisk/utils.h>
  22. #include <asterisk/lock.h>
  23. #include <stdlib.h>
  24. #include <unistd.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <sys/types.h>
  28. #include <sys/socket.h>
  29. #include <netdb.h>
  30. #include <netinet/in.h>
  31. #include <arpa/inet.h>
  32. #include <stdio.h>
  33. #include <signal.h>
  34. #include <stdlib.h>
  35. #include <unistd.h>
  36. #include <fcntl.h>
  37. #include <ctype.h>
  38. #define FESTIVAL_CONFIG "festival.conf"
  39. static char *tdesc = "Simple Festival Interface";
  40. static char *app = "Festival";
  41. static char *synopsis = "Say text to the user";
  42. static char *descrip =
  43. " Festival(text[|intkeys]): Connect to Festival, send the argument, get back the waveform,"
  44. "play it to the user, allowing any given interrupt keys to immediately terminate and return\n"
  45. "the value, or 'any' to allow any number back (useful in dialplan)\n";
  46. STANDARD_LOCAL_USER;
  47. LOCAL_USER_DECL;
  48. static char *socket_receive_file_to_buff(int fd,int *size)
  49. {
  50. /* Receive file (probably a waveform file) from socket using */
  51. /* Festival key stuff technique, but long winded I know, sorry */
  52. /* but will receive any file without closeing the stream or */
  53. /* using OOB data */
  54. static char *file_stuff_key = "ft_StUfF_key"; /* must == Festival's key */
  55. char *buff;
  56. int bufflen;
  57. int n,k,i;
  58. char c;
  59. bufflen = 1024;
  60. buff = (char *)malloc(bufflen);
  61. *size=0;
  62. for (k=0; file_stuff_key[k] != '\0';)
  63. {
  64. n = read(fd,&c,1);
  65. if (n==0) break; /* hit stream eof before end of file */
  66. if ((*size)+k+1 >= bufflen)
  67. { /* +1 so you can add a NULL if you want */
  68. bufflen += bufflen/4;
  69. buff = (char *)realloc(buff,bufflen);
  70. }
  71. if (file_stuff_key[k] == c)
  72. k++;
  73. else if ((c == 'X') && (file_stuff_key[k+1] == '\0'))
  74. { /* It looked like the key but wasn't */
  75. for (i=0; i < k; i++,(*size)++)
  76. buff[*size] = file_stuff_key[i];
  77. k=0;
  78. /* omit the stuffed 'X' */
  79. }
  80. else
  81. {
  82. for (i=0; i < k; i++,(*size)++)
  83. buff[*size] = file_stuff_key[i];
  84. k=0;
  85. buff[*size] = c;
  86. (*size)++;
  87. }
  88. }
  89. return buff;
  90. }
  91. static int send_waveform_to_fd(char *waveform, int length, int fd) {
  92. int res;
  93. int x;
  94. #ifdef __PPC__
  95. char c;
  96. #endif
  97. res = fork();
  98. if (res < 0)
  99. ast_log(LOG_WARNING, "Fork failed\n");
  100. if (res)
  101. return res;
  102. for (x=0;x<256;x++) {
  103. if (x != fd)
  104. close(x);
  105. }
  106. //IAS
  107. #ifdef __PPC__
  108. for( x=0; x<length; x+=2)
  109. {
  110. c = *(waveform+x+1);
  111. *(waveform+x+1)=*(waveform+x);
  112. *(waveform+x)=c;
  113. }
  114. #endif
  115. write(fd,waveform,length);
  116. close(fd);
  117. exit(0);
  118. }
  119. static int send_waveform_to_channel(struct ast_channel *chan, char *waveform, int length, char *intkeys) {
  120. int res=0;
  121. int fds[2];
  122. int ms = -1;
  123. int pid = -1;
  124. int needed = 0;
  125. int owriteformat;
  126. struct ast_frame *f;
  127. struct myframe {
  128. struct ast_frame f;
  129. char offset[AST_FRIENDLY_OFFSET];
  130. char frdata[2048];
  131. } myf;
  132. if (pipe(fds)) {
  133. ast_log(LOG_WARNING, "Unable to create pipe\n");
  134. return -1;
  135. }
  136. /* Answer if it's not already going */
  137. if (chan->_state != AST_STATE_UP)
  138. ast_answer(chan);
  139. ast_stopstream(chan);
  140. owriteformat = chan->writeformat;
  141. res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
  142. if (res < 0) {
  143. ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
  144. return -1;
  145. }
  146. res=send_waveform_to_fd(waveform,length,fds[1]);
  147. if (res >= 0) {
  148. pid = res;
  149. /* Order is important -- there's almost always going to be mp3... we want to prioritize the
  150. user */
  151. for (;;) {
  152. ms = 1000;
  153. res = ast_waitfor(chan, ms);
  154. if (res < 1) {
  155. res = -1;
  156. break;
  157. }
  158. f = ast_read(chan);
  159. if (!f) {
  160. ast_log(LOG_WARNING, "Null frame == hangup() detected\n");
  161. res = -1;
  162. break;
  163. }
  164. if (f->frametype == AST_FRAME_DTMF) {
  165. ast_log(LOG_DEBUG, "User pressed a key\n");
  166. if (intkeys && strchr(intkeys, f->subclass)) {
  167. res = f->subclass;
  168. ast_frfree(f);
  169. break;
  170. }
  171. }
  172. if (f->frametype == AST_FRAME_VOICE) {
  173. /* Treat as a generator */
  174. needed = f->samples * 2;
  175. if (needed > sizeof(myf.frdata)) {
  176. ast_log(LOG_WARNING, "Only able to deliver %d of %d requested samples\n",
  177. (int)sizeof(myf.frdata) / 2, needed/2);
  178. needed = sizeof(myf.frdata);
  179. }
  180. res = read(fds[0], myf.frdata, needed);
  181. if (res > 0) {
  182. myf.f.frametype = AST_FRAME_VOICE;
  183. myf.f.subclass = AST_FORMAT_SLINEAR;
  184. myf.f.datalen = res;
  185. myf.f.samples = res / 2;
  186. myf.f.mallocd = 0;
  187. myf.f.offset = AST_FRIENDLY_OFFSET;
  188. myf.f.src = __PRETTY_FUNCTION__;
  189. myf.f.data = myf.frdata;
  190. if (ast_write(chan, &myf.f) < 0) {
  191. res = -1;
  192. break;
  193. }
  194. if (res < needed) { // last frame
  195. ast_log(LOG_DEBUG, "Last frame\n");
  196. res=0;
  197. break;
  198. }
  199. } else {
  200. ast_log(LOG_DEBUG, "No more waveform\n");
  201. res = 0;
  202. }
  203. }
  204. ast_frfree(f);
  205. }
  206. }
  207. close(fds[0]);
  208. close(fds[1]);
  209. // if (pid > -1)
  210. // kill(pid, SIGKILL);
  211. if (!res && owriteformat)
  212. ast_set_write_format(chan, owriteformat);
  213. return res;
  214. }
  215. #define MAXLEN 180
  216. #define MAXFESTLEN 2048
  217. static int festival_exec(struct ast_channel *chan, void *vdata)
  218. {
  219. int usecache;
  220. int res=0;
  221. struct localuser *u;
  222. struct sockaddr_in serv_addr;
  223. struct hostent *serverhost;
  224. struct ast_hostent ahp;
  225. int fd;
  226. FILE *fs;
  227. char *host;
  228. char *cachedir;
  229. char *temp;
  230. char *festivalcommand;
  231. int port=1314;
  232. int n;
  233. char ack[4];
  234. char *waveform;
  235. int filesize;
  236. int wave;
  237. char bigstring[MAXFESTLEN];
  238. int i;
  239. struct MD5Context md5ctx;
  240. unsigned char MD5Res[16];
  241. char MD5Hex[33] = "";
  242. char koko[4] = "";
  243. char cachefile[MAXFESTLEN]="";
  244. int readcache=0;
  245. int writecache=0;
  246. int strln;
  247. int fdesc = -1;
  248. char buffer[16384];
  249. int seekpos = 0;
  250. char data[256] = "";
  251. char *intstr;
  252. struct ast_config *cfg;
  253. cfg = ast_load(FESTIVAL_CONFIG);
  254. if (!cfg) {
  255. ast_log(LOG_WARNING, "No such configuration file %s\n", FESTIVAL_CONFIG);
  256. return -1;
  257. }
  258. if (!(host = ast_variable_retrieve(cfg, "general", "host"))) {
  259. host = "localhost";
  260. }
  261. if (!(temp = ast_variable_retrieve(cfg, "general", "port"))) {
  262. port = 1314;
  263. } else {
  264. port = atoi(temp);
  265. }
  266. if (!(temp = ast_variable_retrieve(cfg, "general", "usecache"))) {
  267. usecache=0;
  268. } else {
  269. usecache = ast_true(temp);
  270. }
  271. if (!(cachedir = ast_variable_retrieve(cfg, "general", "cachedir"))) {
  272. cachedir = "/tmp/";
  273. }
  274. if (!(festivalcommand = ast_variable_retrieve(cfg, "general", "festivalcommand"))) {
  275. festivalcommand = "(tts_textasterisk \"%s\" 'file)(quit)\n";
  276. }
  277. if (!vdata || ast_strlen_zero(vdata)) {
  278. ast_log(LOG_WARNING, "festival requires an argument (text)\n");
  279. ast_destroy(cfg);
  280. return -1;
  281. }
  282. strncpy(data, vdata, sizeof(data) - 1);
  283. if ((intstr = strchr(data, '|'))) {
  284. *intstr = '\0';
  285. intstr++;
  286. if (!strcasecmp(intstr, "any"))
  287. intstr = AST_DIGIT_ANY;
  288. }
  289. LOCAL_USER_ADD(u);
  290. ast_log(LOG_DEBUG, "Text passed to festival server : %s\n",(char *)data);
  291. /* Connect to local festival server */
  292. fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  293. if (fd < 0) {
  294. ast_log(LOG_WARNING,"festival_client: can't get socket\n");
  295. ast_destroy(cfg);
  296. return -1;
  297. }
  298. memset(&serv_addr, 0, sizeof(serv_addr));
  299. if ((serv_addr.sin_addr.s_addr = inet_addr(host)) == -1) {
  300. /* its a name rather than an ipnum */
  301. serverhost = ast_gethostbyname(host, &ahp);
  302. if (serverhost == (struct hostent *)0) {
  303. ast_log(LOG_WARNING,"festival_client: gethostbyname failed\n");
  304. ast_destroy(cfg);
  305. return -1;
  306. }
  307. memmove(&serv_addr.sin_addr,serverhost->h_addr, serverhost->h_length);
  308. }
  309. serv_addr.sin_family = AF_INET;
  310. serv_addr.sin_port = htons(port);
  311. if (connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) != 0) {
  312. ast_log(LOG_WARNING,"festival_client: connect to server failed\n");
  313. ast_destroy(cfg);
  314. return -1;
  315. }
  316. /* Compute MD5 sum of string */
  317. MD5Init(&md5ctx);
  318. MD5Update(&md5ctx,(unsigned char const *)data,strlen(data));
  319. MD5Final(MD5Res,&md5ctx);
  320. MD5Hex[0] = '\0';
  321. /* Convert to HEX and look if there is any matching file in the cache
  322. directory */
  323. for (i=0;i<16;i++) {
  324. snprintf(koko, sizeof(koko), "%X",MD5Res[i]);
  325. strncat(MD5Hex, koko, sizeof(MD5Hex) - strlen(MD5Hex) - 1);
  326. }
  327. readcache=0;
  328. writecache=0;
  329. if (strlen(cachedir)+strlen(MD5Hex)+1<=MAXFESTLEN && (usecache==-1)) {
  330. snprintf(cachefile, sizeof(cachefile), "%s/%s", cachedir, MD5Hex);
  331. fdesc=open(cachefile,O_RDWR);
  332. if (fdesc==-1) {
  333. fdesc=open(cachefile,O_CREAT|O_RDWR,0777);
  334. if (fdesc!=-1) {
  335. writecache=1;
  336. strln=strlen((char *)data);
  337. ast_log(LOG_DEBUG,"line length : %d\n",strln);
  338. write(fdesc,&strln,sizeof(int));
  339. write(fdesc,data,strln);
  340. seekpos=lseek(fdesc,0,SEEK_CUR);
  341. ast_log(LOG_DEBUG,"Seek position : %d\n",seekpos);
  342. }
  343. } else {
  344. read(fdesc,&strln,sizeof(int));
  345. ast_log(LOG_DEBUG,"Cache file exists, strln=%d, strlen=%d\n",strln,(int)strlen((char *)data));
  346. if (strlen((char *)data)==strln) {
  347. ast_log(LOG_DEBUG,"Size OK\n");
  348. read(fdesc,&bigstring,strln);
  349. bigstring[strln] = 0;
  350. if (strcmp(bigstring,data)==0) {
  351. readcache=1;
  352. } else {
  353. ast_log(LOG_WARNING,"Strings do not match\n");
  354. }
  355. } else {
  356. ast_log(LOG_WARNING,"Size mismatch\n");
  357. }
  358. }
  359. }
  360. if (readcache==1) {
  361. close(fd);
  362. fd=fdesc;
  363. ast_log(LOG_DEBUG,"Reading from cache...\n");
  364. } else {
  365. ast_log(LOG_DEBUG,"Passing text to festival...\n");
  366. fs=fdopen(dup(fd),"wb");
  367. fprintf(fs,festivalcommand,(char *)data);
  368. fflush(fs);
  369. fclose(fs);
  370. }
  371. /* Write to cache and then pass it down */
  372. if (writecache==1) {
  373. ast_log(LOG_DEBUG,"Writing result to cache...\n");
  374. while ((strln=read(fd,buffer,16384))!=0) {
  375. write(fdesc,buffer,strln);
  376. }
  377. close(fd);
  378. close(fdesc);
  379. fd=open(cachefile,O_RDWR);
  380. lseek(fd,seekpos,SEEK_SET);
  381. }
  382. ast_log(LOG_DEBUG,"Passing data to channel...\n");
  383. /* Read back info from server */
  384. /* This assumes only one waveform will come back, also LP is unlikely */
  385. wave = 0;
  386. do {
  387. for (n=0; n < 3; )
  388. n += read(fd,ack+n,3-n);
  389. ack[3] = '\0';
  390. if (strcmp(ack,"WV\n") == 0) { /* receive a waveform */
  391. ast_log(LOG_DEBUG,"Festival WV command\n");
  392. waveform = socket_receive_file_to_buff(fd,&filesize);
  393. res = send_waveform_to_channel(chan,waveform,filesize, intstr);
  394. free(waveform);
  395. break;
  396. }
  397. else if (strcmp(ack,"LP\n") == 0) { /* receive an s-expr */
  398. ast_log(LOG_DEBUG,"Festival LP command\n");
  399. waveform = socket_receive_file_to_buff(fd,&filesize);
  400. waveform[filesize]='\0';
  401. ast_log(LOG_WARNING,"Festival returned LP : %s\n",waveform);
  402. free(waveform);
  403. } else if (strcmp(ack,"ER\n") == 0) { /* server got an error */
  404. ast_log(LOG_WARNING,"Festival returned ER\n");
  405. res=-1;
  406. break;
  407. }
  408. } while (strcmp(ack,"OK\n") != 0);
  409. close(fd);
  410. ast_destroy(cfg);
  411. LOCAL_USER_REMOVE(u);
  412. return res;
  413. }
  414. int unload_module(void)
  415. {
  416. STANDARD_HANGUP_LOCALUSERS;
  417. return ast_unregister_application(app);
  418. }
  419. int load_module(void)
  420. {
  421. return ast_register_application(app, festival_exec, synopsis, descrip);
  422. }
  423. char *description(void)
  424. {
  425. return tdesc;
  426. }
  427. int usecount(void)
  428. {
  429. int res;
  430. STANDARD_USECOUNT(res);
  431. return res;
  432. }
  433. char *key()
  434. {
  435. return ASTERISK_GPL_KEY;
  436. }