BuildSystem: Remove unused variables.
[asterisk/asterisk.git] / main / stream.c
index fb14693..61eef25 100644 (file)
@@ -67,6 +67,11 @@ struct ast_stream {
        ast_stream_data_free_fn data_free_fn[AST_STREAM_DATA_SLOT_MAX];
 
        /*!
+        * \brief The group that the stream is part of
+        */
+       int group;
+
+       /*!
         * \brief Name for the stream within the context of the channel it is on
         */
        char name[0];
@@ -90,6 +95,7 @@ struct ast_stream *ast_stream_alloc(const char *name, enum ast_media_type type)
 
        stream->type = type;
        stream->state = AST_STREAM_STATE_INACTIVE;
+       stream->group = -1;
        strcpy(stream->name, S_OR(name, "")); /* Safe */
 
        return stream;
@@ -115,6 +121,7 @@ struct ast_stream *ast_stream_clone(const struct ast_stream *stream, const char
 
        memcpy(new_stream, stream, sizeof(*new_stream));
        strcpy(new_stream->name, stream_name); /* Safe */
+       new_stream->group = -1;
        if (new_stream->formats) {
                ao2_ref(new_stream->formats, +1);
        }
@@ -214,6 +221,23 @@ const char *ast_stream_state2str(enum ast_stream_state state)
        }
 }
 
+enum ast_stream_state ast_stream_str2state(const char *str)
+{
+       if (!strcmp("sendrecv", str)) {
+               return AST_STREAM_STATE_SENDRECV;
+       }
+       if (!strcmp("sendonly", str)) {
+               return AST_STREAM_STATE_SENDONLY;
+       }
+       if (!strcmp("recvonly", str)) {
+               return AST_STREAM_STATE_RECVONLY;
+       }
+       if (!strcmp("inactive", str)) {
+               return AST_STREAM_STATE_INACTIVE;
+       }
+       return AST_STREAM_STATE_REMOVED;
+}
+
 void *ast_stream_get_data(struct ast_stream *stream, enum ast_stream_data_slot slot)
 {
        ast_assert(stream != NULL);
@@ -271,19 +295,68 @@ struct ast_stream_topology *ast_stream_topology_clone(
        }
 
        for (i = 0; i < AST_VECTOR_SIZE(&topology->streams); i++) {
-               struct ast_stream *stream =
-                       ast_stream_clone(AST_VECTOR_GET(&topology->streams, i), NULL);
+               struct ast_stream *existing = AST_VECTOR_GET(&topology->streams, i);
+               struct ast_stream *stream = ast_stream_clone(existing, NULL);
 
                if (!stream || AST_VECTOR_APPEND(&new_topology->streams, stream)) {
                        ast_stream_free(stream);
                        ast_stream_topology_free(new_topology);
                        return NULL;
                }
+
+               ast_stream_set_group(stream, ast_stream_get_group(existing));
        }
 
        return new_topology;
 }
 
+int ast_stream_topology_equal(const struct ast_stream_topology *left,
+       const struct ast_stream_topology *right)
+{
+       int index;
+
+       ast_assert(left != NULL);
+       ast_assert(right != NULL);
+
+       if (ast_stream_topology_get_count(left) != ast_stream_topology_get_count(right)) {
+               return 0;
+       }
+
+       for (index = 0; index < ast_stream_topology_get_count(left); ++index) {
+               const struct ast_stream *left_stream = ast_stream_topology_get_stream(left, index);
+               const struct ast_stream *right_stream = ast_stream_topology_get_stream(right, index);
+
+               if (ast_stream_get_type(left_stream) != ast_stream_get_type(right_stream)) {
+                       return 0;
+               }
+
+               if (ast_stream_get_state(left_stream) != ast_stream_get_state(right_stream)) {
+                       return 0;
+               }
+
+               if (!ast_stream_get_formats(left_stream) && ast_stream_get_formats(right_stream) &&
+                       ast_format_cap_count(ast_stream_get_formats(right_stream))) {
+                       /* A NULL format capabilities and an empty format capabilities are the same, as they have
+                        * no formats inside. If one does though... they are not equal.
+                        */
+                       return 0;
+               } else if (!ast_stream_get_formats(right_stream) && ast_stream_get_formats(left_stream) &&
+                       ast_format_cap_count(ast_stream_get_formats(left_stream))) {
+                       return 0;
+               } else if (ast_stream_get_formats(left_stream) && ast_stream_get_formats(right_stream) &&
+                       !ast_format_cap_identical(ast_stream_get_formats(left_stream), ast_stream_get_formats(right_stream))) {
+                       /* But if both are actually present we need to do an actual identical check. */
+                       return 0;
+               }
+
+               if (strcmp(ast_stream_get_name(left_stream), ast_stream_get_name(right_stream))) {
+                       return 0;
+               }
+       }
+
+       return 1;
+}
+
 void ast_stream_topology_free(struct ast_stream_topology *topology)
 {
        if (!topology) {
@@ -342,13 +415,35 @@ int ast_stream_topology_set_stream(struct ast_stream_topology *topology,
        stream->position = position;
 
        if (position == AST_VECTOR_SIZE(&topology->streams)) {
-               AST_VECTOR_APPEND(&topology->streams, stream);
-               return 0;
+               return AST_VECTOR_APPEND(&topology->streams, stream);
        }
 
        return AST_VECTOR_REPLACE(&topology->streams, position, stream);
 }
 
+int ast_stream_topology_del_stream(struct ast_stream_topology *topology,
+       unsigned int position)
+{
+       struct ast_stream *stream;
+
+       ast_assert(topology != NULL);
+
+       if (AST_VECTOR_SIZE(&topology->streams) <= position) {
+               return -1;
+       }
+
+       stream = AST_VECTOR_REMOVE_ORDERED(&topology->streams, position);
+       ast_stream_free(stream);
+
+       /* Fix up higher stream position indices */
+       for (; position < AST_VECTOR_SIZE(&topology->streams); ++position) {
+               stream = AST_VECTOR_GET(&topology->streams, position);
+               stream->position = position;
+       }
+
+       return 0;
+}
+
 struct ast_stream_topology *ast_stream_topology_create_from_format_cap(
        struct ast_format_cap *cap)
 {
@@ -414,9 +509,11 @@ struct ast_format_cap *ast_format_cap_from_stream_topology(
        }
 
        for (i = 0; i < AST_VECTOR_SIZE(&topology->streams); i++) {
-               struct ast_stream *stream = AST_VECTOR_GET(&topology->streams, i);
+               struct ast_stream *stream;
 
-               if (!stream->formats) {
+               stream = AST_VECTOR_GET(&topology->streams, i);
+               if (!stream->formats
+                       || stream->state == AST_STREAM_STATE_REMOVED) {
                        continue;
                }
 
@@ -435,9 +532,11 @@ struct ast_stream *ast_stream_topology_get_first_stream_by_type(
        ast_assert(topology != NULL);
 
        for (i = 0; i < AST_VECTOR_SIZE(&topology->streams); i++) {
-               struct ast_stream *stream = AST_VECTOR_GET(&topology->streams, i);
+               struct ast_stream *stream;
 
-               if (stream->type == type) {
+               stream = AST_VECTOR_GET(&topology->streams, i);
+               if (stream->type == type
+                       && stream->state != AST_STREAM_STATE_REMOVED) {
                        return stream;
                }
        }
@@ -489,3 +588,17 @@ void ast_stream_topology_map(const struct ast_stream_topology *topology,
                AST_VECTOR_REPLACE(v1, index, i);
        }
 }
+
+int ast_stream_get_group(const struct ast_stream *stream)
+{
+       ast_assert(stream != NULL);
+
+       return stream->group;
+}
+
+void ast_stream_set_group(struct ast_stream *stream, int group)
+{
+       ast_assert(stream != NULL);
+
+       stream->group = group;
+}