bridge_softmix.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  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. #define MAX_DATALEN 8096
  52. /*! \brief Interval at which mixing will take place. Valid options are 10, 20, and 40. */
  53. #define DEFAULT_SOFTMIX_INTERVAL 20
  54. /*! \brief Size of the buffer used for sample manipulation */
  55. #define SOFTMIX_DATALEN(rate, interval) ((rate/50) * (interval / 10))
  56. /*! \brief Number of samples we are dealing with */
  57. #define SOFTMIX_SAMPLES(rate, interval) (SOFTMIX_DATALEN(rate, interval) / 2)
  58. /*! \brief Number of mixing iterations to perform between gathering statistics. */
  59. #define SOFTMIX_STAT_INTERVAL 100
  60. /* This is the threshold in ms at which a channel's own audio will stop getting
  61. * mixed out its own write audio stream because it is not talking. */
  62. #define DEFAULT_SOFTMIX_SILENCE_THRESHOLD 2500
  63. #define DEFAULT_SOFTMIX_TALKING_THRESHOLD 160
  64. #define DEFAULT_ENERGY_HISTORY_LEN 150
  65. struct video_follow_talker_data {
  66. /*! audio energy history */
  67. int energy_history[DEFAULT_ENERGY_HISTORY_LEN];
  68. /*! The current slot being used in the history buffer, this
  69. * increments and wraps around */
  70. int energy_history_cur_slot;
  71. /*! The current energy sum used for averages. */
  72. int energy_accum;
  73. /*! The current energy average */
  74. int energy_average;
  75. };
  76. /*! \brief Structure which contains per-channel mixing information */
  77. struct softmix_channel {
  78. /*! Lock to protect this structure */
  79. ast_mutex_t lock;
  80. /*! Factory which contains audio read in from the channel */
  81. struct ast_slinfactory factory;
  82. /*! Frame that contains mixed audio to be written out to the channel */
  83. struct ast_frame write_frame;
  84. /*! Frame that contains mixed audio read from the channel */
  85. struct ast_frame read_frame;
  86. /*! DSP for detecting silence */
  87. struct ast_dsp *dsp;
  88. /*!
  89. * \brief TRUE if a channel is talking.
  90. *
  91. * \note This affects how the channel's audio is mixed back to
  92. * it.
  93. */
  94. unsigned int talking:1;
  95. /*! TRUE if the channel provided audio for this mixing interval */
  96. unsigned int have_audio:1;
  97. /*! Buffer containing final mixed audio from all sources */
  98. short final_buf[MAX_DATALEN];
  99. /*! Buffer containing only the audio from the channel */
  100. short our_buf[MAX_DATALEN];
  101. /*! Data pertaining to talker mode for video conferencing */
  102. struct video_follow_talker_data video_talker;
  103. };
  104. struct softmix_bridge_data {
  105. struct ast_timer *timer;
  106. /*!
  107. * \brief Bridge pointer passed to the softmix mixing thread.
  108. *
  109. * \note Does not need a reference because the bridge will
  110. * always exist while the mixing thread exists even if the
  111. * bridge is no longer actively using the softmix technology.
  112. */
  113. struct ast_bridge *bridge;
  114. /*! Lock for signaling the mixing thread. */
  115. ast_mutex_t lock;
  116. /*! Condition, used if we need to wake up the mixing thread. */
  117. ast_cond_t cond;
  118. /*! Thread handling the mixing */
  119. pthread_t thread;
  120. unsigned int internal_rate;
  121. unsigned int internal_mixing_interval;
  122. /*! TRUE if the mixing thread should stop */
  123. unsigned int stop:1;
  124. };
  125. struct softmix_stats {
  126. /*! Each index represents a sample rate used above the internal rate. */
  127. unsigned int sample_rates[16];
  128. /*! Each index represents the number of channels using the same index in the sample_rates array. */
  129. unsigned int num_channels[16];
  130. /*! the number of channels above the internal sample rate */
  131. unsigned int num_above_internal_rate;
  132. /*! the number of channels at the internal sample rate */
  133. unsigned int num_at_internal_rate;
  134. /*! the absolute highest sample rate supported by any channel in the bridge */
  135. unsigned int highest_supported_rate;
  136. /*! Is the sample rate locked by the bridge, if so what is that rate.*/
  137. unsigned int locked_rate;
  138. };
  139. struct softmix_mixing_array {
  140. unsigned int max_num_entries;
  141. unsigned int used_entries;
  142. int16_t **buffers;
  143. };
  144. struct softmix_translate_helper_entry {
  145. int num_times_requested; /*!< Once this entry is no longer requested, free the trans_pvt
  146. and re-init if it was usable. */
  147. struct ast_format dst_format; /*!< The destination format for this helper */
  148. struct ast_trans_pvt *trans_pvt; /*!< the translator for this slot. */
  149. struct ast_frame *out_frame; /*!< The output frame from the last translation */
  150. AST_LIST_ENTRY(softmix_translate_helper_entry) entry;
  151. };
  152. struct softmix_translate_helper {
  153. struct ast_format slin_src; /*!< the source format expected for all the translators */
  154. AST_LIST_HEAD_NOLOCK(, softmix_translate_helper_entry) entries;
  155. };
  156. static struct softmix_translate_helper_entry *softmix_translate_helper_entry_alloc(struct ast_format *dst)
  157. {
  158. struct softmix_translate_helper_entry *entry;
  159. if (!(entry = ast_calloc(1, sizeof(*entry)))) {
  160. return NULL;
  161. }
  162. ast_format_copy(&entry->dst_format, dst);
  163. return entry;
  164. }
  165. static void *softmix_translate_helper_free_entry(struct softmix_translate_helper_entry *entry)
  166. {
  167. if (entry->trans_pvt) {
  168. ast_translator_free_path(entry->trans_pvt);
  169. }
  170. if (entry->out_frame) {
  171. ast_frfree(entry->out_frame);
  172. }
  173. ast_free(entry);
  174. return NULL;
  175. }
  176. static void softmix_translate_helper_init(struct softmix_translate_helper *trans_helper, unsigned int sample_rate)
  177. {
  178. memset(trans_helper, 0, sizeof(*trans_helper));
  179. ast_format_set(&trans_helper->slin_src, ast_format_slin_by_rate(sample_rate), 0);
  180. }
  181. static void softmix_translate_helper_destroy(struct softmix_translate_helper *trans_helper)
  182. {
  183. struct softmix_translate_helper_entry *entry;
  184. while ((entry = AST_LIST_REMOVE_HEAD(&trans_helper->entries, entry))) {
  185. softmix_translate_helper_free_entry(entry);
  186. }
  187. }
  188. static void softmix_translate_helper_change_rate(struct softmix_translate_helper *trans_helper, unsigned int sample_rate)
  189. {
  190. struct softmix_translate_helper_entry *entry;
  191. ast_format_set(&trans_helper->slin_src, ast_format_slin_by_rate(sample_rate), 0);
  192. AST_LIST_TRAVERSE_SAFE_BEGIN(&trans_helper->entries, entry, entry) {
  193. if (entry->trans_pvt) {
  194. ast_translator_free_path(entry->trans_pvt);
  195. if (!(entry->trans_pvt = ast_translator_build_path(&entry->dst_format, &trans_helper->slin_src))) {
  196. AST_LIST_REMOVE_CURRENT(entry);
  197. entry = softmix_translate_helper_free_entry(entry);
  198. }
  199. }
  200. }
  201. AST_LIST_TRAVERSE_SAFE_END;
  202. }
  203. /*!
  204. * \internal
  205. * \brief Get the next available audio on the softmix channel's read stream
  206. * and determine if it should be mixed out or not on the write stream.
  207. *
  208. * \retval pointer to buffer containing the exact number of samples requested on success.
  209. * \retval NULL if no samples are present
  210. */
  211. static int16_t *softmix_process_read_audio(struct softmix_channel *sc, unsigned int num_samples)
  212. {
  213. if ((ast_slinfactory_available(&sc->factory) >= num_samples) &&
  214. ast_slinfactory_read(&sc->factory, sc->our_buf, num_samples)) {
  215. sc->have_audio = 1;
  216. return sc->our_buf;
  217. }
  218. sc->have_audio = 0;
  219. return NULL;
  220. }
  221. /*!
  222. * \internal
  223. * \brief Process a softmix channel's write audio
  224. *
  225. * \details This function will remove the channel's talking from its own audio if present and
  226. * possibly even do the channel's write translation for it depending on how many other
  227. * channels use the same write format.
  228. */
  229. static void softmix_process_write_audio(struct softmix_translate_helper *trans_helper,
  230. struct ast_format *raw_write_fmt,
  231. struct softmix_channel *sc)
  232. {
  233. struct softmix_translate_helper_entry *entry = NULL;
  234. int i;
  235. /* If we provided audio that was not determined to be silence,
  236. * then take it out while in slinear format. */
  237. if (sc->have_audio && sc->talking) {
  238. for (i = 0; i < sc->write_frame.samples; i++) {
  239. ast_slinear_saturated_subtract(&sc->final_buf[i], &sc->our_buf[i]);
  240. }
  241. /* do not do any special write translate optimization if we had to make
  242. * a special mix for them to remove their own audio. */
  243. return;
  244. }
  245. AST_LIST_TRAVERSE(&trans_helper->entries, entry, entry) {
  246. if (ast_format_cmp(&entry->dst_format, raw_write_fmt) == AST_FORMAT_CMP_EQUAL) {
  247. entry->num_times_requested++;
  248. } else {
  249. continue;
  250. }
  251. if (!entry->trans_pvt && (entry->num_times_requested > 1)) {
  252. entry->trans_pvt = ast_translator_build_path(&entry->dst_format, &trans_helper->slin_src);
  253. }
  254. if (entry->trans_pvt && !entry->out_frame) {
  255. entry->out_frame = ast_translate(entry->trans_pvt, &sc->write_frame, 0);
  256. }
  257. if (entry->out_frame && (entry->out_frame->datalen < MAX_DATALEN)) {
  258. ast_format_copy(&sc->write_frame.subclass.format, &entry->out_frame->subclass.format);
  259. memcpy(sc->final_buf, entry->out_frame->data.ptr, entry->out_frame->datalen);
  260. sc->write_frame.datalen = entry->out_frame->datalen;
  261. sc->write_frame.samples = entry->out_frame->samples;
  262. }
  263. break;
  264. }
  265. /* add new entry into list if this format destination was not matched. */
  266. if (!entry && (entry = softmix_translate_helper_entry_alloc(raw_write_fmt))) {
  267. AST_LIST_INSERT_HEAD(&trans_helper->entries, entry, entry);
  268. }
  269. }
  270. static void softmix_translate_helper_cleanup(struct softmix_translate_helper *trans_helper)
  271. {
  272. struct softmix_translate_helper_entry *entry;
  273. AST_LIST_TRAVERSE(&trans_helper->entries, entry, entry) {
  274. if (entry->out_frame) {
  275. ast_frfree(entry->out_frame);
  276. entry->out_frame = NULL;
  277. }
  278. entry->num_times_requested = 0;
  279. }
  280. }
  281. static void set_softmix_bridge_data(int rate, int interval, struct ast_bridge_channel *bridge_channel, int reset)
  282. {
  283. struct softmix_channel *sc = bridge_channel->tech_pvt;
  284. unsigned int channel_read_rate = ast_format_rate(ast_channel_rawreadformat(bridge_channel->chan));
  285. ast_mutex_lock(&sc->lock);
  286. if (reset) {
  287. ast_slinfactory_destroy(&sc->factory);
  288. ast_dsp_free(sc->dsp);
  289. }
  290. /* Setup read/write frame parameters */
  291. sc->write_frame.frametype = AST_FRAME_VOICE;
  292. ast_format_set(&sc->write_frame.subclass.format, ast_format_slin_by_rate(rate), 0);
  293. sc->write_frame.data.ptr = sc->final_buf;
  294. sc->write_frame.datalen = SOFTMIX_DATALEN(rate, interval);
  295. sc->write_frame.samples = SOFTMIX_SAMPLES(rate, interval);
  296. sc->read_frame.frametype = AST_FRAME_VOICE;
  297. ast_format_set(&sc->read_frame.subclass.format, ast_format_slin_by_rate(channel_read_rate), 0);
  298. sc->read_frame.data.ptr = sc->our_buf;
  299. sc->read_frame.datalen = SOFTMIX_DATALEN(channel_read_rate, interval);
  300. sc->read_frame.samples = SOFTMIX_SAMPLES(channel_read_rate, interval);
  301. /* Setup smoother */
  302. ast_slinfactory_init_with_format(&sc->factory, &sc->write_frame.subclass.format);
  303. /* set new read and write formats on channel. */
  304. ast_set_read_format(bridge_channel->chan, &sc->read_frame.subclass.format);
  305. ast_set_write_format(bridge_channel->chan, &sc->write_frame.subclass.format);
  306. /* set up new DSP. This is on the read side only right before the read frame enters the smoother. */
  307. sc->dsp = ast_dsp_new_with_rate(channel_read_rate);
  308. /* we want to aggressively detect silence to avoid feedback */
  309. if (bridge_channel->tech_args.talking_threshold) {
  310. ast_dsp_set_threshold(sc->dsp, bridge_channel->tech_args.talking_threshold);
  311. } else {
  312. ast_dsp_set_threshold(sc->dsp, DEFAULT_SOFTMIX_TALKING_THRESHOLD);
  313. }
  314. ast_mutex_unlock(&sc->lock);
  315. }
  316. /*!
  317. * \internal
  318. * \brief Poke the mixing thread in case it is waiting for an active channel.
  319. * \since 12.0.0
  320. *
  321. * \param softmix_data Bridge mixing data.
  322. *
  323. * \return Nothing
  324. */
  325. static void softmix_poke_thread(struct softmix_bridge_data *softmix_data)
  326. {
  327. ast_mutex_lock(&softmix_data->lock);
  328. ast_cond_signal(&softmix_data->cond);
  329. ast_mutex_unlock(&softmix_data->lock);
  330. }
  331. /*! \brief Function called when a channel is unsuspended from the bridge */
  332. static void softmix_bridge_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  333. {
  334. if (bridge->tech_pvt) {
  335. softmix_poke_thread(bridge->tech_pvt);
  336. }
  337. }
  338. /*!
  339. * \internal
  340. * \brief Indicate a source change to the channel.
  341. * \since 12.0.0
  342. *
  343. * \param bridge_channel Which channel source is changing.
  344. *
  345. * \retval 0 on success.
  346. * \retval -1 on error.
  347. */
  348. static int softmix_src_change(struct ast_bridge_channel *bridge_channel)
  349. {
  350. return ast_bridge_channel_queue_control_data(bridge_channel, AST_CONTROL_SRCCHANGE, NULL, 0);
  351. }
  352. /*! \brief Function called when a channel is joined into the bridge */
  353. static int softmix_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  354. {
  355. struct softmix_channel *sc;
  356. struct softmix_bridge_data *softmix_data;
  357. softmix_data = bridge->tech_pvt;
  358. if (!softmix_data) {
  359. return -1;
  360. }
  361. /* Create a new softmix_channel structure and allocate various things on it */
  362. if (!(sc = ast_calloc(1, sizeof(*sc)))) {
  363. return -1;
  364. }
  365. softmix_src_change(bridge_channel);
  366. /* Can't forget the lock */
  367. ast_mutex_init(&sc->lock);
  368. /* Can't forget to record our pvt structure within the bridged channel structure */
  369. bridge_channel->tech_pvt = sc;
  370. set_softmix_bridge_data(softmix_data->internal_rate,
  371. softmix_data->internal_mixing_interval
  372. ? softmix_data->internal_mixing_interval
  373. : DEFAULT_SOFTMIX_INTERVAL,
  374. bridge_channel, 0);
  375. softmix_poke_thread(softmix_data);
  376. return 0;
  377. }
  378. /*! \brief Function called when a channel leaves the bridge */
  379. static void softmix_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  380. {
  381. struct softmix_channel *sc = bridge_channel->tech_pvt;
  382. if (!sc) {
  383. return;
  384. }
  385. bridge_channel->tech_pvt = NULL;
  386. softmix_src_change(bridge_channel);
  387. /* Drop mutex lock */
  388. ast_mutex_destroy(&sc->lock);
  389. /* Drop the factory */
  390. ast_slinfactory_destroy(&sc->factory);
  391. /* Drop the DSP */
  392. ast_dsp_free(sc->dsp);
  393. /* Eep! drop ourselves */
  394. ast_free(sc);
  395. }
  396. static void softmix_pass_video_top_priority(struct ast_bridge *bridge, struct ast_frame *frame)
  397. {
  398. struct ast_bridge_channel *cur;
  399. AST_LIST_TRAVERSE(&bridge->channels, cur, entry) {
  400. if (cur->suspended) {
  401. continue;
  402. }
  403. if (ast_bridge_is_video_src(bridge, cur->chan) == 1) {
  404. ast_bridge_channel_queue_frame(cur, frame);
  405. break;
  406. }
  407. }
  408. }
  409. /*!
  410. * \internal
  411. * \brief Determine what to do with a video frame.
  412. * \since 12.0.0
  413. *
  414. * \param bridge Which bridge is getting the frame
  415. * \param bridge_channel Which channel is writing the frame.
  416. * \param frame What is being written.
  417. *
  418. * \return Nothing
  419. */
  420. static void softmix_bridge_write_video(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
  421. {
  422. struct softmix_channel *sc;
  423. int video_src_priority;
  424. /* Determine if the video frame should be distributed or not */
  425. switch (bridge->softmix.video_mode.mode) {
  426. case AST_BRIDGE_VIDEO_MODE_NONE:
  427. break;
  428. case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
  429. video_src_priority = ast_bridge_is_video_src(bridge, bridge_channel->chan);
  430. if (video_src_priority == 1) {
  431. /* Pass to me and everyone else. */
  432. ast_bridge_queue_everyone_else(bridge, NULL, frame);
  433. }
  434. break;
  435. case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
  436. sc = bridge_channel->tech_pvt;
  437. ast_mutex_lock(&sc->lock);
  438. ast_bridge_update_talker_src_video_mode(bridge, bridge_channel->chan,
  439. sc->video_talker.energy_average,
  440. ast_format_get_video_mark(&frame->subclass.format));
  441. ast_mutex_unlock(&sc->lock);
  442. video_src_priority = ast_bridge_is_video_src(bridge, bridge_channel->chan);
  443. if (video_src_priority == 1) {
  444. int num_src = ast_bridge_number_video_src(bridge);
  445. int echo = num_src > 1 ? 0 : 1;
  446. ast_bridge_queue_everyone_else(bridge, echo ? NULL : bridge_channel, frame);
  447. } else if (video_src_priority == 2) {
  448. softmix_pass_video_top_priority(bridge, frame);
  449. }
  450. break;
  451. }
  452. }
  453. /*!
  454. * \internal
  455. * \brief Determine what to do with a voice frame.
  456. * \since 12.0.0
  457. *
  458. * \param bridge Which bridge is getting the frame
  459. * \param bridge_channel Which channel is writing the frame.
  460. * \param frame What is being written.
  461. *
  462. * \return Nothing
  463. */
  464. static void softmix_bridge_write_voice(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
  465. {
  466. struct softmix_channel *sc = bridge_channel->tech_pvt;
  467. struct softmix_bridge_data *softmix_data = bridge->tech_pvt;
  468. int totalsilence = 0;
  469. int cur_energy = 0;
  470. int silence_threshold = bridge_channel->tech_args.silence_threshold ?
  471. bridge_channel->tech_args.silence_threshold :
  472. DEFAULT_SOFTMIX_SILENCE_THRESHOLD;
  473. char update_talking = -1; /* if this is set to 0 or 1, tell the bridge that the channel has started or stopped talking. */
  474. /* Write the frame into the conference */
  475. ast_mutex_lock(&sc->lock);
  476. ast_dsp_silence_with_energy(sc->dsp, frame, &totalsilence, &cur_energy);
  477. if (bridge->softmix.video_mode.mode == AST_BRIDGE_VIDEO_MODE_TALKER_SRC) {
  478. int cur_slot = sc->video_talker.energy_history_cur_slot;
  479. sc->video_talker.energy_accum -= sc->video_talker.energy_history[cur_slot];
  480. sc->video_talker.energy_accum += cur_energy;
  481. sc->video_talker.energy_history[cur_slot] = cur_energy;
  482. sc->video_talker.energy_average = sc->video_talker.energy_accum / DEFAULT_ENERGY_HISTORY_LEN;
  483. sc->video_talker.energy_history_cur_slot++;
  484. if (sc->video_talker.energy_history_cur_slot == DEFAULT_ENERGY_HISTORY_LEN) {
  485. sc->video_talker.energy_history_cur_slot = 0; /* wrap around */
  486. }
  487. }
  488. if (totalsilence < silence_threshold) {
  489. if (!sc->talking) {
  490. update_talking = 1;
  491. }
  492. sc->talking = 1; /* tell the write process we have audio to be mixed out */
  493. } else {
  494. if (sc->talking) {
  495. update_talking = 0;
  496. }
  497. sc->talking = 0;
  498. }
  499. /* Before adding audio in, make sure we haven't fallen behind. If audio has fallen
  500. * behind 4 times the amount of samples mixed on every iteration of the mixer, Re-sync
  501. * the audio by flushing the buffer before adding new audio in. */
  502. if (ast_slinfactory_available(&sc->factory) > (4 * SOFTMIX_SAMPLES(softmix_data->internal_rate, softmix_data->internal_mixing_interval))) {
  503. ast_slinfactory_flush(&sc->factory);
  504. }
  505. /* If a frame was provided add it to the smoother, unless drop silence is enabled and this frame
  506. * is not determined to be talking. */
  507. if (!(bridge_channel->tech_args.drop_silence && !sc->talking) &&
  508. (frame->frametype == AST_FRAME_VOICE && ast_format_is_slinear(&frame->subclass.format))) {
  509. ast_slinfactory_feed(&sc->factory, frame);
  510. }
  511. /* Alllll done */
  512. ast_mutex_unlock(&sc->lock);
  513. if (update_talking != -1) {
  514. ast_bridge_channel_notify_talking(bridge_channel, update_talking);
  515. }
  516. }
  517. /*!
  518. * \internal
  519. * \brief Determine what to do with a control frame.
  520. * \since 12.0.0
  521. *
  522. * \param bridge Which bridge is getting the frame
  523. * \param bridge_channel Which channel is writing the frame.
  524. * \param frame What is being written.
  525. *
  526. * \retval 0 Frame accepted into the bridge.
  527. * \retval -1 Frame needs to be deferred.
  528. */
  529. static int softmix_bridge_write_control(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
  530. {
  531. /*
  532. * XXX Softmix needs to use channel roles to determine what to
  533. * do with control frames.
  534. */
  535. return 0;
  536. }
  537. /*!
  538. * \internal
  539. * \brief Determine what to do with a frame written into the bridge.
  540. * \since 12.0.0
  541. *
  542. * \param bridge Which bridge is getting the frame
  543. * \param bridge_channel Which channel is writing the frame.
  544. * \param frame What is being written.
  545. *
  546. * \retval 0 Frame accepted into the bridge.
  547. * \retval -1 Frame needs to be deferred.
  548. *
  549. * \note On entry, bridge is already locked.
  550. */
  551. static int softmix_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
  552. {
  553. int res = 0;
  554. if (!bridge->tech_pvt || (bridge_channel && !bridge_channel->tech_pvt)) {
  555. /* "Accept" the frame and discard it. */
  556. return 0;
  557. }
  558. /*
  559. * XXX Softmix needs to use channel roles to determine who gets
  560. * what frame. Possible roles: announcer, recorder, agent,
  561. * supervisor.
  562. */
  563. switch (frame->frametype) {
  564. case AST_FRAME_NULL:
  565. /* "Accept" the frame and discard it. */
  566. break;
  567. case AST_FRAME_DTMF_BEGIN:
  568. case AST_FRAME_DTMF_END:
  569. res = ast_bridge_queue_everyone_else(bridge, bridge_channel, frame);
  570. break;
  571. case AST_FRAME_VOICE:
  572. if (bridge_channel) {
  573. softmix_bridge_write_voice(bridge, bridge_channel, frame);
  574. }
  575. break;
  576. case AST_FRAME_VIDEO:
  577. if (bridge_channel) {
  578. softmix_bridge_write_video(bridge, bridge_channel, frame);
  579. }
  580. break;
  581. case AST_FRAME_CONTROL:
  582. res = softmix_bridge_write_control(bridge, bridge_channel, frame);
  583. break;
  584. case AST_FRAME_BRIDGE_ACTION:
  585. res = ast_bridge_queue_everyone_else(bridge, bridge_channel, frame);
  586. break;
  587. case AST_FRAME_BRIDGE_ACTION_SYNC:
  588. ast_log(LOG_ERROR, "Synchronous bridge action written to a softmix bridge.\n");
  589. ast_assert(0);
  590. default:
  591. ast_debug(3, "Frame type %u unsupported\n", frame->frametype);
  592. /* "Accept" the frame and discard it. */
  593. break;
  594. }
  595. return res;
  596. }
  597. static void gather_softmix_stats(struct softmix_stats *stats,
  598. const struct softmix_bridge_data *softmix_data,
  599. struct ast_bridge_channel *bridge_channel)
  600. {
  601. int channel_native_rate;
  602. int i;
  603. /* Gather stats about channel sample rates. */
  604. channel_native_rate = MAX(ast_format_rate(ast_channel_rawwriteformat(bridge_channel->chan)),
  605. ast_format_rate(ast_channel_rawreadformat(bridge_channel->chan)));
  606. if (channel_native_rate > stats->highest_supported_rate) {
  607. stats->highest_supported_rate = channel_native_rate;
  608. }
  609. if (channel_native_rate > softmix_data->internal_rate) {
  610. for (i = 0; i < ARRAY_LEN(stats->sample_rates); i++) {
  611. if (stats->sample_rates[i] == channel_native_rate) {
  612. stats->num_channels[i]++;
  613. break;
  614. } else if (!stats->sample_rates[i]) {
  615. stats->sample_rates[i] = channel_native_rate;
  616. stats->num_channels[i]++;
  617. break;
  618. }
  619. }
  620. stats->num_above_internal_rate++;
  621. } else if (channel_native_rate == softmix_data->internal_rate) {
  622. stats->num_at_internal_rate++;
  623. }
  624. }
  625. /*!
  626. * \internal
  627. * \brief Analyse mixing statistics and change bridges internal rate
  628. * if necessary.
  629. *
  630. * \retval 0, no changes to internal rate
  631. * \ratval 1, internal rate was changed, update all the channels on the next mixing iteration.
  632. */
  633. static unsigned int analyse_softmix_stats(struct softmix_stats *stats, struct softmix_bridge_data *softmix_data)
  634. {
  635. int i;
  636. /* Re-adjust the internal bridge sample rate if
  637. * 1. The bridge's internal sample rate is locked in at a sample
  638. * rate other than the current sample rate being used.
  639. * 2. two or more channels support a higher sample rate
  640. * 3. no channels support the current sample rate or a higher rate
  641. */
  642. if (stats->locked_rate) {
  643. /* if the rate is locked by the bridge, only update it if it differs
  644. * from the current rate we are using. */
  645. if (softmix_data->internal_rate != stats->locked_rate) {
  646. softmix_data->internal_rate = stats->locked_rate;
  647. ast_debug(1, "Bridge is locked in at sample rate %u\n",
  648. softmix_data->internal_rate);
  649. return 1;
  650. }
  651. } else if (stats->num_above_internal_rate >= 2) {
  652. /* the highest rate is just used as a starting point */
  653. unsigned int best_rate = stats->highest_supported_rate;
  654. int best_index = -1;
  655. for (i = 0; i < ARRAY_LEN(stats->num_channels); i++) {
  656. if (stats->num_channels[i]) {
  657. break;
  658. }
  659. /* best_rate starts out being the first sample rate
  660. * greater than the internal sample rate that 2 or
  661. * more channels support. */
  662. if (stats->num_channels[i] >= 2 && (best_index == -1)) {
  663. best_rate = stats->sample_rates[i];
  664. best_index = i;
  665. /* If it has been detected that multiple rates above
  666. * the internal rate are present, compare those rates
  667. * to each other and pick the highest one two or more
  668. * channels support. */
  669. } else if (((best_index != -1) &&
  670. (stats->num_channels[i] >= 2) &&
  671. (stats->sample_rates[best_index] < stats->sample_rates[i]))) {
  672. best_rate = stats->sample_rates[i];
  673. best_index = i;
  674. /* It is possible that multiple channels exist with native sample
  675. * rates above the internal sample rate, but none of those channels
  676. * have the same rate in common. In this case, the lowest sample
  677. * rate among those channels is picked. Over time as additional
  678. * statistic runs are made the internal sample rate number will
  679. * adjust to the most optimal sample rate, but it may take multiple
  680. * iterations. */
  681. } else if (best_index == -1) {
  682. best_rate = MIN(best_rate, stats->sample_rates[i]);
  683. }
  684. }
  685. ast_debug(1, "Bridge changed from %u To %u\n",
  686. softmix_data->internal_rate, best_rate);
  687. softmix_data->internal_rate = best_rate;
  688. return 1;
  689. } else if (!stats->num_at_internal_rate && !stats->num_above_internal_rate) {
  690. /* In this case, the highest supported rate is actually lower than the internal rate */
  691. softmix_data->internal_rate = stats->highest_supported_rate;
  692. ast_debug(1, "Bridge changed from %u to %u\n",
  693. softmix_data->internal_rate, stats->highest_supported_rate);
  694. return 1;
  695. }
  696. return 0;
  697. }
  698. static int softmix_mixing_array_init(struct softmix_mixing_array *mixing_array, unsigned int starting_num_entries)
  699. {
  700. memset(mixing_array, 0, sizeof(*mixing_array));
  701. mixing_array->max_num_entries = starting_num_entries;
  702. if (!(mixing_array->buffers = ast_calloc(mixing_array->max_num_entries, sizeof(int16_t *)))) {
  703. ast_log(LOG_NOTICE, "Failed to allocate softmix mixing structure.\n");
  704. return -1;
  705. }
  706. return 0;
  707. }
  708. static void softmix_mixing_array_destroy(struct softmix_mixing_array *mixing_array)
  709. {
  710. ast_free(mixing_array->buffers);
  711. }
  712. static int softmix_mixing_array_grow(struct softmix_mixing_array *mixing_array, unsigned int num_entries)
  713. {
  714. int16_t **tmp;
  715. /* give it some room to grow since memory is cheap but allocations can be expensive */
  716. mixing_array->max_num_entries = num_entries;
  717. if (!(tmp = ast_realloc(mixing_array->buffers, (mixing_array->max_num_entries * sizeof(int16_t *))))) {
  718. ast_log(LOG_NOTICE, "Failed to re-allocate softmix mixing structure.\n");
  719. return -1;
  720. }
  721. mixing_array->buffers = tmp;
  722. return 0;
  723. }
  724. /*!
  725. * \brief Mixing loop.
  726. *
  727. * \retval 0 on success
  728. * \retval -1 on failure
  729. */
  730. static int softmix_mixing_loop(struct ast_bridge *bridge)
  731. {
  732. struct softmix_stats stats = { { 0 }, };
  733. struct softmix_mixing_array mixing_array;
  734. struct softmix_bridge_data *softmix_data = bridge->tech_pvt;
  735. struct ast_timer *timer;
  736. struct softmix_translate_helper trans_helper;
  737. int16_t buf[MAX_DATALEN];
  738. unsigned int stat_iteration_counter = 0; /* counts down, gather stats at zero and reset. */
  739. int timingfd;
  740. int update_all_rates = 0; /* set this when the internal sample rate has changed */
  741. unsigned int idx;
  742. unsigned int x;
  743. int res = -1;
  744. timer = softmix_data->timer;
  745. timingfd = ast_timer_fd(timer);
  746. softmix_translate_helper_init(&trans_helper, softmix_data->internal_rate);
  747. ast_timer_set_rate(timer, (1000 / softmix_data->internal_mixing_interval));
  748. /* Give the mixing array room to grow, memory is cheap but allocations are expensive. */
  749. if (softmix_mixing_array_init(&mixing_array, bridge->num_channels + 10)) {
  750. goto softmix_cleanup;
  751. }
  752. /*
  753. * XXX Softmix needs to use channel roles to determine who gets
  754. * what audio mixed.
  755. */
  756. while (!softmix_data->stop && bridge->num_active) {
  757. struct ast_bridge_channel *bridge_channel;
  758. int timeout = -1;
  759. enum ast_format_id cur_slin_id = ast_format_slin_by_rate(softmix_data->internal_rate);
  760. unsigned int softmix_samples = SOFTMIX_SAMPLES(softmix_data->internal_rate, softmix_data->internal_mixing_interval);
  761. unsigned int softmix_datalen = SOFTMIX_DATALEN(softmix_data->internal_rate, softmix_data->internal_mixing_interval);
  762. if (softmix_datalen > MAX_DATALEN) {
  763. /* This should NEVER happen, but if it does we need to know about it. Almost
  764. * all the memcpys used during this process depend on this assumption. Rather
  765. * than checking this over and over again through out the code, this single
  766. * verification is done on each iteration. */
  767. ast_log(LOG_WARNING,
  768. "Bridge %s: Conference mixing error, requested mixing length greater than mixing buffer.\n",
  769. bridge->uniqueid);
  770. goto softmix_cleanup;
  771. }
  772. /* Grow the mixing array buffer as participants are added. */
  773. if (mixing_array.max_num_entries < bridge->num_channels
  774. && softmix_mixing_array_grow(&mixing_array, bridge->num_channels + 5)) {
  775. goto softmix_cleanup;
  776. }
  777. /* init the number of buffers stored in the mixing array to 0.
  778. * As buffers are added for mixing, this number is incremented. */
  779. mixing_array.used_entries = 0;
  780. /* These variables help determine if a rate change is required */
  781. if (!stat_iteration_counter) {
  782. memset(&stats, 0, sizeof(stats));
  783. stats.locked_rate = bridge->softmix.internal_sample_rate;
  784. }
  785. /* If the sample rate has changed, update the translator helper */
  786. if (update_all_rates) {
  787. softmix_translate_helper_change_rate(&trans_helper, softmix_data->internal_rate);
  788. }
  789. /* Go through pulling audio from each factory that has it available */
  790. AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
  791. struct softmix_channel *sc = bridge_channel->tech_pvt;
  792. /* Update the sample rate to match the bridge's native sample rate if necessary. */
  793. if (update_all_rates) {
  794. set_softmix_bridge_data(softmix_data->internal_rate, softmix_data->internal_mixing_interval, bridge_channel, 1);
  795. }
  796. /* If stat_iteration_counter is 0, then collect statistics during this mixing interation */
  797. if (!stat_iteration_counter) {
  798. gather_softmix_stats(&stats, softmix_data, bridge_channel);
  799. }
  800. /* if the channel is suspended, don't check for audio, but still gather stats */
  801. if (bridge_channel->suspended) {
  802. continue;
  803. }
  804. /* Try to get audio from the factory if available */
  805. ast_mutex_lock(&sc->lock);
  806. if ((mixing_array.buffers[mixing_array.used_entries] = softmix_process_read_audio(sc, softmix_samples))) {
  807. mixing_array.used_entries++;
  808. }
  809. ast_mutex_unlock(&sc->lock);
  810. }
  811. /* mix it like crazy */
  812. memset(buf, 0, softmix_datalen);
  813. for (idx = 0; idx < mixing_array.used_entries; ++idx) {
  814. for (x = 0; x < softmix_samples; ++x) {
  815. ast_slinear_saturated_add(buf + x, mixing_array.buffers[idx] + x);
  816. }
  817. }
  818. /* Next step go through removing the channel's own audio and creating a good frame... */
  819. AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
  820. struct softmix_channel *sc = bridge_channel->tech_pvt;
  821. if (bridge_channel->suspended) {
  822. continue;
  823. }
  824. ast_mutex_lock(&sc->lock);
  825. /* Make SLINEAR write frame from local buffer */
  826. if (sc->write_frame.subclass.format.id != cur_slin_id) {
  827. ast_format_set(&sc->write_frame.subclass.format, cur_slin_id, 0);
  828. }
  829. sc->write_frame.datalen = softmix_datalen;
  830. sc->write_frame.samples = softmix_samples;
  831. memcpy(sc->final_buf, buf, softmix_datalen);
  832. /* process the softmix channel's new write audio */
  833. softmix_process_write_audio(&trans_helper, ast_channel_rawwriteformat(bridge_channel->chan), sc);
  834. ast_mutex_unlock(&sc->lock);
  835. /* A frame is now ready for the channel. */
  836. ast_bridge_channel_queue_frame(bridge_channel, &sc->write_frame);
  837. }
  838. update_all_rates = 0;
  839. if (!stat_iteration_counter) {
  840. update_all_rates = analyse_softmix_stats(&stats, softmix_data);
  841. stat_iteration_counter = SOFTMIX_STAT_INTERVAL;
  842. }
  843. stat_iteration_counter--;
  844. ast_bridge_unlock(bridge);
  845. /* cleanup any translation frame data from the previous mixing iteration. */
  846. softmix_translate_helper_cleanup(&trans_helper);
  847. /* Wait for the timing source to tell us to wake up and get things done */
  848. ast_waitfor_n_fd(&timingfd, 1, &timeout, NULL);
  849. if (ast_timer_ack(timer, 1) < 0) {
  850. ast_log(LOG_ERROR, "Bridge %s: Failed to acknowledge timer in softmix.\n",
  851. bridge->uniqueid);
  852. ast_bridge_lock(bridge);
  853. goto softmix_cleanup;
  854. }
  855. ast_bridge_lock(bridge);
  856. /* make sure to detect mixing interval changes if they occur. */
  857. if (bridge->softmix.internal_mixing_interval
  858. && (bridge->softmix.internal_mixing_interval != softmix_data->internal_mixing_interval)) {
  859. softmix_data->internal_mixing_interval = bridge->softmix.internal_mixing_interval;
  860. ast_timer_set_rate(timer, (1000 / softmix_data->internal_mixing_interval));
  861. update_all_rates = 1; /* if the interval changes, the rates must be adjusted as well just to be notified new interval.*/
  862. }
  863. }
  864. res = 0;
  865. softmix_cleanup:
  866. softmix_translate_helper_destroy(&trans_helper);
  867. softmix_mixing_array_destroy(&mixing_array);
  868. return res;
  869. }
  870. /*!
  871. * \internal
  872. * \brief Mixing thread.
  873. * \since 12.0.0
  874. *
  875. * \note The thread does not have its own reference to the
  876. * bridge. The lifetime of the thread is tied to the lifetime
  877. * of the mixing technology association with the bridge.
  878. */
  879. static void *softmix_mixing_thread(void *data)
  880. {
  881. struct softmix_bridge_data *softmix_data = data;
  882. struct ast_bridge *bridge = softmix_data->bridge;
  883. ast_bridge_lock(bridge);
  884. if (bridge->callid) {
  885. ast_callid_threadassoc_add(bridge->callid);
  886. }
  887. ast_debug(1, "Bridge %s: starting mixing thread\n", bridge->uniqueid);
  888. while (!softmix_data->stop) {
  889. if (!bridge->num_active) {
  890. /* Wait for something to happen to the bridge. */
  891. ast_bridge_unlock(bridge);
  892. ast_mutex_lock(&softmix_data->lock);
  893. if (!softmix_data->stop) {
  894. ast_cond_wait(&softmix_data->cond, &softmix_data->lock);
  895. }
  896. ast_mutex_unlock(&softmix_data->lock);
  897. ast_bridge_lock(bridge);
  898. continue;
  899. }
  900. if (softmix_mixing_loop(bridge)) {
  901. /*
  902. * A mixing error occurred. Sleep and try again later so we
  903. * won't flood the logs.
  904. */
  905. ast_bridge_unlock(bridge);
  906. sleep(1);
  907. ast_bridge_lock(bridge);
  908. }
  909. }
  910. ast_bridge_unlock(bridge);
  911. ast_debug(1, "Bridge %s: stopping mixing thread\n", bridge->uniqueid);
  912. return NULL;
  913. }
  914. static void softmix_bridge_data_destroy(struct softmix_bridge_data *softmix_data)
  915. {
  916. if (softmix_data->timer) {
  917. ast_timer_close(softmix_data->timer);
  918. softmix_data->timer = NULL;
  919. }
  920. ast_mutex_destroy(&softmix_data->lock);
  921. ast_cond_destroy(&softmix_data->cond);
  922. ast_free(softmix_data);
  923. }
  924. /*! \brief Function called when a bridge is created */
  925. static int softmix_bridge_create(struct ast_bridge *bridge)
  926. {
  927. struct softmix_bridge_data *softmix_data;
  928. softmix_data = ast_calloc(1, sizeof(*softmix_data));
  929. if (!softmix_data) {
  930. return -1;
  931. }
  932. softmix_data->bridge = bridge;
  933. ast_mutex_init(&softmix_data->lock);
  934. ast_cond_init(&softmix_data->cond, NULL);
  935. softmix_data->timer = ast_timer_open();
  936. if (!softmix_data->timer) {
  937. ast_log(AST_LOG_WARNING, "Failed to open timer for softmix bridge\n");
  938. softmix_bridge_data_destroy(softmix_data);
  939. return -1;
  940. }
  941. /* start at 8khz, let it grow from there */
  942. softmix_data->internal_rate = 8000;
  943. softmix_data->internal_mixing_interval = DEFAULT_SOFTMIX_INTERVAL;
  944. bridge->tech_pvt = softmix_data;
  945. /* Start the mixing thread. */
  946. if (ast_pthread_create(&softmix_data->thread, NULL, softmix_mixing_thread,
  947. softmix_data)) {
  948. softmix_data->thread = AST_PTHREADT_NULL;
  949. softmix_bridge_data_destroy(softmix_data);
  950. bridge->tech_pvt = NULL;
  951. return -1;
  952. }
  953. return 0;
  954. }
  955. /*!
  956. * \internal
  957. * \brief Request the softmix mixing thread stop.
  958. * \since 12.0.0
  959. *
  960. * \param bridge Which bridge is being stopped.
  961. *
  962. * \return Nothing
  963. */
  964. static void softmix_bridge_stop(struct ast_bridge *bridge)
  965. {
  966. struct softmix_bridge_data *softmix_data;
  967. softmix_data = bridge->tech_pvt;
  968. if (!softmix_data) {
  969. return;
  970. }
  971. ast_mutex_lock(&softmix_data->lock);
  972. softmix_data->stop = 1;
  973. ast_mutex_unlock(&softmix_data->lock);
  974. }
  975. /*! \brief Function called when a bridge is destroyed */
  976. static void softmix_bridge_destroy(struct ast_bridge *bridge)
  977. {
  978. struct softmix_bridge_data *softmix_data;
  979. pthread_t thread;
  980. softmix_data = bridge->tech_pvt;
  981. if (!softmix_data) {
  982. return;
  983. }
  984. /* Stop the mixing thread. */
  985. ast_mutex_lock(&softmix_data->lock);
  986. softmix_data->stop = 1;
  987. ast_cond_signal(&softmix_data->cond);
  988. thread = softmix_data->thread;
  989. softmix_data->thread = AST_PTHREADT_NULL;
  990. ast_mutex_unlock(&softmix_data->lock);
  991. if (thread != AST_PTHREADT_NULL) {
  992. ast_debug(1, "Bridge %s: Waiting for mixing thread to die.\n", bridge->uniqueid);
  993. pthread_join(thread, NULL);
  994. }
  995. softmix_bridge_data_destroy(softmix_data);
  996. bridge->tech_pvt = NULL;
  997. }
  998. static struct ast_bridge_technology softmix_bridge = {
  999. .name = "softmix",
  1000. .capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX,
  1001. .preference = AST_BRIDGE_PREFERENCE_BASE_MULTIMIX,
  1002. .create = softmix_bridge_create,
  1003. .stop = softmix_bridge_stop,
  1004. .destroy = softmix_bridge_destroy,
  1005. .join = softmix_bridge_join,
  1006. .leave = softmix_bridge_leave,
  1007. .unsuspend = softmix_bridge_unsuspend,
  1008. .write = softmix_bridge_write,
  1009. };
  1010. static int unload_module(void)
  1011. {
  1012. ast_format_cap_destroy(softmix_bridge.format_capabilities);
  1013. return ast_bridge_technology_unregister(&softmix_bridge);
  1014. }
  1015. static int load_module(void)
  1016. {
  1017. struct ast_format tmp;
  1018. if (!(softmix_bridge.format_capabilities = ast_format_cap_alloc(0))) {
  1019. return AST_MODULE_LOAD_DECLINE;
  1020. }
  1021. ast_format_cap_add(softmix_bridge.format_capabilities, ast_format_set(&tmp, AST_FORMAT_SLINEAR, 0));
  1022. return ast_bridge_technology_register(&softmix_bridge);
  1023. }
  1024. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Multi-party software based channel mixing");