frame.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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/format_cache.h"
  33. #include "asterisk/channel.h"
  34. #include "asterisk/cli.h"
  35. #include "asterisk/term.h"
  36. #include "asterisk/utils.h"
  37. #include "asterisk/threadstorage.h"
  38. #include "asterisk/linkedlists.h"
  39. #include "asterisk/translate.h"
  40. #include "asterisk/dsp.h"
  41. #include "asterisk/file.h"
  42. #if !defined(LOW_MEMORY)
  43. static void frame_cache_cleanup(void *data);
  44. /*! \brief A per-thread cache of frame headers */
  45. AST_THREADSTORAGE_CUSTOM(frame_cache, NULL, frame_cache_cleanup);
  46. /*!
  47. * \brief Maximum ast_frame cache size
  48. *
  49. * In most cases where the frame header cache will be useful, the size
  50. * of the cache will stay very small. However, it is not always the case that
  51. * the same thread that allocates the frame will be the one freeing them, so
  52. * sometimes a thread will never have any frames in its cache, or the cache
  53. * will never be pulled from. For the latter case, we limit the maximum size.
  54. */
  55. #define FRAME_CACHE_MAX_SIZE 10
  56. /*! \brief This is just so ast_frames, a list head struct for holding a list of
  57. * ast_frame structures, is defined. */
  58. AST_LIST_HEAD_NOLOCK(ast_frames, ast_frame);
  59. struct ast_frame_cache {
  60. struct ast_frames list;
  61. size_t size;
  62. };
  63. #endif
  64. struct ast_frame ast_null_frame = { AST_FRAME_NULL, };
  65. static struct ast_frame *ast_frame_header_new(void)
  66. {
  67. struct ast_frame *f;
  68. #if !defined(LOW_MEMORY)
  69. struct ast_frame_cache *frames;
  70. if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) {
  71. if ((f = AST_LIST_REMOVE_HEAD(&frames->list, frame_list))) {
  72. size_t mallocd_len = f->mallocd_hdr_len;
  73. memset(f, 0, sizeof(*f));
  74. f->mallocd_hdr_len = mallocd_len;
  75. frames->size--;
  76. return f;
  77. }
  78. }
  79. if (!(f = ast_calloc_cache(1, sizeof(*f))))
  80. return NULL;
  81. #else
  82. if (!(f = ast_calloc(1, sizeof(*f))))
  83. return NULL;
  84. #endif
  85. f->mallocd_hdr_len = sizeof(*f);
  86. return f;
  87. }
  88. #if !defined(LOW_MEMORY)
  89. static void frame_cache_cleanup(void *data)
  90. {
  91. struct ast_frame_cache *frames = data;
  92. struct ast_frame *f;
  93. while ((f = AST_LIST_REMOVE_HEAD(&frames->list, frame_list)))
  94. ast_free(f);
  95. ast_free(frames);
  96. }
  97. #endif
  98. static void __frame_free(struct ast_frame *fr, int cache)
  99. {
  100. if (!fr->mallocd)
  101. return;
  102. #if !defined(LOW_MEMORY)
  103. if (fr->mallocd == AST_MALLOCD_HDR
  104. && cache
  105. && ast_opt_cache_media_frames) {
  106. /* Cool, only the header is malloc'd, let's just cache those for now
  107. * to keep things simple... */
  108. struct ast_frame_cache *frames;
  109. frames = ast_threadstorage_get(&frame_cache, sizeof(*frames));
  110. if (frames && frames->size < FRAME_CACHE_MAX_SIZE) {
  111. if (fr->frametype == AST_FRAME_VOICE
  112. || fr->frametype == AST_FRAME_VIDEO
  113. || fr->frametype == AST_FRAME_IMAGE) {
  114. ao2_cleanup(fr->subclass.format);
  115. }
  116. AST_LIST_INSERT_HEAD(&frames->list, fr, frame_list);
  117. frames->size++;
  118. return;
  119. }
  120. }
  121. #endif
  122. if (fr->mallocd & AST_MALLOCD_DATA) {
  123. if (fr->data.ptr) {
  124. ast_free(fr->data.ptr - fr->offset);
  125. }
  126. }
  127. if (fr->mallocd & AST_MALLOCD_SRC) {
  128. ast_free((void *) fr->src);
  129. }
  130. if (fr->mallocd & AST_MALLOCD_HDR) {
  131. if (fr->frametype == AST_FRAME_VOICE
  132. || fr->frametype == AST_FRAME_VIDEO
  133. || fr->frametype == AST_FRAME_IMAGE) {
  134. ao2_cleanup(fr->subclass.format);
  135. }
  136. ast_free(fr);
  137. } else {
  138. fr->mallocd = 0;
  139. }
  140. }
  141. void ast_frame_free(struct ast_frame *frame, int cache)
  142. {
  143. struct ast_frame *next;
  144. while (frame) {
  145. next = AST_LIST_NEXT(frame, frame_list);
  146. __frame_free(frame, cache);
  147. frame = next;
  148. }
  149. }
  150. void ast_frame_dtor(struct ast_frame *f)
  151. {
  152. ast_frfree(f);
  153. }
  154. /*!
  155. * \brief 'isolates' a frame by duplicating non-malloc'ed components
  156. * (header, src, data).
  157. * On return all components are malloc'ed
  158. */
  159. struct ast_frame *ast_frisolate(struct ast_frame *fr)
  160. {
  161. struct ast_frame *out;
  162. void *newdata;
  163. /* if none of the existing frame is malloc'd, let ast_frdup() do it
  164. since it is more efficient
  165. */
  166. if (fr->mallocd == 0) {
  167. return ast_frdup(fr);
  168. }
  169. /* if everything is already malloc'd, we are done */
  170. if ((fr->mallocd & (AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA)) ==
  171. (AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA)) {
  172. return fr;
  173. }
  174. if (!(fr->mallocd & AST_MALLOCD_HDR)) {
  175. /* Allocate a new header if needed */
  176. if (!(out = ast_frame_header_new())) {
  177. return NULL;
  178. }
  179. out->frametype = fr->frametype;
  180. out->subclass = fr->subclass;
  181. if ((fr->frametype == AST_FRAME_VOICE) || (fr->frametype == AST_FRAME_VIDEO) ||
  182. (fr->frametype == AST_FRAME_IMAGE)) {
  183. ao2_bump(out->subclass.format);
  184. }
  185. out->datalen = fr->datalen;
  186. out->samples = fr->samples;
  187. out->mallocd = AST_MALLOCD_HDR;
  188. out->offset = fr->offset;
  189. /* Copy the timing data */
  190. ast_copy_flags(out, fr, AST_FLAGS_ALL);
  191. if (ast_test_flag(fr, AST_FRFLAG_HAS_TIMING_INFO)) {
  192. out->ts = fr->ts;
  193. out->len = fr->len;
  194. out->seqno = fr->seqno;
  195. }
  196. } else {
  197. out = fr;
  198. }
  199. if (fr->src) {
  200. /* The original frame has a source string */
  201. if (!(fr->mallocd & AST_MALLOCD_SRC)) {
  202. /*
  203. * The original frame has a non-malloced source string.
  204. *
  205. * Duplicate the string and put it into the isolated frame
  206. * which may also be the original frame.
  207. */
  208. newdata = ast_strdup(fr->src);
  209. if (!newdata) {
  210. if (out != fr) {
  211. ast_frame_free(out, 0);
  212. }
  213. return NULL;
  214. }
  215. out->src = newdata;
  216. out->mallocd |= AST_MALLOCD_SRC;
  217. } else if (out != fr) {
  218. /* Steal the source string from the original frame. */
  219. out->src = fr->src;
  220. fr->src = NULL;
  221. fr->mallocd &= ~AST_MALLOCD_SRC;
  222. out->mallocd |= AST_MALLOCD_SRC;
  223. }
  224. }
  225. if (!(fr->mallocd & AST_MALLOCD_DATA)) {
  226. /* The original frame has a non-malloced data buffer. */
  227. if (!fr->datalen && fr->frametype != AST_FRAME_TEXT) {
  228. /* Actually it's just an int so we can simply copy it. */
  229. out->data.uint32 = fr->data.uint32;
  230. return out;
  231. }
  232. /*
  233. * Duplicate the data buffer and put it into the isolated frame
  234. * which may also be the original frame.
  235. */
  236. newdata = ast_malloc(fr->datalen + AST_FRIENDLY_OFFSET);
  237. if (!newdata) {
  238. if (out != fr) {
  239. ast_frame_free(out, 0);
  240. }
  241. return NULL;
  242. }
  243. newdata += AST_FRIENDLY_OFFSET;
  244. out->offset = AST_FRIENDLY_OFFSET;
  245. memcpy(newdata, fr->data.ptr, fr->datalen);
  246. out->data.ptr = newdata;
  247. out->mallocd |= AST_MALLOCD_DATA;
  248. } else if (out != fr) {
  249. /* Steal the data buffer from the original frame. */
  250. out->data = fr->data;
  251. memset(&fr->data, 0, sizeof(fr->data));
  252. fr->mallocd &= ~AST_MALLOCD_DATA;
  253. out->mallocd |= AST_MALLOCD_DATA;
  254. }
  255. return out;
  256. }
  257. struct ast_frame *ast_frdup(const struct ast_frame *f)
  258. {
  259. struct ast_frame *out = NULL;
  260. int len, srclen = 0;
  261. void *buf = NULL;
  262. #if !defined(LOW_MEMORY)
  263. struct ast_frame_cache *frames;
  264. #endif
  265. /* Start with standard stuff */
  266. len = sizeof(*out) + AST_FRIENDLY_OFFSET + f->datalen;
  267. /* If we have a source, add space for it */
  268. /*
  269. * XXX Watch out here - if we receive a src which is not terminated
  270. * properly, we can be easily attacked. Should limit the size we deal with.
  271. */
  272. if (f->src)
  273. srclen = strlen(f->src);
  274. if (srclen > 0)
  275. len += srclen + 1;
  276. #if !defined(LOW_MEMORY)
  277. if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) {
  278. AST_LIST_TRAVERSE_SAFE_BEGIN(&frames->list, out, frame_list) {
  279. if (out->mallocd_hdr_len >= len) {
  280. size_t mallocd_len = out->mallocd_hdr_len;
  281. AST_LIST_REMOVE_CURRENT(frame_list);
  282. memset(out, 0, sizeof(*out));
  283. out->mallocd_hdr_len = mallocd_len;
  284. buf = out;
  285. frames->size--;
  286. break;
  287. }
  288. }
  289. AST_LIST_TRAVERSE_SAFE_END;
  290. }
  291. #endif
  292. if (!buf) {
  293. if (!(buf = ast_calloc_cache(1, len)))
  294. return NULL;
  295. out = buf;
  296. out->mallocd_hdr_len = len;
  297. }
  298. out->frametype = f->frametype;
  299. out->subclass = f->subclass;
  300. if ((f->frametype == AST_FRAME_VOICE) || (f->frametype == AST_FRAME_VIDEO) ||
  301. (f->frametype == AST_FRAME_IMAGE)) {
  302. ao2_bump(out->subclass.format);
  303. }
  304. out->datalen = f->datalen;
  305. out->samples = f->samples;
  306. out->delivery = f->delivery;
  307. /* Even though this new frame was allocated from the heap, we can't mark it
  308. * with AST_MALLOCD_HDR, AST_MALLOCD_DATA and AST_MALLOCD_SRC, because that
  309. * would cause ast_frfree() to attempt to individually free each of those
  310. * under the assumption that they were separately allocated. Since this frame
  311. * was allocated in a single allocation, we'll only mark it as if the header
  312. * was heap-allocated; this will result in the entire frame being properly freed.
  313. */
  314. out->mallocd = AST_MALLOCD_HDR;
  315. out->offset = AST_FRIENDLY_OFFSET;
  316. /* Make sure that empty text frames have a valid data.ptr */
  317. if (out->datalen || f->frametype == AST_FRAME_TEXT) {
  318. out->data.ptr = buf + sizeof(*out) + AST_FRIENDLY_OFFSET;
  319. memcpy(out->data.ptr, f->data.ptr, out->datalen);
  320. } else {
  321. out->data.uint32 = f->data.uint32;
  322. }
  323. if (srclen > 0) {
  324. /* This may seem a little strange, but it's to avoid a gcc (4.2.4) compiler warning */
  325. char *src;
  326. out->src = buf + sizeof(*out) + AST_FRIENDLY_OFFSET + f->datalen;
  327. src = (char *) out->src;
  328. /* Must have space since we allocated for it */
  329. strcpy(src, f->src);
  330. }
  331. ast_copy_flags(out, f, AST_FLAGS_ALL);
  332. out->ts = f->ts;
  333. out->len = f->len;
  334. out->seqno = f->seqno;
  335. return out;
  336. }
  337. void ast_swapcopy_samples(void *dst, const void *src, int samples)
  338. {
  339. int i;
  340. unsigned short *dst_s = dst;
  341. const unsigned short *src_s = src;
  342. for (i = 0; i < samples; i++)
  343. dst_s[i] = (src_s[i]<<8) | (src_s[i]>>8);
  344. }
  345. void ast_frame_subclass2str(struct ast_frame *f, char *subclass, size_t slen, char *moreinfo, size_t mlen)
  346. {
  347. switch(f->frametype) {
  348. case AST_FRAME_DTMF_BEGIN:
  349. if (slen > 1) {
  350. subclass[0] = f->subclass.integer;
  351. subclass[1] = '\0';
  352. }
  353. break;
  354. case AST_FRAME_DTMF_END:
  355. if (slen > 1) {
  356. subclass[0] = f->subclass.integer;
  357. subclass[1] = '\0';
  358. }
  359. break;
  360. case AST_FRAME_CONTROL:
  361. switch (f->subclass.integer) {
  362. case AST_CONTROL_HANGUP:
  363. ast_copy_string(subclass, "Hangup", slen);
  364. break;
  365. case AST_CONTROL_RING:
  366. ast_copy_string(subclass, "Ring", slen);
  367. break;
  368. case AST_CONTROL_RINGING:
  369. ast_copy_string(subclass, "Ringing", slen);
  370. break;
  371. case AST_CONTROL_ANSWER:
  372. ast_copy_string(subclass, "Answer", slen);
  373. break;
  374. case AST_CONTROL_BUSY:
  375. ast_copy_string(subclass, "Busy", slen);
  376. break;
  377. case AST_CONTROL_TAKEOFFHOOK:
  378. ast_copy_string(subclass, "Take Off Hook", slen);
  379. break;
  380. case AST_CONTROL_OFFHOOK:
  381. ast_copy_string(subclass, "Line Off Hook", slen);
  382. break;
  383. case AST_CONTROL_CONGESTION:
  384. ast_copy_string(subclass, "Congestion", slen);
  385. break;
  386. case AST_CONTROL_FLASH:
  387. ast_copy_string(subclass, "Flash", slen);
  388. break;
  389. case AST_CONTROL_WINK:
  390. ast_copy_string(subclass, "Wink", slen);
  391. break;
  392. case AST_CONTROL_OPTION:
  393. ast_copy_string(subclass, "Option", slen);
  394. break;
  395. case AST_CONTROL_RADIO_KEY:
  396. ast_copy_string(subclass, "Key Radio", slen);
  397. break;
  398. case AST_CONTROL_RADIO_UNKEY:
  399. ast_copy_string(subclass, "Unkey Radio", slen);
  400. break;
  401. case AST_CONTROL_HOLD:
  402. ast_copy_string(subclass, "Hold", slen);
  403. break;
  404. case AST_CONTROL_UNHOLD:
  405. ast_copy_string(subclass, "Unhold", slen);
  406. break;
  407. case AST_CONTROL_T38_PARAMETERS: {
  408. char *message = "Unknown";
  409. if (f->datalen != sizeof(struct ast_control_t38_parameters)) {
  410. message = "Invalid";
  411. } else {
  412. struct ast_control_t38_parameters *parameters = f->data.ptr;
  413. enum ast_control_t38 state = parameters->request_response;
  414. if (state == AST_T38_REQUEST_NEGOTIATE)
  415. message = "Negotiation Requested";
  416. else if (state == AST_T38_REQUEST_TERMINATE)
  417. message = "Negotiation Request Terminated";
  418. else if (state == AST_T38_NEGOTIATED)
  419. message = "Negotiated";
  420. else if (state == AST_T38_TERMINATED)
  421. message = "Terminated";
  422. else if (state == AST_T38_REFUSED)
  423. message = "Refused";
  424. }
  425. snprintf(subclass, slen, "T38_Parameters/%s", message);
  426. break;
  427. }
  428. case -1:
  429. ast_copy_string(subclass, "Stop generators", slen);
  430. break;
  431. default:
  432. snprintf(subclass, slen, "Unknown control '%d'", f->subclass.integer);
  433. }
  434. break;
  435. case AST_FRAME_NULL:
  436. ast_copy_string(subclass, "N/A", slen);
  437. break;
  438. case AST_FRAME_IAX:
  439. /* Should never happen */
  440. snprintf(subclass, slen, "IAX Frametype %d", f->subclass.integer);
  441. break;
  442. case AST_FRAME_BRIDGE_ACTION:
  443. /* Should never happen */
  444. snprintf(subclass, slen, "Bridge Frametype %d", f->subclass.integer);
  445. break;
  446. case AST_FRAME_BRIDGE_ACTION_SYNC:
  447. /* Should never happen */
  448. snprintf(subclass, slen, "Synchronous Bridge Frametype %d", f->subclass.integer);
  449. break;
  450. case AST_FRAME_TEXT:
  451. ast_copy_string(subclass, "N/A", slen);
  452. if (moreinfo) {
  453. ast_copy_string(moreinfo, f->data.ptr, mlen);
  454. }
  455. break;
  456. case AST_FRAME_IMAGE:
  457. snprintf(subclass, slen, "Image format %s\n", ast_format_get_name(f->subclass.format));
  458. break;
  459. case AST_FRAME_HTML:
  460. switch (f->subclass.integer) {
  461. case AST_HTML_URL:
  462. ast_copy_string(subclass, "URL", slen);
  463. if (moreinfo) {
  464. ast_copy_string(moreinfo, f->data.ptr, mlen);
  465. }
  466. break;
  467. case AST_HTML_DATA:
  468. ast_copy_string(subclass, "Data", slen);
  469. break;
  470. case AST_HTML_BEGIN:
  471. ast_copy_string(subclass, "Begin", slen);
  472. break;
  473. case AST_HTML_END:
  474. ast_copy_string(subclass, "End", slen);
  475. break;
  476. case AST_HTML_LDCOMPLETE:
  477. ast_copy_string(subclass, "Load Complete", slen);
  478. break;
  479. case AST_HTML_NOSUPPORT:
  480. ast_copy_string(subclass, "No Support", slen);
  481. break;
  482. case AST_HTML_LINKURL:
  483. ast_copy_string(subclass, "Link URL", slen);
  484. if (moreinfo) {
  485. ast_copy_string(moreinfo, f->data.ptr, mlen);
  486. }
  487. break;
  488. case AST_HTML_UNLINK:
  489. ast_copy_string(subclass, "Unlink", slen);
  490. break;
  491. case AST_HTML_LINKREJECT:
  492. ast_copy_string(subclass, "Link Reject", slen);
  493. break;
  494. default:
  495. snprintf(subclass, slen, "Unknown HTML frame '%d'\n", f->subclass.integer);
  496. break;
  497. }
  498. break;
  499. case AST_FRAME_MODEM:
  500. switch (f->subclass.integer) {
  501. case AST_MODEM_T38:
  502. ast_copy_string(subclass, "T.38", slen);
  503. break;
  504. case AST_MODEM_V150:
  505. ast_copy_string(subclass, "V.150", slen);
  506. break;
  507. default:
  508. snprintf(subclass, slen, "Unknown MODEM frame '%d'\n", f->subclass.integer);
  509. break;
  510. }
  511. break;
  512. default:
  513. ast_copy_string(subclass, "Unknown Subclass", slen);
  514. break;
  515. }
  516. }
  517. void ast_frame_type2str(enum ast_frame_type frame_type, char *ftype, size_t len)
  518. {
  519. switch (frame_type) {
  520. case AST_FRAME_DTMF_BEGIN:
  521. ast_copy_string(ftype, "DTMF Begin", len);
  522. break;
  523. case AST_FRAME_DTMF_END:
  524. ast_copy_string(ftype, "DTMF End", len);
  525. break;
  526. case AST_FRAME_CONTROL:
  527. ast_copy_string(ftype, "Control", len);
  528. break;
  529. case AST_FRAME_NULL:
  530. ast_copy_string(ftype, "Null Frame", len);
  531. break;
  532. case AST_FRAME_IAX:
  533. /* Should never happen */
  534. ast_copy_string(ftype, "IAX Specific", len);
  535. break;
  536. case AST_FRAME_BRIDGE_ACTION:
  537. /* Should never happen */
  538. ast_copy_string(ftype, "Bridge Specific", len);
  539. break;
  540. case AST_FRAME_BRIDGE_ACTION_SYNC:
  541. /* Should never happen */
  542. ast_copy_string(ftype, "Bridge Specific", len);
  543. break;
  544. case AST_FRAME_TEXT:
  545. ast_copy_string(ftype, "Text", len);
  546. break;
  547. case AST_FRAME_TEXT_DATA:
  548. ast_copy_string(ftype, "Text Data", len);
  549. break;
  550. case AST_FRAME_IMAGE:
  551. ast_copy_string(ftype, "Image", len);
  552. break;
  553. case AST_FRAME_HTML:
  554. ast_copy_string(ftype, "HTML", len);
  555. break;
  556. case AST_FRAME_MODEM:
  557. ast_copy_string(ftype, "Modem", len);
  558. break;
  559. case AST_FRAME_VOICE:
  560. ast_copy_string(ftype, "Voice", len);
  561. break;
  562. case AST_FRAME_VIDEO:
  563. ast_copy_string(ftype, "Video", len);
  564. break;
  565. default:
  566. snprintf(ftype, len, "Unknown Frametype '%u'", frame_type);
  567. break;
  568. }
  569. }
  570. /*! Dump a frame for debugging purposes */
  571. void ast_frame_dump(const char *name, struct ast_frame *f, char *prefix)
  572. {
  573. const char noname[] = "unknown";
  574. char ftype[40] = "Unknown Frametype";
  575. char cft[80];
  576. char subclass[40] = "Unknown Subclass";
  577. char csub[80];
  578. char moreinfo[40] = "";
  579. char cn[60];
  580. char cp[40];
  581. char cmn[40];
  582. if (!name) {
  583. name = noname;
  584. }
  585. if (!f) {
  586. ast_verb(-1, "%s [ %s (NULL) ] [%s]\n",
  587. term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
  588. term_color(cft, "HANGUP", COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
  589. term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
  590. return;
  591. }
  592. /* XXX We should probably print one each of voice and video when the format changes XXX */
  593. if (f->frametype == AST_FRAME_VOICE) {
  594. return;
  595. }
  596. if (f->frametype == AST_FRAME_VIDEO) {
  597. return;
  598. }
  599. ast_frame_type2str(f->frametype, ftype, sizeof(ftype));
  600. ast_frame_subclass2str(f, subclass, sizeof(subclass), moreinfo, sizeof(moreinfo));
  601. if (!ast_strlen_zero(moreinfo))
  602. ast_verb(-1, "%s [ TYPE: %s (%u) SUBCLASS: %s (%d) '%s' ] [%s]\n",
  603. term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
  604. term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
  605. f->frametype,
  606. term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
  607. f->subclass.integer,
  608. term_color(cmn, moreinfo, COLOR_BRGREEN, COLOR_BLACK, sizeof(cmn)),
  609. term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
  610. else
  611. ast_verb(-1, "%s [ TYPE: %s (%u) SUBCLASS: %s (%d) ] [%s]\n",
  612. term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
  613. term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
  614. f->frametype,
  615. term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
  616. f->subclass.integer,
  617. term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
  618. }
  619. int ast_frame_adjust_volume(struct ast_frame *f, int adjustment)
  620. {
  621. int count;
  622. short *fdata = f->data.ptr;
  623. short adjust_value = abs(adjustment);
  624. if ((f->frametype != AST_FRAME_VOICE) || !(ast_format_cache_is_slinear(f->subclass.format))) {
  625. return -1;
  626. }
  627. if (!adjustment) {
  628. return 0;
  629. }
  630. for (count = 0; count < f->samples; count++) {
  631. if (adjustment > 0) {
  632. ast_slinear_saturated_multiply(&fdata[count], &adjust_value);
  633. } else if (adjustment < 0) {
  634. ast_slinear_saturated_divide(&fdata[count], &adjust_value);
  635. }
  636. }
  637. return 0;
  638. }
  639. int ast_frame_slinear_sum(struct ast_frame *f1, struct ast_frame *f2)
  640. {
  641. int count;
  642. short *data1, *data2;
  643. if ((f1->frametype != AST_FRAME_VOICE) || (ast_format_cmp(f1->subclass.format, ast_format_slin) != AST_FORMAT_CMP_NOT_EQUAL))
  644. return -1;
  645. if ((f2->frametype != AST_FRAME_VOICE) || (ast_format_cmp(f2->subclass.format, ast_format_slin) != AST_FORMAT_CMP_NOT_EQUAL))
  646. return -1;
  647. if (f1->samples != f2->samples)
  648. return -1;
  649. for (count = 0, data1 = f1->data.ptr, data2 = f2->data.ptr;
  650. count < f1->samples;
  651. count++, data1++, data2++)
  652. ast_slinear_saturated_add(data1, data2);
  653. return 0;
  654. }
  655. int ast_frame_clear(struct ast_frame *frame)
  656. {
  657. struct ast_frame *next;
  658. for (next = AST_LIST_NEXT(frame, frame_list);
  659. frame;
  660. frame = next, next = frame ? AST_LIST_NEXT(frame, frame_list) : NULL) {
  661. memset(frame->data.ptr, 0, frame->datalen);
  662. }
  663. return 0;
  664. }