audiohook.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2007, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@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 Audiohooks Architecture
  21. *
  22. * \author Joshua Colp <jcolp@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include <signal.h>
  30. #include "asterisk/channel.h"
  31. #include "asterisk/utils.h"
  32. #include "asterisk/lock.h"
  33. #include "asterisk/linkedlists.h"
  34. #include "asterisk/audiohook.h"
  35. #include "asterisk/slinfactory.h"
  36. #include "asterisk/frame.h"
  37. #include "asterisk/translate.h"
  38. #include "asterisk/format_cache.h"
  39. #define AST_AUDIOHOOK_SYNC_TOLERANCE 100 /*!< Tolerance in milliseconds for audiohooks synchronization */
  40. #define AST_AUDIOHOOK_SMALL_QUEUE_TOLERANCE 100 /*!< When small queue is enabled, this is the maximum amount of audio that can remain queued at a time. */
  41. #define DEFAULT_INTERNAL_SAMPLE_RATE 8000
  42. struct ast_audiohook_translate {
  43. struct ast_trans_pvt *trans_pvt;
  44. struct ast_format *format;
  45. };
  46. struct ast_audiohook_list {
  47. /* If all the audiohooks in this list are capable
  48. * of processing slinear at any sample rate, this
  49. * variable will be set and the sample rate will
  50. * be preserved during ast_audiohook_write_list()*/
  51. int native_slin_compatible;
  52. int list_internal_samp_rate;/*!< Internal sample rate used when writing to the audiohook list */
  53. struct ast_audiohook_translate in_translate[2];
  54. struct ast_audiohook_translate out_translate[2];
  55. AST_LIST_HEAD_NOLOCK(, ast_audiohook) spy_list;
  56. AST_LIST_HEAD_NOLOCK(, ast_audiohook) whisper_list;
  57. AST_LIST_HEAD_NOLOCK(, ast_audiohook) manipulate_list;
  58. };
  59. static int audiohook_set_internal_rate(struct ast_audiohook *audiohook, int rate, int reset)
  60. {
  61. struct ast_format *slin;
  62. if (audiohook->hook_internal_samp_rate == rate) {
  63. return 0;
  64. }
  65. audiohook->hook_internal_samp_rate = rate;
  66. slin = ast_format_cache_get_slin_by_rate(rate);
  67. /* Setup the factories that are needed for this audiohook type */
  68. switch (audiohook->type) {
  69. case AST_AUDIOHOOK_TYPE_SPY:
  70. case AST_AUDIOHOOK_TYPE_WHISPER:
  71. if (reset) {
  72. ast_slinfactory_destroy(&audiohook->read_factory);
  73. ast_slinfactory_destroy(&audiohook->write_factory);
  74. }
  75. ast_slinfactory_init_with_format(&audiohook->read_factory, slin);
  76. ast_slinfactory_init_with_format(&audiohook->write_factory, slin);
  77. break;
  78. default:
  79. break;
  80. }
  81. return 0;
  82. }
  83. /*! \brief Initialize an audiohook structure
  84. *
  85. * \param audiohook Audiohook structure
  86. * \param type
  87. * \param source, init_flags
  88. *
  89. * \return Returns 0 on success, -1 on failure
  90. */
  91. int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source, enum ast_audiohook_init_flags init_flags)
  92. {
  93. /* Need to keep the type and source */
  94. audiohook->type = type;
  95. audiohook->source = source;
  96. /* Initialize lock that protects our audiohook */
  97. ast_mutex_init(&audiohook->lock);
  98. ast_cond_init(&audiohook->trigger, NULL);
  99. audiohook->init_flags = init_flags;
  100. /* initialize internal rate at 8khz, this will adjust if necessary */
  101. audiohook_set_internal_rate(audiohook, DEFAULT_INTERNAL_SAMPLE_RATE, 0);
  102. /* Since we are just starting out... this audiohook is new */
  103. ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_NEW);
  104. return 0;
  105. }
  106. /*! \brief Destroys an audiohook structure
  107. * \param audiohook Audiohook structure
  108. * \return Returns 0 on success, -1 on failure
  109. */
  110. int ast_audiohook_destroy(struct ast_audiohook *audiohook)
  111. {
  112. /* Drop the factories used by this audiohook type */
  113. switch (audiohook->type) {
  114. case AST_AUDIOHOOK_TYPE_SPY:
  115. case AST_AUDIOHOOK_TYPE_WHISPER:
  116. ast_slinfactory_destroy(&audiohook->read_factory);
  117. ast_slinfactory_destroy(&audiohook->write_factory);
  118. break;
  119. default:
  120. break;
  121. }
  122. /* Destroy translation path if present */
  123. if (audiohook->trans_pvt)
  124. ast_translator_free_path(audiohook->trans_pvt);
  125. ao2_cleanup(audiohook->format);
  126. /* Lock and trigger be gone! */
  127. ast_cond_destroy(&audiohook->trigger);
  128. ast_mutex_destroy(&audiohook->lock);
  129. return 0;
  130. }
  131. #define SHOULD_MUTE(hook, dir) \
  132. ((ast_test_flag(hook, AST_AUDIOHOOK_MUTE_READ) && (dir == AST_AUDIOHOOK_DIRECTION_READ)) || \
  133. (ast_test_flag(hook, AST_AUDIOHOOK_MUTE_WRITE) && (dir == AST_AUDIOHOOK_DIRECTION_WRITE)) || \
  134. (ast_test_flag(hook, AST_AUDIOHOOK_MUTE_READ | AST_AUDIOHOOK_MUTE_WRITE) == (AST_AUDIOHOOK_MUTE_READ | AST_AUDIOHOOK_MUTE_WRITE)))
  135. /*! \brief Writes a frame into the audiohook structure
  136. * \param audiohook Audiohook structure
  137. * \param direction Direction the audio frame came from
  138. * \param frame Frame to write in
  139. * \return Returns 0 on success, -1 on failure
  140. */
  141. int ast_audiohook_write_frame(struct ast_audiohook *audiohook, enum ast_audiohook_direction direction, struct ast_frame *frame)
  142. {
  143. struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
  144. struct ast_slinfactory *other_factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->write_factory : &audiohook->read_factory);
  145. struct timeval *rwtime = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_time : &audiohook->write_time), previous_time = *rwtime;
  146. int our_factory_samples;
  147. int our_factory_ms;
  148. int other_factory_samples;
  149. int other_factory_ms;
  150. /* Update last feeding time to be current */
  151. *rwtime = ast_tvnow();
  152. our_factory_samples = ast_slinfactory_available(factory);
  153. our_factory_ms = ast_tvdiff_ms(*rwtime, previous_time) + (our_factory_samples / (audiohook->hook_internal_samp_rate / 1000));
  154. other_factory_samples = ast_slinfactory_available(other_factory);
  155. other_factory_ms = other_factory_samples / (audiohook->hook_internal_samp_rate / 1000);
  156. if (ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_SYNC) && (our_factory_ms - other_factory_ms > AST_AUDIOHOOK_SYNC_TOLERANCE)) {
  157. ast_debug(1, "Flushing audiohook %p so it remains in sync\n", audiohook);
  158. ast_slinfactory_flush(factory);
  159. ast_slinfactory_flush(other_factory);
  160. }
  161. if (ast_test_flag(audiohook, AST_AUDIOHOOK_SMALL_QUEUE) && ((our_factory_ms > AST_AUDIOHOOK_SMALL_QUEUE_TOLERANCE) || (other_factory_ms > AST_AUDIOHOOK_SMALL_QUEUE_TOLERANCE))) {
  162. ast_debug(1, "Audiohook %p has stale audio in its factories. Flushing them both\n", audiohook);
  163. ast_slinfactory_flush(factory);
  164. ast_slinfactory_flush(other_factory);
  165. }
  166. /* Write frame out to respective factory */
  167. ast_slinfactory_feed(factory, frame);
  168. /* If we need to notify the respective handler of this audiohook, do so */
  169. if ((ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_MODE) == AST_AUDIOHOOK_TRIGGER_READ) && (direction == AST_AUDIOHOOK_DIRECTION_READ)) {
  170. ast_cond_signal(&audiohook->trigger);
  171. } else if ((ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_MODE) == AST_AUDIOHOOK_TRIGGER_WRITE) && (direction == AST_AUDIOHOOK_DIRECTION_WRITE)) {
  172. ast_cond_signal(&audiohook->trigger);
  173. } else if (ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_SYNC)) {
  174. ast_cond_signal(&audiohook->trigger);
  175. }
  176. return 0;
  177. }
  178. static struct ast_frame *audiohook_read_frame_single(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction)
  179. {
  180. struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
  181. int vol = (direction == AST_AUDIOHOOK_DIRECTION_READ ? audiohook->options.read_volume : audiohook->options.write_volume);
  182. short buf[samples];
  183. struct ast_frame frame = {
  184. .frametype = AST_FRAME_VOICE,
  185. .subclass.format = ast_format_cache_get_slin_by_rate(audiohook->hook_internal_samp_rate),
  186. .data.ptr = buf,
  187. .datalen = sizeof(buf),
  188. .samples = samples,
  189. };
  190. /* Ensure the factory is able to give us the samples we want */
  191. if (samples > ast_slinfactory_available(factory)) {
  192. return NULL;
  193. }
  194. /* Read data in from factory */
  195. if (!ast_slinfactory_read(factory, buf, samples)) {
  196. return NULL;
  197. }
  198. if (SHOULD_MUTE(audiohook, direction)) {
  199. /* Swap frame data for zeros if mute is required */
  200. ast_frame_clear(&frame);
  201. } else if (vol) {
  202. /* If a volume adjustment needs to be applied apply it */
  203. ast_frame_adjust_volume(&frame, vol);
  204. }
  205. return ast_frdup(&frame);
  206. }
  207. static struct ast_frame *audiohook_read_frame_both(struct ast_audiohook *audiohook, size_t samples, struct ast_frame **read_reference, struct ast_frame **write_reference)
  208. {
  209. int count;
  210. int usable_read;
  211. int usable_write;
  212. short adjust_value;
  213. short buf1[samples];
  214. short buf2[samples];
  215. short *read_buf = NULL;
  216. short *write_buf = NULL;
  217. struct ast_frame frame = {
  218. .frametype = AST_FRAME_VOICE,
  219. .datalen = sizeof(buf1),
  220. .samples = samples,
  221. };
  222. /* Make sure both factories have the required samples */
  223. usable_read = (ast_slinfactory_available(&audiohook->read_factory) >= samples ? 1 : 0);
  224. usable_write = (ast_slinfactory_available(&audiohook->write_factory) >= samples ? 1 : 0);
  225. if (!usable_read && !usable_write) {
  226. /* If both factories are unusable bail out */
  227. ast_debug(1, "Read factory %p and write factory %p both fail to provide %zu samples\n", &audiohook->read_factory, &audiohook->write_factory, samples);
  228. return NULL;
  229. }
  230. /* If we want to provide only a read factory make sure we aren't waiting for other audio */
  231. if (usable_read && !usable_write && (ast_tvdiff_ms(ast_tvnow(), audiohook->write_time) < (samples/8)*2)) {
  232. ast_debug(3, "Write factory %p was pretty quick last time, waiting for them.\n", &audiohook->write_factory);
  233. return NULL;
  234. }
  235. /* If we want to provide only a write factory make sure we aren't waiting for other audio */
  236. if (usable_write && !usable_read && (ast_tvdiff_ms(ast_tvnow(), audiohook->read_time) < (samples/8)*2)) {
  237. ast_debug(3, "Read factory %p was pretty quick last time, waiting for them.\n", &audiohook->read_factory);
  238. return NULL;
  239. }
  240. /* Start with the read factory... if there are enough samples, read them in */
  241. if (usable_read) {
  242. if (ast_slinfactory_read(&audiohook->read_factory, buf1, samples)) {
  243. read_buf = buf1;
  244. if ((ast_test_flag(audiohook, AST_AUDIOHOOK_MUTE_READ))) {
  245. /* Clear the frame data if we are muting */
  246. memset(buf1, 0, sizeof(buf1));
  247. } else if (audiohook->options.read_volume) {
  248. /* Adjust read volume if need be */
  249. adjust_value = abs(audiohook->options.read_volume);
  250. for (count = 0; count < samples; count++) {
  251. if (audiohook->options.read_volume > 0) {
  252. ast_slinear_saturated_multiply(&buf1[count], &adjust_value);
  253. } else if (audiohook->options.read_volume < 0) {
  254. ast_slinear_saturated_divide(&buf1[count], &adjust_value);
  255. }
  256. }
  257. }
  258. }
  259. } else {
  260. ast_debug(1, "Failed to get %d samples from read factory %p\n", (int)samples, &audiohook->read_factory);
  261. }
  262. /* Move on to the write factory... if there are enough samples, read them in */
  263. if (usable_write) {
  264. if (ast_slinfactory_read(&audiohook->write_factory, buf2, samples)) {
  265. write_buf = buf2;
  266. if ((ast_test_flag(audiohook, AST_AUDIOHOOK_MUTE_WRITE))) {
  267. /* Clear the frame data if we are muting */
  268. memset(buf2, 0, sizeof(buf2));
  269. } else if (audiohook->options.write_volume) {
  270. /* Adjust write volume if need be */
  271. adjust_value = abs(audiohook->options.write_volume);
  272. for (count = 0; count < samples; count++) {
  273. if (audiohook->options.write_volume > 0) {
  274. ast_slinear_saturated_multiply(&buf2[count], &adjust_value);
  275. } else if (audiohook->options.write_volume < 0) {
  276. ast_slinear_saturated_divide(&buf2[count], &adjust_value);
  277. }
  278. }
  279. }
  280. }
  281. } else {
  282. ast_debug(1, "Failed to get %d samples from write factory %p\n", (int)samples, &audiohook->write_factory);
  283. }
  284. frame.subclass.format = ast_format_cache_get_slin_by_rate(audiohook->hook_internal_samp_rate);
  285. /* Basically we figure out which buffer to use... and if mixing can be done here */
  286. if (read_buf && read_reference) {
  287. frame.data.ptr = read_buf;
  288. *read_reference = ast_frdup(&frame);
  289. }
  290. if (write_buf && write_reference) {
  291. frame.data.ptr = write_buf;
  292. *write_reference = ast_frdup(&frame);
  293. }
  294. /* Make the correct buffer part of the built frame, so it gets duplicated. */
  295. if (read_buf) {
  296. frame.data.ptr = read_buf;
  297. if (write_buf) {
  298. for (count = 0; count < samples; count++) {
  299. ast_slinear_saturated_add(read_buf++, write_buf++);
  300. }
  301. }
  302. } else if (write_buf) {
  303. frame.data.ptr = write_buf;
  304. } else {
  305. return NULL;
  306. }
  307. /* Yahoo, a combined copy of the audio! */
  308. return ast_frdup(&frame);
  309. }
  310. static struct ast_frame *audiohook_read_frame_helper(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction, struct ast_format *format, struct ast_frame **read_reference, struct ast_frame **write_reference)
  311. {
  312. struct ast_frame *read_frame = NULL, *final_frame = NULL;
  313. struct ast_format *slin;
  314. /*
  315. * Update the rate if compatibility mode is turned off or if it is
  316. * turned on and the format rate is higher than the current rate.
  317. *
  318. * This makes it so any unnecessary rate switching/resetting does
  319. * not take place and also any associated audiohook_list's internal
  320. * sample rate maintains the highest sample rate between hooks.
  321. */
  322. if (!ast_test_flag(audiohook, AST_AUDIOHOOK_COMPATIBLE) ||
  323. (ast_test_flag(audiohook, AST_AUDIOHOOK_COMPATIBLE) &&
  324. ast_format_get_sample_rate(format) > audiohook->hook_internal_samp_rate)) {
  325. audiohook_set_internal_rate(audiohook, ast_format_get_sample_rate(format), 1);
  326. }
  327. /* If the sample rate of the requested format differs from that of the underlying audiohook
  328. * sample rate determine how many samples we actually need to get from the audiohook. This
  329. * needs to occur as the signed linear factory stores them at the rate of the audiohook.
  330. * We do this by determining the duration of audio they've requested and then determining
  331. * how many samples that would be in the audiohook format.
  332. */
  333. if (ast_format_get_sample_rate(format) != audiohook->hook_internal_samp_rate) {
  334. samples = (audiohook->hook_internal_samp_rate / 1000) * (samples / (ast_format_get_sample_rate(format) / 1000));
  335. }
  336. if (!(read_frame = (direction == AST_AUDIOHOOK_DIRECTION_BOTH ?
  337. audiohook_read_frame_both(audiohook, samples, read_reference, write_reference) :
  338. audiohook_read_frame_single(audiohook, samples, direction)))) {
  339. return NULL;
  340. }
  341. slin = ast_format_cache_get_slin_by_rate(audiohook->hook_internal_samp_rate);
  342. /* If they don't want signed linear back out, we'll have to send it through the translation path */
  343. if (ast_format_cmp(format, slin) != AST_FORMAT_CMP_EQUAL) {
  344. /* Rebuild translation path if different format then previously */
  345. if (ast_format_cmp(format, audiohook->format) == AST_FORMAT_CMP_NOT_EQUAL) {
  346. if (audiohook->trans_pvt) {
  347. ast_translator_free_path(audiohook->trans_pvt);
  348. audiohook->trans_pvt = NULL;
  349. }
  350. /* Setup new translation path for this format... if we fail we can't very well return signed linear so free the frame and return nothing */
  351. if (!(audiohook->trans_pvt = ast_translator_build_path(format, slin))) {
  352. ast_frfree(read_frame);
  353. return NULL;
  354. }
  355. ao2_replace(audiohook->format, format);
  356. }
  357. /* Convert to requested format, and allow the read in frame to be freed */
  358. final_frame = ast_translate(audiohook->trans_pvt, read_frame, 1);
  359. } else {
  360. final_frame = read_frame;
  361. }
  362. return final_frame;
  363. }
  364. /*! \brief Reads a frame in from the audiohook structure
  365. * \param audiohook Audiohook structure
  366. * \param samples Number of samples wanted in requested output format
  367. * \param direction Direction the audio frame came from
  368. * \param format Format of frame remote side wants back
  369. * \return Returns frame on success, NULL on failure
  370. */
  371. struct ast_frame *ast_audiohook_read_frame(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction, struct ast_format *format)
  372. {
  373. return audiohook_read_frame_helper(audiohook, samples, direction, format, NULL, NULL);
  374. }
  375. /*! \brief Reads a frame in from the audiohook structure
  376. * \param audiohook Audiohook structure
  377. * \param samples Number of samples wanted
  378. * \param direction Direction the audio frame came from
  379. * \param format Format of frame remote side wants back
  380. * \param read_frame frame pointer for copying read frame data
  381. * \param write_frame frame pointer for copying write frame data
  382. * \return Returns frame on success, NULL on failure
  383. */
  384. struct ast_frame *ast_audiohook_read_frame_all(struct ast_audiohook *audiohook, size_t samples, struct ast_format *format, struct ast_frame **read_frame, struct ast_frame **write_frame)
  385. {
  386. return audiohook_read_frame_helper(audiohook, samples, AST_AUDIOHOOK_DIRECTION_BOTH, format, read_frame, write_frame);
  387. }
  388. static void audiohook_list_set_samplerate_compatibility(struct ast_audiohook_list *audiohook_list)
  389. {
  390. struct ast_audiohook *ah = NULL;
  391. /*
  392. * Anytime the samplerate compatibility is set (attach/remove an audiohook) the
  393. * list's internal sample rate needs to be reset so that the next time processing
  394. * through write_list, if needed, it will get updated to the correct rate.
  395. *
  396. * A list's internal rate always chooses the higher between its own rate and a
  397. * given rate. If the current rate is being driven by an audiohook that wanted a
  398. * higher rate then when this audiohook is removed the list's rate would remain
  399. * at that level when it should be lower, and with no way to lower it since any
  400. * rate compared against it would be lower.
  401. *
  402. * By setting it back to the lowest rate it can recalulate the new highest rate.
  403. */
  404. audiohook_list->list_internal_samp_rate = DEFAULT_INTERNAL_SAMPLE_RATE;
  405. audiohook_list->native_slin_compatible = 1;
  406. AST_LIST_TRAVERSE(&audiohook_list->manipulate_list, ah, list) {
  407. if (!(ah->init_flags & AST_AUDIOHOOK_MANIPULATE_ALL_RATES)) {
  408. audiohook_list->native_slin_compatible = 0;
  409. return;
  410. }
  411. }
  412. }
  413. /*! \brief Attach audiohook to channel
  414. * \param chan Channel
  415. * \param audiohook Audiohook structure
  416. * \return Returns 0 on success, -1 on failure
  417. */
  418. int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
  419. {
  420. ast_channel_lock(chan);
  421. if (!ast_channel_audiohooks(chan)) {
  422. struct ast_audiohook_list *ahlist;
  423. /* Whoops... allocate a new structure */
  424. if (!(ahlist = ast_calloc(1, sizeof(*ahlist)))) {
  425. ast_channel_unlock(chan);
  426. return -1;
  427. }
  428. ast_channel_audiohooks_set(chan, ahlist);
  429. AST_LIST_HEAD_INIT_NOLOCK(&ast_channel_audiohooks(chan)->spy_list);
  430. AST_LIST_HEAD_INIT_NOLOCK(&ast_channel_audiohooks(chan)->whisper_list);
  431. AST_LIST_HEAD_INIT_NOLOCK(&ast_channel_audiohooks(chan)->manipulate_list);
  432. /* This sample rate will adjust as necessary when writing to the list. */
  433. ast_channel_audiohooks(chan)->list_internal_samp_rate = DEFAULT_INTERNAL_SAMPLE_RATE;
  434. }
  435. /* Drop into respective list */
  436. if (audiohook->type == AST_AUDIOHOOK_TYPE_SPY) {
  437. AST_LIST_INSERT_TAIL(&ast_channel_audiohooks(chan)->spy_list, audiohook, list);
  438. } else if (audiohook->type == AST_AUDIOHOOK_TYPE_WHISPER) {
  439. AST_LIST_INSERT_TAIL(&ast_channel_audiohooks(chan)->whisper_list, audiohook, list);
  440. } else if (audiohook->type == AST_AUDIOHOOK_TYPE_MANIPULATE) {
  441. AST_LIST_INSERT_TAIL(&ast_channel_audiohooks(chan)->manipulate_list, audiohook, list);
  442. }
  443. /*
  444. * Initialize the audiohook's rate to the default. If it needs to be,
  445. * it will get updated later.
  446. */
  447. audiohook_set_internal_rate(audiohook, DEFAULT_INTERNAL_SAMPLE_RATE, 1);
  448. audiohook_list_set_samplerate_compatibility(ast_channel_audiohooks(chan));
  449. /* Change status over to running since it is now attached */
  450. ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_RUNNING);
  451. if (ast_channel_is_bridged(chan)) {
  452. ast_channel_set_unbridged_nolock(chan, 1);
  453. }
  454. ast_channel_unlock(chan);
  455. return 0;
  456. }
  457. /*! \brief Update audiohook's status
  458. * \param audiohook Audiohook structure
  459. * \param status Audiohook status enum
  460. *
  461. * \note once status is updated to DONE, this function can not be used to set the
  462. * status back to any other setting. Setting DONE effectively locks the status as such.
  463. */
  464. void ast_audiohook_update_status(struct ast_audiohook *audiohook, enum ast_audiohook_status status)
  465. {
  466. ast_audiohook_lock(audiohook);
  467. if (audiohook->status != AST_AUDIOHOOK_STATUS_DONE) {
  468. audiohook->status = status;
  469. ast_cond_signal(&audiohook->trigger);
  470. }
  471. ast_audiohook_unlock(audiohook);
  472. }
  473. /*! \brief Detach audiohook from channel
  474. * \param audiohook Audiohook structure
  475. * \return Returns 0 on success, -1 on failure
  476. */
  477. int ast_audiohook_detach(struct ast_audiohook *audiohook)
  478. {
  479. if (audiohook->status == AST_AUDIOHOOK_STATUS_NEW || audiohook->status == AST_AUDIOHOOK_STATUS_DONE) {
  480. return 0;
  481. }
  482. ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_SHUTDOWN);
  483. while (audiohook->status != AST_AUDIOHOOK_STATUS_DONE) {
  484. ast_audiohook_trigger_wait(audiohook);
  485. }
  486. return 0;
  487. }
  488. void ast_audiohook_detach_list(struct ast_audiohook_list *audiohook_list)
  489. {
  490. int i;
  491. struct ast_audiohook *audiohook;
  492. if (!audiohook_list) {
  493. return;
  494. }
  495. /* Drop any spies */
  496. while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->spy_list, list))) {
  497. ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
  498. }
  499. /* Drop any whispering sources */
  500. while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->whisper_list, list))) {
  501. ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
  502. }
  503. /* Drop any manipulaters */
  504. while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->manipulate_list, list))) {
  505. ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
  506. audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
  507. }
  508. /* Drop translation paths if present */
  509. for (i = 0; i < 2; i++) {
  510. if (audiohook_list->in_translate[i].trans_pvt) {
  511. ast_translator_free_path(audiohook_list->in_translate[i].trans_pvt);
  512. ao2_cleanup(audiohook_list->in_translate[i].format);
  513. }
  514. if (audiohook_list->out_translate[i].trans_pvt) {
  515. ast_translator_free_path(audiohook_list->out_translate[i].trans_pvt);
  516. ao2_cleanup(audiohook_list->in_translate[i].format);
  517. }
  518. }
  519. /* Free ourselves */
  520. ast_free(audiohook_list);
  521. }
  522. /*! \brief find an audiohook based on its source
  523. * \param audiohook_list The list of audiohooks to search in
  524. * \param source The source of the audiohook we wish to find
  525. * \return Return the corresponding audiohook or NULL if it cannot be found.
  526. */
  527. static struct ast_audiohook *find_audiohook_by_source(struct ast_audiohook_list *audiohook_list, const char *source)
  528. {
  529. struct ast_audiohook *audiohook = NULL;
  530. AST_LIST_TRAVERSE(&audiohook_list->spy_list, audiohook, list) {
  531. if (!strcasecmp(audiohook->source, source)) {
  532. return audiohook;
  533. }
  534. }
  535. AST_LIST_TRAVERSE(&audiohook_list->whisper_list, audiohook, list) {
  536. if (!strcasecmp(audiohook->source, source)) {
  537. return audiohook;
  538. }
  539. }
  540. AST_LIST_TRAVERSE(&audiohook_list->manipulate_list, audiohook, list) {
  541. if (!strcasecmp(audiohook->source, source)) {
  542. return audiohook;
  543. }
  544. }
  545. return NULL;
  546. }
  547. static void audiohook_move(struct ast_channel *old_chan, struct ast_channel *new_chan, struct ast_audiohook *audiohook)
  548. {
  549. enum ast_audiohook_status oldstatus;
  550. /* By locking both channels and the audiohook, we can assure that
  551. * another thread will not have a chance to read the audiohook's status
  552. * as done, even though ast_audiohook_remove signals the trigger
  553. * condition.
  554. */
  555. ast_audiohook_lock(audiohook);
  556. oldstatus = audiohook->status;
  557. ast_audiohook_remove(old_chan, audiohook);
  558. ast_audiohook_attach(new_chan, audiohook);
  559. audiohook->status = oldstatus;
  560. ast_audiohook_unlock(audiohook);
  561. }
  562. void ast_audiohook_move_by_source(struct ast_channel *old_chan, struct ast_channel *new_chan, const char *source)
  563. {
  564. struct ast_audiohook *audiohook;
  565. if (!ast_channel_audiohooks(old_chan)) {
  566. return;
  567. }
  568. audiohook = find_audiohook_by_source(ast_channel_audiohooks(old_chan), source);
  569. if (!audiohook) {
  570. return;
  571. }
  572. audiohook_move(old_chan, new_chan, audiohook);
  573. }
  574. void ast_audiohook_move_all(struct ast_channel *old_chan, struct ast_channel *new_chan)
  575. {
  576. struct ast_audiohook *audiohook;
  577. struct ast_audiohook_list *audiohook_list;
  578. audiohook_list = ast_channel_audiohooks(old_chan);
  579. if (!audiohook_list) {
  580. return;
  581. }
  582. AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) {
  583. audiohook_move(old_chan, new_chan, audiohook);
  584. }
  585. AST_LIST_TRAVERSE_SAFE_END;
  586. AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) {
  587. audiohook_move(old_chan, new_chan, audiohook);
  588. }
  589. AST_LIST_TRAVERSE_SAFE_END;
  590. AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
  591. audiohook_move(old_chan, new_chan, audiohook);
  592. }
  593. AST_LIST_TRAVERSE_SAFE_END;
  594. }
  595. /*! \brief Detach specified source audiohook from channel
  596. * \param chan Channel to detach from
  597. * \param source Name of source to detach
  598. * \return Returns 0 on success, -1 on failure
  599. */
  600. int ast_audiohook_detach_source(struct ast_channel *chan, const char *source)
  601. {
  602. struct ast_audiohook *audiohook = NULL;
  603. ast_channel_lock(chan);
  604. /* Ensure the channel has audiohooks on it */
  605. if (!ast_channel_audiohooks(chan)) {
  606. ast_channel_unlock(chan);
  607. return -1;
  608. }
  609. audiohook = find_audiohook_by_source(ast_channel_audiohooks(chan), source);
  610. ast_channel_unlock(chan);
  611. if (audiohook && audiohook->status != AST_AUDIOHOOK_STATUS_DONE) {
  612. ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_SHUTDOWN);
  613. }
  614. return (audiohook ? 0 : -1);
  615. }
  616. /*!
  617. * \brief Remove an audiohook from a specified channel
  618. *
  619. * \param chan Channel to remove from
  620. * \param audiohook Audiohook to remove
  621. *
  622. * \return Returns 0 on success, -1 on failure
  623. *
  624. * \note The channel does not need to be locked before calling this function
  625. */
  626. int ast_audiohook_remove(struct ast_channel *chan, struct ast_audiohook *audiohook)
  627. {
  628. ast_channel_lock(chan);
  629. if (!ast_channel_audiohooks(chan)) {
  630. ast_channel_unlock(chan);
  631. return -1;
  632. }
  633. if (audiohook->type == AST_AUDIOHOOK_TYPE_SPY) {
  634. AST_LIST_REMOVE(&ast_channel_audiohooks(chan)->spy_list, audiohook, list);
  635. } else if (audiohook->type == AST_AUDIOHOOK_TYPE_WHISPER) {
  636. AST_LIST_REMOVE(&ast_channel_audiohooks(chan)->whisper_list, audiohook, list);
  637. } else if (audiohook->type == AST_AUDIOHOOK_TYPE_MANIPULATE) {
  638. AST_LIST_REMOVE(&ast_channel_audiohooks(chan)->manipulate_list, audiohook, list);
  639. }
  640. audiohook_list_set_samplerate_compatibility(ast_channel_audiohooks(chan));
  641. ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
  642. if (ast_channel_is_bridged(chan)) {
  643. ast_channel_set_unbridged_nolock(chan, 1);
  644. }
  645. ast_channel_unlock(chan);
  646. return 0;
  647. }
  648. /*! \brief Pass a DTMF frame off to be handled by the audiohook core
  649. * \param chan Channel that the list is coming off of
  650. * \param audiohook_list List of audiohooks
  651. * \param direction Direction frame is coming in from
  652. * \param frame The frame itself
  653. * \return Return frame on success, NULL on failure
  654. */
  655. static struct ast_frame *dtmf_audiohook_write_list(struct ast_channel *chan, struct ast_audiohook_list *audiohook_list, enum ast_audiohook_direction direction, struct ast_frame *frame)
  656. {
  657. struct ast_audiohook *audiohook = NULL;
  658. int removed = 0;
  659. AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
  660. ast_audiohook_lock(audiohook);
  661. if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
  662. AST_LIST_REMOVE_CURRENT(list);
  663. removed = 1;
  664. ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
  665. ast_audiohook_unlock(audiohook);
  666. audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
  667. if (ast_channel_is_bridged(chan)) {
  668. ast_channel_set_unbridged_nolock(chan, 1);
  669. }
  670. continue;
  671. }
  672. if (ast_test_flag(audiohook, AST_AUDIOHOOK_WANTS_DTMF)) {
  673. audiohook->manipulate_callback(audiohook, chan, frame, direction);
  674. }
  675. ast_audiohook_unlock(audiohook);
  676. }
  677. AST_LIST_TRAVERSE_SAFE_END;
  678. /* if an audiohook got removed, reset samplerate compatibility */
  679. if (removed) {
  680. audiohook_list_set_samplerate_compatibility(audiohook_list);
  681. }
  682. return frame;
  683. }
  684. static struct ast_frame *audiohook_list_translate_to_slin(struct ast_audiohook_list *audiohook_list,
  685. enum ast_audiohook_direction direction, struct ast_frame *frame)
  686. {
  687. struct ast_audiohook_translate *in_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ?
  688. &audiohook_list->in_translate[0] : &audiohook_list->in_translate[1]);
  689. struct ast_frame *new_frame = frame;
  690. struct ast_format *slin;
  691. /*
  692. * If we are capable of sample rates other that 8khz, update the internal
  693. * audiohook_list's rate and higher sample rate audio arrives. If native
  694. * slin compatibility is turned on all audiohooks in the list will be
  695. * updated as well during read/write processing.
  696. */
  697. audiohook_list->list_internal_samp_rate =
  698. MAX(ast_format_get_sample_rate(frame->subclass.format), audiohook_list->list_internal_samp_rate);
  699. slin = ast_format_cache_get_slin_by_rate(audiohook_list->list_internal_samp_rate);
  700. if (ast_format_cmp(frame->subclass.format, slin) == AST_FORMAT_CMP_EQUAL) {
  701. return new_frame;
  702. }
  703. if (!in_translate->format ||
  704. ast_format_cmp(frame->subclass.format, in_translate->format) != AST_FORMAT_CMP_EQUAL) {
  705. struct ast_trans_pvt *new_trans;
  706. new_trans = ast_translator_build_path(slin, frame->subclass.format);
  707. if (!new_trans) {
  708. return NULL;
  709. }
  710. if (in_translate->trans_pvt) {
  711. ast_translator_free_path(in_translate->trans_pvt);
  712. }
  713. in_translate->trans_pvt = new_trans;
  714. ao2_replace(in_translate->format, frame->subclass.format);
  715. }
  716. if (!(new_frame = ast_translate(in_translate->trans_pvt, frame, 0))) {
  717. return NULL;
  718. }
  719. return new_frame;
  720. }
  721. static struct ast_frame *audiohook_list_translate_to_native(struct ast_audiohook_list *audiohook_list,
  722. enum ast_audiohook_direction direction, struct ast_frame *slin_frame, struct ast_format *outformat)
  723. {
  724. struct ast_audiohook_translate *out_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook_list->out_translate[0] : &audiohook_list->out_translate[1]);
  725. struct ast_frame *outframe = NULL;
  726. if (ast_format_cmp(slin_frame->subclass.format, outformat) == AST_FORMAT_CMP_NOT_EQUAL) {
  727. /* rebuild translators if necessary */
  728. if (ast_format_cmp(out_translate->format, outformat) == AST_FORMAT_CMP_NOT_EQUAL) {
  729. if (out_translate->trans_pvt) {
  730. ast_translator_free_path(out_translate->trans_pvt);
  731. }
  732. if (!(out_translate->trans_pvt = ast_translator_build_path(outformat, slin_frame->subclass.format))) {
  733. return NULL;
  734. }
  735. ao2_replace(out_translate->format, outformat);
  736. }
  737. /* translate back to the format the frame came in as. */
  738. if (!(outframe = ast_translate(out_translate->trans_pvt, slin_frame, 0))) {
  739. return NULL;
  740. }
  741. }
  742. return outframe;
  743. }
  744. /*!
  745. *\brief Set the audiohook's internal sample rate to the audiohook_list's rate,
  746. * but only when native slin compatibility is turned on.
  747. *
  748. * \param audiohook_list audiohook_list data object
  749. * \param audiohook the audiohook to update
  750. * \param rate the current max internal sample rate
  751. */
  752. static void audiohook_list_set_hook_rate(struct ast_audiohook_list *audiohook_list,
  753. struct ast_audiohook *audiohook, int *rate)
  754. {
  755. /* The rate should always be the max between itself and the hook */
  756. if (audiohook->hook_internal_samp_rate > *rate) {
  757. *rate = audiohook->hook_internal_samp_rate;
  758. }
  759. /*
  760. * If native slin compatibility is turned on then update the audiohook
  761. * with the audiohook_list's current rate. Note, the audiohook's rate is
  762. * set to the audiohook_list's rate and not the given rate. If there is
  763. * a change in rate the hook's rate is changed on its next check.
  764. */
  765. if (audiohook_list->native_slin_compatible) {
  766. ast_set_flag(audiohook, AST_AUDIOHOOK_COMPATIBLE);
  767. audiohook_set_internal_rate(audiohook, audiohook_list->list_internal_samp_rate, 1);
  768. } else {
  769. ast_clear_flag(audiohook, AST_AUDIOHOOK_COMPATIBLE);
  770. }
  771. }
  772. /*!
  773. * \brief Pass an AUDIO frame off to be handled by the audiohook core
  774. *
  775. * \details
  776. * This function has 3 ast_frames and 3 parts to handle each. At the beginning of this
  777. * function all 3 frames, start_frame, middle_frame, and end_frame point to the initial
  778. * input frame.
  779. *
  780. * Part_1: Translate the start_frame into SLINEAR audio if it is not already in that
  781. * format. The result of this part is middle_frame is guaranteed to be in
  782. * SLINEAR format for Part_2.
  783. * Part_2: Send middle_frame off to spies and manipulators. At this point middle_frame is
  784. * either a new frame as result of the translation, or points directly to the start_frame
  785. * because no translation to SLINEAR audio was required.
  786. * Part_3: Translate end_frame's audio back into the format of start frame if necessary. This
  787. * is only necessary if manipulation of middle_frame occurred.
  788. *
  789. * \param chan Channel that the list is coming off of
  790. * \param audiohook_list List of audiohooks
  791. * \param direction Direction frame is coming in from
  792. * \param frame The frame itself
  793. * \return Return frame on success, NULL on failure
  794. */
  795. static struct ast_frame *audio_audiohook_write_list(struct ast_channel *chan, struct ast_audiohook_list *audiohook_list, enum ast_audiohook_direction direction, struct ast_frame *frame)
  796. {
  797. struct ast_frame *start_frame = frame, *middle_frame = frame, *end_frame = frame;
  798. struct ast_audiohook *audiohook = NULL;
  799. int samples;
  800. int middle_frame_manipulated = 0;
  801. int removed = 0;
  802. int internal_sample_rate;
  803. /* ---Part_1. translate start_frame to SLINEAR if necessary. */
  804. if (!(middle_frame = audiohook_list_translate_to_slin(audiohook_list, direction, start_frame))) {
  805. return frame;
  806. }
  807. /* If the translation resulted in an interpolated frame then immediately return as audiohooks
  808. * rely on actual media being present to do things.
  809. */
  810. if (!middle_frame->data.ptr) {
  811. if (middle_frame != start_frame) {
  812. ast_frfree(middle_frame);
  813. }
  814. return start_frame;
  815. }
  816. samples = middle_frame->samples;
  817. /*
  818. * While processing each audiohook check to see if the internal sample rate needs
  819. * to be adjusted (it should be the highest rate specified between formats and
  820. * hooks). The given audiohook_list's internal sample rate is then set to the
  821. * updated value before returning.
  822. *
  823. * If slin compatibility mode is turned on then an audiohook's internal sample
  824. * rate is set to its audiohook_list's rate. If an audiohook_list's rate is
  825. * adjusted during this pass then the change is picked up by the audiohooks
  826. * on the next pass.
  827. */
  828. internal_sample_rate = audiohook_list->list_internal_samp_rate;
  829. /* ---Part_2: Send middle_frame to spy and manipulator lists. middle_frame is guaranteed to be SLINEAR here.*/
  830. /* Queue up signed linear frame to each spy */
  831. AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) {
  832. ast_audiohook_lock(audiohook);
  833. if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
  834. AST_LIST_REMOVE_CURRENT(list);
  835. removed = 1;
  836. ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
  837. ast_audiohook_unlock(audiohook);
  838. if (ast_channel_is_bridged(chan)) {
  839. ast_channel_set_unbridged_nolock(chan, 1);
  840. }
  841. continue;
  842. }
  843. audiohook_list_set_hook_rate(audiohook_list, audiohook, &internal_sample_rate);
  844. ast_audiohook_write_frame(audiohook, direction, middle_frame);
  845. ast_audiohook_unlock(audiohook);
  846. }
  847. AST_LIST_TRAVERSE_SAFE_END;
  848. /* If this frame is being written out to the channel then we need to use whisper sources */
  849. if (!AST_LIST_EMPTY(&audiohook_list->whisper_list)) {
  850. int i = 0;
  851. short read_buf[samples], combine_buf[samples], *data1 = NULL, *data2 = NULL;
  852. memset(&combine_buf, 0, sizeof(combine_buf));
  853. AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) {
  854. struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
  855. ast_audiohook_lock(audiohook);
  856. if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
  857. AST_LIST_REMOVE_CURRENT(list);
  858. removed = 1;
  859. ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
  860. ast_audiohook_unlock(audiohook);
  861. if (ast_channel_is_bridged(chan)) {
  862. ast_channel_set_unbridged_nolock(chan, 1);
  863. }
  864. continue;
  865. }
  866. audiohook_list_set_hook_rate(audiohook_list, audiohook, &internal_sample_rate);
  867. if (ast_slinfactory_available(factory) >= samples && ast_slinfactory_read(factory, read_buf, samples)) {
  868. /* Take audio from this whisper source and combine it into our main buffer */
  869. for (i = 0, data1 = combine_buf, data2 = read_buf; i < samples; i++, data1++, data2++) {
  870. ast_slinear_saturated_add(data1, data2);
  871. }
  872. }
  873. ast_audiohook_unlock(audiohook);
  874. }
  875. AST_LIST_TRAVERSE_SAFE_END;
  876. /* We take all of the combined whisper sources and combine them into the audio being written out */
  877. for (i = 0, data1 = middle_frame->data.ptr, data2 = combine_buf; i < samples; i++, data1++, data2++) {
  878. ast_slinear_saturated_add(data1, data2);
  879. }
  880. middle_frame_manipulated = 1;
  881. }
  882. /* Pass off frame to manipulate audiohooks */
  883. if (!AST_LIST_EMPTY(&audiohook_list->manipulate_list)) {
  884. AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
  885. ast_audiohook_lock(audiohook);
  886. if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
  887. AST_LIST_REMOVE_CURRENT(list);
  888. removed = 1;
  889. ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
  890. ast_audiohook_unlock(audiohook);
  891. /* We basically drop all of our links to the manipulate audiohook and prod it to do it's own destructive things */
  892. audiohook->manipulate_callback(audiohook, chan, NULL, direction);
  893. if (ast_channel_is_bridged(chan)) {
  894. ast_channel_set_unbridged_nolock(chan, 1);
  895. }
  896. continue;
  897. }
  898. audiohook_list_set_hook_rate(audiohook_list, audiohook, &internal_sample_rate);
  899. /*
  900. * Feed in frame to manipulation.
  901. */
  902. if (!audiohook->manipulate_callback(audiohook, chan, middle_frame, direction)) {
  903. /*
  904. * XXX FAILURES ARE IGNORED XXX
  905. * If the manipulation fails then the frame will be returned in its original state.
  906. * Since there are potentially more manipulator callbacks in the list, no action should
  907. * be taken here to exit early.
  908. */
  909. middle_frame_manipulated = 1;
  910. }
  911. ast_audiohook_unlock(audiohook);
  912. }
  913. AST_LIST_TRAVERSE_SAFE_END;
  914. }
  915. /* ---Part_3: Decide what to do with the end_frame (whether to transcode or not) */
  916. if (middle_frame_manipulated) {
  917. if (!(end_frame = audiohook_list_translate_to_native(audiohook_list, direction, middle_frame, start_frame->subclass.format))) {
  918. /* translation failed, so just pass back the input frame */
  919. end_frame = start_frame;
  920. }
  921. } else {
  922. end_frame = start_frame;
  923. }
  924. /* clean up our middle_frame if required */
  925. if (middle_frame != end_frame) {
  926. ast_frfree(middle_frame);
  927. middle_frame = NULL;
  928. }
  929. /* Before returning, if an audiohook got removed, reset samplerate compatibility */
  930. if (removed) {
  931. audiohook_list_set_samplerate_compatibility(audiohook_list);
  932. } else {
  933. /*
  934. * Set the audiohook_list's rate to the updated rate. Note that if a hook
  935. * was removed then the list's internal rate is reset to the default.
  936. */
  937. audiohook_list->list_internal_samp_rate = internal_sample_rate;
  938. }
  939. return end_frame;
  940. }
  941. int ast_audiohook_write_list_empty(struct ast_audiohook_list *audiohook_list)
  942. {
  943. return !audiohook_list
  944. || (AST_LIST_EMPTY(&audiohook_list->spy_list)
  945. && AST_LIST_EMPTY(&audiohook_list->whisper_list)
  946. && AST_LIST_EMPTY(&audiohook_list->manipulate_list));
  947. }
  948. /*! \brief Pass a frame off to be handled by the audiohook core
  949. * \param chan Channel that the list is coming off of
  950. * \param audiohook_list List of audiohooks
  951. * \param direction Direction frame is coming in from
  952. * \param frame The frame itself
  953. * \return Return frame on success, NULL on failure
  954. */
  955. struct ast_frame *ast_audiohook_write_list(struct ast_channel *chan, struct ast_audiohook_list *audiohook_list, enum ast_audiohook_direction direction, struct ast_frame *frame)
  956. {
  957. /* Pass off frame to it's respective list write function */
  958. if (frame->frametype == AST_FRAME_VOICE) {
  959. return audio_audiohook_write_list(chan, audiohook_list, direction, frame);
  960. } else if (frame->frametype == AST_FRAME_DTMF) {
  961. return dtmf_audiohook_write_list(chan, audiohook_list, direction, frame);
  962. } else {
  963. return frame;
  964. }
  965. }
  966. /*! \brief Wait for audiohook trigger to be triggered
  967. * \param audiohook Audiohook to wait on
  968. */
  969. void ast_audiohook_trigger_wait(struct ast_audiohook *audiohook)
  970. {
  971. struct timeval wait;
  972. struct timespec ts;
  973. wait = ast_tvadd(ast_tvnow(), ast_samp2tv(50000, 1000));
  974. ts.tv_sec = wait.tv_sec;
  975. ts.tv_nsec = wait.tv_usec * 1000;
  976. ast_cond_timedwait(&audiohook->trigger, &audiohook->lock, &ts);
  977. return;
  978. }
  979. /* Count number of channel audiohooks by type, regardless of type */
  980. int ast_channel_audiohook_count_by_source(struct ast_channel *chan, const char *source, enum ast_audiohook_type type)
  981. {
  982. int count = 0;
  983. struct ast_audiohook *ah = NULL;
  984. if (!ast_channel_audiohooks(chan)) {
  985. return -1;
  986. }
  987. switch (type) {
  988. case AST_AUDIOHOOK_TYPE_SPY:
  989. AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->spy_list, ah, list) {
  990. if (!strcmp(ah->source, source)) {
  991. count++;
  992. }
  993. }
  994. break;
  995. case AST_AUDIOHOOK_TYPE_WHISPER:
  996. AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->whisper_list, ah, list) {
  997. if (!strcmp(ah->source, source)) {
  998. count++;
  999. }
  1000. }
  1001. break;
  1002. case AST_AUDIOHOOK_TYPE_MANIPULATE:
  1003. AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->manipulate_list, ah, list) {
  1004. if (!strcmp(ah->source, source)) {
  1005. count++;
  1006. }
  1007. }
  1008. break;
  1009. default:
  1010. ast_debug(1, "Invalid audiohook type supplied, (%u)\n", type);
  1011. return -1;
  1012. }
  1013. return count;
  1014. }
  1015. /* Count number of channel audiohooks by type that are running */
  1016. int ast_channel_audiohook_count_by_source_running(struct ast_channel *chan, const char *source, enum ast_audiohook_type type)
  1017. {
  1018. int count = 0;
  1019. struct ast_audiohook *ah = NULL;
  1020. if (!ast_channel_audiohooks(chan))
  1021. return -1;
  1022. switch (type) {
  1023. case AST_AUDIOHOOK_TYPE_SPY:
  1024. AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->spy_list, ah, list) {
  1025. if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING))
  1026. count++;
  1027. }
  1028. break;
  1029. case AST_AUDIOHOOK_TYPE_WHISPER:
  1030. AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->whisper_list, ah, list) {
  1031. if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING))
  1032. count++;
  1033. }
  1034. break;
  1035. case AST_AUDIOHOOK_TYPE_MANIPULATE:
  1036. AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->manipulate_list, ah, list) {
  1037. if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING))
  1038. count++;
  1039. }
  1040. break;
  1041. default:
  1042. ast_debug(1, "Invalid audiohook type supplied, (%u)\n", type);
  1043. return -1;
  1044. }
  1045. return count;
  1046. }
  1047. /*! \brief Audiohook volume adjustment structure */
  1048. struct audiohook_volume {
  1049. struct ast_audiohook audiohook; /*!< Audiohook attached to the channel */
  1050. int read_adjustment; /*!< Value to adjust frames read from the channel by */
  1051. int write_adjustment; /*!< Value to adjust frames written to the channel by */
  1052. };
  1053. /*! \brief Callback used to destroy the audiohook volume datastore
  1054. * \param data Volume information structure
  1055. * \return Returns nothing
  1056. */
  1057. static void audiohook_volume_destroy(void *data)
  1058. {
  1059. struct audiohook_volume *audiohook_volume = data;
  1060. /* Destroy the audiohook as it is no longer in use */
  1061. ast_audiohook_destroy(&audiohook_volume->audiohook);
  1062. /* Finally free ourselves, we are of no more use */
  1063. ast_free(audiohook_volume);
  1064. return;
  1065. }
  1066. /*! \brief Datastore used to store audiohook volume information */
  1067. static const struct ast_datastore_info audiohook_volume_datastore = {
  1068. .type = "Volume",
  1069. .destroy = audiohook_volume_destroy,
  1070. };
  1071. /*! \brief Helper function which actually gets called by audiohooks to perform the adjustment
  1072. * \param audiohook Audiohook attached to the channel
  1073. * \param chan Channel we are attached to
  1074. * \param frame Frame of audio we want to manipulate
  1075. * \param direction Direction the audio came in from
  1076. * \return Returns 0 on success, -1 on failure
  1077. */
  1078. static int audiohook_volume_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
  1079. {
  1080. struct ast_datastore *datastore = NULL;
  1081. struct audiohook_volume *audiohook_volume = NULL;
  1082. int *gain = NULL;
  1083. /* If the audiohook is shutting down don't even bother */
  1084. if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE) {
  1085. return 0;
  1086. }
  1087. /* Try to find the datastore containg adjustment information, if we can't just bail out */
  1088. if (!(datastore = ast_channel_datastore_find(chan, &audiohook_volume_datastore, NULL))) {
  1089. return 0;
  1090. }
  1091. audiohook_volume = datastore->data;
  1092. /* Based on direction grab the appropriate adjustment value */
  1093. if (direction == AST_AUDIOHOOK_DIRECTION_READ) {
  1094. gain = &audiohook_volume->read_adjustment;
  1095. } else if (direction == AST_AUDIOHOOK_DIRECTION_WRITE) {
  1096. gain = &audiohook_volume->write_adjustment;
  1097. }
  1098. /* If an adjustment value is present modify the frame */
  1099. if (gain && *gain) {
  1100. ast_frame_adjust_volume(frame, *gain);
  1101. }
  1102. return 0;
  1103. }
  1104. /*! \brief Helper function which finds and optionally creates an audiohook_volume_datastore datastore on a channel
  1105. * \param chan Channel to look on
  1106. * \param create Whether to create the datastore if not found
  1107. * \return Returns audiohook_volume structure on success, NULL on failure
  1108. */
  1109. static struct audiohook_volume *audiohook_volume_get(struct ast_channel *chan, int create)
  1110. {
  1111. struct ast_datastore *datastore = NULL;
  1112. struct audiohook_volume *audiohook_volume = NULL;
  1113. /* If we are able to find the datastore return the contents (which is actually an audiohook_volume structure) */
  1114. if ((datastore = ast_channel_datastore_find(chan, &audiohook_volume_datastore, NULL))) {
  1115. return datastore->data;
  1116. }
  1117. /* If we are not allowed to create a datastore or if we fail to create a datastore, bail out now as we have nothing for them */
  1118. if (!create || !(datastore = ast_datastore_alloc(&audiohook_volume_datastore, NULL))) {
  1119. return NULL;
  1120. }
  1121. /* Create a new audiohook_volume structure to contain our adjustments and audiohook */
  1122. if (!(audiohook_volume = ast_calloc(1, sizeof(*audiohook_volume)))) {
  1123. ast_datastore_free(datastore);
  1124. return NULL;
  1125. }
  1126. /* Setup our audiohook structure so we can manipulate the audio */
  1127. ast_audiohook_init(&audiohook_volume->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "Volume", AST_AUDIOHOOK_MANIPULATE_ALL_RATES);
  1128. audiohook_volume->audiohook.manipulate_callback = audiohook_volume_callback;
  1129. /* Attach the audiohook_volume blob to the datastore and attach to the channel */
  1130. datastore->data = audiohook_volume;
  1131. ast_channel_datastore_add(chan, datastore);
  1132. /* All is well... put the audiohook into motion */
  1133. ast_audiohook_attach(chan, &audiohook_volume->audiohook);
  1134. return audiohook_volume;
  1135. }
  1136. /*! \brief Adjust the volume on frames read from or written to a channel
  1137. * \param chan Channel to muck with
  1138. * \param direction Direction to set on
  1139. * \param volume Value to adjust the volume by
  1140. * \return Returns 0 on success, -1 on failure
  1141. */
  1142. int ast_audiohook_volume_set(struct ast_channel *chan, enum ast_audiohook_direction direction, int volume)
  1143. {
  1144. struct audiohook_volume *audiohook_volume = NULL;
  1145. /* Attempt to find the audiohook volume information, but only create it if we are not setting the adjustment value to zero */
  1146. if (!(audiohook_volume = audiohook_volume_get(chan, (volume ? 1 : 0)))) {
  1147. return -1;
  1148. }
  1149. /* Now based on the direction set the proper value */
  1150. if (direction == AST_AUDIOHOOK_DIRECTION_READ || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
  1151. audiohook_volume->read_adjustment = volume;
  1152. }
  1153. if (direction == AST_AUDIOHOOK_DIRECTION_WRITE || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
  1154. audiohook_volume->write_adjustment = volume;
  1155. }
  1156. return 0;
  1157. }
  1158. /*! \brief Retrieve the volume adjustment value on frames read from or written to a channel
  1159. * \param chan Channel to retrieve volume adjustment from
  1160. * \param direction Direction to retrieve
  1161. * \return Returns adjustment value
  1162. */
  1163. int ast_audiohook_volume_get(struct ast_channel *chan, enum ast_audiohook_direction direction)
  1164. {
  1165. struct audiohook_volume *audiohook_volume = NULL;
  1166. int adjustment = 0;
  1167. /* Attempt to find the audiohook volume information, but do not create it as we only want to look at the values */
  1168. if (!(audiohook_volume = audiohook_volume_get(chan, 0))) {
  1169. return 0;
  1170. }
  1171. /* Grab the adjustment value based on direction given */
  1172. if (direction == AST_AUDIOHOOK_DIRECTION_READ) {
  1173. adjustment = audiohook_volume->read_adjustment;
  1174. } else if (direction == AST_AUDIOHOOK_DIRECTION_WRITE) {
  1175. adjustment = audiohook_volume->write_adjustment;
  1176. }
  1177. return adjustment;
  1178. }
  1179. /*! \brief Adjust the volume on frames read from or written to a channel
  1180. * \param chan Channel to muck with
  1181. * \param direction Direction to increase
  1182. * \param volume Value to adjust the adjustment by
  1183. * \return Returns 0 on success, -1 on failure
  1184. */
  1185. int ast_audiohook_volume_adjust(struct ast_channel *chan, enum ast_audiohook_direction direction, int volume)
  1186. {
  1187. struct audiohook_volume *audiohook_volume = NULL;
  1188. /* Attempt to find the audiohook volume information, and create an audiohook if none exists */
  1189. if (!(audiohook_volume = audiohook_volume_get(chan, 1))) {
  1190. return -1;
  1191. }
  1192. /* Based on the direction change the specific adjustment value */
  1193. if (direction == AST_AUDIOHOOK_DIRECTION_READ || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
  1194. audiohook_volume->read_adjustment += volume;
  1195. }
  1196. if (direction == AST_AUDIOHOOK_DIRECTION_WRITE || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
  1197. audiohook_volume->write_adjustment += volume;
  1198. }
  1199. return 0;
  1200. }
  1201. /*! \brief Mute frames read from or written to a channel
  1202. * \param chan Channel to muck with
  1203. * \param source Type of audiohook
  1204. * \param flag which flag to set / clear
  1205. * \param clear set or clear
  1206. * \return Returns 0 on success, -1 on failure
  1207. */
  1208. int ast_audiohook_set_mute(struct ast_channel *chan, const char *source, enum ast_audiohook_flags flag, int clear)
  1209. {
  1210. struct ast_audiohook *audiohook = NULL;
  1211. ast_channel_lock(chan);
  1212. /* Ensure the channel has audiohooks on it */
  1213. if (!ast_channel_audiohooks(chan)) {
  1214. ast_channel_unlock(chan);
  1215. return -1;
  1216. }
  1217. audiohook = find_audiohook_by_source(ast_channel_audiohooks(chan), source);
  1218. if (audiohook) {
  1219. if (clear) {
  1220. ast_clear_flag(audiohook, flag);
  1221. } else {
  1222. ast_set_flag(audiohook, flag);
  1223. }
  1224. }
  1225. ast_channel_unlock(chan);
  1226. return (audiohook ? 0 : -1);
  1227. }