+
+enum bt_graph_status bt_graph_add_component_with_init_method_data(
+ struct bt_graph *graph,
+ struct bt_component_class *component_class,
+ const char *name, struct bt_value *params,
+ void *init_method_data,
+ struct bt_component **user_component)
+{
+ enum bt_graph_status graph_status = BT_GRAPH_STATUS_OK;
+ enum bt_component_status comp_status;
+ struct bt_component *component = NULL;
+ enum bt_component_class_type type;
+ size_t i;
+ bt_bool init_can_consume;
+
+ bt_get(params);
+
+ if (!graph) {
+ BT_LOGW_STR("Invalid parameter: graph is NULL.");
+ graph_status = BT_GRAPH_STATUS_INVALID;
+ goto end;
+ }
+ init_can_consume = graph->can_consume;
+
+ if (!component_class) {
+ BT_LOGW_STR("Invalid parameter: component class is NULL.");
+ graph_status = BT_GRAPH_STATUS_INVALID;
+ goto end;
+ }
+
+ graph->can_consume = BT_FALSE;
+ type = bt_component_class_get_type(component_class);
+ BT_LOGD("Adding component to graph: "
+ "graph-addr=%p, comp-cls-addr=%p, "
+ "comp-cls-type=%s, name=\"%s\", params-addr=%p, "
+ "init-method-data-addr=%p",
+ graph, component_class, bt_component_class_type_string(type),
+ name, params, init_method_data);
+
+ if (!name) {
+ BT_LOGW_STR("Invalid parameter: name is NULL.");
+ graph_status = BT_GRAPH_STATUS_INVALID;
+ goto end;
+ }
+
+ if (graph->canceled) {
+ BT_LOGW_STR("Invalid parameter: graph is canceled.");
+ graph_status = BT_GRAPH_STATUS_CANCELED;
+ goto end;
+ }
+
+ if (type != BT_COMPONENT_CLASS_TYPE_SOURCE &&
+ type != BT_COMPONENT_CLASS_TYPE_FILTER &&
+ type != BT_COMPONENT_CLASS_TYPE_SINK) {
+ BT_LOGW("Invalid parameter: unknown component class type: "
+ "type=%d", type);
+ graph_status = BT_GRAPH_STATUS_INVALID;
+ goto end;
+ }
+
+ for (i = 0; i < graph->components->len; i++) {
+ void *other_comp = graph->components->pdata[i];
+
+ if (strcmp(name, bt_component_get_name(other_comp)) == 0) {
+ BT_LOGW("Invalid parameter: another component with the same name already exists in the graph: "
+ "other-comp-addr=%p, name=\"%s\"",
+ other_comp, name);
+ graph_status = BT_GRAPH_STATUS_INVALID;
+ goto end;
+ }
+ }
+
+ /*
+ * Parameters must be a map value, but we create a convenient
+ * empty one if it's NULL.
+ */
+ if (params) {
+ if (!bt_value_is_map(params)) {
+ BT_LOGW("Invalid parameter: initialization parameters must be a map value: "
+ "type=%s",
+ bt_value_type_string(bt_value_get_type(params)));
+ graph_status = BT_GRAPH_STATUS_INVALID;
+ goto end;
+ }
+ } else {
+ params = bt_value_map_create();
+ if (!params) {
+ BT_LOGE_STR("Cannot create map value object.");
+ graph_status = BT_GRAPH_STATUS_NOMEM;
+ goto end;
+ }
+ }
+
+ comp_status = bt_component_create(component_class, name, &component);
+ if (comp_status != BT_COMPONENT_STATUS_OK) {
+ BT_LOGE("Cannot create empty component object: status=%s",
+ bt_component_status_string(comp_status));
+ graph_status = bt_graph_status_from_component_status(
+ comp_status);
+ goto end;
+ }
+
+ /*
+ * The user's initialization method needs to see that this
+ * component is part of the graph. If the user method fails, we
+ * immediately remove the component from the graph's components.
+ */
+ g_ptr_array_add(graph->components, component);
+ bt_component_set_graph(component, graph);
+
+ if (component_class->methods.init) {
+ BT_LOGD_STR("Calling user's initialization method.");
+ comp_status = component_class->methods.init(
+ bt_private_component_from_component(component), params,
+ init_method_data);
+ BT_LOGD("User method returned: status=%s",
+ bt_component_status_string(comp_status));
+ if (comp_status != BT_COMPONENT_STATUS_OK) {
+ BT_LOGW_STR("Initialization method failed.");
+ graph_status = bt_graph_status_from_component_status(
+ comp_status);
+ bt_component_set_graph(component, NULL);
+ g_ptr_array_remove_fast(graph->components, component);
+ goto end;
+ }
+ }
+
+ /*
+ * Mark the component as initialized so that its finalization
+ * method is called when it is destroyed.
+ */
+ component->initialized = true;
+
+ /*
+ * If it's a sink component, it needs to be part of the graph's
+ * sink queue to be consumed by bt_graph_consume().
+ */
+ if (bt_component_is_sink(component)) {
+ graph->has_sink = BT_TRUE;
+ g_queue_push_tail(graph->sinks_to_consume, component);
+ }
+
+ /*
+ * Freeze the component class now that it's instantiated at
+ * least once.
+ */
+ BT_LOGD_STR("Freezing component class.");
+ bt_component_class_freeze(component->class);
+ BT_LOGD("Added component to graph: "
+ "graph-addr=%p, comp-cls-addr=%p, "
+ "comp-cls-type=%s, name=\"%s\", params-addr=%p, "
+ "init-method-data-addr=%p, comp-addr=%p",
+ graph, component_class, bt_component_class_type_string(type),
+ name, params, init_method_data, component);
+
+ if (user_component) {
+ /* Move reference to user */
+ *user_component = component;
+ component = NULL;
+ }
+
+end:
+ bt_put(component);
+ bt_put(params);
+ if (graph) {
+ graph->can_consume = init_can_consume;
+ }
+ return graph_status;
+}
+
+enum bt_graph_status bt_graph_add_component(
+ struct bt_graph *graph,
+ struct bt_component_class *component_class,
+ const char *name, struct bt_value *params,
+ struct bt_component **component)
+{
+ return bt_graph_add_component_with_init_method_data(graph,
+ component_class, name, params, NULL, component);
+}
+
+BT_HIDDEN
+int bt_graph_remove_unconnected_component(struct bt_graph *graph,
+ struct bt_component *component)
+{
+ bt_bool init_can_consume;
+ int64_t count;
+ uint64_t i;
+ int ret = 0;
+
+ BT_ASSERT(graph);
+ BT_ASSERT(component);
+ BT_ASSERT(component->base.ref_count.count == 0);
+ BT_ASSERT(bt_component_borrow_graph(component) == graph);
+
+ init_can_consume = graph->can_consume;
+ count = bt_component_get_input_port_count(component);
+
+ for (i = 0; i < count; i++) {
+ struct bt_port *port =
+ bt_component_get_input_port_by_index(component, i);
+
+ BT_ASSERT(port);
+ bt_put(port);
+
+ if (bt_port_is_connected(port)) {
+ BT_LOGW("Cannot remove component from graph: "
+ "an input port is connected: "
+ "graph-addr=%p, comp-addr=%p, "
+ "comp-name=\"%s\", connected-port-addr=%p, "
+ "connected-port-name=\"%s\"",
+ graph, component,
+ bt_component_get_name(component),
+ port, bt_port_get_name(port));
+ goto error;
+ }
+ }
+
+ count = bt_component_get_output_port_count(component);
+
+ for (i = 0; i < count; i++) {
+ struct bt_port *port =
+ bt_component_get_output_port_by_index(component, i);
+
+ BT_ASSERT(port);
+ bt_put(port);
+
+ if (bt_port_is_connected(port)) {
+ BT_LOGW("Cannot remove component from graph: "
+ "an output port is connected: "
+ "graph-addr=%p, comp-addr=%p, "
+ "comp-name=\"%s\", connected-port-addr=%p, "
+ "connected-port-name=\"%s\"",
+ graph, component,
+ bt_component_get_name(component),
+ port, bt_port_get_name(port));
+ goto error;
+ }
+ }
+
+ graph->can_consume = BT_FALSE;
+
+ /* Possibly remove from sinks to consume */
+ (void) g_queue_remove(graph->sinks_to_consume, component);
+
+ if (graph->sinks_to_consume->length == 0) {
+ graph->has_sink = BT_FALSE;
+ }
+
+ /*
+ * This calls bt_object_release() on the component, and since
+ * its reference count is 0, its destructor is called. Its
+ * destructor calls the user's finalization method (if set).
+ */
+ g_ptr_array_remove(graph->components, component);
+ goto end;
+
+error:
+ ret = -1;
+
+end:
+ graph->can_consume = init_can_consume;
+ return ret;
+}
+
+BT_HIDDEN
+void bt_graph_add_notification(struct bt_graph *graph,
+ struct bt_notification *notif)
+{
+ BT_ASSERT(graph);
+ BT_ASSERT(notif);
+
+ /*
+ * It's okay not to take a reference because, when a
+ * notification's reference count drops to 0, either:
+ *
+ * * It is recycled back to one of this graph's pool.
+ * * It is destroyed because it doesn't have any link to any
+ * graph, which means the original graph is already destroyed.
+ */
+ g_ptr_array_add(graph->notifications, notif);
+}