translate.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Translate via the use of pseudo channels
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <asterisk/lock.h>
  14. #include <asterisk/channel.h>
  15. #include <asterisk/channel_pvt.h>
  16. #include <asterisk/logger.h>
  17. #include <asterisk/translate.h>
  18. #include <asterisk/options.h>
  19. #include <asterisk/frame.h>
  20. #include <asterisk/sched.h>
  21. #include <asterisk/cli.h>
  22. #include <asterisk/term.h>
  23. #include <sys/socket.h>
  24. #include <sys/time.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <pthread.h>
  28. #include <string.h>
  29. #include <stdio.h>
  30. /* This could all be done more efficiently *IF* we chained packets together
  31. by default, but it would also complicate virtually every application. */
  32. static ast_mutex_t list_lock = AST_MUTEX_INITIALIZER;
  33. static struct ast_translator *list = NULL;
  34. struct ast_translator_dir {
  35. struct ast_translator *step; /* Next step translator */
  36. int cost; /* Complete cost to destination */
  37. };
  38. struct ast_frame_delivery {
  39. struct ast_frame *f;
  40. struct ast_channel *chan;
  41. int fd;
  42. struct translator_pvt *owner;
  43. struct ast_frame_delivery *prev;
  44. struct ast_frame_delivery *next;
  45. };
  46. static struct ast_translator_dir tr_matrix[MAX_FORMAT][MAX_FORMAT];
  47. struct ast_trans_pvt {
  48. struct ast_translator *step;
  49. struct ast_translator_pvt *state;
  50. struct ast_trans_pvt *next;
  51. };
  52. static int powerof(int d)
  53. {
  54. int x;
  55. for (x = 0; x < 32; x++)
  56. if ((1 << x) & d)
  57. return x;
  58. ast_log(LOG_WARNING, "Powerof %d: No power??\n", d);
  59. return -1;
  60. }
  61. void ast_translator_free_path(struct ast_trans_pvt *p)
  62. {
  63. struct ast_trans_pvt *pl, *pn;
  64. pn = p;
  65. while(pn) {
  66. pl = pn;
  67. pn = pn->next;
  68. if (pl->state && pl->step->destroy)
  69. pl->step->destroy(pl->state);
  70. free(pl);
  71. }
  72. }
  73. struct ast_trans_pvt *ast_translator_build_path(int dest, int source)
  74. {
  75. struct ast_trans_pvt *tmpr = NULL, *tmp = NULL;
  76. /* One of the hardest parts: Build a set of translators based upon
  77. the given source and destination formats */
  78. source = powerof(source);
  79. dest = powerof(dest);
  80. while(source != dest) {
  81. if (tr_matrix[source][dest].step) {
  82. if (tmp) {
  83. tmp->next = malloc(sizeof(struct ast_trans_pvt));
  84. tmp = tmp->next;
  85. } else
  86. tmp = malloc(sizeof(struct ast_trans_pvt));
  87. if (tmp) {
  88. tmp->next = NULL;
  89. tmp->step = tr_matrix[source][dest].step;
  90. tmp->state = tmp->step->new();
  91. if (!tmp->state) {
  92. ast_log(LOG_WARNING, "Failed to build translator step from %d to %d\n", source, dest);
  93. free(tmp);
  94. tmp = NULL;
  95. return NULL;
  96. }
  97. /* Set the root, if it doesn't exist yet... */
  98. if (!tmpr)
  99. tmpr = tmp;
  100. /* Keep going if this isn't the final destination */
  101. source = tmp->step->dstfmt;
  102. } else {
  103. /* XXX This could leak XXX */
  104. ast_log(LOG_WARNING, "Out of memory\n");
  105. return NULL;
  106. }
  107. } else {
  108. /* We shouldn't have allocated any memory */
  109. ast_log(LOG_WARNING, "No translator path from %s to %s\n",
  110. ast_getformatname(source), ast_getformatname(dest));
  111. return NULL;
  112. }
  113. }
  114. return tmpr;
  115. }
  116. struct ast_frame *ast_translate(struct ast_trans_pvt *path, struct ast_frame *f, int consume)
  117. {
  118. struct ast_trans_pvt *p;
  119. struct ast_frame *out;
  120. p = path;
  121. /* Feed the first frame into the first translator */
  122. p->step->framein(p->state, f);
  123. if (consume)
  124. ast_frfree(f);
  125. while(p) {
  126. out = p->step->frameout(p->state);
  127. /* If we get nothing out, return NULL */
  128. if (!out)
  129. return NULL;
  130. /* If there is a next state, feed it in there. If not,
  131. return this frame */
  132. if (p->next)
  133. p->next->step->framein(p->next->state, out);
  134. else
  135. return out;
  136. p = p->next;
  137. }
  138. ast_log(LOG_WARNING, "I should never get here...\n");
  139. return NULL;
  140. }
  141. static void rebuild_matrix(void)
  142. {
  143. struct ast_translator *t;
  144. int changed;
  145. int x,y,z;
  146. if (option_debug)
  147. ast_log(LOG_DEBUG, "Reseting translation matrix\n");
  148. /* Use the list of translators to build a translation matrix */
  149. bzero(tr_matrix, sizeof(tr_matrix));
  150. t = list;
  151. while(t) {
  152. if (!tr_matrix[t->srcfmt][t->dstfmt].step ||
  153. tr_matrix[t->srcfmt][t->dstfmt].cost > t->cost) {
  154. tr_matrix[t->srcfmt][t->dstfmt].step = t;
  155. tr_matrix[t->srcfmt][t->dstfmt].cost = t->cost;
  156. }
  157. t = t->next;
  158. }
  159. do {
  160. changed = 0;
  161. /* Don't you just love O(N^3) operations? */
  162. for (x=0; x< MAX_FORMAT; x++) /* For each source format */
  163. for (y=0; y < MAX_FORMAT; y++) /* And each destination format */
  164. if (x != y) /* Except ourselves, of course */
  165. for (z=0; z < MAX_FORMAT; z++) /* And each format it might convert to */
  166. if ((x!=z) && (y!=z)) /* Don't ever convert back to us */
  167. if (tr_matrix[x][y].step && /* We can convert from x to y */
  168. tr_matrix[y][z].step && /* And from y to z and... */
  169. (!tr_matrix[x][z].step || /* Either there isn't an x->z conversion */
  170. (tr_matrix[x][y].cost +
  171. tr_matrix[y][z].cost < /* Or we're cheaper than the existing */
  172. tr_matrix[x][z].cost) /* solution */
  173. )) {
  174. /* We can get from x to z via y with a cost that
  175. is the sum of the transition from x to y and
  176. from y to z */
  177. tr_matrix[x][z].step = tr_matrix[x][y].step;
  178. tr_matrix[x][z].cost = tr_matrix[x][y].cost +
  179. tr_matrix[y][z].cost;
  180. if (option_debug)
  181. ast_log(LOG_DEBUG, "Discovered %d cost path from %s to %s, via %d\n", tr_matrix[x][z].cost, ast_getformatname(x), ast_getformatname(z), y);
  182. changed++;
  183. }
  184. } while (changed);
  185. }
  186. static void calc_cost(struct ast_translator *t)
  187. {
  188. int sofar=0;
  189. struct ast_translator_pvt *pvt;
  190. struct ast_frame *f, *out;
  191. struct timeval start, finish;
  192. int cost;
  193. /* If they don't make samples, give them a terrible score */
  194. if (!t->sample) {
  195. ast_log(LOG_WARNING, "Translator '%s' does not produce sample frames.\n", t->name);
  196. t->cost = 99999;
  197. return;
  198. }
  199. pvt = t->new();
  200. if (!pvt) {
  201. ast_log(LOG_WARNING, "Translator '%s' appears to be broken and will probably fail.\n", t->name);
  202. t->cost = 99999;
  203. return;
  204. }
  205. gettimeofday(&start, NULL);
  206. /* Call the encoder until we've processed one second of time */
  207. while(sofar < 8000) {
  208. f = t->sample();
  209. if (!f) {
  210. ast_log(LOG_WARNING, "Translator '%s' failed to produce a sample frame.\n", t->name);
  211. t->destroy(pvt);
  212. t->cost = 99999;
  213. return;
  214. }
  215. t->framein(pvt, f);
  216. ast_frfree(f);
  217. while((out = t->frameout(pvt))) {
  218. sofar += out->samples;
  219. ast_frfree(out);
  220. }
  221. }
  222. gettimeofday(&finish, NULL);
  223. t->destroy(pvt);
  224. cost = (finish.tv_sec - start.tv_sec) * 1000 + (finish.tv_usec - start.tv_usec) / 1000;
  225. t->cost = cost;
  226. if (!t->cost)
  227. t->cost = 1;
  228. }
  229. static int show_translation(int fd, int argc, char *argv[])
  230. {
  231. #define SHOW_TRANS 11
  232. int x,y;
  233. char line[80];
  234. if (argc != 2)
  235. return RESULT_SHOWUSAGE;
  236. ast_cli(fd, " Translation times between formats (in milliseconds)\n");
  237. ast_cli(fd, " Source Format (Rows) Destination Format(Columns)\n\n");
  238. ast_mutex_lock(&list_lock);
  239. for (x=-1;x<SHOW_TRANS; x++) {
  240. strcpy(line, " ");
  241. for (y=-1;y<SHOW_TRANS;y++) {
  242. if (x >= 0 && y >= 0 && tr_matrix[x][y].step)
  243. snprintf(line + strlen(line), sizeof(line) - strlen(line), " %5d", tr_matrix[x][y].cost >= 99999 ? tr_matrix[x][y].cost-99999 : tr_matrix[x][y].cost);
  244. else
  245. if (((x == -1 && y >= 0) || (y == -1 && x >= 0))) {
  246. snprintf(line + strlen(line), sizeof(line) - strlen(line),
  247. " %5s", ast_getformatname(1<<(x+y+1)) );
  248. } else if (x != -1 && y != -1 ) {
  249. snprintf(line + strlen(line), sizeof(line) - strlen(line), " -");
  250. } else {
  251. snprintf(line + strlen(line), sizeof(line) - strlen(line), " ");
  252. }
  253. }
  254. snprintf(line + strlen(line), sizeof(line) - strlen(line), "\n");
  255. ast_cli(fd, line);
  256. }
  257. ast_mutex_unlock(&list_lock);
  258. return RESULT_SUCCESS;
  259. }
  260. static int added_cli = 0;
  261. static char show_trans_usage[] =
  262. "Usage: show translation\n"
  263. " Displays known codec translators and the cost associated\n"
  264. "with each conversion.\n";
  265. static struct ast_cli_entry show_trans =
  266. { { "show", "translation", NULL }, show_translation, "Display translation matrix", show_trans_usage };
  267. int ast_register_translator(struct ast_translator *t)
  268. {
  269. char tmp[80];
  270. t->srcfmt = powerof(t->srcfmt);
  271. t->dstfmt = powerof(t->dstfmt);
  272. if ((t->srcfmt >= MAX_FORMAT) || (t->dstfmt >= MAX_FORMAT)) {
  273. ast_log(LOG_WARNING, "Format %s is larger than MAX_FORMAT\n", ast_getformatname(t->srcfmt));
  274. return -1;
  275. }
  276. calc_cost(t);
  277. if (option_verbose > 1)
  278. ast_verbose(VERBOSE_PREFIX_2 "Registered translator '%s' from format %s to %s, cost %d\n", term_color(tmp, t->name, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp)), ast_getformatname(1 << t->srcfmt), ast_getformatname(1 << t->dstfmt), t->cost);
  279. ast_mutex_lock(&list_lock);
  280. if (!added_cli) {
  281. ast_cli_register(&show_trans);
  282. added_cli++;
  283. }
  284. t->next = list;
  285. list = t;
  286. rebuild_matrix();
  287. ast_mutex_unlock(&list_lock);
  288. return 0;
  289. }
  290. int ast_unregister_translator(struct ast_translator *t)
  291. {
  292. struct ast_translator *u, *ul = NULL;
  293. ast_mutex_lock(&list_lock);
  294. u = list;
  295. while(u) {
  296. if (u == t) {
  297. if (ul)
  298. ul->next = u->next;
  299. else
  300. list = u->next;
  301. break;
  302. }
  303. ul = u;
  304. u = u->next;
  305. }
  306. rebuild_matrix();
  307. ast_mutex_unlock(&list_lock);
  308. return (u ? 0 : -1);
  309. }
  310. int ast_translator_best_choice(int *dst, int *srcs)
  311. {
  312. /* Calculate our best source format, given costs, and a desired destination */
  313. int x,y;
  314. int best=-1;
  315. int bestdst=0;
  316. int cur = 1;
  317. int besttime=999999999;
  318. ast_mutex_lock(&list_lock);
  319. for (y=0;y<MAX_FORMAT;y++) {
  320. if ((cur & *dst) && (cur & *srcs)) {
  321. /* This is a common format to both. Pick it if we don't have one already */
  322. besttime=0;
  323. bestdst = cur;
  324. best = cur;
  325. break;
  326. }
  327. if (cur & *dst)
  328. for (x=0;x<MAX_FORMAT;x++) {
  329. if (tr_matrix[x][y].step && /* There's a step */
  330. (tr_matrix[x][y].cost < besttime) && /* We're better than what exists now */
  331. (*srcs & (1 << x))) /* x is a valid source format */
  332. {
  333. best = 1 << x;
  334. bestdst = cur;
  335. besttime = tr_matrix[x][y].cost;
  336. }
  337. }
  338. cur = cur << 1;
  339. }
  340. if (best > -1) {
  341. *srcs = best;
  342. *dst = bestdst;
  343. best = 0;
  344. }
  345. ast_mutex_unlock(&list_lock);
  346. return best;
  347. }