file.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Generic File Format Support.
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  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/frame.h>
  15. #include <asterisk/file.h>
  16. #include <asterisk/cli.h>
  17. #include <asterisk/logger.h>
  18. #include <asterisk/channel.h>
  19. #include <asterisk/sched.h>
  20. #include <asterisk/options.h>
  21. #include <asterisk/translate.h>
  22. #include <asterisk/utils.h>
  23. #include <asterisk/lock.h>
  24. #include <asterisk/app.h>
  25. #include <errno.h>
  26. #include <unistd.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <stdio.h>
  30. #include <fcntl.h>
  31. #include <dirent.h>
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include "asterisk.h"
  35. #include "astconf.h"
  36. struct ast_format {
  37. /* Name of format */
  38. char name[80];
  39. /* Extensions (separated by | if more than one)
  40. this format can read. First is assumed for writing (e.g. .mp3) */
  41. char exts[80];
  42. /* Format of frames it uses/provides (one only) */
  43. int format;
  44. /* Open an input stream, and start playback */
  45. struct ast_filestream * (*open)(int fd);
  46. /* Open an output stream, of a given file descriptor and comment it appropriately if applicable */
  47. struct ast_filestream * (*rewrite)(int fd, char *comment);
  48. /* Write a frame to a channel */
  49. int (*write)(struct ast_filestream *, struct ast_frame *);
  50. /* seek num samples into file, whence(think normal seek) */
  51. int (*seek)(struct ast_filestream *, long offset, int whence);
  52. /* trunc file to current position */
  53. int (*trunc)(struct ast_filestream *fs);
  54. /* tell current position */
  55. long (*tell)(struct ast_filestream *fs);
  56. /* Read the next frame from the filestream (if available) and report when to get next one
  57. (in samples) */
  58. struct ast_frame * (*read)(struct ast_filestream *, int *whennext);
  59. /* Close file, and destroy filestream structure */
  60. void (*close)(struct ast_filestream *);
  61. /* Retrieve file comment */
  62. char * (*getcomment)(struct ast_filestream *);
  63. /* Link */
  64. struct ast_format *next;
  65. };
  66. struct ast_filestream {
  67. /* Everybody reserves a block of AST_RESERVED_POINTERS pointers for us */
  68. struct ast_format *fmt;
  69. int flags;
  70. mode_t mode;
  71. char *filename;
  72. char *realfilename;
  73. /* Video file stream */
  74. struct ast_filestream *vfs;
  75. /* Transparently translate from another format -- just once */
  76. struct ast_trans_pvt *trans;
  77. struct ast_tranlator_pvt *tr;
  78. int lastwriteformat;
  79. int lasttimeout;
  80. struct ast_channel *owner;
  81. };
  82. AST_MUTEX_DEFINE_STATIC(formatlock);
  83. static struct ast_format *formats = NULL;
  84. int ast_format_register(char *name, char *exts, int format,
  85. struct ast_filestream * (*open)(int fd),
  86. struct ast_filestream * (*rewrite)(int fd, char *comment),
  87. int (*write)(struct ast_filestream *, struct ast_frame *),
  88. int (*seek)(struct ast_filestream *, long sample_offset, int whence),
  89. int (*trunc)(struct ast_filestream *),
  90. long (*tell)(struct ast_filestream *),
  91. struct ast_frame * (*read)(struct ast_filestream *, int *whennext),
  92. void (*close)(struct ast_filestream *),
  93. char * (*getcomment)(struct ast_filestream *))
  94. {
  95. struct ast_format *tmp;
  96. if (ast_mutex_lock(&formatlock)) {
  97. ast_log(LOG_WARNING, "Unable to lock format list\n");
  98. return -1;
  99. }
  100. tmp = formats;
  101. while(tmp) {
  102. if (!strcasecmp(name, tmp->name)) {
  103. ast_mutex_unlock(&formatlock);
  104. ast_log(LOG_WARNING, "Tried to register '%s' format, already registered\n", name);
  105. return -1;
  106. }
  107. tmp = tmp->next;
  108. }
  109. tmp = malloc(sizeof(struct ast_format));
  110. if (!tmp) {
  111. ast_log(LOG_WARNING, "Out of memory\n");
  112. ast_mutex_unlock(&formatlock);
  113. return -1;
  114. }
  115. strncpy(tmp->name, name, sizeof(tmp->name)-1);
  116. strncpy(tmp->exts, exts, sizeof(tmp->exts)-1);
  117. tmp->open = open;
  118. tmp->rewrite = rewrite;
  119. tmp->read = read;
  120. tmp->write = write;
  121. tmp->seek = seek;
  122. tmp->trunc = trunc;
  123. tmp->tell = tell;
  124. tmp->close = close;
  125. tmp->format = format;
  126. tmp->getcomment = getcomment;
  127. tmp->next = formats;
  128. formats = tmp;
  129. ast_mutex_unlock(&formatlock);
  130. if (option_verbose > 1)
  131. ast_verbose( VERBOSE_PREFIX_2 "Registered file format %s, extension(s) %s\n", name, exts);
  132. return 0;
  133. }
  134. int ast_format_unregister(char *name)
  135. {
  136. struct ast_format *tmp, *tmpl = NULL;
  137. if (ast_mutex_lock(&formatlock)) {
  138. ast_log(LOG_WARNING, "Unable to lock format list\n");
  139. return -1;
  140. }
  141. tmp = formats;
  142. while(tmp) {
  143. if (!strcasecmp(name, tmp->name)) {
  144. if (tmpl)
  145. tmpl->next = tmp->next;
  146. else
  147. formats = tmp->next;
  148. free(tmp);
  149. ast_mutex_unlock(&formatlock);
  150. if (option_verbose > 1)
  151. ast_verbose( VERBOSE_PREFIX_2 "Unregistered format %s\n", name);
  152. return 0;
  153. }
  154. tmpl = tmp;
  155. tmp = tmp->next;
  156. }
  157. ast_log(LOG_WARNING, "Tried to unregister format %s, already unregistered\n", name);
  158. return -1;
  159. }
  160. int ast_stopstream(struct ast_channel *tmp)
  161. {
  162. /* Stop a running stream if there is one */
  163. if (tmp->vstream)
  164. ast_closestream(tmp->vstream);
  165. if (tmp->stream) {
  166. ast_closestream(tmp->stream);
  167. if (tmp->oldwriteformat && ast_set_write_format(tmp, tmp->oldwriteformat))
  168. ast_log(LOG_WARNING, "Unable to restore format back to %d\n", tmp->oldwriteformat);
  169. }
  170. return 0;
  171. }
  172. int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
  173. {
  174. struct ast_frame *trf;
  175. int res = -1;
  176. int alt=0;
  177. if (f->frametype == AST_FRAME_VIDEO) {
  178. if (fs->fmt->format < AST_FORMAT_MAX_AUDIO) {
  179. /* This is the audio portion. Call the video one... */
  180. if (!fs->vfs && fs->filename) {
  181. /* XXX Support other video formats XXX */
  182. char *type = "h263";
  183. fs->vfs = ast_writefile(fs->filename, type, NULL, fs->flags, 0, fs->mode);
  184. ast_log(LOG_DEBUG, "Opened video output file\n");
  185. }
  186. if (fs->vfs)
  187. return ast_writestream(fs->vfs, f);
  188. /* Ignore */
  189. return 0;
  190. } else {
  191. /* Might / might not have mark set */
  192. alt = 1;
  193. }
  194. } else if (f->frametype != AST_FRAME_VOICE) {
  195. ast_log(LOG_WARNING, "Tried to write non-voice frame\n");
  196. return -1;
  197. }
  198. if (((fs->fmt->format | alt) & f->subclass) == f->subclass) {
  199. res = fs->fmt->write(fs, f);
  200. if (res < 0)
  201. ast_log(LOG_WARNING, "Natural write failed\n");
  202. if (res > 0)
  203. ast_log(LOG_WARNING, "Huh??\n");
  204. return res;
  205. } else {
  206. /* XXX If they try to send us a type of frame that isn't the normal frame, and isn't
  207. the one we've setup a translator for, we do the "wrong thing" XXX */
  208. if (fs->trans && (f->subclass != fs->lastwriteformat)) {
  209. ast_translator_free_path(fs->trans);
  210. fs->trans = NULL;
  211. }
  212. if (!fs->trans)
  213. fs->trans = ast_translator_build_path(fs->fmt->format, f->subclass);
  214. if (!fs->trans)
  215. ast_log(LOG_WARNING, "Unable to translate to format %s, source format %s\n", fs->fmt->name, ast_getformatname(f->subclass));
  216. else {
  217. fs->lastwriteformat = f->subclass;
  218. res = 0;
  219. /* Get the translated frame but don't consume the original in case they're using it on another stream */
  220. trf = ast_translate(fs->trans, f, 0);
  221. if (trf) {
  222. res = fs->fmt->write(fs, trf);
  223. if (res)
  224. ast_log(LOG_WARNING, "Translated frame write failed\n");
  225. } else
  226. res = 0;
  227. }
  228. return res;
  229. }
  230. }
  231. static int copy(char *infile, char *outfile)
  232. {
  233. int ifd;
  234. int ofd;
  235. int res;
  236. int len;
  237. char buf[4096];
  238. if ((ifd = open(infile, O_RDONLY)) < 0) {
  239. ast_log(LOG_WARNING, "Unable to open %s in read-only mode\n", infile);
  240. return -1;
  241. }
  242. if ((ofd = open(outfile, O_WRONLY | O_TRUNC | O_CREAT, 0600)) < 0) {
  243. ast_log(LOG_WARNING, "Unable to open %s in write-only mode\n", outfile);
  244. close(ifd);
  245. return -1;
  246. }
  247. do {
  248. len = read(ifd, buf, sizeof(buf));
  249. if (len < 0) {
  250. ast_log(LOG_WARNING, "Read failed on %s: %s\n", infile, strerror(errno));
  251. close(ifd);
  252. close(ofd);
  253. unlink(outfile);
  254. }
  255. if (len) {
  256. res = write(ofd, buf, len);
  257. if (res != len) {
  258. ast_log(LOG_WARNING, "Write failed on %s (%d of %d): %s\n", outfile, res, len, strerror(errno));
  259. close(ifd);
  260. close(ofd);
  261. unlink(outfile);
  262. }
  263. }
  264. } while(len);
  265. close(ifd);
  266. close(ofd);
  267. return 0;
  268. }
  269. static char *build_filename(char *filename, char *ext)
  270. {
  271. char *fn;
  272. int fnsize = 0;
  273. char tmp[AST_CONFIG_MAX_PATH]="";
  274. snprintf(tmp, sizeof(tmp), "%s/%s", ast_config_AST_VAR_DIR, "sounds");
  275. fnsize = strlen(tmp) + strlen(filename) + strlen(ext) + 10;
  276. fn = malloc(fnsize);
  277. if (fn) {
  278. if (filename[0] == '/')
  279. snprintf(fn, fnsize, "%s.%s", filename, ext);
  280. else
  281. snprintf(fn, fnsize, "%s/%s.%s", tmp, filename, ext);
  282. }
  283. return fn;
  284. }
  285. static int exts_compare(char *exts, char *type)
  286. {
  287. char *stringp = NULL, *ext;
  288. char tmp[256];
  289. strncpy(tmp, exts, sizeof(tmp) - 1);
  290. stringp = tmp;
  291. while ((ext = strsep(&stringp, "|"))) {
  292. if (!strcmp(ext, type)) {
  293. return 1;
  294. }
  295. }
  296. return 0;
  297. }
  298. #define ACTION_EXISTS 1
  299. #define ACTION_DELETE 2
  300. #define ACTION_RENAME 3
  301. #define ACTION_OPEN 4
  302. #define ACTION_COPY 5
  303. static int ast_filehelper(char *filename, char *filename2, char *fmt, int action)
  304. {
  305. struct stat st;
  306. struct ast_format *f;
  307. struct ast_filestream *s;
  308. int res=0, ret = 0;
  309. char *ext=NULL, *exts, *fn, *nfn;
  310. struct ast_channel *chan = (struct ast_channel *)filename2;
  311. /* Start with negative response */
  312. if (action == ACTION_EXISTS)
  313. res = 0;
  314. else
  315. res = -1;
  316. if (action == ACTION_OPEN)
  317. ret = -1;
  318. /* Check for a specific format */
  319. if (ast_mutex_lock(&formatlock)) {
  320. ast_log(LOG_WARNING, "Unable to lock format list\n");
  321. if (action == ACTION_EXISTS)
  322. return 0;
  323. else
  324. return -1;
  325. }
  326. f = formats;
  327. while(f) {
  328. if (!fmt || exts_compare(f->exts, fmt)) {
  329. char *stringp=NULL;
  330. exts = strdup(f->exts);
  331. /* Try each kind of extension */
  332. stringp=exts;
  333. ext = strsep(&stringp, "|");
  334. if (!strcmp(ext,"wav49")) {
  335. ext = "WAV";
  336. }
  337. do {
  338. fn = build_filename(filename, ext);
  339. if (fn) {
  340. res = stat(fn, &st);
  341. if (!res) {
  342. switch(action) {
  343. case ACTION_EXISTS:
  344. ret |= f->format;
  345. break;
  346. case ACTION_DELETE:
  347. res = unlink(fn);
  348. if (res)
  349. ast_log(LOG_WARNING, "unlink(%s) failed: %s\n", fn, strerror(errno));
  350. break;
  351. case ACTION_RENAME:
  352. nfn = build_filename(filename2, ext);
  353. if (nfn) {
  354. res = rename(fn, nfn);
  355. if (res)
  356. ast_log(LOG_WARNING, "rename(%s,%s) failed: %s\n", fn, nfn, strerror(errno));
  357. free(nfn);
  358. } else
  359. ast_log(LOG_WARNING, "Out of memory\n");
  360. break;
  361. case ACTION_COPY:
  362. nfn = build_filename(filename2, ext);
  363. if (nfn) {
  364. res = copy(fn, nfn);
  365. if (res)
  366. ast_log(LOG_WARNING, "copy(%s,%s) failed: %s\n", fn, nfn, strerror(errno));
  367. free(nfn);
  368. } else
  369. ast_log(LOG_WARNING, "Out of memory\n");
  370. break;
  371. case ACTION_OPEN:
  372. if ((ret < 0) && ((chan->writeformat & f->format) ||
  373. ((f->format >= AST_FORMAT_MAX_AUDIO) && fmt))) {
  374. ret = open(fn, O_RDONLY);
  375. if (ret >= 0) {
  376. s = f->open(ret);
  377. if (s) {
  378. s->lasttimeout = -1;
  379. s->fmt = f;
  380. s->trans = NULL;
  381. s->filename = NULL;
  382. if (s->fmt->format < AST_FORMAT_MAX_AUDIO)
  383. chan->stream = s;
  384. else
  385. chan->vstream = s;
  386. } else {
  387. close(ret);
  388. ast_log(LOG_WARNING, "Unable to open fd on %s\n", fn);
  389. }
  390. } else
  391. ast_log(LOG_WARNING, "Couldn't open file %s\n", fn);
  392. }
  393. break;
  394. default:
  395. ast_log(LOG_WARNING, "Unknown helper %d\n", action);
  396. }
  397. /* Conveniently this logic is the same for all */
  398. if (res)
  399. break;
  400. }
  401. free(fn);
  402. }
  403. ext = strsep(&stringp, "|");
  404. } while(ext);
  405. free(exts);
  406. }
  407. f = f->next;
  408. }
  409. ast_mutex_unlock(&formatlock);
  410. if ((action == ACTION_EXISTS) || (action == ACTION_OPEN))
  411. res = ret ? ret : -1;
  412. return res;
  413. }
  414. struct ast_filestream *ast_openstream(struct ast_channel *chan, char *filename, char *preflang)
  415. {
  416. /* This is a fairly complex routine. Essentially we should do
  417. the following:
  418. 1) Find which file handlers produce our type of format.
  419. 2) Look for a filename which it can handle.
  420. 3) If we find one, then great.
  421. 4) If not, see what files are there
  422. 5) See what we can actually support
  423. 6) Choose the one with the least costly translator path and
  424. set it up.
  425. */
  426. int fd = -1;
  427. int fmts = -1;
  428. char filename2[256]="";
  429. char filename3[256]="";
  430. char *endpart;
  431. int res;
  432. ast_stopstream(chan);
  433. /* do this first, otherwise we detect the wrong writeformat */
  434. if (chan->generator)
  435. ast_deactivate_generator(chan);
  436. if (preflang && !ast_strlen_zero(preflang)) {
  437. strncpy(filename3, filename, sizeof(filename3) - 1);
  438. endpart = strrchr(filename3, '/');
  439. if (endpart) {
  440. *endpart = '\0';
  441. endpart++;
  442. snprintf(filename2, sizeof(filename2), "%s/%s/%s", filename3, preflang, endpart);
  443. } else
  444. snprintf(filename2, sizeof(filename2), "%s/%s", preflang, filename);
  445. fmts = ast_fileexists(filename2, NULL, NULL);
  446. }
  447. if (fmts < 1) {
  448. strncpy(filename2, filename, sizeof(filename2)-1);
  449. fmts = ast_fileexists(filename2, NULL, NULL);
  450. }
  451. if (fmts < 1) {
  452. ast_log(LOG_WARNING, "File %s does not exist in any format\n", filename);
  453. return NULL;
  454. }
  455. chan->oldwriteformat = chan->writeformat;
  456. /* Set the channel to a format we can work with */
  457. res = ast_set_write_format(chan, fmts);
  458. fd = ast_filehelper(filename2, (char *)chan, NULL, ACTION_OPEN);
  459. if (fd >= 0)
  460. return chan->stream;
  461. return NULL;
  462. }
  463. struct ast_filestream *ast_openvstream(struct ast_channel *chan, char *filename, char *preflang)
  464. {
  465. /* This is a fairly complex routine. Essentially we should do
  466. the following:
  467. 1) Find which file handlers produce our type of format.
  468. 2) Look for a filename which it can handle.
  469. 3) If we find one, then great.
  470. 4) If not, see what files are there
  471. 5) See what we can actually support
  472. 6) Choose the one with the least costly translator path and
  473. set it up.
  474. */
  475. int fd = -1;
  476. int fmts = -1;
  477. char filename2[256];
  478. char lang2[MAX_LANGUAGE];
  479. /* XXX H.263 only XXX */
  480. char *fmt = "h263";
  481. if (preflang && !ast_strlen_zero(preflang)) {
  482. snprintf(filename2, sizeof(filename2), "%s/%s", preflang, filename);
  483. fmts = ast_fileexists(filename2, fmt, NULL);
  484. if (fmts < 1) {
  485. strncpy(lang2, preflang, sizeof(lang2)-1);
  486. snprintf(filename2, sizeof(filename2), "%s/%s", lang2, filename);
  487. fmts = ast_fileexists(filename2, fmt, NULL);
  488. }
  489. }
  490. if (fmts < 1) {
  491. strncpy(filename2, filename, sizeof(filename2)-1);
  492. fmts = ast_fileexists(filename2, fmt, NULL);
  493. }
  494. if (fmts < 1) {
  495. return NULL;
  496. }
  497. fd = ast_filehelper(filename2, (char *)chan, fmt, ACTION_OPEN);
  498. if (fd >= 0)
  499. return chan->vstream;
  500. ast_log(LOG_WARNING, "File %s has video but couldn't be opened\n", filename);
  501. return NULL;
  502. }
  503. struct ast_frame *ast_readframe(struct ast_filestream *s)
  504. {
  505. struct ast_frame *f = NULL;
  506. int whennext = 0;
  507. if (s && s->fmt)
  508. f = s->fmt->read(s, &whennext);
  509. return f;
  510. }
  511. static int ast_readaudio_callback(void *data)
  512. {
  513. struct ast_filestream *s = data;
  514. struct ast_frame *fr;
  515. int whennext = 0;
  516. while(!whennext) {
  517. fr = s->fmt->read(s, &whennext);
  518. if (fr) {
  519. if (ast_write(s->owner, fr)) {
  520. ast_log(LOG_WARNING, "Failed to write frame\n");
  521. s->owner->streamid = -1;
  522. #ifdef ZAPTEL_OPTIMIZATIONS
  523. ast_settimeout(s->owner, 0, NULL, NULL);
  524. #endif
  525. return 0;
  526. }
  527. } else {
  528. /* Stream has finished */
  529. s->owner->streamid = -1;
  530. #ifdef ZAPTEL_OPTIMIZATIONS
  531. ast_settimeout(s->owner, 0, NULL, NULL);
  532. #endif
  533. return 0;
  534. }
  535. }
  536. if (whennext != s->lasttimeout) {
  537. #ifdef ZAPTEL_OPTIMIZATIONS
  538. if (s->owner->timingfd > -1)
  539. ast_settimeout(s->owner, whennext, ast_readaudio_callback, s);
  540. else
  541. #endif
  542. s->owner->streamid = ast_sched_add(s->owner->sched, whennext/8, ast_readaudio_callback, s);
  543. s->lasttimeout = whennext;
  544. return 0;
  545. }
  546. return 1;
  547. }
  548. static int ast_readvideo_callback(void *data)
  549. {
  550. struct ast_filestream *s = data;
  551. struct ast_frame *fr;
  552. int whennext = 0;
  553. while(!whennext) {
  554. fr = s->fmt->read(s, &whennext);
  555. if (fr) {
  556. if (ast_write(s->owner, fr)) {
  557. ast_log(LOG_WARNING, "Failed to write frame\n");
  558. s->owner->vstreamid = -1;
  559. return 0;
  560. }
  561. } else {
  562. /* Stream has finished */
  563. s->owner->vstreamid = -1;
  564. return 0;
  565. }
  566. }
  567. if (whennext != s->lasttimeout) {
  568. s->owner->vstreamid = ast_sched_add(s->owner->sched, whennext/8, ast_readvideo_callback, s);
  569. s->lasttimeout = whennext;
  570. return 0;
  571. }
  572. return 1;
  573. }
  574. int ast_applystream(struct ast_channel *chan, struct ast_filestream *s)
  575. {
  576. s->owner = chan;
  577. return 0;
  578. }
  579. int ast_playstream(struct ast_filestream *s)
  580. {
  581. if (s->fmt->format < AST_FORMAT_MAX_AUDIO)
  582. ast_readaudio_callback(s);
  583. else
  584. ast_readvideo_callback(s);
  585. return 0;
  586. }
  587. int ast_seekstream(struct ast_filestream *fs, long sample_offset, int whence)
  588. {
  589. return fs->fmt->seek(fs, sample_offset, whence);
  590. }
  591. int ast_truncstream(struct ast_filestream *fs)
  592. {
  593. return fs->fmt->trunc(fs);
  594. }
  595. long ast_tellstream(struct ast_filestream *fs)
  596. {
  597. return fs->fmt->tell(fs);
  598. }
  599. int ast_stream_fastforward(struct ast_filestream *fs, long ms)
  600. {
  601. /* I think this is right, 8000 samples per second, 1000 ms a second so 8
  602. * samples per ms */
  603. long samples = ms * 8;
  604. return ast_seekstream(fs, samples, SEEK_CUR);
  605. }
  606. int ast_stream_rewind(struct ast_filestream *fs, long ms)
  607. {
  608. long samples = ms * 8;
  609. samples = samples * -1;
  610. return ast_seekstream(fs, samples, SEEK_CUR);
  611. }
  612. int ast_closestream(struct ast_filestream *f)
  613. {
  614. char *cmd = NULL;
  615. size_t size = 0;
  616. /* Stop a running stream if there is one */
  617. if (f->owner) {
  618. if (f->fmt->format < AST_FORMAT_MAX_AUDIO) {
  619. f->owner->stream = NULL;
  620. if (f->owner->streamid > -1)
  621. ast_sched_del(f->owner->sched, f->owner->streamid);
  622. f->owner->streamid = -1;
  623. #ifdef ZAPTEL_OPTIMIZATIONS
  624. ast_settimeout(f->owner, 0, NULL, NULL);
  625. #endif
  626. } else {
  627. f->owner->vstream = NULL;
  628. if (f->owner->vstreamid > -1)
  629. ast_sched_del(f->owner->sched, f->owner->vstreamid);
  630. f->owner->vstreamid = -1;
  631. }
  632. }
  633. /* destroy the translator on exit */
  634. if (f->trans) {
  635. ast_translator_free_path(f->trans);
  636. f->trans = NULL;
  637. }
  638. if (f->realfilename && f->filename) {
  639. size = strlen(f->filename) + strlen(f->realfilename) + 15;
  640. cmd = alloca(size);
  641. memset(cmd,0,size);
  642. snprintf(cmd,size,"/bin/mv -f %s %s",f->filename,f->realfilename);
  643. ast_safe_system(cmd);
  644. }
  645. if (f->filename) {
  646. free(f->filename);
  647. f->filename = NULL;
  648. }
  649. if (f->realfilename) {
  650. free(f->realfilename);
  651. f->realfilename = NULL;
  652. }
  653. f->fmt->close(f);
  654. return 0;
  655. }
  656. int ast_fileexists(char *filename, char *fmt, char *preflang)
  657. {
  658. char filename2[256];
  659. char tmp[256];
  660. char *postfix;
  661. char *prefix;
  662. char *c;
  663. char lang2[MAX_LANGUAGE];
  664. int res = -1;
  665. if (preflang && !ast_strlen_zero(preflang)) {
  666. /* Insert the language between the last two parts of the path */
  667. strncpy(tmp, filename, sizeof(tmp) - 1);
  668. c = strrchr(tmp, '/');
  669. if (c) {
  670. *c = '\0';
  671. postfix = c+1;
  672. prefix = tmp;
  673. snprintf(filename2, sizeof(filename2), "%s/%s/%s", prefix, preflang, postfix);
  674. } else {
  675. postfix = tmp;
  676. prefix="";
  677. snprintf(filename2, sizeof(filename2), "%s/%s", preflang, postfix);
  678. }
  679. res = ast_filehelper(filename2, NULL, fmt, ACTION_EXISTS);
  680. if (res < 1) {
  681. char *stringp=NULL;
  682. strncpy(lang2, preflang, sizeof(lang2)-1);
  683. stringp=lang2;
  684. strsep(&stringp, "_");
  685. /* If language is a specific locality of a language (like es_MX), strip the locality and try again */
  686. if (strcmp(lang2, preflang)) {
  687. if (ast_strlen_zero(prefix)) {
  688. snprintf(filename2, sizeof(filename2), "%s/%s", lang2, postfix);
  689. } else {
  690. snprintf(filename2, sizeof(filename2), "%s/%s/%s", prefix, lang2, postfix);
  691. }
  692. res = ast_filehelper(filename2, NULL, fmt, ACTION_EXISTS);
  693. }
  694. }
  695. }
  696. /* Fallback to no language (usually winds up being American English) */
  697. if (res < 1) {
  698. res = ast_filehelper(filename, NULL, fmt, ACTION_EXISTS);
  699. }
  700. return res;
  701. }
  702. int ast_filedelete(char *filename, char *fmt)
  703. {
  704. return ast_filehelper(filename, NULL, fmt, ACTION_DELETE);
  705. }
  706. int ast_filerename(char *filename, char *filename2, char *fmt)
  707. {
  708. return ast_filehelper(filename, filename2, fmt, ACTION_RENAME);
  709. }
  710. int ast_filecopy(char *filename, char *filename2, char *fmt)
  711. {
  712. return ast_filehelper(filename, filename2, fmt, ACTION_COPY);
  713. }
  714. int ast_streamfile(struct ast_channel *chan, char *filename, char *preflang)
  715. {
  716. struct ast_filestream *fs;
  717. struct ast_filestream *vfs;
  718. fs = ast_openstream(chan, filename, preflang);
  719. vfs = ast_openvstream(chan, filename, preflang);
  720. if (vfs)
  721. ast_log(LOG_DEBUG, "Ooh, found a video stream, too\n");
  722. if (fs){
  723. if (ast_applystream(chan, fs))
  724. return -1;
  725. if (vfs && ast_applystream(chan, vfs))
  726. return -1;
  727. if (ast_playstream(fs))
  728. return -1;
  729. if (vfs && ast_playstream(vfs))
  730. return -1;
  731. #if 1
  732. if (option_verbose > 2)
  733. ast_verbose(VERBOSE_PREFIX_3 "Playing '%s' (language '%s')\n", filename, preflang ? preflang : "default");
  734. #endif
  735. return 0;
  736. }
  737. ast_log(LOG_WARNING, "Unable to open %s (format %s): %s\n", filename, ast_getformatname(chan->nativeformats), strerror(errno));
  738. return -1;
  739. }
  740. struct ast_filestream *ast_readfile(char *filename, char *type, char *comment, int flags, int check, mode_t mode)
  741. {
  742. int fd,myflags = 0;
  743. struct ast_format *f;
  744. struct ast_filestream *fs=NULL;
  745. char *fn;
  746. char *ext;
  747. if (ast_mutex_lock(&formatlock)) {
  748. ast_log(LOG_WARNING, "Unable to lock format list\n");
  749. return NULL;
  750. }
  751. f = formats;
  752. while(f) {
  753. if (exts_compare(f->exts, type)) {
  754. char *stringp=NULL;
  755. /* XXX Implement check XXX */
  756. ext = strdup(f->exts);
  757. stringp=ext;
  758. ext = strsep(&stringp, "|");
  759. fn = build_filename(filename, ext);
  760. fd = open(fn, flags | myflags);
  761. if (fd >= 0) {
  762. errno = 0;
  763. if ((fs = f->open(fd))) {
  764. fs->trans = NULL;
  765. fs->fmt = f;
  766. fs->flags = flags;
  767. fs->mode = mode;
  768. fs->filename = strdup(filename);
  769. fs->vfs = NULL;
  770. } else {
  771. ast_log(LOG_WARNING, "Unable to open %s\n", fn);
  772. close(fd);
  773. unlink(fn);
  774. }
  775. } else if (errno != EEXIST)
  776. ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));
  777. free(fn);
  778. free(ext);
  779. break;
  780. }
  781. f = f->next;
  782. }
  783. ast_mutex_unlock(&formatlock);
  784. if (!f)
  785. ast_log(LOG_WARNING, "No such format '%s'\n", type);
  786. return fs;
  787. }
  788. struct ast_filestream *ast_writefile(char *filename, char *type, char *comment, int flags, int check, mode_t mode)
  789. {
  790. int fd,myflags = 0;
  791. struct ast_format *f;
  792. struct ast_filestream *fs=NULL;
  793. char *fn,*orig_fn=NULL;
  794. char *ext;
  795. char *buf=NULL;
  796. size_t size = 0;
  797. if (ast_mutex_lock(&formatlock)) {
  798. ast_log(LOG_WARNING, "Unable to lock format list\n");
  799. return NULL;
  800. }
  801. /* set the O_TRUNC flag if and only if there is no O_APPEND specified */
  802. if (flags & O_APPEND){
  803. /* We really can't use O_APPEND as it will break WAV header updates */
  804. flags &= ~O_APPEND;
  805. }else{
  806. myflags = O_TRUNC;
  807. }
  808. myflags |= O_WRONLY | O_CREAT;
  809. f = formats;
  810. while(f) {
  811. if (exts_compare(f->exts, type)) {
  812. char *stringp=NULL;
  813. /* XXX Implement check XXX */
  814. ext = ast_strdupa(f->exts);
  815. stringp=ext;
  816. ext = strsep(&stringp, "|");
  817. fn = build_filename(filename, ext);
  818. fd = open(fn, flags | myflags, mode);
  819. if (option_cache_record_files && fd >= 0) {
  820. close(fd);
  821. /*
  822. We touch orig_fn just as a place-holder so other things (like vmail) see the file is there.
  823. What we are really doing is writing to record_cache_dir until we are done then we will mv the file into place.
  824. */
  825. orig_fn = ast_strdupa(fn);
  826. for (size=0;size<strlen(fn);size++) {
  827. if (fn[size] == '/')
  828. fn[size] = '_';
  829. }
  830. size += (strlen(record_cache_dir) + 10);
  831. buf = alloca(size);
  832. memset(buf, 0, size);
  833. snprintf(buf, size, "%s/%s", record_cache_dir, fn);
  834. free(fn);
  835. fn=buf;
  836. fd = open(fn, flags | myflags, mode);
  837. }
  838. if (fd >= 0) {
  839. errno = 0;
  840. if ((fs = f->rewrite(fd, comment))) {
  841. fs->trans = NULL;
  842. fs->fmt = f;
  843. fs->flags = flags;
  844. fs->mode = mode;
  845. if (option_cache_record_files) {
  846. fs->realfilename = build_filename(filename, ext);
  847. fs->filename = strdup(fn);
  848. } else {
  849. fs->realfilename = NULL;
  850. fs->filename = strdup(filename);
  851. }
  852. fs->vfs = NULL;
  853. } else {
  854. ast_log(LOG_WARNING, "Unable to rewrite %s\n", fn);
  855. close(fd);
  856. unlink(fn);
  857. if (orig_fn)
  858. unlink(orig_fn);
  859. }
  860. } else if (errno != EEXIST) {
  861. ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));
  862. if (orig_fn)
  863. unlink(orig_fn);
  864. }
  865. if (!buf) /* if buf != NULL then fn is already free and pointing to it */
  866. free(fn);
  867. break;
  868. }
  869. f = f->next;
  870. }
  871. ast_mutex_unlock(&formatlock);
  872. if (!f)
  873. ast_log(LOG_WARNING, "No such format '%s'\n", type);
  874. return fs;
  875. }
  876. int ast_waitstream(struct ast_channel *c, char *breakon)
  877. {
  878. /* XXX Maybe I should just front-end ast_waitstream_full ? XXX */
  879. int res;
  880. struct ast_frame *fr;
  881. if (!breakon) breakon = "";
  882. while(c->stream) {
  883. res = ast_sched_wait(c->sched);
  884. if ((res < 0) && !c->timingfunc) {
  885. ast_stopstream(c);
  886. break;
  887. }
  888. if (res < 0)
  889. res = 1000;
  890. res = ast_waitfor(c, res);
  891. if (res < 0) {
  892. ast_log(LOG_WARNING, "Select failed (%s)\n", strerror(errno));
  893. return res;
  894. } else if (res > 0) {
  895. fr = ast_read(c);
  896. if (!fr) {
  897. #if 0
  898. ast_log(LOG_DEBUG, "Got hung up\n");
  899. #endif
  900. return -1;
  901. }
  902. switch(fr->frametype) {
  903. case AST_FRAME_DTMF:
  904. res = fr->subclass;
  905. if (strchr(breakon, res)) {
  906. ast_frfree(fr);
  907. return res;
  908. }
  909. break;
  910. case AST_FRAME_CONTROL:
  911. switch(fr->subclass) {
  912. case AST_CONTROL_HANGUP:
  913. ast_frfree(fr);
  914. return -1;
  915. case AST_CONTROL_RINGING:
  916. case AST_CONTROL_ANSWER:
  917. /* Unimportant */
  918. break;
  919. default:
  920. ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", fr->subclass);
  921. }
  922. }
  923. /* Ignore */
  924. ast_frfree(fr);
  925. }
  926. ast_sched_runq(c->sched);
  927. }
  928. return (c->_softhangup ? -1 : 0);
  929. }
  930. int ast_waitstream_fr(struct ast_channel *c, char *breakon, char *forward, char *rewind, int ms)
  931. {
  932. int res;
  933. struct ast_frame *fr;
  934. if (!breakon)
  935. breakon = "";
  936. if (!forward)
  937. forward = "";
  938. if (!rewind)
  939. rewind = "";
  940. while(c->stream) {
  941. res = ast_sched_wait(c->sched);
  942. if ((res < 0) && !c->timingfunc) {
  943. ast_stopstream(c);
  944. break;
  945. }
  946. if (res < 0)
  947. res = 1000;
  948. res = ast_waitfor(c, res);
  949. if (res < 0) {
  950. ast_log(LOG_WARNING, "Select failed (%s)\n", strerror(errno));
  951. return res;
  952. } else
  953. if (res > 0) {
  954. fr = ast_read(c);
  955. if (!fr) {
  956. #if 0
  957. ast_log(LOG_DEBUG, "Got hung up\n");
  958. #endif
  959. return -1;
  960. }
  961. switch(fr->frametype) {
  962. case AST_FRAME_DTMF:
  963. res = fr->subclass;
  964. if (strchr(forward,res)) {
  965. ast_stream_fastforward(c->stream, ms);
  966. } else if (strchr(rewind,res)) {
  967. ast_stream_rewind(c->stream, ms);
  968. } else if (strchr(breakon, res)) {
  969. ast_frfree(fr);
  970. return res;
  971. }
  972. break;
  973. case AST_FRAME_CONTROL:
  974. switch(fr->subclass) {
  975. case AST_CONTROL_HANGUP:
  976. ast_frfree(fr);
  977. return -1;
  978. case AST_CONTROL_RINGING:
  979. case AST_CONTROL_ANSWER:
  980. /* Unimportant */
  981. break;
  982. default:
  983. ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", fr->subclass);
  984. }
  985. }
  986. /* Ignore */
  987. ast_frfree(fr);
  988. } else
  989. ast_sched_runq(c->sched);
  990. }
  991. return (c->_softhangup ? -1 : 0);
  992. }
  993. int ast_waitstream_full(struct ast_channel *c, char *breakon, int audiofd, int cmdfd)
  994. {
  995. int res;
  996. int ms;
  997. int outfd;
  998. struct ast_frame *fr;
  999. struct ast_channel *rchan;
  1000. if (!breakon)
  1001. breakon = "";
  1002. while(c->stream) {
  1003. ms = ast_sched_wait(c->sched);
  1004. if ((ms < 0) && !c->timingfunc) {
  1005. ast_stopstream(c);
  1006. break;
  1007. }
  1008. if (ms < 0)
  1009. ms = 1000;
  1010. rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
  1011. if (!rchan && (outfd < 0) && (ms)) {
  1012. /* Continue */
  1013. if (errno == EINTR)
  1014. continue;
  1015. ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
  1016. return -1;
  1017. } else if (outfd > -1) {
  1018. /* The FD we were watching has something waiting */
  1019. return 1;
  1020. } else if (rchan) {
  1021. fr = ast_read(c);
  1022. if (!fr) {
  1023. #if 0
  1024. ast_log(LOG_DEBUG, "Got hung up\n");
  1025. #endif
  1026. return -1;
  1027. }
  1028. switch(fr->frametype) {
  1029. case AST_FRAME_DTMF:
  1030. res = fr->subclass;
  1031. if (strchr(breakon, res)) {
  1032. ast_frfree(fr);
  1033. return res;
  1034. }
  1035. break;
  1036. case AST_FRAME_CONTROL:
  1037. switch(fr->subclass) {
  1038. case AST_CONTROL_HANGUP:
  1039. ast_frfree(fr);
  1040. return -1;
  1041. case AST_CONTROL_RINGING:
  1042. case AST_CONTROL_ANSWER:
  1043. /* Unimportant */
  1044. break;
  1045. default:
  1046. ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", fr->subclass);
  1047. }
  1048. case AST_FRAME_VOICE:
  1049. /* Write audio if appropriate */
  1050. if (audiofd > -1)
  1051. write(audiofd, fr->data, fr->datalen);
  1052. }
  1053. /* Ignore */
  1054. ast_frfree(fr);
  1055. }
  1056. ast_sched_runq(c->sched);
  1057. }
  1058. return (c->_softhangup ? -1 : 0);
  1059. }
  1060. static int show_file_formats(int fd, int argc, char *argv[])
  1061. {
  1062. #define FORMAT "%-10s %-10s %-20s\n"
  1063. #define FORMAT2 "%-10s %-10s %-20s\n"
  1064. struct ast_format *f;
  1065. if (argc != 3)
  1066. return RESULT_SHOWUSAGE;
  1067. ast_cli(fd, FORMAT, "Format", "Name", "Extensions");
  1068. if (ast_mutex_lock(&formatlock)) {
  1069. ast_log(LOG_WARNING, "Unable to lock format list\n");
  1070. return -1;
  1071. }
  1072. f = formats;
  1073. while(f) {
  1074. ast_cli(fd, FORMAT2, ast_getformatname(f->format), f->name, f->exts);
  1075. f = f->next;
  1076. };
  1077. ast_mutex_unlock(&formatlock);
  1078. return RESULT_SUCCESS;
  1079. }
  1080. struct ast_cli_entry show_file =
  1081. {
  1082. { "show", "file", "formats" },
  1083. show_file_formats,
  1084. "Displays file formats",
  1085. "Usage: show file formats\n"
  1086. " displays currently registered file formats (if any)\n"
  1087. };
  1088. int ast_file_init(void)
  1089. {
  1090. ast_cli_register(&show_file);
  1091. return 0;
  1092. }