frame.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Frame manipulation routines
  5. *
  6. * Copyright (C) 1999-2004, Mark Spencer
  7. *
  8. * Mark Spencer <markster@digium.com>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <asterisk/lock.h>
  14. #include <asterisk/frame.h>
  15. #include <asterisk/logger.h>
  16. #include <asterisk/options.h>
  17. #include <asterisk/channel.h>
  18. #include <asterisk/cli.h>
  19. #include <asterisk/term.h>
  20. #include <asterisk/utils.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <stdio.h>
  26. #include "asterisk.h"
  27. #ifdef TRACE_FRAMES
  28. static int headers = 0;
  29. static struct ast_frame *headerlist = NULL;
  30. AST_MUTEX_DEFINE_STATIC(framelock);
  31. #endif
  32. #define SMOOTHER_SIZE 8000
  33. struct ast_format_list {
  34. int visible; /* Can we see this entry */
  35. int bits; /* bitmask value */
  36. char *name; /* short name */
  37. char *desc; /* Description */
  38. };
  39. struct ast_smoother {
  40. int size;
  41. int format;
  42. int readdata;
  43. int optimizablestream;
  44. int flags;
  45. float samplesperbyte;
  46. struct ast_frame f;
  47. struct timeval delivery;
  48. char data[SMOOTHER_SIZE];
  49. char framedata[SMOOTHER_SIZE + AST_FRIENDLY_OFFSET];
  50. struct ast_frame *opt;
  51. int len;
  52. };
  53. void ast_smoother_reset(struct ast_smoother *s, int size)
  54. {
  55. memset(s, 0, sizeof(struct ast_smoother));
  56. s->size = size;
  57. }
  58. struct ast_smoother *ast_smoother_new(int size)
  59. {
  60. struct ast_smoother *s;
  61. if (size < 1)
  62. return NULL;
  63. s = malloc(sizeof(struct ast_smoother));
  64. if (s)
  65. ast_smoother_reset(s, size);
  66. return s;
  67. }
  68. int ast_smoother_get_flags(struct ast_smoother *s)
  69. {
  70. return s->flags;
  71. }
  72. void ast_smoother_set_flags(struct ast_smoother *s, int flags)
  73. {
  74. s->flags = flags;
  75. }
  76. int __ast_smoother_feed(struct ast_smoother *s, struct ast_frame *f, int swap)
  77. {
  78. if (f->frametype != AST_FRAME_VOICE) {
  79. ast_log(LOG_WARNING, "Huh? Can't smooth a non-voice frame!\n");
  80. return -1;
  81. }
  82. if (!s->format) {
  83. s->format = f->subclass;
  84. s->samplesperbyte = (float)f->samples / (float)f->datalen;
  85. } else if (s->format != f->subclass) {
  86. ast_log(LOG_WARNING, "Smoother was working on %d format frames, now trying to feed %d?\n", s->format, f->subclass);
  87. return -1;
  88. }
  89. if (s->len + f->datalen > SMOOTHER_SIZE) {
  90. ast_log(LOG_WARNING, "Out of smoother space\n");
  91. return -1;
  92. }
  93. if (((f->datalen == s->size) || ((f->datalen < 10) && (s->flags & AST_SMOOTHER_FLAG_G729)))
  94. && !s->opt && (f->offset >= AST_MIN_OFFSET)) {
  95. if (!s->len) {
  96. /* Optimize by sending the frame we just got
  97. on the next read, thus eliminating the douple
  98. copy */
  99. s->opt = f;
  100. return 0;
  101. } else {
  102. s->optimizablestream++;
  103. if (s->optimizablestream > 10) {
  104. /* For the past 10 rounds, we have input and output
  105. frames of the correct size for this smoother, yet
  106. we were unable to optimize because there was still
  107. some cruft left over. Lets just drop the cruft so
  108. we can move to a fully optimized path */
  109. s->len = 0;
  110. s->opt = f;
  111. return 0;
  112. }
  113. }
  114. } else
  115. s->optimizablestream = 0;
  116. if (s->flags & AST_SMOOTHER_FLAG_G729) {
  117. if (s->len % 10) {
  118. ast_log(LOG_NOTICE, "Dropping extra frame of G.729 since we already have a VAD frame at the end\n");
  119. return 0;
  120. }
  121. }
  122. if (swap)
  123. ast_memcpy_byteswap(s->data+s->len, f->data, f->samples);
  124. else
  125. memcpy(s->data + s->len, f->data, f->datalen);
  126. /* If either side is empty, reset the delivery time */
  127. if (!s->len || (!f->delivery.tv_sec && !f->delivery.tv_usec) ||
  128. (!s->delivery.tv_sec && !s->delivery.tv_usec))
  129. s->delivery = f->delivery;
  130. s->len += f->datalen;
  131. return 0;
  132. }
  133. struct ast_frame *ast_smoother_read(struct ast_smoother *s)
  134. {
  135. struct ast_frame *opt;
  136. int len;
  137. /* IF we have an optimization frame, send it */
  138. if (s->opt) {
  139. if (s->opt->offset < AST_FRIENDLY_OFFSET)
  140. ast_log(LOG_WARNING, "Returning a frame of inappropriate offset (%d).",
  141. s->opt->offset);
  142. opt = s->opt;
  143. s->opt = NULL;
  144. return opt;
  145. }
  146. /* Make sure we have enough data */
  147. if (s->len < s->size) {
  148. /* Or, if this is a G.729 frame with VAD on it, send it immediately anyway */
  149. if (!((s->flags & AST_SMOOTHER_FLAG_G729) && (s->size % 10)))
  150. return NULL;
  151. }
  152. len = s->size;
  153. if (len > s->len)
  154. len = s->len;
  155. /* Make frame */
  156. s->f.frametype = AST_FRAME_VOICE;
  157. s->f.subclass = s->format;
  158. s->f.data = s->framedata + AST_FRIENDLY_OFFSET;
  159. s->f.offset = AST_FRIENDLY_OFFSET;
  160. s->f.datalen = len;
  161. /* Samples will be improper given VAD, but with VAD the concept really doesn't even exist */
  162. s->f.samples = len * s->samplesperbyte;
  163. s->f.delivery = s->delivery;
  164. /* Fill Data */
  165. memcpy(s->f.data, s->data, len);
  166. s->len -= len;
  167. /* Move remaining data to the front if applicable */
  168. if (s->len) {
  169. /* In principle this should all be fine because if we are sending
  170. G.729 VAD, the next timestamp will take over anyawy */
  171. memmove(s->data, s->data + len, s->len);
  172. if (s->delivery.tv_sec || s->delivery.tv_usec) {
  173. /* If we have delivery time, increment it, otherwise, leave it at 0 */
  174. s->delivery.tv_sec += (len * s->samplesperbyte) / 8000.0;
  175. s->delivery.tv_usec += (((int)(len * s->samplesperbyte)) % 8000) * 125;
  176. if (s->delivery.tv_usec > 1000000) {
  177. s->delivery.tv_usec -= 1000000;
  178. s->delivery.tv_sec += 1;
  179. }
  180. }
  181. }
  182. /* Return frame */
  183. return &s->f;
  184. }
  185. void ast_smoother_free(struct ast_smoother *s)
  186. {
  187. free(s);
  188. }
  189. static struct ast_frame *ast_frame_header_new(void)
  190. {
  191. struct ast_frame *f;
  192. f = malloc(sizeof(struct ast_frame));
  193. if (f)
  194. memset(f, 0, sizeof(struct ast_frame));
  195. #ifdef TRACE_FRAMES
  196. if (f) {
  197. headers++;
  198. f->prev = NULL;
  199. ast_mutex_lock(&framelock);
  200. f->next = headerlist;
  201. if (headerlist)
  202. headerlist->prev = f;
  203. headerlist = f;
  204. ast_mutex_unlock(&framelock);
  205. }
  206. #endif
  207. return f;
  208. }
  209. /*
  210. * Important: I should be made more efficient. Frame headers should
  211. * most definitely be cached
  212. */
  213. void ast_frfree(struct ast_frame *fr)
  214. {
  215. if (fr->mallocd & AST_MALLOCD_DATA) {
  216. if (fr->data)
  217. free(fr->data - fr->offset);
  218. }
  219. if (fr->mallocd & AST_MALLOCD_SRC) {
  220. if (fr->src)
  221. free(fr->src);
  222. }
  223. if (fr->mallocd & AST_MALLOCD_HDR) {
  224. #ifdef TRACE_FRAMES
  225. headers--;
  226. ast_mutex_lock(&framelock);
  227. if (fr->next)
  228. fr->next->prev = fr->prev;
  229. if (fr->prev)
  230. fr->prev->next = fr->next;
  231. else
  232. headerlist = fr->next;
  233. ast_mutex_unlock(&framelock);
  234. #endif
  235. free(fr);
  236. }
  237. }
  238. struct ast_frame *ast_frisolate(struct ast_frame *fr)
  239. {
  240. struct ast_frame *out;
  241. if (!(fr->mallocd & AST_MALLOCD_HDR)) {
  242. /* Allocate a new header if needed */
  243. out = ast_frame_header_new();
  244. if (!out) {
  245. ast_log(LOG_WARNING, "Out of memory\n");
  246. return NULL;
  247. }
  248. out->frametype = fr->frametype;
  249. out->subclass = fr->subclass;
  250. out->datalen = 0;
  251. out->samples = fr->samples;
  252. out->offset = 0;
  253. out->src = NULL;
  254. out->data = NULL;
  255. } else {
  256. out = fr;
  257. }
  258. if (!(fr->mallocd & AST_MALLOCD_SRC)) {
  259. if (fr->src)
  260. out->src = strdup(fr->src);
  261. } else
  262. out->src = fr->src;
  263. if (!(fr->mallocd & AST_MALLOCD_DATA)) {
  264. out->data = malloc(fr->datalen + AST_FRIENDLY_OFFSET);
  265. if (!out->data) {
  266. free(out);
  267. ast_log(LOG_WARNING, "Out of memory\n");
  268. return NULL;
  269. }
  270. out->data += AST_FRIENDLY_OFFSET;
  271. out->offset = AST_FRIENDLY_OFFSET;
  272. out->datalen = fr->datalen;
  273. memcpy(out->data, fr->data, fr->datalen);
  274. }
  275. out->mallocd = AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA;
  276. return out;
  277. }
  278. struct ast_frame *ast_frdup(struct ast_frame *f)
  279. {
  280. struct ast_frame *out;
  281. int len, srclen = 0;
  282. void *buf;
  283. /* Start with standard stuff */
  284. len = sizeof(struct ast_frame) + AST_FRIENDLY_OFFSET + f->datalen;
  285. /* If we have a source, add space for it */
  286. if (f->src)
  287. srclen = strlen(f->src);
  288. if (srclen > 0)
  289. len += srclen + 1;
  290. buf = malloc(len);
  291. if (!buf)
  292. return NULL;
  293. out = buf;
  294. /* Set us as having malloc'd header only, so it will eventually
  295. get freed. */
  296. out->frametype = f->frametype;
  297. out->subclass = f->subclass;
  298. out->datalen = f->datalen;
  299. out->samples = f->samples;
  300. out->delivery = f->delivery;
  301. out->mallocd = AST_MALLOCD_HDR;
  302. out->offset = AST_FRIENDLY_OFFSET;
  303. out->data = buf + sizeof(struct ast_frame) + AST_FRIENDLY_OFFSET;
  304. if (srclen > 0) {
  305. out->src = out->data + f->datalen;
  306. /* Must have space since we allocated for it */
  307. strcpy(out->src, f->src);
  308. } else
  309. out->src = NULL;
  310. out->prev = NULL;
  311. out->next = NULL;
  312. memcpy(out->data, f->data, out->datalen);
  313. return out;
  314. }
  315. struct ast_frame *ast_fr_fdread(int fd)
  316. {
  317. char buf[65536];
  318. int res;
  319. int ttl = sizeof(struct ast_frame);
  320. struct ast_frame *f = (struct ast_frame *)buf;
  321. /* Read a frame directly from there. They're always in the
  322. right format. */
  323. while(ttl) {
  324. res = read(fd, buf, ttl);
  325. if (res < 0) {
  326. ast_log(LOG_WARNING, "Bad read on %d: %s\n", fd, strerror(errno));
  327. return NULL;
  328. }
  329. ttl -= res;
  330. }
  331. /* read the frame header */
  332. f->mallocd = 0;
  333. /* Re-write data position */
  334. f->data = buf + sizeof(struct ast_frame);
  335. f->offset = 0;
  336. /* Forget about being mallocd */
  337. f->mallocd = 0;
  338. /* Re-write the source */
  339. f->src = (char *)__FUNCTION__;
  340. if (f->datalen > sizeof(buf) - sizeof(struct ast_frame)) {
  341. /* Really bad read */
  342. ast_log(LOG_WARNING, "Strange read (%d bytes)\n", f->datalen);
  343. return NULL;
  344. }
  345. if (f->datalen) {
  346. if ((res = read(fd, f->data, f->datalen)) != f->datalen) {
  347. /* Bad read */
  348. ast_log(LOG_WARNING, "How very strange, expected %d, got %d\n", f->datalen, res);
  349. return NULL;
  350. }
  351. }
  352. if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP)) {
  353. return NULL;
  354. }
  355. return ast_frisolate(f);
  356. }
  357. /* Some convenient routines for sending frames to/from stream or datagram
  358. sockets, pipes, etc (maybe even files) */
  359. int ast_fr_fdwrite(int fd, struct ast_frame *frame)
  360. {
  361. /* Write the frame exactly */
  362. if (write(fd, frame, sizeof(struct ast_frame)) != sizeof(struct ast_frame)) {
  363. ast_log(LOG_WARNING, "Write error: %s\n", strerror(errno));
  364. return -1;
  365. }
  366. if (write(fd, frame->data, frame->datalen) != frame->datalen) {
  367. ast_log(LOG_WARNING, "Write error: %s\n", strerror(errno));
  368. return -1;
  369. }
  370. return 0;
  371. }
  372. int ast_fr_fdhangup(int fd)
  373. {
  374. struct ast_frame hangup = {
  375. AST_FRAME_CONTROL,
  376. AST_CONTROL_HANGUP
  377. };
  378. return ast_fr_fdwrite(fd, &hangup);
  379. }
  380. void ast_memcpy_byteswap(void *dst, void *src, int samples)
  381. {
  382. int i;
  383. unsigned short *dst_s = dst;
  384. unsigned short *src_s = src;
  385. for (i=0; i<samples; i++)
  386. dst_s[i] = (src_s[i]<<8)|(src_s[i]>>8);
  387. }
  388. static struct ast_format_list AST_FORMAT_LIST[] = {
  389. { 1, AST_FORMAT_G723_1 , "g723" , "G.723.1"},
  390. { 1, AST_FORMAT_GSM, "gsm" , "GSM"},
  391. { 1, AST_FORMAT_ULAW, "ulaw", "G.711 u-law" },
  392. { 1, AST_FORMAT_ALAW, "alaw", "G.711 A-law" },
  393. { 1, AST_FORMAT_G726, "g726", "G.726" },
  394. { 1, AST_FORMAT_ADPCM, "adpcm" , "ADPCM"},
  395. { 1, AST_FORMAT_SLINEAR, "slin", "16 bit Signed Linear PCM"},
  396. { 1, AST_FORMAT_LPC10, "lpc10", "LPC10" },
  397. { 1, AST_FORMAT_G729A, "g729", "G.729A" },
  398. { 1, AST_FORMAT_SPEEX, "speex", "SpeeX" },
  399. { 1, AST_FORMAT_ILBC, "ilbc", "iLBC"},
  400. { 0, 0, "nothing", "undefined" },
  401. { 0, 0, "nothing", "undefined" },
  402. { 0, 0, "nothing", "undefined" },
  403. { 0, 0, "nothing", "undefined" },
  404. { 0, AST_FORMAT_MAX_AUDIO, "maxaudio", "Maximum audio format" },
  405. { 1, AST_FORMAT_JPEG, "jpeg", "JPEG image"},
  406. { 1, AST_FORMAT_PNG, "png", "PNG image"},
  407. { 1, AST_FORMAT_H261, "h261", "H.261 Video" },
  408. { 1, AST_FORMAT_H263, "h263", "H.263 Video" },
  409. { 0, 0, "nothing", "undefined" },
  410. { 0, 0, "nothing", "undefined" },
  411. { 0, 0, "nothing", "undefined" },
  412. { 0, 0, "nothing", "undefined" },
  413. { 0, AST_FORMAT_MAX_VIDEO, "maxvideo", "Maximum video format" },
  414. };
  415. struct ast_format_list *ast_get_format_list_index(int index) {
  416. return &AST_FORMAT_LIST[index];
  417. }
  418. struct ast_format_list *ast_get_format_list(size_t *size) {
  419. *size = (sizeof(AST_FORMAT_LIST) / sizeof(struct ast_format_list));
  420. return AST_FORMAT_LIST;
  421. }
  422. char* ast_getformatname(int format)
  423. {
  424. int x = 0;
  425. char *ret = "unknown";
  426. for (x = 0 ; x < sizeof(AST_FORMAT_LIST) / sizeof(struct ast_format_list) ; x++) {
  427. if(AST_FORMAT_LIST[x].visible && AST_FORMAT_LIST[x].bits == format) {
  428. ret = AST_FORMAT_LIST[x].name;
  429. break;
  430. }
  431. }
  432. return ret;
  433. }
  434. char *ast_getformatname_multiple(char *buf, size_t size, int format) {
  435. int x = 0;
  436. unsigned len;
  437. char *end = buf;
  438. char *start = buf;
  439. if (!size) return buf;
  440. snprintf(end, size, "0x%x (", format);
  441. len = strlen(end);
  442. end += len;
  443. size -= len;
  444. start = end;
  445. for (x = 0 ; x < sizeof(AST_FORMAT_LIST) / sizeof(struct ast_format_list) ; x++) {
  446. if (AST_FORMAT_LIST[x].visible && (AST_FORMAT_LIST[x].bits & format)) {
  447. snprintf(end, size,"%s|",AST_FORMAT_LIST[x].name);
  448. len = strlen(end);
  449. end += len;
  450. size -= len;
  451. }
  452. }
  453. if (start == end)
  454. snprintf(start, size, "nothing)");
  455. else if (size > 1)
  456. *(end -1) = ')';
  457. return buf;
  458. }
  459. static struct ast_codec_alias_table {
  460. char *alias;
  461. char *realname;
  462. } ast_codec_alias_table[] = {
  463. {"slinear","slin"},
  464. {"g723.1","g723"},
  465. };
  466. static char *ast_expand_codec_alias(char *in) {
  467. int x = 0;
  468. for (x = 0; x < sizeof(ast_codec_alias_table) / sizeof(struct ast_codec_alias_table) ; x++) {
  469. if(!strcmp(in,ast_codec_alias_table[x].alias))
  470. return ast_codec_alias_table[x].realname;
  471. }
  472. return in;
  473. }
  474. int ast_getformatbyname(char *name)
  475. {
  476. int x = 0, all = 0, format = 0;
  477. all = strcasecmp(name, "all") ? 0 : 1;
  478. for (x = 0 ; x < sizeof(AST_FORMAT_LIST) / sizeof(struct ast_format_list) ; x++) {
  479. if(AST_FORMAT_LIST[x].visible && (all ||
  480. !strcasecmp(AST_FORMAT_LIST[x].name,name) ||
  481. !strcasecmp(AST_FORMAT_LIST[x].name,ast_expand_codec_alias(name)))) {
  482. format |= AST_FORMAT_LIST[x].bits;
  483. if(!all)
  484. break;
  485. }
  486. }
  487. return format;
  488. }
  489. char *ast_codec2str(int codec) {
  490. int x = 0;
  491. char *ret = "unknown";
  492. for (x = 0 ; x < sizeof(AST_FORMAT_LIST) / sizeof(struct ast_format_list) ; x++) {
  493. if(AST_FORMAT_LIST[x].visible && AST_FORMAT_LIST[x].bits == codec) {
  494. ret = AST_FORMAT_LIST[x].desc;
  495. break;
  496. }
  497. }
  498. return ret;
  499. }
  500. static int show_codecs(int fd, int argc, char *argv[])
  501. {
  502. int i, found=0;
  503. char hex[25];
  504. if ((argc < 2) || (argc > 3))
  505. return RESULT_SHOWUSAGE;
  506. if (getenv("I_AM_NOT_AN_IDIOT") == NULL)
  507. ast_cli(fd, "Disclaimer: this command is for informational purposes only.\n"
  508. "\tIt does not indicate anything about your configuration.\n");
  509. ast_cli(fd, "%11s %9s %10s TYPE %5s %s\n","INT","BINARY","HEX","NAME","DESC");
  510. ast_cli(fd, "--------------------------------------------------------------------------------\n");
  511. if ((argc == 2) || (!strcasecmp(argv[1],"audio"))) {
  512. found = 1;
  513. for (i=0;i<11;i++) {
  514. snprintf(hex,25,"(0x%x)",1<<i);
  515. ast_cli(fd, "%11u (1 << %2d) %10s audio %5s (%s)\n",1 << i,i,hex,ast_getformatname(1<<i),ast_codec2str(1<<i));
  516. }
  517. }
  518. if ((argc == 2) || (!strcasecmp(argv[1],"image"))) {
  519. found = 1;
  520. for (i=16;i<18;i++) {
  521. snprintf(hex,25,"(0x%x)",1<<i);
  522. ast_cli(fd, "%11u (1 << %2d) %10s image %5s (%s)\n",1 << i,i,hex,ast_getformatname(1<<i),ast_codec2str(1<<i));
  523. }
  524. }
  525. if ((argc == 2) || (!strcasecmp(argv[1],"video"))) {
  526. found = 1;
  527. for (i=18;i<20;i++) {
  528. snprintf(hex,25,"(0x%x)",1<<i);
  529. ast_cli(fd, "%11u (1 << %2d) %10s video %5s (%s)\n",1 << i,i,hex,ast_getformatname(1<<i),ast_codec2str(1<<i));
  530. }
  531. }
  532. if (! found)
  533. return RESULT_SHOWUSAGE;
  534. else
  535. return RESULT_SUCCESS;
  536. }
  537. static char frame_show_codecs_usage[] =
  538. "Usage: show [audio|video|image] codecs\n"
  539. " Displays codec mapping\n";
  540. struct ast_cli_entry cli_show_codecs =
  541. { { "show", "codecs", NULL }, show_codecs, "Shows codecs", frame_show_codecs_usage };
  542. struct ast_cli_entry cli_show_codecs_audio =
  543. { { "show", "audio", "codecs", NULL }, show_codecs, "Shows audio codecs", frame_show_codecs_usage };
  544. struct ast_cli_entry cli_show_codecs_video =
  545. { { "show", "video", "codecs", NULL }, show_codecs, "Shows video codecs", frame_show_codecs_usage };
  546. struct ast_cli_entry cli_show_codecs_image =
  547. { { "show", "image", "codecs", NULL }, show_codecs, "Shows image codecs", frame_show_codecs_usage };
  548. static int show_codec_n(int fd, int argc, char *argv[])
  549. {
  550. int codec, i, found=0;
  551. if (argc != 3)
  552. return RESULT_SHOWUSAGE;
  553. if (sscanf(argv[2],"%d",&codec) != 1)
  554. return RESULT_SHOWUSAGE;
  555. for (i=0;i<32;i++)
  556. if (codec & (1 << i)) {
  557. found = 1;
  558. ast_cli(fd, "%11u (1 << %2d) %s\n",1 << i,i,ast_codec2str(1<<i));
  559. }
  560. if (! found)
  561. ast_cli(fd, "Codec %d not found\n", codec);
  562. return RESULT_SUCCESS;
  563. }
  564. static char frame_show_codec_n_usage[] =
  565. "Usage: show codec <number>\n"
  566. " Displays codec mapping\n";
  567. struct ast_cli_entry cli_show_codec_n =
  568. { { "show", "codec", NULL }, show_codec_n, "Shows a specific codec", frame_show_codec_n_usage };
  569. void ast_frame_dump(char *name, struct ast_frame *f, char *prefix)
  570. {
  571. char *n = "unknown";
  572. char ftype[40] = "Unknown Frametype";
  573. char cft[80];
  574. char subclass[40] = "Unknown Subclass";
  575. char csub[80];
  576. char moreinfo[40] = "";
  577. char cn[60];
  578. char cp[40];
  579. char cmn[40];
  580. if (name)
  581. n = name;
  582. if (!f) {
  583. ast_verbose("%s [ %s (NULL) ] [%s]\n",
  584. term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
  585. term_color(cft, "HANGUP", COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
  586. term_color(cn, n, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
  587. return;
  588. }
  589. /* XXX We should probably print one each of voice and video when the format changes XXX */
  590. if (f->frametype == AST_FRAME_VOICE)
  591. return;
  592. if (f->frametype == AST_FRAME_VIDEO)
  593. return;
  594. switch(f->frametype) {
  595. case AST_FRAME_DTMF:
  596. strcpy(ftype, "DTMF");
  597. subclass[0] = f->subclass;
  598. subclass[1] = '\0';
  599. break;
  600. case AST_FRAME_CONTROL:
  601. strcpy(ftype, "Control");
  602. switch(f->subclass) {
  603. case AST_CONTROL_HANGUP:
  604. strcpy(subclass, "Hangup");
  605. break;
  606. case AST_CONTROL_RING:
  607. strcpy(subclass, "Ring");
  608. break;
  609. case AST_CONTROL_RINGING:
  610. strcpy(subclass, "Ringing");
  611. break;
  612. case AST_CONTROL_ANSWER:
  613. strcpy(subclass, "Answer");
  614. break;
  615. case AST_CONTROL_BUSY:
  616. strcpy(subclass, "Busy");
  617. break;
  618. case AST_CONTROL_TAKEOFFHOOK:
  619. strcpy(subclass, "Take Off Hook");
  620. break;
  621. case AST_CONTROL_OFFHOOK:
  622. strcpy(subclass, "Line Off Hook");
  623. break;
  624. case AST_CONTROL_CONGESTION:
  625. strcpy(subclass, "Congestion");
  626. break;
  627. case AST_CONTROL_FLASH:
  628. strcpy(subclass, "Flash");
  629. break;
  630. case AST_CONTROL_WINK:
  631. strcpy(subclass, "Wink");
  632. break;
  633. case AST_CONTROL_OPTION:
  634. strcpy(subclass, "Option");
  635. break;
  636. case AST_CONTROL_RADIO_KEY:
  637. strcpy(subclass, "Key Radio");
  638. break;
  639. case AST_CONTROL_RADIO_UNKEY:
  640. strcpy(subclass, "Unkey Radio");
  641. break;
  642. case -1:
  643. strcpy(subclass, "Stop generators");
  644. break;
  645. default:
  646. snprintf(subclass, sizeof(subclass), "Unknown control '%d'", f->subclass);
  647. }
  648. break;
  649. case AST_FRAME_NULL:
  650. strcpy(ftype, "Null Frame");
  651. strcpy(subclass, "N/A");
  652. break;
  653. case AST_FRAME_IAX:
  654. /* Should never happen */
  655. strcpy(ftype, "IAX Specific");
  656. snprintf(subclass, sizeof(subclass), "IAX Frametype %d", f->subclass);
  657. break;
  658. case AST_FRAME_TEXT:
  659. strcpy(ftype, "Text");
  660. strcpy(subclass, "N/A");
  661. strncpy(moreinfo, f->data, sizeof(moreinfo) - 1);
  662. break;
  663. case AST_FRAME_IMAGE:
  664. strcpy(ftype, "Image");
  665. snprintf(subclass, sizeof(subclass), "Image format %s\n", ast_getformatname(f->subclass));
  666. break;
  667. case AST_FRAME_HTML:
  668. strcpy(ftype, "HTML");
  669. switch(f->subclass) {
  670. case AST_HTML_URL:
  671. strcpy(subclass, "URL");
  672. strncpy(moreinfo, f->data, sizeof(moreinfo) - 1);
  673. break;
  674. case AST_HTML_DATA:
  675. strcpy(subclass, "Data");
  676. break;
  677. case AST_HTML_BEGIN:
  678. strcpy(subclass, "Begin");
  679. break;
  680. case AST_HTML_END:
  681. strcpy(subclass, "End");
  682. break;
  683. case AST_HTML_LDCOMPLETE:
  684. strcpy(subclass, "Load Complete");
  685. break;
  686. case AST_HTML_NOSUPPORT:
  687. strcpy(subclass, "No Support");
  688. break;
  689. case AST_HTML_LINKURL:
  690. strcpy(subclass, "Link URL");
  691. strncpy(moreinfo, f->data, sizeof(moreinfo) - 1);
  692. break;
  693. case AST_HTML_UNLINK:
  694. strcpy(subclass, "Unlink");
  695. break;
  696. case AST_HTML_LINKREJECT:
  697. strcpy(subclass, "Link Reject");
  698. break;
  699. default:
  700. snprintf(subclass, sizeof(subclass), "Unknown HTML frame '%d'\n", f->subclass);
  701. break;
  702. }
  703. break;
  704. default:
  705. snprintf(ftype, sizeof(ftype), "Unknown Frametype '%d'", f->frametype);
  706. }
  707. if (!ast_strlen_zero(moreinfo))
  708. ast_verbose("%s [ TYPE: %s (%d) SUBCLASS: %s (%d) '%s' ] [%s]\n",
  709. term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
  710. term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
  711. f->frametype,
  712. term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
  713. f->subclass,
  714. term_color(cmn, moreinfo, COLOR_BRGREEN, COLOR_BLACK, sizeof(cmn)),
  715. term_color(cn, n, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
  716. else
  717. ast_verbose("%s [ TYPE: %s (%d) SUBCLASS: %s (%d) ] [%s]\n",
  718. term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
  719. term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
  720. f->frametype,
  721. term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
  722. f->subclass,
  723. term_color(cn, n, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
  724. }
  725. #ifdef TRACE_FRAMES
  726. static int show_frame_stats(int fd, int argc, char *argv[])
  727. {
  728. struct ast_frame *f;
  729. int x=1;
  730. if (argc != 3)
  731. return RESULT_SHOWUSAGE;
  732. ast_cli(fd, " Framer Statistics \n");
  733. ast_cli(fd, "---------------------------\n");
  734. ast_cli(fd, "Total allocated headers: %d\n", headers);
  735. ast_cli(fd, "Queue Dump:\n");
  736. ast_mutex_lock(&framelock);
  737. for (f=headerlist; f; f = f->next) {
  738. ast_cli(fd, "%d. Type %d, subclass %d from %s\n", x++, f->frametype, f->subclass, f->src ? f->src : "<Unknown>");
  739. }
  740. ast_mutex_unlock(&framelock);
  741. return RESULT_SUCCESS;
  742. }
  743. static char frame_stats_usage[] =
  744. "Usage: show frame stats\n"
  745. " Displays debugging statistics from framer\n";
  746. struct ast_cli_entry cli_frame_stats =
  747. { { "show", "frame", "stats", NULL }, show_frame_stats, "Shows frame statistics", frame_stats_usage };
  748. #endif
  749. int init_framer(void)
  750. {
  751. #ifdef TRACE_FRAMES
  752. ast_cli_register(&cli_frame_stats);
  753. #endif
  754. ast_cli_register(&cli_show_codecs);
  755. ast_cli_register(&cli_show_codecs_audio);
  756. ast_cli_register(&cli_show_codecs_video);
  757. ast_cli_register(&cli_show_codecs_image);
  758. ast_cli_register(&cli_show_codec_n);
  759. return 0;
  760. }
  761. void ast_codec_pref_shift(struct ast_codec_pref *pref, char *buf, size_t size, int right)
  762. {
  763. int x = 0, differential = 65, mem = 0;
  764. char *from = NULL, *to = NULL;
  765. if(right) {
  766. from = pref->order;
  767. to = buf;
  768. mem = size;
  769. } else {
  770. to = pref->order;
  771. from = buf;
  772. mem = 32;
  773. }
  774. memset(to, 0, mem);
  775. for (x = 0; x < 32 ; x++) {
  776. if(!from[x])
  777. break;
  778. to[x] = right ? (from[x] + differential) : (from[x] - differential);
  779. }
  780. }
  781. int ast_codec_pref_string(struct ast_codec_pref *pref, char *buf, size_t size)
  782. {
  783. int x = 0, codec = 0;
  784. size_t total_len = 0, slen = 0;
  785. char *formatname = 0;
  786. memset(buf,0,size);
  787. total_len = size;
  788. buf[0] = '(';
  789. total_len--;
  790. for(x = 0; x < 32 ; x++) {
  791. if(total_len <= 0)
  792. break;
  793. if(!(codec = ast_codec_pref_index(pref,x)))
  794. break;
  795. if((formatname = ast_getformatname(codec))) {
  796. slen = strlen(formatname);
  797. if(slen > total_len)
  798. break;
  799. strncat(buf,formatname,total_len);
  800. total_len -= slen;
  801. }
  802. if(total_len && x < 31 && ast_codec_pref_index(pref , x + 1)) {
  803. strncat(buf,"|",total_len);
  804. total_len--;
  805. }
  806. }
  807. if(total_len) {
  808. strncat(buf,")",total_len);
  809. total_len--;
  810. }
  811. return size - total_len;
  812. }
  813. int ast_codec_pref_index(struct ast_codec_pref *pref, int index)
  814. {
  815. int slot = 0;
  816. if((index >= 0) && (index < sizeof(pref->order))) {
  817. slot = pref->order[index];
  818. }
  819. return slot ? AST_FORMAT_LIST[slot-1].bits : 0;
  820. }
  821. /*--- ast_codec_pref_remove: Remove codec from pref list ---*/
  822. void ast_codec_pref_remove(struct ast_codec_pref *pref, int format)
  823. {
  824. struct ast_codec_pref oldorder;
  825. int x=0, y=0;
  826. size_t size = 0;
  827. int slot = 0;
  828. if(!pref->order[0])
  829. return;
  830. size = sizeof(AST_FORMAT_LIST) / sizeof(struct ast_format_list);
  831. memcpy(&oldorder,pref,sizeof(struct ast_codec_pref));
  832. memset(pref,0,sizeof(struct ast_codec_pref));
  833. for (x = 0; x < size; x++) {
  834. slot = oldorder.order[x];
  835. if(! slot)
  836. break;
  837. if(AST_FORMAT_LIST[slot-1].bits != format)
  838. pref->order[y++] = slot;
  839. }
  840. }
  841. /*--- ast_codec_pref_append: Append codec to list ---*/
  842. int ast_codec_pref_append(struct ast_codec_pref *pref, int format)
  843. {
  844. size_t size = 0;
  845. int x = 0, newindex = -1;
  846. ast_codec_pref_remove(pref, format);
  847. size = sizeof(AST_FORMAT_LIST) / sizeof(struct ast_format_list);
  848. for (x = 0; x < size; x++) {
  849. if(AST_FORMAT_LIST[x].bits == format) {
  850. newindex = x + 1;
  851. break;
  852. }
  853. }
  854. if(newindex) {
  855. for (x = 0; x < size; x++) {
  856. if(!pref->order[x]) {
  857. pref->order[x] = newindex;
  858. break;
  859. }
  860. }
  861. }
  862. return x;
  863. }
  864. /*--- sip_codec_choose: Pick a codec ---*/
  865. int ast_codec_choose(struct ast_codec_pref *pref, int formats, int find_best)
  866. {
  867. size_t size = 0;
  868. int x = 0, ret = 0, slot = 0;
  869. size = sizeof(AST_FORMAT_LIST) / sizeof(struct ast_format_list);
  870. for (x = 0; x < size; x++) {
  871. slot = pref->order[x];
  872. if(!slot)
  873. break;
  874. if ( formats & AST_FORMAT_LIST[slot-1].bits ) {
  875. ret = AST_FORMAT_LIST[slot-1].bits;
  876. break;
  877. }
  878. }
  879. if(ret)
  880. return ret;
  881. return find_best ? ast_best_codec(formats) : 0;
  882. }
  883. void ast_parse_allow_disallow(struct ast_codec_pref *pref, int *mask, char *list, int allowing)
  884. {
  885. int format_i = 0;
  886. char *next_format = NULL, *last_format = NULL;
  887. last_format = ast_strdupa(list);
  888. while(last_format) {
  889. if((next_format = strchr(last_format, ','))) {
  890. *next_format = '\0';
  891. next_format++;
  892. }
  893. if ((format_i = ast_getformatbyname(last_format)) > 0) {
  894. if (mask) {
  895. if (allowing)
  896. (*mask) |= format_i;
  897. else
  898. (*mask) &= ~format_i;
  899. }
  900. /* can't consider 'all' a prefered codec*/
  901. if(pref && strcasecmp(last_format, "all")) {
  902. if(allowing)
  903. ast_codec_pref_append(pref, format_i);
  904. else
  905. ast_codec_pref_remove(pref, format_i);
  906. } else if(!allowing) /* disallow all must clear your prefs or it makes no sense */
  907. memset(pref, 0, sizeof(struct ast_codec_pref));
  908. } else
  909. ast_log(LOG_WARNING, "Cannot %s unknown format '%s'\n", allowing ? "allow" : "disallow", last_format);
  910. last_format = next_format;
  911. }
  912. }