Merge "BuildSystem: Add DragonFly BSD."
[asterisk/asterisk.git] / bridges / bridge_softmix.c
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
20 /*! \file
21  *
22  * \brief Multi-party software based channel mixing
23  *
24  * \author Joshua Colp <jcolp@digium.com>
25  * \author David Vossel <dvossel@digium.com>
26  *
27  * \ingroup bridges
28  */
29
30 /*** MODULEINFO
31         <support_level>core</support_level>
32  ***/
33
34 #include "asterisk.h"
35
36 #include "asterisk/stream.h"
37 #include "asterisk/test.h"
38 #include "asterisk/vector.h"
39 #include "asterisk/message.h"
40 #include "bridge_softmix/include/bridge_softmix_internal.h"
41
42 /*! The minimum sample rate of the bridge. */
43 #define SOFTMIX_MIN_SAMPLE_RATE 8000    /* 8 kHz sample rate */
44
45 /*! \brief Interval at which mixing will take place. Valid options are 10, 20, and 40. */
46 #define DEFAULT_SOFTMIX_INTERVAL 20
47
48 /*! \brief Size of the buffer used for sample manipulation */
49 #define SOFTMIX_DATALEN(rate, interval) ((rate/50) * (interval / 10))
50
51 /*! \brief Number of samples we are dealing with */
52 #define SOFTMIX_SAMPLES(rate, interval) (SOFTMIX_DATALEN(rate, interval) / 2)
53
54 /*! \brief Number of mixing iterations to perform between gathering statistics. */
55 #define SOFTMIX_STAT_INTERVAL 100
56
57 /*!
58  * \brief Default time in ms of silence necessary to declare talking stopped by the bridge.
59  *
60  * \details
61  * This is the time at which a channel's own audio will stop getting
62  * mixed out of its own write audio stream because it is no longer talking.
63  */
64 #define DEFAULT_SOFTMIX_SILENCE_THRESHOLD 2500
65
66 /*! Default minimum average magnitude threshold to determine talking by the DSP. */
67 #define DEFAULT_SOFTMIX_TALKING_THRESHOLD 160
68
69 #define SOFTBRIDGE_VIDEO_DEST_PREFIX "softbridge_dest"
70 #define SOFTBRIDGE_VIDEO_DEST_LEN strlen(SOFTBRIDGE_VIDEO_DEST_PREFIX)
71 #define SOFTBRIDGE_VIDEO_DEST_SEPARATOR '_'
72
73 struct softmix_remb_collector {
74         /*! The frame which will be given to each source stream */
75         struct ast_frame frame;
76         /*! The REMB to send to the source which is collecting REMB reports */
77         struct ast_rtp_rtcp_feedback feedback;
78         /*! The maximum bitrate */
79         unsigned int bitrate;
80 };
81
82 struct softmix_stats {
83         /*! Each index represents a sample rate used above the internal rate. */
84         unsigned int sample_rates[16];
85         /*! Each index represents the number of channels using the same index in the sample_rates array.  */
86         unsigned int num_channels[16];
87         /*! The number of channels above the internal sample rate */
88         unsigned int num_above_internal_rate;
89         /*! The number of channels at the internal sample rate */
90         unsigned int num_at_internal_rate;
91         /*! The absolute highest sample rate preferred by any channel in the bridge */
92         unsigned int highest_supported_rate;
93         /*! Is the sample rate locked by the bridge, if so what is that rate.*/
94         unsigned int locked_rate;
95 };
96
97 struct softmix_translate_helper_entry {
98         int num_times_requested; /*!< Once this entry is no longer requested, free the trans_pvt
99                                                                   and re-init if it was usable. */
100         struct ast_format *dst_format; /*!< The destination format for this helper */
101         struct ast_trans_pvt *trans_pvt; /*!< the translator for this slot. */
102         struct ast_frame *out_frame; /*!< The output frame from the last translation */
103         AST_LIST_ENTRY(softmix_translate_helper_entry) entry;
104 };
105
106 struct softmix_translate_helper {
107         struct ast_format *slin_src; /*!< the source format expected for all the translators */
108         AST_LIST_HEAD_NOLOCK(, softmix_translate_helper_entry) entries;
109 };
110
111 static struct softmix_translate_helper_entry *softmix_translate_helper_entry_alloc(struct ast_format *dst)
112 {
113         struct softmix_translate_helper_entry *entry;
114         if (!(entry = ast_calloc(1, sizeof(*entry)))) {
115                 return NULL;
116         }
117         entry->dst_format = ao2_bump(dst);
118         /* initialize this to one so that the first time through the cleanup code after
119            allocation it won't be removed from the entry list */
120         entry->num_times_requested = 1;
121         return entry;
122 }
123
124 static void *softmix_translate_helper_free_entry(struct softmix_translate_helper_entry *entry)
125 {
126         ao2_cleanup(entry->dst_format);
127
128         if (entry->trans_pvt) {
129                 ast_translator_free_path(entry->trans_pvt);
130         }
131         if (entry->out_frame) {
132                 ast_frfree(entry->out_frame);
133         }
134         ast_free(entry);
135         return NULL;
136 }
137
138 static void softmix_translate_helper_init(struct softmix_translate_helper *trans_helper, unsigned int sample_rate)
139 {
140         memset(trans_helper, 0, sizeof(*trans_helper));
141         trans_helper->slin_src = ast_format_cache_get_slin_by_rate(sample_rate);
142 }
143
144 static void softmix_translate_helper_destroy(struct softmix_translate_helper *trans_helper)
145 {
146         struct softmix_translate_helper_entry *entry;
147
148         while ((entry = AST_LIST_REMOVE_HEAD(&trans_helper->entries, entry))) {
149                 softmix_translate_helper_free_entry(entry);
150         }
151 }
152
153 static void softmix_translate_helper_change_rate(struct softmix_translate_helper *trans_helper, unsigned int sample_rate)
154 {
155         struct softmix_translate_helper_entry *entry;
156
157         trans_helper->slin_src = ast_format_cache_get_slin_by_rate(sample_rate);
158         AST_LIST_TRAVERSE_SAFE_BEGIN(&trans_helper->entries, entry, entry) {
159                 if (entry->trans_pvt) {
160                         ast_translator_free_path(entry->trans_pvt);
161                         if (!(entry->trans_pvt = ast_translator_build_path(entry->dst_format, trans_helper->slin_src))) {
162                                 AST_LIST_REMOVE_CURRENT(entry);
163                                 entry = softmix_translate_helper_free_entry(entry);
164                         }
165                 }
166         }
167         AST_LIST_TRAVERSE_SAFE_END;
168 }
169
170 /*!
171  * \internal
172  * \brief Get the next available audio on the softmix channel's read stream
173  * and determine if it should be mixed out or not on the write stream.
174  *
175  * \retval pointer to buffer containing the exact number of samples requested on success.
176  * \retval NULL if no samples are present
177  */
178 static int16_t *softmix_process_read_audio(struct softmix_channel *sc, unsigned int num_samples)
179 {
180         if ((ast_slinfactory_available(&sc->factory) >= num_samples) &&
181                 ast_slinfactory_read(&sc->factory, sc->our_buf, num_samples)) {
182                 sc->have_audio = 1;
183                 return sc->our_buf;
184         }
185         sc->have_audio = 0;
186         return NULL;
187 }
188
189 /*!
190  * \internal
191  * \brief Process a softmix channel's write audio
192  *
193  * \details This function will remove the channel's talking from its own audio if present and
194  * possibly even do the channel's write translation for it depending on how many other
195  * channels use the same write format.
196  */
197 static void softmix_process_write_audio(struct softmix_translate_helper *trans_helper,
198         struct ast_format *raw_write_fmt,
199         struct softmix_channel *sc, unsigned int default_sample_size)
200 {
201         struct softmix_translate_helper_entry *entry = NULL;
202         int i;
203
204         /* If we provided audio that was not determined to be silence,
205          * then take it out while in slinear format. */
206         if (sc->have_audio && sc->talking && !sc->binaural) {
207                 for (i = 0; i < sc->write_frame.samples; i++) {
208                         ast_slinear_saturated_subtract(&sc->final_buf[i], &sc->our_buf[i]);
209                 }
210                 /* check to see if any entries exist for the format. if not we'll want
211                    to remove it during cleanup */
212                 AST_LIST_TRAVERSE(&trans_helper->entries, entry, entry) {
213                         if (ast_format_cmp(entry->dst_format, raw_write_fmt) == AST_FORMAT_CMP_EQUAL) {
214                                 ++entry->num_times_requested;
215                                 break;
216                         }
217                 }
218                 /* do not do any special write translate optimization if we had to make
219                  * a special mix for them to remove their own audio. */
220                 return;
221         } else if (sc->have_audio && sc->talking && sc->binaural > 0) {
222                 /*
223                  * Binaural audio requires special saturated substract since we have two
224                  * audio signals per channel now.
225                  */
226                 softmix_process_write_binaural_audio(sc, default_sample_size);
227                 return;
228         }
229
230         /* Attempt to optimize channels using the same translation path/codec. Build a list of entries
231            of translation paths and track the number of references for each type. Each one of the same
232            type should be able to use the same out_frame. Since the optimization is only necessary for
233            multiple channels (>=2) using the same codec make sure resources are allocated only when
234            needed and released when not (see also softmix_translate_helper_cleanup */
235         AST_LIST_TRAVERSE(&trans_helper->entries, entry, entry) {
236                 if (sc->binaural != 0) {
237                         continue;
238                 }
239                 if (ast_format_cmp(entry->dst_format, raw_write_fmt) == AST_FORMAT_CMP_EQUAL) {
240                         entry->num_times_requested++;
241                 } else {
242                         continue;
243                 }
244                 if (!entry->trans_pvt && (entry->num_times_requested > 1)) {
245                         entry->trans_pvt = ast_translator_build_path(entry->dst_format, trans_helper->slin_src);
246                 }
247                 if (entry->trans_pvt && !entry->out_frame) {
248                         entry->out_frame = ast_translate(entry->trans_pvt, &sc->write_frame, 0);
249                 }
250                 if (entry->out_frame && entry->out_frame->frametype == AST_FRAME_VOICE
251                                 && entry->out_frame->datalen < MAX_DATALEN) {
252                         ao2_replace(sc->write_frame.subclass.format, entry->out_frame->subclass.format);
253                         memcpy(sc->final_buf, entry->out_frame->data.ptr, entry->out_frame->datalen);
254                         sc->write_frame.datalen = entry->out_frame->datalen;
255                         sc->write_frame.samples = entry->out_frame->samples;
256                 }
257                 break;
258         }
259
260         /* add new entry into list if this format destination was not matched. */
261         if (!entry && (entry = softmix_translate_helper_entry_alloc(raw_write_fmt))) {
262                 AST_LIST_INSERT_HEAD(&trans_helper->entries, entry, entry);
263         }
264 }
265
266 static void softmix_translate_helper_cleanup(struct softmix_translate_helper *trans_helper)
267 {
268         struct softmix_translate_helper_entry *entry;
269
270         AST_LIST_TRAVERSE_SAFE_BEGIN(&trans_helper->entries, entry, entry) {
271                 /* if it hasn't been requested then remove it */
272                 if (!entry->num_times_requested) {
273                         AST_LIST_REMOVE_CURRENT(entry);
274                         softmix_translate_helper_free_entry(entry);
275                         continue;
276                 }
277
278                 if (entry->out_frame) {
279                         ast_frfree(entry->out_frame);
280                         entry->out_frame = NULL;
281                 }
282
283                 /* nothing is optimized for a single path reference, so there is
284                    no reason to continue to hold onto the codec */
285                 if (entry->num_times_requested == 1 && entry->trans_pvt) {
286                         ast_translator_free_path(entry->trans_pvt);
287                         entry->trans_pvt = NULL;
288                 }
289
290                 /* for each iteration (a mixing run) in the bridge softmix thread the number
291                    of references to a given entry is recalculated, so reset the number of
292                    times requested */
293                 entry->num_times_requested = 0;
294         }
295         AST_LIST_TRAVERSE_SAFE_END;
296 }
297
298 static void set_softmix_bridge_data(int rate, int interval, struct ast_bridge_channel *bridge_channel, int reset, int set_binaural, int binaural_pos_id, int is_announcement)
299 {
300         struct softmix_channel *sc = bridge_channel->tech_pvt;
301         struct ast_format *slin_format;
302         int setup_fail;
303
304 #ifdef BINAURAL_RENDERING
305         if (interval != BINAURAL_MIXING_INTERVAL) {
306                 interval = BINAURAL_MIXING_INTERVAL;
307         }
308 #endif
309
310         /* The callers have already ensured that sc is never NULL. */
311         ast_assert(sc != NULL);
312
313         slin_format = ast_format_cache_get_slin_by_rate(rate);
314
315         ast_mutex_lock(&sc->lock);
316         if (reset) {
317                 ast_slinfactory_destroy(&sc->factory);
318                 ast_dsp_free(sc->dsp);
319         }
320
321         /* Setup write frame parameters */
322         sc->write_frame.frametype = AST_FRAME_VOICE;
323         /*
324          * NOTE: The write_frame format holds a reference because translation
325          * could be needed and the format changed to the translated format
326          * for the channel.  The translated format may not be a
327          * static cached format.
328          */
329         ao2_replace(sc->write_frame.subclass.format, slin_format);
330         sc->write_frame.data.ptr = sc->final_buf;
331         sc->write_frame.datalen = SOFTMIX_DATALEN(rate, interval);
332         sc->write_frame.samples = SOFTMIX_SAMPLES(rate, interval);
333
334         /* We will store the rate here cause we need to set the data again when a channel is unsuspended */
335         sc->rate = rate;
336
337         /* If the channel will contain binaural data we will set a identifier in the channel
338          * if set_binaural == -1 this is just a sample rate update, will ignore it. */
339         if (set_binaural == 1) {
340                 sc->binaural = 1;
341         } else if (set_binaural == 0) {
342                 sc->binaural = 0;
343         }
344
345         /* Setting the binaural position. This doesn't require a change of the overlaying channel infos
346          * and doesn't have to be done if we just updating sample rates. */
347         if (binaural_pos_id != -1) {
348                 sc->binaural_pos = binaural_pos_id;
349         }
350         if (is_announcement != -1) {
351                 sc->is_announcement = is_announcement;
352         }
353
354         /*
355          * NOTE: The read_slin_format does not hold a reference because it
356          * will always be a signed linear format.
357          */
358         sc->read_slin_format = slin_format;
359
360         /* Setup smoother */
361         setup_fail = ast_slinfactory_init_with_format(&sc->factory, slin_format);
362
363         /* set new read and write formats on channel. */
364         ast_channel_lock(bridge_channel->chan);
365         setup_fail |= ast_set_read_format_path(bridge_channel->chan,
366                 ast_channel_rawreadformat(bridge_channel->chan), slin_format);
367         ast_channel_unlock(bridge_channel->chan);
368
369         /* If channel contains binaural data we will set it here for the trans_pvt. */
370         if (set_binaural == 1 || (set_binaural == -1 && sc->binaural == 1)) {
371                 setup_fail |= ast_set_write_format_interleaved_stereo(bridge_channel->chan, slin_format);
372         } else if (set_binaural == 0) {
373                 setup_fail |= ast_set_write_format(bridge_channel->chan, slin_format);
374         }
375
376         /* set up new DSP.  This is on the read side only right before the read frame enters the smoother.  */
377         sc->dsp = ast_dsp_new_with_rate(rate);
378         if (setup_fail || !sc->dsp) {
379                 /* Bad news.  Could not setup the channel for softmix. */
380                 ast_mutex_unlock(&sc->lock);
381                 ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END, 0);
382                 return;
383         }
384
385         /* we want to aggressively detect silence to avoid feedback */
386         if (bridge_channel->tech_args.talking_threshold) {
387                 ast_dsp_set_threshold(sc->dsp, bridge_channel->tech_args.talking_threshold);
388         } else {
389                 ast_dsp_set_threshold(sc->dsp, DEFAULT_SOFTMIX_TALKING_THRESHOLD);
390         }
391
392         ast_mutex_unlock(&sc->lock);
393 }
394
395 /*!
396  * \internal
397  * \brief Poke the mixing thread in case it is waiting for an active channel.
398  * \since 12.0.0
399  *
400  * \param softmix_data Bridge mixing data.
401  *
402  * \return Nothing
403  */
404 static void softmix_poke_thread(struct softmix_bridge_data *softmix_data)
405 {
406         ast_mutex_lock(&softmix_data->lock);
407         ast_cond_signal(&softmix_data->cond);
408         ast_mutex_unlock(&softmix_data->lock);
409 }
410
411 /*! \brief Function called when a channel is unsuspended from the bridge */
412 static void softmix_bridge_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
413 {
414 #ifdef BINAURAL_RENDERING
415         struct softmix_channel *sc = bridge_channel->tech_pvt;
416         if (sc->binaural) {
417                 /* Restore some usefull data if it was a binaural channel */
418                 struct ast_format *slin_format;
419
420                 slin_format = ast_format_cache_get_slin_by_rate(sc->rate);
421                 ast_set_write_format_interleaved_stereo(bridge_channel->chan, slin_format);
422         }
423 #endif
424         if (bridge->tech_pvt) {
425                 softmix_poke_thread(bridge->tech_pvt);
426         }
427 }
428
429 /*!
430  * \brief Determine if a stream is a video source stream.
431  *
432  * \param stream The stream to test
433  * \retval 1 The stream is a video source
434  * \retval 0 The stream is not a video source
435  */
436 static int is_video_source(const struct ast_stream *stream)
437 {
438         if (ast_stream_get_state(stream) != AST_STREAM_STATE_REMOVED
439                 && ast_stream_get_type(stream) == AST_MEDIA_TYPE_VIDEO
440                 && strncmp(ast_stream_get_name(stream), SOFTBRIDGE_VIDEO_DEST_PREFIX,
441                         SOFTBRIDGE_VIDEO_DEST_LEN)) {
442                 return 1;
443         }
444
445         return 0;
446 }
447
448 /*!
449  * \brief Determine if a stream is a video destination stream.
450  *
451  * A source channel name can be provided to narrow this to a destination stream
452  * for a particular source channel. Further, a source stream name can be provided
453  * to narrow this to a particular source stream's destination. However, empty strings
454  * can be provided to match any destination video stream, regardless of source channel
455  * or source stream.
456  *
457  * \param stream The stream to test
458  * \param source_channel_name The name of a source video channel to match
459  * \param source_stream_name The name of the source video stream to match
460  * \retval 1 The stream is a video destination stream
461  * \retval 0 The stream is not a video destination stream
462  */
463 static int is_video_dest(const struct ast_stream *stream, const char *source_channel_name,
464         const char *source_stream_name)
465 {
466         char *dest_video_name;
467         size_t dest_video_name_len;
468
469         if (ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED
470                 || ast_stream_get_type(stream) != AST_MEDIA_TYPE_VIDEO) {
471                 return 0;
472         }
473
474         dest_video_name_len = SOFTBRIDGE_VIDEO_DEST_LEN + 1;
475         if (!ast_strlen_zero(source_channel_name)) {
476                 dest_video_name_len += strlen(source_channel_name) + 1;
477                 if (!ast_strlen_zero(source_stream_name)) {
478                         dest_video_name_len += strlen(source_stream_name) + 1;
479                 }
480
481                 dest_video_name = ast_alloca(dest_video_name_len);
482                 if (!ast_strlen_zero(source_stream_name)) {
483                         /* We are looking for an exact stream name */
484                         snprintf(dest_video_name, dest_video_name_len, "%s%c%s%c%s",
485                                 SOFTBRIDGE_VIDEO_DEST_PREFIX, SOFTBRIDGE_VIDEO_DEST_SEPARATOR,
486                                 source_channel_name, SOFTBRIDGE_VIDEO_DEST_SEPARATOR,
487                                 source_stream_name);
488                         return !strcmp(ast_stream_get_name(stream), dest_video_name);
489                 }
490                 snprintf(dest_video_name, dest_video_name_len, "%s%c%s",
491                         SOFTBRIDGE_VIDEO_DEST_PREFIX, SOFTBRIDGE_VIDEO_DEST_SEPARATOR,
492                         source_channel_name);
493         } else {
494                 dest_video_name = SOFTBRIDGE_VIDEO_DEST_PREFIX;
495         }
496
497         return !strncmp(ast_stream_get_name(stream), dest_video_name, dest_video_name_len - 1);
498 }
499
500 static int append_source_streams(struct ast_stream_topology *dest,
501         const char *channel_name,
502         const struct ast_stream_topology *source)
503 {
504         int i;
505
506         for (i = 0; i < ast_stream_topology_get_count(source); ++i) {
507                 struct ast_stream *stream;
508                 struct ast_stream *stream_clone;
509                 char *stream_clone_name = NULL;
510
511                 stream = ast_stream_topology_get_stream(source, i);
512                 if (!is_video_source(stream)) {
513                         continue;
514                 }
515
516                 if (ast_asprintf(&stream_clone_name, "%s_%s_%s", SOFTBRIDGE_VIDEO_DEST_PREFIX,
517                         channel_name, ast_stream_get_name(stream)) < 0) {
518                         return -1;
519                 }
520
521                 stream_clone = ast_stream_clone(stream, stream_clone_name);
522                 ast_free(stream_clone_name);
523                 if (!stream_clone) {
524                         return -1;
525                 }
526                 if (ast_stream_topology_append_stream(dest, stream_clone) < 0) {
527                         ast_stream_free(stream_clone);
528                         return -1;
529                 }
530         }
531
532         return 0;
533 }
534
535 static int append_all_streams(struct ast_stream_topology *dest,
536         const struct ast_stream_topology *source)
537 {
538         int i;
539         int dest_index = 0;
540
541         for (i = 0; i < ast_stream_topology_get_count(source); ++i) {
542                 struct ast_stream *clone;
543                 int added = 0;
544
545                 clone = ast_stream_clone(ast_stream_topology_get_stream(source, i), NULL);
546                 if (!clone) {
547                         return -1;
548                 }
549
550                 /* If we can reuse an existing removed stream then do so */
551                 while (dest_index < ast_stream_topology_get_count(dest)) {
552                         struct ast_stream *stream = ast_stream_topology_get_stream(dest, dest_index);
553
554                         dest_index++;
555
556                         if (ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED) {
557                                 /* This cannot fail because dest_index - 1 is less than the
558                                  * current count in dest. */
559                                 ast_stream_topology_set_stream(dest, dest_index - 1, clone);
560                                 added = 1;
561                                 break;
562                         }
563                 }
564
565                 /* If no removed stream exists that we took the place of append the stream */
566                 if (!added && ast_stream_topology_append_stream(dest, clone) < 0) {
567                         ast_stream_free(clone);
568                         return -1;
569                 }
570         }
571
572         return 0;
573 }
574
575 /*!
576  * \brief Issue channel stream topology change requests.
577  *
578  * When in SFU mode, each participant needs to be able to
579  * send video directly to other participants in the bridge.
580  * This means that all participants need to have their topologies
581  * updated. The joiner needs to have destination streams for
582  * all current participants, and the current participants need
583  * to have destinations streams added for the joiner's sources.
584  *
585  * \param joiner The channel that is joining the softmix bridge
586  * \param participants The current participants in the softmix bridge
587  */
588 static void sfu_topologies_on_join(struct ast_bridge_channel *joiner, struct ast_bridge_channels_list *participants)
589 {
590         struct ast_stream_topology *joiner_video = NULL;
591         struct ast_bridge_channel *participant;
592         int res;
593         struct softmix_channel *sc;
594
595         joiner_video = ast_stream_topology_alloc();
596         if (!joiner_video) {
597                 return;
598         }
599
600         sc = joiner->tech_pvt;
601
602         ast_channel_lock(joiner->chan);
603         res = append_source_streams(joiner_video, ast_channel_name(joiner->chan), ast_channel_get_stream_topology(joiner->chan));
604         sc->topology = ast_stream_topology_clone(ast_channel_get_stream_topology(joiner->chan));
605         ast_channel_unlock(joiner->chan);
606
607         if (res || !sc->topology) {
608                 goto cleanup;
609         }
610
611         AST_LIST_TRAVERSE(participants, participant, entry) {
612                 if (participant == joiner) {
613                         continue;
614                 }
615                 ast_channel_lock(participant->chan);
616                 res = append_source_streams(sc->topology, ast_channel_name(participant->chan),
617                                 ast_channel_get_stream_topology(participant->chan));
618                 ast_channel_unlock(participant->chan);
619                 if (res) {
620                         goto cleanup;
621                 }
622         }
623
624         ast_channel_request_stream_topology_change(joiner->chan, sc->topology, NULL);
625
626         AST_LIST_TRAVERSE(participants, participant, entry) {
627                 if (participant == joiner) {
628                         continue;
629                 }
630
631                 sc = participant->tech_pvt;
632                 if (append_all_streams(sc->topology, joiner_video)) {
633                         goto cleanup;
634                 }
635                 ast_channel_request_stream_topology_change(participant->chan, sc->topology, NULL);
636         }
637
638 cleanup:
639         ast_stream_topology_free(joiner_video);
640 }
641
642 /*! \brief Function called when a channel is joined into the bridge */
643 static int softmix_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
644 {
645         struct softmix_channel *sc;
646         struct softmix_bridge_data *softmix_data;
647         int set_binaural = 0;
648         /*
649          * If false, the channel will be convolved, but since it is a non stereo channel, output
650          * will be mono.
651          */
652         int skip_binaural_output = 1;
653         int pos_id;
654         int is_announcement = 0;
655         int samplerate_change;
656
657         softmix_data = bridge->tech_pvt;
658         if (!softmix_data) {
659                 return -1;
660         }
661
662         /* Create a new softmix_channel structure and allocate various things on it */
663         if (!(sc = ast_calloc(1, sizeof(*sc)))) {
664                 return -1;
665         }
666
667         samplerate_change = softmix_data->internal_rate;
668         pos_id = -1;
669         if (bridge->softmix.binaural_active) {
670                 if (strncmp(ast_channel_name(bridge_channel->chan), "CBAnn", 5) != 0) {
671                         set_binaural = ast_format_get_channel_count(bridge_channel->write_format) > 1 ? 1 : 0;
672                         if (set_binaural) {
673                                 softmix_data->internal_rate = samplerate_change;
674                         }
675                         skip_binaural_output = 0;
676                 } else {
677                         is_announcement = 1;
678                 }
679                 if (set_binaural) {
680                         softmix_data->convolve.binaural_active = 1;
681                 }
682                 if (!skip_binaural_output)      {
683                         pos_id = set_binaural_data_join(&softmix_data->convolve, softmix_data->default_sample_size);
684                         if (pos_id == -1) {
685                                 ast_log(LOG_ERROR, "Bridge %s: Failed to join channel %s. "
686                                                 "Could not allocate enough memory.\n", bridge->uniqueid,
687                                                 ast_channel_name(bridge_channel->chan));
688                                 ast_free(sc);
689                                 return -1;
690                         }
691                 }
692         }
693
694         /* Can't forget the lock */
695         ast_mutex_init(&sc->lock);
696
697         /* Can't forget to record our pvt structure within the bridged channel structure */
698         bridge_channel->tech_pvt = sc;
699
700         set_softmix_bridge_data(softmix_data->internal_rate,
701                 softmix_data->internal_mixing_interval
702                         ? softmix_data->internal_mixing_interval
703                         : DEFAULT_SOFTMIX_INTERVAL,
704                 bridge_channel, 0, set_binaural, pos_id, is_announcement);
705
706         if (bridge->softmix.video_mode.mode == AST_BRIDGE_VIDEO_MODE_SFU) {
707                 sfu_topologies_on_join(bridge_channel, &bridge->channels);
708         }
709
710         softmix_poke_thread(softmix_data);
711         return 0;
712 }
713
714 static int remove_destination_streams(struct ast_stream_topology *topology,
715         const char *channel_name)
716 {
717         int i;
718         int stream_removed = 0;
719
720         for (i = 0; i < ast_stream_topology_get_count(topology); ++i) {
721                 struct ast_stream *stream;
722
723                 stream = ast_stream_topology_get_stream(topology, i);
724
725                 if (is_video_dest(stream, channel_name, NULL)) {
726                         ast_stream_set_state(stream, AST_STREAM_STATE_REMOVED);
727                         stream_removed = 1;
728                 }
729         }
730         return stream_removed;
731 }
732
733 static int sfu_topologies_on_leave(struct ast_bridge_channel *leaver, struct ast_bridge_channels_list *participants)
734 {
735         struct ast_bridge_channel *participant;
736         struct softmix_channel *sc;
737
738         AST_LIST_TRAVERSE(participants, participant, entry) {
739                 sc = participant->tech_pvt;
740                 if (!remove_destination_streams(sc->topology, ast_channel_name(leaver->chan))) {
741                         continue;
742                 }
743                 ast_channel_request_stream_topology_change(participant->chan, sc->topology, NULL);
744         }
745
746         sc = leaver->tech_pvt;
747         if (remove_destination_streams(sc->topology, "")) {
748                 ast_channel_request_stream_topology_change(leaver->chan, sc->topology, NULL);
749         }
750
751         return 0;
752 }
753
754 /*! \brief Function called when a channel leaves the bridge */
755 static void softmix_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
756 {
757         struct softmix_channel *sc;
758         struct softmix_bridge_data *softmix_data;
759
760         softmix_data = bridge->tech_pvt;
761         sc = bridge_channel->tech_pvt;
762         if (!sc) {
763                 return;
764         }
765
766         if (bridge->softmix.video_mode.mode == AST_BRIDGE_VIDEO_MODE_SFU) {
767                 sfu_topologies_on_leave(bridge_channel, &bridge->channels);
768         }
769
770         if (bridge->softmix.binaural_active) {
771                 if (sc->binaural) {
772                         set_binaural_data_leave(&softmix_data->convolve, sc->binaural_pos,
773                                         softmix_data->default_sample_size);
774                 }
775         }
776
777         bridge_channel->tech_pvt = NULL;
778
779         ast_stream_topology_free(sc->topology);
780
781         ao2_cleanup(sc->remb_collector);
782
783         AST_VECTOR_FREE(&sc->video_sources);
784
785         /* Drop mutex lock */
786         ast_mutex_destroy(&sc->lock);
787
788         /* Drop the factory */
789         ast_slinfactory_destroy(&sc->factory);
790
791         /* Drop any formats on the frames */
792         ao2_cleanup(sc->write_frame.subclass.format);
793
794         /* Drop the DSP */
795         ast_dsp_free(sc->dsp);
796
797         /* Eep! drop ourselves */
798         ast_free(sc);
799 }
800
801 static void softmix_pass_video_top_priority(struct ast_bridge *bridge, struct ast_frame *frame)
802 {
803         struct ast_bridge_channel *cur;
804
805         AST_LIST_TRAVERSE(&bridge->channels, cur, entry) {
806                 if (cur->suspended) {
807                         continue;
808                 }
809                 if (ast_bridge_is_video_src(bridge, cur->chan) == 1) {
810                         ast_bridge_channel_queue_frame(cur, frame);
811                         break;
812                 }
813         }
814 }
815
816 /*!
817  * \internal
818  * \brief Determine what to do with a video frame.
819  * \since 12.0.0
820  *
821  * \param bridge Which bridge is getting the frame
822  * \param bridge_channel Which channel is writing the frame.
823  * \param frame What is being written.
824  *
825  * \return Nothing
826  */
827 static void softmix_bridge_write_video(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
828 {
829         struct softmix_channel *sc;
830         int video_src_priority;
831
832         /* Determine if the video frame should be distributed or not */
833         switch (bridge->softmix.video_mode.mode) {
834         case AST_BRIDGE_VIDEO_MODE_NONE:
835                 break;
836         case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
837                 video_src_priority = ast_bridge_is_video_src(bridge, bridge_channel->chan);
838                 if (video_src_priority == 1) {
839                         /* Pass to me and everyone else. */
840                         ast_bridge_queue_everyone_else(bridge, NULL, frame);
841                 }
842                 break;
843         case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
844                 sc = bridge_channel->tech_pvt;
845                 ast_mutex_lock(&sc->lock);
846                 ast_bridge_update_talker_src_video_mode(bridge, bridge_channel->chan,
847                         sc->video_talker.energy_average,
848                         frame->subclass.frame_ending);
849                 ast_mutex_unlock(&sc->lock);
850                 video_src_priority = ast_bridge_is_video_src(bridge, bridge_channel->chan);
851                 if (video_src_priority == 1) {
852                         int num_src = ast_bridge_number_video_src(bridge);
853                         int echo = num_src > 1 ? 0 : 1;
854
855                         ast_bridge_queue_everyone_else(bridge, echo ? NULL : bridge_channel, frame);
856                 } else if (video_src_priority == 2) {
857                         softmix_pass_video_top_priority(bridge, frame);
858                 }
859                 break;
860         case AST_BRIDGE_VIDEO_MODE_SFU:
861                 /* Nothing special to do here, the bridge channel stream map will ensure the
862                  * video goes everywhere it needs to
863                  */
864                 ast_bridge_queue_everyone_else(bridge, bridge_channel, frame);
865                 break;
866         }
867 }
868
869 /*!
870  * \internal
871  * \brief Determine what to do with a voice frame.
872  * \since 12.0.0
873  *
874  * \param bridge Which bridge is getting the frame
875  * \param bridge_channel Which channel is writing the frame.
876  * \param frame What is being written.
877  *
878  * \return Nothing
879  */
880 static void softmix_bridge_write_voice(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
881 {
882         struct softmix_channel *sc = bridge_channel->tech_pvt;
883         struct softmix_bridge_data *softmix_data = bridge->tech_pvt;
884         int silent = 0;
885         int totalsilence = 0;
886         int cur_energy = 0;
887         int silence_threshold = bridge_channel->tech_args.silence_threshold ?
888                 bridge_channel->tech_args.silence_threshold :
889                 DEFAULT_SOFTMIX_SILENCE_THRESHOLD;
890         /*
891          * If update_talking is set to 0 or 1, tell the bridge that the channel
892          * has started or stopped talking.
893          */
894         char update_talking = -1;
895
896         /* Write the frame into the conference */
897         ast_mutex_lock(&sc->lock);
898
899         if (ast_format_cmp(frame->subclass.format, sc->read_slin_format) != AST_FORMAT_CMP_EQUAL) {
900                 /*
901                  * The incoming frame is not the expected format.  Update
902                  * the channel's translation path to get us slinear from
903                  * the new format for the next frame.
904                  *
905                  * There is the possibility that this frame is an old slinear
906                  * rate frame that was in flight when the softmix bridge
907                  * changed rates.  If so it will self correct on subsequent
908                  * frames.
909                  */
910                 ast_channel_lock(bridge_channel->chan);
911                 ast_debug(1, "Channel %s wrote unexpected format into bridge.  Got %s, expected %s.\n",
912                         ast_channel_name(bridge_channel->chan),
913                         ast_format_get_name(frame->subclass.format),
914                         ast_format_get_name(sc->read_slin_format));
915                 ast_set_read_format_path(bridge_channel->chan, frame->subclass.format,
916                         sc->read_slin_format);
917                 ast_channel_unlock(bridge_channel->chan);
918         }
919
920         /* The channel will be leaving soon if there is no dsp. */
921         if (sc->dsp) {
922                 silent = ast_dsp_silence_with_energy(sc->dsp, frame, &totalsilence, &cur_energy);
923         }
924
925         if (bridge->softmix.video_mode.mode == AST_BRIDGE_VIDEO_MODE_TALKER_SRC) {
926                 int cur_slot = sc->video_talker.energy_history_cur_slot;
927
928                 sc->video_talker.energy_accum -= sc->video_talker.energy_history[cur_slot];
929                 sc->video_talker.energy_accum += cur_energy;
930                 sc->video_talker.energy_history[cur_slot] = cur_energy;
931                 sc->video_talker.energy_average = sc->video_talker.energy_accum / DEFAULT_ENERGY_HISTORY_LEN;
932                 sc->video_talker.energy_history_cur_slot++;
933                 if (sc->video_talker.energy_history_cur_slot == DEFAULT_ENERGY_HISTORY_LEN) {
934                         sc->video_talker.energy_history_cur_slot = 0; /* wrap around */
935                 }
936         }
937
938         if (totalsilence < silence_threshold) {
939                 if (!sc->talking && !silent) {
940                         /* Tell the write process we have audio to be mixed out */
941                         sc->talking = 1;
942                         update_talking = 1;
943                 }
944         } else {
945                 if (sc->talking) {
946                         sc->talking = 0;
947                         update_talking = 0;
948                 }
949         }
950
951         /* Before adding audio in, make sure we haven't fallen behind. If audio has fallen
952          * behind 4 times the amount of samples mixed on every iteration of the mixer, Re-sync
953          * the audio by flushing the buffer before adding new audio in. */
954         if (ast_slinfactory_available(&sc->factory) > (4 * SOFTMIX_SAMPLES(softmix_data->internal_rate, softmix_data->internal_mixing_interval))) {
955                 ast_slinfactory_flush(&sc->factory);
956         }
957
958         if (sc->talking || !bridge_channel->tech_args.drop_silence) {
959                 /* Add frame to the smoother for mixing with other channels. */
960                 ast_slinfactory_feed(&sc->factory, frame);
961         }
962
963         /* Alllll done */
964         ast_mutex_unlock(&sc->lock);
965
966         if (update_talking != -1) {
967                 ast_bridge_channel_notify_talking(bridge_channel, update_talking);
968         }
969 }
970
971 /*!
972  * \internal
973  * \brief Clear talking flag, stop contributing to mixing and notify handlers.
974  * \since 13.21.0, 15.4.0
975  *
976  * \param bridge_channel Which channel's talking to clear
977  *
978  * \return Nothing
979  */
980 static void clear_talking(struct ast_bridge_channel *bridge_channel)
981 {
982         struct softmix_channel *sc = bridge_channel->tech_pvt;
983
984         if (sc->talking) {
985                 ast_mutex_lock(&sc->lock);
986                 ast_slinfactory_flush(&sc->factory);
987                 sc->talking = 0;
988                 ast_mutex_unlock(&sc->lock);
989
990                 /* Notify that we are no longer talking. */
991                 ast_bridge_channel_notify_talking(bridge_channel, 0);
992         }
993 }
994
995 /*!
996  * \internal
997  * \brief Check for voice status updates.
998  * \since 13.20.0
999  *
1000  * \param bridge Which bridge we are in
1001  * \param bridge_channel Which channel we are checking
1002  *
1003  * \return Nothing
1004  */
1005 static void softmix_bridge_check_voice(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
1006 {
1007         if (bridge_channel->features->mute) {
1008                 /*
1009                  * We were muted while we were talking.
1010                  *
1011                  * Immediately stop contributing to mixing
1012                  * and report no longer talking.
1013                  */
1014                 clear_talking(bridge_channel);
1015         }
1016 }
1017
1018 static int remove_all_original_streams(struct ast_stream_topology *dest,
1019         const struct ast_stream_topology *source,
1020         const struct ast_stream_topology *original)
1021 {
1022         int i;
1023
1024         for (i = 0; i < ast_stream_topology_get_count(source); ++i) {
1025                 struct ast_stream *stream;
1026                 int original_index;
1027
1028                 stream = ast_stream_topology_get_stream(source, i);
1029
1030                 /* Mark the existing stream as removed so we get a new one, this will get
1031                  * reused on a subsequent renegotiation.
1032                  */
1033                 for (original_index = 0; original_index < ast_stream_topology_get_count(original); ++original_index) {
1034                         struct ast_stream *original_stream = ast_stream_topology_get_stream(original, original_index);
1035
1036                         if (!strcmp(ast_stream_get_name(stream), ast_stream_get_name(original_stream))) {
1037                                 struct ast_stream *removed;
1038
1039                                 /* Since the participant is still going to be in the bridge we
1040                                  * change the name so that routing does not attempt to route video
1041                                  * to this stream.
1042                                  */
1043                                 removed = ast_stream_clone(stream, "removed");
1044                                 if (!removed) {
1045                                         return -1;
1046                                 }
1047
1048                                 ast_stream_set_state(removed, AST_STREAM_STATE_REMOVED);
1049
1050                                 /* The destination topology can only ever contain the same, or more,
1051                                  * streams than the original so this is safe.
1052                                  */
1053                                 if (ast_stream_topology_set_stream(dest, original_index, removed)) {
1054                                         ast_stream_free(removed);
1055                                         return -1;
1056                                 }
1057
1058                                 break;
1059                         }
1060                 }
1061         }
1062
1063         return 0;
1064 }
1065
1066 static void sfu_topologies_on_source_change(struct ast_bridge_channel *source, struct ast_bridge_channels_list *participants)
1067 {
1068         struct ast_stream_topology *source_video = NULL;
1069         struct ast_bridge_channel *participant;
1070         int res;
1071
1072         source_video = ast_stream_topology_alloc();
1073         if (!source_video) {
1074                 return;
1075         }
1076
1077         ast_channel_lock(source->chan);
1078         res = append_source_streams(source_video, ast_channel_name(source->chan), ast_channel_get_stream_topology(source->chan));
1079         ast_channel_unlock(source->chan);
1080         if (res) {
1081                 goto cleanup;
1082         }
1083
1084         AST_LIST_TRAVERSE(participants, participant, entry) {
1085                 struct ast_stream_topology *original_topology;
1086                 struct softmix_channel *sc;
1087
1088                 if (participant == source) {
1089                         continue;
1090                 }
1091
1092                 sc = participant->tech_pvt;
1093
1094                 original_topology = ast_stream_topology_clone(sc->topology);
1095                 if (!original_topology) {
1096                         goto cleanup;
1097                 }
1098
1099                 /* We add all the source streams back in, if any removed streams are already present they will
1100                  * get used first followed by appending new ones.
1101                  */
1102                 if (append_all_streams(sc->topology, source_video)) {
1103                         ast_stream_topology_free(original_topology);
1104                         goto cleanup;
1105                 }
1106
1107                 /* And the original existing streams get marked as removed. This causes the remote side to see
1108                  * a new stream for the source streams.
1109                  */
1110                 if (remove_all_original_streams(sc->topology, source_video, original_topology)) {
1111                         ast_stream_topology_free(original_topology);
1112                         goto cleanup;
1113                 }
1114
1115                 ast_channel_request_stream_topology_change(participant->chan, sc->topology, NULL);
1116                 ast_stream_topology_free(original_topology);
1117         }
1118
1119 cleanup:
1120         ast_stream_topology_free(source_video);
1121 }
1122
1123 /*!
1124  * \internal
1125  * \brief Determine what to do with a text frame.
1126  * \since 13.22.0
1127  * \since 15.5.0
1128  *
1129  * \param bridge Which bridge is getting the frame
1130  * \param bridge_channel Which channel is writing the frame.
1131  * \param frame What is being written.
1132  *
1133  * \return Nothing
1134  */
1135 static void softmix_bridge_write_text(struct ast_bridge *bridge,
1136         struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
1137 {
1138         if (DEBUG_ATLEAST(1)) {
1139                 struct ast_msg_data *msg = frame->data.ptr;
1140                 char frame_type[64];
1141
1142                 ast_frame_type2str(frame->frametype, frame_type, sizeof(frame_type));
1143
1144                 if (frame->frametype == AST_FRAME_TEXT_DATA) {
1145                         ast_log(LOG_DEBUG, "Received %s frame from '%s:%s': %s\n", frame_type,
1146                                 ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_FROM),
1147                                 ast_channel_name(bridge_channel->chan),
1148                                 ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_BODY));
1149                 } else {
1150                         ast_log(LOG_DEBUG, "Received %s frame from '%s': %.*s\n", frame_type,
1151                                 ast_channel_name(bridge_channel->chan), frame->datalen,
1152                                 (char *)frame->data.ptr);
1153                 }
1154         }
1155
1156         ast_bridge_queue_everyone_else(bridge, bridge_channel, frame);
1157 }
1158
1159 /*!
1160  * \internal
1161  * \brief Determine what to do with a control frame.
1162  * \since 12.0.0
1163  *
1164  * \param bridge Which bridge is getting the frame
1165  * \param bridge_channel Which channel is writing the frame.
1166  * \param frame What is being written.
1167  *
1168  * \retval 0 Frame accepted into the bridge.
1169  * \retval -1 Frame needs to be deferred.
1170  */
1171 static int softmix_bridge_write_control(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
1172 {
1173         struct softmix_bridge_data *softmix_data = bridge->tech_pvt;
1174
1175         /*
1176          * XXX Softmix needs to use channel roles to determine what to
1177          * do with control frames.
1178          */
1179
1180         switch (frame->subclass.integer) {
1181         case AST_CONTROL_HOLD:
1182                 /*
1183                  * Doing anything for holds in a conference bridge could be considered a bit
1184                  * odd. That being said, in most cases one would probably want the talking
1185                  * flag cleared when 'hold' is pressed by the remote endpoint, so go ahead
1186                  * and do that here. However, that is all we'll do. Meaning if for some reason
1187                  * the endpoint continues to send audio frames despite pressing 'hold' talking
1188                  * will once again be detected for that channel.
1189                  */
1190                 clear_talking(bridge_channel);
1191                 break;
1192         case AST_CONTROL_VIDUPDATE:
1193                 if (!bridge->softmix.video_mode.video_update_discard ||
1194                         ast_tvdiff_ms(ast_tvnow(), softmix_data->last_video_update) > bridge->softmix.video_mode.video_update_discard) {
1195                         ast_bridge_queue_everyone_else(bridge, NULL, frame);
1196                         softmix_data->last_video_update = ast_tvnow();
1197                 }
1198                 break;
1199         case AST_CONTROL_STREAM_TOPOLOGY_SOURCE_CHANGED:
1200                 if (bridge->softmix.video_mode.mode == AST_BRIDGE_VIDEO_MODE_SFU) {
1201                         sfu_topologies_on_source_change(bridge_channel, &bridge->channels);
1202                 }
1203                 break;
1204         default:
1205                 break;
1206         }
1207
1208         return 0;
1209 }
1210
1211 /*!
1212  * \internal
1213  * \brief Determine what to do with an RTCP frame.
1214  * \since 15.4.0
1215  *
1216  * \param bridge Which bridge is getting the frame
1217  * \param bridge_channel Which channel is writing the frame.
1218  * \param frame What is being written.
1219  */
1220 static void softmix_bridge_write_rtcp(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
1221 {
1222         struct ast_rtp_rtcp_feedback *feedback = frame->data.ptr;
1223         struct softmix_channel *sc = bridge_channel->tech_pvt;
1224
1225         /* We only care about REMB reports right now. In the future we may be able to use sender or
1226          * receiver reports to further tweak things, but not yet.
1227          */
1228         if (frame->subclass.integer != AST_RTP_RTCP_PSFB || feedback->fmt != AST_RTP_RTCP_FMT_REMB ||
1229                 bridge->softmix.video_mode.mode != AST_BRIDGE_VIDEO_MODE_SFU ||
1230                 !bridge->softmix.video_mode.mode_data.sfu_data.remb_send_interval) {
1231                 return;
1232         }
1233
1234         /* REMB is the total estimated maximum bitrate across all streams within the session, so we store
1235          * only the latest report and use it everywhere.
1236          */
1237         ast_mutex_lock(&sc->lock);
1238         sc->remb = feedback->remb;
1239         ast_mutex_unlock(&sc->lock);
1240
1241         return;
1242 }
1243
1244 /*!
1245  * \internal
1246  * \brief Determine what to do with a frame written into the bridge.
1247  * \since 12.0.0
1248  *
1249  * \param bridge Which bridge is getting the frame
1250  * \param bridge_channel Which channel is writing the frame.
1251  * \param frame What is being written.
1252  *
1253  * \retval 0 Frame accepted into the bridge.
1254  * \retval -1 Frame needs to be deferred.
1255  *
1256  * \note On entry, bridge is already locked.
1257  */
1258 static int softmix_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
1259 {
1260         int res = 0;
1261
1262         if (!bridge->tech_pvt || !bridge_channel || !bridge_channel->tech_pvt) {
1263                 /* "Accept" the frame and discard it. */
1264                 return 0;
1265         }
1266
1267         /*
1268          * XXX Softmix needs to use channel roles to determine who gets
1269          * what frame.  Possible roles: announcer, recorder, agent,
1270          * supervisor.
1271          */
1272         switch (frame->frametype) {
1273         case AST_FRAME_NULL:
1274                 /* "Accept" the frame and discard it. */
1275                 softmix_bridge_check_voice(bridge, bridge_channel);
1276                 break;
1277         case AST_FRAME_DTMF_BEGIN:
1278         case AST_FRAME_DTMF_END:
1279                 res = ast_bridge_queue_everyone_else(bridge, bridge_channel, frame);
1280                 break;
1281         case AST_FRAME_VOICE:
1282                 softmix_bridge_write_voice(bridge, bridge_channel, frame);
1283                 break;
1284         case AST_FRAME_VIDEO:
1285                 softmix_bridge_write_video(bridge, bridge_channel, frame);
1286                 break;
1287         case AST_FRAME_TEXT:
1288         case AST_FRAME_TEXT_DATA:
1289                 softmix_bridge_write_text(bridge, bridge_channel, frame);
1290                 break;
1291         case AST_FRAME_CONTROL:
1292                 res = softmix_bridge_write_control(bridge, bridge_channel, frame);
1293                 break;
1294         case AST_FRAME_RTCP:
1295                 softmix_bridge_write_rtcp(bridge, bridge_channel, frame);
1296                 break;
1297         case AST_FRAME_BRIDGE_ACTION:
1298                 res = ast_bridge_queue_everyone_else(bridge, bridge_channel, frame);
1299                 break;
1300         case AST_FRAME_BRIDGE_ACTION_SYNC:
1301                 ast_log(LOG_ERROR, "Synchronous bridge action written to a softmix bridge.\n");
1302                 ast_assert(0);
1303         default:
1304                 ast_debug(3, "Frame type %u unsupported\n", frame->frametype);
1305                 /* "Accept" the frame and discard it. */
1306                 break;
1307         }
1308
1309         return res;
1310 }
1311
1312 static void remb_collect_report(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel,
1313         struct softmix_bridge_data *softmix_data, struct softmix_channel *sc)
1314 {
1315         int i;
1316         unsigned int bitrate;
1317
1318         /* If there are no video sources that we are a receiver of then we have noone to
1319          * report REMB to.
1320          */
1321         if (!AST_VECTOR_SIZE(&sc->video_sources)) {
1322                 return;
1323         }
1324
1325         /* We evenly divide the available maximum bitrate across the video sources
1326          * to this receiver so each source gets an equal slice.
1327          */
1328         bitrate = (sc->remb.br_mantissa << sc->remb.br_exp) / AST_VECTOR_SIZE(&sc->video_sources);
1329
1330         /* If this receiver has no bitrate yet ignore it */
1331         if (!bitrate) {
1332                 return;
1333         }
1334
1335         for (i = 0; i < AST_VECTOR_SIZE(&sc->video_sources); ++i) {
1336                 struct softmix_remb_collector *collector;
1337
1338                 /* The collector will always exist if a video source is in our list */
1339                 collector = AST_VECTOR_GET(&softmix_data->remb_collectors, AST_VECTOR_GET(&sc->video_sources, i));
1340
1341                 if (!collector->bitrate) {
1342                         collector->bitrate = bitrate;
1343                         continue;
1344                 }
1345
1346                 switch (bridge->softmix.video_mode.mode_data.sfu_data.remb_behavior) {
1347                 case AST_BRIDGE_VIDEO_SFU_REMB_AVERAGE:
1348                         collector->bitrate = (collector->bitrate + bitrate) / 2;
1349                         break;
1350                 case AST_BRIDGE_VIDEO_SFU_REMB_LOWEST:
1351                         if (bitrate < collector->bitrate) {
1352                                 collector->bitrate = bitrate;
1353                         }
1354                         break;
1355                 case AST_BRIDGE_VIDEO_SFU_REMB_HIGHEST:
1356                         if (bitrate > collector->bitrate) {
1357                                 collector->bitrate = bitrate;
1358                         }
1359                         break;
1360                 }
1361         }
1362
1363         /* After the report is integrated we reset this to 0 in case they stop producing
1364          * REMB reports.
1365          */
1366         sc->remb.br_mantissa = 0;
1367         sc->remb.br_exp = 0;
1368 }
1369
1370 static void remb_send_report(struct ast_bridge_channel *bridge_channel, struct softmix_channel *sc)
1371 {
1372         int i;
1373
1374         if (!sc->remb_collector) {
1375                 return;
1376         }
1377
1378         /* We always do this calculation as even when the bitrate is zero the browser
1379          * still prefers it to be accurate instead of lying.
1380          */
1381         sc->remb_collector->feedback.remb.br_mantissa = sc->remb_collector->bitrate;
1382         sc->remb_collector->feedback.remb.br_exp = 0;
1383
1384         /* The mantissa only has 18 bits available, so while it exceeds them we bump
1385          * up the exp.
1386          */
1387         while (sc->remb_collector->feedback.remb.br_mantissa > 0x3ffff) {
1388                 sc->remb_collector->feedback.remb.br_mantissa = sc->remb_collector->feedback.remb.br_mantissa >> 1;
1389                 sc->remb_collector->feedback.remb.br_exp++;
1390         }
1391
1392         for (i = 0; i < AST_VECTOR_SIZE(&bridge_channel->stream_map.to_bridge); ++i) {
1393                 int bridge_num = AST_VECTOR_GET(&bridge_channel->stream_map.to_bridge, i);
1394
1395                 /* If this stream is not being provided to the bridge there can be no receivers of it
1396                  * so therefore no REMB reports.
1397                  */
1398                 if (bridge_num == -1) {
1399                         continue;
1400                 }
1401
1402                 /* We need to update the frame with this stream, or else it won't be
1403                  * properly routed. We don't use the actual channel stream identifier as
1404                  * the bridging core will do the translation from bridge stream identifier to
1405                  * channel stream identifier.
1406                  */
1407                 sc->remb_collector->frame.stream_num = bridge_num;
1408                 ast_bridge_channel_queue_frame(bridge_channel, &sc->remb_collector->frame);
1409         }
1410
1411         sc->remb_collector->bitrate = 0;
1412 }
1413
1414 static void gather_softmix_stats(struct softmix_stats *stats,
1415         const struct softmix_bridge_data *softmix_data,
1416         struct ast_bridge_channel *bridge_channel)
1417 {
1418         int channel_native_rate;
1419
1420         /* Gather stats about channel sample rates. */
1421         ast_channel_lock(bridge_channel->chan);
1422         channel_native_rate = MAX(SOFTMIX_MIN_SAMPLE_RATE,
1423                 ast_format_get_sample_rate(ast_channel_rawreadformat(bridge_channel->chan)));
1424         ast_channel_unlock(bridge_channel->chan);
1425
1426         if (stats->highest_supported_rate < channel_native_rate) {
1427                 stats->highest_supported_rate = channel_native_rate;
1428         }
1429         if (softmix_data->internal_rate < channel_native_rate) {
1430                 int i;
1431
1432                 for (i = 0; i < ARRAY_LEN(stats->sample_rates); i++) {
1433                         if (stats->sample_rates[i] == channel_native_rate) {
1434                                 stats->num_channels[i]++;
1435                                 break;
1436                         } else if (!stats->sample_rates[i]) {
1437                                 stats->sample_rates[i] = channel_native_rate;
1438                                 stats->num_channels[i]++;
1439                                 break;
1440                         }
1441                 }
1442                 stats->num_above_internal_rate++;
1443         } else if (softmix_data->internal_rate == channel_native_rate) {
1444                 stats->num_at_internal_rate++;
1445         }
1446 }
1447
1448 /*!
1449  * \internal
1450  * \brief Analyse mixing statistics and change bridges internal rate
1451  * if necessary.
1452  *
1453  * \retval 0, no changes to internal rate
1454  * \retval 1, internal rate was changed, update all the channels on the next mixing iteration.
1455  */
1456 static unsigned int analyse_softmix_stats(struct softmix_stats *stats,
1457                 struct softmix_bridge_data *softmix_data, int binaural_active)
1458 {
1459         int i;
1460
1461         if (binaural_active) {
1462                 stats->locked_rate = SOFTMIX_BINAURAL_SAMPLE_RATE;
1463         }
1464
1465         /*
1466          * Re-adjust the internal bridge sample rate if
1467          * 1. The bridge's internal sample rate is locked in at a sample
1468          *    rate other than the current sample rate being used.
1469          * 2. two or more channels support a higher sample rate
1470          * 3. no channels support the current sample rate or a higher rate
1471          */
1472         if (stats->locked_rate) {
1473                 /* if the rate is locked by the bridge, only update it if it differs
1474                  * from the current rate we are using. */
1475                 if (softmix_data->internal_rate != stats->locked_rate) {
1476                         ast_debug(1, "Locking at new rate.  Bridge changed from %u to %u.\n",
1477                                 softmix_data->internal_rate, stats->locked_rate);
1478                         softmix_data->internal_rate = stats->locked_rate;
1479                         return 1;
1480                 }
1481         } else if (stats->num_above_internal_rate >= 2) {
1482                 /* the highest rate is just used as a starting point */
1483                 unsigned int best_rate = stats->highest_supported_rate;
1484                 int best_index = -1;
1485
1486                 for (i = 0; i < ARRAY_LEN(stats->num_channels); i++) {
1487                         if (stats->num_channels[i]) {
1488                                 break;
1489                         }
1490                         if (2 <= stats->num_channels[i]) {
1491                                 /* Two or more channels support this rate. */
1492                                 if (best_index == -1
1493                                         || stats->sample_rates[best_index] < stats->sample_rates[i]) {
1494                                         /*
1495                                          * best_rate starts out being the first sample rate
1496                                          * greater than the internal sample rate that two or
1497                                          * more channels support.
1498                                          *
1499                                          * or
1500                                          *
1501                                          * There are multiple rates above the internal rate
1502                                          * and this rate is higher than the previous rate two
1503                                          * or more channels support.
1504                                          */
1505                                         best_rate = stats->sample_rates[i];
1506                                         best_index = i;
1507                                 }
1508                         } else if (best_index == -1) {
1509                                 /*
1510                                  * It is possible that multiple channels exist with native sample
1511                                  * rates above the internal sample rate, but none of those channels
1512                                  * have the same rate in common.  In this case, the lowest sample
1513                                  * rate among those channels is picked. Over time as additional
1514                                  * statistic runs are made the internal sample rate number will
1515                                  * adjust to the most optimal sample rate, but it may take multiple
1516                                  * iterations.
1517                                  */
1518                                 best_rate = MIN(best_rate, stats->sample_rates[i]);
1519                         }
1520                 }
1521
1522                 ast_debug(1, "Multiple above internal rate.  Bridge changed from %u to %u.\n",
1523                         softmix_data->internal_rate, best_rate);
1524                 softmix_data->internal_rate = best_rate;
1525                 return 1;
1526         } else if (!stats->num_at_internal_rate && !stats->num_above_internal_rate) {
1527                 /* In this case, the highest supported rate is actually lower than the internal rate */
1528                 ast_debug(1, "All below internal rate.  Bridge changed from %u to %u.\n",
1529                         softmix_data->internal_rate, stats->highest_supported_rate);
1530                 softmix_data->internal_rate = stats->highest_supported_rate;
1531                 return 1;
1532         }
1533         return 0;
1534 }
1535
1536 static int softmix_mixing_array_init(struct softmix_mixing_array *mixing_array,
1537                 unsigned int starting_num_entries, unsigned int binaural_active)
1538 {
1539         memset(mixing_array, 0, sizeof(*mixing_array));
1540         mixing_array->max_num_entries = starting_num_entries;
1541         if (!(mixing_array->buffers = ast_calloc(mixing_array->max_num_entries, sizeof(int16_t *)))) {
1542                 ast_log(LOG_NOTICE, "Failed to allocate softmix mixing structure.\n");
1543                 return -1;
1544         }
1545         if (binaural_active) {
1546                 if (!(mixing_array->chan_pairs = ast_calloc(mixing_array->max_num_entries,
1547                                 sizeof(struct convolve_channel_pair *)))) {
1548                         ast_log(LOG_NOTICE, "Failed to allocate softmix mixing structure.\n");
1549                         return -1;
1550                 }
1551         }
1552         return 0;
1553 }
1554
1555 static void softmix_mixing_array_destroy(struct softmix_mixing_array *mixing_array,
1556                 unsigned int binaural_active)
1557 {
1558         ast_free(mixing_array->buffers);
1559         if (binaural_active) {
1560                 ast_free(mixing_array->chan_pairs);
1561         }
1562 }
1563
1564 static int softmix_mixing_array_grow(struct softmix_mixing_array *mixing_array,
1565                 unsigned int num_entries, unsigned int binaural_active)
1566 {
1567         int16_t **tmp;
1568
1569         /* give it some room to grow since memory is cheap but allocations can be expensive */
1570         mixing_array->max_num_entries = num_entries;
1571         if (!(tmp = ast_realloc(mixing_array->buffers, (mixing_array->max_num_entries * sizeof(int16_t *))))) {
1572                 ast_log(LOG_NOTICE, "Failed to re-allocate softmix mixing structure.\n");
1573                 return -1;
1574         }
1575         mixing_array->buffers = tmp;
1576
1577         if (binaural_active) {
1578                 struct convolve_channel_pair **tmp2;
1579                 if (!(tmp2 = ast_realloc(mixing_array->chan_pairs,
1580                                 (mixing_array->max_num_entries * sizeof(struct convolve_channel_pair *))))) {
1581                         ast_log(LOG_NOTICE, "Failed to re-allocate softmix mixing structure.\n");
1582                         return -1;
1583                 }
1584                 mixing_array->chan_pairs = tmp2;
1585         }
1586         return 0;
1587 }
1588
1589 /*!
1590  * \brief Mixing loop.
1591  *
1592  * \retval 0 on success
1593  * \retval -1 on failure
1594  */
1595 static int softmix_mixing_loop(struct ast_bridge *bridge)
1596 {
1597         struct softmix_stats stats = { { 0 }, };
1598         struct softmix_mixing_array mixing_array;
1599         struct softmix_bridge_data *softmix_data = bridge->tech_pvt;
1600         struct ast_timer *timer;
1601         struct softmix_translate_helper trans_helper;
1602         int16_t buf[MAX_DATALEN];
1603 #ifdef BINAURAL_RENDERING
1604         int16_t bin_buf[MAX_DATALEN];
1605         int16_t ann_buf[MAX_DATALEN];
1606 #endif
1607         unsigned int stat_iteration_counter = 0; /* counts down, gather stats at zero and reset. */
1608         int timingfd;
1609         int update_all_rates = 0; /* set this when the internal sample rate has changed */
1610         unsigned int idx;
1611         unsigned int x;
1612         int res = -1;
1613
1614         timer = softmix_data->timer;
1615         timingfd = ast_timer_fd(timer);
1616         softmix_translate_helper_init(&trans_helper, softmix_data->internal_rate);
1617         ast_timer_set_rate(timer, (1000 / softmix_data->internal_mixing_interval));
1618
1619         /* Give the mixing array room to grow, memory is cheap but allocations are expensive. */
1620         if (softmix_mixing_array_init(&mixing_array, bridge->num_channels + 10,
1621                         bridge->softmix.binaural_active)) {
1622                 goto softmix_cleanup;
1623         }
1624
1625         /*
1626          * XXX Softmix needs to use channel roles to determine who gets
1627          * what audio mixed.
1628          */
1629         while (!softmix_data->stop && bridge->num_active) {
1630                 struct ast_bridge_channel *bridge_channel;
1631                 int timeout = -1;
1632                 struct ast_format *cur_slin = ast_format_cache_get_slin_by_rate(softmix_data->internal_rate);
1633                 unsigned int softmix_samples = SOFTMIX_SAMPLES(softmix_data->internal_rate, softmix_data->internal_mixing_interval);
1634                 unsigned int softmix_datalen = SOFTMIX_DATALEN(softmix_data->internal_rate, softmix_data->internal_mixing_interval);
1635                 int remb_update = 0;
1636
1637                 if (softmix_datalen > MAX_DATALEN) {
1638                         /* This should NEVER happen, but if it does we need to know about it. Almost
1639                          * all the memcpys used during this process depend on this assumption.  Rather
1640                          * than checking this over and over again through out the code, this single
1641                          * verification is done on each iteration. */
1642                         ast_log(LOG_WARNING,
1643                                 "Bridge %s: Conference mixing error, requested mixing length greater than mixing buffer.\n",
1644                                 bridge->uniqueid);
1645                         goto softmix_cleanup;
1646                 }
1647
1648                 /* Grow the mixing array buffer as participants are added. */
1649                 if (mixing_array.max_num_entries < bridge->num_channels
1650                         && softmix_mixing_array_grow(&mixing_array, bridge->num_channels + 5,
1651                                         bridge->softmix.binaural_active)) {
1652                         goto softmix_cleanup;
1653                 }
1654
1655                 /* init the number of buffers stored in the mixing array to 0.
1656                  * As buffers are added for mixing, this number is incremented. */
1657                 mixing_array.used_entries = 0;
1658
1659                 /* These variables help determine if a rate change is required */
1660                 if (!stat_iteration_counter) {
1661                         memset(&stats, 0, sizeof(stats));
1662                         stats.locked_rate = bridge->softmix.internal_sample_rate;
1663                 }
1664
1665                 /* If the sample rate has changed, update the translator helper */
1666                 if (update_all_rates) {
1667                         softmix_translate_helper_change_rate(&trans_helper, softmix_data->internal_rate);
1668                 }
1669
1670 #ifdef BINAURAL_RENDERING
1671                 check_binaural_position_change(bridge, softmix_data);
1672 #endif
1673
1674                 /* If we need to do a REMB update to all video sources then do so */
1675                 if (bridge->softmix.video_mode.mode == AST_BRIDGE_VIDEO_MODE_SFU &&
1676                         bridge->softmix.video_mode.mode_data.sfu_data.remb_send_interval &&
1677                         ast_tvdiff_ms(ast_tvnow(), softmix_data->last_remb_update) > bridge->softmix.video_mode.mode_data.sfu_data.remb_send_interval) {
1678                         remb_update = 1;
1679                         softmix_data->last_remb_update = ast_tvnow();
1680                 }
1681
1682                 /* Go through pulling audio from each factory that has it available */
1683                 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
1684                         struct softmix_channel *sc = bridge_channel->tech_pvt;
1685
1686                         if (!sc) {
1687                                 /* This channel failed to join successfully. */
1688                                 continue;
1689                         }
1690
1691                         /* Update the sample rate to match the bridge's native sample rate if necessary. */
1692                         if (update_all_rates) {
1693                                 set_softmix_bridge_data(softmix_data->internal_rate,
1694                                                 softmix_data->internal_mixing_interval, bridge_channel, 1, -1, -1, -1);
1695                         }
1696
1697                         /* If stat_iteration_counter is 0, then collect statistics during this mixing interation */
1698                         if (!stat_iteration_counter) {
1699                                 gather_softmix_stats(&stats, softmix_data, bridge_channel);
1700                         }
1701
1702                         /* if the channel is suspended, don't check for audio, but still gather stats */
1703                         if (bridge_channel->suspended) {
1704                                 continue;
1705                         }
1706
1707                         /* Try to get audio from the factory if available */
1708                         ast_mutex_lock(&sc->lock);
1709                         if ((mixing_array.buffers[mixing_array.used_entries] = softmix_process_read_audio(sc, softmix_samples))) {
1710 #ifdef BINAURAL_RENDERING
1711                                 add_binaural_mixing(bridge, softmix_data, softmix_samples, &mixing_array, sc,
1712                                                 ast_channel_name(bridge_channel->chan));
1713 #endif
1714                                 mixing_array.used_entries++;
1715                         }
1716                         if (remb_update) {
1717                                 remb_collect_report(bridge, bridge_channel, softmix_data, sc);
1718                         }
1719                         ast_mutex_unlock(&sc->lock);
1720                 }
1721
1722                 /* mix it like crazy (non binaural channels)*/
1723                 memset(buf, 0, softmix_datalen);
1724                 for (idx = 0; idx < mixing_array.used_entries; ++idx) {
1725                         for (x = 0; x < softmix_samples; ++x) {
1726                                 ast_slinear_saturated_add(buf + x, mixing_array.buffers[idx] + x);
1727                         }
1728                 }
1729
1730 #ifdef BINAURAL_RENDERING
1731                 binaural_mixing(bridge, softmix_data, &mixing_array, bin_buf, ann_buf);
1732 #endif
1733
1734                 /* Next step go through removing the channel's own audio and creating a good frame... */
1735                 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
1736                         struct softmix_channel *sc = bridge_channel->tech_pvt;
1737
1738                         if (!sc || bridge_channel->suspended) {
1739                                 /* This channel failed to join successfully or is suspended. */
1740                                 continue;
1741                         }
1742
1743                         ast_mutex_lock(&sc->lock);
1744
1745                         /* Make SLINEAR write frame from local buffer */
1746                         ao2_t_replace(sc->write_frame.subclass.format, cur_slin,
1747                                 "Replace softmix channel slin format");
1748 #ifdef BINAURAL_RENDERING
1749                         if (bridge->softmix.binaural_active && softmix_data->convolve.binaural_active
1750                                         && sc->binaural) {
1751                                 create_binaural_frame(bridge_channel, sc, bin_buf, ann_buf, softmix_datalen,
1752                                                 softmix_samples, buf);
1753                         } else
1754 #endif
1755                         {
1756                                 sc->write_frame.datalen = softmix_datalen;
1757                                 sc->write_frame.samples = softmix_samples;
1758                                 memcpy(sc->final_buf, buf, softmix_datalen);
1759                         }
1760                         /* process the softmix channel's new write audio */
1761                         softmix_process_write_audio(&trans_helper,
1762                                         ast_channel_rawwriteformat(bridge_channel->chan), sc,
1763                                         softmix_data->default_sample_size);
1764
1765                         ast_mutex_unlock(&sc->lock);
1766
1767                         /* A frame is now ready for the channel. */
1768                         ast_bridge_channel_queue_frame(bridge_channel, &sc->write_frame);
1769
1770                         if (remb_update) {
1771                                 remb_send_report(bridge_channel, sc);
1772                         }
1773                 }
1774
1775                 update_all_rates = 0;
1776                 if (!stat_iteration_counter) {
1777                         update_all_rates = analyse_softmix_stats(&stats, softmix_data,
1778                                         bridge->softmix.binaural_active);
1779                         stat_iteration_counter = SOFTMIX_STAT_INTERVAL;
1780                 }
1781                 stat_iteration_counter--;
1782
1783                 ast_bridge_unlock(bridge);
1784                 /* cleanup any translation frame data from the previous mixing iteration. */
1785                 softmix_translate_helper_cleanup(&trans_helper);
1786                 /* Wait for the timing source to tell us to wake up and get things done */
1787                 ast_waitfor_n_fd(&timingfd, 1, &timeout, NULL);
1788                 if (ast_timer_ack(timer, 1) < 0) {
1789                         ast_log(LOG_ERROR, "Bridge %s: Failed to acknowledge timer in softmix.\n",
1790                                 bridge->uniqueid);
1791                         ast_bridge_lock(bridge);
1792                         goto softmix_cleanup;
1793                 }
1794                 ast_bridge_lock(bridge);
1795
1796                 /* make sure to detect mixing interval changes if they occur. */
1797                 if (bridge->softmix.internal_mixing_interval
1798                         && (bridge->softmix.internal_mixing_interval != softmix_data->internal_mixing_interval)) {
1799                         softmix_data->internal_mixing_interval = bridge->softmix.internal_mixing_interval;
1800                         ast_timer_set_rate(timer, (1000 / softmix_data->internal_mixing_interval));
1801                         update_all_rates = 1; /* if the interval changes, the rates must be adjusted as well just to be notified new interval.*/
1802                 }
1803         }
1804
1805         res = 0;
1806
1807 softmix_cleanup:
1808         softmix_translate_helper_destroy(&trans_helper);
1809         softmix_mixing_array_destroy(&mixing_array, bridge->softmix.binaural_active);
1810         return res;
1811 }
1812
1813 /*!
1814  * \internal
1815  * \brief Mixing thread.
1816  * \since 12.0.0
1817  *
1818  * \note The thread does not have its own reference to the
1819  * bridge.  The lifetime of the thread is tied to the lifetime
1820  * of the mixing technology association with the bridge.
1821  */
1822 static void *softmix_mixing_thread(void *data)
1823 {
1824         struct softmix_bridge_data *softmix_data = data;
1825         struct ast_bridge *bridge = softmix_data->bridge;
1826
1827         ast_bridge_lock(bridge);
1828         if (bridge->callid) {
1829                 ast_callid_threadassoc_add(bridge->callid);
1830         }
1831
1832         ast_debug(1, "Bridge %s: starting mixing thread\n", bridge->uniqueid);
1833
1834         while (!softmix_data->stop) {
1835                 if (!bridge->num_active) {
1836                         /* Wait for something to happen to the bridge. */
1837                         ast_bridge_unlock(bridge);
1838                         ast_mutex_lock(&softmix_data->lock);
1839                         if (!softmix_data->stop) {
1840                                 ast_cond_wait(&softmix_data->cond, &softmix_data->lock);
1841                         }
1842                         ast_mutex_unlock(&softmix_data->lock);
1843                         ast_bridge_lock(bridge);
1844                         continue;
1845                 }
1846
1847                 if (bridge->softmix.binaural_active && !softmix_data->binaural_init) {
1848 #ifndef BINAURAL_RENDERING
1849                         ast_bridge_lock(bridge);
1850                         bridge->softmix.binaural_active = 0;
1851                         ast_bridge_unlock(bridge);
1852                         ast_log(LOG_WARNING, "Bridge: %s: Binaural rendering active by config but not "
1853                                         "compiled.\n", bridge->uniqueid);
1854 #else
1855                         /* Set and init binaural data if binaural is activated in the configuration. */
1856                         softmix_data->internal_rate = SOFTMIX_BINAURAL_SAMPLE_RATE;
1857                         softmix_data->default_sample_size = SOFTMIX_SAMPLES(softmix_data->internal_rate,
1858                                         softmix_data->internal_mixing_interval);
1859                         /* If init for binaural processing fails we will fall back to mono audio processing. */
1860                         if (init_convolve_data(&softmix_data->convolve, softmix_data->default_sample_size)
1861                                         == -1) {
1862                                 ast_bridge_lock(bridge);
1863                                 bridge->softmix.binaural_active = 0;
1864                                 ast_bridge_unlock(bridge);
1865                                 ast_log(LOG_ERROR, "Bridge: %s: Unable to allocate memory for "
1866                                                 "binaural processing,  Will only process mono audio.\n",
1867                                                 bridge->uniqueid);
1868                         }
1869                         softmix_data->binaural_init = 1;
1870 #endif
1871                 }
1872
1873                 if (softmix_mixing_loop(bridge)) {
1874                         /*
1875                          * A mixing error occurred.  Sleep and try again later so we
1876                          * won't flood the logs.
1877                          */
1878                         ast_bridge_unlock(bridge);
1879                         sleep(1);
1880                         ast_bridge_lock(bridge);
1881                 }
1882         }
1883
1884         ast_bridge_unlock(bridge);
1885
1886         ast_debug(1, "Bridge %s: stopping mixing thread\n", bridge->uniqueid);
1887
1888         return NULL;
1889 }
1890
1891 static void softmix_bridge_data_destroy(struct softmix_bridge_data *softmix_data)
1892 {
1893         if (softmix_data->timer) {
1894                 ast_timer_close(softmix_data->timer);
1895                 softmix_data->timer = NULL;
1896         }
1897         ast_mutex_destroy(&softmix_data->lock);
1898         ast_cond_destroy(&softmix_data->cond);
1899         AST_VECTOR_RESET(&softmix_data->remb_collectors, ao2_cleanup);
1900         AST_VECTOR_FREE(&softmix_data->remb_collectors);
1901         ast_free(softmix_data);
1902 }
1903
1904 /*! \brief Function called when a bridge is created */
1905 static int softmix_bridge_create(struct ast_bridge *bridge)
1906 {
1907         struct softmix_bridge_data *softmix_data;
1908
1909         softmix_data = ast_calloc(1, sizeof(*softmix_data));
1910         if (!softmix_data) {
1911                 return -1;
1912         }
1913         softmix_data->bridge = bridge;
1914         ast_mutex_init(&softmix_data->lock);
1915         ast_cond_init(&softmix_data->cond, NULL);
1916         softmix_data->timer = ast_timer_open();
1917         if (!softmix_data->timer) {
1918                 ast_log(AST_LOG_WARNING, "Failed to open timer for softmix bridge\n");
1919                 softmix_bridge_data_destroy(softmix_data);
1920                 return -1;
1921         }
1922         /* start at minimum rate, let it grow from there */
1923         softmix_data->internal_rate = SOFTMIX_MIN_SAMPLE_RATE;
1924         softmix_data->internal_mixing_interval = DEFAULT_SOFTMIX_INTERVAL;
1925
1926 #ifdef BINAURAL_RENDERING
1927         softmix_data->default_sample_size = SOFTMIX_SAMPLES(softmix_data->internal_rate,
1928                         softmix_data->internal_mixing_interval);
1929 #endif
1930
1931         AST_VECTOR_INIT(&softmix_data->remb_collectors, 0);
1932
1933         bridge->tech_pvt = softmix_data;
1934
1935         /* Start the mixing thread. */
1936         if (ast_pthread_create(&softmix_data->thread, NULL, softmix_mixing_thread,
1937                 softmix_data)) {
1938                 softmix_data->thread = AST_PTHREADT_NULL;
1939                 softmix_bridge_data_destroy(softmix_data);
1940                 bridge->tech_pvt = NULL;
1941                 return -1;
1942         }
1943
1944         return 0;
1945 }
1946
1947 /*!
1948  * \internal
1949  * \brief Request the softmix mixing thread stop.
1950  * \since 12.0.0
1951  *
1952  * \param bridge Which bridge is being stopped.
1953  *
1954  * \return Nothing
1955  */
1956 static void softmix_bridge_stop(struct ast_bridge *bridge)
1957 {
1958         struct softmix_bridge_data *softmix_data;
1959
1960         softmix_data = bridge->tech_pvt;
1961         if (!softmix_data) {
1962                 return;
1963         }
1964
1965         ast_mutex_lock(&softmix_data->lock);
1966         softmix_data->stop = 1;
1967         ast_mutex_unlock(&softmix_data->lock);
1968 }
1969
1970 /*! \brief Function called when a bridge is destroyed */
1971 static void softmix_bridge_destroy(struct ast_bridge *bridge)
1972 {
1973         struct softmix_bridge_data *softmix_data;
1974         pthread_t thread;
1975
1976         softmix_data = bridge->tech_pvt;
1977         if (!softmix_data) {
1978                 return;
1979         }
1980
1981         /* Stop the mixing thread. */
1982         ast_mutex_lock(&softmix_data->lock);
1983         softmix_data->stop = 1;
1984         ast_cond_signal(&softmix_data->cond);
1985         thread = softmix_data->thread;
1986         softmix_data->thread = AST_PTHREADT_NULL;
1987         ast_mutex_unlock(&softmix_data->lock);
1988         if (thread != AST_PTHREADT_NULL) {
1989                 ast_debug(1, "Bridge %s: Waiting for mixing thread to die.\n", bridge->uniqueid);
1990                 pthread_join(thread, NULL);
1991         }
1992 #ifdef BINAURAL_RENDERING
1993         free_convolve_data(&softmix_data->convolve);
1994 #endif
1995         softmix_bridge_data_destroy(softmix_data);
1996         bridge->tech_pvt = NULL;
1997 }
1998
1999 /*!
2000  * \brief Map a source stream to all of its destination streams.
2001  *
2002  * \param source_stream_name Name of the source stream
2003  * \param source_channel_name Name of channel where the source stream originates
2004  * \param bridge_stream_position The slot in the bridge where source video will come from
2005  * \param participants The bridge_channels in the bridge
2006  */
2007 static void map_source_to_destinations(const char *source_stream_name, const char *source_channel_name,
2008         size_t bridge_stream_position, struct ast_bridge_channels_list *participants)
2009 {
2010         struct ast_bridge_channel *participant;
2011
2012         AST_LIST_TRAVERSE(participants, participant, entry) {
2013                 int i;
2014                 struct ast_stream_topology *topology;
2015
2016                 if (!strcmp(source_channel_name, ast_channel_name(participant->chan))) {
2017                         continue;
2018                 }
2019
2020                 ast_bridge_channel_lock(participant);
2021                 ast_channel_lock(participant->chan);
2022                 topology = ast_channel_get_stream_topology(participant->chan);
2023
2024                 for (i = 0; i < ast_stream_topology_get_count(topology); ++i) {
2025                         struct ast_stream *stream;
2026
2027                         stream = ast_stream_topology_get_stream(topology, i);
2028                         if (is_video_dest(stream, source_channel_name, source_stream_name)) {
2029                                 struct softmix_channel *sc = participant->tech_pvt;
2030
2031                                 AST_VECTOR_REPLACE(&participant->stream_map.to_channel, bridge_stream_position, i);
2032                                 AST_VECTOR_APPEND(&sc->video_sources, bridge_stream_position);
2033                                 break;
2034                         }
2035                 }
2036                 ast_channel_unlock(participant->chan);
2037                 ast_bridge_channel_unlock(participant);
2038         }
2039 }
2040
2041 /*!
2042  * \brief Allocate a REMB collector
2043  *
2044  * \retval non-NULL success
2045  * \retval NULL failure
2046  */
2047 static struct softmix_remb_collector *remb_collector_alloc(void)
2048 {
2049         struct softmix_remb_collector *collector;
2050
2051         collector = ao2_alloc_options(sizeof(*collector), NULL, AO2_ALLOC_OPT_LOCK_NOLOCK);
2052         if (!collector) {
2053                 return NULL;
2054         }
2055
2056         collector->frame.frametype = AST_FRAME_RTCP;
2057         collector->frame.subclass.integer = AST_RTP_RTCP_PSFB;
2058         collector->feedback.fmt = AST_RTP_RTCP_FMT_REMB;
2059         collector->frame.data.ptr = &collector->feedback;
2060         collector->frame.datalen = sizeof(collector->feedback);
2061
2062         return collector;
2063 }
2064
2065 /*!
2066  * \brief Setup REMB collection for a particular bridge stream and channel.
2067  *
2068  * \param bridge The bridge
2069  * \param bridge_channel Channel that is collecting REMB information
2070  * \param bridge_stream_position The slot in the bridge where source video comes from
2071  */
2072 static void remb_enable_collection(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel,
2073         size_t bridge_stream_position)
2074 {
2075         struct softmix_channel *sc = bridge_channel->tech_pvt;
2076         struct softmix_bridge_data *softmix_data = bridge->tech_pvt;
2077
2078         if (!sc->remb_collector) {
2079                 sc->remb_collector = remb_collector_alloc();
2080                 if (!sc->remb_collector) {
2081                         /* This is not fatal. Things will still continue to work but we won't
2082                          * produce a REMB report to the sender.
2083                          */
2084                         return;
2085                 }
2086         }
2087
2088         if (AST_VECTOR_REPLACE(&softmix_data->remb_collectors, bridge_stream_position, ao2_bump(sc->remb_collector))) {
2089                 ao2_ref(sc->remb_collector, -1);
2090         }
2091 }
2092
2093 /*!
2094  * \brief stream_topology_changed callback
2095  *
2096  * For most video modes, nothing beyond the ordinary is required.
2097  * For the SFU case, though, we need to completely remap the streams
2098  * in order to ensure video gets directed where it is expected to go.
2099  *
2100  * \param bridge The bridge
2101  * \param bridge_channel Channel whose topology has changed
2102  */
2103 static void softmix_bridge_stream_topology_changed(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
2104 {
2105         struct softmix_bridge_data *softmix_data = bridge->tech_pvt;
2106         struct softmix_channel *sc;
2107         struct ast_bridge_channel *participant;
2108         struct ast_vector_int media_types;
2109         int nths[AST_MEDIA_TYPE_END] = {0};
2110         int idx;
2111
2112         switch (bridge->softmix.video_mode.mode) {
2113         case AST_BRIDGE_VIDEO_MODE_NONE:
2114         case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
2115         case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
2116         default:
2117                 ast_bridge_channel_stream_map(bridge_channel);
2118                 return;
2119         case AST_BRIDGE_VIDEO_MODE_SFU:
2120                 break;
2121         }
2122
2123         AST_VECTOR_INIT(&media_types, AST_MEDIA_TYPE_END);
2124
2125         /* The bridge stream identifiers may change, so reset the mapping for them.
2126          * When channels end up getting added back in they'll reuse their existing
2127          * collector and won't need to allocate a new one (unless they were just added).
2128          */
2129         for (idx = 0; idx < AST_VECTOR_SIZE(&softmix_data->remb_collectors); ++idx) {
2130                 ao2_cleanup(AST_VECTOR_GET(&softmix_data->remb_collectors, idx));
2131                 AST_VECTOR_REPLACE(&softmix_data->remb_collectors, idx, NULL);
2132         }
2133
2134         /* First traversal: re-initialize all of the participants' stream maps */
2135         AST_LIST_TRAVERSE(&bridge->channels, participant, entry) {
2136                 ast_bridge_channel_lock(participant);
2137
2138                 AST_VECTOR_RESET(&participant->stream_map.to_channel, AST_VECTOR_ELEM_CLEANUP_NOOP);
2139                 AST_VECTOR_RESET(&participant->stream_map.to_bridge, AST_VECTOR_ELEM_CLEANUP_NOOP);
2140
2141                 sc = participant->tech_pvt;
2142                 AST_VECTOR_RESET(&sc->video_sources, AST_VECTOR_ELEM_CLEANUP_NOOP);
2143
2144                 ast_bridge_channel_unlock(participant);
2145         }
2146
2147         /* Second traversal: Map specific video channels from their source to their destinations.
2148          *
2149          * This is similar to what is done in ast_stream_topology_map(),
2150          * except that video channels are handled differently.  Each video
2151          * source has it's own unique index on the bridge.  This way, a
2152          * particular channel's source video can be distributed to the
2153          * appropriate destination streams on the other channels.
2154          */
2155         AST_LIST_TRAVERSE(&bridge->channels, participant, entry) {
2156                 int i;
2157                 struct ast_stream_topology *topology;
2158
2159                 ast_bridge_channel_lock(participant);
2160                 ast_channel_lock(participant->chan);
2161
2162                 topology = ast_channel_get_stream_topology(participant->chan);
2163                 if (topology) {
2164                         /*
2165                          * Sigh.  We have to clone to avoid deadlock in
2166                          * map_source_to_destinations() because topology
2167                          * is not an ao2 object.
2168                          */
2169                         topology = ast_stream_topology_clone(topology);
2170                 }
2171                 if (!topology) {
2172                         /* Oh, my, we are in trouble. */
2173                         ast_channel_unlock(participant->chan);
2174                         ast_bridge_channel_unlock(participant);
2175                         continue;
2176                 }
2177
2178                 for (i = 0; i < ast_stream_topology_get_count(topology); ++i) {
2179                         struct ast_stream *stream = ast_stream_topology_get_stream(topology, i);
2180
2181                         if (is_video_source(stream)) {
2182                                 AST_VECTOR_APPEND(&media_types, AST_MEDIA_TYPE_VIDEO);
2183                                 AST_VECTOR_REPLACE(&participant->stream_map.to_bridge, i, AST_VECTOR_SIZE(&media_types) - 1);
2184                                 /*
2185                                  * There are cases where we need to bidirectionally send frames, such as for REMB reports
2186                                  * so we also map back to the channel.
2187                                  */
2188                                 AST_VECTOR_REPLACE(&participant->stream_map.to_channel, AST_VECTOR_SIZE(&media_types) - 1, i);
2189                                 remb_enable_collection(bridge, participant, AST_VECTOR_SIZE(&media_types) - 1);
2190                                 /*
2191                                  * Unlock the channel and participant to prevent
2192                                  * potential deadlock in map_source_to_destinations().
2193                                  */
2194                                 ast_channel_unlock(participant->chan);
2195                                 ast_bridge_channel_unlock(participant);
2196                                 map_source_to_destinations(ast_stream_get_name(stream), ast_channel_name(participant->chan),
2197                                         AST_VECTOR_SIZE(&media_types) - 1, &bridge->channels);
2198                                 ast_bridge_channel_lock(participant);
2199                                 ast_channel_lock(participant->chan);
2200                         } else if (is_video_dest(stream, NULL, NULL)) {
2201                                 /* We expect to never read media from video destination channels, but just
2202                                  * in case, we should set their to_bridge value to -1.
2203                                  */
2204                                 AST_VECTOR_REPLACE(&participant->stream_map.to_bridge, i, -1);
2205                         } else {
2206                                 /* XXX This is copied from ast_stream_topology_map(). This likely could
2207                                  * be factored out in some way
2208                                  */
2209                                 enum ast_media_type type = ast_stream_get_type(stream);
2210                                 int index = AST_VECTOR_GET_INDEX_NTH(&media_types, ++nths[type],
2211                                         type, AST_VECTOR_ELEM_DEFAULT_CMP);
2212
2213                                 if (index == -1) {
2214                                         AST_VECTOR_APPEND(&media_types, type);
2215                                         index = AST_VECTOR_SIZE(&media_types) - 1;
2216                                 }
2217
2218                                 AST_VECTOR_REPLACE(&participant->stream_map.to_bridge, i, index);
2219                                 AST_VECTOR_REPLACE(&participant->stream_map.to_channel, index, i);
2220                         }
2221                 }
2222
2223                 ast_stream_topology_free(topology);
2224
2225                 ast_channel_unlock(participant->chan);
2226                 ast_bridge_channel_unlock(participant);
2227         }
2228 }
2229
2230 static struct ast_bridge_technology softmix_bridge = {
2231         .name = "softmix",
2232         .capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX,
2233         .preference = AST_BRIDGE_PREFERENCE_BASE_MULTIMIX,
2234         .create = softmix_bridge_create,
2235         .stop = softmix_bridge_stop,
2236         .destroy = softmix_bridge_destroy,
2237         .join = softmix_bridge_join,
2238         .leave = softmix_bridge_leave,
2239         .unsuspend = softmix_bridge_unsuspend,
2240         .write = softmix_bridge_write,
2241         .stream_topology_changed = softmix_bridge_stream_topology_changed,
2242 };
2243
2244 #ifdef TEST_FRAMEWORK
2245 struct stream_parameters {
2246         const char *name;
2247         const char *formats;
2248         enum ast_media_type type;
2249 };
2250
2251 static struct ast_stream_topology *build_topology(const struct stream_parameters *params, size_t num_streams)
2252 {
2253         struct ast_stream_topology *topology;
2254         size_t i;
2255
2256         topology = ast_stream_topology_alloc();
2257         if (!topology) {
2258                 return NULL;
2259         }
2260
2261         for (i = 0; i < num_streams; ++i) {
2262                 RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
2263                 struct ast_stream *stream;
2264
2265                 caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
2266                 if (!caps) {
2267                         goto fail;
2268                 }
2269                 if (ast_format_cap_update_by_allow_disallow(caps, params[i].formats, 1) < 0) {
2270                         goto fail;
2271                 }
2272                 stream = ast_stream_alloc(params[i].name, params[i].type);
2273                 if (!stream) {
2274                         goto fail;
2275                 }
2276                 ast_stream_set_formats(stream, caps);
2277                 if (ast_stream_topology_append_stream(topology, stream) < 0) {
2278                         ast_stream_free(stream);
2279                         goto fail;
2280                 }
2281         }
2282
2283         return topology;
2284
2285 fail:
2286         ast_stream_topology_free(topology);
2287         return NULL;
2288 }
2289
2290 static int validate_stream(struct ast_test *test, struct ast_stream *stream,
2291         const struct stream_parameters *params)
2292 {
2293         struct ast_format_cap *stream_caps;
2294         struct ast_format_cap *params_caps;
2295
2296         if (ast_stream_get_type(stream) != params->type) {
2297                 ast_test_status_update(test, "Expected stream type '%s' but got type '%s'\n",
2298                         ast_codec_media_type2str(params->type),
2299                         ast_codec_media_type2str(ast_stream_get_type(stream)));
2300                 return -1;
2301         }
2302         if (strcmp(ast_stream_get_name(stream), params->name)) {
2303                 ast_test_status_update(test, "Expected stream name '%s' but got type '%s'\n",
2304                         params->name, ast_stream_get_name(stream));
2305                 return -1;
2306         }
2307
2308         stream_caps = ast_stream_get_formats(stream);
2309         params_caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
2310         if (!params_caps) {
2311                 ast_test_status_update(test, "Allocation error on capabilities\n");
2312                 return -1;
2313         }
2314         ast_format_cap_update_by_allow_disallow(params_caps, params->formats, 1);
2315
2316         if (!ast_format_cap_identical(stream_caps, params_caps)) {
2317                 ast_test_status_update(test, "Formats are not as expected on stream '%s'\n",
2318                         ast_stream_get_name(stream));
2319                 ao2_cleanup(params_caps);
2320                 return -1;
2321         }
2322
2323         ao2_cleanup(params_caps);
2324         return 0;
2325 }
2326
2327 static int validate_original_streams(struct ast_test *test, struct ast_stream_topology *topology,
2328         const struct stream_parameters *params, size_t num_streams)
2329 {
2330         int i;
2331
2332         if (ast_stream_topology_get_count(topology) < num_streams) {
2333                 ast_test_status_update(test, "Topology only has %d streams. Needs to have at least %zu\n",
2334                         ast_stream_topology_get_count(topology), num_streams);
2335                 return -1;
2336         }
2337
2338         for (i = 0; i < num_streams; ++i) {
2339                 if (validate_stream(test, ast_stream_topology_get_stream(topology, i), &params[i])) {
2340                         return -1;
2341                 }
2342         }
2343
2344         return 0;
2345 }
2346
2347 AST_TEST_DEFINE(sfu_append_source_streams)
2348 {
2349         enum ast_test_result_state res = AST_TEST_FAIL;
2350         static const struct stream_parameters bob_streams[] = {
2351                 { "bob_audio", "ulaw,alaw,g722,opus", AST_MEDIA_TYPE_AUDIO, },
2352                 { "bob_video", "h264,vp8", AST_MEDIA_TYPE_VIDEO, },
2353         };
2354         static const struct stream_parameters alice_streams[] = {
2355                 { "alice_audio", "ulaw,opus", AST_MEDIA_TYPE_AUDIO, },
2356                 { "alice_video", "vp8", AST_MEDIA_TYPE_VIDEO, },
2357         };
2358         static const struct stream_parameters alice_dest_stream = {
2359                 "softbridge_dest_PJSIP/Bob-00000001_bob_video", "h264,vp8", AST_MEDIA_TYPE_VIDEO,
2360         };
2361         static const struct stream_parameters bob_dest_stream = {
2362                 "softbridge_dest_PJSIP/Alice-00000000_alice_video", "vp8", AST_MEDIA_TYPE_VIDEO,
2363         };
2364         struct ast_stream_topology *topology_alice = NULL;
2365         struct ast_stream_topology *topology_bob = NULL;
2366
2367         switch (cmd) {
2368         case TEST_INIT:
2369                 info->name = "sfu_append_source_streams";
2370                 info->category = "/bridges/bridge_softmix/";
2371                 info->summary = "Test appending of video streams";
2372                 info->description =
2373                         "This tests does stuff.";
2374                 return AST_TEST_NOT_RUN;
2375         case TEST_EXECUTE:
2376                 break;
2377         }
2378
2379         topology_alice = build_topology(alice_streams, ARRAY_LEN(alice_streams));
2380         if (!topology_alice) {
2381                 goto end;
2382         }
2383
2384         topology_bob = build_topology(bob_streams, ARRAY_LEN(bob_streams));
2385         if (!topology_bob) {
2386                 goto end;
2387         }
2388
2389         if (append_source_streams(topology_alice, "PJSIP/Bob-00000001", topology_bob)) {
2390                 ast_test_status_update(test, "Failed to append Bob's streams to Alice\n");
2391                 goto end;
2392         }
2393
2394         if (ast_stream_topology_get_count(topology_alice) != 3) {
2395                 ast_test_status_update(test, "Alice's topology isn't large enough! It's %d but needs to be %d\n",
2396                         ast_stream_topology_get_count(topology_alice), 3);
2397                 goto end;
2398         }
2399
2400         if (validate_original_streams(test, topology_alice, alice_streams, ARRAY_LEN(alice_streams))) {
2401                 goto end;
2402         }
2403
2404         if (validate_stream(test, ast_stream_topology_get_stream(topology_alice, 2), &alice_dest_stream)) {
2405                 goto end;
2406         }
2407
2408         if (append_source_streams(topology_bob, "PJSIP/Alice-00000000", topology_alice)) {
2409                 ast_test_status_update(test, "Failed to append Alice's streams to Bob\n");
2410                 goto end;
2411         }
2412
2413         if (ast_stream_topology_get_count(topology_bob) != 3) {
2414                 ast_test_status_update(test, "Bob's topology isn't large enough! It's %d but needs to be %d\n",
2415                         ast_stream_topology_get_count(topology_bob), 3);
2416                 goto end;
2417         }
2418
2419         if (validate_original_streams(test, topology_bob, bob_streams, ARRAY_LEN(bob_streams))) {
2420                 goto end;
2421         }
2422
2423         if (validate_stream(test, ast_stream_topology_get_stream(topology_bob, 2), &bob_dest_stream)) {
2424                 goto end;
2425         }
2426
2427         res = AST_TEST_PASS;
2428
2429 end:
2430         ast_stream_topology_free(topology_alice);
2431         ast_stream_topology_free(topology_bob);
2432         return res;
2433 }
2434
2435 AST_TEST_DEFINE(sfu_remove_destination_streams)
2436 {
2437         enum ast_test_result_state res = AST_TEST_FAIL;
2438         static const struct stream_parameters params[] = {
2439                 { "alice_audio", "ulaw,alaw,g722,opus", AST_MEDIA_TYPE_AUDIO, },
2440                 { "alice_video", "h264,vp8", AST_MEDIA_TYPE_VIDEO, },
2441                 { "softbridge_dest_PJSIP/Bob-00000001_video", "vp8", AST_MEDIA_TYPE_VIDEO, },
2442                 { "softbridge_dest_PJSIP/Carol-00000002_video", "h264", AST_MEDIA_TYPE_VIDEO, },
2443         };
2444         static const struct {
2445                 const char *channel_name;
2446                 int num_streams;
2447                 int params_index[4];
2448         } removal_results[] = {
2449                 { "PJSIP/Bob-00000001", 4, { 0, 1, 2, 3 }, },
2450                 { "PJSIP/Edward-00000004", 4, { 0, 1, 2, 3 }, },
2451                 { "", 4, { 0, 1, 2, 3 }, },
2452         };
2453         struct ast_stream_topology *orig = NULL;
2454         int i;
2455
2456         switch (cmd) {
2457         case TEST_INIT:
2458                 info->name = "sfu_remove_destination_streams";
2459                 info->category = "/bridges/bridge_softmix/";
2460                 info->summary = "Test removal of destination video streams";
2461                 info->description =
2462                         "This tests does stuff.";
2463                 return AST_TEST_NOT_RUN;
2464         case TEST_EXECUTE:
2465                 break;
2466         }
2467
2468         orig = build_topology(params, ARRAY_LEN(params));
2469         if (!orig) {
2470                 ast_test_status_update(test, "Unable to build initial stream topology\n");
2471                 goto end;
2472         }
2473
2474         for (i = 0; i < ARRAY_LEN(removal_results); ++i) {
2475                 int j;
2476
2477                 remove_destination_streams(orig, removal_results[i].channel_name);
2478
2479                 if (ast_stream_topology_get_count(orig) != removal_results[i].num_streams) {
2480                         ast_test_status_update(test, "Resulting topology has %d streams, when %d are expected\n",
2481                                 ast_stream_topology_get_count(orig), removal_results[i].num_streams);
2482                         goto end;
2483                 }
2484
2485                 for (j = 0; j < removal_results[i].num_streams; ++j) {
2486                         struct ast_stream *actual;
2487                         struct ast_stream *expected;
2488                         int orig_index;
2489
2490                         actual = ast_stream_topology_get_stream(orig, j);
2491
2492                         orig_index = removal_results[i].params_index[j];
2493                         expected = ast_stream_topology_get_stream(orig, orig_index);
2494
2495                         if (!ast_format_cap_identical(ast_stream_get_formats(actual),
2496                                 ast_stream_get_formats(expected))) {
2497                                 struct ast_str *expected_str;
2498                                 struct ast_str *actual_str;
2499
2500                                 expected_str = ast_str_alloca(64);
2501                                 actual_str = ast_str_alloca(64);
2502
2503                                 ast_test_status_update(test, "Mismatch between expected (%s) and actual (%s) stream formats\n",
2504                                         ast_format_cap_get_names(ast_stream_get_formats(expected), &expected_str),
2505                                         ast_format_cap_get_names(ast_stream_get_formats(actual), &actual_str));
2506                                 goto end;
2507                         }
2508
2509                         if (is_video_dest(actual, removal_results[i].channel_name, NULL) &&
2510                                 ast_stream_get_state(actual) != AST_STREAM_STATE_REMOVED) {
2511                                 ast_test_status_update(test, "Removed stream %s does not have a state of removed\n", ast_stream_get_name(actual));
2512                                 goto end;
2513                         }
2514                 }
2515         }
2516
2517         res = AST_TEST_PASS;
2518
2519 end:
2520         ast_stream_topology_free(orig);
2521         return res;
2522 }
2523
2524 #endif
2525
2526 static int unload_module(void)
2527 {
2528         ast_bridge_technology_unregister(&softmix_bridge);
2529         AST_TEST_UNREGISTER(sfu_append_source_streams);
2530         AST_TEST_UNREGISTER(sfu_remove_destination_streams);
2531         return 0;
2532 }
2533
2534 static int load_module(void)
2535 {
2536         if (ast_bridge_technology_register(&softmix_bridge)) {
2537                 unload_module();
2538                 return AST_MODULE_LOAD_DECLINE;
2539         }
2540         AST_TEST_REGISTER(sfu_append_source_streams);
2541         AST_TEST_REGISTER(sfu_remove_destination_streams);
2542         return AST_MODULE_LOAD_SUCCESS;
2543 }
2544
2545 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Multi-party software based channel mixing");