audiohook.c 44 KB

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