+
+static
+void bt_output_port_notification_iterator_destroy(struct bt_object *obj)
+{
+ 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 output port.");
+ bt_put(iterator->output_port);
+ 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,
+ const enum bt_notification_type *notification_types)
+{
+ struct bt_notification_iterator *iterator_base = NULL;
+ 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;
+
+ if (!output_port) {
+ BT_LOGW_STR("Invalid parameter: port is NULL.");
+ goto error;
+ }
+
+ if (bt_port_get_type(output_port) != BT_PORT_TYPE_OUTPUT) {
+ BT_LOGW_STR("Invalid parameter: port is not an output port.");
+ goto error;
+ }
+
+ output_port_comp = bt_port_get_component(output_port);
+ if (!output_port_comp) {
+ BT_LOGW("Cannot get output port's component: "
+ "port-addr=%p, port-name=\"%s\"",
+ output_port, bt_port_get_name(output_port));
+ goto error;
+ }
+
+ graph = bt_component_get_graph(output_port_comp);
+ 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;
+ }
+
+ init_notification_iterator((void *) iterator,
+ BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT,
+ bt_output_port_notification_iterator_destroy);
+
+ /* 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);
+ iterator_base = (void *) iterator;
+ colander_comp_name =
+ colander_component_name ? colander_component_name : "colander";
+ colander_data.notification = &iterator_base->current_notification;
+ colander_data.notification_types = notification_types;
+ 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);
+ 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 base notification iterator's current
+ * 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);
+ 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;
+}