| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#include "asterisk.h" |
| 35 |
|
| 36 |
#include <math.h> |
| 37 |
|
| 38 |
#include "asterisk/stream.h" |
| 39 |
#include "asterisk/test.h" |
| 40 |
#include "asterisk/vector.h" |
| 41 |
#include "asterisk/message.h" |
| 42 |
#include "bridge_softmix/include/bridge_softmix_internal.h" |
| 43 |
|
| 44 |
|
| 45 |
#define SOFTMIX_MIN_SAMPLE_RATE 8000 |
| 46 |
|
| 47 |
|
| 48 |
#define DEFAULT_SOFTMIX_INTERVAL 20 |
| 49 |
|
| 50 |
|
| 51 |
#define SOFTMIX_DATALEN(rate, interval) ((rate/50) * (interval / 10)) |
| 52 |
|
| 53 |
|
| 54 |
#define SOFTMIX_SAMPLES(rate, interval) (SOFTMIX_DATALEN(rate, interval) / 2) |
| 55 |
|
| 56 |
|
| 57 |
#define SOFTMIX_STAT_INTERVAL 100 |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
#define DEFAULT_SOFTMIX_SILENCE_THRESHOLD 2500 |
| 67 |
|
| 68 |
|
| 69 |
#define DEFAULT_SOFTMIX_TALKING_THRESHOLD 160 |
| 70 |
|
| 71 |
#define SOFTBRIDGE_VIDEO_DEST_PREFIX "softbridge_dest" |
| 72 |
#define SOFTBRIDGE_VIDEO_DEST_LEN strlen(SOFTBRIDGE_VIDEO_DEST_PREFIX) |
| 73 |
#define SOFTBRIDGE_VIDEO_DEST_SEPARATOR '_' |
| 74 |
|
| 75 |
struct softmix_remb_collector { |
| 76 |
|
| 77 |
struct ast_frame frame; |
| 78 |
|
| 79 |
struct ast_rtp_rtcp_feedback feedback; |
| 80 |
|
| 81 |
float bitrate; |
| 82 |
}; |
| 83 |
|
| 84 |
struct softmix_stats { |
| 85 |
|
| 86 |
unsigned int sample_rates[16]; |
| 87 |
|
| 88 |
unsigned int num_channels[16]; |
| 89 |
|
| 90 |
unsigned int num_above_internal_rate; |
| 91 |
|
| 92 |
unsigned int num_above_maximum_rate; |
| 93 |
|
| 94 |
unsigned int num_at_internal_rate; |
| 95 |
|
| 96 |
unsigned int highest_supported_rate; |
| 97 |
|
| 98 |
unsigned int locked_rate; |
| 99 |
|
| 100 |
unsigned int maximum_rate; |
| 101 |
}; |
| 102 |
|
| 103 |
struct softmix_translate_helper_entry { |
| 104 |
int num_times_requested; |
| 105 |
|
| 106 |
struct ast_format *dst_format; |
| 107 |
struct ast_trans_pvt *trans_pvt; |
| 108 |
struct ast_frame *out_frame; |
| 109 |
AST_LIST_ENTRY(softmix_translate_helper_entry) entry; |
| 110 |
}; |
| 111 |
|
| 112 |
struct softmix_translate_helper { |
| 113 |
struct ast_format *slin_src; |
| 114 |
AST_LIST_HEAD_NOLOCK(, softmix_translate_helper_entry) entries; |
| 115 |
}; |
| 116 |
|
| 117 |
static struct softmix_translate_helper_entry *softmix_translate_helper_entry_alloc(struct ast_format *dst) |
| 118 |
{ |
| 119 |
struct softmix_translate_helper_entry *entry; |
| 120 |
if (!(entry = ast_calloc(1, sizeof(*entry)))) { |
| 121 |
return NULL; |
| 122 |
} |
| 123 |
entry->dst_format = ao2_bump(dst); |
| 124 |
|
| 125 |
|
| 126 |
entry->num_times_requested = 1; |
| 127 |
return entry; |
| 128 |
} |
| 129 |
|
| 130 |
static void *softmix_translate_helper_free_entry(struct softmix_translate_helper_entry *entry) |
| 131 |
{ |
| 132 |
ao2_cleanup(entry->dst_format); |
| 133 |
|
| 134 |
if (entry->trans_pvt) { |
| 135 |
ast_translator_free_path(entry->trans_pvt); |
| 136 |
} |
| 137 |
if (entry->out_frame) { |
| 138 |
ast_frfree(entry->out_frame); |
| 139 |
} |
| 140 |
ast_free(entry); |
| 141 |
return NULL; |
| 142 |
} |
| 143 |
|
| 144 |
static void softmix_translate_helper_init(struct softmix_translate_helper *trans_helper, unsigned int sample_rate) |
| 145 |
{ |
| 146 |
memset(trans_helper, 0, sizeof(*trans_helper)); |
| 147 |
trans_helper->slin_src = ast_format_cache_get_slin_by_rate(sample_rate); |
| 148 |
} |
| 149 |
|
| 150 |
static void softmix_translate_helper_destroy(struct softmix_translate_helper *trans_helper) |
| 151 |
{ |
| 152 |
struct softmix_translate_helper_entry *entry; |
| 153 |
|
| 154 |
while ((entry = AST_LIST_REMOVE_HEAD(&trans_helper->entries, entry))) { |
| 155 |
softmix_translate_helper_free_entry(entry); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
static void softmix_translate_helper_change_rate(struct softmix_translate_helper *trans_helper, unsigned int sample_rate) |
| 160 |
{ |
| 161 |
struct softmix_translate_helper_entry *entry; |
| 162 |
|
| 163 |
trans_helper->slin_src = ast_format_cache_get_slin_by_rate(sample_rate); |
| 164 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&trans_helper->entries, entry, entry) { |
| 165 |
if (entry->trans_pvt) { |
| 166 |
ast_translator_free_path(entry->trans_pvt); |
| 167 |
if (!(entry->trans_pvt = ast_translator_build_path(entry->dst_format, trans_helper->slin_src))) { |
| 168 |
AST_LIST_REMOVE_CURRENT(entry); |
| 169 |
entry = softmix_translate_helper_free_entry(entry); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
AST_LIST_TRAVERSE_SAFE_END; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
static int16_t *softmix_process_read_audio(struct softmix_channel *sc, unsigned int num_samples) |
| 185 |
{ |
| 186 |
if ((ast_slinfactory_available(&sc->factory) >= num_samples) && |
| 187 |
ast_slinfactory_read(&sc->factory, sc->our_buf, num_samples)) { |
| 188 |
sc->have_audio = 1; |
| 189 |
return sc->our_buf; |
| 190 |
} |
| 191 |
sc->have_audio = 0; |
| 192 |
return NULL; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
static void softmix_process_write_audio(struct softmix_translate_helper *trans_helper, |
| 204 |
struct ast_format *raw_write_fmt, |
| 205 |
struct softmix_channel *sc, unsigned int default_sample_size) |
| 206 |
{ |
| 207 |
struct softmix_translate_helper_entry *entry = NULL; |
| 208 |
int i; |
| 209 |
|
| 210 |
|
| 211 |
if (sc->have_audio && !sc->binaural) { |
| 212 |
for (i = 0; i < sc->write_frame.samples; i++) { |
| 213 |
ast_slinear_saturated_subtract(&sc->final_buf[i], &sc->our_buf[i]); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
AST_LIST_TRAVERSE(&trans_helper->entries, entry, entry) { |
| 218 |
if (ast_format_cmp(entry->dst_format, raw_write_fmt) == AST_FORMAT_CMP_EQUAL) { |
| 219 |
++entry->num_times_requested; |
| 220 |
break; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
return; |
| 226 |
} else if (sc->have_audio && sc->binaural > 0) { |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
softmix_process_write_binaural_audio(sc, default_sample_size); |
| 232 |
return; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
AST_LIST_TRAVERSE(&trans_helper->entries, entry, entry) { |
| 241 |
if (sc->binaural != 0) { |
| 242 |
continue; |
| 243 |
} |
| 244 |
if (ast_format_cmp(entry->dst_format, raw_write_fmt) == AST_FORMAT_CMP_EQUAL) { |
| 245 |
entry->num_times_requested++; |
| 246 |
} else { |
| 247 |
continue; |
| 248 |
} |
| 249 |
if (!entry->trans_pvt && (entry->num_times_requested > 1)) { |
| 250 |
entry->trans_pvt = ast_translator_build_path(entry->dst_format, trans_helper->slin_src); |
| 251 |
} |
| 252 |
if (entry->trans_pvt && !entry->out_frame) { |
| 253 |
entry->out_frame = ast_translate(entry->trans_pvt, &sc->write_frame, 0); |
| 254 |
} |
| 255 |
if (entry->out_frame && entry->out_frame->frametype == AST_FRAME_VOICE |
| 256 |
&& entry->out_frame->datalen < MAX_DATALEN) { |
| 257 |
ao2_replace(sc->write_frame.subclass.format, entry->out_frame->subclass.format); |
| 258 |
memcpy(sc->final_buf, entry->out_frame->data.ptr, entry->out_frame->datalen); |
| 259 |
sc->write_frame.datalen = entry->out_frame->datalen; |
| 260 |
sc->write_frame.samples = entry->out_frame->samples; |
| 261 |
} |
| 262 |
break; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
if (!entry && (entry = softmix_translate_helper_entry_alloc(raw_write_fmt))) { |
| 267 |
AST_LIST_INSERT_HEAD(&trans_helper->entries, entry, entry); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
static void softmix_translate_helper_cleanup(struct softmix_translate_helper *trans_helper) |
| 272 |
{ |
| 273 |
struct softmix_translate_helper_entry *entry; |
| 274 |
|
| 275 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&trans_helper->entries, entry, entry) { |
| 276 |
|
| 277 |
if (!entry->num_times_requested) { |
| 278 |
AST_LIST_REMOVE_CURRENT(entry); |
| 279 |
softmix_translate_helper_free_entry(entry); |
| 280 |
continue; |
| 281 |
} |
| 282 |
|
| 283 |
if (entry->out_frame) { |
| 284 |
ast_frfree(entry->out_frame); |
| 285 |
entry->out_frame = NULL; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
if (entry->num_times_requested == 1 && entry->trans_pvt) { |
| 291 |
ast_translator_free_path(entry->trans_pvt); |
| 292 |
entry->trans_pvt = NULL; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
entry->num_times_requested = 0; |
| 299 |
} |
| 300 |
AST_LIST_TRAVERSE_SAFE_END; |
| 301 |
} |
| 302 |
|
| 303 |
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) |
| 304 |
{ |
| 305 |
struct softmix_channel *sc = bridge_channel->tech_pvt; |
| 306 |
struct ast_format *slin_format; |
| 307 |
int setup_fail; |
| 308 |
|
| 309 |
#ifdef BINAURAL_RENDERING |
| 310 |
if (interval != BINAURAL_MIXING_INTERVAL) { |
| 311 |
interval = BINAURAL_MIXING_INTERVAL; |
| 312 |
} |
| 313 |
#endif |
| 314 |
|
| 315 |
|
| 316 |
ast_assert(sc != NULL); |
| 317 |
|
| 318 |
slin_format = ast_format_cache_get_slin_by_rate(rate); |
| 319 |
|
| 320 |
ast_mutex_lock(&sc->lock); |
| 321 |
if (reset) { |
| 322 |
ast_slinfactory_destroy(&sc->factory); |
| 323 |
ast_dsp_free(sc->dsp); |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
sc->write_frame.frametype = AST_FRAME_VOICE; |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
ao2_replace(sc->write_frame.subclass.format, slin_format); |
| 335 |
sc->write_frame.data.ptr = sc->final_buf; |
| 336 |
sc->write_frame.datalen = SOFTMIX_DATALEN(rate, interval); |
| 337 |
sc->write_frame.samples = SOFTMIX_SAMPLES(rate, interval); |
| 338 |
|
| 339 |
|
| 340 |
sc->rate = rate; |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
if (set_binaural == 1) { |
| 345 |
sc->binaural = 1; |
| 346 |
} else if (set_binaural == 0) { |
| 347 |
sc->binaural = 0; |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
if (binaural_pos_id != -1) { |
| 353 |
sc->binaural_pos = binaural_pos_id; |
| 354 |
} |
| 355 |
if (is_announcement != -1) { |
| 356 |
sc->is_announcement = is_announcement; |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
sc->read_slin_format = slin_format; |
| 364 |
|
| 365 |
|
| 366 |
setup_fail = ast_slinfactory_init_with_format(&sc->factory, slin_format); |
| 367 |
|
| 368 |
|
| 369 |
ast_channel_lock(bridge_channel->chan); |
| 370 |
setup_fail |= ast_set_read_format_path(bridge_channel->chan, |
| 371 |
ast_channel_rawreadformat(bridge_channel->chan), slin_format); |
| 372 |
ast_channel_unlock(bridge_channel->chan); |
| 373 |
|
| 374 |
|
| 375 |
if (set_binaural == 1 || (set_binaural == -1 && sc->binaural == 1)) { |
| 376 |
setup_fail |= ast_set_write_format_interleaved_stereo(bridge_channel->chan, slin_format); |
| 377 |
} else if (set_binaural == 0) { |
| 378 |
setup_fail |= ast_set_write_format(bridge_channel->chan, slin_format); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
sc->dsp = ast_dsp_new_with_rate(rate); |
| 383 |
if (setup_fail || !sc->dsp) { |
| 384 |
|
| 385 |
ast_mutex_unlock(&sc->lock); |
| 386 |
ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END, 0); |
| 387 |
return; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
if (bridge_channel->tech_args.talking_threshold) { |
| 392 |
ast_dsp_set_threshold(sc->dsp, bridge_channel->tech_args.talking_threshold); |
| 393 |
} else { |
| 394 |
ast_dsp_set_threshold(sc->dsp, DEFAULT_SOFTMIX_TALKING_THRESHOLD); |
| 395 |
} |
| 396 |
|
| 397 |
ast_mutex_unlock(&sc->lock); |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
static void softmix_poke_thread(struct softmix_bridge_data *softmix_data) |
| 408 |
{ |
| 409 |
ast_mutex_lock(&softmix_data->lock); |
| 410 |
ast_cond_signal(&softmix_data->cond); |
| 411 |
ast_mutex_unlock(&softmix_data->lock); |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
static void softmix_bridge_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel) |
| 416 |
{ |
| 417 |
#ifdef BINAURAL_RENDERING |
| 418 |
struct softmix_channel *sc = bridge_channel->tech_pvt; |
| 419 |
if (sc->binaural) { |
| 420 |
|
| 421 |
struct ast_format *slin_format; |
| 422 |
|
| 423 |
slin_format = ast_format_cache_get_slin_by_rate(sc->rate); |
| 424 |
ast_set_write_format_interleaved_stereo(bridge_channel->chan, slin_format); |
| 425 |
} |
| 426 |
#endif |
| 427 |
if (bridge->tech_pvt) { |
| 428 |
softmix_poke_thread(bridge->tech_pvt); |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
static int is_video_source(const struct ast_stream *stream) |
| 440 |
{ |
| 441 |
if (ast_stream_get_state(stream) != AST_STREAM_STATE_REMOVED |
| 442 |
&& ast_stream_get_type(stream) == AST_MEDIA_TYPE_VIDEO |
| 443 |
&& strncmp(ast_stream_get_name(stream), SOFTBRIDGE_VIDEO_DEST_PREFIX, |
| 444 |
SOFTBRIDGE_VIDEO_DEST_LEN)) { |
| 445 |
return 1; |
| 446 |
} |
| 447 |
|
| 448 |
return 0; |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
static int is_video_dest(const struct ast_stream *stream, const char *source_channel_name, |
| 467 |
int source_channel_stream_position) |
| 468 |
{ |
| 469 |
char *dest_video_name; |
| 470 |
size_t dest_video_name_len; |
| 471 |
|
| 472 |
if (ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED |
| 473 |
|| ast_stream_get_type(stream) != AST_MEDIA_TYPE_VIDEO) { |
| 474 |
return 0; |
| 475 |
} |
| 476 |
|
| 477 |
dest_video_name_len = SOFTBRIDGE_VIDEO_DEST_LEN + 1; |
| 478 |
if (!ast_strlen_zero(source_channel_name)) { |
| 479 |
dest_video_name_len += strlen(source_channel_name) + 1; |
| 480 |
if (source_channel_stream_position != -1) { |
| 481 |
dest_video_name_len += 11; |
| 482 |
} |
| 483 |
|
| 484 |
dest_video_name = ast_alloca(dest_video_name_len); |
| 485 |
if (source_channel_stream_position != -1) { |
| 486 |
|
| 487 |
snprintf(dest_video_name, dest_video_name_len, "%s%c%s%c%d", |
| 488 |
SOFTBRIDGE_VIDEO_DEST_PREFIX, SOFTBRIDGE_VIDEO_DEST_SEPARATOR, |
| 489 |
source_channel_name, SOFTBRIDGE_VIDEO_DEST_SEPARATOR, |
| 490 |
source_channel_stream_position); |
| 491 |
return !strcmp(ast_stream_get_name(stream), dest_video_name); |
| 492 |
} |
| 493 |
snprintf(dest_video_name, dest_video_name_len, "%s%c%s", |
| 494 |
SOFTBRIDGE_VIDEO_DEST_PREFIX, SOFTBRIDGE_VIDEO_DEST_SEPARATOR, |
| 495 |
source_channel_name); |
| 496 |
} else { |
| 497 |
dest_video_name = SOFTBRIDGE_VIDEO_DEST_PREFIX; |
| 498 |
} |
| 499 |
|
| 500 |
return !strncmp(ast_stream_get_name(stream), dest_video_name, dest_video_name_len - 1); |
| 501 |
} |
| 502 |
|
| 503 |
static int append_source_stream(struct ast_stream_topology *dest, |
| 504 |
const char *channel_name, const char *sdp_label, |
| 505 |
struct ast_stream *stream, int index) |
| 506 |
{ |
| 507 |
char *stream_clone_name = NULL; |
| 508 |
struct ast_stream *stream_clone; |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
if (ast_asprintf(&stream_clone_name, "%s%c%s%c%d", SOFTBRIDGE_VIDEO_DEST_PREFIX, |
| 515 |
SOFTBRIDGE_VIDEO_DEST_SEPARATOR, channel_name, SOFTBRIDGE_VIDEO_DEST_SEPARATOR, |
| 516 |
index) < 0) { |
| 517 |
return -1; |
| 518 |
} |
| 519 |
|
| 520 |
stream_clone = ast_stream_clone(stream, stream_clone_name); |
| 521 |
ast_free(stream_clone_name); |
| 522 |
if (!stream_clone) { |
| 523 |
return -1; |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
if (!ast_strlen_zero(sdp_label)) { |
| 528 |
ast_stream_set_metadata(stream_clone, "SDP:LABEL", sdp_label); |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
ast_stream_set_state(stream_clone, AST_STREAM_STATE_SENDONLY); |
| 533 |
|
| 534 |
if (ast_stream_topology_append_stream(dest, stream_clone) < 0) { |
| 535 |
ast_stream_free(stream_clone); |
| 536 |
return -1; |
| 537 |
} |
| 538 |
|
| 539 |
return 0; |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
static int append_source_streams(struct ast_stream_topology *dest, |
| 544 |
const char *channel_name, const char *sdp_label, |
| 545 |
const struct ast_stream_topology *source) |
| 546 |
{ |
| 547 |
int i; |
| 548 |
|
| 549 |
for (i = 0; i < ast_stream_topology_get_count(source); ++i) { |
| 550 |
struct ast_stream *stream; |
| 551 |
|
| 552 |
stream = ast_stream_topology_get_stream(source, i); |
| 553 |
|
| 554 |
if (!is_video_source(stream)) { |
| 555 |
continue; |
| 556 |
} |
| 557 |
|
| 558 |
if (append_source_stream(dest, channel_name, sdp_label, stream, i)) { |
| 559 |
return -1; |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
return 0; |
| 564 |
} |
| 565 |
|
| 566 |
static int append_all_streams(struct ast_stream_topology *dest, |
| 567 |
const struct ast_stream_topology *source) |
| 568 |
{ |
| 569 |
int i; |
| 570 |
int dest_index = 0; |
| 571 |
|
| 572 |
for (i = 0; i < ast_stream_topology_get_count(source); ++i) { |
| 573 |
struct ast_stream *clone; |
| 574 |
int added = 0; |
| 575 |
|
| 576 |
clone = ast_stream_clone(ast_stream_topology_get_stream(source, i), NULL); |
| 577 |
if (!clone) { |
| 578 |
return -1; |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
while (dest_index < ast_stream_topology_get_count(dest)) { |
| 583 |
struct ast_stream *stream = ast_stream_topology_get_stream(dest, dest_index); |
| 584 |
|
| 585 |
dest_index++; |
| 586 |
|
| 587 |
if (ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED) { |
| 588 |
|
| 589 |
|
| 590 |
ast_stream_topology_set_stream(dest, dest_index - 1, clone); |
| 591 |
added = 1; |
| 592 |
break; |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
if (!added && ast_stream_topology_append_stream(dest, clone) < 0) { |
| 598 |
ast_stream_free(clone); |
| 599 |
return -1; |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
return 0; |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
static void sfu_topologies_on_join(struct ast_bridge *bridge, |
| 620 |
struct ast_bridge_channel *joiner) |
| 621 |
{ |
| 622 |
RAII_VAR(struct ast_stream_topology *, joiner_video, NULL, ast_stream_topology_free); |
| 623 |
struct ast_bridge_channels_list *participants = &bridge->channels; |
| 624 |
struct ast_bridge_channel *participant; |
| 625 |
int res; |
| 626 |
struct softmix_channel *sc; |
| 627 |
SCOPE_ENTER(3, "%s: \n", ast_channel_name(joiner->chan)); |
| 628 |
|
| 629 |
joiner_video = ast_stream_topology_alloc(); |
| 630 |
if (!joiner_video) { |
| 631 |
SCOPE_EXIT_LOG_RTN(LOG_ERROR, "%s: Couldn't alloc topology\n", ast_channel_name(joiner->chan)); |
| 632 |
} |
| 633 |
|
| 634 |
sc = joiner->tech_pvt; |
| 635 |
|
| 636 |
ast_channel_lock(joiner->chan); |
| 637 |
res = append_source_streams(joiner_video, ast_channel_name(joiner->chan), |
| 638 |
bridge->softmix.send_sdp_label ? ast_channel_uniqueid(joiner->chan) : NULL, |
| 639 |
ast_channel_get_stream_topology(joiner->chan)); |
| 640 |
sc->topology = ast_stream_topology_clone(ast_channel_get_stream_topology(joiner->chan)); |
| 641 |
ast_channel_unlock(joiner->chan); |
| 642 |
|
| 643 |
if (res || !sc->topology) { |
| 644 |
SCOPE_EXIT_LOG_RTN(LOG_ERROR, "%s: Couldn't append source streams\n", ast_channel_name(joiner->chan)); |
| 645 |
} |
| 646 |
|
| 647 |
AST_LIST_TRAVERSE(participants, participant, entry) { |
| 648 |
if (participant == joiner) { |
| 649 |
continue; |
| 650 |
} |
| 651 |
ast_trace(-1, "%s: Appending existing participant %s\n", ast_channel_name(joiner->chan), |
| 652 |
ast_channel_name(participant->chan)); |
| 653 |
ast_channel_lock(participant->chan); |
| 654 |
res = append_source_streams(sc->topology, ast_channel_name(participant->chan), |
| 655 |
bridge->softmix.send_sdp_label ? ast_channel_uniqueid(participant->chan) : NULL, |
| 656 |
ast_channel_get_stream_topology(participant->chan)); |
| 657 |
ast_channel_unlock(participant->chan); |
| 658 |
if (res) { |
| 659 |
SCOPE_EXIT_LOG_RTN(LOG_ERROR, "%s/%s: Couldn't append source streams\n", |
| 660 |
ast_channel_name(participant->chan), ast_channel_name(joiner->chan)); |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
ast_trace(-1, "%s: Requesting topology change.\n", ast_channel_name(joiner->chan)); |
| 665 |
res = ast_channel_request_stream_topology_change(joiner->chan, sc->topology, NULL); |
| 666 |
if (res) { |
| 667 |
|
| 668 |
SCOPE_EXIT_RTN("%s: Couldn't request topology change\n", ast_channel_name(joiner->chan)); |
| 669 |
} |
| 670 |
|
| 671 |
AST_LIST_TRAVERSE(participants, participant, entry) { |
| 672 |
if (participant == joiner) { |
| 673 |
continue; |
| 674 |
} |
| 675 |
|
| 676 |
sc = participant->tech_pvt; |
| 677 |
ast_trace(-1, "%s: Appending joiner %s\n", ast_channel_name(participant->chan), |
| 678 |
ast_channel_name(joiner->chan)); |
| 679 |
|
| 680 |
if (append_all_streams(sc->topology, joiner_video)) { |
| 681 |
SCOPE_EXIT_LOG_RTN(LOG_ERROR, "%s/%s: Couldn't append streams\n", |
| 682 |
ast_channel_name(participant->chan), ast_channel_name(joiner->chan)); |
| 683 |
} |
| 684 |
ast_trace(-1, "%s: Requesting topology change\n", ast_channel_name(participant->chan)); |
| 685 |
res = ast_channel_request_stream_topology_change(participant->chan, sc->topology, NULL); |
| 686 |
if (res) { |
| 687 |
ast_trace(-1, "%s/%s: Couldn't request topology change\n", |
| 688 |
ast_channel_name(participant->chan), ast_channel_name(joiner->chan)); |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
SCOPE_EXIT(); |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
static int softmix_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel) |
| 697 |
{ |
| 698 |
struct softmix_channel *sc; |
| 699 |
struct softmix_bridge_data *softmix_data; |
| 700 |
int set_binaural = 0; |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
int skip_binaural_output = 1; |
| 706 |
int pos_id; |
| 707 |
int is_announcement = 0; |
| 708 |
int samplerate_change; |
| 709 |
SCOPE_ENTER(3, "%s:\n", ast_channel_name(bridge_channel->chan)); |
| 710 |
|
| 711 |
softmix_data = bridge->tech_pvt; |
| 712 |
if (!softmix_data) { |
| 713 |
SCOPE_EXIT_RTN_VALUE(-1, "No tech_pvt\n"); |
| 714 |
} |
| 715 |
|
| 716 |
|
| 717 |
if (!(sc = ast_calloc(1, sizeof(*sc)))) { |
| 718 |
SCOPE_EXIT_RTN_VALUE(-1, "Couldn't alloc tech_pvt\n"); |
| 719 |
} |
| 720 |
|
| 721 |
samplerate_change = softmix_data->internal_rate; |
| 722 |
pos_id = -1; |
| 723 |
if (bridge->softmix.binaural_active) { |
| 724 |
if (strncmp(ast_channel_name(bridge_channel->chan), "CBAnn", 5) != 0) { |
| 725 |
set_binaural = ast_format_get_channel_count(bridge_channel->write_format) > 1 ? 1 : 0; |
| 726 |
if (set_binaural) { |
| 727 |
softmix_data->internal_rate = samplerate_change; |
| 728 |
} |
| 729 |
skip_binaural_output = 0; |
| 730 |
} else { |
| 731 |
is_announcement = 1; |
| 732 |
} |
| 733 |
if (set_binaural) { |
| 734 |
softmix_data->convolve.binaural_active = 1; |
| 735 |
} |
| 736 |
if (!skip_binaural_output) { |
| 737 |
pos_id = set_binaural_data_join(&softmix_data->convolve, softmix_data->default_sample_size); |
| 738 |
if (pos_id == -1) { |
| 739 |
ast_log(LOG_ERROR, "Bridge %s: Failed to join channel %s. " |
| 740 |
"Could not allocate enough memory.\n", bridge->uniqueid, |
| 741 |
ast_channel_name(bridge_channel->chan)); |
| 742 |
ast_free(sc); |
| 743 |
SCOPE_EXIT_RTN_VALUE(-1, "Couldn't do binaural join\n"); |
| 744 |
} |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
|
| 749 |
ast_mutex_init(&sc->lock); |
| 750 |
|
| 751 |
|
| 752 |
bridge_channel->tech_pvt = sc; |
| 753 |
|
| 754 |
set_softmix_bridge_data(softmix_data->internal_rate, |
| 755 |
softmix_data->internal_mixing_interval |
| 756 |
? softmix_data->internal_mixing_interval |
| 757 |
: DEFAULT_SOFTMIX_INTERVAL, |
| 758 |
bridge_channel, 0, set_binaural, pos_id, is_announcement); |
| 759 |
|
| 760 |
if (bridge->softmix.video_mode.mode == AST_BRIDGE_VIDEO_MODE_SFU) { |
| 761 |
sfu_topologies_on_join(bridge, bridge_channel); |
| 762 |
} |
| 763 |
|
| 764 |
|
| 765 |
if (ast_channel_hold_state(bridge_channel->chan) == AST_CONTROL_HOLD) { |
| 766 |
ast_debug(1, "Channel %s simulating UNHOLD for bridge softmix join.\n", |
| 767 |
ast_channel_name(bridge_channel->chan)); |
| 768 |
ast_indicate(bridge_channel->chan, AST_CONTROL_UNHOLD); |
| 769 |
} |
| 770 |
|
| 771 |
softmix_poke_thread(softmix_data); |
| 772 |
SCOPE_EXIT_RTN_VALUE(0); |
| 773 |
} |
| 774 |
|
| 775 |
static int remove_destination_streams(struct ast_stream_topology *topology, |
| 776 |
const char *channel_name) |
| 777 |
{ |
| 778 |
int i; |
| 779 |
int stream_removed = 0; |
| 780 |
|
| 781 |
for (i = 0; i < ast_stream_topology_get_count(topology); ++i) { |
| 782 |
struct ast_stream *stream; |
| 783 |
|
| 784 |
stream = ast_stream_topology_get_stream(topology, i); |
| 785 |
|
| 786 |
if (is_video_dest(stream, channel_name, -1)) { |
| 787 |
ast_stream_set_state(stream, AST_STREAM_STATE_REMOVED); |
| 788 |
stream_removed = 1; |
| 789 |
} |
| 790 |
} |
| 791 |
return stream_removed; |
| 792 |
} |
| 793 |
|
| 794 |
static int sfu_topologies_on_leave(struct ast_bridge_channel *leaver, struct ast_bridge_channels_list *participants) |
| 795 |
{ |
| 796 |
struct ast_bridge_channel *participant; |
| 797 |
struct softmix_channel *sc; |
| 798 |
|
| 799 |
AST_LIST_TRAVERSE(participants, participant, entry) { |
| 800 |
sc = participant->tech_pvt; |
| 801 |
if (!remove_destination_streams(sc->topology, ast_channel_name(leaver->chan))) { |
| 802 |
continue; |
| 803 |
} |
| 804 |
ast_channel_request_stream_topology_change(participant->chan, sc->topology, NULL); |
| 805 |
} |
| 806 |
|
| 807 |
sc = leaver->tech_pvt; |
| 808 |
if (remove_destination_streams(sc->topology, "")) { |
| 809 |
ast_channel_request_stream_topology_change(leaver->chan, sc->topology, NULL); |
| 810 |
} |
| 811 |
|
| 812 |
return 0; |
| 813 |
} |
| 814 |
|
| 815 |
|
| 816 |
static void softmix_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel) |
| 817 |
{ |
| 818 |
struct softmix_channel *sc; |
| 819 |
struct softmix_bridge_data *softmix_data; |
| 820 |
|
| 821 |
softmix_data = bridge->tech_pvt; |
| 822 |
sc = bridge_channel->tech_pvt; |
| 823 |
if (!sc) { |
| 824 |
return; |
| 825 |
} |
| 826 |
|
| 827 |
if (bridge->softmix.video_mode.mode == AST_BRIDGE_VIDEO_MODE_SFU) { |
| 828 |
sfu_topologies_on_leave(bridge_channel, &bridge->channels); |
| 829 |
} |
| 830 |
|
| 831 |
if (bridge->softmix.binaural_active) { |
| 832 |
if (sc->binaural) { |
| 833 |
set_binaural_data_leave(&softmix_data->convolve, sc->binaural_pos, |
| 834 |
softmix_data->default_sample_size); |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
bridge_channel->tech_pvt = NULL; |
| 839 |
|
| 840 |
ast_stream_topology_free(sc->topology); |
| 841 |
|
| 842 |
ao2_cleanup(sc->remb_collector); |
| 843 |
|
| 844 |
AST_VECTOR_FREE(&sc->video_sources); |
| 845 |
|
| 846 |
|
| 847 |
ast_mutex_destroy(&sc->lock); |
| 848 |
|
| 849 |
|
| 850 |
ast_slinfactory_destroy(&sc->factory); |
| 851 |
|
| 852 |
|
| 853 |
ao2_cleanup(sc->write_frame.subclass.format); |
| 854 |
|
| 855 |
|
| 856 |
ast_dsp_free(sc->dsp); |
| 857 |
|
| 858 |
|
| 859 |
ast_free(sc); |
| 860 |
} |
| 861 |
|
| 862 |
static void softmix_pass_video_top_priority(struct ast_bridge *bridge, struct ast_frame *frame) |
| 863 |
{ |
| 864 |
struct ast_bridge_channel *cur; |
| 865 |
|
| 866 |
AST_LIST_TRAVERSE(&bridge->channels, cur, entry) { |
| 867 |
if (cur->suspended) { |
| 868 |
continue; |
| 869 |
} |
| 870 |
if (ast_bridge_is_video_src(bridge, cur->chan) == 1) { |
| 871 |
ast_bridge_channel_queue_frame(cur, frame); |
| 872 |
break; |
| 873 |
} |
| 874 |
} |
| 875 |
} |
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
static void softmix_bridge_write_video(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame) |
| 887 |
{ |
| 888 |
struct softmix_channel *sc; |
| 889 |
int video_src_priority; |
| 890 |
|
| 891 |
|
| 892 |
switch (bridge->softmix.video_mode.mode) { |
| 893 |
case AST_BRIDGE_VIDEO_MODE_NONE: |
| 894 |
break; |
| 895 |
case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC: |
| 896 |
video_src_priority = ast_bridge_is_video_src(bridge, bridge_channel->chan); |
| 897 |
if (video_src_priority == 1) { |
| 898 |
|
| 899 |
ast_bridge_queue_everyone_else(bridge, NULL, frame); |
| 900 |
} |
| 901 |
break; |
| 902 |
case AST_BRIDGE_VIDEO_MODE_TALKER_SRC: |
| 903 |
sc = bridge_channel->tech_pvt; |
| 904 |
ast_mutex_lock(&sc->lock); |
| 905 |
ast_bridge_update_talker_src_video_mode(bridge, bridge_channel->chan, |
| 906 |
sc->video_talker.energy_average, |
| 907 |
frame->subclass.frame_ending); |
| 908 |
ast_mutex_unlock(&sc->lock); |
| 909 |
video_src_priority = ast_bridge_is_video_src(bridge, bridge_channel->chan); |
| 910 |
if (video_src_priority == 1) { |
| 911 |
int num_src = ast_bridge_number_video_src(bridge); |
| 912 |
int echo = num_src > 1 ? 0 : 1; |
| 913 |
|
| 914 |
ast_bridge_queue_everyone_else(bridge, echo ? NULL : bridge_channel, frame); |
| 915 |
} else if (video_src_priority == 2) { |
| 916 |
softmix_pass_video_top_priority(bridge, frame); |
| 917 |
} |
| 918 |
break; |
| 919 |
case AST_BRIDGE_VIDEO_MODE_SFU: |
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
ast_bridge_queue_everyone_else(bridge, bridge_channel, frame); |
| 924 |
break; |
| 925 |
} |
| 926 |
} |
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
static void softmix_bridge_write_voice(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame) |
| 938 |
{ |
| 939 |
struct softmix_channel *sc = bridge_channel->tech_pvt; |
| 940 |
struct softmix_bridge_data *softmix_data = bridge->tech_pvt; |
| 941 |
int silent = 0; |
| 942 |
int totalsilence = 0; |
| 943 |
int cur_energy = 0; |
| 944 |
int silence_threshold = bridge_channel->tech_args.silence_threshold ? |
| 945 |
bridge_channel->tech_args.silence_threshold : |
| 946 |
DEFAULT_SOFTMIX_SILENCE_THRESHOLD; |
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
char update_talking = -1; |
| 952 |
|
| 953 |
|
| 954 |
ast_mutex_lock(&sc->lock); |
| 955 |
|
| 956 |
if (ast_format_cmp(frame->subclass.format, sc->read_slin_format) != AST_FORMAT_CMP_EQUAL) { |
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
ast_channel_lock(bridge_channel->chan); |
| 968 |
ast_debug(1, "Channel %s wrote unexpected format into bridge. Got %s, expected %s.\n", |
| 969 |
ast_channel_name(bridge_channel->chan), |
| 970 |
ast_format_get_name(frame->subclass.format), |
| 971 |
ast_format_get_name(sc->read_slin_format)); |
| 972 |
ast_set_read_format_path(bridge_channel->chan, frame->subclass.format, |
| 973 |
sc->read_slin_format); |
| 974 |
ast_channel_unlock(bridge_channel->chan); |
| 975 |
} |
| 976 |
|
| 977 |
|
| 978 |
if (sc->dsp) { |
| 979 |
silent = ast_dsp_silence_with_energy(sc->dsp, frame, &totalsilence, &cur_energy); |
| 980 |
} |
| 981 |
|
| 982 |
if (bridge->softmix.video_mode.mode == AST_BRIDGE_VIDEO_MODE_TALKER_SRC) { |
| 983 |
int cur_slot = sc->video_talker.energy_history_cur_slot; |
| 984 |
|
| 985 |
sc->video_talker.energy_accum -= sc->video_talker.energy_history[cur_slot]; |
| 986 |
sc->video_talker.energy_accum += cur_energy; |
| 987 |
sc->video_talker.energy_history[cur_slot] = cur_energy; |
| 988 |
sc->video_talker.energy_average = sc->video_talker.energy_accum / DEFAULT_ENERGY_HISTORY_LEN; |
| 989 |
sc->video_talker.energy_history_cur_slot++; |
| 990 |
if (sc->video_talker.energy_history_cur_slot == DEFAULT_ENERGY_HISTORY_LEN) { |
| 991 |
sc->video_talker.energy_history_cur_slot = 0; |
| 992 |
} |
| 993 |
} |
| 994 |
|
| 995 |
if (totalsilence < silence_threshold) { |
| 996 |
if (!sc->talking && !silent) { |
| 997 |
|
| 998 |
sc->talking = 1; |
| 999 |
update_talking = 1; |
| 1000 |
} |
| 1001 |
} else { |
| 1002 |
if (sc->talking) { |
| 1003 |
sc->talking = 0; |
| 1004 |
update_talking = 0; |
| 1005 |
} |
| 1006 |
} |
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
if (ast_slinfactory_available(&sc->factory) > (4 * SOFTMIX_SAMPLES(softmix_data->internal_rate, softmix_data->internal_mixing_interval))) { |
| 1012 |
ast_slinfactory_flush(&sc->factory); |
| 1013 |
} |
| 1014 |
|
| 1015 |
if (sc->talking || !bridge_channel->tech_args.drop_silence) { |
| 1016 |
|
| 1017 |
ast_slinfactory_feed(&sc->factory, frame); |
| 1018 |
} |
| 1019 |
|
| 1020 |
|
| 1021 |
ast_mutex_unlock(&sc->lock); |
| 1022 |
|
| 1023 |
if (update_talking != -1) { |
| 1024 |
ast_bridge_channel_notify_talking(bridge_channel, update_talking); |
| 1025 |
} |
| 1026 |
} |
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
static void clear_talking(struct ast_bridge_channel *bridge_channel) |
| 1036 |
{ |
| 1037 |
struct softmix_channel *sc = bridge_channel->tech_pvt; |
| 1038 |
|
| 1039 |
if (sc->talking) { |
| 1040 |
ast_mutex_lock(&sc->lock); |
| 1041 |
ast_slinfactory_flush(&sc->factory); |
| 1042 |
sc->talking = 0; |
| 1043 |
ast_mutex_unlock(&sc->lock); |
| 1044 |
|
| 1045 |
|
| 1046 |
ast_bridge_channel_notify_talking(bridge_channel, 0); |
| 1047 |
} |
| 1048 |
} |
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
static void softmix_bridge_check_voice(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel) |
| 1059 |
{ |
| 1060 |
if (bridge_channel->features->mute) { |
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
clear_talking(bridge_channel); |
| 1068 |
} |
| 1069 |
} |
| 1070 |
|
| 1071 |
static int remove_all_original_streams(struct ast_stream_topology *dest, |
| 1072 |
const struct ast_stream_topology *source, |
| 1073 |
const struct ast_stream_topology *original) |
| 1074 |
{ |
| 1075 |
int i; |
| 1076 |
|
| 1077 |
for (i = 0; i < ast_stream_topology_get_count(source); ++i) { |
| 1078 |
struct ast_stream *stream; |
| 1079 |
int original_index; |
| 1080 |
|
| 1081 |
stream = ast_stream_topology_get_stream(source, i); |
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
for (original_index = 0; original_index < ast_stream_topology_get_count(original); ++original_index) { |
| 1087 |
struct ast_stream *original_stream = ast_stream_topology_get_stream(original, original_index); |
| 1088 |
|
| 1089 |
if (!strcmp(ast_stream_get_name(stream), ast_stream_get_name(original_stream))) { |
| 1090 |
struct ast_stream *removed; |
| 1091 |
|
| 1092 |
removed = ast_stream_clone(stream, NULL); |
| 1093 |
if (!removed) { |
| 1094 |
return -1; |
| 1095 |
} |
| 1096 |
|
| 1097 |
ast_stream_set_state(removed, AST_STREAM_STATE_REMOVED); |
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
if (ast_stream_topology_set_stream(dest, original_index, removed)) { |
| 1103 |
ast_stream_free(removed); |
| 1104 |
return -1; |
| 1105 |
} |
| 1106 |
|
| 1107 |
break; |
| 1108 |
} |
| 1109 |
} |
| 1110 |
} |
| 1111 |
|
| 1112 |
return 0; |
| 1113 |
} |
| 1114 |
|
| 1115 |
static void sfu_topologies_on_source_change(struct ast_bridge *bridge, |
| 1116 |
struct ast_bridge_channel *source) |
| 1117 |
{ |
| 1118 |
struct ast_stream_topology *source_video = NULL; |
| 1119 |
struct ast_bridge_channels_list *participants = &bridge->channels; |
| 1120 |
struct ast_bridge_channel *participant; |
| 1121 |
int res; |
| 1122 |
|
| 1123 |
source_video = ast_stream_topology_alloc(); |
| 1124 |
if (!source_video) { |
| 1125 |
return; |
| 1126 |
} |
| 1127 |
|
| 1128 |
ast_channel_lock(source->chan); |
| 1129 |
res = append_source_streams(source_video, ast_channel_name(source->chan), |
| 1130 |
bridge->softmix.send_sdp_label ? ast_channel_uniqueid(source->chan) : NULL, |
| 1131 |
ast_channel_get_stream_topology(source->chan)); |
| 1132 |
ast_channel_unlock(source->chan); |
| 1133 |
if (res) { |
| 1134 |
goto cleanup; |
| 1135 |
} |
| 1136 |
|
| 1137 |
AST_LIST_TRAVERSE(participants, participant, entry) { |
| 1138 |
struct ast_stream_topology *original_topology; |
| 1139 |
struct softmix_channel *sc; |
| 1140 |
|
| 1141 |
if (participant == source) { |
| 1142 |
continue; |
| 1143 |
} |
| 1144 |
|
| 1145 |
sc = participant->tech_pvt; |
| 1146 |
|
| 1147 |
original_topology = ast_stream_topology_clone(sc->topology); |
| 1148 |
if (!original_topology) { |
| 1149 |
goto cleanup; |
| 1150 |
} |
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
if (append_all_streams(sc->topology, source_video)) { |
| 1156 |
ast_stream_topology_free(original_topology); |
| 1157 |
goto cleanup; |
| 1158 |
} |
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
if (remove_all_original_streams(sc->topology, source_video, original_topology)) { |
| 1164 |
ast_stream_topology_free(original_topology); |
| 1165 |
goto cleanup; |
| 1166 |
} |
| 1167 |
|
| 1168 |
ast_channel_request_stream_topology_change(participant->chan, sc->topology, NULL); |
| 1169 |
ast_stream_topology_free(original_topology); |
| 1170 |
} |
| 1171 |
|
| 1172 |
cleanup: |
| 1173 |
ast_stream_topology_free(source_video); |
| 1174 |
} |
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
static void softmix_bridge_write_text(struct ast_bridge *bridge, |
| 1187 |
struct ast_bridge_channel *bridge_channel, struct ast_frame *frame) |
| 1188 |
{ |
| 1189 |
if (DEBUG_ATLEAST(1)) { |
| 1190 |
struct ast_msg_data *msg = frame->data.ptr; |
| 1191 |
char frame_type[64]; |
| 1192 |
|
| 1193 |
ast_frame_type2str(frame->frametype, frame_type, sizeof(frame_type)); |
| 1194 |
|
| 1195 |
if (frame->frametype == AST_FRAME_TEXT_DATA) { |
| 1196 |
ast_log(LOG_DEBUG, "Received %s frame from '%s:%s': %s\n", frame_type, |
| 1197 |
ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_FROM), |
| 1198 |
ast_channel_name(bridge_channel->chan), |
| 1199 |
ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_BODY)); |
| 1200 |
} else { |
| 1201 |
ast_log(LOG_DEBUG, "Received %s frame from '%s': %.*s\n", frame_type, |
| 1202 |
ast_channel_name(bridge_channel->chan), frame->datalen, |
| 1203 |
(char *)frame->data.ptr); |
| 1204 |
} |
| 1205 |
} |
| 1206 |
|
| 1207 |
ast_bridge_queue_everyone_else(bridge, bridge_channel, frame); |
| 1208 |
} |
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
static int softmix_bridge_write_control(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame) |
| 1223 |
{ |
| 1224 |
struct softmix_bridge_data *softmix_data = bridge->tech_pvt; |
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
switch (frame->subclass.integer) { |
| 1232 |
case AST_CONTROL_HOLD: |
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
clear_talking(bridge_channel); |
| 1242 |
break; |
| 1243 |
case AST_CONTROL_VIDUPDATE: |
| 1244 |
if (!bridge->softmix.video_mode.video_update_discard || |
| 1245 |
ast_tvdiff_ms(ast_tvnow(), softmix_data->last_video_update) > bridge->softmix.video_mode.video_update_discard) { |
| 1246 |
ast_bridge_queue_everyone_else(bridge, bridge_channel, frame); |
| 1247 |
softmix_data->last_video_update = ast_tvnow(); |
| 1248 |
} |
| 1249 |
break; |
| 1250 |
case AST_CONTROL_STREAM_TOPOLOGY_SOURCE_CHANGED: |
| 1251 |
if (bridge->softmix.video_mode.mode == AST_BRIDGE_VIDEO_MODE_SFU) { |
| 1252 |
sfu_topologies_on_source_change(bridge, bridge_channel); |
| 1253 |
} |
| 1254 |
break; |
| 1255 |
default: |
| 1256 |
break; |
| 1257 |
} |
| 1258 |
|
| 1259 |
return 0; |
| 1260 |
} |
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
static void softmix_bridge_write_rtcp(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame) |
| 1272 |
{ |
| 1273 |
struct ast_rtp_rtcp_feedback *feedback = frame->data.ptr; |
| 1274 |
struct softmix_channel *sc = bridge_channel->tech_pvt; |
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
if (frame->subclass.integer != AST_RTP_RTCP_PSFB || feedback->fmt != AST_RTP_RTCP_FMT_REMB || |
| 1280 |
bridge->softmix.video_mode.mode != AST_BRIDGE_VIDEO_MODE_SFU || |
| 1281 |
!bridge->softmix.video_mode.mode_data.sfu_data.remb_send_interval) { |
| 1282 |
return; |
| 1283 |
} |
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
ast_mutex_lock(&sc->lock); |
| 1289 |
sc->remb = feedback->remb; |
| 1290 |
ast_mutex_unlock(&sc->lock); |
| 1291 |
|
| 1292 |
return; |
| 1293 |
} |
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
static int softmix_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame) |
| 1310 |
{ |
| 1311 |
int res = 0; |
| 1312 |
|
| 1313 |
if (!bridge->tech_pvt || !bridge_channel || !bridge_channel->tech_pvt) { |
| 1314 |
|
| 1315 |
return 0; |
| 1316 |
} |
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
switch (frame->frametype) { |
| 1324 |
case AST_FRAME_NULL: |
| 1325 |
|
| 1326 |
softmix_bridge_check_voice(bridge, bridge_channel); |
| 1327 |
break; |
| 1328 |
case AST_FRAME_DTMF_BEGIN: |
| 1329 |
case AST_FRAME_DTMF_END: |
| 1330 |
res = ast_bridge_queue_everyone_else(bridge, bridge_channel, frame); |
| 1331 |
break; |
| 1332 |
case AST_FRAME_VOICE: |
| 1333 |
softmix_bridge_write_voice(bridge, bridge_channel, frame); |
| 1334 |
break; |
| 1335 |
case AST_FRAME_VIDEO: |
| 1336 |
softmix_bridge_write_video(bridge, bridge_channel, frame); |
| 1337 |
break; |
| 1338 |
case AST_FRAME_TEXT: |
| 1339 |
case AST_FRAME_TEXT_DATA: |
| 1340 |
softmix_bridge_write_text(bridge, bridge_channel, frame); |
| 1341 |
break; |
| 1342 |
case AST_FRAME_CONTROL: |
| 1343 |
res = softmix_bridge_write_control(bridge, bridge_channel, frame); |
| 1344 |
break; |
| 1345 |
case AST_FRAME_RTCP: |
| 1346 |
softmix_bridge_write_rtcp(bridge, bridge_channel, frame); |
| 1347 |
break; |
| 1348 |
case AST_FRAME_BRIDGE_ACTION: |
| 1349 |
res = ast_bridge_queue_everyone_else(bridge, bridge_channel, frame); |
| 1350 |
break; |
| 1351 |
case AST_FRAME_BRIDGE_ACTION_SYNC: |
| 1352 |
ast_log(LOG_ERROR, "Synchronous bridge action written to a softmix bridge.\n"); |
| 1353 |
ast_assert(0); |
| 1354 |
default: |
| 1355 |
ast_debug(3, "Frame type %u unsupported\n", frame->frametype); |
| 1356 |
|
| 1357 |
break; |
| 1358 |
} |
| 1359 |
|
| 1360 |
return res; |
| 1361 |
} |
| 1362 |
|
| 1363 |
static void remb_collect_report_all(struct ast_bridge *bridge, struct softmix_bridge_data *softmix_data, |
| 1364 |
float bitrate) |
| 1365 |
{ |
| 1366 |
if (!softmix_data->bitrate) { |
| 1367 |
softmix_data->bitrate = bitrate; |
| 1368 |
return; |
| 1369 |
} |
| 1370 |
|
| 1371 |
switch (bridge->softmix.video_mode.mode_data.sfu_data.remb_behavior) { |
| 1372 |
case AST_BRIDGE_VIDEO_SFU_REMB_AVERAGE_ALL: |
| 1373 |
softmix_data->bitrate = (softmix_data->bitrate + bitrate) / 2; |
| 1374 |
break; |
| 1375 |
case AST_BRIDGE_VIDEO_SFU_REMB_LOWEST_ALL: |
| 1376 |
if (bitrate < softmix_data->bitrate) { |
| 1377 |
softmix_data->bitrate = bitrate; |
| 1378 |
} |
| 1379 |
break; |
| 1380 |
case AST_BRIDGE_VIDEO_SFU_REMB_HIGHEST_ALL: |
| 1381 |
if (bitrate > softmix_data->bitrate) { |
| 1382 |
softmix_data->bitrate = bitrate; |
| 1383 |
} |
| 1384 |
break; |
| 1385 |
case AST_BRIDGE_VIDEO_SFU_REMB_AVERAGE: |
| 1386 |
case AST_BRIDGE_VIDEO_SFU_REMB_LOWEST: |
| 1387 |
case AST_BRIDGE_VIDEO_SFU_REMB_HIGHEST: |
| 1388 |
case AST_BRIDGE_VIDEO_SFU_REMB_FORCE: |
| 1389 |
|
| 1390 |
break; |
| 1391 |
} |
| 1392 |
} |
| 1393 |
|
| 1394 |
static void remb_collect_report(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, |
| 1395 |
struct softmix_bridge_data *softmix_data, struct softmix_channel *sc) |
| 1396 |
{ |
| 1397 |
int i; |
| 1398 |
float bitrate; |
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
if (!AST_VECTOR_SIZE(&sc->video_sources)) { |
| 1404 |
return; |
| 1405 |
} |
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
if (bridge->softmix.video_mode.mode_data.sfu_data.remb_behavior == AST_BRIDGE_VIDEO_SFU_REMB_FORCE) { |
| 1412 |
softmix_data->bitrate = bridge->softmix.video_mode.mode_data.sfu_data.estimated_bitrate; |
| 1413 |
return; |
| 1414 |
} |
| 1415 |
|
| 1416 |
bitrate = (sc->remb.br_mantissa << sc->remb.br_exp) / AST_VECTOR_SIZE(&sc->video_sources); |
| 1417 |
|
| 1418 |
|
| 1419 |
if (!bitrate) { |
| 1420 |
return; |
| 1421 |
} |
| 1422 |
|
| 1423 |
|
| 1424 |
if (bridge->softmix.video_mode.mode_data.sfu_data.remb_behavior == AST_BRIDGE_VIDEO_SFU_REMB_AVERAGE_ALL || |
| 1425 |
bridge->softmix.video_mode.mode_data.sfu_data.remb_behavior == AST_BRIDGE_VIDEO_SFU_REMB_LOWEST_ALL || |
| 1426 |
bridge->softmix.video_mode.mode_data.sfu_data.remb_behavior == AST_BRIDGE_VIDEO_SFU_REMB_HIGHEST_ALL) { |
| 1427 |
remb_collect_report_all(bridge, softmix_data, bitrate); |
| 1428 |
return; |
| 1429 |
} |
| 1430 |
|
| 1431 |
for (i = 0; i < AST_VECTOR_SIZE(&sc->video_sources); ++i) { |
| 1432 |
struct softmix_remb_collector *collector; |
| 1433 |
|
| 1434 |
|
| 1435 |
collector = AST_VECTOR_GET(&softmix_data->remb_collectors, AST_VECTOR_GET(&sc->video_sources, i)); |
| 1436 |
|
| 1437 |
if (!collector->bitrate) { |
| 1438 |
collector->bitrate = bitrate; |
| 1439 |
continue; |
| 1440 |
} |
| 1441 |
|
| 1442 |
switch (bridge->softmix.video_mode.mode_data.sfu_data.remb_behavior) { |
| 1443 |
case AST_BRIDGE_VIDEO_SFU_REMB_AVERAGE: |
| 1444 |
collector->bitrate = (collector->bitrate + bitrate) / 2; |
| 1445 |
break; |
| 1446 |
case AST_BRIDGE_VIDEO_SFU_REMB_LOWEST: |
| 1447 |
if (bitrate < collector->bitrate) { |
| 1448 |
collector->bitrate = bitrate; |
| 1449 |
} |
| 1450 |
break; |
| 1451 |
case AST_BRIDGE_VIDEO_SFU_REMB_HIGHEST: |
| 1452 |
if (bitrate > collector->bitrate) { |
| 1453 |
collector->bitrate = bitrate; |
| 1454 |
} |
| 1455 |
break; |
| 1456 |
case AST_BRIDGE_VIDEO_SFU_REMB_AVERAGE_ALL: |
| 1457 |
case AST_BRIDGE_VIDEO_SFU_REMB_LOWEST_ALL: |
| 1458 |
case AST_BRIDGE_VIDEO_SFU_REMB_HIGHEST_ALL: |
| 1459 |
|
| 1460 |
break; |
| 1461 |
case AST_BRIDGE_VIDEO_SFU_REMB_FORCE: |
| 1462 |
|
| 1463 |
break; |
| 1464 |
} |
| 1465 |
} |
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
sc->remb.br_mantissa = 0; |
| 1471 |
sc->remb.br_exp = 0; |
| 1472 |
} |
| 1473 |
|
| 1474 |
static void remb_send_report(struct ast_bridge_channel *bridge_channel, struct softmix_bridge_data *softmix_data, |
| 1475 |
struct softmix_channel *sc) |
| 1476 |
{ |
| 1477 |
float bitrate = softmix_data->bitrate; |
| 1478 |
int i; |
| 1479 |
int exp; |
| 1480 |
|
| 1481 |
if (!sc->remb_collector) { |
| 1482 |
return; |
| 1483 |
} |
| 1484 |
|
| 1485 |
|
| 1486 |
if (!bitrate) { |
| 1487 |
bitrate = sc->remb_collector->bitrate; |
| 1488 |
sc->remb_collector->bitrate = 0; |
| 1489 |
} |
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
frexp(bitrate, &exp); |
| 1535 |
exp = exp > 18 ? exp - 18 : 0; |
| 1536 |
|
| 1537 |
sc->remb_collector->feedback.remb.br_mantissa = bitrate / (1 << exp); |
| 1538 |
sc->remb_collector->feedback.remb.br_exp = exp; |
| 1539 |
|
| 1540 |
for (i = 0; i < AST_VECTOR_SIZE(&bridge_channel->stream_map.to_bridge); ++i) { |
| 1541 |
int bridge_num = AST_VECTOR_GET(&bridge_channel->stream_map.to_bridge, i); |
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
|
| 1546 |
if (bridge_num == -1) { |
| 1547 |
continue; |
| 1548 |
} |
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
|
| 1554 |
|
| 1555 |
sc->remb_collector->frame.stream_num = bridge_num; |
| 1556 |
ast_bridge_channel_queue_frame(bridge_channel, &sc->remb_collector->frame); |
| 1557 |
} |
| 1558 |
} |
| 1559 |
|
| 1560 |
static void gather_softmix_stats(struct softmix_stats *stats, |
| 1561 |
const struct softmix_bridge_data *softmix_data, |
| 1562 |
struct ast_bridge_channel *bridge_channel) |
| 1563 |
{ |
| 1564 |
int channel_native_rate; |
| 1565 |
|
| 1566 |
|
| 1567 |
ast_channel_lock(bridge_channel->chan); |
| 1568 |
channel_native_rate = MAX(SOFTMIX_MIN_SAMPLE_RATE, |
| 1569 |
ast_format_get_sample_rate(ast_channel_rawreadformat(bridge_channel->chan))); |
| 1570 |
ast_channel_unlock(bridge_channel->chan); |
| 1571 |
|
| 1572 |
if (stats->highest_supported_rate < channel_native_rate) { |
| 1573 |
stats->highest_supported_rate = channel_native_rate; |
| 1574 |
} |
| 1575 |
if (stats->maximum_rate && stats->maximum_rate < channel_native_rate) { |
| 1576 |
stats->num_above_maximum_rate++; |
| 1577 |
} else if (softmix_data->internal_rate < channel_native_rate) { |
| 1578 |
int i; |
| 1579 |
|
| 1580 |
for (i = 0; i < ARRAY_LEN(stats->sample_rates); i++) { |
| 1581 |
if (stats->sample_rates[i] == channel_native_rate) { |
| 1582 |
stats->num_channels[i]++; |
| 1583 |
break; |
| 1584 |
} else if (!stats->sample_rates[i]) { |
| 1585 |
stats->sample_rates[i] = channel_native_rate; |
| 1586 |
stats->num_channels[i]++; |
| 1587 |
break; |
| 1588 |
} |
| 1589 |
} |
| 1590 |
stats->num_above_internal_rate++; |
| 1591 |
} else if (softmix_data->internal_rate == channel_native_rate) { |
| 1592 |
stats->num_at_internal_rate++; |
| 1593 |
} |
| 1594 |
} |
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
static unsigned int analyse_softmix_stats(struct softmix_stats *stats, |
| 1605 |
struct softmix_bridge_data *softmix_data, int binaural_active) |
| 1606 |
{ |
| 1607 |
int i; |
| 1608 |
|
| 1609 |
if (binaural_active) { |
| 1610 |
stats->locked_rate = SOFTMIX_BINAURAL_SAMPLE_RATE; |
| 1611 |
} |
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
if (stats->locked_rate) { |
| 1621 |
|
| 1622 |
|
| 1623 |
if (softmix_data->internal_rate != stats->locked_rate) { |
| 1624 |
ast_debug(1, "Locking at new rate. Bridge changed from %u to %u.\n", |
| 1625 |
softmix_data->internal_rate, stats->locked_rate); |
| 1626 |
softmix_data->internal_rate = stats->locked_rate; |
| 1627 |
return 1; |
| 1628 |
} |
| 1629 |
} else if (stats->num_above_maximum_rate) { |
| 1630 |
|
| 1631 |
|
| 1632 |
if (softmix_data->internal_rate != stats->maximum_rate) { |
| 1633 |
ast_debug(1, "Locking at new maximum rate. Bridge changed from %u to %u.\n", |
| 1634 |
softmix_data->internal_rate, stats->maximum_rate); |
| 1635 |
softmix_data->internal_rate = stats->maximum_rate; |
| 1636 |
return 1; |
| 1637 |
} |
| 1638 |
} else if (stats->num_above_internal_rate >= 2) { |
| 1639 |
|
| 1640 |
unsigned int best_rate = stats->highest_supported_rate; |
| 1641 |
int best_index = -1; |
| 1642 |
|
| 1643 |
for (i = 0; i < ARRAY_LEN(stats->num_channels); i++) { |
| 1644 |
if (stats->num_channels[i]) { |
| 1645 |
break; |
| 1646 |
} |
| 1647 |
if (2 <= stats->num_channels[i]) { |
| 1648 |
|
| 1649 |
if (best_index == -1 |
| 1650 |
|| stats->sample_rates[best_index] < stats->sample_rates[i]) { |
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
|
| 1657 |
|
| 1658 |
|
| 1659 |
|
| 1660 |
|
| 1661 |
|
| 1662 |
best_rate = stats->sample_rates[i]; |
| 1663 |
best_index = i; |
| 1664 |
} |
| 1665 |
} else if (best_index == -1) { |
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
best_rate = MIN(best_rate, stats->sample_rates[i]); |
| 1676 |
} |
| 1677 |
} |
| 1678 |
|
| 1679 |
ast_debug(1, "Multiple above internal rate. Bridge changed from %u to %u.\n", |
| 1680 |
softmix_data->internal_rate, best_rate); |
| 1681 |
softmix_data->internal_rate = best_rate; |
| 1682 |
return 1; |
| 1683 |
} else if (!stats->num_at_internal_rate && !stats->num_above_internal_rate) { |
| 1684 |
|
| 1685 |
ast_debug(1, "All below internal rate. Bridge changed from %u to %u.\n", |
| 1686 |
softmix_data->internal_rate, stats->highest_supported_rate); |
| 1687 |
softmix_data->internal_rate = stats->highest_supported_rate; |
| 1688 |
return 1; |
| 1689 |
} |
| 1690 |
return 0; |
| 1691 |
} |
| 1692 |
|
| 1693 |
static int softmix_mixing_array_init(struct softmix_mixing_array *mixing_array, |
| 1694 |
unsigned int starting_num_entries, unsigned int binaural_active) |
| 1695 |
{ |
| 1696 |
memset(mixing_array, 0, sizeof(*mixing_array)); |
| 1697 |
mixing_array->max_num_entries = starting_num_entries; |
| 1698 |
if (!(mixing_array->buffers = ast_calloc(mixing_array->max_num_entries, sizeof(int16_t *)))) { |
| 1699 |
ast_log(LOG_NOTICE, "Failed to allocate softmix mixing structure.\n"); |
| 1700 |
return -1; |
| 1701 |
} |
| 1702 |
if (binaural_active) { |
| 1703 |
if (!(mixing_array->chan_pairs = ast_calloc(mixing_array->max_num_entries, |
| 1704 |
sizeof(struct convolve_channel_pair *)))) { |
| 1705 |
ast_log(LOG_NOTICE, "Failed to allocate softmix mixing structure.\n"); |
| 1706 |
return -1; |
| 1707 |
} |
| 1708 |
} |
| 1709 |
return 0; |
| 1710 |
} |
| 1711 |
|
| 1712 |
static void softmix_mixing_array_destroy(struct softmix_mixing_array *mixing_array, |
| 1713 |
unsigned int binaural_active) |
| 1714 |
{ |
| 1715 |
ast_free(mixing_array->buffers); |
| 1716 |
if (binaural_active) { |
| 1717 |
ast_free(mixing_array->chan_pairs); |
| 1718 |
} |
| 1719 |
} |
| 1720 |
|
| 1721 |
static int softmix_mixing_array_grow(struct softmix_mixing_array *mixing_array, |
| 1722 |
unsigned int num_entries, unsigned int binaural_active) |
| 1723 |
{ |
| 1724 |
int16_t **tmp; |
| 1725 |
|
| 1726 |
|
| 1727 |
mixing_array->max_num_entries = num_entries; |
| 1728 |
if (!(tmp = ast_realloc(mixing_array->buffers, (mixing_array->max_num_entries * sizeof(int16_t *))))) { |
| 1729 |
ast_log(LOG_NOTICE, "Failed to re-allocate softmix mixing structure.\n"); |
| 1730 |
return -1; |
| 1731 |
} |
| 1732 |
mixing_array->buffers = tmp; |
| 1733 |
|
| 1734 |
if (binaural_active) { |
| 1735 |
struct convolve_channel_pair **tmp2; |
| 1736 |
if (!(tmp2 = ast_realloc(mixing_array->chan_pairs, |
| 1737 |
(mixing_array->max_num_entries * sizeof(struct convolve_channel_pair *))))) { |
| 1738 |
ast_log(LOG_NOTICE, "Failed to re-allocate softmix mixing structure.\n"); |
| 1739 |
return -1; |
| 1740 |
} |
| 1741 |
mixing_array->chan_pairs = tmp2; |
| 1742 |
} |
| 1743 |
return 0; |
| 1744 |
} |
| 1745 |
|
| 1746 |
|
| 1747 |
|
| 1748 |
|
| 1749 |
|
| 1750 |
|
| 1751 |
|
| 1752 |
static int softmix_mixing_loop(struct ast_bridge *bridge) |
| 1753 |
{ |
| 1754 |
struct softmix_stats stats = { { 0 }, }; |
| 1755 |
struct softmix_mixing_array mixing_array; |
| 1756 |
struct softmix_bridge_data *softmix_data = bridge->tech_pvt; |
| 1757 |
struct ast_timer *timer; |
| 1758 |
struct softmix_translate_helper trans_helper; |
| 1759 |
int16_t buf[MAX_DATALEN]; |
| 1760 |
#ifdef BINAURAL_RENDERING |
| 1761 |
int16_t bin_buf[MAX_DATALEN]; |
| 1762 |
int16_t ann_buf[MAX_DATALEN]; |
| 1763 |
#endif |
| 1764 |
unsigned int stat_iteration_counter = 0; |
| 1765 |
int timingfd; |
| 1766 |
int update_all_rates = 0; |
| 1767 |
unsigned int idx; |
| 1768 |
unsigned int x; |
| 1769 |
int res = -1; |
| 1770 |
|
| 1771 |
timer = softmix_data->timer; |
| 1772 |
timingfd = ast_timer_fd(timer); |
| 1773 |
softmix_translate_helper_init(&trans_helper, softmix_data->internal_rate); |
| 1774 |
ast_timer_set_rate(timer, (1000 / softmix_data->internal_mixing_interval)); |
| 1775 |
|
| 1776 |
|
| 1777 |
if (softmix_mixing_array_init(&mixing_array, bridge->num_channels + 10, |
| 1778 |
bridge->softmix.binaural_active)) { |
| 1779 |
goto softmix_cleanup; |
| 1780 |
} |
| 1781 |
|
| 1782 |
|
| 1783 |
|
| 1784 |
|
| 1785 |
|
| 1786 |
while (!softmix_data->stop && bridge->num_active) { |
| 1787 |
struct ast_bridge_channel *bridge_channel; |
| 1788 |
int timeout = -1; |
| 1789 |
struct ast_format *cur_slin = ast_format_cache_get_slin_by_rate(softmix_data->internal_rate); |
| 1790 |
unsigned int softmix_samples = SOFTMIX_SAMPLES(softmix_data->internal_rate, softmix_data->internal_mixing_interval); |
| 1791 |
unsigned int softmix_datalen = SOFTMIX_DATALEN(softmix_data->internal_rate, softmix_data->internal_mixing_interval); |
| 1792 |
int remb_update = 0; |
| 1793 |
|
| 1794 |
if (softmix_datalen > MAX_DATALEN) { |
| 1795 |
|
| 1796 |
|
| 1797 |
|
| 1798 |
|
| 1799 |
ast_log(LOG_WARNING, |
| 1800 |
"Bridge %s: Conference mixing error, requested mixing length greater than mixing buffer.\n", |
| 1801 |
bridge->uniqueid); |
| 1802 |
goto softmix_cleanup; |
| 1803 |
} |
| 1804 |
|
| 1805 |
|
| 1806 |
if (mixing_array.max_num_entries < bridge->num_channels |
| 1807 |
&& softmix_mixing_array_grow(&mixing_array, bridge->num_channels + 5, |
| 1808 |
bridge->softmix.binaural_active)) { |
| 1809 |
goto softmix_cleanup; |
| 1810 |
} |
| 1811 |
|
| 1812 |
|
| 1813 |
|
| 1814 |
mixing_array.used_entries = 0; |
| 1815 |
|
| 1816 |
|
| 1817 |
if (!stat_iteration_counter) { |
| 1818 |
memset(&stats, 0, sizeof(stats)); |
| 1819 |
stats.locked_rate = bridge->softmix.internal_sample_rate; |
| 1820 |
stats.maximum_rate = bridge->softmix.maximum_sample_rate; |
| 1821 |
} |
| 1822 |
|
| 1823 |
|
| 1824 |
if (update_all_rates) { |
| 1825 |
softmix_translate_helper_change_rate(&trans_helper, softmix_data->internal_rate); |
| 1826 |
} |
| 1827 |
|
| 1828 |
#ifdef BINAURAL_RENDERING |
| 1829 |
check_binaural_position_change(bridge, softmix_data); |
| 1830 |
#endif |
| 1831 |
|
| 1832 |
|
| 1833 |
if (bridge->softmix.video_mode.mode == AST_BRIDGE_VIDEO_MODE_SFU && |
| 1834 |
bridge->softmix.video_mode.mode_data.sfu_data.remb_send_interval && |
| 1835 |
ast_tvdiff_ms(ast_tvnow(), softmix_data->last_remb_update) > bridge->softmix.video_mode.mode_data.sfu_data.remb_send_interval) { |
| 1836 |
remb_update = 1; |
| 1837 |
softmix_data->last_remb_update = ast_tvnow(); |
| 1838 |
} |
| 1839 |
|
| 1840 |
|
| 1841 |
AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) { |
| 1842 |
struct softmix_channel *sc = bridge_channel->tech_pvt; |
| 1843 |
|
| 1844 |
if (!sc) { |
| 1845 |
|
| 1846 |
continue; |
| 1847 |
} |
| 1848 |
|
| 1849 |
|
| 1850 |
if (update_all_rates) { |
| 1851 |
set_softmix_bridge_data(softmix_data->internal_rate, |
| 1852 |
softmix_data->internal_mixing_interval, bridge_channel, 1, -1, -1, -1); |
| 1853 |
} |
| 1854 |
|
| 1855 |
|
| 1856 |
if (!stat_iteration_counter) { |
| 1857 |
gather_softmix_stats(&stats, softmix_data, bridge_channel); |
| 1858 |
} |
| 1859 |
|
| 1860 |
|
| 1861 |
if (bridge_channel->suspended) { |
| 1862 |
continue; |
| 1863 |
} |
| 1864 |
|
| 1865 |
|
| 1866 |
ast_mutex_lock(&sc->lock); |
| 1867 |
if ((mixing_array.buffers[mixing_array.used_entries] = softmix_process_read_audio(sc, softmix_samples))) { |
| 1868 |
#ifdef BINAURAL_RENDERING |
| 1869 |
add_binaural_mixing(bridge, softmix_data, softmix_samples, &mixing_array, sc, |
| 1870 |
ast_channel_name(bridge_channel->chan)); |
| 1871 |
#endif |
| 1872 |
mixing_array.used_entries++; |
| 1873 |
} |
| 1874 |
if (remb_update) { |
| 1875 |
remb_collect_report(bridge, bridge_channel, softmix_data, sc); |
| 1876 |
} |
| 1877 |
ast_mutex_unlock(&sc->lock); |
| 1878 |
} |
| 1879 |
|
| 1880 |
|
| 1881 |
memset(buf, 0, softmix_datalen); |
| 1882 |
for (idx = 0; idx < mixing_array.used_entries; ++idx) { |
| 1883 |
for (x = 0; x < softmix_samples; ++x) { |
| 1884 |
ast_slinear_saturated_add(buf + x, mixing_array.buffers[idx] + x); |
| 1885 |
} |
| 1886 |
} |
| 1887 |
|
| 1888 |
#ifdef BINAURAL_RENDERING |
| 1889 |
binaural_mixing(bridge, softmix_data, &mixing_array, bin_buf, ann_buf); |
| 1890 |
#endif |
| 1891 |
|
| 1892 |
|
| 1893 |
AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) { |
| 1894 |
struct softmix_channel *sc = bridge_channel->tech_pvt; |
| 1895 |
|
| 1896 |
if (!sc || bridge_channel->suspended) { |
| 1897 |
|
| 1898 |
continue; |
| 1899 |
} |
| 1900 |
|
| 1901 |
ast_mutex_lock(&sc->lock); |
| 1902 |
|
| 1903 |
|
| 1904 |
ao2_t_replace(sc->write_frame.subclass.format, cur_slin, |
| 1905 |
"Replace softmix channel slin format"); |
| 1906 |
#ifdef BINAURAL_RENDERING |
| 1907 |
if (bridge->softmix.binaural_active && softmix_data->convolve.binaural_active |
| 1908 |
&& sc->binaural) { |
| 1909 |
create_binaural_frame(bridge_channel, sc, bin_buf, ann_buf, softmix_datalen, |
| 1910 |
softmix_samples, buf); |
| 1911 |
} else |
| 1912 |
#endif |
| 1913 |
{ |
| 1914 |
sc->write_frame.datalen = softmix_datalen; |
| 1915 |
sc->write_frame.samples = softmix_samples; |
| 1916 |
memcpy(sc->final_buf, buf, softmix_datalen); |
| 1917 |
} |
| 1918 |
|
| 1919 |
softmix_process_write_audio(&trans_helper, |
| 1920 |
ast_channel_rawwriteformat(bridge_channel->chan), sc, |
| 1921 |
softmix_data->default_sample_size); |
| 1922 |
|
| 1923 |
ast_mutex_unlock(&sc->lock); |
| 1924 |
|
| 1925 |
|
| 1926 |
ast_bridge_channel_queue_frame(bridge_channel, &sc->write_frame); |
| 1927 |
|
| 1928 |
if (remb_update) { |
| 1929 |
remb_send_report(bridge_channel, softmix_data, sc); |
| 1930 |
} |
| 1931 |
} |
| 1932 |
|
| 1933 |
if (remb_update) { |
| 1934 |
|
| 1935 |
softmix_data->bitrate = 0; |
| 1936 |
} |
| 1937 |
|
| 1938 |
update_all_rates = 0; |
| 1939 |
if (!stat_iteration_counter) { |
| 1940 |
update_all_rates = analyse_softmix_stats(&stats, softmix_data, |
| 1941 |
bridge->softmix.binaural_active); |
| 1942 |
stat_iteration_counter = SOFTMIX_STAT_INTERVAL; |
| 1943 |
} |
| 1944 |
stat_iteration_counter--; |
| 1945 |
|
| 1946 |
ast_bridge_unlock(bridge); |
| 1947 |
|
| 1948 |
softmix_translate_helper_cleanup(&trans_helper); |
| 1949 |
|
| 1950 |
ast_waitfor_n_fd(&timingfd, 1, &timeout, NULL); |
| 1951 |
if (ast_timer_ack(timer, 1) < 0) { |
| 1952 |
ast_log(LOG_ERROR, "Bridge %s: Failed to acknowledge timer in softmix.\n", |
| 1953 |
bridge->uniqueid); |
| 1954 |
ast_bridge_lock(bridge); |
| 1955 |
goto softmix_cleanup; |
| 1956 |
} |
| 1957 |
ast_bridge_lock(bridge); |
| 1958 |
|
| 1959 |
|
| 1960 |
if (bridge->softmix.internal_mixing_interval |
| 1961 |
&& (bridge->softmix.internal_mixing_interval != softmix_data->internal_mixing_interval)) { |
| 1962 |
softmix_data->internal_mixing_interval = bridge->softmix.internal_mixing_interval; |
| 1963 |
ast_timer_set_rate(timer, (1000 / softmix_data->internal_mixing_interval)); |
| 1964 |
update_all_rates = 1; |
| 1965 |
} |
| 1966 |
} |
| 1967 |
|
| 1968 |
res = 0; |
| 1969 |
|
| 1970 |
softmix_cleanup: |
| 1971 |
softmix_translate_helper_destroy(&trans_helper); |
| 1972 |
softmix_mixing_array_destroy(&mixing_array, bridge->softmix.binaural_active); |
| 1973 |
return res; |
| 1974 |
} |
| 1975 |
|
| 1976 |
|
| 1977 |
|
| 1978 |
|
| 1979 |
|
| 1980 |
|
| 1981 |
|
| 1982 |
|
| 1983 |
|
| 1984 |
|
| 1985 |
static void *softmix_mixing_thread(void *data) |
| 1986 |
{ |
| 1987 |
struct softmix_bridge_data *softmix_data = data; |
| 1988 |
struct ast_bridge *bridge = softmix_data->bridge; |
| 1989 |
|
| 1990 |
ast_bridge_lock(bridge); |
| 1991 |
if (bridge->callid) { |
| 1992 |
ast_callid_threadassoc_add(bridge->callid); |
| 1993 |
} |
| 1994 |
|
| 1995 |
ast_debug(1, "Bridge %s: starting mixing thread\n", bridge->uniqueid); |
| 1996 |
|
| 1997 |
while (!softmix_data->stop) { |
| 1998 |
if (!bridge->num_active) { |
| 1999 |
|
| 2000 |
ast_bridge_unlock(bridge); |
| 2001 |
ast_mutex_lock(&softmix_data->lock); |
| 2002 |
if (!softmix_data->stop) { |
| 2003 |
ast_cond_wait(&softmix_data->cond, &softmix_data->lock); |
| 2004 |
} |
| 2005 |
ast_mutex_unlock(&softmix_data->lock); |
| 2006 |
ast_bridge_lock(bridge); |
| 2007 |
continue; |
| 2008 |
} |
| 2009 |
|
| 2010 |
if (bridge->softmix.binaural_active && !softmix_data->binaural_init) { |
| 2011 |
#ifndef BINAURAL_RENDERING |
| 2012 |
ast_bridge_lock(bridge); |
| 2013 |
bridge->softmix.binaural_active = 0; |
| 2014 |
ast_bridge_unlock(bridge); |
| 2015 |
ast_log(LOG_WARNING, "Bridge: %s: Binaural rendering active by config but not " |
| 2016 |
"compiled.\n", bridge->uniqueid); |
| 2017 |
#else |
| 2018 |
|
| 2019 |
softmix_data->internal_rate = SOFTMIX_BINAURAL_SAMPLE_RATE; |
| 2020 |
softmix_data->default_sample_size = SOFTMIX_SAMPLES(softmix_data->internal_rate, |
| 2021 |
softmix_data->internal_mixing_interval); |
| 2022 |
|
| 2023 |
if (init_convolve_data(&softmix_data->convolve, softmix_data->default_sample_size) |
| 2024 |
== -1) { |
| 2025 |
ast_bridge_lock(bridge); |
| 2026 |
bridge->softmix.binaural_active = 0; |
| 2027 |
ast_bridge_unlock(bridge); |
| 2028 |
ast_log(LOG_ERROR, "Bridge: %s: Unable to allocate memory for " |
| 2029 |
"binaural processing, Will only process mono audio.\n", |
| 2030 |
bridge->uniqueid); |
| 2031 |
} |
| 2032 |
softmix_data->binaural_init = 1; |
| 2033 |
#endif |
| 2034 |
} |
| 2035 |
|
| 2036 |
if (softmix_mixing_loop(bridge)) { |
| 2037 |
|
| 2038 |
|
| 2039 |
|
| 2040 |
|
| 2041 |
ast_bridge_unlock(bridge); |
| 2042 |
sleep(1); |
| 2043 |
ast_bridge_lock(bridge); |
| 2044 |
} |
| 2045 |
} |
| 2046 |
|
| 2047 |
ast_bridge_unlock(bridge); |
| 2048 |
|
| 2049 |
ast_debug(1, "Bridge %s: stopping mixing thread\n", bridge->uniqueid); |
| 2050 |
|
| 2051 |
return NULL; |
| 2052 |
} |
| 2053 |
|
| 2054 |
static void softmix_bridge_data_destroy(struct softmix_bridge_data *softmix_data) |
| 2055 |
{ |
| 2056 |
if (softmix_data->timer) { |
| 2057 |
ast_timer_close(softmix_data->timer); |
| 2058 |
softmix_data->timer = NULL; |
| 2059 |
} |
| 2060 |
ast_mutex_destroy(&softmix_data->lock); |
| 2061 |
ast_cond_destroy(&softmix_data->cond); |
| 2062 |
AST_VECTOR_RESET(&softmix_data->remb_collectors, ao2_cleanup); |
| 2063 |
AST_VECTOR_FREE(&softmix_data->remb_collectors); |
| 2064 |
ast_free(softmix_data); |
| 2065 |
} |
| 2066 |
|
| 2067 |
|
| 2068 |
static int softmix_bridge_create(struct ast_bridge *bridge) |
| 2069 |
{ |
| 2070 |
struct softmix_bridge_data *softmix_data; |
| 2071 |
|
| 2072 |
softmix_data = ast_calloc(1, sizeof(*softmix_data)); |
| 2073 |
if (!softmix_data) { |
| 2074 |
return -1; |
| 2075 |
} |
| 2076 |
softmix_data->bridge = bridge; |
| 2077 |
ast_mutex_init(&softmix_data->lock); |
| 2078 |
ast_cond_init(&softmix_data->cond, NULL); |
| 2079 |
softmix_data->timer = ast_timer_open(); |
| 2080 |
if (!softmix_data->timer) { |
| 2081 |
ast_log(AST_LOG_WARNING, "Failed to open timer for softmix bridge\n"); |
| 2082 |
softmix_bridge_data_destroy(softmix_data); |
| 2083 |
return -1; |
| 2084 |
} |
| 2085 |
|
| 2086 |
softmix_data->internal_rate = SOFTMIX_MIN_SAMPLE_RATE; |
| 2087 |
softmix_data->internal_mixing_interval = DEFAULT_SOFTMIX_INTERVAL; |
| 2088 |
|
| 2089 |
#ifdef BINAURAL_RENDERING |
| 2090 |
softmix_data->default_sample_size = SOFTMIX_SAMPLES(softmix_data->internal_rate, |
| 2091 |
softmix_data->internal_mixing_interval); |
| 2092 |
#endif |
| 2093 |
|
| 2094 |
AST_VECTOR_INIT(&softmix_data->remb_collectors, 0); |
| 2095 |
|
| 2096 |
bridge->tech_pvt = softmix_data; |
| 2097 |
|
| 2098 |
|
| 2099 |
if (ast_pthread_create(&softmix_data->thread, NULL, softmix_mixing_thread, |
| 2100 |
softmix_data)) { |
| 2101 |
softmix_data->thread = AST_PTHREADT_NULL; |
| 2102 |
softmix_bridge_data_destroy(softmix_data); |
| 2103 |
bridge->tech_pvt = NULL; |
| 2104 |
return -1; |
| 2105 |
} |
| 2106 |
|
| 2107 |
return 0; |
| 2108 |
} |
| 2109 |
|
| 2110 |
|
| 2111 |
|
| 2112 |
|
| 2113 |
|
| 2114 |
|
| 2115 |
|
| 2116 |
|
| 2117 |
static void softmix_bridge_stop(struct ast_bridge *bridge) |
| 2118 |
{ |
| 2119 |
struct softmix_bridge_data *softmix_data; |
| 2120 |
|
| 2121 |
softmix_data = bridge->tech_pvt; |
| 2122 |
if (!softmix_data) { |
| 2123 |
return; |
| 2124 |
} |
| 2125 |
|
| 2126 |
ast_mutex_lock(&softmix_data->lock); |
| 2127 |
softmix_data->stop = 1; |
| 2128 |
ast_mutex_unlock(&softmix_data->lock); |
| 2129 |
} |
| 2130 |
|
| 2131 |
|
| 2132 |
static void softmix_bridge_destroy(struct ast_bridge *bridge) |
| 2133 |
{ |
| 2134 |
struct softmix_bridge_data *softmix_data; |
| 2135 |
pthread_t thread; |
| 2136 |
|
| 2137 |
softmix_data = bridge->tech_pvt; |
| 2138 |
if (!softmix_data) { |
| 2139 |
return; |
| 2140 |
} |
| 2141 |
|
| 2142 |
|
| 2143 |
ast_mutex_lock(&softmix_data->lock); |
| 2144 |
softmix_data->stop = 1; |
| 2145 |
ast_cond_signal(&softmix_data->cond); |
| 2146 |
thread = softmix_data->thread; |
| 2147 |
softmix_data->thread = AST_PTHREADT_NULL; |
| 2148 |
ast_mutex_unlock(&softmix_data->lock); |
| 2149 |
if (thread != AST_PTHREADT_NULL) { |
| 2150 |
ast_debug(1, "Bridge %s: Waiting for mixing thread to die.\n", bridge->uniqueid); |
| 2151 |
pthread_join(thread, NULL); |
| 2152 |
} |
| 2153 |
#ifdef BINAURAL_RENDERING |
| 2154 |
free_convolve_data(&softmix_data->convolve); |
| 2155 |
#endif |
| 2156 |
softmix_bridge_data_destroy(softmix_data); |
| 2157 |
bridge->tech_pvt = NULL; |
| 2158 |
} |
| 2159 |
|
| 2160 |
|
| 2161 |
|
| 2162 |
|
| 2163 |
|
| 2164 |
|
| 2165 |
|
| 2166 |
|
| 2167 |
|
| 2168 |
static void map_source_to_destinations(const char *source_channel_name, |
| 2169 |
size_t bridge_stream_position, struct ast_bridge_channels_list *participants, int source_channel_stream_position) |
| 2170 |
{ |
| 2171 |
struct ast_bridge_channel *participant; |
| 2172 |
|
| 2173 |
AST_LIST_TRAVERSE(participants, participant, entry) { |
| 2174 |
int i; |
| 2175 |
struct ast_stream_topology *topology; |
| 2176 |
|
| 2177 |
if (!strcmp(source_channel_name, ast_channel_name(participant->chan))) { |
| 2178 |
continue; |
| 2179 |
} |
| 2180 |
|
| 2181 |
ast_bridge_channel_lock(participant); |
| 2182 |
ast_channel_lock(participant->chan); |
| 2183 |
topology = ast_channel_get_stream_topology(participant->chan); |
| 2184 |
|
| 2185 |
for (i = 0; i < ast_stream_topology_get_count(topology); ++i) { |
| 2186 |
struct ast_stream *stream; |
| 2187 |
|
| 2188 |
stream = ast_stream_topology_get_stream(topology, i); |
| 2189 |
if (is_video_dest(stream, source_channel_name, source_channel_stream_position)) { |
| 2190 |
struct softmix_channel *sc = participant->tech_pvt; |
| 2191 |
|
| 2192 |
AST_VECTOR_REPLACE(&participant->stream_map.to_channel, bridge_stream_position, i); |
| 2193 |
AST_VECTOR_APPEND(&sc->video_sources, bridge_stream_position); |
| 2194 |
break; |
| 2195 |
} |
| 2196 |
} |
| 2197 |
ast_channel_unlock(participant->chan); |
| 2198 |
ast_bridge_channel_unlock(participant); |
| 2199 |
} |
| 2200 |
} |
| 2201 |
|
| 2202 |
|
| 2203 |
|
| 2204 |
|
| 2205 |
|
| 2206 |
|
| 2207 |
|
| 2208 |
static struct softmix_remb_collector *remb_collector_alloc(void) |
| 2209 |
{ |
| 2210 |
struct softmix_remb_collector *collector; |
| 2211 |
|
| 2212 |
collector = ao2_alloc_options(sizeof(*collector), NULL, AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 2213 |
if (!collector) { |
| 2214 |
return NULL; |
| 2215 |
} |
| 2216 |
|
| 2217 |
collector->frame.frametype = AST_FRAME_RTCP; |
| 2218 |
collector->frame.subclass.integer = AST_RTP_RTCP_PSFB; |
| 2219 |
collector->feedback.fmt = AST_RTP_RTCP_FMT_REMB; |
| 2220 |
collector->frame.data.ptr = &collector->feedback; |
| 2221 |
collector->frame.datalen = sizeof(collector->feedback); |
| 2222 |
|
| 2223 |
return collector; |
| 2224 |
} |
| 2225 |
|
| 2226 |
|
| 2227 |
|
| 2228 |
|
| 2229 |
|
| 2230 |
|
| 2231 |
|
| 2232 |
|
| 2233 |
static void remb_enable_collection(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, |
| 2234 |
size_t bridge_stream_position) |
| 2235 |
{ |
| 2236 |
struct softmix_channel *sc = bridge_channel->tech_pvt; |
| 2237 |
struct softmix_bridge_data *softmix_data = bridge->tech_pvt; |
| 2238 |
|
| 2239 |
if (!sc->remb_collector) { |
| 2240 |
sc->remb_collector = remb_collector_alloc(); |
| 2241 |
if (!sc->remb_collector) { |
| 2242 |
|
| 2243 |
|
| 2244 |
|
| 2245 |
return; |
| 2246 |
} |
| 2247 |
} |
| 2248 |
|
| 2249 |
ao2_ref(sc->remb_collector, +1); |
| 2250 |
if (AST_VECTOR_REPLACE(&softmix_data->remb_collectors, bridge_stream_position, |
| 2251 |
sc->remb_collector)) { |
| 2252 |
ao2_ref(sc->remb_collector, -1); |
| 2253 |
} |
| 2254 |
} |
| 2255 |
|
| 2256 |
static void softmix_bridge_stream_sources_update(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, |
| 2257 |
struct softmix_channel *sc) |
| 2258 |
{ |
| 2259 |
int index; |
| 2260 |
struct ast_stream_topology *old_topology = sc->topology; |
| 2261 |
struct ast_stream_topology *new_topology = ast_channel_get_stream_topology(bridge_channel->chan); |
| 2262 |
int removed_streams[MAX(ast_stream_topology_get_count(sc->topology), ast_stream_topology_get_count(new_topology))]; |
| 2263 |
size_t removed_streams_count = 0; |
| 2264 |
struct ast_stream_topology *added_streams; |
| 2265 |
struct ast_bridge_channels_list *participants = &bridge->channels; |
| 2266 |
struct ast_bridge_channel *participant; |
| 2267 |
SCOPE_ENTER(3, "%s: OT: %s NT: %s\n", ast_channel_name(bridge_channel->chan), |
| 2268 |
ast_str_tmp(256, ast_stream_topology_to_str(old_topology, &STR_TMP)), |
| 2269 |
ast_str_tmp(256, ast_stream_topology_to_str(new_topology, &STR_TMP))); |
| 2270 |
|
| 2271 |
added_streams = ast_stream_topology_alloc(); |
| 2272 |
if (!added_streams) { |
| 2273 |
SCOPE_EXIT_LOG_RTN(LOG_ERROR, "%s: Couldn't alloc topology\n", ast_channel_name(bridge_channel->chan)); |
| 2274 |
} |
| 2275 |
|
| 2276 |
|
| 2277 |
|
| 2278 |
|
| 2279 |
|
| 2280 |
|
| 2281 |
|
| 2282 |
ast_trace(-1, "%s: Checking for state changes\n", ast_channel_name(bridge_channel->chan)); |
| 2283 |
for (index = 0; index < ast_stream_topology_get_count(sc->topology) && index < ast_stream_topology_get_count(new_topology); ++index) { |
| 2284 |
struct ast_stream *old_stream = ast_stream_topology_get_stream(sc->topology, index); |
| 2285 |
struct ast_stream *new_stream = ast_stream_topology_get_stream(new_topology, index); |
| 2286 |
SCOPE_ENTER(4, "%s: Slot: %d Old stream: %s New stream: %s\n", ast_channel_name(bridge_channel->chan), |
| 2287 |
index, ast_str_tmp(256, ast_stream_to_str(old_stream, &STR_TMP)), |
| 2288 |
ast_str_tmp(256, ast_stream_to_str(new_stream, &STR_TMP))); |
| 2289 |
|
| 2290 |
|
| 2291 |
if ((ast_stream_get_type(old_stream) != AST_MEDIA_TYPE_VIDEO && ast_stream_get_type(new_stream) != AST_MEDIA_TYPE_VIDEO) || |
| 2292 |
!strncmp(ast_stream_get_name(new_stream), SOFTBRIDGE_VIDEO_DEST_PREFIX, |
| 2293 |
SOFTBRIDGE_VIDEO_DEST_LEN)) { |
| 2294 |
SCOPE_EXIT_EXPR(continue, "%s: Stream %d ignored\n", ast_channel_name(bridge_channel->chan), index); |
| 2295 |
} |
| 2296 |
|
| 2297 |
if (ast_stream_get_type(old_stream) == AST_MEDIA_TYPE_VIDEO && ast_stream_get_type(new_stream) != AST_MEDIA_TYPE_VIDEO) { |
| 2298 |
|
| 2299 |
ast_trace(-1, "%s: Stream %d added to remove list\n", ast_channel_name(bridge_channel->chan), index); |
| 2300 |
removed_streams[removed_streams_count++] = index; |
| 2301 |
} else if (ast_stream_get_type(old_stream) != AST_MEDIA_TYPE_VIDEO && ast_stream_get_type(new_stream) == AST_MEDIA_TYPE_VIDEO) { |
| 2302 |
if (ast_stream_get_state(new_stream) != AST_STREAM_STATE_REMOVED) { |
| 2303 |
|
| 2304 |
if (append_source_stream(added_streams, ast_channel_name(bridge_channel->chan), |
| 2305 |
bridge->softmix.send_sdp_label ? ast_channel_uniqueid(bridge_channel->chan) : NULL, |
| 2306 |
new_stream, index)) { |
| 2307 |
SCOPE_EXIT_EXPR(goto cleanup, "%s: Couldn't append source stream %d:%s\n", ast_channel_name(bridge_channel->chan), |
| 2308 |
index, ast_stream_get_name(new_stream)); |
| 2309 |
} |
| 2310 |
ast_trace(-1, "%s: Stream %d changed from non-video to video\n", ast_channel_name(bridge_channel->chan), index); |
| 2311 |
} |
| 2312 |
} else if (ast_stream_get_state(old_stream) != AST_STREAM_STATE_REMOVED && |
| 2313 |
ast_stream_get_state(new_stream) != AST_STREAM_STATE_SENDRECV && ast_stream_get_state(new_stream) != AST_STREAM_STATE_RECVONLY) { |
| 2314 |
ast_trace(-1, "%s: Stream %d added to remove list\n", ast_channel_name(bridge_channel->chan), index); |
| 2315 |
|
| 2316 |
removed_streams[removed_streams_count++] = index; |
| 2317 |
} else if ((ast_stream_get_state(old_stream) == AST_STREAM_STATE_REMOVED || ast_stream_get_state(old_stream) == AST_STREAM_STATE_INACTIVE || |
| 2318 |
ast_stream_get_state(old_stream) == AST_STREAM_STATE_SENDONLY) && |
| 2319 |
ast_stream_get_state(new_stream) != AST_STREAM_STATE_INACTIVE && ast_stream_get_state(new_stream) != AST_STREAM_STATE_SENDONLY && |
| 2320 |
ast_stream_get_state(new_stream) != AST_STREAM_STATE_REMOVED) { |
| 2321 |
|
| 2322 |
if (append_source_stream(added_streams, ast_channel_name(bridge_channel->chan), |
| 2323 |
bridge->softmix.send_sdp_label ? ast_channel_uniqueid(bridge_channel->chan) : NULL, |
| 2324 |
new_stream, index)) { |
| 2325 |
SCOPE_EXIT_EXPR(goto cleanup, "%s: Couldn't append source stream %d:%s\n", ast_channel_name(bridge_channel->chan), |
| 2326 |
index, ast_stream_get_name(new_stream)); |
| 2327 |
} |
| 2328 |
ast_trace(-1, "%s: Stream %d:%s changed state from %s to %s\n", ast_channel_name(bridge_channel->chan), |
| 2329 |
index, ast_stream_get_name(old_stream), ast_stream_state2str(ast_stream_get_state(old_stream)), |
| 2330 |
ast_stream_state2str(ast_stream_get_state(new_stream))); |
| 2331 |
} else { |
| 2332 |
ast_trace(-1, "%s: Stream %d:%s didn't do anything\n", ast_channel_name(bridge_channel->chan), |
| 2333 |
index, ast_stream_get_name(old_stream)); |
| 2334 |
} |
| 2335 |
SCOPE_EXIT(); |
| 2336 |
} |
| 2337 |
|
| 2338 |
|
| 2339 |
|
| 2340 |
|
| 2341 |
|
| 2342 |
|
| 2343 |
ast_trace(-1, "%s: Checking for newly added streams\n", ast_channel_name(bridge_channel->chan)); |
| 2344 |
|
| 2345 |
for (; index < ast_stream_topology_get_count(new_topology); ++index) { |
| 2346 |
struct ast_stream *stream = ast_stream_topology_get_stream(new_topology, index); |
| 2347 |
SCOPE_ENTER(4, "%s: Checking stream %d:%s\n", ast_channel_name(bridge_channel->chan), index, |
| 2348 |
ast_stream_get_name(stream)); |
| 2349 |
|
| 2350 |
if (!is_video_source(stream)) { |
| 2351 |
SCOPE_EXIT_EXPR(continue, "%s: Stream %d:%s is not video source\n", ast_channel_name(bridge_channel->chan), |
| 2352 |
index, ast_stream_get_name(stream)); |
| 2353 |
} |
| 2354 |
|
| 2355 |
if (append_source_stream(added_streams, ast_channel_name(bridge_channel->chan), |
| 2356 |
bridge->softmix.send_sdp_label ? ast_channel_uniqueid(bridge_channel->chan) : NULL, |
| 2357 |
stream, index)) { |
| 2358 |
SCOPE_EXIT_EXPR(goto cleanup, "%s: Couldn't append stream %d:%s\n", ast_channel_name(bridge_channel->chan), |
| 2359 |
index, ast_stream_get_name(stream)); |
| 2360 |
} |
| 2361 |
SCOPE_EXIT("%s: Added new stream %s\n", ast_channel_name(bridge_channel->chan), |
| 2362 |
ast_str_tmp(256, ast_stream_to_str(stream, &STR_TMP))); |
| 2363 |
} |
| 2364 |
|
| 2365 |
|
| 2366 |
sc->topology = ast_stream_topology_clone(new_topology); |
| 2367 |
if (!sc->topology) { |
| 2368 |
sc->topology = old_topology; |
| 2369 |
} else { |
| 2370 |
ast_stream_topology_free(old_topology); |
| 2371 |
} |
| 2372 |
|
| 2373 |
|
| 2374 |
|
| 2375 |
|
| 2376 |
if (!removed_streams_count && !ast_stream_topology_get_count(added_streams)) { |
| 2377 |
ast_trace(-1, "%s: Nothing added or removed\n", ast_channel_name(bridge_channel->chan)); |
| 2378 |
goto cleanup; |
| 2379 |
} |
| 2380 |
|
| 2381 |
ast_trace(-1, "%s: Processing adds and removes\n", ast_channel_name(bridge_channel->chan)); |
| 2382 |
|
| 2383 |
AST_LIST_TRAVERSE(participants, participant, entry) |
| 2384 |
{ |
| 2385 |
struct softmix_channel *participant_sc = participant->tech_pvt; |
| 2386 |
SCOPE_ENTER(4, "%s/%s: Old participant topology %s\n", |
| 2387 |
ast_channel_name(bridge_channel->chan), |
| 2388 |
ast_channel_name(participant->chan), |
| 2389 |
ast_str_tmp(256, ast_stream_topology_to_str(participant_sc->topology, &STR_TMP))); |
| 2390 |
|
| 2391 |
if (participant == bridge_channel) { |
| 2392 |
SCOPE_EXIT_EXPR(continue, "%s/%s: Same channel. Skipping\n", |
| 2393 |
ast_channel_name(bridge_channel->chan), |
| 2394 |
ast_channel_name(participant->chan)); |
| 2395 |
} |
| 2396 |
|
| 2397 |
|
| 2398 |
|
| 2399 |
|
| 2400 |
if (append_all_streams(participant_sc->topology, added_streams)) { |
| 2401 |
SCOPE_EXIT_EXPR(goto cleanup, "%s/%s: Couldn't append streams\n", ast_channel_name(bridge_channel->chan), |
| 2402 |
ast_channel_name(participant->chan)); |
| 2403 |
} |
| 2404 |
ast_trace(-1, "%s/%s: Adding streams %s\n", ast_channel_name(bridge_channel->chan), |
| 2405 |
ast_channel_name(participant->chan), |
| 2406 |
ast_str_tmp(256, ast_stream_topology_to_str(added_streams, &STR_TMP))); |
| 2407 |
|
| 2408 |
|
| 2409 |
for (index = 0; |
| 2410 |
removed_streams_count && index < ast_stream_topology_get_count(sc->topology); ++index) { |
| 2411 |
struct ast_stream *stream = ast_stream_topology_get_stream(sc->topology, index); |
| 2412 |
int removed_stream; |
| 2413 |
|
| 2414 |
for (removed_stream = 0; removed_stream < removed_streams_count; ++removed_stream) { |
| 2415 |
if (is_video_dest(stream, ast_channel_name(bridge_channel->chan), |
| 2416 |
removed_streams[removed_stream])) { |
| 2417 |
ast_trace(-1, "%s/%s: Removing stream %s\n", |
| 2418 |
ast_channel_name(bridge_channel->chan), |
| 2419 |
ast_channel_name(participant->chan), |
| 2420 |
ast_str_tmp(256, ast_stream_to_str(stream, &STR_TMP))); |
| 2421 |
ast_stream_set_state(stream, AST_STREAM_STATE_REMOVED); |
| 2422 |
} |
| 2423 |
} |
| 2424 |
} |
| 2425 |
ast_channel_request_stream_topology_change(participant->chan, participant_sc->topology, NULL); |
| 2426 |
SCOPE_EXIT("%s/%s: New participant topology %s\n", |
| 2427 |
ast_channel_name(bridge_channel->chan), |
| 2428 |
ast_channel_name(participant->chan), |
| 2429 |
ast_str_tmp(256, ast_stream_topology_to_str(participant_sc->topology, &STR_TMP))); |
| 2430 |
} |
| 2431 |
|
| 2432 |
ast_trace(-1, "%s: New topology %s\n", ast_channel_name(bridge_channel->chan), |
| 2433 |
ast_str_tmp(256, ast_stream_topology_to_str(sc->topology, &STR_TMP))); |
| 2434 |
|
| 2435 |
cleanup: |
| 2436 |
ast_stream_topology_free(added_streams); |
| 2437 |
SCOPE_EXIT(); |
| 2438 |
} |
| 2439 |
|
| 2440 |
|
| 2441 |
|
| 2442 |
|
| 2443 |
|
| 2444 |
|
| 2445 |
|
| 2446 |
|
| 2447 |
|
| 2448 |
|
| 2449 |
|
| 2450 |
static void softmix_bridge_stream_topology_changed(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel) |
| 2451 |
{ |
| 2452 |
struct softmix_bridge_data *softmix_data = bridge->tech_pvt; |
| 2453 |
struct softmix_channel *sc = bridge_channel->tech_pvt; |
| 2454 |
struct ast_bridge_channel *participant; |
| 2455 |
struct ast_vector_int media_types; |
| 2456 |
int nths[AST_MEDIA_TYPE_END] = {0}; |
| 2457 |
int idx; |
| 2458 |
SCOPE_ENTER(3, "%s: \n", ast_channel_name(bridge_channel->chan)); |
| 2459 |
|
| 2460 |
switch (bridge->softmix.video_mode.mode) { |
| 2461 |
case AST_BRIDGE_VIDEO_MODE_NONE: |
| 2462 |
case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC: |
| 2463 |
case AST_BRIDGE_VIDEO_MODE_TALKER_SRC: |
| 2464 |
default: |
| 2465 |
ast_bridge_channel_stream_map(bridge_channel); |
| 2466 |
SCOPE_EXIT_RTN("%s: Not in SFU mode\n", ast_channel_name(bridge_channel->chan)); |
| 2467 |
case AST_BRIDGE_VIDEO_MODE_SFU: |
| 2468 |
break; |
| 2469 |
} |
| 2470 |
|
| 2471 |
ast_channel_lock(bridge_channel->chan); |
| 2472 |
softmix_bridge_stream_sources_update(bridge, bridge_channel, sc); |
| 2473 |
ast_channel_unlock(bridge_channel->chan); |
| 2474 |
|
| 2475 |
AST_VECTOR_INIT(&media_types, AST_MEDIA_TYPE_END); |
| 2476 |
|
| 2477 |
|
| 2478 |
|
| 2479 |
|
| 2480 |
|
| 2481 |
for (idx = 0; idx < AST_VECTOR_SIZE(&softmix_data->remb_collectors); ++idx) { |
| 2482 |
ao2_cleanup(AST_VECTOR_GET(&softmix_data->remb_collectors, idx)); |
| 2483 |
AST_VECTOR_REPLACE(&softmix_data->remb_collectors, idx, NULL); |
| 2484 |
} |
| 2485 |
|
| 2486 |
|
| 2487 |
AST_LIST_TRAVERSE(&bridge->channels, participant, entry) { |
| 2488 |
ast_bridge_channel_lock(participant); |
| 2489 |
|
| 2490 |
AST_VECTOR_RESET(&participant->stream_map.to_channel, AST_VECTOR_ELEM_CLEANUP_NOOP); |
| 2491 |
AST_VECTOR_RESET(&participant->stream_map.to_bridge, AST_VECTOR_ELEM_CLEANUP_NOOP); |
| 2492 |
|
| 2493 |
sc = participant->tech_pvt; |
| 2494 |
AST_VECTOR_RESET(&sc->video_sources, AST_VECTOR_ELEM_CLEANUP_NOOP); |
| 2495 |
|
| 2496 |
ast_bridge_channel_unlock(participant); |
| 2497 |
} |
| 2498 |
|
| 2499 |
|
| 2500 |
|
| 2501 |
|
| 2502 |
|
| 2503 |
|
| 2504 |
|
| 2505 |
|
| 2506 |
|
| 2507 |
AST_LIST_TRAVERSE(&bridge->channels, participant, entry) { |
| 2508 |
int i; |
| 2509 |
struct ast_stream_topology *topology; |
| 2510 |
|
| 2511 |
ast_bridge_channel_lock(participant); |
| 2512 |
ast_channel_lock(participant->chan); |
| 2513 |
|
| 2514 |
topology = ao2_bump(ast_channel_get_stream_topology(participant->chan)); |
| 2515 |
if (!topology) { |
| 2516 |
|
| 2517 |
ast_channel_unlock(participant->chan); |
| 2518 |
ast_bridge_channel_unlock(participant); |
| 2519 |
continue; |
| 2520 |
} |
| 2521 |
|
| 2522 |
for (i = 0; i < ast_stream_topology_get_count(topology); ++i) { |
| 2523 |
struct ast_stream *stream = ast_stream_topology_get_stream(topology, i); |
| 2524 |
|
| 2525 |
if (is_video_source(stream)) { |
| 2526 |
AST_VECTOR_APPEND(&media_types, AST_MEDIA_TYPE_VIDEO); |
| 2527 |
AST_VECTOR_REPLACE(&participant->stream_map.to_bridge, i, AST_VECTOR_SIZE(&media_types) - 1); |
| 2528 |
|
| 2529 |
|
| 2530 |
|
| 2531 |
|
| 2532 |
AST_VECTOR_REPLACE(&participant->stream_map.to_channel, AST_VECTOR_SIZE(&media_types) - 1, i); |
| 2533 |
remb_enable_collection(bridge, participant, AST_VECTOR_SIZE(&media_types) - 1); |
| 2534 |
|
| 2535 |
|
| 2536 |
|
| 2537 |
|
| 2538 |
ast_channel_unlock(participant->chan); |
| 2539 |
ast_bridge_channel_unlock(participant); |
| 2540 |
|
| 2541 |
map_source_to_destinations(ast_channel_name(participant->chan), |
| 2542 |
AST_VECTOR_SIZE(&media_types) - 1, &bridge->channels, i); |
| 2543 |
ast_bridge_channel_lock(participant); |
| 2544 |
ast_channel_lock(participant->chan); |
| 2545 |
} else if (ast_stream_get_type(stream) == AST_MEDIA_TYPE_VIDEO) { |
| 2546 |
|
| 2547 |
|
| 2548 |
|
| 2549 |
|
| 2550 |
AST_VECTOR_REPLACE(&participant->stream_map.to_bridge, i, -1); |
| 2551 |
} else if (ast_stream_get_state(stream) != AST_STREAM_STATE_REMOVED) { |
| 2552 |
|
| 2553 |
|
| 2554 |
|
| 2555 |
enum ast_media_type type = ast_stream_get_type(stream); |
| 2556 |
int index = AST_VECTOR_GET_INDEX_NTH(&media_types, ++nths[type], |
| 2557 |
type, AST_VECTOR_ELEM_DEFAULT_CMP); |
| 2558 |
|
| 2559 |
if (index == -1) { |
| 2560 |
AST_VECTOR_APPEND(&media_types, type); |
| 2561 |
index = AST_VECTOR_SIZE(&media_types) - 1; |
| 2562 |
} |
| 2563 |
|
| 2564 |
AST_VECTOR_REPLACE(&participant->stream_map.to_bridge, i, index); |
| 2565 |
AST_VECTOR_REPLACE(&participant->stream_map.to_channel, index, i); |
| 2566 |
} |
| 2567 |
} |
| 2568 |
|
| 2569 |
ast_stream_topology_free(topology); |
| 2570 |
|
| 2571 |
ast_channel_unlock(participant->chan); |
| 2572 |
ast_bridge_channel_unlock(participant); |
| 2573 |
} |
| 2574 |
|
| 2575 |
AST_VECTOR_FREE(&media_types); |
| 2576 |
SCOPE_EXIT_RTN("%s\n", ast_channel_name(bridge_channel->chan)); |
| 2577 |
} |
| 2578 |
|
| 2579 |
static struct ast_bridge_technology softmix_bridge = { |
| 2580 |
.name = "softmix", |
| 2581 |
.capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX, |
| 2582 |
.preference = AST_BRIDGE_PREFERENCE_BASE_MULTIMIX, |
| 2583 |
.create = softmix_bridge_create, |
| 2584 |
.stop = softmix_bridge_stop, |
| 2585 |
.destroy = softmix_bridge_destroy, |
| 2586 |
.join = softmix_bridge_join, |
| 2587 |
.leave = softmix_bridge_leave, |
| 2588 |
.unsuspend = softmix_bridge_unsuspend, |
| 2589 |
.write = softmix_bridge_write, |
| 2590 |
.stream_topology_changed = softmix_bridge_stream_topology_changed, |
| 2591 |
}; |
| 2592 |
|
| 2593 |
#ifdef TEST_FRAMEWORK |
| 2594 |
struct stream_parameters { |
| 2595 |
const char *name; |
| 2596 |
const char *formats; |
| 2597 |
enum ast_media_type type; |
| 2598 |
}; |
| 2599 |
|
| 2600 |
static struct ast_stream_topology *build_topology(const struct stream_parameters *params, size_t num_streams) |
| 2601 |
{ |
| 2602 |
struct ast_stream_topology *topology; |
| 2603 |
size_t i; |
| 2604 |
|
| 2605 |
topology = ast_stream_topology_alloc(); |
| 2606 |
if (!topology) { |
| 2607 |
return NULL; |
| 2608 |
} |
| 2609 |
|
| 2610 |
for (i = 0; i < num_streams; ++i) { |
| 2611 |
RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup); |
| 2612 |
struct ast_stream *stream; |
| 2613 |
|
| 2614 |
caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT); |
| 2615 |
if (!caps) { |
| 2616 |
goto fail; |
| 2617 |
} |
| 2618 |
if (ast_format_cap_update_by_allow_disallow(caps, params[i].formats, 1) < 0) { |
| 2619 |
goto fail; |
| 2620 |
} |
| 2621 |
stream = ast_stream_alloc(params[i].name, params[i].type); |
| 2622 |
if (!stream) { |
| 2623 |
goto fail; |
| 2624 |
} |
| 2625 |
ast_stream_set_formats(stream, caps); |
| 2626 |
if (ast_stream_topology_append_stream(topology, stream) < 0) { |
| 2627 |
ast_stream_free(stream); |
| 2628 |
goto fail; |
| 2629 |
} |
| 2630 |
} |
| 2631 |
|
| 2632 |
return topology; |
| 2633 |
|
| 2634 |
fail: |
| 2635 |
ast_stream_topology_free(topology); |
| 2636 |
return NULL; |
| 2637 |
} |
| 2638 |
|
| 2639 |
static int validate_stream(struct ast_test *test, struct ast_stream *stream, |
| 2640 |
const struct stream_parameters *params) |
| 2641 |
{ |
| 2642 |
const struct ast_format_cap *stream_caps; |
| 2643 |
struct ast_format_cap *params_caps; |
| 2644 |
|
| 2645 |
if (ast_stream_get_type(stream) != params->type) { |
| 2646 |
ast_test_status_update(test, "Expected stream type '%s' but got type '%s'\n", |
| 2647 |
ast_codec_media_type2str(params->type), |
| 2648 |
ast_codec_media_type2str(ast_stream_get_type(stream))); |
| 2649 |
return -1; |
| 2650 |
} |
| 2651 |
if (strcmp(ast_stream_get_name(stream), params->name)) { |
| 2652 |
ast_test_status_update(test, "Expected stream name '%s' but got type '%s'\n", |
| 2653 |
params->name, ast_stream_get_name(stream)); |
| 2654 |
return -1; |
| 2655 |
} |
| 2656 |
|
| 2657 |
stream_caps = ast_stream_get_formats(stream); |
| 2658 |
params_caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT); |
| 2659 |
if (!params_caps) { |
| 2660 |
ast_test_status_update(test, "Allocation error on capabilities\n"); |
| 2661 |
return -1; |
| 2662 |
} |
| 2663 |
ast_format_cap_update_by_allow_disallow(params_caps, params->formats, 1); |
| 2664 |
|
| 2665 |
if (!ast_format_cap_identical(stream_caps, params_caps)) { |
| 2666 |
ast_test_status_update(test, "Formats are not as expected on stream '%s'\n", |
| 2667 |
ast_stream_get_name(stream)); |
| 2668 |
ao2_cleanup(params_caps); |
| 2669 |
return -1; |
| 2670 |
} |
| 2671 |
|
| 2672 |
ao2_cleanup(params_caps); |
| 2673 |
return 0; |
| 2674 |
} |
| 2675 |
|
| 2676 |
static int validate_original_streams(struct ast_test *test, struct ast_stream_topology *topology, |
| 2677 |
const struct stream_parameters *params, size_t num_streams) |
| 2678 |
{ |
| 2679 |
int i; |
| 2680 |
|
| 2681 |
if (ast_stream_topology_get_count(topology) < num_streams) { |
| 2682 |
ast_test_status_update(test, "Topology only has %d streams. Needs to have at least %zu\n", |
| 2683 |
ast_stream_topology_get_count(topology), num_streams); |
| 2684 |
return -1; |
| 2685 |
} |
| 2686 |
|
| 2687 |
for (i = 0; i < num_streams; ++i) { |
| 2688 |
if (validate_stream(test, ast_stream_topology_get_stream(topology, i), ¶ms[i])) { |
| 2689 |
return -1; |
| 2690 |
} |
| 2691 |
} |
| 2692 |
|
| 2693 |
return 0; |
| 2694 |
} |
| 2695 |
|
| 2696 |
AST_TEST_DEFINE(sfu_append_source_streams) |
| 2697 |
{ |
| 2698 |
enum ast_test_result_state res = AST_TEST_FAIL; |
| 2699 |
static const struct stream_parameters bob_streams[] = { |
| 2700 |
{ "bob_audio", "ulaw,alaw,g722,opus", AST_MEDIA_TYPE_AUDIO, }, |
| 2701 |
{ "bob_video", "h264,vp8", AST_MEDIA_TYPE_VIDEO, }, |
| 2702 |
}; |
| 2703 |
static const struct stream_parameters alice_streams[] = { |
| 2704 |
{ "alice_audio", "ulaw,opus", AST_MEDIA_TYPE_AUDIO, }, |
| 2705 |
{ "alice_video", "vp8", AST_MEDIA_TYPE_VIDEO, }, |
| 2706 |
}; |
| 2707 |
static const struct stream_parameters alice_dest_stream = { |
| 2708 |
"softbridge_dest_PJSIP/Bob-00000001_1", "h264,vp8", AST_MEDIA_TYPE_VIDEO, |
| 2709 |
}; |
| 2710 |
static const struct stream_parameters bob_dest_stream = { |
| 2711 |
"softbridge_dest_PJSIP/Alice-00000000_1", "vp8", AST_MEDIA_TYPE_VIDEO, |
| 2712 |
}; |
| 2713 |
struct ast_stream_topology *topology_alice = NULL; |
| 2714 |
struct ast_stream_topology *topology_bob = NULL; |
| 2715 |
|
| 2716 |
switch (cmd) { |
| 2717 |
case TEST_INIT: |
| 2718 |
info->name = "sfu_append_source_streams"; |
| 2719 |
info->category = "/bridges/bridge_softmix/"; |
| 2720 |
info->summary = "Test appending of video streams"; |
| 2721 |
info->description = |
| 2722 |
"This tests does stuff."; |
| 2723 |
return AST_TEST_NOT_RUN; |
| 2724 |
case TEST_EXECUTE: |
| 2725 |
break; |
| 2726 |
} |
| 2727 |
|
| 2728 |
topology_alice = build_topology(alice_streams, ARRAY_LEN(alice_streams)); |
| 2729 |
if (!topology_alice) { |
| 2730 |
goto end; |
| 2731 |
} |
| 2732 |
|
| 2733 |
topology_bob = build_topology(bob_streams, ARRAY_LEN(bob_streams)); |
| 2734 |
if (!topology_bob) { |
| 2735 |
goto end; |
| 2736 |
} |
| 2737 |
|
| 2738 |
if (append_source_streams(topology_alice, "PJSIP/Bob-00000001", NULL, topology_bob)) { |
| 2739 |
ast_test_status_update(test, "Failed to append Bob's streams to Alice\n"); |
| 2740 |
goto end; |
| 2741 |
} |
| 2742 |
|
| 2743 |
if (ast_stream_topology_get_count(topology_alice) != 3) { |
| 2744 |
ast_test_status_update(test, "Alice's topology isn't large enough! It's %d but needs to be %d\n", |
| 2745 |
ast_stream_topology_get_count(topology_alice), 3); |
| 2746 |
goto end; |
| 2747 |
} |
| 2748 |
|
| 2749 |
if (validate_original_streams(test, topology_alice, alice_streams, ARRAY_LEN(alice_streams))) { |
| 2750 |
goto end; |
| 2751 |
} |
| 2752 |
|
| 2753 |
if (validate_stream(test, ast_stream_topology_get_stream(topology_alice, 2), &alice_dest_stream)) { |
| 2754 |
goto end; |
| 2755 |
} |
| 2756 |
|
| 2757 |
if (append_source_streams(topology_bob, "PJSIP/Alice-00000000", NULL, topology_alice)) { |
| 2758 |
ast_test_status_update(test, "Failed to append Alice's streams to Bob\n"); |
| 2759 |
goto end; |
| 2760 |
} |
| 2761 |
|
| 2762 |
if (ast_stream_topology_get_count(topology_bob) != 3) { |
| 2763 |
ast_test_status_update(test, "Bob's topology isn't large enough! It's %d but needs to be %d\n", |
| 2764 |
ast_stream_topology_get_count(topology_bob), 3); |
| 2765 |
goto end; |
| 2766 |
} |
| 2767 |
|
| 2768 |
if (validate_original_streams(test, topology_bob, bob_streams, ARRAY_LEN(bob_streams))) { |
| 2769 |
goto end; |
| 2770 |
} |
| 2771 |
|
| 2772 |
if (validate_stream(test, ast_stream_topology_get_stream(topology_bob, 2), &bob_dest_stream)) { |
| 2773 |
goto end; |
| 2774 |
} |
| 2775 |
|
| 2776 |
res = AST_TEST_PASS; |
| 2777 |
|
| 2778 |
end: |
| 2779 |
ast_stream_topology_free(topology_alice); |
| 2780 |
ast_stream_topology_free(topology_bob); |
| 2781 |
return res; |
| 2782 |
} |
| 2783 |
|
| 2784 |
AST_TEST_DEFINE(sfu_remove_destination_streams) |
| 2785 |
{ |
| 2786 |
enum ast_test_result_state res = AST_TEST_FAIL; |
| 2787 |
static const struct stream_parameters params[] = { |
| 2788 |
{ "alice_audio", "ulaw,alaw,g722,opus", AST_MEDIA_TYPE_AUDIO, }, |
| 2789 |
{ "alice_video", "h264,vp8", AST_MEDIA_TYPE_VIDEO, }, |
| 2790 |
{ "softbridge_dest_PJSIP/Bob-00000001_video", "vp8", AST_MEDIA_TYPE_VIDEO, }, |
| 2791 |
{ "softbridge_dest_PJSIP/Carol-00000002_video", "h264", AST_MEDIA_TYPE_VIDEO, }, |
| 2792 |
}; |
| 2793 |
static const struct { |
| 2794 |
const char *channel_name; |
| 2795 |
int num_streams; |
| 2796 |
int params_index[4]; |
| 2797 |
} removal_results[] = { |
| 2798 |
{ "PJSIP/Bob-00000001", 4, { 0, 1, 2, 3 }, }, |
| 2799 |
{ "PJSIP/Edward-00000004", 4, { 0, 1, 2, 3 }, }, |
| 2800 |
{ "", 4, { 0, 1, 2, 3 }, }, |
| 2801 |
}; |
| 2802 |
struct ast_stream_topology *orig = NULL; |
| 2803 |
int i; |
| 2804 |
|
| 2805 |
switch (cmd) { |
| 2806 |
case TEST_INIT: |
| 2807 |
info->name = "sfu_remove_destination_streams"; |
| 2808 |
info->category = "/bridges/bridge_softmix/"; |
| 2809 |
info->summary = "Test removal of destination video streams"; |
| 2810 |
info->description = |
| 2811 |
"This tests does stuff."; |
| 2812 |
return AST_TEST_NOT_RUN; |
| 2813 |
case TEST_EXECUTE: |
| 2814 |
break; |
| 2815 |
} |
| 2816 |
|
| 2817 |
orig = build_topology(params, ARRAY_LEN(params)); |
| 2818 |
if (!orig) { |
| 2819 |
ast_test_status_update(test, "Unable to build initial stream topology\n"); |
| 2820 |
goto end; |
| 2821 |
} |
| 2822 |
|
| 2823 |
for (i = 0; i < ARRAY_LEN(removal_results); ++i) { |
| 2824 |
int j; |
| 2825 |
|
| 2826 |
remove_destination_streams(orig, removal_results[i].channel_name); |
| 2827 |
|
| 2828 |
if (ast_stream_topology_get_count(orig) != removal_results[i].num_streams) { |
| 2829 |
ast_test_status_update(test, "Resulting topology has %d streams, when %d are expected\n", |
| 2830 |
ast_stream_topology_get_count(orig), removal_results[i].num_streams); |
| 2831 |
goto end; |
| 2832 |
} |
| 2833 |
|
| 2834 |
for (j = 0; j < removal_results[i].num_streams; ++j) { |
| 2835 |
struct ast_stream *actual; |
| 2836 |
struct ast_stream *expected; |
| 2837 |
int orig_index; |
| 2838 |
|
| 2839 |
actual = ast_stream_topology_get_stream(orig, j); |
| 2840 |
|
| 2841 |
orig_index = removal_results[i].params_index[j]; |
| 2842 |
expected = ast_stream_topology_get_stream(orig, orig_index); |
| 2843 |
|
| 2844 |
if (!ast_format_cap_identical(ast_stream_get_formats(actual), |
| 2845 |
ast_stream_get_formats(expected))) { |
| 2846 |
struct ast_str *expected_str; |
| 2847 |
struct ast_str *actual_str; |
| 2848 |
|
| 2849 |
expected_str = ast_str_alloca(64); |
| 2850 |
actual_str = ast_str_alloca(64); |
| 2851 |
|
| 2852 |
ast_test_status_update(test, "Mismatch between expected (%s) and actual (%s) stream formats\n", |
| 2853 |
ast_format_cap_get_names(ast_stream_get_formats(expected), &expected_str), |
| 2854 |
ast_format_cap_get_names(ast_stream_get_formats(actual), &actual_str)); |
| 2855 |
goto end; |
| 2856 |
} |
| 2857 |
|
| 2858 |
if (is_video_dest(actual, removal_results[i].channel_name, -1) && |
| 2859 |
ast_stream_get_state(actual) != AST_STREAM_STATE_REMOVED) { |
| 2860 |
ast_test_status_update(test, "Removed stream %s does not have a state of removed\n", ast_stream_get_name(actual)); |
| 2861 |
goto end; |
| 2862 |
} |
| 2863 |
} |
| 2864 |
} |
| 2865 |
|
| 2866 |
res = AST_TEST_PASS; |
| 2867 |
|
| 2868 |
end: |
| 2869 |
ast_stream_topology_free(orig); |
| 2870 |
return res; |
| 2871 |
} |
| 2872 |
|
| 2873 |
#endif |
| 2874 |
|
| 2875 |
static int unload_module(void) |
| 2876 |
{ |
| 2877 |
ast_bridge_technology_unregister(&softmix_bridge); |
| 2878 |
AST_TEST_UNREGISTER(sfu_append_source_streams); |
| 2879 |
AST_TEST_UNREGISTER(sfu_remove_destination_streams); |
| 2880 |
return 0; |
| 2881 |
} |
| 2882 |
|
| 2883 |
static int load_module(void) |
| 2884 |
{ |
| 2885 |
if (ast_bridge_technology_register(&softmix_bridge)) { |
| 2886 |
unload_module(); |
| 2887 |
return AST_MODULE_LOAD_DECLINE; |
| 2888 |
} |
| 2889 |
AST_TEST_REGISTER(sfu_append_source_streams); |
| 2890 |
AST_TEST_REGISTER(sfu_remove_destination_streams); |
| 2891 |
return AST_MODULE_LOAD_SUCCESS; |
| 2892 |
} |
| 2893 |
|
| 2894 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Multi-party software based channel mixing"); |