Debian Codegraph @

main/stream.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2017, Digium, Inc.
5 *
6 * Joshua Colp <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19 /*! \file
20 *
21 * \brief Media Stream API
22 *
23 * \author Joshua Colp <[email protected]>
24 */
25
26 /*** MODULEINFO
27 <support_level>core</support_level>
28 ***/
29
30 #include "asterisk.h"
31
32 #include "asterisk/logger.h"
33 #include "asterisk/stream.h"
34 #include "asterisk/strings.h"
35 #include "asterisk/format.h"
36 #include "asterisk/format_cap.h"
37 #include "asterisk/vector.h"
38 #include "asterisk/config.h"
39 #include "asterisk/rtp_engine.h"
40
41 struct ast_stream_metadata_entry {
42 size_t length;
43 int value_start;
44 char name_value[0];
45 };
46
47 const char *ast_stream_codec_negotiation_params_map[] = {
48 [CODEC_NEGOTIATION_PARAM_UNSPECIFIED] = "unspecified",
49 [CODEC_NEGOTIATION_PARAM_PREFER] = "prefer",
50 [CODEC_NEGOTIATION_PARAM_OPERATION] = "operation",
51 [CODEC_NEGOTIATION_PARAM_KEEP] = "keep",
52 [CODEC_NEGOTIATION_PARAM_TRANSCODE] = "transcode",
53 };
54
55 const char *ast_stream_codec_negotiation_prefer_map[] = {
56 [CODEC_NEGOTIATION_PREFER_UNSPECIFIED] = "unspecified",
57 [CODEC_NEGOTIATION_PREFER_PENDING] = "pending",
58 [CODEC_NEGOTIATION_PREFER_CONFIGURED] = "configured",
59 };
60
61 const char *ast_stream_codec_negotiation_operation_map[] = {
62 [CODEC_NEGOTIATION_OPERATION_UNSPECIFIED] = "unspecified",
63 [CODEC_NEGOTIATION_OPERATION_INTERSECT] = "intersect",
64 [CODEC_NEGOTIATION_OPERATION_UNION] = "union",
65 [CODEC_NEGOTIATION_OPERATION_ONLY_PREFERRED] = "only_preferred",
66 [CODEC_NEGOTIATION_OPERATION_ONLY_NONPREFERRED] = "only_nonpreferred",
67 };
68
69 const char *ast_stream_codec_negotiation_keep_map[] = {
70 [CODEC_NEGOTIATION_KEEP_UNSPECIFIED] = "unspecified",
71 [CODEC_NEGOTIATION_KEEP_ALL] = "all",
72 [CODEC_NEGOTIATION_KEEP_FIRST] = "first",
73 };
74
75 const char *ast_stream_codec_negotiation_transcode_map[] = {
76 [CODEC_NEGOTIATION_TRANSCODE_UNSPECIFIED] = "unspecified",
77 [CODEC_NEGOTIATION_TRANSCODE_ALLOW] = "allow",
78 [CODEC_NEGOTIATION_TRANSCODE_PREVENT] = "prevent",
79 };
80
81 struct ast_stream {
82 /*!
83 * \brief The type of media the stream is handling
84 */
85 enum ast_media_type type;
86
87 /*!
88 * \brief The position of the stream in the topology
89 */
90 unsigned int position;
91
92 /*!
93 * \brief Current formats negotiated on the stream
94 */
95 struct ast_format_cap *formats;
96
97 /*!
98 * \brief The current state of the stream
99 */
100 enum ast_stream_state state;
101
102 /*!
103 * \brief Stream metadata vector
104 */
105 struct ast_variable *metadata;
106
107 /*!
108 * \brief The group that the stream is part of
109 */
110 int group;
111
112 /*!
113 * \brief The rtp_codecs used by the stream
114 */
115 struct ast_rtp_codecs *rtp_codecs;
116
117 /*!
118 * \brief Name for the stream within the context of the channel it is on
119 */
120 char name[0];
121 };
122
123 struct ast_stream_topology {
124 /*!
125 * \brief A vector of all the streams in this topology
126 */
127 AST_VECTOR(, struct ast_stream *) streams;
128 /*! Indicates that this topology should not have further operations applied to it. */
129 int final;
130 };
131
132 const char *ast_stream_codec_prefs_to_str(const struct ast_stream_codec_negotiation_prefs *prefs, struct ast_str **buf)
133 {
134 if (!prefs || !buf || !*buf) {
135 return "";
136 }
137
138 ast_str_append(buf, 0, "%s:%s, %s:%s, %s:%s, %s:%s",
139 ast_stream_codec_param_to_str(CODEC_NEGOTIATION_PARAM_PREFER),
140 ast_stream_codec_prefer_to_str(prefs->prefer),
141 ast_stream_codec_param_to_str(CODEC_NEGOTIATION_PARAM_OPERATION),
142 ast_stream_codec_operation_to_str(prefs->operation),
143 ast_stream_codec_param_to_str(CODEC_NEGOTIATION_PARAM_KEEP),
144 ast_stream_codec_keep_to_str(prefs->keep),
145 ast_stream_codec_param_to_str(CODEC_NEGOTIATION_PARAM_TRANSCODE),
146 ast_stream_codec_transcode_to_str(prefs->transcode)
147 );
148
149 return ast_str_buffer(*buf);
150 }
151
152 /*!
153 * \internal
154 * \brief Sets a codec prefs member based on a the preference name and string value
155 *
156 * \warning This macro will cause the calling function to return if a preference name
157 * is matched but the value isn't valid for this preference.
158 */
159 #define set_pref_value(_name, _value, _prefs, _UC, _lc, _error_message) \
160 ({ \
161 int _res = 0; \
162 if (strcmp(_name, ast_stream_codec_negotiation_params_map[CODEC_NEGOTIATION_PARAM_ ## _UC]) == 0) { \
163 int i; \
164 for (i = CODEC_NEGOTIATION_ ## _UC ## _UNSPECIFIED + 1; i < CODEC_NEGOTIATION_ ## _UC ## _END; i++) { \
165 if (strcmp(value, ast_stream_codec_negotiation_ ## _lc ## _map[i]) == 0) { \
166 prefs->_lc = i; \
167 } \
168 } \
169 if ( prefs->_lc == CODEC_NEGOTIATION_ ## _UC ## _UNSPECIFIED) { \
170 _res = -1; \
171 if (_error_message) { \
172 ast_str_append(_error_message, 0, "Codec preference '%s' has invalid value '%s'", name, value); \
173 } \
174 } \
175 } \
176 if (_res < 0) { \
177 return _res; \
178 } \
179 })
180
181 int ast_stream_codec_prefs_parse(const char *pref_string, struct ast_stream_codec_negotiation_prefs *prefs,
182 struct ast_str **error_message)
183 {
184 char *initial_value = ast_strdupa(pref_string);
185 char *current_value;
186 char *pref;
187 char *saveptr1;
188 char *saveptr2;
189 char *name;
190 char *value;
191
192 if (!prefs) {
193 return -1;
194 }
195
196 prefs->prefer = CODEC_NEGOTIATION_PREFER_UNSPECIFIED;
197 prefs->operation = CODEC_NEGOTIATION_OPERATION_UNSPECIFIED;
198 prefs->keep = CODEC_NEGOTIATION_KEEP_UNSPECIFIED;
199 prefs->transcode = CODEC_NEGOTIATION_TRANSCODE_UNSPECIFIED;
200
201 for (current_value = initial_value; (pref = strtok_r(current_value, ",", &saveptr1)) != NULL; ) {
202 name = strtok_r(pref, ": ", &saveptr2);
203 value = strtok_r(NULL, ": ", &saveptr2);
204
205 if (!name || !value) {
206 if (error_message) {
207 ast_str_append(error_message, 0, "Codec preference '%s' is invalid", pref);
208 }
209 return -1;
210 }
211
212 set_pref_value(name, value, prefs, OPERATION, operation, error_message);
213 set_pref_value(name, value, prefs, PREFER, prefer, error_message);
214 set_pref_value(name, value, prefs, KEEP, keep, error_message);
215 set_pref_value(name, value, prefs, TRANSCODE, transcode, error_message);
216
217 current_value = NULL;
218 }
219
220 return 0;
221 }
222
223 const char *ast_stream_state_map[] = {
224 [AST_STREAM_STATE_REMOVED] = "removed",
225 [AST_STREAM_STATE_SENDRECV] = "sendrecv",
226 [AST_STREAM_STATE_SENDONLY] = "sendonly",
227 [AST_STREAM_STATE_RECVONLY] = "recvonly",
228 [AST_STREAM_STATE_INACTIVE] = "inactive",
229 };
230
231 #define MIN_STREAM_NAME_LEN 16
232
233 struct ast_stream *ast_stream_alloc(const char *name, enum ast_media_type type)
234 {
235 struct ast_stream *stream;
236 size_t name_len = MAX(strlen(S_OR(name, "")), MIN_STREAM_NAME_LEN); /* Ensure there is enough room for 'removed' or a type-position */
237
238 stream = ast_calloc(1, sizeof(*stream) + name_len + 1);
239 if (!stream) {
240 return NULL;
241 }
242
243 stream->type = type;
244 stream->state = AST_STREAM_STATE_INACTIVE;
245 stream->group = -1;
246 strcpy(stream->name, S_OR(name, "")); /* Safe */
247
248 stream->formats = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
249 if (!stream->formats) {
250 ast_free(stream);
251 return NULL;
252 }
253
254 return stream;
255 }
256
257 struct ast_stream *ast_stream_clone(const struct ast_stream *stream, const char *name)
258 {
259 struct ast_stream *new_stream;
260 const char *stream_name;
261 size_t name_len;
262
263 if (!stream) {
264 return NULL;
265 }
266
267 stream_name = name ?: stream->name;
268 name_len = MAX(strlen(S_OR(stream_name, "")), MIN_STREAM_NAME_LEN); /* Ensure there is enough room for 'removed' or a type-position */
269 new_stream = ast_calloc(1, sizeof(*stream) + name_len + 1);
270 if (!new_stream) {
271 return NULL;
272 }
273
274 memcpy(new_stream, stream, sizeof(*new_stream));
275 strcpy(new_stream->name, stream_name); /* Safe */
276 new_stream->group = -1;
277
278 new_stream->formats = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
279 if (!new_stream->formats) {
280 ast_free(new_stream);
281 return NULL;
282 }
283 ast_format_cap_append_from_cap(new_stream->formats, stream->formats, AST_MEDIA_TYPE_UNKNOWN);
284
285 new_stream->metadata = ast_stream_get_metadata_list(stream);
286
287 /* rtp_codecs aren't cloned */
288
289 return new_stream;
290 }
291
292 void ast_stream_free(struct ast_stream *stream)
293 {
294 if (!stream) {
295 return;
296 }
297
298 ast_variables_destroy(stream->metadata);
299
300 if (stream->rtp_codecs) {
301 ast_rtp_codecs_payloads_destroy(stream->rtp_codecs);
302 }
303
304 ao2_cleanup(stream->formats);
305
306 ast_free(stream);
307 }
308
309 const char *ast_stream_get_name(const struct ast_stream *stream)
310 {
311 ast_assert(stream != NULL);
312
313 return stream->name;
314 }
315
316 enum ast_media_type ast_stream_get_type(const struct ast_stream *stream)
317 {
318 ast_assert(stream != NULL);
319
320 return stream->type;
321 }
322
323 void ast_stream_set_type(struct ast_stream *stream, enum ast_media_type type)
324 {
325 ast_assert(stream != NULL);
326
327 stream->type = type;
328 }
329
330 const struct ast_format_cap *ast_stream_get_formats(const struct ast_stream *stream)
331 {
332 ast_assert(stream != NULL);
333
334 return stream->formats;
335 }
336
337 const char *ast_stream_to_str(const struct ast_stream *stream, struct ast_str **buf)
338 {
339 if (!buf || !*buf) {
340 return "";
341 }
342
343 if (!stream) {
344 ast_str_append(buf, 0, "(null stream)");
345 return ast_str_buffer(*buf);
346 }
347
348 ast_str_append(buf, 0, "%d:%s:%s:%s ",
349 stream->position,
350 S_OR(stream->name, "noname"),
351 ast_codec_media_type2str(stream->type),
352 ast_stream_state_map[stream->state]);
353 ast_format_cap_append_names(stream->formats, buf);
354
355 return ast_str_buffer(*buf);
356 }
357
358 int ast_stream_get_format_count(const struct ast_stream *stream)
359 {
360 ast_assert(stream != NULL);
361
362 return stream->formats ? ast_format_cap_count(stream->formats) : 0;
363 }
364
365 void ast_stream_set_formats(struct ast_stream *stream, struct ast_format_cap *caps)
366 {
367 ast_assert(stream != NULL);
368
369 ao2_cleanup(stream->formats);
370 stream->formats = ao2_bump(caps);
371 }
372
373 enum ast_stream_state ast_stream_get_state(const struct ast_stream *stream)
374 {
375 ast_assert(stream != NULL);
376
377 return stream->state;
378 }
379
380 void ast_stream_set_state(struct ast_stream *stream, enum ast_stream_state state)
381 {
382 ast_assert(stream != NULL);
383
384 stream->state = state;
385
386 }
387
388 const char *ast_stream_state2str(enum ast_stream_state state)
389 {
390 switch (state) {
391 case AST_STREAM_STATE_REMOVED:
392 return "removed";
393 case AST_STREAM_STATE_SENDRECV:
394 return "sendrecv";
395 case AST_STREAM_STATE_SENDONLY:
396 return "sendonly";
397 case AST_STREAM_STATE_RECVONLY:
398 return "recvonly";
399 case AST_STREAM_STATE_INACTIVE:
400 return "inactive";
401 default:
402 return "<unknown>";
403 }
404 }
405
406 enum ast_stream_state ast_stream_str2state(const char *str)
407 {
408 if (!strcmp("sendrecv", str)) {
409 return AST_STREAM_STATE_SENDRECV;
410 }
411 if (!strcmp("sendonly", str)) {
412 return AST_STREAM_STATE_SENDONLY;
413 }
414 if (!strcmp("recvonly", str)) {
415 return AST_STREAM_STATE_RECVONLY;
416 }
417 if (!strcmp("inactive", str)) {
418 return AST_STREAM_STATE_INACTIVE;
419 }
420 return AST_STREAM_STATE_REMOVED;
421 }
422
423 const char *ast_stream_get_metadata(const struct ast_stream *stream, const char *m_key)
424 {
425 struct ast_variable *v;
426
427 ast_assert_return(stream != NULL, NULL);
428 ast_assert_return(m_key != NULL, NULL);
429
430 for (v = stream->metadata; v; v = v->next) {
431 if (strcmp(v->name, m_key) == 0) {
432 return v->value;
433 }
434 }
435
436 return NULL;
437 }
438
439 struct ast_variable *ast_stream_get_metadata_list(const struct ast_stream *stream)
440 {
441 struct ast_variable *v;
442 struct ast_variable *vout = NULL;
443
444 ast_assert_return(stream != NULL, NULL);
445
446 for (v = stream->metadata; v; v = v->next) {
447 struct ast_variable *vt = ast_variable_new(v->name, v->value, "");
448
449 if (!vt) {
450 ast_variables_destroy(vout);
451 return NULL;
452 }
453
454 ast_variable_list_append(&vout, vt);
455 }
456
457 return vout;
458 }
459
460 int ast_stream_set_metadata(struct ast_stream *stream, const char *m_key, const char *value)
461 {
462 struct ast_variable *v;
463 struct ast_variable *prev;
464
465 ast_assert_return(stream != NULL, -1);
466 ast_assert_return(m_key != NULL, -1);
467
468 prev = NULL;
469 v = stream->metadata;
470 while(v) {
471 struct ast_variable *next = v->next;
472 if (strcmp(v->name, m_key) == 0) {
473 if (prev) {
474 prev->next = next;
475 } else {
476 stream->metadata = next;
477 }
478 ast_free(v);
479 break;
480 } else {
481 prev = v;
482 }
483 v = next;
484 }
485
486 if (!value) {
487 return 0;
488 }
489
490 v = ast_variable_new(m_key, value, "");
491 if (!v) {
492 return -1;
493 }
494
495 ast_variable_list_append(&stream->metadata, v);
496
497 return 0;
498 }
499
500 int ast_stream_get_position(const struct ast_stream *stream)
501 {
502 ast_assert(stream != NULL);
503
504 return stream->position;
505 }
506
507 struct ast_rtp_codecs *ast_stream_get_rtp_codecs(const struct ast_stream *stream)
508 {
509 ast_assert(stream != NULL);
510
511 return stream->rtp_codecs;
512 }
513
514 void ast_stream_set_rtp_codecs(struct ast_stream *stream, struct ast_rtp_codecs *rtp_codecs)
515 {
516 ast_assert(stream != NULL);
517
518 if (stream->rtp_codecs) {
519 ast_rtp_codecs_payloads_destroy(rtp_codecs);
520 }
521
522 stream->rtp_codecs = rtp_codecs;
523 }
524
525 struct ast_stream *ast_stream_create_resolved(struct ast_stream *pending_stream,
526 struct ast_stream *validation_stream, struct ast_stream_codec_negotiation_prefs *prefs,
527 struct ast_str **error_message)
528 {
529 struct ast_format_cap *preferred_caps = NULL;
530 struct ast_format_cap *nonpreferred_caps = NULL;
531 struct ast_format_cap *joint_caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
532 struct ast_stream *joint_stream;
533 enum ast_media_type media_type = pending_stream ? pending_stream->type : AST_MEDIA_TYPE_UNKNOWN;
534 int res = 0;
535 SCOPE_ENTER(4, "Pending: %s Validation: %s Prefs: %s\n",
536 ast_str_tmp(128, ast_stream_to_str(pending_stream, &STR_TMP)),
537 ast_str_tmp(128, ast_stream_to_str(validation_stream, &STR_TMP)),
538 ast_str_tmp(128, ast_stream_codec_prefs_to_str(prefs, &STR_TMP)));
539
540 if (!pending_stream || !validation_stream || !prefs || !joint_caps
541 || media_type == AST_MEDIA_TYPE_UNKNOWN) {
542 if (error_message) {
543 ast_str_append(error_message, 0, "Invalid arguments");
544 }
545 ao2_cleanup(joint_caps);
546 SCOPE_EXIT_RTN_VALUE(NULL, "Invalid arguments\n");
547 }
548
549 if (prefs->prefer == CODEC_NEGOTIATION_PREFER_PENDING) {
550 preferred_caps = pending_stream->formats;
551 nonpreferred_caps = validation_stream->formats;
552 } else {
553 preferred_caps = validation_stream->formats;
554 nonpreferred_caps = pending_stream->formats;
555 }
556 ast_format_cap_set_framing(joint_caps, ast_format_cap_get_framing(pending_stream->formats));
557
558 switch(prefs->operation) {
559 case CODEC_NEGOTIATION_OPERATION_ONLY_PREFERRED:
560 res = ast_format_cap_append_from_cap(joint_caps, preferred_caps, media_type);
561 break;
562 case CODEC_NEGOTIATION_OPERATION_ONLY_NONPREFERRED:
563 res = ast_format_cap_append_from_cap(joint_caps, nonpreferred_caps, media_type);
564 break;
565 case CODEC_NEGOTIATION_OPERATION_INTERSECT:
566 res = ast_format_cap_get_compatible(preferred_caps, nonpreferred_caps, joint_caps);
567 break;
568 case CODEC_NEGOTIATION_OPERATION_UNION:
569 res = ast_format_cap_append_from_cap(joint_caps, preferred_caps, media_type);
570 if (res == 0) {
571 res = ast_format_cap_append_from_cap(joint_caps, nonpreferred_caps, media_type);
572 }
573 break;
574 default:
575 break;
576 }
577
578 if (res) {
579 if (error_message) {
580 ast_str_append(error_message, 0, "No common formats available for media type '%s' ",
581 ast_codec_media_type2str(pending_stream->type));
582 ast_format_cap_append_names(preferred_caps, error_message);
583 ast_str_append(error_message, 0, "<>");
584 ast_format_cap_append_names(nonpreferred_caps, error_message);
585 ast_str_append(error_message, 0, " with prefs: ");
586 ast_stream_codec_prefs_to_str(prefs, error_message);
587 }
588
589 ao2_cleanup(joint_caps);
590 SCOPE_EXIT_RTN_VALUE(NULL, "No common formats available\n");
591 }
592
593 if (!ast_format_cap_empty(joint_caps)) {
594 if (prefs->keep == CODEC_NEGOTIATION_KEEP_FIRST) {
595 struct ast_format *single = ast_format_cap_get_format(joint_caps, 0);
596 ast_format_cap_remove_by_type(joint_caps, AST_MEDIA_TYPE_UNKNOWN);
597 ast_format_cap_append(joint_caps, single, 0);
598 ao2_ref(single, -1);
599 }
600 } else {
601 if (error_message) {
602 ast_str_append(error_message, 0, "No common formats available for media type '%s' ",
603 ast_codec_media_type2str(pending_stream->type));
604 ast_format_cap_append_names(preferred_caps, error_message);
605 ast_str_append(error_message, 0, "<>");
606 ast_format_cap_append_names(nonpreferred_caps, error_message);
607 ast_str_append(error_message, 0, " with prefs: ");
608 ast_stream_codec_prefs_to_str(prefs, error_message);
609 }
610 }
611
612 joint_stream = ast_stream_clone(pending_stream, NULL);
613 if (!joint_stream) {
614 ao2_cleanup(joint_caps);
615 return NULL;
616 }
617
618 /* ref to joint_caps will be transferred to the stream */
619 ast_stream_set_formats(joint_stream, joint_caps);
620
621 if (TRACE_ATLEAST(3)) {
622 struct ast_str *buf = ast_str_create((AST_FORMAT_CAP_NAMES_LEN * 3) + AST_STREAM_MAX_CODEC_PREFS_LENGTH);
623 if (buf) {
624 ast_str_set(&buf, 0, "Resolved '%s' stream ", ast_codec_media_type2str(pending_stream->type));
625 ast_format_cap_append_names(preferred_caps, &buf);
626 ast_str_append(&buf, 0, "<>");
627 ast_format_cap_append_names(nonpreferred_caps, &buf);
628 ast_str_append(&buf, 0, " to ");
629 ast_format_cap_append_names(joint_caps, &buf);
630 ast_str_append(&buf, 0, " with prefs: ");
631 ast_stream_codec_prefs_to_str(prefs, &buf);
632 ast_trace(1, "%s\n", ast_str_buffer(buf));
633 ast_free(buf);
634 }
635 }
636
637 ao2_cleanup(joint_caps);
638 SCOPE_EXIT_RTN_VALUE(joint_stream, "Joint stream: %s\n", ast_str_tmp(128, ast_stream_to_str(joint_stream, &STR_TMP)));
639 }
640
641 static void stream_topology_destroy(void *data)
642 {
643 struct ast_stream_topology *topology = data;
644 SCOPE_ENTER(4, "Topology: %p: %s\n", topology, ast_str_tmp(128, ast_stream_topology_to_str(topology, &STR_TMP)));
645
646 AST_VECTOR_CALLBACK_VOID(&topology->streams, ast_stream_free);
647 AST_VECTOR_FREE(&topology->streams);
648 SCOPE_EXIT_RTN("Destroyed: %p\n", topology);
649 }
650
651 #define TOPOLOGY_INITIAL_STREAM_COUNT 2
652 struct ast_stream_topology *ast_stream_topology_alloc(void)
653 {
654 struct ast_stream_topology *topology;
655 SCOPE_ENTER(4, "Topology Create\n");
656
657 topology = ao2_alloc_options(sizeof(*topology), stream_topology_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
658 if (!topology) {
659 SCOPE_EXIT_RTN_VALUE(NULL, "Allocation failed\n");
660 }
661
662 if (AST_VECTOR_INIT(&topology->streams, TOPOLOGY_INITIAL_STREAM_COUNT)) {
663 ao2_ref(topology, -1);
664 SCOPE_EXIT_RTN_VALUE(NULL, "Vector init failed\n");
665 }
666
667 SCOPE_EXIT_RTN_VALUE(topology, "Created: %p\n", topology);
668 }
669
670 struct ast_stream_topology *ast_stream_topology_clone(
671 const struct ast_stream_topology *topology)
672 {
673 struct ast_stream_topology *new_topology;
674 int i;
675
676 ast_assert(topology != NULL);
677
678 new_topology = ast_stream_topology_alloc();
679 if (!new_topology) {
680 return NULL;
681 }
682
683 for (i = 0; i < AST_VECTOR_SIZE(&topology->streams); i++) {
684 struct ast_stream *existing = AST_VECTOR_GET(&topology->streams, i);
685 struct ast_stream *stream = ast_stream_clone(existing, NULL);
686
687 if (!stream || AST_VECTOR_APPEND(&new_topology->streams, stream)) {
688 ast_stream_free(stream);
689 ast_stream_topology_free(new_topology);
690 return NULL;
691 }
692
693 ast_stream_set_group(stream, ast_stream_get_group(existing));
694 }
695
696 return new_topology;
697 }
698
699 int ast_stream_topology_equal(const struct ast_stream_topology *left,
700 const struct ast_stream_topology *right)
701 {
702 int index;
703
704 ast_assert(left != NULL);
705 ast_assert(right != NULL);
706
707 if (ast_stream_topology_get_count(left) != ast_stream_topology_get_count(right)) {
708 return 0;
709 }
710
711 for (index = 0; index < ast_stream_topology_get_count(left); ++index) {
712 const struct ast_stream *left_stream = ast_stream_topology_get_stream(left, index);
713 const struct ast_stream *right_stream = ast_stream_topology_get_stream(right, index);
714
715 if (ast_stream_get_type(left_stream) != ast_stream_get_type(right_stream)) {
716 return 0;
717 }
718
719 if (ast_stream_get_state(left_stream) != ast_stream_get_state(right_stream)) {
720 return 0;
721 }
722
723 if (!ast_stream_get_formats(left_stream) && ast_stream_get_formats(right_stream) &&
724 ast_format_cap_count(ast_stream_get_formats(right_stream))) {
725 /* A NULL format capabilities and an empty format capabilities are the same, as they have
726 * no formats inside. If one does though... they are not equal.
727 */
728 return 0;
729 } else if (!ast_stream_get_formats(right_stream) && ast_stream_get_formats(left_stream) &&
730 ast_format_cap_count(ast_stream_get_formats(left_stream))) {
731 return 0;
732 } else if (ast_stream_get_formats(left_stream) && ast_stream_get_formats(right_stream) &&
733 !ast_format_cap_identical(ast_stream_get_formats(left_stream), ast_stream_get_formats(right_stream))) {
734 /* But if both are actually present we need to do an actual identical check. */
735 return 0;
736 }
737
738 if (strcmp(ast_stream_get_name(left_stream), ast_stream_get_name(right_stream))) {
739 return 0;
740 }
741 }
742
743 return 1;
744 }
745
746 void ast_stream_topology_free(struct ast_stream_topology *topology)
747 {
748 ao2_cleanup(topology);
749 }
750
751 int ast_stream_topology_append_stream(struct ast_stream_topology *topology, struct ast_stream *stream)
752 {
753 ast_assert(topology && stream);
754
755 if (AST_VECTOR_APPEND(&topology->streams, stream)) {
756 return -1;
757 }
758
759 stream->position = AST_VECTOR_SIZE(&topology->streams) - 1;
760
761 if (ast_strlen_zero(stream->name)) {
762 snprintf(stream->name, MIN_STREAM_NAME_LEN, "%s-%d", ast_codec_media_type2str(stream->type), stream->position);
763 }
764
765 return AST_VECTOR_SIZE(&topology->streams) - 1;
766 }
767
768 int ast_stream_topology_get_count(const struct ast_stream_topology *topology)
769 {
770 ast_assert(topology != NULL);
771
772 return AST_VECTOR_SIZE(&topology->streams);
773 }
774
775 int ast_stream_topology_get_active_count(const struct ast_stream_topology *topology)
776 {
777 int i;
778 int count = 0;
779 ast_assert(topology != NULL);
780
781 for (i = 0; i < AST_VECTOR_SIZE(&topology->streams); i++) {
782 struct ast_stream *stream = AST_VECTOR_GET(&topology->streams, i);
783 if (stream->state != AST_STREAM_STATE_REMOVED) {
784 count++;
785 }
786 }
787
788 return count;
789 }
790
791 struct ast_stream *ast_stream_topology_get_stream(
792 const struct ast_stream_topology *topology, unsigned int stream_num)
793 {
794 ast_assert(topology != NULL);
795
796 return AST_VECTOR_GET(&topology->streams, stream_num);
797 }
798
799 int ast_stream_topology_set_stream(struct ast_stream_topology *topology,
800 unsigned int position, struct ast_stream *stream)
801 {
802 struct ast_stream *existing_stream;
803
804 ast_assert(topology && stream);
805
806 if (position > AST_VECTOR_SIZE(&topology->streams)) {
807 return -1;
808 }
809
810 if (position < AST_VECTOR_SIZE(&topology->streams)) {
811 existing_stream = AST_VECTOR_GET(&topology->streams, position);
812 ast_stream_free(existing_stream);
813 }
814
815 stream->position = position;
816
817 if (position == AST_VECTOR_SIZE(&topology->streams)) {
818 return AST_VECTOR_APPEND(&topology->streams, stream);
819 }
820
821 if (ast_strlen_zero(stream->name)) {
822 snprintf(stream->name, MIN_STREAM_NAME_LEN, "%s-%d", ast_codec_media_type2str(stream->type), stream->position);
823 }
824
825 return AST_VECTOR_REPLACE(&topology->streams, position, stream);
826 }
827
828 int ast_stream_topology_del_stream(struct ast_stream_topology *topology,
829 unsigned int position)
830 {
831 struct ast_stream *stream;
832
833 ast_assert(topology != NULL);
834
835 if (AST_VECTOR_SIZE(&topology->streams) <= position) {
836 return -1;
837 }
838
839 stream = AST_VECTOR_REMOVE_ORDERED(&topology->streams, position);
840 ast_stream_free(stream);
841
842 /* Fix up higher stream position indices */
843 for (; position < AST_VECTOR_SIZE(&topology->streams); ++position) {
844 stream = AST_VECTOR_GET(&topology->streams, position);
845 stream->position = position;
846 }
847
848 return 0;
849 }
850
851 struct ast_stream_topology *ast_stream_topology_create_from_format_cap(
852 struct ast_format_cap *cap)
853 {
854 struct ast_stream_topology *topology;
855 enum ast_media_type type;
856
857 topology = ast_stream_topology_alloc();
858 if (!topology || !cap || !ast_format_cap_count(cap)) {
859 return topology;
860 }
861
862 for (type = AST_MEDIA_TYPE_UNKNOWN + 1; type < AST_MEDIA_TYPE_END; type++) {
863 struct ast_format_cap *new_cap;
864 struct ast_stream *stream;
865
866 if (!ast_format_cap_has_type(cap, type)) {
867 continue;
868 }
869
870 new_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
871 if (!new_cap) {
872 ast_stream_topology_free(topology);
873 return NULL;
874 }
875
876 ast_format_cap_set_framing(new_cap, ast_format_cap_get_framing(cap));
877 if (ast_format_cap_append_from_cap(new_cap, cap, type)) {
878 ao2_cleanup(new_cap);
879 ast_stream_topology_free(topology);
880 return NULL;
881 }
882
883 stream = ast_stream_alloc(NULL, type);
884 if (!stream) {
885 ao2_cleanup(new_cap);
886 ast_stream_topology_free(topology);
887 return NULL;
888 }
889
890 ast_stream_set_formats(stream, new_cap);
891 ao2_ref(new_cap, -1);
892 stream->state = AST_STREAM_STATE_SENDRECV;
893 if (ast_stream_topology_append_stream(topology, stream) == -1) {
894 ast_stream_free(stream);
895 ast_stream_topology_free(topology);
896 return NULL;
897 }
898
899 snprintf(stream->name, MIN_STREAM_NAME_LEN, "%s-%d", ast_codec_media_type2str(stream->type), stream->position);
900 }
901
902 return topology;
903 }
904
905 struct ast_format_cap *ast_stream_topology_get_formats_by_type(
906 struct ast_stream_topology *topology, enum ast_media_type type)
907 {
908 struct ast_format_cap *caps;
909 int i;
910
911 ast_assert(topology != NULL);
912
913 caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
914 if (!caps) {
915 return NULL;
916 }
917
918 for (i = 0; i < AST_VECTOR_SIZE(&topology->streams); i++) {
919 struct ast_stream *stream;
920
921 stream = AST_VECTOR_GET(&topology->streams, i);
922 if (!stream->formats || stream->state == AST_STREAM_STATE_REMOVED) {
923 continue;
924 }
925 if (type == AST_MEDIA_TYPE_UNKNOWN || type == stream->type) {
926 ast_format_cap_append_from_cap(caps, stream->formats, AST_MEDIA_TYPE_UNKNOWN);
927 }
928 }
929
930 return caps;
931 }
932
933 struct ast_format_cap *ast_stream_topology_get_formats(
934 struct ast_stream_topology *topology)
935 {
936 return ast_stream_topology_get_formats_by_type(topology, AST_MEDIA_TYPE_UNKNOWN);
937 }
938
939 const char *ast_stream_topology_to_str(const struct ast_stream_topology *topology,
940 struct ast_str **buf)
941 {
942 int i;
943
944 if (!buf ||!*buf) {
945 return "";
946 }
947
948 if (!topology) {
949 ast_str_append(buf, 0, "(null topology)");
950 return ast_str_buffer(*buf);
951 }
952
953 ast_str_append(buf, 0, "%s", S_COR(topology->final, "final", ""));
954
955 for (i = 0; i < AST_VECTOR_SIZE(&topology->streams); i++) {
956 struct ast_stream *stream;
957
958 stream = AST_VECTOR_GET(&topology->streams, i);
959 ast_str_append(buf, 0, " <");
960 ast_stream_to_str(stream, buf);
961 ast_str_append(buf, 0, ">");
962 }
963
964 return ast_str_buffer(*buf);
965 }
966
967 struct ast_stream *ast_stream_topology_get_first_stream_by_type(
968 const struct ast_stream_topology *topology,
969 enum ast_media_type type)
970 {
971 int i;
972
973 ast_assert(topology != NULL);
974
975 for (i = 0; i < AST_VECTOR_SIZE(&topology->streams); i++) {
976 struct ast_stream *stream;
977
978 stream = AST_VECTOR_GET(&topology->streams, i);
979 if (stream->type == type
980 && stream->state != AST_STREAM_STATE_REMOVED) {
981 return stream;
982 }
983 }
984
985 return NULL;
986 }
987
988 void ast_stream_topology_map(const struct ast_stream_topology *topology,
989 struct ast_vector_int *types, struct ast_vector_int *v0, struct ast_vector_int *v1)
990 {
991 int i;
992 int nths[AST_MEDIA_TYPE_END] = {0};
993 int size = ast_stream_topology_get_count(topology);
994
995 /*
996 * Clear out any old mappings and initialize the new ones
997 */
998 AST_VECTOR_FREE(v0);
999 AST_VECTOR_FREE(v1);
1000
1001 /*
1002 * Both vectors are sized to the topology. The media types vector is always
1003 * guaranteed to be the size of the given topology or greater.
1004 */
1005 AST_VECTOR_INIT(v0, size);
1006 AST_VECTOR_INIT(v1, size);
1007
1008 for (i = 0; i < size; ++i) {
1009 struct ast_stream *stream = ast_stream_topology_get_stream(topology, i);
1010 enum ast_media_type type = ast_stream_get_type(stream);
1011 int index = AST_VECTOR_GET_INDEX_NTH(types, ++nths[type],
1012 type, AST_VECTOR_ELEM_DEFAULT_CMP);
1013
1014 if (index == -1) {
1015 /*
1016 * If a given type is not found for an index level then update the
1017 * media types vector with that type. This keeps the media types
1018 * vector always at the max topology size.
1019 */
1020 AST_VECTOR_APPEND(types, type);
1021 index = AST_VECTOR_SIZE(types) - 1;
1022 }
1023
1024 /*
1025 * The mapping is reflexive in the sense that if it maps in one direction
1026 * then the reverse direction maps back to the other's index.
1027 */
1028 AST_VECTOR_REPLACE(v0, i, index);
1029 AST_VECTOR_REPLACE(v1, index, i);
1030 }
1031 }
1032
1033 struct ast_stream_topology *ast_stream_topology_create_resolved(
1034 struct ast_stream_topology *pending_topology, struct ast_stream_topology *configured_topology,
1035 struct ast_stream_codec_negotiation_prefs *prefs, struct ast_str**error_message)
1036 {
1037 struct ast_stream_topology *joint_topology = ast_stream_topology_alloc();
1038 int res = 0;
1039 int i;
1040
1041 if (!pending_topology || !configured_topology || !joint_topology) {
1042 ao2_cleanup(joint_topology);
1043 return NULL;
1044 }
1045
1046 for (i = 0; i < AST_VECTOR_SIZE(&pending_topology->streams); i++) {
1047 struct ast_stream *pending_stream = AST_VECTOR_GET(&pending_topology->streams, i);
1048 struct ast_stream *configured_stream =
1049 ast_stream_topology_get_first_stream_by_type(configured_topology, pending_stream->type);
1050 struct ast_stream *joint_stream;
1051
1052 if (!configured_stream) {
1053 joint_stream = ast_stream_clone(pending_stream, NULL);
1054 if (!joint_stream) {
1055 ao2_cleanup(joint_topology);
1056 return NULL;
1057 }
1058 ast_stream_set_state(joint_stream, AST_STREAM_STATE_REMOVED);
1059 } else {
1060 joint_stream = ast_stream_create_resolved(pending_stream, configured_stream, prefs, error_message);
1061 if (!joint_stream) {
1062 ao2_cleanup(joint_topology);
1063 return NULL;
1064 } else if (ast_stream_get_format_count(joint_stream) == 0) {
1065 ast_stream_set_state(joint_stream, AST_STREAM_STATE_REMOVED);
1066 }
1067 }
1068
1069 res = ast_stream_topology_append_stream(joint_topology, joint_stream);
1070 if (res < 0) {
1071 ast_stream_free(joint_stream);
1072 ao2_cleanup(joint_topology);
1073 return NULL;
1074 }
1075 }
1076
1077 return joint_topology;
1078 }
1079
1080 int ast_stream_get_group(const struct ast_stream *stream)
1081 {
1082 ast_assert(stream != NULL);
1083
1084 return stream->group;
1085 }
1086
1087 void ast_stream_set_group(struct ast_stream *stream, int group)
1088 {
1089 ast_assert(stream != NULL);
1090
1091 stream->group = group;
1092 }