bridge_softmix.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2011, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@digium.com>
  7. * David Vossel <dvossel@digium.com>
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*! \file
  20. *
  21. * \brief Multi-party software based channel mixing
  22. *
  23. * \author Joshua Colp <jcolp@digium.com>
  24. * \author David Vossel <dvossel@digium.com>
  25. *
  26. * \ingroup bridges
  27. */
  28. /*** MODULEINFO
  29. <support_level>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <sys/time.h>
  37. #include <signal.h>
  38. #include <errno.h>
  39. #include <unistd.h>
  40. #include "asterisk/module.h"
  41. #include "asterisk/channel.h"
  42. #include "asterisk/bridge.h"
  43. #include "asterisk/bridge_technology.h"
  44. #include "asterisk/frame.h"
  45. #include "asterisk/options.h"
  46. #include "asterisk/logger.h"
  47. #include "asterisk/slinfactory.h"
  48. #include "asterisk/astobj2.h"
  49. #include "asterisk/timing.h"
  50. #include "asterisk/translate.h"
  51. #include "asterisk/message.h"
  52. #define MAX_DATALEN 8096
  53. /*! The minimum sample rate of the bridge. */
  54. #define SOFTMIX_MIN_SAMPLE_RATE 8000 /* 8 kHz sample rate */
  55. /*! \brief Interval at which mixing will take place. Valid options are 10, 20, and 40. */
  56. #define DEFAULT_SOFTMIX_INTERVAL 20
  57. /*! \brief Size of the buffer used for sample manipulation */
  58. #define SOFTMIX_DATALEN(rate, interval) ((rate/50) * (interval / 10))
  59. /*! \brief Number of samples we are dealing with */
  60. #define SOFTMIX_SAMPLES(rate, interval) (SOFTMIX_DATALEN(rate, interval) / 2)
  61. /*! \brief Number of mixing iterations to perform between gathering statistics. */
  62. #define SOFTMIX_STAT_INTERVAL 100
  63. /*!
  64. * \brief Default time in ms of silence necessary to declare talking stopped by the bridge.
  65. *
  66. * \details
  67. * This is the time at which a channel's own audio will stop getting
  68. * mixed out of its own write audio stream because it is no longer talking.
  69. */
  70. #define DEFAULT_SOFTMIX_SILENCE_THRESHOLD 2500
  71. /*! Default minimum average magnitude threshold to determine talking by the DSP. */
  72. #define DEFAULT_SOFTMIX_TALKING_THRESHOLD 160
  73. #define DEFAULT_ENERGY_HISTORY_LEN 150
  74. struct video_follow_talker_data {
  75. /*! audio energy history */
  76. int energy_history[DEFAULT_ENERGY_HISTORY_LEN];
  77. /*! The current slot being used in the history buffer, this
  78. * increments and wraps around */
  79. int energy_history_cur_slot;
  80. /*! The current energy sum used for averages. */
  81. int energy_accum;
  82. /*! The current energy average */
  83. int energy_average;
  84. };
  85. /*! \brief Structure which contains per-channel mixing information */
  86. struct softmix_channel {
  87. /*! Lock to protect this structure */
  88. ast_mutex_t lock;
  89. /*! Factory which contains audio read in from the channel */
  90. struct ast_slinfactory factory;
  91. /*! Frame that contains mixed audio to be written out to the channel */
  92. struct ast_frame write_frame;
  93. /*! Current expected read slinear format. */
  94. struct ast_format *read_slin_format;
  95. /*! DSP for detecting silence */
  96. struct ast_dsp *dsp;
  97. /*!
  98. * \brief TRUE if a channel is talking.
  99. *
  100. * \note This affects how the channel's audio is mixed back to
  101. * it.
  102. */
  103. unsigned int talking:1;
  104. /*! TRUE if the channel provided audio for this mixing interval */
  105. unsigned int have_audio:1;
  106. /*! Buffer containing final mixed audio from all sources */
  107. short final_buf[MAX_DATALEN];
  108. /*! Buffer containing only the audio from the channel */
  109. short our_buf[MAX_DATALEN];
  110. /*! Data pertaining to talker mode for video conferencing */
  111. struct video_follow_talker_data video_talker;
  112. };
  113. struct softmix_bridge_data {
  114. struct ast_timer *timer;
  115. /*!
  116. * \brief Bridge pointer passed to the softmix mixing thread.
  117. *
  118. * \note Does not need a reference because the bridge will
  119. * always exist while the mixing thread exists even if the
  120. * bridge is no longer actively using the softmix technology.
  121. */
  122. struct ast_bridge *bridge;
  123. /*! Lock for signaling the mixing thread. */
  124. ast_mutex_t lock;
  125. /*! Condition, used if we need to wake up the mixing thread. */
  126. ast_cond_t cond;
  127. /*! Thread handling the mixing */
  128. pthread_t thread;
  129. unsigned int internal_rate;
  130. unsigned int internal_mixing_interval;
  131. /*! TRUE if the mixing thread should stop */
  132. unsigned int stop:1;
  133. };
  134. struct softmix_stats {
  135. /*! Each index represents a sample rate used above the internal rate. */
  136. unsigned int sample_rates[16];
  137. /*! Each index represents the number of channels using the same index in the sample_rates array. */
  138. unsigned int num_channels[16];
  139. /*! The number of channels above the internal sample rate */
  140. unsigned int num_above_internal_rate;
  141. /*! The number of channels at the internal sample rate */
  142. unsigned int num_at_internal_rate;
  143. /*! The absolute highest sample rate preferred by any channel in the bridge */
  144. unsigned int highest_supported_rate;
  145. /*! Is the sample rate locked by the bridge, if so what is that rate.*/
  146. unsigned int locked_rate;
  147. };
  148. struct softmix_mixing_array {
  149. unsigned int max_num_entries;
  150. unsigned int used_entries;
  151. int16_t **buffers;
  152. };
  153. struct softmix_translate_helper_entry {
  154. int num_times_requested; /*!< Once this entry is no longer requested, free the trans_pvt
  155. and re-init if it was usable. */
  156. struct ast_format *dst_format; /*!< The destination format for this helper */
  157. struct ast_trans_pvt *trans_pvt; /*!< the translator for this slot. */
  158. struct ast_frame *out_frame; /*!< The output frame from the last translation */
  159. AST_LIST_ENTRY(softmix_translate_helper_entry) entry;
  160. };
  161. struct softmix_translate_helper {
  162. struct ast_format *slin_src; /*!< the source format expected for all the translators */
  163. AST_LIST_HEAD_NOLOCK(, softmix_translate_helper_entry) entries;
  164. };
  165. static struct softmix_translate_helper_entry *softmix_translate_helper_entry_alloc(struct ast_format *dst)
  166. {
  167. struct softmix_translate_helper_entry *entry;
  168. if (!(entry = ast_calloc(1, sizeof(*entry)))) {
  169. return NULL;
  170. }
  171. entry->dst_format = ao2_bump(dst);
  172. /* initialize this to one so that the first time through the cleanup code after
  173. allocation it won't be removed from the entry list */
  174. entry->num_times_requested = 1;
  175. return entry;
  176. }
  177. static void *softmix_translate_helper_free_entry(struct softmix_translate_helper_entry *entry)
  178. {
  179. ao2_cleanup(entry->dst_format);
  180. if (entry->trans_pvt) {
  181. ast_translator_free_path(entry->trans_pvt);
  182. }
  183. if (entry->out_frame) {
  184. ast_frfree(entry->out_frame);
  185. }
  186. ast_free(entry);
  187. return NULL;
  188. }
  189. static void softmix_translate_helper_init(struct softmix_translate_helper *trans_helper, unsigned int sample_rate)
  190. {
  191. memset(trans_helper, 0, sizeof(*trans_helper));
  192. trans_helper->slin_src = ast_format_cache_get_slin_by_rate(sample_rate);
  193. }
  194. static void softmix_translate_helper_destroy(struct softmix_translate_helper *trans_helper)
  195. {
  196. struct softmix_translate_helper_entry *entry;
  197. while ((entry = AST_LIST_REMOVE_HEAD(&trans_helper->entries, entry))) {
  198. softmix_translate_helper_free_entry(entry);
  199. }
  200. }
  201. static void softmix_translate_helper_change_rate(struct softmix_translate_helper *trans_helper, unsigned int sample_rate)
  202. {
  203. struct softmix_translate_helper_entry *entry;
  204. trans_helper->slin_src = ast_format_cache_get_slin_by_rate(sample_rate);
  205. AST_LIST_TRAVERSE_SAFE_BEGIN(&trans_helper->entries, entry, entry) {
  206. if (entry->trans_pvt) {
  207. ast_translator_free_path(entry->trans_pvt);
  208. if (!(entry->trans_pvt = ast_translator_build_path(entry->dst_format, trans_helper->slin_src))) {
  209. AST_LIST_REMOVE_CURRENT(entry);
  210. entry = softmix_translate_helper_free_entry(entry);
  211. }
  212. }
  213. }
  214. AST_LIST_TRAVERSE_SAFE_END;
  215. }
  216. /*!
  217. * \internal
  218. * \brief Get the next available audio on the softmix channel's read stream
  219. * and determine if it should be mixed out or not on the write stream.
  220. *
  221. * \retval pointer to buffer containing the exact number of samples requested on success.
  222. * \retval NULL if no samples are present
  223. */
  224. static int16_t *softmix_process_read_audio(struct softmix_channel *sc, unsigned int num_samples)
  225. {
  226. if ((ast_slinfactory_available(&sc->factory) >= num_samples) &&
  227. ast_slinfactory_read(&sc->factory, sc->our_buf, num_samples)) {
  228. sc->have_audio = 1;
  229. return sc->our_buf;
  230. }
  231. sc->have_audio = 0;
  232. return NULL;
  233. }
  234. /*!
  235. * \internal
  236. * \brief Process a softmix channel's write audio
  237. *
  238. * \details This function will remove the channel's talking from its own audio if present and
  239. * possibly even do the channel's write translation for it depending on how many other
  240. * channels use the same write format.
  241. */
  242. static void softmix_process_write_audio(struct softmix_translate_helper *trans_helper,
  243. struct ast_format *raw_write_fmt,
  244. struct softmix_channel *sc)
  245. {
  246. struct softmix_translate_helper_entry *entry = NULL;
  247. int i;
  248. /* If we provided audio that was not determined to be silence,
  249. * then take it out while in slinear format. */
  250. if (sc->have_audio && sc->talking) {
  251. for (i = 0; i < sc->write_frame.samples; i++) {
  252. ast_slinear_saturated_subtract(&sc->final_buf[i], &sc->our_buf[i]);
  253. }
  254. /* check to see if any entries exist for the format. if not we'll want
  255. to remove it during cleanup */
  256. AST_LIST_TRAVERSE(&trans_helper->entries, entry, entry) {
  257. if (ast_format_cmp(entry->dst_format, raw_write_fmt) == AST_FORMAT_CMP_EQUAL) {
  258. ++entry->num_times_requested;
  259. break;
  260. }
  261. }
  262. /* do not do any special write translate optimization if we had to make
  263. * a special mix for them to remove their own audio. */
  264. return;
  265. }
  266. /* Attempt to optimize channels using the same translation path/codec. Build a list of entries
  267. of translation paths and track the number of references for each type. Each one of the same
  268. type should be able to use the same out_frame. Since the optimization is only necessary for
  269. multiple channels (>=2) using the same codec make sure resources are allocated only when
  270. needed and released when not (see also softmix_translate_helper_cleanup */
  271. AST_LIST_TRAVERSE(&trans_helper->entries, entry, entry) {
  272. if (ast_format_cmp(entry->dst_format, raw_write_fmt) == AST_FORMAT_CMP_EQUAL) {
  273. entry->num_times_requested++;
  274. } else {
  275. continue;
  276. }
  277. if (!entry->trans_pvt && (entry->num_times_requested > 1)) {
  278. entry->trans_pvt = ast_translator_build_path(entry->dst_format, trans_helper->slin_src);
  279. }
  280. if (entry->trans_pvt && !entry->out_frame) {
  281. entry->out_frame = ast_translate(entry->trans_pvt, &sc->write_frame, 0);
  282. }
  283. if (entry->out_frame && entry->out_frame->frametype == AST_FRAME_VOICE
  284. && entry->out_frame->datalen < MAX_DATALEN) {
  285. ao2_replace(sc->write_frame.subclass.format, entry->out_frame->subclass.format);
  286. memcpy(sc->final_buf, entry->out_frame->data.ptr, entry->out_frame->datalen);
  287. sc->write_frame.datalen = entry->out_frame->datalen;
  288. sc->write_frame.samples = entry->out_frame->samples;
  289. }
  290. break;
  291. }
  292. /* add new entry into list if this format destination was not matched. */
  293. if (!entry && (entry = softmix_translate_helper_entry_alloc(raw_write_fmt))) {
  294. AST_LIST_INSERT_HEAD(&trans_helper->entries, entry, entry);
  295. }
  296. }
  297. static void softmix_translate_helper_cleanup(struct softmix_translate_helper *trans_helper)
  298. {
  299. struct softmix_translate_helper_entry *entry;
  300. AST_LIST_TRAVERSE_SAFE_BEGIN(&trans_helper->entries, entry, entry) {
  301. /* if it hasn't been requested then remove it */
  302. if (!entry->num_times_requested) {
  303. AST_LIST_REMOVE_CURRENT(entry);
  304. softmix_translate_helper_free_entry(entry);
  305. continue;
  306. }
  307. if (entry->out_frame) {
  308. ast_frfree(entry->out_frame);
  309. entry->out_frame = NULL;
  310. }
  311. /* nothing is optimized for a single path reference, so there is
  312. no reason to continue to hold onto the codec */
  313. if (entry->num_times_requested == 1 && entry->trans_pvt) {
  314. ast_translator_free_path(entry->trans_pvt);
  315. entry->trans_pvt = NULL;
  316. }
  317. /* for each iteration (a mixing run) in the bridge softmix thread the number
  318. of references to a given entry is recalculated, so reset the number of
  319. times requested */
  320. entry->num_times_requested = 0;
  321. }
  322. AST_LIST_TRAVERSE_SAFE_END;
  323. }
  324. static void set_softmix_bridge_data(int rate, int interval, struct ast_bridge_channel *bridge_channel, int reset)
  325. {
  326. struct softmix_channel *sc = bridge_channel->tech_pvt;
  327. struct ast_format *slin_format;
  328. int setup_fail;
  329. /* The callers have already ensured that sc is never NULL. */
  330. ast_assert(sc != NULL);
  331. slin_format = ast_format_cache_get_slin_by_rate(rate);
  332. ast_mutex_lock(&sc->lock);
  333. if (reset) {
  334. ast_slinfactory_destroy(&sc->factory);
  335. ast_dsp_free(sc->dsp);
  336. }
  337. /* Setup write frame parameters */
  338. sc->write_frame.frametype = AST_FRAME_VOICE;
  339. /*
  340. * NOTE: The write_frame format holds a reference because translation
  341. * could be needed and the format changed to the translated format
  342. * for the channel. The translated format may not be a
  343. * static cached format.
  344. */
  345. ao2_replace(sc->write_frame.subclass.format, slin_format);
  346. sc->write_frame.data.ptr = sc->final_buf;
  347. sc->write_frame.datalen = SOFTMIX_DATALEN(rate, interval);
  348. sc->write_frame.samples = SOFTMIX_SAMPLES(rate, interval);
  349. /*
  350. * NOTE: The read_slin_format does not hold a reference because it
  351. * will always be a signed linear format.
  352. */
  353. sc->read_slin_format = slin_format;
  354. /* Setup smoother */
  355. setup_fail = ast_slinfactory_init_with_format(&sc->factory, slin_format);
  356. /* set new read and write formats on channel. */
  357. ast_channel_lock(bridge_channel->chan);
  358. setup_fail |= ast_set_read_format_path(bridge_channel->chan,
  359. ast_channel_rawreadformat(bridge_channel->chan), slin_format);
  360. ast_channel_unlock(bridge_channel->chan);
  361. setup_fail |= ast_set_write_format(bridge_channel->chan, slin_format);
  362. /* set up new DSP. This is on the read side only right before the read frame enters the smoother. */
  363. sc->dsp = ast_dsp_new_with_rate(rate);
  364. if (setup_fail || !sc->dsp) {
  365. /* Bad news. Could not setup the channel for softmix. */
  366. ast_mutex_unlock(&sc->lock);
  367. ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END, 0);
  368. return;
  369. }
  370. /* we want to aggressively detect silence to avoid feedback */
  371. if (bridge_channel->tech_args.talking_threshold) {
  372. ast_dsp_set_threshold(sc->dsp, bridge_channel->tech_args.talking_threshold);
  373. } else {
  374. ast_dsp_set_threshold(sc->dsp, DEFAULT_SOFTMIX_TALKING_THRESHOLD);
  375. }
  376. ast_mutex_unlock(&sc->lock);
  377. }
  378. /*!
  379. * \internal
  380. * \brief Poke the mixing thread in case it is waiting for an active channel.
  381. * \since 12.0.0
  382. *
  383. * \param softmix_data Bridge mixing data.
  384. *
  385. * \return Nothing
  386. */
  387. static void softmix_poke_thread(struct softmix_bridge_data *softmix_data)
  388. {
  389. ast_mutex_lock(&softmix_data->lock);
  390. ast_cond_signal(&softmix_data->cond);
  391. ast_mutex_unlock(&softmix_data->lock);
  392. }
  393. /*! \brief Function called when a channel is unsuspended from the bridge */
  394. static void softmix_bridge_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  395. {
  396. if (bridge->tech_pvt) {
  397. softmix_poke_thread(bridge->tech_pvt);
  398. }
  399. }
  400. /*! \brief Function called when a channel is joined into the bridge */
  401. static int softmix_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  402. {
  403. struct softmix_channel *sc;
  404. struct softmix_bridge_data *softmix_data;
  405. softmix_data = bridge->tech_pvt;
  406. if (!softmix_data) {
  407. return -1;
  408. }
  409. /* Create a new softmix_channel structure and allocate various things on it */
  410. if (!(sc = ast_calloc(1, sizeof(*sc)))) {
  411. return -1;
  412. }
  413. /* Can't forget the lock */
  414. ast_mutex_init(&sc->lock);
  415. /* Can't forget to record our pvt structure within the bridged channel structure */
  416. bridge_channel->tech_pvt = sc;
  417. set_softmix_bridge_data(softmix_data->internal_rate,
  418. softmix_data->internal_mixing_interval
  419. ? softmix_data->internal_mixing_interval
  420. : DEFAULT_SOFTMIX_INTERVAL,
  421. bridge_channel, 0);
  422. softmix_poke_thread(softmix_data);
  423. return 0;
  424. }
  425. /*! \brief Function called when a channel leaves the bridge */
  426. static void softmix_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  427. {
  428. struct softmix_channel *sc = bridge_channel->tech_pvt;
  429. if (!sc) {
  430. return;
  431. }
  432. bridge_channel->tech_pvt = NULL;
  433. /* Drop mutex lock */
  434. ast_mutex_destroy(&sc->lock);
  435. /* Drop the factory */
  436. ast_slinfactory_destroy(&sc->factory);
  437. /* Drop any formats on the frames */
  438. ao2_cleanup(sc->write_frame.subclass.format);
  439. /* Drop the DSP */
  440. ast_dsp_free(sc->dsp);
  441. /* Eep! drop ourselves */
  442. ast_free(sc);
  443. }
  444. static void softmix_pass_video_top_priority(struct ast_bridge *bridge, struct ast_frame *frame)
  445. {
  446. struct ast_bridge_channel *cur;
  447. AST_LIST_TRAVERSE(&bridge->channels, cur, entry) {
  448. if (cur->suspended) {
  449. continue;
  450. }
  451. if (ast_bridge_is_video_src(bridge, cur->chan) == 1) {
  452. ast_bridge_channel_queue_frame(cur, frame);
  453. break;
  454. }
  455. }
  456. }
  457. /*!
  458. * \internal
  459. * \brief Determine what to do with a video frame.
  460. * \since 12.0.0
  461. *
  462. * \param bridge Which bridge is getting the frame
  463. * \param bridge_channel Which channel is writing the frame.
  464. * \param frame What is being written.
  465. *
  466. * \return Nothing
  467. */
  468. static void softmix_bridge_write_video(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
  469. {
  470. struct softmix_channel *sc;
  471. int video_src_priority;
  472. /* Determine if the video frame should be distributed or not */
  473. switch (bridge->softmix.video_mode.mode) {
  474. case AST_BRIDGE_VIDEO_MODE_NONE:
  475. break;
  476. case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
  477. video_src_priority = ast_bridge_is_video_src(bridge, bridge_channel->chan);
  478. if (video_src_priority == 1) {
  479. /* Pass to me and everyone else. */
  480. ast_bridge_queue_everyone_else(bridge, NULL, frame);
  481. }
  482. break;
  483. case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
  484. sc = bridge_channel->tech_pvt;
  485. ast_mutex_lock(&sc->lock);
  486. ast_bridge_update_talker_src_video_mode(bridge, bridge_channel->chan,
  487. sc->video_talker.energy_average,
  488. frame->subclass.frame_ending);
  489. ast_mutex_unlock(&sc->lock);
  490. video_src_priority = ast_bridge_is_video_src(bridge, bridge_channel->chan);
  491. if (video_src_priority == 1) {
  492. int num_src = ast_bridge_number_video_src(bridge);
  493. int echo = num_src > 1 ? 0 : 1;
  494. ast_bridge_queue_everyone_else(bridge, echo ? NULL : bridge_channel, frame);
  495. } else if (video_src_priority == 2) {
  496. softmix_pass_video_top_priority(bridge, frame);
  497. }
  498. break;
  499. }
  500. }
  501. /*!
  502. * \internal
  503. * \brief Determine what to do with a voice frame.
  504. * \since 12.0.0
  505. *
  506. * \param bridge Which bridge is getting the frame
  507. * \param bridge_channel Which channel is writing the frame.
  508. * \param frame What is being written.
  509. *
  510. * \return Nothing
  511. */
  512. static void softmix_bridge_write_voice(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
  513. {
  514. struct softmix_channel *sc = bridge_channel->tech_pvt;
  515. struct softmix_bridge_data *softmix_data = bridge->tech_pvt;
  516. int silent = 0;
  517. int totalsilence = 0;
  518. int cur_energy = 0;
  519. int silence_threshold = bridge_channel->tech_args.silence_threshold ?
  520. bridge_channel->tech_args.silence_threshold :
  521. DEFAULT_SOFTMIX_SILENCE_THRESHOLD;
  522. /*
  523. * If update_talking is set to 0 or 1, tell the bridge that the channel
  524. * has started or stopped talking.
  525. */
  526. char update_talking = -1;
  527. /* Write the frame into the conference */
  528. ast_mutex_lock(&sc->lock);
  529. if (ast_format_cmp(frame->subclass.format, sc->read_slin_format) != AST_FORMAT_CMP_EQUAL) {
  530. /*
  531. * The incoming frame is not the expected format. Update
  532. * the channel's translation path to get us slinear from
  533. * the new format for the next frame.
  534. *
  535. * There is the possibility that this frame is an old slinear
  536. * rate frame that was in flight when the softmix bridge
  537. * changed rates. If so it will self correct on subsequent
  538. * frames.
  539. */
  540. ast_channel_lock(bridge_channel->chan);
  541. ast_debug(1, "Channel %s wrote unexpected format into bridge. Got %s, expected %s.\n",
  542. ast_channel_name(bridge_channel->chan),
  543. ast_format_get_name(frame->subclass.format),
  544. ast_format_get_name(sc->read_slin_format));
  545. ast_set_read_format_path(bridge_channel->chan, frame->subclass.format,
  546. sc->read_slin_format);
  547. ast_channel_unlock(bridge_channel->chan);
  548. }
  549. /* The channel will be leaving soon if there is no dsp. */
  550. if (sc->dsp) {
  551. silent = ast_dsp_silence_with_energy(sc->dsp, frame, &totalsilence, &cur_energy);
  552. }
  553. if (bridge->softmix.video_mode.mode == AST_BRIDGE_VIDEO_MODE_TALKER_SRC) {
  554. int cur_slot = sc->video_talker.energy_history_cur_slot;
  555. sc->video_talker.energy_accum -= sc->video_talker.energy_history[cur_slot];
  556. sc->video_talker.energy_accum += cur_energy;
  557. sc->video_talker.energy_history[cur_slot] = cur_energy;
  558. sc->video_talker.energy_average = sc->video_talker.energy_accum / DEFAULT_ENERGY_HISTORY_LEN;
  559. sc->video_talker.energy_history_cur_slot++;
  560. if (sc->video_talker.energy_history_cur_slot == DEFAULT_ENERGY_HISTORY_LEN) {
  561. sc->video_talker.energy_history_cur_slot = 0; /* wrap around */
  562. }
  563. }
  564. if (totalsilence < silence_threshold) {
  565. if (!sc->talking && !silent) {
  566. /* Tell the write process we have audio to be mixed out */
  567. sc->talking = 1;
  568. update_talking = 1;
  569. }
  570. } else {
  571. if (sc->talking) {
  572. sc->talking = 0;
  573. update_talking = 0;
  574. }
  575. }
  576. /* Before adding audio in, make sure we haven't fallen behind. If audio has fallen
  577. * behind 4 times the amount of samples mixed on every iteration of the mixer, Re-sync
  578. * the audio by flushing the buffer before adding new audio in. */
  579. if (ast_slinfactory_available(&sc->factory) > (4 * SOFTMIX_SAMPLES(softmix_data->internal_rate, softmix_data->internal_mixing_interval))) {
  580. ast_slinfactory_flush(&sc->factory);
  581. }
  582. if (sc->talking || !bridge_channel->tech_args.drop_silence) {
  583. /* Add frame to the smoother for mixing with other channels. */
  584. ast_slinfactory_feed(&sc->factory, frame);
  585. }
  586. /* Alllll done */
  587. ast_mutex_unlock(&sc->lock);
  588. if (update_talking != -1) {
  589. ast_bridge_channel_notify_talking(bridge_channel, update_talking);
  590. }
  591. }
  592. /*!
  593. * \internal
  594. * \brief Clear talking flag, stop contributing to mixing and notify handlers.
  595. * \since 13.21.0, 15.4.0
  596. *
  597. * \param bridge_channel Which channel's talking to clear
  598. *
  599. * \return Nothing
  600. */
  601. static void clear_talking(struct ast_bridge_channel *bridge_channel)
  602. {
  603. struct softmix_channel *sc = bridge_channel->tech_pvt;
  604. if (sc->talking) {
  605. ast_mutex_lock(&sc->lock);
  606. ast_slinfactory_flush(&sc->factory);
  607. sc->talking = 0;
  608. ast_mutex_unlock(&sc->lock);
  609. /* Notify that we are no longer talking. */
  610. ast_bridge_channel_notify_talking(bridge_channel, 0);
  611. }
  612. }
  613. /*!
  614. * \internal
  615. * \brief Check for voice status updates.
  616. * \since 13.20.0
  617. *
  618. * \param bridge Which bridge we are in
  619. * \param bridge_channel Which channel we are checking
  620. *
  621. * \return Nothing
  622. */
  623. static void softmix_bridge_check_voice(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  624. {
  625. if (bridge_channel->features->mute) {
  626. /*
  627. * We were muted while we were talking.
  628. *
  629. * Immediately stop contributing to mixing
  630. * and report no longer talking.
  631. */
  632. clear_talking(bridge_channel);
  633. }
  634. }
  635. /*!
  636. * \internal
  637. * \brief Determine what to do with a text frame.
  638. * \since 13.22.0
  639. * \since 15.5.0
  640. *
  641. * \param bridge Which bridge is getting the frame
  642. * \param bridge_channel Which channel is writing the frame.
  643. * \param frame What is being written.
  644. *
  645. * \return Nothing
  646. */
  647. static void softmix_bridge_write_text(struct ast_bridge *bridge,
  648. struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
  649. {
  650. if (DEBUG_ATLEAST(1)) {
  651. struct ast_msg_data *msg = frame->data.ptr;
  652. char frame_type[64];
  653. ast_frame_type2str(frame->frametype, frame_type, sizeof(frame_type));
  654. if (frame->frametype == AST_FRAME_TEXT_DATA) {
  655. ast_log(LOG_DEBUG, "Received %s frame from '%s:%s': %s\n", frame_type,
  656. ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_FROM),
  657. ast_channel_name(bridge_channel->chan),
  658. ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_BODY));
  659. } else {
  660. ast_log(LOG_DEBUG, "Received %s frame from '%s': %.*s\n", frame_type,
  661. ast_channel_name(bridge_channel->chan), frame->datalen,
  662. (char *)frame->data.ptr);
  663. }
  664. }
  665. ast_bridge_queue_everyone_else(bridge, bridge_channel, frame);
  666. }
  667. /*!
  668. * \internal
  669. * \brief Determine what to do with a control frame.
  670. * \since 12.0.0
  671. *
  672. * \param bridge Which bridge is getting the frame
  673. * \param bridge_channel Which channel is writing the frame.
  674. * \param frame What is being written.
  675. *
  676. * \retval 0 Frame accepted into the bridge.
  677. * \retval -1 Frame needs to be deferred.
  678. */
  679. static int softmix_bridge_write_control(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
  680. {
  681. /*
  682. * XXX Softmix needs to use channel roles to determine what to
  683. * do with control frames.
  684. */
  685. switch (frame->subclass.integer) {
  686. case AST_CONTROL_HOLD:
  687. /*
  688. * Doing anything for holds in a conference bridge could be considered a bit
  689. * odd. That being said, in most cases one would probably want the talking
  690. * flag cleared when 'hold' is pressed by the remote endpoint, so go ahead
  691. * and do that here. However, that is all we'll do. Meaning if for some reason
  692. * the endpoint continues to send audio frames despite pressing 'hold' talking
  693. * will once again be detected for that channel.
  694. */
  695. clear_talking(bridge_channel);
  696. break;
  697. case AST_CONTROL_VIDUPDATE:
  698. ast_bridge_queue_everyone_else(bridge, NULL, frame);
  699. break;
  700. default:
  701. break;
  702. }
  703. return 0;
  704. }
  705. /*!
  706. * \internal
  707. * \brief Determine what to do with a frame written into the bridge.
  708. * \since 12.0.0
  709. *
  710. * \param bridge Which bridge is getting the frame
  711. * \param bridge_channel Which channel is writing the frame.
  712. * \param frame What is being written.
  713. *
  714. * \retval 0 Frame accepted into the bridge.
  715. * \retval -1 Frame needs to be deferred.
  716. *
  717. * \note On entry, bridge is already locked.
  718. */
  719. static int softmix_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
  720. {
  721. int res = 0;
  722. if (!bridge->tech_pvt || !bridge_channel || !bridge_channel->tech_pvt) {
  723. /* "Accept" the frame and discard it. */
  724. return 0;
  725. }
  726. /*
  727. * XXX Softmix needs to use channel roles to determine who gets
  728. * what frame. Possible roles: announcer, recorder, agent,
  729. * supervisor.
  730. */
  731. switch (frame->frametype) {
  732. case AST_FRAME_NULL:
  733. /* "Accept" the frame and discard it. */
  734. softmix_bridge_check_voice(bridge, bridge_channel);
  735. break;
  736. case AST_FRAME_DTMF_BEGIN:
  737. case AST_FRAME_DTMF_END:
  738. res = ast_bridge_queue_everyone_else(bridge, bridge_channel, frame);
  739. break;
  740. case AST_FRAME_VOICE:
  741. softmix_bridge_write_voice(bridge, bridge_channel, frame);
  742. break;
  743. case AST_FRAME_VIDEO:
  744. softmix_bridge_write_video(bridge, bridge_channel, frame);
  745. break;
  746. case AST_FRAME_TEXT:
  747. case AST_FRAME_TEXT_DATA:
  748. softmix_bridge_write_text(bridge, bridge_channel, frame);
  749. break;
  750. case AST_FRAME_CONTROL:
  751. res = softmix_bridge_write_control(bridge, bridge_channel, frame);
  752. break;
  753. case AST_FRAME_BRIDGE_ACTION:
  754. res = ast_bridge_queue_everyone_else(bridge, bridge_channel, frame);
  755. break;
  756. case AST_FRAME_BRIDGE_ACTION_SYNC:
  757. ast_log(LOG_ERROR, "Synchronous bridge action written to a softmix bridge.\n");
  758. ast_assert(0);
  759. default:
  760. ast_debug(3, "Frame type %u unsupported\n", frame->frametype);
  761. /* "Accept" the frame and discard it. */
  762. break;
  763. }
  764. return res;
  765. }
  766. static void gather_softmix_stats(struct softmix_stats *stats,
  767. const struct softmix_bridge_data *softmix_data,
  768. struct ast_bridge_channel *bridge_channel)
  769. {
  770. int channel_native_rate;
  771. /* Gather stats about channel sample rates. */
  772. ast_channel_lock(bridge_channel->chan);
  773. channel_native_rate = MAX(SOFTMIX_MIN_SAMPLE_RATE,
  774. ast_format_get_sample_rate(ast_channel_rawreadformat(bridge_channel->chan)));
  775. ast_channel_unlock(bridge_channel->chan);
  776. if (stats->highest_supported_rate < channel_native_rate) {
  777. stats->highest_supported_rate = channel_native_rate;
  778. }
  779. if (softmix_data->internal_rate < channel_native_rate) {
  780. int i;
  781. for (i = 0; i < ARRAY_LEN(stats->sample_rates); i++) {
  782. if (stats->sample_rates[i] == channel_native_rate) {
  783. stats->num_channels[i]++;
  784. break;
  785. } else if (!stats->sample_rates[i]) {
  786. stats->sample_rates[i] = channel_native_rate;
  787. stats->num_channels[i]++;
  788. break;
  789. }
  790. }
  791. stats->num_above_internal_rate++;
  792. } else if (softmix_data->internal_rate == channel_native_rate) {
  793. stats->num_at_internal_rate++;
  794. }
  795. }
  796. /*!
  797. * \internal
  798. * \brief Analyse mixing statistics and change bridges internal rate
  799. * if necessary.
  800. *
  801. * \retval 0, no changes to internal rate
  802. * \retval 1, internal rate was changed, update all the channels on the next mixing iteration.
  803. */
  804. static unsigned int analyse_softmix_stats(struct softmix_stats *stats, struct softmix_bridge_data *softmix_data)
  805. {
  806. int i;
  807. /*
  808. * Re-adjust the internal bridge sample rate if
  809. * 1. The bridge's internal sample rate is locked in at a sample
  810. * rate other than the current sample rate being used.
  811. * 2. two or more channels support a higher sample rate
  812. * 3. no channels support the current sample rate or a higher rate
  813. */
  814. if (stats->locked_rate) {
  815. /* if the rate is locked by the bridge, only update it if it differs
  816. * from the current rate we are using. */
  817. if (softmix_data->internal_rate != stats->locked_rate) {
  818. ast_debug(1, "Locking at new rate. Bridge changed from %u to %u.\n",
  819. softmix_data->internal_rate, stats->locked_rate);
  820. softmix_data->internal_rate = stats->locked_rate;
  821. return 1;
  822. }
  823. } else if (stats->num_above_internal_rate >= 2) {
  824. /* the highest rate is just used as a starting point */
  825. unsigned int best_rate = stats->highest_supported_rate;
  826. int best_index = -1;
  827. for (i = 0; i < ARRAY_LEN(stats->num_channels); i++) {
  828. if (stats->num_channels[i]) {
  829. break;
  830. }
  831. if (2 <= stats->num_channels[i]) {
  832. /* Two or more channels support this rate. */
  833. if (best_index == -1
  834. || stats->sample_rates[best_index] < stats->sample_rates[i]) {
  835. /*
  836. * best_rate starts out being the first sample rate
  837. * greater than the internal sample rate that two or
  838. * more channels support.
  839. *
  840. * or
  841. *
  842. * There are multiple rates above the internal rate
  843. * and this rate is higher than the previous rate two
  844. * or more channels support.
  845. */
  846. best_rate = stats->sample_rates[i];
  847. best_index = i;
  848. }
  849. } else if (best_index == -1) {
  850. /*
  851. * It is possible that multiple channels exist with native sample
  852. * rates above the internal sample rate, but none of those channels
  853. * have the same rate in common. In this case, the lowest sample
  854. * rate among those channels is picked. Over time as additional
  855. * statistic runs are made the internal sample rate number will
  856. * adjust to the most optimal sample rate, but it may take multiple
  857. * iterations.
  858. */
  859. best_rate = MIN(best_rate, stats->sample_rates[i]);
  860. }
  861. }
  862. ast_debug(1, "Multiple above internal rate. Bridge changed from %u to %u.\n",
  863. softmix_data->internal_rate, best_rate);
  864. softmix_data->internal_rate = best_rate;
  865. return 1;
  866. } else if (!stats->num_at_internal_rate && !stats->num_above_internal_rate) {
  867. /* In this case, the highest supported rate is actually lower than the internal rate */
  868. ast_debug(1, "All below internal rate. Bridge changed from %u to %u.\n",
  869. softmix_data->internal_rate, stats->highest_supported_rate);
  870. softmix_data->internal_rate = stats->highest_supported_rate;
  871. return 1;
  872. }
  873. return 0;
  874. }
  875. static int softmix_mixing_array_init(struct softmix_mixing_array *mixing_array, unsigned int starting_num_entries)
  876. {
  877. memset(mixing_array, 0, sizeof(*mixing_array));
  878. mixing_array->max_num_entries = starting_num_entries;
  879. if (!(mixing_array->buffers = ast_calloc(mixing_array->max_num_entries, sizeof(int16_t *)))) {
  880. ast_log(LOG_NOTICE, "Failed to allocate softmix mixing structure.\n");
  881. return -1;
  882. }
  883. return 0;
  884. }
  885. static void softmix_mixing_array_destroy(struct softmix_mixing_array *mixing_array)
  886. {
  887. ast_free(mixing_array->buffers);
  888. }
  889. static int softmix_mixing_array_grow(struct softmix_mixing_array *mixing_array, unsigned int num_entries)
  890. {
  891. int16_t **tmp;
  892. /* give it some room to grow since memory is cheap but allocations can be expensive */
  893. mixing_array->max_num_entries = num_entries;
  894. if (!(tmp = ast_realloc(mixing_array->buffers, (mixing_array->max_num_entries * sizeof(int16_t *))))) {
  895. ast_log(LOG_NOTICE, "Failed to re-allocate softmix mixing structure.\n");
  896. return -1;
  897. }
  898. mixing_array->buffers = tmp;
  899. return 0;
  900. }
  901. /*!
  902. * \brief Mixing loop.
  903. *
  904. * \retval 0 on success
  905. * \retval -1 on failure
  906. */
  907. static int softmix_mixing_loop(struct ast_bridge *bridge)
  908. {
  909. struct softmix_stats stats = { { 0 }, };
  910. struct softmix_mixing_array mixing_array;
  911. struct softmix_bridge_data *softmix_data = bridge->tech_pvt;
  912. struct ast_timer *timer;
  913. struct softmix_translate_helper trans_helper;
  914. int16_t buf[MAX_DATALEN];
  915. unsigned int stat_iteration_counter = 0; /* counts down, gather stats at zero and reset. */
  916. int timingfd;
  917. int update_all_rates = 0; /* set this when the internal sample rate has changed */
  918. unsigned int idx;
  919. unsigned int x;
  920. int res = -1;
  921. timer = softmix_data->timer;
  922. timingfd = ast_timer_fd(timer);
  923. softmix_translate_helper_init(&trans_helper, softmix_data->internal_rate);
  924. ast_timer_set_rate(timer, (1000 / softmix_data->internal_mixing_interval));
  925. /* Give the mixing array room to grow, memory is cheap but allocations are expensive. */
  926. if (softmix_mixing_array_init(&mixing_array, bridge->num_channels + 10)) {
  927. goto softmix_cleanup;
  928. }
  929. /*
  930. * XXX Softmix needs to use channel roles to determine who gets
  931. * what audio mixed.
  932. */
  933. while (!softmix_data->stop && bridge->num_active) {
  934. struct ast_bridge_channel *bridge_channel;
  935. int timeout = -1;
  936. struct ast_format *cur_slin = ast_format_cache_get_slin_by_rate(softmix_data->internal_rate);
  937. unsigned int softmix_samples = SOFTMIX_SAMPLES(softmix_data->internal_rate, softmix_data->internal_mixing_interval);
  938. unsigned int softmix_datalen = SOFTMIX_DATALEN(softmix_data->internal_rate, softmix_data->internal_mixing_interval);
  939. if (softmix_datalen > MAX_DATALEN) {
  940. /* This should NEVER happen, but if it does we need to know about it. Almost
  941. * all the memcpys used during this process depend on this assumption. Rather
  942. * than checking this over and over again through out the code, this single
  943. * verification is done on each iteration. */
  944. ast_log(LOG_WARNING,
  945. "Bridge %s: Conference mixing error, requested mixing length greater than mixing buffer.\n",
  946. bridge->uniqueid);
  947. goto softmix_cleanup;
  948. }
  949. /* Grow the mixing array buffer as participants are added. */
  950. if (mixing_array.max_num_entries < bridge->num_channels
  951. && softmix_mixing_array_grow(&mixing_array, bridge->num_channels + 5)) {
  952. goto softmix_cleanup;
  953. }
  954. /* init the number of buffers stored in the mixing array to 0.
  955. * As buffers are added for mixing, this number is incremented. */
  956. mixing_array.used_entries = 0;
  957. /* These variables help determine if a rate change is required */
  958. if (!stat_iteration_counter) {
  959. memset(&stats, 0, sizeof(stats));
  960. stats.locked_rate = bridge->softmix.internal_sample_rate;
  961. }
  962. /* If the sample rate has changed, update the translator helper */
  963. if (update_all_rates) {
  964. softmix_translate_helper_change_rate(&trans_helper, softmix_data->internal_rate);
  965. }
  966. /* Go through pulling audio from each factory that has it available */
  967. AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
  968. struct softmix_channel *sc = bridge_channel->tech_pvt;
  969. if (!sc) {
  970. /* This channel failed to join successfully. */
  971. continue;
  972. }
  973. /* Update the sample rate to match the bridge's native sample rate if necessary. */
  974. if (update_all_rates) {
  975. set_softmix_bridge_data(softmix_data->internal_rate, softmix_data->internal_mixing_interval, bridge_channel, 1);
  976. }
  977. /* If stat_iteration_counter is 0, then collect statistics during this mixing interation */
  978. if (!stat_iteration_counter) {
  979. gather_softmix_stats(&stats, softmix_data, bridge_channel);
  980. }
  981. /* if the channel is suspended, don't check for audio, but still gather stats */
  982. if (bridge_channel->suspended) {
  983. continue;
  984. }
  985. /* Try to get audio from the factory if available */
  986. ast_mutex_lock(&sc->lock);
  987. if ((mixing_array.buffers[mixing_array.used_entries] = softmix_process_read_audio(sc, softmix_samples))) {
  988. mixing_array.used_entries++;
  989. }
  990. ast_mutex_unlock(&sc->lock);
  991. }
  992. /* mix it like crazy */
  993. memset(buf, 0, softmix_datalen);
  994. for (idx = 0; idx < mixing_array.used_entries; ++idx) {
  995. for (x = 0; x < softmix_samples; ++x) {
  996. ast_slinear_saturated_add(buf + x, mixing_array.buffers[idx] + x);
  997. }
  998. }
  999. /* Next step go through removing the channel's own audio and creating a good frame... */
  1000. AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
  1001. struct softmix_channel *sc = bridge_channel->tech_pvt;
  1002. if (!sc || bridge_channel->suspended) {
  1003. /* This channel failed to join successfully or is suspended. */
  1004. continue;
  1005. }
  1006. ast_mutex_lock(&sc->lock);
  1007. /* Make SLINEAR write frame from local buffer */
  1008. ao2_t_replace(sc->write_frame.subclass.format, cur_slin,
  1009. "Replace softmix channel slin format");
  1010. sc->write_frame.datalen = softmix_datalen;
  1011. sc->write_frame.samples = softmix_samples;
  1012. memcpy(sc->final_buf, buf, softmix_datalen);
  1013. /* process the softmix channel's new write audio */
  1014. softmix_process_write_audio(&trans_helper, ast_channel_rawwriteformat(bridge_channel->chan), sc);
  1015. ast_mutex_unlock(&sc->lock);
  1016. /* A frame is now ready for the channel. */
  1017. ast_bridge_channel_queue_frame(bridge_channel, &sc->write_frame);
  1018. }
  1019. update_all_rates = 0;
  1020. if (!stat_iteration_counter) {
  1021. update_all_rates = analyse_softmix_stats(&stats, softmix_data);
  1022. stat_iteration_counter = SOFTMIX_STAT_INTERVAL;
  1023. }
  1024. stat_iteration_counter--;
  1025. ast_bridge_unlock(bridge);
  1026. /* cleanup any translation frame data from the previous mixing iteration. */
  1027. softmix_translate_helper_cleanup(&trans_helper);
  1028. /* Wait for the timing source to tell us to wake up and get things done */
  1029. ast_waitfor_n_fd(&timingfd, 1, &timeout, NULL);
  1030. if (ast_timer_ack(timer, 1) < 0) {
  1031. ast_log(LOG_ERROR, "Bridge %s: Failed to acknowledge timer in softmix.\n",
  1032. bridge->uniqueid);
  1033. ast_bridge_lock(bridge);
  1034. goto softmix_cleanup;
  1035. }
  1036. ast_bridge_lock(bridge);
  1037. /* make sure to detect mixing interval changes if they occur. */
  1038. if (bridge->softmix.internal_mixing_interval
  1039. && (bridge->softmix.internal_mixing_interval != softmix_data->internal_mixing_interval)) {
  1040. softmix_data->internal_mixing_interval = bridge->softmix.internal_mixing_interval;
  1041. ast_timer_set_rate(timer, (1000 / softmix_data->internal_mixing_interval));
  1042. update_all_rates = 1; /* if the interval changes, the rates must be adjusted as well just to be notified new interval.*/
  1043. }
  1044. }
  1045. res = 0;
  1046. softmix_cleanup:
  1047. softmix_translate_helper_destroy(&trans_helper);
  1048. softmix_mixing_array_destroy(&mixing_array);
  1049. return res;
  1050. }
  1051. /*!
  1052. * \internal
  1053. * \brief Mixing thread.
  1054. * \since 12.0.0
  1055. *
  1056. * \note The thread does not have its own reference to the
  1057. * bridge. The lifetime of the thread is tied to the lifetime
  1058. * of the mixing technology association with the bridge.
  1059. */
  1060. static void *softmix_mixing_thread(void *data)
  1061. {
  1062. struct softmix_bridge_data *softmix_data = data;
  1063. struct ast_bridge *bridge = softmix_data->bridge;
  1064. ast_bridge_lock(bridge);
  1065. if (bridge->callid) {
  1066. ast_callid_threadassoc_add(bridge->callid);
  1067. }
  1068. ast_debug(1, "Bridge %s: starting mixing thread\n", bridge->uniqueid);
  1069. while (!softmix_data->stop) {
  1070. if (!bridge->num_active) {
  1071. /* Wait for something to happen to the bridge. */
  1072. ast_bridge_unlock(bridge);
  1073. ast_mutex_lock(&softmix_data->lock);
  1074. if (!softmix_data->stop) {
  1075. ast_cond_wait(&softmix_data->cond, &softmix_data->lock);
  1076. }
  1077. ast_mutex_unlock(&softmix_data->lock);
  1078. ast_bridge_lock(bridge);
  1079. continue;
  1080. }
  1081. if (softmix_mixing_loop(bridge)) {
  1082. /*
  1083. * A mixing error occurred. Sleep and try again later so we
  1084. * won't flood the logs.
  1085. */
  1086. ast_bridge_unlock(bridge);
  1087. sleep(1);
  1088. ast_bridge_lock(bridge);
  1089. }
  1090. }
  1091. ast_bridge_unlock(bridge);
  1092. ast_debug(1, "Bridge %s: stopping mixing thread\n", bridge->uniqueid);
  1093. return NULL;
  1094. }
  1095. static void softmix_bridge_data_destroy(struct softmix_bridge_data *softmix_data)
  1096. {
  1097. if (softmix_data->timer) {
  1098. ast_timer_close(softmix_data->timer);
  1099. softmix_data->timer = NULL;
  1100. }
  1101. ast_mutex_destroy(&softmix_data->lock);
  1102. ast_cond_destroy(&softmix_data->cond);
  1103. ast_free(softmix_data);
  1104. }
  1105. /*! \brief Function called when a bridge is created */
  1106. static int softmix_bridge_create(struct ast_bridge *bridge)
  1107. {
  1108. struct softmix_bridge_data *softmix_data;
  1109. softmix_data = ast_calloc(1, sizeof(*softmix_data));
  1110. if (!softmix_data) {
  1111. return -1;
  1112. }
  1113. softmix_data->bridge = bridge;
  1114. ast_mutex_init(&softmix_data->lock);
  1115. ast_cond_init(&softmix_data->cond, NULL);
  1116. softmix_data->timer = ast_timer_open();
  1117. if (!softmix_data->timer) {
  1118. ast_log(AST_LOG_WARNING, "Failed to open timer for softmix bridge\n");
  1119. softmix_bridge_data_destroy(softmix_data);
  1120. return -1;
  1121. }
  1122. /* start at minimum rate, let it grow from there */
  1123. softmix_data->internal_rate = SOFTMIX_MIN_SAMPLE_RATE;
  1124. softmix_data->internal_mixing_interval = DEFAULT_SOFTMIX_INTERVAL;
  1125. bridge->tech_pvt = softmix_data;
  1126. /* Start the mixing thread. */
  1127. if (ast_pthread_create(&softmix_data->thread, NULL, softmix_mixing_thread,
  1128. softmix_data)) {
  1129. softmix_data->thread = AST_PTHREADT_NULL;
  1130. softmix_bridge_data_destroy(softmix_data);
  1131. bridge->tech_pvt = NULL;
  1132. return -1;
  1133. }
  1134. return 0;
  1135. }
  1136. /*!
  1137. * \internal
  1138. * \brief Request the softmix mixing thread stop.
  1139. * \since 12.0.0
  1140. *
  1141. * \param bridge Which bridge is being stopped.
  1142. *
  1143. * \return Nothing
  1144. */
  1145. static void softmix_bridge_stop(struct ast_bridge *bridge)
  1146. {
  1147. struct softmix_bridge_data *softmix_data;
  1148. softmix_data = bridge->tech_pvt;
  1149. if (!softmix_data) {
  1150. return;
  1151. }
  1152. ast_mutex_lock(&softmix_data->lock);
  1153. softmix_data->stop = 1;
  1154. ast_mutex_unlock(&softmix_data->lock);
  1155. }
  1156. /*! \brief Function called when a bridge is destroyed */
  1157. static void softmix_bridge_destroy(struct ast_bridge *bridge)
  1158. {
  1159. struct softmix_bridge_data *softmix_data;
  1160. pthread_t thread;
  1161. softmix_data = bridge->tech_pvt;
  1162. if (!softmix_data) {
  1163. return;
  1164. }
  1165. /* Stop the mixing thread. */
  1166. ast_mutex_lock(&softmix_data->lock);
  1167. softmix_data->stop = 1;
  1168. ast_cond_signal(&softmix_data->cond);
  1169. thread = softmix_data->thread;
  1170. softmix_data->thread = AST_PTHREADT_NULL;
  1171. ast_mutex_unlock(&softmix_data->lock);
  1172. if (thread != AST_PTHREADT_NULL) {
  1173. ast_debug(1, "Bridge %s: Waiting for mixing thread to die.\n", bridge->uniqueid);
  1174. pthread_join(thread, NULL);
  1175. }
  1176. softmix_bridge_data_destroy(softmix_data);
  1177. bridge->tech_pvt = NULL;
  1178. }
  1179. static struct ast_bridge_technology softmix_bridge = {
  1180. .name = "softmix",
  1181. .capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX,
  1182. .preference = AST_BRIDGE_PREFERENCE_BASE_MULTIMIX,
  1183. .create = softmix_bridge_create,
  1184. .stop = softmix_bridge_stop,
  1185. .destroy = softmix_bridge_destroy,
  1186. .join = softmix_bridge_join,
  1187. .leave = softmix_bridge_leave,
  1188. .unsuspend = softmix_bridge_unsuspend,
  1189. .write = softmix_bridge_write,
  1190. };
  1191. static int unload_module(void)
  1192. {
  1193. ast_bridge_technology_unregister(&softmix_bridge);
  1194. return 0;
  1195. }
  1196. static int load_module(void)
  1197. {
  1198. if (ast_bridge_technology_register(&softmix_bridge)) {
  1199. unload_module();
  1200. return AST_MODULE_LOAD_DECLINE;
  1201. }
  1202. return AST_MODULE_LOAD_SUCCESS;
  1203. }
  1204. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Multi-party software based channel mixing");