+ struct bt_notification_iterator_output_port *iterator =
+ (void *) container_of(obj, struct bt_notification_iterator, base);
+
+ BT_LOGD("Destroying output port notification iterator object: addr=%p",
+ iterator);
+ BT_LOGD_STR("Putting graph.");
+ bt_put(iterator->graph);
+ BT_LOGD_STR("Putting colander sink component.");
+ bt_put(iterator->colander);
+ destroy_base_notification_iterator(obj);
+}
+
+struct bt_notification_iterator *bt_output_port_notification_iterator_create(
+ struct bt_port *output_port,
+ const char *colander_component_name)
+{
+ struct bt_notification_iterator_output_port *iterator = NULL;
+ struct bt_component_class *colander_comp_cls = NULL;
+ struct bt_component *output_port_comp = NULL;
+ struct bt_component *colander_comp;
+ struct bt_graph *graph = NULL;
+ enum bt_graph_status graph_status;
+ const char *colander_comp_name;
+ struct bt_port *colander_in_port = NULL;
+ struct bt_component_class_sink_colander_data colander_data;
+ int ret;
+
+ BT_ASSERT_PRE_NON_NULL(output_port, "Output port");
+ BT_ASSERT_PRE(bt_port_get_type(output_port) == BT_PORT_TYPE_OUTPUT,
+ "Port is not an output port: %!+p", output_port);
+ output_port_comp = bt_port_get_component(output_port);
+ BT_ASSERT_PRE(output_port_comp,
+ "Output port has no component: %!+p", output_port);
+ graph = bt_component_get_graph(output_port_comp);
+ BT_ASSERT(graph);
+
+ /* Create notification iterator */
+ BT_LOGD("Creating notification iterator on output port: "
+ "comp-addr=%p, comp-name\"%s\", port-addr=%p, port-name=\"%s\"",
+ output_port_comp, bt_component_get_name(output_port_comp),
+ output_port, bt_port_get_name(output_port));
+ iterator = g_new0(struct bt_notification_iterator_output_port, 1);
+ if (!iterator) {
+ BT_LOGE_STR("Failed to allocate one output port notification iterator.");
+ goto error;
+ }
+
+ ret = init_notification_iterator((void *) iterator,
+ BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT,
+ bt_output_port_notification_iterator_destroy);
+ if (ret) {
+ /* init_notification_iterator() logs errors */
+ BT_PUT(iterator);
+ goto end;
+ }
+
+ /* Create colander component */
+ colander_comp_cls = bt_component_class_sink_colander_get();
+ if (!colander_comp_cls) {
+ BT_LOGW("Cannot get colander sink component class.");
+ goto error;
+ }
+
+ BT_MOVE(iterator->graph, graph);
+ colander_comp_name =
+ colander_component_name ? colander_component_name : "colander";
+ colander_data.notifs = (void *) iterator->base.notifs->pdata;
+ colander_data.count_addr = &iterator->count;
+
+ graph_status = bt_graph_add_component_with_init_method_data(
+ iterator->graph, colander_comp_cls, colander_comp_name,
+ NULL, &colander_data, &iterator->colander);
+ if (graph_status != BT_GRAPH_STATUS_OK) {
+ BT_LOGW("Cannot add colander sink component to graph: "
+ "graph-addr=%p, name=\"%s\", graph-status=%s",
+ iterator->graph, colander_comp_name,
+ bt_graph_status_string(graph_status));
+ goto error;
+ }
+
+ /*
+ * Connect provided output port to the colander component's
+ * input port.
+ */
+ colander_in_port = bt_component_sink_get_input_port_by_index(
+ iterator->colander, 0);
+ BT_ASSERT(colander_in_port);
+ graph_status = bt_graph_connect_ports(iterator->graph,
+ output_port, colander_in_port, NULL);
+ if (graph_status != BT_GRAPH_STATUS_OK) {
+ BT_LOGW("Cannot add colander sink component to graph: "
+ "graph-addr=%p, name=\"%s\", graph-status=%s",
+ iterator->graph, colander_comp_name,
+ bt_graph_status_string(graph_status));
+ goto error;
+ }
+
+ /*
+ * At this point everything went fine. Make the graph
+ * nonconsumable forever so that only this notification iterator
+ * can consume (thanks to bt_graph_consume_sink_no_check()).
+ * This avoids leaking the notification created by the colander
+ * sink and moved to the notification iterator's notification
+ * member.
+ */
+ bt_graph_set_can_consume(iterator->graph, BT_FALSE);
+ goto end;
+
+error:
+ if (iterator && iterator->graph && iterator->colander) {
+ int ret;
+
+ /* Remove created colander component from graph if any */
+ colander_comp = iterator->colander;
+ BT_PUT(iterator->colander);
+
+ /*
+ * At this point the colander component's reference
+ * count is 0 because iterator->colander was the only
+ * owner. We also know that it is not connected because
+ * this is the last operation before this function
+ * succeeds.
+ *
+ * Since we honor the preconditions here,
+ * bt_graph_remove_unconnected_component() always
+ * succeeds.
+ */
+ ret = bt_graph_remove_unconnected_component(iterator->graph,
+ colander_comp);
+ BT_ASSERT(ret == 0);
+ }
+
+ BT_PUT(iterator);
+
+end:
+ bt_put(colander_in_port);
+ bt_put(colander_comp_cls);
+ bt_put(output_port_comp);
+ bt_put(graph);
+ return (void *) iterator;
+}
+
+struct bt_notification_iterator *
+bt_private_connection_notification_iterator_borrow_from_private(
+ struct bt_private_connection_private_notification_iterator *private_notification_iterator)
+{
+ return (void *) private_notification_iterator;