audiohook.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  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 %zd 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. ast_channel_unlock(chan);
  407. return 0;
  408. }
  409. /*! \brief Update audiohook's status
  410. * \param audiohook Audiohook structure
  411. * \param status Audiohook status enum
  412. *
  413. * \note once status is updated to DONE, this function can not be used to set the
  414. * status back to any other setting. Setting DONE effectively locks the status as such.
  415. */
  416. void ast_audiohook_update_status(struct ast_audiohook *audiohook, enum ast_audiohook_status status)
  417. {
  418. ast_audiohook_lock(audiohook);
  419. if (audiohook->status != AST_AUDIOHOOK_STATUS_DONE) {
  420. audiohook->status = status;
  421. ast_cond_signal(&audiohook->trigger);
  422. }
  423. ast_audiohook_unlock(audiohook);
  424. }
  425. /*! \brief Detach audiohook from channel
  426. * \param audiohook Audiohook structure
  427. * \return Returns 0 on success, -1 on failure
  428. */
  429. int ast_audiohook_detach(struct ast_audiohook *audiohook)
  430. {
  431. if (audiohook->status == AST_AUDIOHOOK_STATUS_NEW || audiohook->status == AST_AUDIOHOOK_STATUS_DONE)
  432. return 0;
  433. ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_SHUTDOWN);
  434. while (audiohook->status != AST_AUDIOHOOK_STATUS_DONE)
  435. ast_audiohook_trigger_wait(audiohook);
  436. return 0;
  437. }
  438. void ast_audiohook_detach_list(struct ast_audiohook_list *audiohook_list)
  439. {
  440. int i;
  441. struct ast_audiohook *audiohook;
  442. if (!audiohook_list) {
  443. return;
  444. }
  445. /* Drop any spies */
  446. while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->spy_list, list))) {
  447. ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
  448. }
  449. /* Drop any whispering sources */
  450. while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->whisper_list, list))) {
  451. ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
  452. }
  453. /* Drop any manipulaters */
  454. while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->manipulate_list, list))) {
  455. ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
  456. audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
  457. }
  458. /* Drop translation paths if present */
  459. for (i = 0; i < 2; i++) {
  460. if (audiohook_list->in_translate[i].trans_pvt)
  461. ast_translator_free_path(audiohook_list->in_translate[i].trans_pvt);
  462. if (audiohook_list->out_translate[i].trans_pvt)
  463. ast_translator_free_path(audiohook_list->out_translate[i].trans_pvt);
  464. }
  465. /* Free ourselves */
  466. ast_free(audiohook_list);
  467. }
  468. /*! \brief find an audiohook based on its source
  469. * \param audiohook_list The list of audiohooks to search in
  470. * \param source The source of the audiohook we wish to find
  471. * \return Return the corresponding audiohook or NULL if it cannot be found.
  472. */
  473. static struct ast_audiohook *find_audiohook_by_source(struct ast_audiohook_list *audiohook_list, const char *source)
  474. {
  475. struct ast_audiohook *audiohook = NULL;
  476. AST_LIST_TRAVERSE(&audiohook_list->spy_list, audiohook, list) {
  477. if (!strcasecmp(audiohook->source, source))
  478. return audiohook;
  479. }
  480. AST_LIST_TRAVERSE(&audiohook_list->whisper_list, audiohook, list) {
  481. if (!strcasecmp(audiohook->source, source))
  482. return audiohook;
  483. }
  484. AST_LIST_TRAVERSE(&audiohook_list->manipulate_list, audiohook, list) {
  485. if (!strcasecmp(audiohook->source, source))
  486. return audiohook;
  487. }
  488. return NULL;
  489. }
  490. void ast_audiohook_move_by_source(struct ast_channel *old_chan, struct ast_channel *new_chan, const char *source)
  491. {
  492. struct ast_audiohook *audiohook;
  493. enum ast_audiohook_status oldstatus;
  494. if (!ast_channel_audiohooks(old_chan) || !(audiohook = find_audiohook_by_source(ast_channel_audiohooks(old_chan), source))) {
  495. return;
  496. }
  497. /* By locking both channels and the audiohook, we can assure that
  498. * another thread will not have a chance to read the audiohook's status
  499. * as done, even though ast_audiohook_remove signals the trigger
  500. * condition.
  501. */
  502. ast_audiohook_lock(audiohook);
  503. oldstatus = audiohook->status;
  504. ast_audiohook_remove(old_chan, audiohook);
  505. ast_audiohook_attach(new_chan, audiohook);
  506. audiohook->status = oldstatus;
  507. ast_audiohook_unlock(audiohook);
  508. }
  509. /*! \brief Detach specified source audiohook from channel
  510. * \param chan Channel to detach from
  511. * \param source Name of source to detach
  512. * \return Returns 0 on success, -1 on failure
  513. */
  514. int ast_audiohook_detach_source(struct ast_channel *chan, const char *source)
  515. {
  516. struct ast_audiohook *audiohook = NULL;
  517. ast_channel_lock(chan);
  518. /* Ensure the channel has audiohooks on it */
  519. if (!ast_channel_audiohooks(chan)) {
  520. ast_channel_unlock(chan);
  521. return -1;
  522. }
  523. audiohook = find_audiohook_by_source(ast_channel_audiohooks(chan), source);
  524. ast_channel_unlock(chan);
  525. if (audiohook && audiohook->status != AST_AUDIOHOOK_STATUS_DONE)
  526. ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_SHUTDOWN);
  527. return (audiohook ? 0 : -1);
  528. }
  529. /*!
  530. * \brief Remove an audiohook from a specified channel
  531. *
  532. * \param chan Channel to remove from
  533. * \param audiohook Audiohook to remove
  534. *
  535. * \return Returns 0 on success, -1 on failure
  536. *
  537. * \note The channel does not need to be locked before calling this function
  538. */
  539. int ast_audiohook_remove(struct ast_channel *chan, struct ast_audiohook *audiohook)
  540. {
  541. ast_channel_lock(chan);
  542. if (!ast_channel_audiohooks(chan)) {
  543. ast_channel_unlock(chan);
  544. return -1;
  545. }
  546. if (audiohook->type == AST_AUDIOHOOK_TYPE_SPY)
  547. AST_LIST_REMOVE(&ast_channel_audiohooks(chan)->spy_list, audiohook, list);
  548. else if (audiohook->type == AST_AUDIOHOOK_TYPE_WHISPER)
  549. AST_LIST_REMOVE(&ast_channel_audiohooks(chan)->whisper_list, audiohook, list);
  550. else if (audiohook->type == AST_AUDIOHOOK_TYPE_MANIPULATE)
  551. AST_LIST_REMOVE(&ast_channel_audiohooks(chan)->manipulate_list, audiohook, list);
  552. audiohook_list_set_samplerate_compatibility(ast_channel_audiohooks(chan));
  553. ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
  554. ast_channel_unlock(chan);
  555. return 0;
  556. }
  557. /*! \brief Pass a DTMF frame off to be handled by the audiohook core
  558. * \param chan Channel that the list is coming off of
  559. * \param audiohook_list List of audiohooks
  560. * \param direction Direction frame is coming in from
  561. * \param frame The frame itself
  562. * \return Return frame on success, NULL on failure
  563. */
  564. 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)
  565. {
  566. struct ast_audiohook *audiohook = NULL;
  567. int removed = 0;
  568. AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
  569. ast_audiohook_lock(audiohook);
  570. if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
  571. AST_LIST_REMOVE_CURRENT(list);
  572. removed = 1;
  573. ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
  574. ast_audiohook_unlock(audiohook);
  575. audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
  576. continue;
  577. }
  578. if (ast_test_flag(audiohook, AST_AUDIOHOOK_WANTS_DTMF))
  579. audiohook->manipulate_callback(audiohook, chan, frame, direction);
  580. ast_audiohook_unlock(audiohook);
  581. }
  582. AST_LIST_TRAVERSE_SAFE_END;
  583. /* if an audiohook got removed, reset samplerate compatibility */
  584. if (removed) {
  585. audiohook_list_set_samplerate_compatibility(audiohook_list);
  586. }
  587. return frame;
  588. }
  589. static struct ast_frame *audiohook_list_translate_to_slin(struct ast_audiohook_list *audiohook_list,
  590. enum ast_audiohook_direction direction, struct ast_frame *frame)
  591. {
  592. struct ast_audiohook_translate *in_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ?
  593. &audiohook_list->in_translate[0] : &audiohook_list->in_translate[1]);
  594. struct ast_frame *new_frame = frame;
  595. struct ast_format tmp_fmt;
  596. enum ast_format_id slin_id;
  597. /* If we are capable of maintaining doing samplerates other that 8khz, update
  598. * the internal audiohook_list's rate and higher samplerate audio arrives. By
  599. * updating the list's rate, all the audiohooks in the list will be updated as well
  600. * as the are written and read from. */
  601. if (audiohook_list->native_slin_compatible) {
  602. audiohook_list->list_internal_samp_rate =
  603. MAX(ast_format_rate(&frame->subclass.format), audiohook_list->list_internal_samp_rate);
  604. }
  605. slin_id = ast_format_slin_by_rate(audiohook_list->list_internal_samp_rate);
  606. if (frame->subclass.format.id == slin_id) {
  607. return new_frame;
  608. }
  609. if (ast_format_cmp(&frame->subclass.format, &in_translate->format) == AST_FORMAT_CMP_NOT_EQUAL) {
  610. if (in_translate->trans_pvt) {
  611. ast_translator_free_path(in_translate->trans_pvt);
  612. }
  613. if (!(in_translate->trans_pvt = ast_translator_build_path(ast_format_set(&tmp_fmt, slin_id, 0), &frame->subclass.format))) {
  614. return NULL;
  615. }
  616. ast_format_copy(&in_translate->format, &frame->subclass.format);
  617. }
  618. if (!(new_frame = ast_translate(in_translate->trans_pvt, frame, 0))) {
  619. return NULL;
  620. }
  621. return new_frame;
  622. }
  623. static struct ast_frame *audiohook_list_translate_to_native(struct ast_audiohook_list *audiohook_list,
  624. enum ast_audiohook_direction direction, struct ast_frame *slin_frame, struct ast_format *outformat)
  625. {
  626. struct ast_audiohook_translate *out_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook_list->out_translate[0] : &audiohook_list->out_translate[1]);
  627. struct ast_frame *outframe = NULL;
  628. if (ast_format_cmp(&slin_frame->subclass.format, outformat) == AST_FORMAT_CMP_NOT_EQUAL) {
  629. /* rebuild translators if necessary */
  630. if (ast_format_cmp(&out_translate->format, outformat) == AST_FORMAT_CMP_NOT_EQUAL) {
  631. if (out_translate->trans_pvt) {
  632. ast_translator_free_path(out_translate->trans_pvt);
  633. }
  634. if (!(out_translate->trans_pvt = ast_translator_build_path(outformat, &slin_frame->subclass.format))) {
  635. return NULL;
  636. }
  637. ast_format_copy(&out_translate->format, outformat);
  638. }
  639. /* translate back to the format the frame came in as. */
  640. if (!(outframe = ast_translate(out_translate->trans_pvt, slin_frame, 0))) {
  641. return NULL;
  642. }
  643. }
  644. return outframe;
  645. }
  646. /*!
  647. * \brief Pass an AUDIO frame off to be handled by the audiohook core
  648. *
  649. * \details
  650. * This function has 3 ast_frames and 3 parts to handle each. At the beginning of this
  651. * function all 3 frames, start_frame, middle_frame, and end_frame point to the initial
  652. * input frame.
  653. *
  654. * Part_1: Translate the start_frame into SLINEAR audio if it is not already in that
  655. * format. The result of this part is middle_frame is guaranteed to be in
  656. * SLINEAR format for Part_2.
  657. * Part_2: Send middle_frame off to spies and manipulators. At this point middle_frame is
  658. * either a new frame as result of the translation, or points directly to the start_frame
  659. * because no translation to SLINEAR audio was required.
  660. * Part_3: Translate end_frame's audio back into the format of start frame if necessary. This
  661. * is only necessary if manipulation of middle_frame occurred.
  662. *
  663. * \param chan Channel that the list is coming off of
  664. * \param audiohook_list List of audiohooks
  665. * \param direction Direction frame is coming in from
  666. * \param frame The frame itself
  667. * \return Return frame on success, NULL on failure
  668. */
  669. 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)
  670. {
  671. struct ast_frame *start_frame = frame, *middle_frame = frame, *end_frame = frame;
  672. struct ast_audiohook *audiohook = NULL;
  673. int samples;
  674. int middle_frame_manipulated = 0;
  675. int removed = 0;
  676. /* ---Part_1. translate start_frame to SLINEAR if necessary. */
  677. if (!(middle_frame = audiohook_list_translate_to_slin(audiohook_list, direction, start_frame))) {
  678. return frame;
  679. }
  680. samples = middle_frame->samples;
  681. /* ---Part_2: Send middle_frame to spy and manipulator lists. middle_frame is guaranteed to be SLINEAR here.*/
  682. /* Queue up signed linear frame to each spy */
  683. AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) {
  684. ast_audiohook_lock(audiohook);
  685. if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
  686. AST_LIST_REMOVE_CURRENT(list);
  687. removed = 1;
  688. ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
  689. ast_audiohook_unlock(audiohook);
  690. continue;
  691. }
  692. audiohook_set_internal_rate(audiohook, audiohook_list->list_internal_samp_rate, 1);
  693. ast_audiohook_write_frame(audiohook, direction, middle_frame);
  694. ast_audiohook_unlock(audiohook);
  695. }
  696. AST_LIST_TRAVERSE_SAFE_END;
  697. /* If this frame is being written out to the channel then we need to use whisper sources */
  698. if (!AST_LIST_EMPTY(&audiohook_list->whisper_list)) {
  699. int i = 0;
  700. short read_buf[samples], combine_buf[samples], *data1 = NULL, *data2 = NULL;
  701. memset(&combine_buf, 0, sizeof(combine_buf));
  702. AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) {
  703. struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
  704. ast_audiohook_lock(audiohook);
  705. if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
  706. AST_LIST_REMOVE_CURRENT(list);
  707. removed = 1;
  708. ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
  709. ast_audiohook_unlock(audiohook);
  710. continue;
  711. }
  712. audiohook_set_internal_rate(audiohook, audiohook_list->list_internal_samp_rate, 1);
  713. if (ast_slinfactory_available(factory) >= samples && ast_slinfactory_read(factory, read_buf, samples)) {
  714. /* Take audio from this whisper source and combine it into our main buffer */
  715. for (i = 0, data1 = combine_buf, data2 = read_buf; i < samples; i++, data1++, data2++)
  716. ast_slinear_saturated_add(data1, data2);
  717. }
  718. ast_audiohook_unlock(audiohook);
  719. }
  720. AST_LIST_TRAVERSE_SAFE_END;
  721. /* We take all of the combined whisper sources and combine them into the audio being written out */
  722. for (i = 0, data1 = middle_frame->data.ptr, data2 = combine_buf; i < samples; i++, data1++, data2++) {
  723. ast_slinear_saturated_add(data1, data2);
  724. }
  725. middle_frame_manipulated = 1;
  726. }
  727. /* Pass off frame to manipulate audiohooks */
  728. if (!AST_LIST_EMPTY(&audiohook_list->manipulate_list)) {
  729. AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
  730. ast_audiohook_lock(audiohook);
  731. if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
  732. AST_LIST_REMOVE_CURRENT(list);
  733. removed = 1;
  734. ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
  735. ast_audiohook_unlock(audiohook);
  736. /* We basically drop all of our links to the manipulate audiohook and prod it to do it's own destructive things */
  737. audiohook->manipulate_callback(audiohook, chan, NULL, direction);
  738. continue;
  739. }
  740. audiohook_set_internal_rate(audiohook, audiohook_list->list_internal_samp_rate, 1);
  741. /* Feed in frame to manipulation. */
  742. if (audiohook->manipulate_callback(audiohook, chan, middle_frame, direction)) {
  743. /* XXX IGNORE FAILURE */
  744. /* If the manipulation fails then the frame will be returned in its original state.
  745. * Since there are potentially more manipulator callbacks in the list, no action should
  746. * be taken here to exit early. */
  747. }
  748. ast_audiohook_unlock(audiohook);
  749. }
  750. AST_LIST_TRAVERSE_SAFE_END;
  751. middle_frame_manipulated = 1;
  752. }
  753. /* ---Part_3: Decide what to do with the end_frame (whether to transcode or not) */
  754. if (middle_frame_manipulated) {
  755. if (!(end_frame = audiohook_list_translate_to_native(audiohook_list, direction, middle_frame, &start_frame->subclass.format))) {
  756. /* translation failed, so just pass back the input frame */
  757. end_frame = start_frame;
  758. }
  759. } else {
  760. end_frame = start_frame;
  761. }
  762. /* clean up our middle_frame if required */
  763. if (middle_frame != end_frame) {
  764. ast_frfree(middle_frame);
  765. middle_frame = NULL;
  766. }
  767. /* Before returning, if an audiohook got removed, reset samplerate compatibility */
  768. if (removed) {
  769. audiohook_list_set_samplerate_compatibility(audiohook_list);
  770. }
  771. return end_frame;
  772. }
  773. int ast_audiohook_write_list_empty(struct ast_audiohook_list *audiohook_list)
  774. {
  775. return !audiohook_list
  776. || (AST_LIST_EMPTY(&audiohook_list->spy_list)
  777. && AST_LIST_EMPTY(&audiohook_list->whisper_list)
  778. && AST_LIST_EMPTY(&audiohook_list->manipulate_list));
  779. }
  780. /*! \brief Pass a frame off to be handled by the audiohook core
  781. * \param chan Channel that the list is coming off of
  782. * \param audiohook_list List of audiohooks
  783. * \param direction Direction frame is coming in from
  784. * \param frame The frame itself
  785. * \return Return frame on success, NULL on failure
  786. */
  787. 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)
  788. {
  789. /* Pass off frame to it's respective list write function */
  790. if (frame->frametype == AST_FRAME_VOICE)
  791. return audio_audiohook_write_list(chan, audiohook_list, direction, frame);
  792. else if (frame->frametype == AST_FRAME_DTMF)
  793. return dtmf_audiohook_write_list(chan, audiohook_list, direction, frame);
  794. else
  795. return frame;
  796. }
  797. /*! \brief Wait for audiohook trigger to be triggered
  798. * \param audiohook Audiohook to wait on
  799. */
  800. void ast_audiohook_trigger_wait(struct ast_audiohook *audiohook)
  801. {
  802. struct timeval wait;
  803. struct timespec ts;
  804. wait = ast_tvadd(ast_tvnow(), ast_samp2tv(50000, 1000));
  805. ts.tv_sec = wait.tv_sec;
  806. ts.tv_nsec = wait.tv_usec * 1000;
  807. ast_cond_timedwait(&audiohook->trigger, &audiohook->lock, &ts);
  808. return;
  809. }
  810. /* Count number of channel audiohooks by type, regardless of type */
  811. int ast_channel_audiohook_count_by_source(struct ast_channel *chan, const char *source, enum ast_audiohook_type type)
  812. {
  813. int count = 0;
  814. struct ast_audiohook *ah = NULL;
  815. if (!ast_channel_audiohooks(chan))
  816. return -1;
  817. switch (type) {
  818. case AST_AUDIOHOOK_TYPE_SPY:
  819. AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->spy_list, ah, list) {
  820. if (!strcmp(ah->source, source)) {
  821. count++;
  822. }
  823. }
  824. break;
  825. case AST_AUDIOHOOK_TYPE_WHISPER:
  826. AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->whisper_list, ah, list) {
  827. if (!strcmp(ah->source, source)) {
  828. count++;
  829. }
  830. }
  831. break;
  832. case AST_AUDIOHOOK_TYPE_MANIPULATE:
  833. AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->manipulate_list, ah, list) {
  834. if (!strcmp(ah->source, source)) {
  835. count++;
  836. }
  837. }
  838. break;
  839. default:
  840. ast_debug(1, "Invalid audiohook type supplied, (%d)\n", type);
  841. return -1;
  842. }
  843. return count;
  844. }
  845. /* Count number of channel audiohooks by type that are running */
  846. int ast_channel_audiohook_count_by_source_running(struct ast_channel *chan, const char *source, enum ast_audiohook_type type)
  847. {
  848. int count = 0;
  849. struct ast_audiohook *ah = NULL;
  850. if (!ast_channel_audiohooks(chan))
  851. return -1;
  852. switch (type) {
  853. case AST_AUDIOHOOK_TYPE_SPY:
  854. AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->spy_list, ah, list) {
  855. if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING))
  856. count++;
  857. }
  858. break;
  859. case AST_AUDIOHOOK_TYPE_WHISPER:
  860. AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->whisper_list, ah, list) {
  861. if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING))
  862. count++;
  863. }
  864. break;
  865. case AST_AUDIOHOOK_TYPE_MANIPULATE:
  866. AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->manipulate_list, ah, list) {
  867. if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING))
  868. count++;
  869. }
  870. break;
  871. default:
  872. ast_debug(1, "Invalid audiohook type supplied, (%d)\n", type);
  873. return -1;
  874. }
  875. return count;
  876. }
  877. /*! \brief Audiohook volume adjustment structure */
  878. struct audiohook_volume {
  879. struct ast_audiohook audiohook; /*!< Audiohook attached to the channel */
  880. int read_adjustment; /*!< Value to adjust frames read from the channel by */
  881. int write_adjustment; /*!< Value to adjust frames written to the channel by */
  882. };
  883. /*! \brief Callback used to destroy the audiohook volume datastore
  884. * \param data Volume information structure
  885. * \return Returns nothing
  886. */
  887. static void audiohook_volume_destroy(void *data)
  888. {
  889. struct audiohook_volume *audiohook_volume = data;
  890. /* Destroy the audiohook as it is no longer in use */
  891. ast_audiohook_destroy(&audiohook_volume->audiohook);
  892. /* Finally free ourselves, we are of no more use */
  893. ast_free(audiohook_volume);
  894. return;
  895. }
  896. /*! \brief Datastore used to store audiohook volume information */
  897. static const struct ast_datastore_info audiohook_volume_datastore = {
  898. .type = "Volume",
  899. .destroy = audiohook_volume_destroy,
  900. };
  901. /*! \brief Helper function which actually gets called by audiohooks to perform the adjustment
  902. * \param audiohook Audiohook attached to the channel
  903. * \param chan Channel we are attached to
  904. * \param frame Frame of audio we want to manipulate
  905. * \param direction Direction the audio came in from
  906. * \return Returns 0 on success, -1 on failure
  907. */
  908. static int audiohook_volume_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
  909. {
  910. struct ast_datastore *datastore = NULL;
  911. struct audiohook_volume *audiohook_volume = NULL;
  912. int *gain = NULL;
  913. /* If the audiohook is shutting down don't even bother */
  914. if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE) {
  915. return 0;
  916. }
  917. /* Try to find the datastore containg adjustment information, if we can't just bail out */
  918. if (!(datastore = ast_channel_datastore_find(chan, &audiohook_volume_datastore, NULL))) {
  919. return 0;
  920. }
  921. audiohook_volume = datastore->data;
  922. /* Based on direction grab the appropriate adjustment value */
  923. if (direction == AST_AUDIOHOOK_DIRECTION_READ) {
  924. gain = &audiohook_volume->read_adjustment;
  925. } else if (direction == AST_AUDIOHOOK_DIRECTION_WRITE) {
  926. gain = &audiohook_volume->write_adjustment;
  927. }
  928. /* If an adjustment value is present modify the frame */
  929. if (gain && *gain) {
  930. ast_frame_adjust_volume(frame, *gain);
  931. }
  932. return 0;
  933. }
  934. /*! \brief Helper function which finds and optionally creates an audiohook_volume_datastore datastore on a channel
  935. * \param chan Channel to look on
  936. * \param create Whether to create the datastore if not found
  937. * \return Returns audiohook_volume structure on success, NULL on failure
  938. */
  939. static struct audiohook_volume *audiohook_volume_get(struct ast_channel *chan, int create)
  940. {
  941. struct ast_datastore *datastore = NULL;
  942. struct audiohook_volume *audiohook_volume = NULL;
  943. /* If we are able to find the datastore return the contents (which is actually an audiohook_volume structure) */
  944. if ((datastore = ast_channel_datastore_find(chan, &audiohook_volume_datastore, NULL))) {
  945. return datastore->data;
  946. }
  947. /* 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 */
  948. if (!create || !(datastore = ast_datastore_alloc(&audiohook_volume_datastore, NULL))) {
  949. return NULL;
  950. }
  951. /* Create a new audiohook_volume structure to contain our adjustments and audiohook */
  952. if (!(audiohook_volume = ast_calloc(1, sizeof(*audiohook_volume)))) {
  953. ast_datastore_free(datastore);
  954. return NULL;
  955. }
  956. /* Setup our audiohook structure so we can manipulate the audio */
  957. ast_audiohook_init(&audiohook_volume->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "Volume", AST_AUDIOHOOK_MANIPULATE_ALL_RATES);
  958. audiohook_volume->audiohook.manipulate_callback = audiohook_volume_callback;
  959. /* Attach the audiohook_volume blob to the datastore and attach to the channel */
  960. datastore->data = audiohook_volume;
  961. ast_channel_datastore_add(chan, datastore);
  962. /* All is well... put the audiohook into motion */
  963. ast_audiohook_attach(chan, &audiohook_volume->audiohook);
  964. return audiohook_volume;
  965. }
  966. /*! \brief Adjust the volume on frames read from or written to a channel
  967. * \param chan Channel to muck with
  968. * \param direction Direction to set on
  969. * \param volume Value to adjust the volume by
  970. * \return Returns 0 on success, -1 on failure
  971. */
  972. int ast_audiohook_volume_set(struct ast_channel *chan, enum ast_audiohook_direction direction, int volume)
  973. {
  974. struct audiohook_volume *audiohook_volume = NULL;
  975. /* Attempt to find the audiohook volume information, but only create it if we are not setting the adjustment value to zero */
  976. if (!(audiohook_volume = audiohook_volume_get(chan, (volume ? 1 : 0)))) {
  977. return -1;
  978. }
  979. /* Now based on the direction set the proper value */
  980. if (direction == AST_AUDIOHOOK_DIRECTION_READ || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
  981. audiohook_volume->read_adjustment = volume;
  982. }
  983. if (direction == AST_AUDIOHOOK_DIRECTION_WRITE || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
  984. audiohook_volume->write_adjustment = volume;
  985. }
  986. return 0;
  987. }
  988. /*! \brief Retrieve the volume adjustment value on frames read from or written to a channel
  989. * \param chan Channel to retrieve volume adjustment from
  990. * \param direction Direction to retrieve
  991. * \return Returns adjustment value
  992. */
  993. int ast_audiohook_volume_get(struct ast_channel *chan, enum ast_audiohook_direction direction)
  994. {
  995. struct audiohook_volume *audiohook_volume = NULL;
  996. int adjustment = 0;
  997. /* Attempt to find the audiohook volume information, but do not create it as we only want to look at the values */
  998. if (!(audiohook_volume = audiohook_volume_get(chan, 0))) {
  999. return 0;
  1000. }
  1001. /* Grab the adjustment value based on direction given */
  1002. if (direction == AST_AUDIOHOOK_DIRECTION_READ) {
  1003. adjustment = audiohook_volume->read_adjustment;
  1004. } else if (direction == AST_AUDIOHOOK_DIRECTION_WRITE) {
  1005. adjustment = audiohook_volume->write_adjustment;
  1006. }
  1007. return adjustment;
  1008. }
  1009. /*! \brief Adjust the volume on frames read from or written to a channel
  1010. * \param chan Channel to muck with
  1011. * \param direction Direction to increase
  1012. * \param volume Value to adjust the adjustment by
  1013. * \return Returns 0 on success, -1 on failure
  1014. */
  1015. int ast_audiohook_volume_adjust(struct ast_channel *chan, enum ast_audiohook_direction direction, int volume)
  1016. {
  1017. struct audiohook_volume *audiohook_volume = NULL;
  1018. /* Attempt to find the audiohook volume information, and create an audiohook if none exists */
  1019. if (!(audiohook_volume = audiohook_volume_get(chan, 1))) {
  1020. return -1;
  1021. }
  1022. /* Based on the direction change the specific adjustment value */
  1023. if (direction == AST_AUDIOHOOK_DIRECTION_READ || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
  1024. audiohook_volume->read_adjustment += volume;
  1025. }
  1026. if (direction == AST_AUDIOHOOK_DIRECTION_WRITE || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
  1027. audiohook_volume->write_adjustment += volume;
  1028. }
  1029. return 0;
  1030. }
  1031. /*! \brief Mute frames read from or written to a channel
  1032. * \param chan Channel to muck with
  1033. * \param source Type of audiohook
  1034. * \param flag which flag to set / clear
  1035. * \param clear set or clear
  1036. * \return Returns 0 on success, -1 on failure
  1037. */
  1038. int ast_audiohook_set_mute(struct ast_channel *chan, const char *source, enum ast_audiohook_flags flag, int clear)
  1039. {
  1040. struct ast_audiohook *audiohook = NULL;
  1041. ast_channel_lock(chan);
  1042. /* Ensure the channel has audiohooks on it */
  1043. if (!ast_channel_audiohooks(chan)) {
  1044. ast_channel_unlock(chan);
  1045. return -1;
  1046. }
  1047. audiohook = find_audiohook_by_source(ast_channel_audiohooks(chan), source);
  1048. if (audiohook) {
  1049. if (clear) {
  1050. ast_clear_flag(audiohook, flag);
  1051. } else {
  1052. ast_set_flag(audiohook, flag);
  1053. }
  1054. }
  1055. ast_channel_unlock(chan);
  1056. return (audiohook ? 0 : -1);
  1057. }