frame.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Frame manipulation routines
  21. *
  22. */
  23. #include <stdlib.h>
  24. #include <unistd.h>
  25. #include <string.h>
  26. #include <errno.h>
  27. #include <stdio.h>
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include "asterisk/lock.h"
  31. #include "asterisk/frame.h"
  32. #include "asterisk/logger.h"
  33. #include "asterisk/options.h"
  34. #include "asterisk/channel.h"
  35. #include "asterisk/cli.h"
  36. #include "asterisk/term.h"
  37. #include "asterisk/utils.h"
  38. #ifdef TRACE_FRAMES
  39. static int headers = 0;
  40. static struct ast_frame *headerlist = NULL;
  41. AST_MUTEX_DEFINE_STATIC(framelock);
  42. #endif
  43. #define SMOOTHER_SIZE 8000
  44. #define TYPE_HIGH 0x0
  45. #define TYPE_LOW 0x1
  46. #define TYPE_SILENCE 0x2
  47. #define TYPE_DONTSEND 0x3
  48. #define TYPE_MASK 0x3
  49. struct ast_format_list {
  50. int visible; /*!< Can we see this entry */
  51. int bits; /*!< bitmask value */
  52. char *name; /*!< short name */
  53. char *desc; /*!< Description */
  54. };
  55. struct ast_smoother {
  56. int size;
  57. int format;
  58. int readdata;
  59. int optimizablestream;
  60. int flags;
  61. float samplesperbyte;
  62. struct ast_frame f;
  63. struct timeval delivery;
  64. char data[SMOOTHER_SIZE];
  65. char framedata[SMOOTHER_SIZE + AST_FRIENDLY_OFFSET];
  66. struct ast_frame *opt;
  67. int len;
  68. };
  69. /*! \brief Definition of supported media formats (codecs) */
  70. static struct ast_format_list AST_FORMAT_LIST[] = {
  71. { 1, AST_FORMAT_G723_1 , "g723" , "G.723.1"}, /*!< codec_g723_1.c */
  72. { 1, AST_FORMAT_GSM, "gsm" , "GSM"}, /*!< codec_gsm.c */
  73. { 1, AST_FORMAT_ULAW, "ulaw", "G.711 u-law" }, /*!< codec_ulaw.c */
  74. { 1, AST_FORMAT_ALAW, "alaw", "G.711 A-law" }, /*!< codec_alaw.c */
  75. { 1, AST_FORMAT_G726, "g726", "G.726" }, /*!< codec_g726.c */
  76. { 1, AST_FORMAT_ADPCM, "adpcm" , "ADPCM"}, /*!< codec_adpcm.c */
  77. { 1, AST_FORMAT_SLINEAR, "slin", "16 bit Signed Linear PCM"}, /*!< */
  78. { 1, AST_FORMAT_LPC10, "lpc10", "LPC10" }, /*!< codec_lpc10.c */
  79. { 1, AST_FORMAT_G729A, "g729", "G.729A" }, /*!< Binary commercial distribution */
  80. { 1, AST_FORMAT_SPEEX, "speex", "SpeeX" }, /*!< codec_speex.c */
  81. { 1, AST_FORMAT_ILBC, "ilbc", "iLBC"}, /*!< codec_ilbc.c */
  82. { 0, 0, "nothing", "undefined" },
  83. { 0, 0, "nothing", "undefined" },
  84. { 0, 0, "nothing", "undefined" },
  85. { 0, 0, "nothing", "undefined" },
  86. { 0, AST_FORMAT_MAX_AUDIO, "maxaudio", "Maximum audio format" },
  87. { 1, AST_FORMAT_JPEG, "jpeg", "JPEG image"}, /*!< See format_jpeg.c */
  88. { 1, AST_FORMAT_PNG, "png", "PNG image"}, /*!< Image format */
  89. { 1, AST_FORMAT_H261, "h261", "H.261 Video" }, /*!< Passthrough */
  90. { 1, AST_FORMAT_H263, "h263", "H.263 Video" }, /*!< Passthrough support, see format_h263.c */
  91. { 1, AST_FORMAT_H263_PLUS, "h263p", "H.263+ Video" }, /*!< See format_h263.c */
  92. { 0, 0, "nothing", "undefined" },
  93. { 0, 0, "nothing", "undefined" },
  94. { 0, 0, "nothing", "undefined" },
  95. { 0, 0, "nothing", "undefined" },
  96. { 0, AST_FORMAT_MAX_VIDEO, "maxvideo", "Maximum video format" },
  97. };
  98. void ast_smoother_reset(struct ast_smoother *s, int size)
  99. {
  100. memset(s, 0, sizeof(struct ast_smoother));
  101. s->size = size;
  102. }
  103. struct ast_smoother *ast_smoother_new(int size)
  104. {
  105. struct ast_smoother *s;
  106. if (size < 1)
  107. return NULL;
  108. s = malloc(sizeof(struct ast_smoother));
  109. if (s)
  110. ast_smoother_reset(s, size);
  111. return s;
  112. }
  113. int ast_smoother_get_flags(struct ast_smoother *s)
  114. {
  115. return s->flags;
  116. }
  117. void ast_smoother_set_flags(struct ast_smoother *s, int flags)
  118. {
  119. s->flags = flags;
  120. }
  121. int __ast_smoother_feed(struct ast_smoother *s, struct ast_frame *f, int swap)
  122. {
  123. if (f->frametype != AST_FRAME_VOICE) {
  124. ast_log(LOG_WARNING, "Huh? Can't smooth a non-voice frame!\n");
  125. return -1;
  126. }
  127. if (!s->format) {
  128. s->format = f->subclass;
  129. s->samplesperbyte = (float)f->samples / (float)f->datalen;
  130. } else if (s->format != f->subclass) {
  131. ast_log(LOG_WARNING, "Smoother was working on %d format frames, now trying to feed %d?\n", s->format, f->subclass);
  132. return -1;
  133. }
  134. if (s->len + f->datalen > SMOOTHER_SIZE) {
  135. ast_log(LOG_WARNING, "Out of smoother space\n");
  136. return -1;
  137. }
  138. if (((f->datalen == s->size) || ((f->datalen < 10) && (s->flags & AST_SMOOTHER_FLAG_G729)))
  139. && !s->opt && (f->offset >= AST_MIN_OFFSET)) {
  140. if (!s->len) {
  141. /* Optimize by sending the frame we just got
  142. on the next read, thus eliminating the douple
  143. copy */
  144. s->opt = f;
  145. return 0;
  146. } else {
  147. s->optimizablestream++;
  148. if (s->optimizablestream > 10) {
  149. /* For the past 10 rounds, we have input and output
  150. frames of the correct size for this smoother, yet
  151. we were unable to optimize because there was still
  152. some cruft left over. Lets just drop the cruft so
  153. we can move to a fully optimized path */
  154. s->len = 0;
  155. s->opt = f;
  156. return 0;
  157. }
  158. }
  159. } else
  160. s->optimizablestream = 0;
  161. if (s->flags & AST_SMOOTHER_FLAG_G729) {
  162. if (s->len % 10) {
  163. ast_log(LOG_NOTICE, "Dropping extra frame of G.729 since we already have a VAD frame at the end\n");
  164. return 0;
  165. }
  166. }
  167. if (swap)
  168. ast_swapcopy_samples(s->data+s->len, f->data, f->samples);
  169. else
  170. memcpy(s->data + s->len, f->data, f->datalen);
  171. /* If either side is empty, reset the delivery time */
  172. if (!s->len || ast_tvzero(f->delivery) || ast_tvzero(s->delivery)) /* XXX really ? */
  173. s->delivery = f->delivery;
  174. s->len += f->datalen;
  175. return 0;
  176. }
  177. struct ast_frame *ast_smoother_read(struct ast_smoother *s)
  178. {
  179. struct ast_frame *opt;
  180. int len;
  181. /* IF we have an optimization frame, send it */
  182. if (s->opt) {
  183. if (s->opt->offset < AST_FRIENDLY_OFFSET)
  184. ast_log(LOG_WARNING, "Returning a frame of inappropriate offset (%d).",
  185. s->opt->offset);
  186. opt = s->opt;
  187. s->opt = NULL;
  188. return opt;
  189. }
  190. /* Make sure we have enough data */
  191. if (s->len < s->size) {
  192. /* Or, if this is a G.729 frame with VAD on it, send it immediately anyway */
  193. if (!((s->flags & AST_SMOOTHER_FLAG_G729) && (s->size % 10)))
  194. return NULL;
  195. }
  196. len = s->size;
  197. if (len > s->len)
  198. len = s->len;
  199. /* Make frame */
  200. s->f.frametype = AST_FRAME_VOICE;
  201. s->f.subclass = s->format;
  202. s->f.data = s->framedata + AST_FRIENDLY_OFFSET;
  203. s->f.offset = AST_FRIENDLY_OFFSET;
  204. s->f.datalen = len;
  205. /* Samples will be improper given VAD, but with VAD the concept really doesn't even exist */
  206. s->f.samples = len * s->samplesperbyte; /* XXX rounding */
  207. s->f.delivery = s->delivery;
  208. /* Fill Data */
  209. memcpy(s->f.data, s->data, len);
  210. s->len -= len;
  211. /* Move remaining data to the front if applicable */
  212. if (s->len) {
  213. /* In principle this should all be fine because if we are sending
  214. G.729 VAD, the next timestamp will take over anyawy */
  215. memmove(s->data, s->data + len, s->len);
  216. if (!ast_tvzero(s->delivery)) {
  217. /* If we have delivery time, increment it, otherwise, leave it at 0 */
  218. s->delivery = ast_tvadd(s->delivery, ast_samp2tv(s->f.samples, 8000));
  219. }
  220. }
  221. /* Return frame */
  222. return &s->f;
  223. }
  224. void ast_smoother_free(struct ast_smoother *s)
  225. {
  226. free(s);
  227. }
  228. static struct ast_frame *ast_frame_header_new(void)
  229. {
  230. struct ast_frame *f;
  231. f = malloc(sizeof(struct ast_frame));
  232. if (f)
  233. memset(f, 0, sizeof(struct ast_frame));
  234. #ifdef TRACE_FRAMES
  235. if (f) {
  236. headers++;
  237. f->prev = NULL;
  238. ast_mutex_lock(&framelock);
  239. f->next = headerlist;
  240. if (headerlist)
  241. headerlist->prev = f;
  242. headerlist = f;
  243. ast_mutex_unlock(&framelock);
  244. }
  245. #endif
  246. return f;
  247. }
  248. /*!
  249. * \todo Important: I should be made more efficient. Frame headers should
  250. * most definitely be cached
  251. */
  252. void ast_frfree(struct ast_frame *fr)
  253. {
  254. if (fr->mallocd & AST_MALLOCD_DATA) {
  255. if (fr->data)
  256. free(fr->data - fr->offset);
  257. }
  258. if (fr->mallocd & AST_MALLOCD_SRC) {
  259. if (fr->src)
  260. free((char *)fr->src);
  261. }
  262. if (fr->mallocd & AST_MALLOCD_HDR) {
  263. #ifdef TRACE_FRAMES
  264. headers--;
  265. ast_mutex_lock(&framelock);
  266. if (fr->next)
  267. fr->next->prev = fr->prev;
  268. if (fr->prev)
  269. fr->prev->next = fr->next;
  270. else
  271. headerlist = fr->next;
  272. ast_mutex_unlock(&framelock);
  273. #endif
  274. free(fr);
  275. }
  276. }
  277. /*!
  278. * \brief 'isolates' a frame by duplicating non-malloc'ed components
  279. * (header, src, data).
  280. * On return all components are malloc'ed
  281. */
  282. struct ast_frame *ast_frisolate(struct ast_frame *fr)
  283. {
  284. struct ast_frame *out;
  285. if (!(fr->mallocd & AST_MALLOCD_HDR)) {
  286. /* Allocate a new header if needed */
  287. out = ast_frame_header_new();
  288. if (!out) {
  289. ast_log(LOG_WARNING, "Out of memory\n");
  290. return NULL;
  291. }
  292. out->frametype = fr->frametype;
  293. out->subclass = fr->subclass;
  294. out->datalen = fr->datalen;
  295. out->samples = fr->samples;
  296. out->offset = fr->offset;
  297. out->src = NULL;
  298. out->data = fr->data;
  299. } else {
  300. out = fr;
  301. }
  302. if (!(fr->mallocd & AST_MALLOCD_SRC)) {
  303. if (fr->src)
  304. out->src = strdup(fr->src);
  305. } else
  306. out->src = fr->src;
  307. if (!(fr->mallocd & AST_MALLOCD_DATA)) {
  308. out->data = malloc(fr->datalen + AST_FRIENDLY_OFFSET);
  309. if (!out->data) {
  310. free(out);
  311. ast_log(LOG_WARNING, "Out of memory\n");
  312. return NULL;
  313. }
  314. out->data += AST_FRIENDLY_OFFSET;
  315. out->offset = AST_FRIENDLY_OFFSET;
  316. out->datalen = fr->datalen;
  317. memcpy(out->data, fr->data, fr->datalen);
  318. }
  319. out->mallocd = AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA;
  320. return out;
  321. }
  322. struct ast_frame *ast_frdup(struct ast_frame *f)
  323. {
  324. struct ast_frame *out;
  325. int len, srclen = 0;
  326. void *buf;
  327. /* Start with standard stuff */
  328. len = sizeof(struct ast_frame) + AST_FRIENDLY_OFFSET + f->datalen;
  329. /* If we have a source, add space for it */
  330. /*
  331. * XXX Watch out here - if we receive a src which is not terminated
  332. * properly, we can be easily attacked. Should limit the size we deal with.
  333. */
  334. if (f->src)
  335. srclen = strlen(f->src);
  336. if (srclen > 0)
  337. len += srclen + 1;
  338. buf = malloc(len);
  339. if (!buf)
  340. return NULL;
  341. out = buf;
  342. /* Set us as having malloc'd header only, so it will eventually
  343. get freed. */
  344. out->frametype = f->frametype;
  345. out->subclass = f->subclass;
  346. out->datalen = f->datalen;
  347. out->samples = f->samples;
  348. out->delivery = f->delivery;
  349. out->mallocd = AST_MALLOCD_HDR;
  350. out->offset = AST_FRIENDLY_OFFSET;
  351. out->data = buf + sizeof(struct ast_frame) + AST_FRIENDLY_OFFSET;
  352. if (srclen > 0) {
  353. out->src = out->data + f->datalen;
  354. /* Must have space since we allocated for it */
  355. strcpy((char *)out->src, f->src);
  356. } else
  357. out->src = NULL;
  358. out->prev = NULL;
  359. out->next = NULL;
  360. memcpy(out->data, f->data, out->datalen);
  361. return out;
  362. }
  363. #if 0
  364. /*
  365. * XXX
  366. * This function is badly broken - it does not handle correctly
  367. * partial reads on either header or body.
  368. * However is it never used anywhere so we leave it commented out
  369. */
  370. struct ast_frame *ast_fr_fdread(int fd)
  371. {
  372. char buf[65536];
  373. int res;
  374. int ttl = sizeof(struct ast_frame);
  375. struct ast_frame *f = (struct ast_frame *)buf;
  376. /* Read a frame directly from there. They're always in the
  377. right format. */
  378. while(ttl) {
  379. res = read(fd, buf, ttl);
  380. if (res < 0) {
  381. ast_log(LOG_WARNING, "Bad read on %d: %s\n", fd, strerror(errno));
  382. return NULL;
  383. }
  384. ttl -= res;
  385. }
  386. /* read the frame header */
  387. f->mallocd = 0;
  388. /* Re-write data position */
  389. f->data = buf + sizeof(struct ast_frame);
  390. f->offset = 0;
  391. /* Forget about being mallocd */
  392. f->mallocd = 0;
  393. /* Re-write the source */
  394. f->src = (char *)__FUNCTION__;
  395. if (f->datalen > sizeof(buf) - sizeof(struct ast_frame)) {
  396. /* Really bad read */
  397. ast_log(LOG_WARNING, "Strange read (%d bytes)\n", f->datalen);
  398. return NULL;
  399. }
  400. if (f->datalen) {
  401. if ((res = read(fd, f->data, f->datalen)) != f->datalen) {
  402. /* Bad read */
  403. ast_log(LOG_WARNING, "How very strange, expected %d, got %d\n", f->datalen, res);
  404. return NULL;
  405. }
  406. }
  407. if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP)) {
  408. return NULL;
  409. }
  410. return ast_frisolate(f);
  411. }
  412. /* Some convenient routines for sending frames to/from stream or datagram
  413. sockets, pipes, etc (maybe even files) */
  414. /*
  415. * XXX this function is also partly broken because it does not handle
  416. * partial writes. We comment it out too, and also the unique
  417. * client it has, ast_fr_fdhangup()
  418. */
  419. int ast_fr_fdwrite(int fd, struct ast_frame *frame)
  420. {
  421. /* Write the frame exactly */
  422. if (write(fd, frame, sizeof(struct ast_frame)) != sizeof(struct ast_frame)) {
  423. ast_log(LOG_WARNING, "Write error: %s\n", strerror(errno));
  424. return -1;
  425. }
  426. if (write(fd, frame->data, frame->datalen) != frame->datalen) {
  427. ast_log(LOG_WARNING, "Write error: %s\n", strerror(errno));
  428. return -1;
  429. }
  430. return 0;
  431. }
  432. int ast_fr_fdhangup(int fd)
  433. {
  434. struct ast_frame hangup = {
  435. AST_FRAME_CONTROL,
  436. AST_CONTROL_HANGUP
  437. };
  438. return ast_fr_fdwrite(fd, &hangup);
  439. }
  440. #endif /* unused functions */
  441. void ast_swapcopy_samples(void *dst, const void *src, int samples)
  442. {
  443. int i;
  444. unsigned short *dst_s = dst;
  445. const unsigned short *src_s = src;
  446. for (i=0; i<samples; i++)
  447. dst_s[i] = (src_s[i]<<8) | (src_s[i]>>8);
  448. }
  449. struct ast_format_list *ast_get_format_list_index(int index)
  450. {
  451. return &AST_FORMAT_LIST[index];
  452. }
  453. struct ast_format_list *ast_get_format_list(size_t *size)
  454. {
  455. *size = (sizeof(AST_FORMAT_LIST) / sizeof(struct ast_format_list));
  456. return AST_FORMAT_LIST;
  457. }
  458. char* ast_getformatname(int format)
  459. {
  460. int x = 0;
  461. char *ret = "unknown";
  462. for (x = 0 ; x < sizeof(AST_FORMAT_LIST) / sizeof(struct ast_format_list) ; x++) {
  463. if(AST_FORMAT_LIST[x].visible && AST_FORMAT_LIST[x].bits == format) {
  464. ret = AST_FORMAT_LIST[x].name;
  465. break;
  466. }
  467. }
  468. return ret;
  469. }
  470. char *ast_getformatname_multiple(char *buf, size_t size, int format) {
  471. int x = 0;
  472. unsigned len;
  473. char *end = buf;
  474. char *start = buf;
  475. if (!size) return buf;
  476. snprintf(end, size, "0x%x (", format);
  477. len = strlen(end);
  478. end += len;
  479. size -= len;
  480. start = end;
  481. for (x = 0 ; x < sizeof(AST_FORMAT_LIST) / sizeof(struct ast_format_list) ; x++) {
  482. if (AST_FORMAT_LIST[x].visible && (AST_FORMAT_LIST[x].bits & format)) {
  483. snprintf(end, size,"%s|",AST_FORMAT_LIST[x].name);
  484. len = strlen(end);
  485. end += len;
  486. size -= len;
  487. }
  488. }
  489. if (start == end)
  490. snprintf(start, size, "nothing)");
  491. else if (size > 1)
  492. *(end -1) = ')';
  493. return buf;
  494. }
  495. static struct ast_codec_alias_table {
  496. char *alias;
  497. char *realname;
  498. } ast_codec_alias_table[] = {
  499. {"slinear","slin"},
  500. {"g723.1","g723"},
  501. };
  502. static char *ast_expand_codec_alias(char *in) {
  503. int x = 0;
  504. for (x = 0; x < sizeof(ast_codec_alias_table) / sizeof(struct ast_codec_alias_table) ; x++) {
  505. if(!strcmp(in,ast_codec_alias_table[x].alias))
  506. return ast_codec_alias_table[x].realname;
  507. }
  508. return in;
  509. }
  510. int ast_getformatbyname(char *name)
  511. {
  512. int x = 0, all = 0, format = 0;
  513. all = strcasecmp(name, "all") ? 0 : 1;
  514. for (x = 0 ; x < sizeof(AST_FORMAT_LIST) / sizeof(struct ast_format_list) ; x++) {
  515. if(AST_FORMAT_LIST[x].visible && (all ||
  516. !strcasecmp(AST_FORMAT_LIST[x].name,name) ||
  517. !strcasecmp(AST_FORMAT_LIST[x].name,ast_expand_codec_alias(name)))) {
  518. format |= AST_FORMAT_LIST[x].bits;
  519. if(!all)
  520. break;
  521. }
  522. }
  523. return format;
  524. }
  525. char *ast_codec2str(int codec) {
  526. int x = 0;
  527. char *ret = "unknown";
  528. for (x = 0 ; x < sizeof(AST_FORMAT_LIST) / sizeof(struct ast_format_list) ; x++) {
  529. if(AST_FORMAT_LIST[x].visible && AST_FORMAT_LIST[x].bits == codec) {
  530. ret = AST_FORMAT_LIST[x].desc;
  531. break;
  532. }
  533. }
  534. return ret;
  535. }
  536. static int show_codecs(int fd, int argc, char *argv[])
  537. {
  538. int i, found=0;
  539. char hex[25];
  540. if ((argc < 2) || (argc > 3))
  541. return RESULT_SHOWUSAGE;
  542. if (!option_dontwarn)
  543. ast_cli(fd, "Disclaimer: this command is for informational purposes only.\n"
  544. "\tIt does not indicate anything about your configuration.\n");
  545. ast_cli(fd, "%11s %9s %10s TYPE %5s %s\n","INT","BINARY","HEX","NAME","DESC");
  546. ast_cli(fd, "--------------------------------------------------------------------------------\n");
  547. if ((argc == 2) || (!strcasecmp(argv[1],"audio"))) {
  548. found = 1;
  549. for (i=0;i<11;i++) {
  550. snprintf(hex,25,"(0x%x)",1<<i);
  551. ast_cli(fd, "%11u (1 << %2d) %10s audio %5s (%s)\n",1 << i,i,hex,ast_getformatname(1<<i),ast_codec2str(1<<i));
  552. }
  553. }
  554. if ((argc == 2) || (!strcasecmp(argv[1],"image"))) {
  555. found = 1;
  556. for (i=16;i<18;i++) {
  557. snprintf(hex,25,"(0x%x)",1<<i);
  558. ast_cli(fd, "%11u (1 << %2d) %10s image %5s (%s)\n",1 << i,i,hex,ast_getformatname(1<<i),ast_codec2str(1<<i));
  559. }
  560. }
  561. if ((argc == 2) || (!strcasecmp(argv[1],"video"))) {
  562. found = 1;
  563. for (i=18;i<21;i++) {
  564. snprintf(hex,25,"(0x%x)",1<<i);
  565. ast_cli(fd, "%11u (1 << %2d) %10s video %5s (%s)\n",1 << i,i,hex,ast_getformatname(1<<i),ast_codec2str(1<<i));
  566. }
  567. }
  568. if (! found)
  569. return RESULT_SHOWUSAGE;
  570. else
  571. return RESULT_SUCCESS;
  572. }
  573. static char frame_show_codecs_usage[] =
  574. "Usage: show [audio|video|image] codecs\n"
  575. " Displays codec mapping\n";
  576. static int show_codec_n(int fd, int argc, char *argv[])
  577. {
  578. int codec, i, found=0;
  579. if (argc != 3)
  580. return RESULT_SHOWUSAGE;
  581. if (sscanf(argv[2],"%d",&codec) != 1)
  582. return RESULT_SHOWUSAGE;
  583. for (i=0;i<32;i++)
  584. if (codec & (1 << i)) {
  585. found = 1;
  586. ast_cli(fd, "%11u (1 << %2d) %s\n",1 << i,i,ast_codec2str(1<<i));
  587. }
  588. if (! found)
  589. ast_cli(fd, "Codec %d not found\n", codec);
  590. return RESULT_SUCCESS;
  591. }
  592. static char frame_show_codec_n_usage[] =
  593. "Usage: show codec <number>\n"
  594. " Displays codec mapping\n";
  595. /*! Dump a frame for debugging purposes */
  596. void ast_frame_dump(char *name, struct ast_frame *f, char *prefix)
  597. {
  598. char *n = "unknown";
  599. char ftype[40] = "Unknown Frametype";
  600. char cft[80];
  601. char subclass[40] = "Unknown Subclass";
  602. char csub[80];
  603. char moreinfo[40] = "";
  604. char cn[60];
  605. char cp[40];
  606. char cmn[40];
  607. if (name)
  608. n = name;
  609. if (!f) {
  610. ast_verbose("%s [ %s (NULL) ] [%s]\n",
  611. term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
  612. term_color(cft, "HANGUP", COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
  613. term_color(cn, n, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
  614. return;
  615. }
  616. /* XXX We should probably print one each of voice and video when the format changes XXX */
  617. if (f->frametype == AST_FRAME_VOICE)
  618. return;
  619. if (f->frametype == AST_FRAME_VIDEO)
  620. return;
  621. switch(f->frametype) {
  622. case AST_FRAME_DTMF:
  623. strcpy(ftype, "DTMF");
  624. subclass[0] = f->subclass;
  625. subclass[1] = '\0';
  626. break;
  627. case AST_FRAME_CONTROL:
  628. strcpy(ftype, "Control");
  629. switch(f->subclass) {
  630. case AST_CONTROL_HANGUP:
  631. strcpy(subclass, "Hangup");
  632. break;
  633. case AST_CONTROL_RING:
  634. strcpy(subclass, "Ring");
  635. break;
  636. case AST_CONTROL_RINGING:
  637. strcpy(subclass, "Ringing");
  638. break;
  639. case AST_CONTROL_ANSWER:
  640. strcpy(subclass, "Answer");
  641. break;
  642. case AST_CONTROL_BUSY:
  643. strcpy(subclass, "Busy");
  644. break;
  645. case AST_CONTROL_TAKEOFFHOOK:
  646. strcpy(subclass, "Take Off Hook");
  647. break;
  648. case AST_CONTROL_OFFHOOK:
  649. strcpy(subclass, "Line Off Hook");
  650. break;
  651. case AST_CONTROL_CONGESTION:
  652. strcpy(subclass, "Congestion");
  653. break;
  654. case AST_CONTROL_FLASH:
  655. strcpy(subclass, "Flash");
  656. break;
  657. case AST_CONTROL_WINK:
  658. strcpy(subclass, "Wink");
  659. break;
  660. case AST_CONTROL_OPTION:
  661. strcpy(subclass, "Option");
  662. break;
  663. case AST_CONTROL_RADIO_KEY:
  664. strcpy(subclass, "Key Radio");
  665. break;
  666. case AST_CONTROL_RADIO_UNKEY:
  667. strcpy(subclass, "Unkey Radio");
  668. break;
  669. case -1:
  670. strcpy(subclass, "Stop generators");
  671. break;
  672. default:
  673. snprintf(subclass, sizeof(subclass), "Unknown control '%d'", f->subclass);
  674. }
  675. break;
  676. case AST_FRAME_NULL:
  677. strcpy(ftype, "Null Frame");
  678. strcpy(subclass, "N/A");
  679. break;
  680. case AST_FRAME_IAX:
  681. /* Should never happen */
  682. strcpy(ftype, "IAX Specific");
  683. snprintf(subclass, sizeof(subclass), "IAX Frametype %d", f->subclass);
  684. break;
  685. case AST_FRAME_TEXT:
  686. strcpy(ftype, "Text");
  687. strcpy(subclass, "N/A");
  688. ast_copy_string(moreinfo, f->data, sizeof(moreinfo));
  689. break;
  690. case AST_FRAME_IMAGE:
  691. strcpy(ftype, "Image");
  692. snprintf(subclass, sizeof(subclass), "Image format %s\n", ast_getformatname(f->subclass));
  693. break;
  694. case AST_FRAME_HTML:
  695. strcpy(ftype, "HTML");
  696. switch(f->subclass) {
  697. case AST_HTML_URL:
  698. strcpy(subclass, "URL");
  699. ast_copy_string(moreinfo, f->data, sizeof(moreinfo));
  700. break;
  701. case AST_HTML_DATA:
  702. strcpy(subclass, "Data");
  703. break;
  704. case AST_HTML_BEGIN:
  705. strcpy(subclass, "Begin");
  706. break;
  707. case AST_HTML_END:
  708. strcpy(subclass, "End");
  709. break;
  710. case AST_HTML_LDCOMPLETE:
  711. strcpy(subclass, "Load Complete");
  712. break;
  713. case AST_HTML_NOSUPPORT:
  714. strcpy(subclass, "No Support");
  715. break;
  716. case AST_HTML_LINKURL:
  717. strcpy(subclass, "Link URL");
  718. ast_copy_string(moreinfo, f->data, sizeof(moreinfo));
  719. break;
  720. case AST_HTML_UNLINK:
  721. strcpy(subclass, "Unlink");
  722. break;
  723. case AST_HTML_LINKREJECT:
  724. strcpy(subclass, "Link Reject");
  725. break;
  726. default:
  727. snprintf(subclass, sizeof(subclass), "Unknown HTML frame '%d'\n", f->subclass);
  728. break;
  729. }
  730. break;
  731. default:
  732. snprintf(ftype, sizeof(ftype), "Unknown Frametype '%d'", f->frametype);
  733. }
  734. if (!ast_strlen_zero(moreinfo))
  735. ast_verbose("%s [ TYPE: %s (%d) SUBCLASS: %s (%d) '%s' ] [%s]\n",
  736. term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
  737. term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
  738. f->frametype,
  739. term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
  740. f->subclass,
  741. term_color(cmn, moreinfo, COLOR_BRGREEN, COLOR_BLACK, sizeof(cmn)),
  742. term_color(cn, n, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
  743. else
  744. ast_verbose("%s [ TYPE: %s (%d) SUBCLASS: %s (%d) ] [%s]\n",
  745. term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
  746. term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
  747. f->frametype,
  748. term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
  749. f->subclass,
  750. term_color(cn, n, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
  751. }
  752. #ifdef TRACE_FRAMES
  753. static int show_frame_stats(int fd, int argc, char *argv[])
  754. {
  755. struct ast_frame *f;
  756. int x=1;
  757. if (argc != 3)
  758. return RESULT_SHOWUSAGE;
  759. ast_cli(fd, " Framer Statistics \n");
  760. ast_cli(fd, "---------------------------\n");
  761. ast_cli(fd, "Total allocated headers: %d\n", headers);
  762. ast_cli(fd, "Queue Dump:\n");
  763. ast_mutex_lock(&framelock);
  764. for (f=headerlist; f; f = f->next) {
  765. ast_cli(fd, "%d. Type %d, subclass %d from %s\n", x++, f->frametype, f->subclass, f->src ? f->src : "<Unknown>");
  766. }
  767. ast_mutex_unlock(&framelock);
  768. return RESULT_SUCCESS;
  769. }
  770. static char frame_stats_usage[] =
  771. "Usage: show frame stats\n"
  772. " Displays debugging statistics from framer\n";
  773. #endif
  774. /* Builtin Asterisk CLI-commands for debugging */
  775. static struct ast_cli_entry my_clis[] = {
  776. { { "show", "codecs", NULL }, show_codecs, "Shows codecs", frame_show_codecs_usage },
  777. { { "show", "audio", "codecs", NULL }, show_codecs, "Shows audio codecs", frame_show_codecs_usage },
  778. { { "show", "video", "codecs", NULL }, show_codecs, "Shows video codecs", frame_show_codecs_usage },
  779. { { "show", "image", "codecs", NULL }, show_codecs, "Shows image codecs", frame_show_codecs_usage },
  780. { { "show", "codec", NULL }, show_codec_n, "Shows a specific codec", frame_show_codec_n_usage },
  781. #ifdef TRACE_FRAMES
  782. { { "show", "frame", "stats", NULL }, show_frame_stats, "Shows frame statistics", frame_stats_usage },
  783. #endif
  784. };
  785. int init_framer(void)
  786. {
  787. ast_cli_register_multiple(my_clis, sizeof(my_clis)/sizeof(my_clis[0]) );
  788. return 0;
  789. }
  790. void ast_codec_pref_convert(struct ast_codec_pref *pref, char *buf, size_t size, int right)
  791. {
  792. int x = 0, differential = (int) 'A', mem = 0;
  793. char *from = NULL, *to = NULL;
  794. if(right) {
  795. from = pref->order;
  796. to = buf;
  797. mem = size;
  798. } else {
  799. to = pref->order;
  800. from = buf;
  801. mem = 32;
  802. }
  803. memset(to, 0, mem);
  804. for (x = 0; x < 32 ; x++) {
  805. if(!from[x])
  806. break;
  807. to[x] = right ? (from[x] + differential) : (from[x] - differential);
  808. }
  809. }
  810. int ast_codec_pref_string(struct ast_codec_pref *pref, char *buf, size_t size)
  811. {
  812. int x = 0, codec = 0;
  813. size_t total_len = 0, slen = 0;
  814. char *formatname = 0;
  815. memset(buf,0,size);
  816. total_len = size;
  817. buf[0] = '(';
  818. total_len--;
  819. for(x = 0; x < 32 ; x++) {
  820. if(total_len <= 0)
  821. break;
  822. if(!(codec = ast_codec_pref_index(pref,x)))
  823. break;
  824. if((formatname = ast_getformatname(codec))) {
  825. slen = strlen(formatname);
  826. if(slen > total_len)
  827. break;
  828. strncat(buf,formatname,total_len);
  829. total_len -= slen;
  830. }
  831. if(total_len && x < 31 && ast_codec_pref_index(pref , x + 1)) {
  832. strncat(buf,"|",total_len);
  833. total_len--;
  834. }
  835. }
  836. if(total_len) {
  837. strncat(buf,")",total_len);
  838. total_len--;
  839. }
  840. return size - total_len;
  841. }
  842. int ast_codec_pref_index(struct ast_codec_pref *pref, int index)
  843. {
  844. int slot = 0;
  845. if((index >= 0) && (index < sizeof(pref->order))) {
  846. slot = pref->order[index];
  847. }
  848. return slot ? AST_FORMAT_LIST[slot-1].bits : 0;
  849. }
  850. /*! \brief ast_codec_pref_remove: Remove codec from pref list ---*/
  851. void ast_codec_pref_remove(struct ast_codec_pref *pref, int format)
  852. {
  853. struct ast_codec_pref oldorder;
  854. int x=0, y=0;
  855. size_t size = 0;
  856. int slot = 0;
  857. if(!pref->order[0])
  858. return;
  859. size = sizeof(AST_FORMAT_LIST) / sizeof(struct ast_format_list);
  860. memcpy(&oldorder,pref,sizeof(struct ast_codec_pref));
  861. memset(pref,0,sizeof(struct ast_codec_pref));
  862. for (x = 0; x < size; x++) {
  863. slot = oldorder.order[x];
  864. if(! slot)
  865. break;
  866. if(AST_FORMAT_LIST[slot-1].bits != format)
  867. pref->order[y++] = slot;
  868. }
  869. }
  870. /*! \brief ast_codec_pref_append: Append codec to list ---*/
  871. int ast_codec_pref_append(struct ast_codec_pref *pref, int format)
  872. {
  873. size_t size = 0;
  874. int x = 0, newindex = -1;
  875. ast_codec_pref_remove(pref, format);
  876. size = sizeof(AST_FORMAT_LIST) / sizeof(struct ast_format_list);
  877. for (x = 0; x < size; x++) {
  878. if(AST_FORMAT_LIST[x].bits == format) {
  879. newindex = x + 1;
  880. break;
  881. }
  882. }
  883. if(newindex) {
  884. for (x = 0; x < size; x++) {
  885. if(!pref->order[x]) {
  886. pref->order[x] = newindex;
  887. break;
  888. }
  889. }
  890. }
  891. return x;
  892. }
  893. /*! \brief ast_codec_choose: Pick a codec ---*/
  894. int ast_codec_choose(struct ast_codec_pref *pref, int formats, int find_best)
  895. {
  896. size_t size = 0;
  897. int x = 0, ret = 0, slot = 0;
  898. size = sizeof(AST_FORMAT_LIST) / sizeof(struct ast_format_list);
  899. for (x = 0; x < size; x++) {
  900. slot = pref->order[x];
  901. if(!slot)
  902. break;
  903. if ( formats & AST_FORMAT_LIST[slot-1].bits ) {
  904. ret = AST_FORMAT_LIST[slot-1].bits;
  905. break;
  906. }
  907. }
  908. if(ret)
  909. return ret;
  910. return find_best ? ast_best_codec(formats) : 0;
  911. }
  912. void ast_parse_allow_disallow(struct ast_codec_pref *pref, int *mask, const char *list, int allowing)
  913. {
  914. char *parse;
  915. char *this;
  916. int format;
  917. parse = ast_strdupa(list);
  918. while ((this = strsep(&parse, ","))) {
  919. if (!(format = ast_getformatbyname(this))) {
  920. ast_log(LOG_WARNING, "Cannot %s unknown format '%s'\n", allowing ? "allow" : "disallow", this);
  921. continue;
  922. }
  923. if (mask) {
  924. if (allowing)
  925. *mask |= format;
  926. else
  927. *mask &= ~format;
  928. }
  929. if (pref) {
  930. if (strcasecmp(this, "all")) {
  931. if (allowing)
  932. ast_codec_pref_append(pref, format);
  933. else
  934. ast_codec_pref_remove(pref, format);
  935. } else if (!allowing) {
  936. memset(pref, 0, sizeof(*pref));
  937. }
  938. }
  939. }
  940. }
  941. static int g723_len(unsigned char buf)
  942. {
  943. switch(buf & TYPE_MASK) {
  944. case TYPE_DONTSEND:
  945. return 0;
  946. break;
  947. case TYPE_SILENCE:
  948. return 4;
  949. break;
  950. case TYPE_HIGH:
  951. return 24;
  952. break;
  953. case TYPE_LOW:
  954. return 20;
  955. break;
  956. default:
  957. ast_log(LOG_WARNING, "Badly encoded frame (%d)\n", buf & TYPE_MASK);
  958. }
  959. return -1;
  960. }
  961. static int g723_samples(unsigned char *buf, int maxlen)
  962. {
  963. int pos = 0;
  964. int samples = 0;
  965. int res;
  966. while(pos < maxlen) {
  967. res = g723_len(buf[pos]);
  968. if (res <= 0)
  969. break;
  970. samples += 240;
  971. pos += res;
  972. }
  973. return samples;
  974. }
  975. static unsigned char get_n_bits_at(unsigned char *data, int n, int bit)
  976. {
  977. int byte = bit / 8; /* byte containing first bit */
  978. int rem = 8 - (bit % 8); /* remaining bits in first byte */
  979. unsigned char ret = 0;
  980. if (n <= 0 || n > 8)
  981. return 0;
  982. if (rem < n) {
  983. ret = (data[byte] << (n - rem));
  984. ret |= (data[byte + 1] >> (8 - n + rem));
  985. } else {
  986. ret = (data[byte] >> (rem - n));
  987. }
  988. return (ret & (0xff >> (8 - n)));
  989. }
  990. static int speex_get_wb_sz_at(unsigned char *data, int len, int bit)
  991. {
  992. static int SpeexWBSubModeSz[] = {
  993. 0, 36, 112, 192,
  994. 352, 0, 0, 0 };
  995. int off = bit;
  996. unsigned char c;
  997. /* skip up to two wideband frames */
  998. if (((len * 8 - off) >= 5) &&
  999. get_n_bits_at(data, 1, off)) {
  1000. c = get_n_bits_at(data, 3, off + 1);
  1001. off += SpeexWBSubModeSz[c];
  1002. if (((len * 8 - off) >= 5) &&
  1003. get_n_bits_at(data, 1, off)) {
  1004. c = get_n_bits_at(data, 3, off + 1);
  1005. off += SpeexWBSubModeSz[c];
  1006. if (((len * 8 - off) >= 5) &&
  1007. get_n_bits_at(data, 1, off)) {
  1008. ast_log(LOG_WARNING, "Encountered corrupt speex frame; too many wideband frames in a row.\n");
  1009. return -1;
  1010. }
  1011. }
  1012. }
  1013. return off - bit;
  1014. }
  1015. static int speex_samples(unsigned char *data, int len)
  1016. {
  1017. static int SpeexSubModeSz[] = {
  1018. 5, 43, 119, 160,
  1019. 220, 300, 364, 492,
  1020. 79, 0, 0, 0,
  1021. 0, 0, 0, 0 };
  1022. static int SpeexInBandSz[] = {
  1023. 1, 1, 4, 4,
  1024. 4, 4, 4, 4,
  1025. 8, 8, 16, 16,
  1026. 32, 32, 64, 64 };
  1027. int bit = 0;
  1028. int cnt = 0;
  1029. int off = 0;
  1030. unsigned char c;
  1031. while ((len * 8 - bit) >= 5) {
  1032. /* skip wideband frames */
  1033. off = speex_get_wb_sz_at(data, len, bit);
  1034. if (off < 0) {
  1035. ast_log(LOG_WARNING, "Had error while reading wideband frames for speex samples\n");
  1036. break;
  1037. }
  1038. bit += off;
  1039. if ((len * 8 - bit) < 5) {
  1040. ast_log(LOG_WARNING, "Not enough bits remaining after wide band for speex samples.\n");
  1041. break;
  1042. }
  1043. /* get control bits */
  1044. c = get_n_bits_at(data, 5, bit);
  1045. bit += 5;
  1046. if (c == 15) {
  1047. /* terminator */
  1048. break;
  1049. } else if (c == 14) {
  1050. /* in-band signal; next 4 bits contain signal id */
  1051. c = get_n_bits_at(data, 4, bit);
  1052. bit += 4;
  1053. bit += SpeexInBandSz[c];
  1054. } else if (c == 13) {
  1055. /* user in-band; next 5 bits contain msg len */
  1056. c = get_n_bits_at(data, 5, bit);
  1057. bit += 5;
  1058. bit += c * 8;
  1059. } else if (c > 8) {
  1060. /* unknown */
  1061. break;
  1062. } else {
  1063. /* skip number bits for submode (less the 5 control bits) */
  1064. bit += SpeexSubModeSz[c] - 5;
  1065. cnt += 160; /* new frame */
  1066. }
  1067. }
  1068. return cnt;
  1069. }
  1070. int ast_codec_get_samples(struct ast_frame *f)
  1071. {
  1072. int samples=0;
  1073. switch(f->subclass) {
  1074. case AST_FORMAT_SPEEX:
  1075. samples = speex_samples(f->data, f->datalen);
  1076. break;
  1077. case AST_FORMAT_G723_1:
  1078. samples = g723_samples(f->data, f->datalen);
  1079. break;
  1080. case AST_FORMAT_ILBC:
  1081. samples = 240 * (f->datalen / 50);
  1082. break;
  1083. case AST_FORMAT_GSM:
  1084. samples = 160 * (f->datalen / 33);
  1085. break;
  1086. case AST_FORMAT_G729A:
  1087. samples = f->datalen * 8;
  1088. break;
  1089. case AST_FORMAT_SLINEAR:
  1090. samples = f->datalen / 2;
  1091. break;
  1092. case AST_FORMAT_LPC10:
  1093. /* assumes that the RTP packet contains one LPC10 frame */
  1094. samples = 22 * 8;
  1095. samples += (((char *)(f->data))[7] & 0x1) * 8;
  1096. break;
  1097. case AST_FORMAT_ULAW:
  1098. case AST_FORMAT_ALAW:
  1099. samples = f->datalen;
  1100. break;
  1101. case AST_FORMAT_ADPCM:
  1102. case AST_FORMAT_G726:
  1103. samples = f->datalen * 2;
  1104. break;
  1105. default:
  1106. ast_log(LOG_WARNING, "Unable to calculate samples for format %s\n", ast_getformatname(f->subclass));
  1107. }
  1108. return samples;
  1109. }
  1110. int ast_codec_get_len(int format, int samples)
  1111. {
  1112. int len = 0;
  1113. /* XXX Still need speex, g723, and lpc10 XXX */
  1114. switch(format) {
  1115. case AST_FORMAT_ILBC:
  1116. len = (samples / 240) * 50;
  1117. break;
  1118. case AST_FORMAT_GSM:
  1119. len = (samples / 160) * 33;
  1120. break;
  1121. case AST_FORMAT_G729A:
  1122. len = samples / 8;
  1123. break;
  1124. case AST_FORMAT_SLINEAR:
  1125. len = samples * 2;
  1126. break;
  1127. case AST_FORMAT_ULAW:
  1128. case AST_FORMAT_ALAW:
  1129. len = samples;
  1130. break;
  1131. case AST_FORMAT_ADPCM:
  1132. case AST_FORMAT_G726:
  1133. len = samples / 2;
  1134. break;
  1135. default:
  1136. ast_log(LOG_WARNING, "Unable to calculate sample length for format %s\n", ast_getformatname(format));
  1137. }
  1138. return len;
  1139. }
  1140. int ast_frame_adjust_volume(struct ast_frame *f, int adjustment)
  1141. {
  1142. int count;
  1143. short *fdata = f->data;
  1144. short adjust_value = abs(adjustment);
  1145. if ((f->frametype != AST_FRAME_VOICE) || (f->subclass != AST_FORMAT_SLINEAR))
  1146. return -1;
  1147. if (!adjustment)
  1148. return 0;
  1149. for (count = 0; count < f->samples; count++) {
  1150. if (adjustment > 0) {
  1151. ast_slinear_saturated_multiply(&fdata[count], &adjust_value);
  1152. } else if (adjustment < 0) {
  1153. ast_slinear_saturated_divide(&fdata[count], &adjust_value);
  1154. }
  1155. }
  1156. return 0;
  1157. }
  1158. int ast_frame_slinear_sum(struct ast_frame *f1, struct ast_frame *f2)
  1159. {
  1160. int count;
  1161. short *data1, *data2;
  1162. if ((f1->frametype != AST_FRAME_VOICE) || (f1->subclass != AST_FORMAT_SLINEAR))
  1163. return -1;
  1164. if ((f2->frametype != AST_FRAME_VOICE) || (f2->subclass != AST_FORMAT_SLINEAR))
  1165. return -1;
  1166. if (f1->samples != f2->samples)
  1167. return -1;
  1168. for (count = 0, data1 = f1->data, data2 = f2->data;
  1169. count < f1->samples;
  1170. count++, data1++, data2++)
  1171. ast_slinear_saturated_add(data1, data2);
  1172. return 0;
  1173. }