audiohook.c 46 KB

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