audiohook.c 37 KB

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