frame.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  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 and codec manipulation routines
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/_private.h"
  30. #include "asterisk/lock.h"
  31. #include "asterisk/frame.h"
  32. #include "asterisk/channel.h"
  33. #include "asterisk/cli.h"
  34. #include "asterisk/term.h"
  35. #include "asterisk/utils.h"
  36. #include "asterisk/threadstorage.h"
  37. #include "asterisk/linkedlists.h"
  38. #include "asterisk/translate.h"
  39. #include "asterisk/dsp.h"
  40. #include "asterisk/file.h"
  41. #if !defined(LOW_MEMORY)
  42. static void frame_cache_cleanup(void *data);
  43. /*! \brief A per-thread cache of frame headers */
  44. AST_THREADSTORAGE_CUSTOM(frame_cache, NULL, frame_cache_cleanup);
  45. /*!
  46. * \brief Maximum ast_frame cache size
  47. *
  48. * In most cases where the frame header cache will be useful, the size
  49. * of the cache will stay very small. However, it is not always the case that
  50. * the same thread that allocates the frame will be the one freeing them, so
  51. * sometimes a thread will never have any frames in its cache, or the cache
  52. * will never be pulled from. For the latter case, we limit the maximum size.
  53. */
  54. #define FRAME_CACHE_MAX_SIZE 10
  55. /*! \brief This is just so ast_frames, a list head struct for holding a list of
  56. * ast_frame structures, is defined. */
  57. AST_LIST_HEAD_NOLOCK(ast_frames, ast_frame);
  58. struct ast_frame_cache {
  59. struct ast_frames list;
  60. size_t size;
  61. };
  62. #endif
  63. #define SMOOTHER_SIZE 8000
  64. enum frame_type {
  65. TYPE_HIGH, /* 0x0 */
  66. TYPE_LOW, /* 0x1 */
  67. TYPE_SILENCE, /* 0x2 */
  68. TYPE_DONTSEND /* 0x3 */
  69. };
  70. #define TYPE_MASK 0x3
  71. struct ast_smoother {
  72. int size;
  73. struct ast_format format;
  74. int flags;
  75. float samplesperbyte;
  76. unsigned int opt_needs_swap:1;
  77. struct ast_frame f;
  78. struct timeval delivery;
  79. char data[SMOOTHER_SIZE];
  80. char framedata[SMOOTHER_SIZE + AST_FRIENDLY_OFFSET];
  81. struct ast_frame *opt;
  82. int len;
  83. };
  84. struct ast_frame ast_null_frame = { AST_FRAME_NULL, };
  85. static int smoother_frame_feed(struct ast_smoother *s, struct ast_frame *f, int swap)
  86. {
  87. if (s->flags & AST_SMOOTHER_FLAG_G729) {
  88. if (s->len % 10) {
  89. ast_log(LOG_NOTICE, "Dropping extra frame of G.729 since we already have a VAD frame at the end\n");
  90. return 0;
  91. }
  92. }
  93. if (swap) {
  94. ast_swapcopy_samples(s->data + s->len, f->data.ptr, f->samples);
  95. } else {
  96. memcpy(s->data + s->len, f->data.ptr, f->datalen);
  97. }
  98. /* If either side is empty, reset the delivery time */
  99. if (!s->len || ast_tvzero(f->delivery) || ast_tvzero(s->delivery)) { /* XXX really ? */
  100. s->delivery = f->delivery;
  101. }
  102. s->len += f->datalen;
  103. return 0;
  104. }
  105. void ast_smoother_reset(struct ast_smoother *s, int bytes)
  106. {
  107. memset(s, 0, sizeof(*s));
  108. s->size = bytes;
  109. }
  110. void ast_smoother_reconfigure(struct ast_smoother *s, int bytes)
  111. {
  112. /* if there is no change, then nothing to do */
  113. if (s->size == bytes) {
  114. return;
  115. }
  116. /* set the new desired output size */
  117. s->size = bytes;
  118. /* if there is no 'optimized' frame in the smoother,
  119. * then there is nothing left to do
  120. */
  121. if (!s->opt) {
  122. return;
  123. }
  124. /* there is an 'optimized' frame here at the old size,
  125. * but it must now be put into the buffer so the data
  126. * can be extracted at the new size
  127. */
  128. smoother_frame_feed(s, s->opt, s->opt_needs_swap);
  129. s->opt = NULL;
  130. }
  131. struct ast_smoother *ast_smoother_new(int size)
  132. {
  133. struct ast_smoother *s;
  134. if (size < 1)
  135. return NULL;
  136. if ((s = ast_malloc(sizeof(*s))))
  137. ast_smoother_reset(s, size);
  138. return s;
  139. }
  140. int ast_smoother_get_flags(struct ast_smoother *s)
  141. {
  142. return s->flags;
  143. }
  144. void ast_smoother_set_flags(struct ast_smoother *s, int flags)
  145. {
  146. s->flags = flags;
  147. }
  148. int ast_smoother_test_flag(struct ast_smoother *s, int flag)
  149. {
  150. return (s->flags & flag);
  151. }
  152. int __ast_smoother_feed(struct ast_smoother *s, struct ast_frame *f, int swap)
  153. {
  154. if (f->frametype != AST_FRAME_VOICE) {
  155. ast_log(LOG_WARNING, "Huh? Can't smooth a non-voice frame!\n");
  156. return -1;
  157. }
  158. if (!s->format.id) {
  159. ast_format_copy(&s->format, &f->subclass.format);
  160. s->samplesperbyte = (float)f->samples / (float)f->datalen;
  161. } else if (ast_format_cmp(&s->format, &f->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
  162. ast_log(LOG_WARNING, "Smoother was working on %s format frames, now trying to feed %s?\n",
  163. ast_getformatname(&s->format), ast_getformatname(&f->subclass.format));
  164. return -1;
  165. }
  166. if (s->len + f->datalen > SMOOTHER_SIZE) {
  167. ast_log(LOG_WARNING, "Out of smoother space\n");
  168. return -1;
  169. }
  170. if (((f->datalen == s->size) ||
  171. ((f->datalen < 10) && (s->flags & AST_SMOOTHER_FLAG_G729))) &&
  172. !s->opt &&
  173. !s->len &&
  174. (f->offset >= AST_MIN_OFFSET)) {
  175. /* Optimize by sending the frame we just got
  176. on the next read, thus eliminating the douple
  177. copy */
  178. if (swap)
  179. ast_swapcopy_samples(f->data.ptr, f->data.ptr, f->samples);
  180. s->opt = f;
  181. s->opt_needs_swap = swap ? 1 : 0;
  182. return 0;
  183. }
  184. return smoother_frame_feed(s, f, swap);
  185. }
  186. struct ast_frame *ast_smoother_read(struct ast_smoother *s)
  187. {
  188. struct ast_frame *opt;
  189. int len;
  190. /* IF we have an optimization frame, send it */
  191. if (s->opt) {
  192. if (s->opt->offset < AST_FRIENDLY_OFFSET)
  193. ast_log(LOG_WARNING, "Returning a frame of inappropriate offset (%d).\n",
  194. s->opt->offset);
  195. opt = s->opt;
  196. s->opt = NULL;
  197. return opt;
  198. }
  199. /* Make sure we have enough data */
  200. if (s->len < s->size) {
  201. /* Or, if this is a G.729 frame with VAD on it, send it immediately anyway */
  202. if (!((s->flags & AST_SMOOTHER_FLAG_G729) && (s->len % 10)))
  203. return NULL;
  204. }
  205. len = s->size;
  206. if (len > s->len)
  207. len = s->len;
  208. /* Make frame */
  209. s->f.frametype = AST_FRAME_VOICE;
  210. ast_format_copy(&s->f.subclass.format, &s->format);
  211. s->f.data.ptr = s->framedata + AST_FRIENDLY_OFFSET;
  212. s->f.offset = AST_FRIENDLY_OFFSET;
  213. s->f.datalen = len;
  214. /* Samples will be improper given VAD, but with VAD the concept really doesn't even exist */
  215. s->f.samples = len * s->samplesperbyte; /* XXX rounding */
  216. s->f.delivery = s->delivery;
  217. /* Fill Data */
  218. memcpy(s->f.data.ptr, s->data, len);
  219. s->len -= len;
  220. /* Move remaining data to the front if applicable */
  221. if (s->len) {
  222. /* In principle this should all be fine because if we are sending
  223. G.729 VAD, the next timestamp will take over anyawy */
  224. memmove(s->data, s->data + len, s->len);
  225. if (!ast_tvzero(s->delivery)) {
  226. /* If we have delivery time, increment it, otherwise, leave it at 0 */
  227. s->delivery = ast_tvadd(s->delivery, ast_samp2tv(s->f.samples, ast_format_rate(&s->format)));
  228. }
  229. }
  230. /* Return frame */
  231. return &s->f;
  232. }
  233. void ast_smoother_free(struct ast_smoother *s)
  234. {
  235. ast_free(s);
  236. }
  237. static struct ast_frame *ast_frame_header_new(void)
  238. {
  239. struct ast_frame *f;
  240. #if !defined(LOW_MEMORY)
  241. struct ast_frame_cache *frames;
  242. if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) {
  243. if ((f = AST_LIST_REMOVE_HEAD(&frames->list, frame_list))) {
  244. size_t mallocd_len = f->mallocd_hdr_len;
  245. memset(f, 0, sizeof(*f));
  246. f->mallocd_hdr_len = mallocd_len;
  247. f->mallocd = AST_MALLOCD_HDR;
  248. frames->size--;
  249. return f;
  250. }
  251. }
  252. if (!(f = ast_calloc_cache(1, sizeof(*f))))
  253. return NULL;
  254. #else
  255. if (!(f = ast_calloc(1, sizeof(*f))))
  256. return NULL;
  257. #endif
  258. f->mallocd_hdr_len = sizeof(*f);
  259. return f;
  260. }
  261. #if !defined(LOW_MEMORY)
  262. static void frame_cache_cleanup(void *data)
  263. {
  264. struct ast_frame_cache *frames = data;
  265. struct ast_frame *f;
  266. while ((f = AST_LIST_REMOVE_HEAD(&frames->list, frame_list)))
  267. ast_free(f);
  268. ast_free(frames);
  269. }
  270. #endif
  271. static void __frame_free(struct ast_frame *fr, int cache)
  272. {
  273. if (!fr->mallocd)
  274. return;
  275. #if !defined(LOW_MEMORY)
  276. if (cache && fr->mallocd == AST_MALLOCD_HDR) {
  277. /* Cool, only the header is malloc'd, let's just cache those for now
  278. * to keep things simple... */
  279. struct ast_frame_cache *frames;
  280. if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames))) &&
  281. (frames->size < FRAME_CACHE_MAX_SIZE)) {
  282. AST_LIST_INSERT_HEAD(&frames->list, fr, frame_list);
  283. frames->size++;
  284. return;
  285. }
  286. }
  287. #endif
  288. if (fr->mallocd & AST_MALLOCD_DATA) {
  289. if (fr->data.ptr)
  290. ast_free(fr->data.ptr - fr->offset);
  291. }
  292. if (fr->mallocd & AST_MALLOCD_SRC) {
  293. if (fr->src)
  294. ast_free((void *) fr->src);
  295. }
  296. if (fr->mallocd & AST_MALLOCD_HDR) {
  297. ast_free(fr);
  298. }
  299. }
  300. void ast_frame_free(struct ast_frame *frame, int cache)
  301. {
  302. struct ast_frame *next;
  303. for (next = AST_LIST_NEXT(frame, frame_list);
  304. frame;
  305. frame = next, next = frame ? AST_LIST_NEXT(frame, frame_list) : NULL) {
  306. __frame_free(frame, cache);
  307. }
  308. }
  309. void ast_frame_dtor(struct ast_frame *f)
  310. {
  311. if (f) {
  312. ast_frfree(f);
  313. }
  314. }
  315. /*!
  316. * \brief 'isolates' a frame by duplicating non-malloc'ed components
  317. * (header, src, data).
  318. * On return all components are malloc'ed
  319. */
  320. struct ast_frame *ast_frisolate(struct ast_frame *fr)
  321. {
  322. struct ast_frame *out;
  323. void *newdata;
  324. /* if none of the existing frame is malloc'd, let ast_frdup() do it
  325. since it is more efficient
  326. */
  327. if (fr->mallocd == 0) {
  328. return ast_frdup(fr);
  329. }
  330. /* if everything is already malloc'd, we are done */
  331. if ((fr->mallocd & (AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA)) ==
  332. (AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA)) {
  333. return fr;
  334. }
  335. if (!(fr->mallocd & AST_MALLOCD_HDR)) {
  336. /* Allocate a new header if needed */
  337. if (!(out = ast_frame_header_new())) {
  338. return NULL;
  339. }
  340. out->frametype = fr->frametype;
  341. ast_format_copy(&out->subclass.format, &fr->subclass.format);
  342. out->datalen = fr->datalen;
  343. out->samples = fr->samples;
  344. out->offset = fr->offset;
  345. /* Copy the timing data */
  346. ast_copy_flags(out, fr, AST_FLAGS_ALL);
  347. if (ast_test_flag(fr, AST_FRFLAG_HAS_TIMING_INFO)) {
  348. out->ts = fr->ts;
  349. out->len = fr->len;
  350. out->seqno = fr->seqno;
  351. }
  352. } else {
  353. out = fr;
  354. }
  355. if (!(fr->mallocd & AST_MALLOCD_SRC) && fr->src) {
  356. if (!(out->src = ast_strdup(fr->src))) {
  357. if (out != fr) {
  358. ast_free(out);
  359. }
  360. return NULL;
  361. }
  362. } else {
  363. out->src = fr->src;
  364. fr->src = NULL;
  365. fr->mallocd &= ~AST_MALLOCD_SRC;
  366. }
  367. if (!(fr->mallocd & AST_MALLOCD_DATA)) {
  368. if (!fr->datalen) {
  369. out->data.uint32 = fr->data.uint32;
  370. out->mallocd = AST_MALLOCD_HDR | AST_MALLOCD_SRC;
  371. return out;
  372. }
  373. if (!(newdata = ast_malloc(fr->datalen + AST_FRIENDLY_OFFSET))) {
  374. if (out->src != fr->src) {
  375. ast_free((void *) out->src);
  376. }
  377. if (out != fr) {
  378. ast_free(out);
  379. }
  380. return NULL;
  381. }
  382. newdata += AST_FRIENDLY_OFFSET;
  383. out->offset = AST_FRIENDLY_OFFSET;
  384. out->datalen = fr->datalen;
  385. memcpy(newdata, fr->data.ptr, fr->datalen);
  386. out->data.ptr = newdata;
  387. } else {
  388. out->data = fr->data;
  389. memset(&fr->data, 0, sizeof(fr->data));
  390. fr->mallocd &= ~AST_MALLOCD_DATA;
  391. }
  392. out->mallocd = AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA;
  393. return out;
  394. }
  395. struct ast_frame *ast_frdup(const struct ast_frame *f)
  396. {
  397. struct ast_frame *out = NULL;
  398. int len, srclen = 0;
  399. void *buf = NULL;
  400. #if !defined(LOW_MEMORY)
  401. struct ast_frame_cache *frames;
  402. #endif
  403. /* Start with standard stuff */
  404. len = sizeof(*out) + AST_FRIENDLY_OFFSET + f->datalen;
  405. /* If we have a source, add space for it */
  406. /*
  407. * XXX Watch out here - if we receive a src which is not terminated
  408. * properly, we can be easily attacked. Should limit the size we deal with.
  409. */
  410. if (f->src)
  411. srclen = strlen(f->src);
  412. if (srclen > 0)
  413. len += srclen + 1;
  414. #if !defined(LOW_MEMORY)
  415. if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) {
  416. AST_LIST_TRAVERSE_SAFE_BEGIN(&frames->list, out, frame_list) {
  417. if (out->mallocd_hdr_len >= len) {
  418. size_t mallocd_len = out->mallocd_hdr_len;
  419. AST_LIST_REMOVE_CURRENT(frame_list);
  420. memset(out, 0, sizeof(*out));
  421. out->mallocd_hdr_len = mallocd_len;
  422. buf = out;
  423. frames->size--;
  424. break;
  425. }
  426. }
  427. AST_LIST_TRAVERSE_SAFE_END;
  428. }
  429. #endif
  430. if (!buf) {
  431. if (!(buf = ast_calloc_cache(1, len)))
  432. return NULL;
  433. out = buf;
  434. out->mallocd_hdr_len = len;
  435. }
  436. out->frametype = f->frametype;
  437. ast_format_copy(&out->subclass.format, &f->subclass.format);
  438. out->datalen = f->datalen;
  439. out->samples = f->samples;
  440. out->delivery = f->delivery;
  441. /* Even though this new frame was allocated from the heap, we can't mark it
  442. * with AST_MALLOCD_HDR, AST_MALLOCD_DATA and AST_MALLOCD_SRC, because that
  443. * would cause ast_frfree() to attempt to individually free each of those
  444. * under the assumption that they were separately allocated. Since this frame
  445. * was allocated in a single allocation, we'll only mark it as if the header
  446. * was heap-allocated; this will result in the entire frame being properly freed.
  447. */
  448. out->mallocd = AST_MALLOCD_HDR;
  449. out->offset = AST_FRIENDLY_OFFSET;
  450. if (out->datalen) {
  451. out->data.ptr = buf + sizeof(*out) + AST_FRIENDLY_OFFSET;
  452. memcpy(out->data.ptr, f->data.ptr, out->datalen);
  453. } else {
  454. out->data.uint32 = f->data.uint32;
  455. }
  456. if (srclen > 0) {
  457. /* This may seem a little strange, but it's to avoid a gcc (4.2.4) compiler warning */
  458. char *src;
  459. out->src = buf + sizeof(*out) + AST_FRIENDLY_OFFSET + f->datalen;
  460. src = (char *) out->src;
  461. /* Must have space since we allocated for it */
  462. strcpy(src, f->src);
  463. }
  464. ast_copy_flags(out, f, AST_FLAGS_ALL);
  465. out->ts = f->ts;
  466. out->len = f->len;
  467. out->seqno = f->seqno;
  468. return out;
  469. }
  470. void ast_swapcopy_samples(void *dst, const void *src, int samples)
  471. {
  472. int i;
  473. unsigned short *dst_s = dst;
  474. const unsigned short *src_s = src;
  475. for (i = 0; i < samples; i++)
  476. dst_s[i] = (src_s[i]<<8) | (src_s[i]>>8);
  477. }
  478. void ast_frame_subclass2str(struct ast_frame *f, char *subclass, size_t slen, char *moreinfo, size_t mlen)
  479. {
  480. switch(f->frametype) {
  481. case AST_FRAME_DTMF_BEGIN:
  482. if (slen > 1) {
  483. subclass[0] = f->subclass.integer;
  484. subclass[1] = '\0';
  485. }
  486. break;
  487. case AST_FRAME_DTMF_END:
  488. if (slen > 1) {
  489. subclass[0] = f->subclass.integer;
  490. subclass[1] = '\0';
  491. }
  492. break;
  493. case AST_FRAME_CONTROL:
  494. switch (f->subclass.integer) {
  495. case AST_CONTROL_HANGUP:
  496. ast_copy_string(subclass, "Hangup", slen);
  497. break;
  498. case AST_CONTROL_RING:
  499. ast_copy_string(subclass, "Ring", slen);
  500. break;
  501. case AST_CONTROL_RINGING:
  502. ast_copy_string(subclass, "Ringing", slen);
  503. break;
  504. case AST_CONTROL_ANSWER:
  505. ast_copy_string(subclass, "Answer", slen);
  506. break;
  507. case AST_CONTROL_BUSY:
  508. ast_copy_string(subclass, "Busy", slen);
  509. break;
  510. case AST_CONTROL_TAKEOFFHOOK:
  511. ast_copy_string(subclass, "Take Off Hook", slen);
  512. break;
  513. case AST_CONTROL_OFFHOOK:
  514. ast_copy_string(subclass, "Line Off Hook", slen);
  515. break;
  516. case AST_CONTROL_CONGESTION:
  517. ast_copy_string(subclass, "Congestion", slen);
  518. break;
  519. case AST_CONTROL_FLASH:
  520. ast_copy_string(subclass, "Flash", slen);
  521. break;
  522. case AST_CONTROL_WINK:
  523. ast_copy_string(subclass, "Wink", slen);
  524. break;
  525. case AST_CONTROL_OPTION:
  526. ast_copy_string(subclass, "Option", slen);
  527. break;
  528. case AST_CONTROL_RADIO_KEY:
  529. ast_copy_string(subclass, "Key Radio", slen);
  530. break;
  531. case AST_CONTROL_RADIO_UNKEY:
  532. ast_copy_string(subclass, "Unkey Radio", slen);
  533. break;
  534. case AST_CONTROL_HOLD:
  535. ast_copy_string(subclass, "Hold", slen);
  536. break;
  537. case AST_CONTROL_UNHOLD:
  538. ast_copy_string(subclass, "Unhold", slen);
  539. break;
  540. case AST_CONTROL_T38_PARAMETERS: {
  541. char *message = "Unknown";
  542. if (f->datalen != sizeof(struct ast_control_t38_parameters)) {
  543. message = "Invalid";
  544. } else {
  545. struct ast_control_t38_parameters *parameters = f->data.ptr;
  546. enum ast_control_t38 state = parameters->request_response;
  547. if (state == AST_T38_REQUEST_NEGOTIATE)
  548. message = "Negotiation Requested";
  549. else if (state == AST_T38_REQUEST_TERMINATE)
  550. message = "Negotiation Request Terminated";
  551. else if (state == AST_T38_NEGOTIATED)
  552. message = "Negotiated";
  553. else if (state == AST_T38_TERMINATED)
  554. message = "Terminated";
  555. else if (state == AST_T38_REFUSED)
  556. message = "Refused";
  557. }
  558. snprintf(subclass, slen, "T38_Parameters/%s", message);
  559. break;
  560. }
  561. case -1:
  562. ast_copy_string(subclass, "Stop generators", slen);
  563. break;
  564. default:
  565. snprintf(subclass, slen, "Unknown control '%d'", f->subclass.integer);
  566. }
  567. break;
  568. case AST_FRAME_NULL:
  569. ast_copy_string(subclass, "N/A", slen);
  570. break;
  571. case AST_FRAME_IAX:
  572. /* Should never happen */
  573. snprintf(subclass, slen, "IAX Frametype %d", f->subclass.integer);
  574. break;
  575. case AST_FRAME_BRIDGE_ACTION:
  576. /* Should never happen */
  577. snprintf(subclass, slen, "Bridge Frametype %d", f->subclass.integer);
  578. break;
  579. case AST_FRAME_BRIDGE_ACTION_SYNC:
  580. /* Should never happen */
  581. snprintf(subclass, slen, "Synchronous Bridge Frametype %d", f->subclass.integer);
  582. break;
  583. case AST_FRAME_TEXT:
  584. ast_copy_string(subclass, "N/A", slen);
  585. if (moreinfo) {
  586. ast_copy_string(moreinfo, f->data.ptr, mlen);
  587. }
  588. break;
  589. case AST_FRAME_IMAGE:
  590. snprintf(subclass, slen, "Image format %s\n", ast_getformatname(&f->subclass.format));
  591. break;
  592. case AST_FRAME_HTML:
  593. switch (f->subclass.integer) {
  594. case AST_HTML_URL:
  595. ast_copy_string(subclass, "URL", slen);
  596. if (moreinfo) {
  597. ast_copy_string(moreinfo, f->data.ptr, mlen);
  598. }
  599. break;
  600. case AST_HTML_DATA:
  601. ast_copy_string(subclass, "Data", slen);
  602. break;
  603. case AST_HTML_BEGIN:
  604. ast_copy_string(subclass, "Begin", slen);
  605. break;
  606. case AST_HTML_END:
  607. ast_copy_string(subclass, "End", slen);
  608. break;
  609. case AST_HTML_LDCOMPLETE:
  610. ast_copy_string(subclass, "Load Complete", slen);
  611. break;
  612. case AST_HTML_NOSUPPORT:
  613. ast_copy_string(subclass, "No Support", slen);
  614. break;
  615. case AST_HTML_LINKURL:
  616. ast_copy_string(subclass, "Link URL", slen);
  617. if (moreinfo) {
  618. ast_copy_string(moreinfo, f->data.ptr, mlen);
  619. }
  620. break;
  621. case AST_HTML_UNLINK:
  622. ast_copy_string(subclass, "Unlink", slen);
  623. break;
  624. case AST_HTML_LINKREJECT:
  625. ast_copy_string(subclass, "Link Reject", slen);
  626. break;
  627. default:
  628. snprintf(subclass, slen, "Unknown HTML frame '%d'\n", f->subclass.integer);
  629. break;
  630. }
  631. break;
  632. case AST_FRAME_MODEM:
  633. switch (f->subclass.integer) {
  634. case AST_MODEM_T38:
  635. ast_copy_string(subclass, "T.38", slen);
  636. break;
  637. case AST_MODEM_V150:
  638. ast_copy_string(subclass, "V.150", slen);
  639. break;
  640. default:
  641. snprintf(subclass, slen, "Unknown MODEM frame '%d'\n", f->subclass.integer);
  642. break;
  643. }
  644. break;
  645. default:
  646. ast_copy_string(subclass, "Unknown Subclass", slen);
  647. break;
  648. }
  649. }
  650. void ast_frame_type2str(enum ast_frame_type frame_type, char *ftype, size_t len)
  651. {
  652. switch (frame_type) {
  653. case AST_FRAME_DTMF_BEGIN:
  654. ast_copy_string(ftype, "DTMF Begin", len);
  655. break;
  656. case AST_FRAME_DTMF_END:
  657. ast_copy_string(ftype, "DTMF End", len);
  658. break;
  659. case AST_FRAME_CONTROL:
  660. ast_copy_string(ftype, "Control", len);
  661. break;
  662. case AST_FRAME_NULL:
  663. ast_copy_string(ftype, "Null Frame", len);
  664. break;
  665. case AST_FRAME_IAX:
  666. /* Should never happen */
  667. ast_copy_string(ftype, "IAX Specific", len);
  668. break;
  669. case AST_FRAME_BRIDGE_ACTION:
  670. /* Should never happen */
  671. ast_copy_string(ftype, "Bridge Specific", len);
  672. break;
  673. case AST_FRAME_BRIDGE_ACTION_SYNC:
  674. /* Should never happen */
  675. ast_copy_string(ftype, "Bridge Specific", len);
  676. break;
  677. case AST_FRAME_TEXT:
  678. ast_copy_string(ftype, "Text", len);
  679. break;
  680. case AST_FRAME_IMAGE:
  681. ast_copy_string(ftype, "Image", len);
  682. break;
  683. case AST_FRAME_HTML:
  684. ast_copy_string(ftype, "HTML", len);
  685. break;
  686. case AST_FRAME_MODEM:
  687. ast_copy_string(ftype, "Modem", len);
  688. break;
  689. case AST_FRAME_VOICE:
  690. ast_copy_string(ftype, "Voice", len);
  691. break;
  692. case AST_FRAME_VIDEO:
  693. ast_copy_string(ftype, "Video", len);
  694. break;
  695. default:
  696. snprintf(ftype, len, "Unknown Frametype '%d'", frame_type);
  697. break;
  698. }
  699. }
  700. /*! Dump a frame for debugging purposes */
  701. void ast_frame_dump(const char *name, struct ast_frame *f, char *prefix)
  702. {
  703. const char noname[] = "unknown";
  704. char ftype[40] = "Unknown Frametype";
  705. char cft[80];
  706. char subclass[40] = "Unknown Subclass";
  707. char csub[80];
  708. char moreinfo[40] = "";
  709. char cn[60];
  710. char cp[40];
  711. char cmn[40];
  712. if (!name) {
  713. name = noname;
  714. }
  715. if (!f) {
  716. ast_verb(-1, "%s [ %s (NULL) ] [%s]\n",
  717. term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
  718. term_color(cft, "HANGUP", COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
  719. term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
  720. return;
  721. }
  722. /* XXX We should probably print one each of voice and video when the format changes XXX */
  723. if (f->frametype == AST_FRAME_VOICE) {
  724. return;
  725. }
  726. if (f->frametype == AST_FRAME_VIDEO) {
  727. return;
  728. }
  729. ast_frame_type2str(f->frametype, ftype, sizeof(ftype));
  730. ast_frame_subclass2str(f, subclass, sizeof(subclass), moreinfo, sizeof(moreinfo));
  731. if (!ast_strlen_zero(moreinfo))
  732. ast_verb(-1, "%s [ TYPE: %s (%d) SUBCLASS: %s (%d) '%s' ] [%s]\n",
  733. term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
  734. term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
  735. f->frametype,
  736. term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
  737. f->subclass.integer,
  738. term_color(cmn, moreinfo, COLOR_BRGREEN, COLOR_BLACK, sizeof(cmn)),
  739. term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
  740. else
  741. ast_verb(-1, "%s [ TYPE: %s (%d) SUBCLASS: %s (%d) ] [%s]\n",
  742. term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
  743. term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
  744. f->frametype,
  745. term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
  746. f->subclass.integer,
  747. term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
  748. }
  749. int ast_parse_allow_disallow(struct ast_codec_pref *pref, struct ast_format_cap *cap, const char *list, int allowing)
  750. {
  751. int errors = 0, framems = 0, all = 0, iter_allowing;
  752. char *parse = NULL, *this = NULL, *psize = NULL;
  753. struct ast_format format;
  754. parse = ast_strdupa(list);
  755. while ((this = strsep(&parse, ","))) {
  756. iter_allowing = allowing;
  757. framems = 0;
  758. if (*this == '!') {
  759. this++;
  760. iter_allowing = !allowing;
  761. }
  762. if ((psize = strrchr(this, ':'))) {
  763. *psize++ = '\0';
  764. ast_debug(1, "Packetization for codec: %s is %s\n", this, psize);
  765. framems = atoi(psize);
  766. if (framems < 0) {
  767. framems = 0;
  768. errors++;
  769. ast_log(LOG_WARNING, "Bad packetization value for codec %s\n", this);
  770. }
  771. }
  772. all = strcasecmp(this, "all") ? 0 : 1;
  773. if (!all && !ast_getformatbyname(this, &format)) {
  774. ast_log(LOG_WARNING, "Cannot %s unknown format '%s'\n", iter_allowing ? "allow" : "disallow", this);
  775. errors++;
  776. continue;
  777. }
  778. if (cap) {
  779. if (iter_allowing) {
  780. if (all) {
  781. ast_format_cap_add_all(cap);
  782. } else {
  783. ast_format_cap_add(cap, &format);
  784. }
  785. } else {
  786. if (all) {
  787. ast_format_cap_remove_all(cap);
  788. } else {
  789. ast_format_cap_remove(cap, &format);
  790. }
  791. }
  792. }
  793. if (pref) {
  794. if (!all) {
  795. if (iter_allowing) {
  796. ast_codec_pref_append(pref, &format);
  797. ast_codec_pref_setsize(pref, &format, framems);
  798. } else {
  799. ast_codec_pref_remove(pref, &format);
  800. }
  801. } else if (!iter_allowing) {
  802. memset(pref, 0, sizeof(*pref));
  803. } else {
  804. ast_codec_pref_append_all(pref);
  805. }
  806. }
  807. }
  808. return errors;
  809. }
  810. static int g723_len(unsigned char buf)
  811. {
  812. enum frame_type type = buf & TYPE_MASK;
  813. switch(type) {
  814. case TYPE_DONTSEND:
  815. return 0;
  816. break;
  817. case TYPE_SILENCE:
  818. return 4;
  819. break;
  820. case TYPE_HIGH:
  821. return 24;
  822. break;
  823. case TYPE_LOW:
  824. return 20;
  825. break;
  826. default:
  827. ast_log(LOG_WARNING, "Badly encoded frame (%d)\n", type);
  828. }
  829. return -1;
  830. }
  831. static int g723_samples(unsigned char *buf, int maxlen)
  832. {
  833. int pos = 0;
  834. int samples = 0;
  835. int res;
  836. while(pos < maxlen) {
  837. res = g723_len(buf[pos]);
  838. if (res <= 0)
  839. break;
  840. samples += 240;
  841. pos += res;
  842. }
  843. return samples;
  844. }
  845. static unsigned char get_n_bits_at(unsigned char *data, int n, int bit)
  846. {
  847. int byte = bit / 8; /* byte containing first bit */
  848. int rem = 8 - (bit % 8); /* remaining bits in first byte */
  849. unsigned char ret = 0;
  850. if (n <= 0 || n > 8)
  851. return 0;
  852. if (rem < n) {
  853. ret = (data[byte] << (n - rem));
  854. ret |= (data[byte + 1] >> (8 - n + rem));
  855. } else {
  856. ret = (data[byte] >> (rem - n));
  857. }
  858. return (ret & (0xff >> (8 - n)));
  859. }
  860. static int speex_get_wb_sz_at(unsigned char *data, int len, int bit)
  861. {
  862. static const int SpeexWBSubModeSz[] = {
  863. 4, 36, 112, 192,
  864. 352, 0, 0, 0 };
  865. int off = bit;
  866. unsigned char c;
  867. /* skip up to two wideband frames */
  868. if (((len * 8 - off) >= 5) &&
  869. get_n_bits_at(data, 1, off)) {
  870. c = get_n_bits_at(data, 3, off + 1);
  871. off += SpeexWBSubModeSz[c];
  872. if (((len * 8 - off) >= 5) &&
  873. get_n_bits_at(data, 1, off)) {
  874. c = get_n_bits_at(data, 3, off + 1);
  875. off += SpeexWBSubModeSz[c];
  876. if (((len * 8 - off) >= 5) &&
  877. get_n_bits_at(data, 1, off)) {
  878. ast_log(LOG_WARNING, "Encountered corrupt speex frame; too many wideband frames in a row.\n");
  879. return -1;
  880. }
  881. }
  882. }
  883. return off - bit;
  884. }
  885. static int speex_samples(unsigned char *data, int len)
  886. {
  887. static const int SpeexSubModeSz[] = {
  888. 5, 43, 119, 160,
  889. 220, 300, 364, 492,
  890. 79, 0, 0, 0,
  891. 0, 0, 0, 0 };
  892. static const int SpeexInBandSz[] = {
  893. 1, 1, 4, 4,
  894. 4, 4, 4, 4,
  895. 8, 8, 16, 16,
  896. 32, 32, 64, 64 };
  897. int bit = 0;
  898. int cnt = 0;
  899. int off;
  900. unsigned char c;
  901. while ((len * 8 - bit) >= 5) {
  902. /* skip wideband frames */
  903. off = speex_get_wb_sz_at(data, len, bit);
  904. if (off < 0) {
  905. ast_log(LOG_WARNING, "Had error while reading wideband frames for speex samples\n");
  906. break;
  907. }
  908. bit += off;
  909. if ((len * 8 - bit) < 5)
  910. break;
  911. /* get control bits */
  912. c = get_n_bits_at(data, 5, bit);
  913. bit += 5;
  914. if (c == 15) {
  915. /* terminator */
  916. break;
  917. } else if (c == 14) {
  918. /* in-band signal; next 4 bits contain signal id */
  919. c = get_n_bits_at(data, 4, bit);
  920. bit += 4;
  921. bit += SpeexInBandSz[c];
  922. } else if (c == 13) {
  923. /* user in-band; next 4 bits contain msg len */
  924. c = get_n_bits_at(data, 4, bit);
  925. bit += 4;
  926. /* after which it's 5-bit signal id + c bytes of data */
  927. bit += 5 + c * 8;
  928. } else if (c > 8) {
  929. /* unknown */
  930. ast_log(LOG_WARNING, "Unknown speex control frame %d\n", c);
  931. break;
  932. } else {
  933. /* skip number bits for submode (less the 5 control bits) */
  934. bit += SpeexSubModeSz[c] - 5;
  935. cnt += 160; /* new frame */
  936. }
  937. }
  938. return cnt;
  939. }
  940. int ast_codec_get_samples(struct ast_frame *f)
  941. {
  942. int samples = 0;
  943. switch (f->subclass.format.id) {
  944. case AST_FORMAT_SPEEX:
  945. samples = speex_samples(f->data.ptr, f->datalen);
  946. break;
  947. case AST_FORMAT_SPEEX16:
  948. samples = 2 * speex_samples(f->data.ptr, f->datalen);
  949. break;
  950. case AST_FORMAT_SPEEX32:
  951. samples = 4 * speex_samples(f->data.ptr, f->datalen);
  952. break;
  953. case AST_FORMAT_G723_1:
  954. samples = g723_samples(f->data.ptr, f->datalen);
  955. break;
  956. case AST_FORMAT_ILBC:
  957. samples = 240 * (f->datalen / 50);
  958. break;
  959. case AST_FORMAT_GSM:
  960. samples = 160 * (f->datalen / 33);
  961. break;
  962. case AST_FORMAT_G729A:
  963. samples = f->datalen * 8;
  964. break;
  965. case AST_FORMAT_SLINEAR:
  966. case AST_FORMAT_SLINEAR16:
  967. samples = f->datalen / 2;
  968. break;
  969. case AST_FORMAT_LPC10:
  970. /* assumes that the RTP packet contains one LPC10 frame */
  971. samples = 22 * 8;
  972. samples += (((char *)(f->data.ptr))[7] & 0x1) * 8;
  973. break;
  974. case AST_FORMAT_ULAW:
  975. case AST_FORMAT_ALAW:
  976. case AST_FORMAT_TESTLAW:
  977. samples = f->datalen;
  978. break;
  979. case AST_FORMAT_G722:
  980. case AST_FORMAT_ADPCM:
  981. case AST_FORMAT_G726:
  982. case AST_FORMAT_G726_AAL2:
  983. samples = f->datalen * 2;
  984. break;
  985. case AST_FORMAT_SIREN7:
  986. /* 16,000 samples per second at 32kbps is 4,000 bytes per second */
  987. samples = f->datalen * (16000 / 4000);
  988. break;
  989. case AST_FORMAT_SIREN14:
  990. /* 32,000 samples per second at 48kbps is 6,000 bytes per second */
  991. samples = (int) f->datalen * ((float) 32000 / 6000);
  992. break;
  993. case AST_FORMAT_G719:
  994. /* 48,000 samples per second at 64kbps is 8,000 bytes per second */
  995. samples = (int) f->datalen * ((float) 48000 / 8000);
  996. break;
  997. case AST_FORMAT_SILK:
  998. if (!(ast_format_isset(&f->subclass.format,
  999. SILK_ATTR_KEY_SAMP_RATE,
  1000. SILK_ATTR_VAL_SAMP_24KHZ,
  1001. AST_FORMAT_ATTR_END))) {
  1002. return 480;
  1003. } else if (!(ast_format_isset(&f->subclass.format,
  1004. SILK_ATTR_KEY_SAMP_RATE,
  1005. SILK_ATTR_VAL_SAMP_16KHZ,
  1006. AST_FORMAT_ATTR_END))) {
  1007. return 320;
  1008. } else if (!(ast_format_isset(&f->subclass.format,
  1009. SILK_ATTR_KEY_SAMP_RATE,
  1010. SILK_ATTR_VAL_SAMP_12KHZ,
  1011. AST_FORMAT_ATTR_END))) {
  1012. return 240;
  1013. } else {
  1014. return 160;
  1015. }
  1016. case AST_FORMAT_CELT:
  1017. /* TODO This assumes 20ms delivery right now, which is incorrect */
  1018. samples = ast_format_rate(&f->subclass.format) / 50;
  1019. break;
  1020. case AST_FORMAT_OPUS:
  1021. /* TODO This assumes 20ms delivery right now, which is incorrect */
  1022. samples = 960;
  1023. break;
  1024. default:
  1025. ast_log(LOG_WARNING, "Unable to calculate samples for format %s\n", ast_getformatname(&f->subclass.format));
  1026. }
  1027. return samples;
  1028. }
  1029. int ast_codec_get_len(struct ast_format *format, int samples)
  1030. {
  1031. int len = 0;
  1032. /* XXX Still need speex, and lpc10 XXX */
  1033. switch(format->id) {
  1034. case AST_FORMAT_G723_1:
  1035. len = (samples / 240) * 20;
  1036. break;
  1037. case AST_FORMAT_ILBC:
  1038. len = (samples / 240) * 50;
  1039. break;
  1040. case AST_FORMAT_GSM:
  1041. len = (samples / 160) * 33;
  1042. break;
  1043. case AST_FORMAT_G729A:
  1044. len = samples / 8;
  1045. break;
  1046. case AST_FORMAT_SLINEAR:
  1047. case AST_FORMAT_SLINEAR16:
  1048. len = samples * 2;
  1049. break;
  1050. case AST_FORMAT_ULAW:
  1051. case AST_FORMAT_ALAW:
  1052. case AST_FORMAT_TESTLAW:
  1053. len = samples;
  1054. break;
  1055. case AST_FORMAT_G722:
  1056. case AST_FORMAT_ADPCM:
  1057. case AST_FORMAT_G726:
  1058. case AST_FORMAT_G726_AAL2:
  1059. len = samples / 2;
  1060. break;
  1061. case AST_FORMAT_SIREN7:
  1062. /* 16,000 samples per second at 32kbps is 4,000 bytes per second */
  1063. len = samples / (16000 / 4000);
  1064. break;
  1065. case AST_FORMAT_SIREN14:
  1066. /* 32,000 samples per second at 48kbps is 6,000 bytes per second */
  1067. len = (int) samples / ((float) 32000 / 6000);
  1068. break;
  1069. case AST_FORMAT_G719:
  1070. /* 48,000 samples per second at 64kbps is 8,000 bytes per second */
  1071. len = (int) samples / ((float) 48000 / 8000);
  1072. break;
  1073. default:
  1074. ast_log(LOG_WARNING, "Unable to calculate sample length for format %s\n", ast_getformatname(format));
  1075. }
  1076. return len;
  1077. }
  1078. int ast_frame_adjust_volume(struct ast_frame *f, int adjustment)
  1079. {
  1080. int count;
  1081. short *fdata = f->data.ptr;
  1082. short adjust_value = abs(adjustment);
  1083. if ((f->frametype != AST_FRAME_VOICE) || !(ast_format_is_slinear(&f->subclass.format))) {
  1084. return -1;
  1085. }
  1086. if (!adjustment) {
  1087. return 0;
  1088. }
  1089. for (count = 0; count < f->samples; count++) {
  1090. if (adjustment > 0) {
  1091. ast_slinear_saturated_multiply(&fdata[count], &adjust_value);
  1092. } else if (adjustment < 0) {
  1093. ast_slinear_saturated_divide(&fdata[count], &adjust_value);
  1094. }
  1095. }
  1096. return 0;
  1097. }
  1098. int ast_frame_slinear_sum(struct ast_frame *f1, struct ast_frame *f2)
  1099. {
  1100. int count;
  1101. short *data1, *data2;
  1102. if ((f1->frametype != AST_FRAME_VOICE) || (f1->subclass.format.id != AST_FORMAT_SLINEAR))
  1103. return -1;
  1104. if ((f2->frametype != AST_FRAME_VOICE) || (f2->subclass.format.id != AST_FORMAT_SLINEAR))
  1105. return -1;
  1106. if (f1->samples != f2->samples)
  1107. return -1;
  1108. for (count = 0, data1 = f1->data.ptr, data2 = f2->data.ptr;
  1109. count < f1->samples;
  1110. count++, data1++, data2++)
  1111. ast_slinear_saturated_add(data1, data2);
  1112. return 0;
  1113. }
  1114. int ast_frame_clear(struct ast_frame *frame)
  1115. {
  1116. struct ast_frame *next;
  1117. for (next = AST_LIST_NEXT(frame, frame_list);
  1118. frame;
  1119. frame = next, next = frame ? AST_LIST_NEXT(frame, frame_list) : NULL) {
  1120. memset(frame->data.ptr, 0, frame->datalen);
  1121. }
  1122. return 0;
  1123. }