| 1 | /* |
| 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
| 3 | * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | * of this software and associated documentation files (the "Software"), to deal |
| 7 | * in the Software without restriction, including without limitation the rights |
| 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | * copies of the Software, and to permit persons to whom the Software is |
| 10 | * furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be included in |
| 13 | * all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | * SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #define BT_LOG_TAG "LIB/GRAPH" |
| 25 | #include "lib/logging.h" |
| 26 | |
| 27 | #include "common/assert.h" |
| 28 | #include "lib/assert-pre.h" |
| 29 | #include "lib/assert-post.h" |
| 30 | #include <babeltrace2/graph/graph.h> |
| 31 | #include <babeltrace2/graph/component.h> |
| 32 | #include <babeltrace2/graph/port.h> |
| 33 | #include "lib/graph/message/message.h" |
| 34 | #include "compat/compiler.h" |
| 35 | #include "common/common.h" |
| 36 | #include <babeltrace2/types.h> |
| 37 | #include <babeltrace2/value.h> |
| 38 | #include "lib/value.h" |
| 39 | #include <unistd.h> |
| 40 | #include <stdbool.h> |
| 41 | #include <glib.h> |
| 42 | |
| 43 | #include "component-class-sink-simple.h" |
| 44 | #include "component.h" |
| 45 | #include "component-sink.h" |
| 46 | #include "connection.h" |
| 47 | #include "graph.h" |
| 48 | #include "interrupter.h" |
| 49 | #include "message/event.h" |
| 50 | #include "message/packet.h" |
| 51 | |
| 52 | typedef enum bt_graph_listener_func_status |
| 53 | (*port_added_func_t)(const void *, const void *, void *); |
| 54 | |
| 55 | typedef enum bt_component_class_initialize_method_status |
| 56 | (*comp_init_method_t)(const void *, void *, const void *, void *); |
| 57 | |
| 58 | struct bt_graph_listener_port_added { |
| 59 | port_added_func_t func; |
| 60 | void *data; |
| 61 | }; |
| 62 | |
| 63 | #define INIT_LISTENERS_ARRAY(_type, _listeners) \ |
| 64 | do { \ |
| 65 | _listeners = g_array_new(FALSE, TRUE, sizeof(_type)); \ |
| 66 | if (!(_listeners)) { \ |
| 67 | BT_LIB_LOGE_APPEND_CAUSE( \ |
| 68 | "Failed to allocate one GArray."); \ |
| 69 | } \ |
| 70 | } while (0) |
| 71 | |
| 72 | static |
| 73 | void destroy_graph(struct bt_object *obj) |
| 74 | { |
| 75 | struct bt_graph *graph = container_of(obj, struct bt_graph, base); |
| 76 | |
| 77 | /* |
| 78 | * The graph's reference count is 0 if we're here. Increment |
| 79 | * it to avoid a double-destroy (possibly infinitely recursive) |
| 80 | * in this situation: |
| 81 | * |
| 82 | * 1. We put and destroy a connection. |
| 83 | * 2. This connection's destructor finalizes its active message |
| 84 | * iterators. |
| 85 | * 3. A message iterator's finalization function gets a new |
| 86 | * reference on its component (reference count goes from 0 to |
| 87 | * 1). |
| 88 | * 4. Since this component's reference count goes to 1, it takes |
| 89 | * a reference on its parent (this graph). This graph's |
| 90 | * reference count goes from 0 to 1. |
| 91 | * 5. The message iterator's finalization function puts its |
| 92 | * component reference (reference count goes from 1 to 0). |
| 93 | * 6. Since this component's reference count goes from 1 to 0, |
| 94 | * it puts its parent (this graph). This graph's reference |
| 95 | * count goes from 1 to 0. |
| 96 | * 7. Since this graph's reference count goes from 1 to 0, its |
| 97 | * destructor is called (this function). |
| 98 | * |
| 99 | * With the incrementation below, the graph's reference count at |
| 100 | * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This |
| 101 | * ensures that this function is not called two times. |
| 102 | */ |
| 103 | BT_LIB_LOGI("Destroying graph: %!+g", graph); |
| 104 | obj->ref_count++; |
| 105 | graph->config_state = BT_GRAPH_CONFIGURATION_STATE_DESTROYING; |
| 106 | |
| 107 | if (graph->messages) { |
| 108 | g_ptr_array_free(graph->messages, TRUE); |
| 109 | graph->messages = NULL; |
| 110 | } |
| 111 | |
| 112 | if (graph->connections) { |
| 113 | BT_LOGD_STR("Destroying connections."); |
| 114 | g_ptr_array_free(graph->connections, TRUE); |
| 115 | graph->connections = NULL; |
| 116 | } |
| 117 | |
| 118 | if (graph->components) { |
| 119 | BT_LOGD_STR("Destroying components."); |
| 120 | g_ptr_array_free(graph->components, TRUE); |
| 121 | graph->components = NULL; |
| 122 | } |
| 123 | |
| 124 | if (graph->interrupters) { |
| 125 | BT_LOGD_STR("Putting interrupters."); |
| 126 | g_ptr_array_free(graph->interrupters, TRUE); |
| 127 | graph->interrupters = NULL; |
| 128 | } |
| 129 | |
| 130 | BT_OBJECT_PUT_REF_AND_RESET(graph->default_interrupter); |
| 131 | |
| 132 | if (graph->sinks_to_consume) { |
| 133 | g_queue_free(graph->sinks_to_consume); |
| 134 | graph->sinks_to_consume = NULL; |
| 135 | } |
| 136 | |
| 137 | if (graph->listeners.source_output_port_added) { |
| 138 | g_array_free(graph->listeners.source_output_port_added, TRUE); |
| 139 | graph->listeners.source_output_port_added = NULL; |
| 140 | } |
| 141 | |
| 142 | if (graph->listeners.filter_output_port_added) { |
| 143 | g_array_free(graph->listeners.filter_output_port_added, TRUE); |
| 144 | graph->listeners.filter_output_port_added = NULL; |
| 145 | } |
| 146 | |
| 147 | if (graph->listeners.filter_input_port_added) { |
| 148 | g_array_free(graph->listeners.filter_input_port_added, TRUE); |
| 149 | graph->listeners.filter_input_port_added = NULL; |
| 150 | } |
| 151 | |
| 152 | if (graph->listeners.sink_input_port_added) { |
| 153 | g_array_free(graph->listeners.sink_input_port_added, TRUE); |
| 154 | graph->listeners.sink_input_port_added = NULL; |
| 155 | } |
| 156 | |
| 157 | bt_object_pool_finalize(&graph->event_msg_pool); |
| 158 | bt_object_pool_finalize(&graph->packet_begin_msg_pool); |
| 159 | bt_object_pool_finalize(&graph->packet_end_msg_pool); |
| 160 | g_free(graph); |
| 161 | } |
| 162 | |
| 163 | static |
| 164 | void destroy_message_event(struct bt_message *msg, |
| 165 | struct bt_graph *graph) |
| 166 | { |
| 167 | bt_message_event_destroy(msg); |
| 168 | } |
| 169 | |
| 170 | static |
| 171 | void destroy_message_packet_begin(struct bt_message *msg, |
| 172 | struct bt_graph *graph) |
| 173 | { |
| 174 | bt_message_packet_destroy(msg); |
| 175 | } |
| 176 | |
| 177 | static |
| 178 | void destroy_message_packet_end(struct bt_message *msg, |
| 179 | struct bt_graph *graph) |
| 180 | { |
| 181 | bt_message_packet_destroy(msg); |
| 182 | } |
| 183 | |
| 184 | static |
| 185 | void notify_message_graph_is_destroyed(struct bt_message *msg) |
| 186 | { |
| 187 | bt_message_unlink_graph(msg); |
| 188 | } |
| 189 | |
| 190 | struct bt_graph *bt_graph_create(uint64_t mip_version) |
| 191 | { |
| 192 | struct bt_graph *graph; |
| 193 | int ret; |
| 194 | |
| 195 | BT_ASSERT_PRE_NO_ERROR(); |
| 196 | BT_ASSERT_PRE(mip_version <= bt_get_maximal_mip_version(), |
| 197 | "Unknown MIP version: mip-version=%" PRIu64 ", " |
| 198 | "max-mip-version=%" PRIu64, |
| 199 | mip_version, bt_get_maximal_mip_version()); |
| 200 | BT_LOGI_STR("Creating graph object."); |
| 201 | graph = g_new0(struct bt_graph, 1); |
| 202 | if (!graph) { |
| 203 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one graph."); |
| 204 | goto end; |
| 205 | } |
| 206 | |
| 207 | bt_object_init_shared(&graph->base, destroy_graph); |
| 208 | graph->mip_version = mip_version; |
| 209 | graph->connections = g_ptr_array_new_with_free_func( |
| 210 | (GDestroyNotify) bt_object_try_spec_release); |
| 211 | if (!graph->connections) { |
| 212 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); |
| 213 | goto error; |
| 214 | } |
| 215 | graph->components = g_ptr_array_new_with_free_func( |
| 216 | (GDestroyNotify) bt_object_try_spec_release); |
| 217 | if (!graph->components) { |
| 218 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); |
| 219 | goto error; |
| 220 | } |
| 221 | graph->sinks_to_consume = g_queue_new(); |
| 222 | if (!graph->sinks_to_consume) { |
| 223 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GQueue."); |
| 224 | goto error; |
| 225 | } |
| 226 | |
| 227 | bt_graph_set_can_consume(graph, true); |
| 228 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added, |
| 229 | graph->listeners.source_output_port_added); |
| 230 | |
| 231 | if (!graph->listeners.source_output_port_added) { |
| 232 | goto error; |
| 233 | } |
| 234 | |
| 235 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added, |
| 236 | graph->listeners.filter_output_port_added); |
| 237 | |
| 238 | if (!graph->listeners.filter_output_port_added) { |
| 239 | goto error; |
| 240 | } |
| 241 | |
| 242 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added, |
| 243 | graph->listeners.filter_input_port_added); |
| 244 | |
| 245 | if (!graph->listeners.filter_input_port_added) { |
| 246 | goto error; |
| 247 | } |
| 248 | |
| 249 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added, |
| 250 | graph->listeners.sink_input_port_added); |
| 251 | |
| 252 | if (!graph->listeners.sink_input_port_added) { |
| 253 | goto error; |
| 254 | } |
| 255 | |
| 256 | graph->interrupters = g_ptr_array_new_with_free_func( |
| 257 | (GDestroyNotify) bt_object_put_ref_no_null_check); |
| 258 | if (!graph->interrupters) { |
| 259 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); |
| 260 | goto error; |
| 261 | } |
| 262 | |
| 263 | graph->default_interrupter = bt_interrupter_create(); |
| 264 | if (!graph->default_interrupter) { |
| 265 | BT_LIB_LOGE_APPEND_CAUSE( |
| 266 | "Failed to create one interrupter object."); |
| 267 | goto error; |
| 268 | } |
| 269 | |
| 270 | bt_graph_add_interrupter(graph, graph->default_interrupter); |
| 271 | ret = bt_object_pool_initialize(&graph->event_msg_pool, |
| 272 | (bt_object_pool_new_object_func) bt_message_event_new, |
| 273 | (bt_object_pool_destroy_object_func) destroy_message_event, |
| 274 | graph); |
| 275 | if (ret) { |
| 276 | BT_LIB_LOGE_APPEND_CAUSE( |
| 277 | "Failed to initialize event message pool: ret=%d", |
| 278 | ret); |
| 279 | goto error; |
| 280 | } |
| 281 | |
| 282 | ret = bt_object_pool_initialize(&graph->packet_begin_msg_pool, |
| 283 | (bt_object_pool_new_object_func) bt_message_packet_beginning_new, |
| 284 | (bt_object_pool_destroy_object_func) destroy_message_packet_begin, |
| 285 | graph); |
| 286 | if (ret) { |
| 287 | BT_LIB_LOGE_APPEND_CAUSE( |
| 288 | "Failed to initialize packet beginning message pool: ret=%d", |
| 289 | ret); |
| 290 | goto error; |
| 291 | } |
| 292 | |
| 293 | ret = bt_object_pool_initialize(&graph->packet_end_msg_pool, |
| 294 | (bt_object_pool_new_object_func) bt_message_packet_end_new, |
| 295 | (bt_object_pool_destroy_object_func) destroy_message_packet_end, |
| 296 | graph); |
| 297 | if (ret) { |
| 298 | BT_LIB_LOGE_APPEND_CAUSE( |
| 299 | "Failed to initialize packet end message pool: ret=%d", |
| 300 | ret); |
| 301 | goto error; |
| 302 | } |
| 303 | |
| 304 | graph->messages = g_ptr_array_new_with_free_func( |
| 305 | (GDestroyNotify) notify_message_graph_is_destroyed); |
| 306 | BT_LIB_LOGI("Created graph object: %!+g", graph); |
| 307 | |
| 308 | end: |
| 309 | return (void *) graph; |
| 310 | |
| 311 | error: |
| 312 | BT_OBJECT_PUT_REF_AND_RESET(graph); |
| 313 | goto end; |
| 314 | } |
| 315 | |
| 316 | enum bt_graph_connect_ports_status bt_graph_connect_ports( |
| 317 | struct bt_graph *graph, |
| 318 | const struct bt_port_output *upstream_port_out, |
| 319 | const struct bt_port_input *downstream_port_in, |
| 320 | const struct bt_connection **user_connection) |
| 321 | { |
| 322 | enum bt_graph_connect_ports_status status = BT_FUNC_STATUS_OK; |
| 323 | struct bt_connection *connection = NULL; |
| 324 | struct bt_port *upstream_port = (void *) upstream_port_out; |
| 325 | struct bt_port *downstream_port = (void *) downstream_port_in; |
| 326 | struct bt_component *upstream_component = NULL; |
| 327 | struct bt_component *downstream_component = NULL; |
| 328 | enum bt_component_class_port_connected_method_status port_connected_status; |
| 329 | bool init_can_consume; |
| 330 | |
| 331 | BT_ASSERT_PRE_NO_ERROR(); |
| 332 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 333 | BT_ASSERT_PRE_NON_NULL(upstream_port, "Upstream port"); |
| 334 | BT_ASSERT_PRE_NON_NULL(downstream_port, "Downstream port port"); |
| 335 | BT_ASSERT_PRE( |
| 336 | graph->config_state == BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, |
| 337 | "Graph is not in the \"configuring\" state: %!+g", graph); |
| 338 | BT_ASSERT_PRE(!bt_port_is_connected(upstream_port), |
| 339 | "Upstream port is already connected: %!+p", upstream_port); |
| 340 | BT_ASSERT_PRE(!bt_port_is_connected(downstream_port), |
| 341 | "Downstream port is already connected: %!+p", downstream_port); |
| 342 | BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) upstream_port), |
| 343 | "Upstream port does not belong to a component: %!+p", |
| 344 | upstream_port); |
| 345 | BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) downstream_port), |
| 346 | "Downstream port does not belong to a component: %!+p", |
| 347 | downstream_port); |
| 348 | init_can_consume = graph->can_consume; |
| 349 | BT_LIB_LOGI("Connecting component ports within graph: " |
| 350 | "%![graph-]+g, %![up-port-]+p, %![down-port-]+p", |
| 351 | graph, upstream_port, downstream_port); |
| 352 | bt_graph_set_can_consume(graph, false); |
| 353 | upstream_component = bt_port_borrow_component_inline( |
| 354 | (void *) upstream_port); |
| 355 | downstream_component = bt_port_borrow_component_inline( |
| 356 | (void *) downstream_port); |
| 357 | |
| 358 | BT_LOGD_STR("Creating connection."); |
| 359 | connection = bt_connection_create(graph, (void *) upstream_port, |
| 360 | (void *) downstream_port); |
| 361 | if (!connection) { |
| 362 | BT_LIB_LOGE_APPEND_CAUSE("Cannot create connection object."); |
| 363 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 364 | goto end; |
| 365 | } |
| 366 | |
| 367 | BT_LIB_LOGD("Connection object created: %!+x", connection); |
| 368 | |
| 369 | /* |
| 370 | * Ownership of upstream_component/downstream_component and of |
| 371 | * the connection object is transferred to the graph. |
| 372 | */ |
| 373 | g_ptr_array_add(graph->connections, connection); |
| 374 | |
| 375 | /* |
| 376 | * Notify both components that their port is connected. |
| 377 | */ |
| 378 | BT_LIB_LOGD("Notifying upstream component that its port is connected: " |
| 379 | "%![comp-]+c, %![port-]+p", upstream_component, upstream_port); |
| 380 | port_connected_status = bt_component_port_connected(upstream_component, |
| 381 | (void *) upstream_port, (void *) downstream_port); |
| 382 | if (port_connected_status != BT_FUNC_STATUS_OK) { |
| 383 | if (port_connected_status < 0) { |
| 384 | BT_LIB_LOGW_APPEND_CAUSE( |
| 385 | "Upstream component's \"port connected\" method failed: " |
| 386 | "status=%s, %![graph-]+g, %![up-comp-]+c, " |
| 387 | "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p", |
| 388 | bt_common_func_status_string( |
| 389 | port_connected_status), |
| 390 | graph, upstream_component, downstream_component, |
| 391 | upstream_port, downstream_port); |
| 392 | } |
| 393 | |
| 394 | bt_connection_end(connection, true); |
| 395 | status = (int) port_connected_status; |
| 396 | goto end; |
| 397 | } |
| 398 | |
| 399 | connection->notified_upstream_port_connected = true; |
| 400 | BT_LIB_LOGD("Notifying downstream component that its port is connected: " |
| 401 | "%![comp-]+c, %![port-]+p", downstream_component, |
| 402 | downstream_port); |
| 403 | port_connected_status = bt_component_port_connected(downstream_component, |
| 404 | (void *) downstream_port, (void *) upstream_port); |
| 405 | if (port_connected_status != BT_FUNC_STATUS_OK) { |
| 406 | if (port_connected_status < 0) { |
| 407 | BT_LIB_LOGW_APPEND_CAUSE( |
| 408 | "Downstream component's \"port connected\" method failed: " |
| 409 | "status=%s, %![graph-]+g, %![up-comp-]+c, " |
| 410 | "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p", |
| 411 | bt_common_func_status_string( |
| 412 | port_connected_status), |
| 413 | graph, upstream_component, downstream_component, |
| 414 | upstream_port, downstream_port); |
| 415 | } |
| 416 | |
| 417 | bt_connection_end(connection, true); |
| 418 | status = (int) port_connected_status; |
| 419 | goto end; |
| 420 | } |
| 421 | |
| 422 | connection->notified_downstream_port_connected = true; |
| 423 | |
| 424 | BT_LIB_LOGI("Connected component ports within graph: " |
| 425 | "%![graph-]+g, %![up-comp-]+c, %![down-comp-]+c, " |
| 426 | "%![up-port-]+p, %![down-port-]+p", |
| 427 | graph, upstream_component, downstream_component, |
| 428 | upstream_port, downstream_port); |
| 429 | |
| 430 | if (user_connection) { |
| 431 | /* Move reference to user */ |
| 432 | *user_connection = connection; |
| 433 | } |
| 434 | |
| 435 | end: |
| 436 | if (status != BT_FUNC_STATUS_OK) { |
| 437 | bt_graph_make_faulty(graph); |
| 438 | } |
| 439 | |
| 440 | bt_object_put_ref(connection); |
| 441 | (void) init_can_consume; |
| 442 | bt_graph_set_can_consume(graph, init_can_consume); |
| 443 | return status; |
| 444 | } |
| 445 | |
| 446 | static inline |
| 447 | int consume_graph_sink(struct bt_component_sink *comp) |
| 448 | { |
| 449 | enum bt_component_class_sink_consume_method_status consume_status; |
| 450 | struct bt_component_class_sink *sink_class = NULL; |
| 451 | |
| 452 | BT_ASSERT_DBG(comp); |
| 453 | sink_class = (void *) comp->parent.class; |
| 454 | BT_ASSERT_DBG(sink_class->methods.consume); |
| 455 | BT_LIB_LOGD("Calling user's consume method: %!+c", comp); |
| 456 | consume_status = sink_class->methods.consume((void *) comp); |
| 457 | BT_LOGD("User method returned: status=%s", |
| 458 | bt_common_func_status_string(consume_status)); |
| 459 | BT_ASSERT_POST_DEV(consume_status == BT_FUNC_STATUS_OK || |
| 460 | consume_status == BT_FUNC_STATUS_END || |
| 461 | consume_status == BT_FUNC_STATUS_AGAIN || |
| 462 | consume_status == BT_FUNC_STATUS_ERROR || |
| 463 | consume_status == BT_FUNC_STATUS_MEMORY_ERROR, |
| 464 | "Invalid component status returned by consuming method: " |
| 465 | "status=%s", bt_common_func_status_string(consume_status)); |
| 466 | BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(consume_status); |
| 467 | if (consume_status) { |
| 468 | if (consume_status < 0) { |
| 469 | BT_LIB_LOGW_APPEND_CAUSE( |
| 470 | "Component's \"consume\" method failed: " |
| 471 | "status=%s, %![comp-]+c", |
| 472 | bt_common_func_status_string(consume_status), |
| 473 | comp); |
| 474 | } |
| 475 | |
| 476 | goto end; |
| 477 | } |
| 478 | |
| 479 | BT_LIB_LOGD("Consumed from sink: %![comp-]+c, status=%s", |
| 480 | comp, bt_common_func_status_string(consume_status)); |
| 481 | |
| 482 | end: |
| 483 | return consume_status; |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | * `node` is removed from the queue of sinks to consume when passed to |
| 488 | * this function. This function adds it back to the queue if there's |
| 489 | * still something to consume afterwards. |
| 490 | */ |
| 491 | static inline |
| 492 | int consume_sink_node(struct bt_graph *graph, GList *node) |
| 493 | { |
| 494 | int status; |
| 495 | struct bt_component_sink *sink; |
| 496 | |
| 497 | sink = node->data; |
| 498 | status = consume_graph_sink(sink); |
| 499 | if (G_UNLIKELY(status != BT_FUNC_STATUS_END)) { |
| 500 | g_queue_push_tail_link(graph->sinks_to_consume, node); |
| 501 | goto end; |
| 502 | } |
| 503 | |
| 504 | /* End reached, the node is not added back to the queue and free'd. */ |
| 505 | g_queue_delete_link(graph->sinks_to_consume, node); |
| 506 | |
| 507 | /* Don't forward an END status if there are sinks left to consume. */ |
| 508 | if (!g_queue_is_empty(graph->sinks_to_consume)) { |
| 509 | status = BT_FUNC_STATUS_OK; |
| 510 | goto end; |
| 511 | } |
| 512 | |
| 513 | end: |
| 514 | BT_LIB_LOGD("Consumed sink node: %![comp-]+c, status=%s", |
| 515 | sink, bt_common_func_status_string(status)); |
| 516 | return status; |
| 517 | } |
| 518 | |
| 519 | BT_HIDDEN |
| 520 | int bt_graph_consume_sink_no_check(struct bt_graph *graph, |
| 521 | struct bt_component_sink *sink) |
| 522 | { |
| 523 | int status; |
| 524 | GList *sink_node; |
| 525 | int index; |
| 526 | |
| 527 | BT_LIB_LOGD("Making specific sink consume: %![comp-]+c", sink); |
| 528 | BT_ASSERT_DBG(bt_component_borrow_graph((void *) sink) == graph); |
| 529 | |
| 530 | if (g_queue_is_empty(graph->sinks_to_consume)) { |
| 531 | BT_LOGD_STR("Graph's sink queue is empty: end of graph."); |
| 532 | status = BT_FUNC_STATUS_END; |
| 533 | goto end; |
| 534 | } |
| 535 | |
| 536 | index = g_queue_index(graph->sinks_to_consume, sink); |
| 537 | if (index < 0) { |
| 538 | BT_LIB_LOGD("Sink component is not marked as consumable: " |
| 539 | "component sink is ended: %![comp-]+c", sink); |
| 540 | status = BT_FUNC_STATUS_END; |
| 541 | goto end; |
| 542 | } |
| 543 | |
| 544 | sink_node = g_queue_pop_nth_link(graph->sinks_to_consume, index); |
| 545 | BT_ASSERT_DBG(sink_node); |
| 546 | status = consume_sink_node(graph, sink_node); |
| 547 | |
| 548 | end: |
| 549 | return status; |
| 550 | } |
| 551 | |
| 552 | static inline |
| 553 | int consume_no_check(struct bt_graph *graph) |
| 554 | { |
| 555 | int status = BT_FUNC_STATUS_OK; |
| 556 | struct bt_component *sink; |
| 557 | GList *current_node; |
| 558 | |
| 559 | BT_ASSERT_PRE_DEV(graph->has_sink, |
| 560 | "Graph has no sink component: %!+g", graph); |
| 561 | BT_LIB_LOGD("Making next sink component consume: %![graph-]+g", graph); |
| 562 | |
| 563 | if (G_UNLIKELY(g_queue_is_empty(graph->sinks_to_consume))) { |
| 564 | BT_LOGD_STR("Graph's sink queue is empty: end of graph."); |
| 565 | status = BT_FUNC_STATUS_END; |
| 566 | goto end; |
| 567 | } |
| 568 | |
| 569 | current_node = g_queue_pop_head_link(graph->sinks_to_consume); |
| 570 | sink = current_node->data; |
| 571 | BT_LIB_LOGD("Chose next sink to consume: %!+c", sink); |
| 572 | status = consume_sink_node(graph, current_node); |
| 573 | |
| 574 | end: |
| 575 | return status; |
| 576 | } |
| 577 | |
| 578 | enum bt_graph_run_once_status bt_graph_run_once(struct bt_graph *graph) |
| 579 | { |
| 580 | enum bt_graph_run_once_status status; |
| 581 | |
| 582 | BT_ASSERT_PRE_NO_ERROR(); |
| 583 | BT_ASSERT_PRE_DEV_NON_NULL(graph, "Graph"); |
| 584 | BT_ASSERT_PRE_DEV(graph->can_consume, |
| 585 | "Cannot consume graph in its current state: %!+g", graph); |
| 586 | BT_ASSERT_PRE_DEV(graph->config_state != |
| 587 | BT_GRAPH_CONFIGURATION_STATE_FAULTY, |
| 588 | "Graph is in a faulty state: %!+g", graph); |
| 589 | bt_graph_set_can_consume(graph, false); |
| 590 | status = bt_graph_configure(graph); |
| 591 | if (G_UNLIKELY(status)) { |
| 592 | /* bt_graph_configure() logs errors */ |
| 593 | goto end; |
| 594 | } |
| 595 | |
| 596 | status = consume_no_check(graph); |
| 597 | bt_graph_set_can_consume(graph, true); |
| 598 | |
| 599 | end: |
| 600 | return status; |
| 601 | } |
| 602 | |
| 603 | enum bt_graph_run_status bt_graph_run(struct bt_graph *graph) |
| 604 | { |
| 605 | enum bt_graph_run_status status; |
| 606 | |
| 607 | BT_ASSERT_PRE_NO_ERROR(); |
| 608 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 609 | BT_ASSERT_PRE(graph->can_consume, |
| 610 | "Cannot consume graph in its current state: %!+g", graph); |
| 611 | BT_ASSERT_PRE(graph->config_state != BT_GRAPH_CONFIGURATION_STATE_FAULTY, |
| 612 | "Graph is in a faulty state: %!+g", graph); |
| 613 | bt_graph_set_can_consume(graph, false); |
| 614 | status = bt_graph_configure(graph); |
| 615 | if (G_UNLIKELY(status)) { |
| 616 | /* bt_graph_configure() logs errors */ |
| 617 | goto end; |
| 618 | } |
| 619 | |
| 620 | BT_LIB_LOGI("Running graph: %!+g", graph); |
| 621 | |
| 622 | do { |
| 623 | /* |
| 624 | * Check if the graph is interrupted at each iteration. |
| 625 | * If the graph was interrupted by another thread or by |
| 626 | * a signal handler, this is NOT a warning nor an error; |
| 627 | * it was intentional: log with an INFO level only. |
| 628 | */ |
| 629 | if (G_UNLIKELY(bt_graph_is_interrupted(graph))) { |
| 630 | BT_LIB_LOGI("Stopping the graph: " |
| 631 | "graph was interrupted: %!+g", graph); |
| 632 | status = BT_FUNC_STATUS_AGAIN; |
| 633 | goto end; |
| 634 | } |
| 635 | |
| 636 | status = consume_no_check(graph); |
| 637 | if (G_UNLIKELY(status == BT_FUNC_STATUS_AGAIN)) { |
| 638 | /* |
| 639 | * If AGAIN is received and there are multiple |
| 640 | * sinks, go ahead and consume from the next |
| 641 | * sink. |
| 642 | * |
| 643 | * However, in the case where a single sink is |
| 644 | * left, the caller can decide to busy-wait and |
| 645 | * call bt_graph_run() continuously |
| 646 | * until the source is ready or it can decide to |
| 647 | * sleep for an arbitrary amount of time. |
| 648 | */ |
| 649 | if (graph->sinks_to_consume->length > 1) { |
| 650 | status = BT_FUNC_STATUS_OK; |
| 651 | } |
| 652 | } |
| 653 | } while (status == BT_FUNC_STATUS_OK); |
| 654 | |
| 655 | if (status == BT_FUNC_STATUS_END) { |
| 656 | /* |
| 657 | * The last call to consume_no_check() returned |
| 658 | * `BT_FUNC_STATUS_END`, but bt_graph_run() has no |
| 659 | * `BT_GRAPH_RUN_STATUS_END` status: replace with |
| 660 | * `BT_GRAPH_RUN_STATUS_OK` (success: graph ran |
| 661 | * completely). |
| 662 | */ |
| 663 | status = BT_FUNC_STATUS_OK; |
| 664 | } |
| 665 | |
| 666 | end: |
| 667 | BT_LIB_LOGI("Graph ran: %![graph-]+g, status=%s", graph, |
| 668 | bt_common_func_status_string(status)); |
| 669 | bt_graph_set_can_consume(graph, true); |
| 670 | return status; |
| 671 | } |
| 672 | |
| 673 | enum bt_graph_add_listener_status |
| 674 | bt_graph_add_source_component_output_port_added_listener( |
| 675 | struct bt_graph *graph, |
| 676 | bt_graph_source_component_output_port_added_listener_func func, |
| 677 | void *data, bt_listener_id *out_listener_id) |
| 678 | { |
| 679 | struct bt_graph_listener_port_added listener = { |
| 680 | .func = (port_added_func_t) func, |
| 681 | .data = data, |
| 682 | }; |
| 683 | bt_listener_id listener_id; |
| 684 | |
| 685 | BT_ASSERT_PRE_NO_ERROR(); |
| 686 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 687 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); |
| 688 | g_array_append_val(graph->listeners.source_output_port_added, listener); |
| 689 | listener_id = graph->listeners.source_output_port_added->len - 1; |
| 690 | BT_LIB_LOGD("Added \"source component output port added\" listener to graph: " |
| 691 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
| 692 | listener_id); |
| 693 | |
| 694 | if (listener_id) { |
| 695 | *out_listener_id = listener_id; |
| 696 | } |
| 697 | |
| 698 | return BT_FUNC_STATUS_OK; |
| 699 | } |
| 700 | |
| 701 | enum bt_graph_add_listener_status |
| 702 | bt_graph_add_filter_component_output_port_added_listener( |
| 703 | struct bt_graph *graph, |
| 704 | bt_graph_filter_component_output_port_added_listener_func func, |
| 705 | void *data, bt_listener_id *out_listener_id) |
| 706 | { |
| 707 | struct bt_graph_listener_port_added listener = { |
| 708 | .func = (port_added_func_t) func, |
| 709 | .data = data, |
| 710 | }; |
| 711 | bt_listener_id listener_id; |
| 712 | |
| 713 | BT_ASSERT_PRE_NO_ERROR(); |
| 714 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 715 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); |
| 716 | g_array_append_val(graph->listeners.filter_output_port_added, listener); |
| 717 | listener_id = graph->listeners.filter_output_port_added->len - 1; |
| 718 | BT_LIB_LOGD("Added \"filter component output port added\" listener to graph: " |
| 719 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
| 720 | listener_id); |
| 721 | |
| 722 | if (listener_id) { |
| 723 | *out_listener_id = listener_id; |
| 724 | } |
| 725 | |
| 726 | return BT_FUNC_STATUS_OK; |
| 727 | } |
| 728 | |
| 729 | enum bt_graph_add_listener_status |
| 730 | bt_graph_add_filter_component_input_port_added_listener( |
| 731 | struct bt_graph *graph, |
| 732 | bt_graph_filter_component_input_port_added_listener_func func, |
| 733 | void *data, bt_listener_id *out_listener_id) |
| 734 | { |
| 735 | struct bt_graph_listener_port_added listener = { |
| 736 | .func = (port_added_func_t) func, |
| 737 | .data = data, |
| 738 | }; |
| 739 | bt_listener_id listener_id; |
| 740 | |
| 741 | BT_ASSERT_PRE_NO_ERROR(); |
| 742 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 743 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); |
| 744 | g_array_append_val(graph->listeners.filter_input_port_added, listener); |
| 745 | listener_id = graph->listeners.filter_input_port_added->len - 1; |
| 746 | BT_LIB_LOGD("Added \"filter component input port added\" listener to graph: " |
| 747 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
| 748 | listener_id); |
| 749 | |
| 750 | if (listener_id) { |
| 751 | *out_listener_id = listener_id; |
| 752 | } |
| 753 | |
| 754 | return BT_FUNC_STATUS_OK; |
| 755 | } |
| 756 | |
| 757 | enum bt_graph_add_listener_status |
| 758 | bt_graph_add_sink_component_input_port_added_listener( |
| 759 | struct bt_graph *graph, |
| 760 | bt_graph_sink_component_input_port_added_listener_func func, |
| 761 | void *data, bt_listener_id *out_listener_id) |
| 762 | { |
| 763 | struct bt_graph_listener_port_added listener = { |
| 764 | .func = (port_added_func_t) func, |
| 765 | .data = data, |
| 766 | }; |
| 767 | bt_listener_id listener_id; |
| 768 | |
| 769 | BT_ASSERT_PRE_NO_ERROR(); |
| 770 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 771 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); |
| 772 | g_array_append_val(graph->listeners.sink_input_port_added, listener); |
| 773 | listener_id = graph->listeners.sink_input_port_added->len - 1; |
| 774 | BT_LIB_LOGD("Added \"sink component input port added\" listener to graph: " |
| 775 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
| 776 | listener_id); |
| 777 | |
| 778 | if (listener_id) { |
| 779 | *out_listener_id = listener_id; |
| 780 | } |
| 781 | |
| 782 | return BT_FUNC_STATUS_OK; |
| 783 | } |
| 784 | |
| 785 | BT_HIDDEN |
| 786 | enum bt_graph_listener_func_status bt_graph_notify_port_added( |
| 787 | struct bt_graph *graph, struct bt_port *port) |
| 788 | { |
| 789 | uint64_t i; |
| 790 | GArray *listeners; |
| 791 | struct bt_component *comp; |
| 792 | enum bt_graph_listener_func_status status = BT_FUNC_STATUS_OK; |
| 793 | |
| 794 | BT_ASSERT(graph); |
| 795 | BT_ASSERT(port); |
| 796 | BT_LIB_LOGD("Notifying graph listeners that a port was added: " |
| 797 | "%![graph-]+g, %![port-]+p", graph, port); |
| 798 | comp = bt_port_borrow_component_inline(port); |
| 799 | BT_ASSERT(comp); |
| 800 | |
| 801 | switch (comp->class->type) { |
| 802 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 803 | { |
| 804 | switch (port->type) { |
| 805 | case BT_PORT_TYPE_OUTPUT: |
| 806 | listeners = graph->listeners.source_output_port_added; |
| 807 | break; |
| 808 | default: |
| 809 | bt_common_abort(); |
| 810 | } |
| 811 | |
| 812 | break; |
| 813 | } |
| 814 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 815 | { |
| 816 | switch (port->type) { |
| 817 | case BT_PORT_TYPE_INPUT: |
| 818 | listeners = graph->listeners.filter_input_port_added; |
| 819 | break; |
| 820 | case BT_PORT_TYPE_OUTPUT: |
| 821 | listeners = graph->listeners.filter_output_port_added; |
| 822 | break; |
| 823 | default: |
| 824 | bt_common_abort(); |
| 825 | } |
| 826 | |
| 827 | break; |
| 828 | } |
| 829 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 830 | { |
| 831 | switch (port->type) { |
| 832 | case BT_PORT_TYPE_INPUT: |
| 833 | listeners = graph->listeners.sink_input_port_added; |
| 834 | break; |
| 835 | default: |
| 836 | bt_common_abort(); |
| 837 | } |
| 838 | |
| 839 | break; |
| 840 | } |
| 841 | default: |
| 842 | bt_common_abort(); |
| 843 | } |
| 844 | |
| 845 | for (i = 0; i < listeners->len; i++) { |
| 846 | struct bt_graph_listener_port_added *listener = |
| 847 | &g_array_index(listeners, |
| 848 | struct bt_graph_listener_port_added, i); |
| 849 | |
| 850 | |
| 851 | BT_ASSERT(listener->func); |
| 852 | status = listener->func(comp, port, listener->data); |
| 853 | BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status); |
| 854 | if (status != BT_FUNC_STATUS_OK) { |
| 855 | goto end; |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | end: |
| 860 | return status; |
| 861 | } |
| 862 | |
| 863 | BT_HIDDEN |
| 864 | void bt_graph_remove_connection(struct bt_graph *graph, |
| 865 | struct bt_connection *connection) |
| 866 | { |
| 867 | BT_ASSERT(graph); |
| 868 | BT_ASSERT(connection); |
| 869 | BT_LIB_LOGD("Removing graph's connection: %![graph-]+g, %![conn-]+x", |
| 870 | graph, connection); |
| 871 | g_ptr_array_remove(graph->connections, connection); |
| 872 | } |
| 873 | |
| 874 | static inline |
| 875 | bool component_name_exists(struct bt_graph *graph, const char *name) |
| 876 | { |
| 877 | bool exists = false; |
| 878 | uint64_t i; |
| 879 | |
| 880 | for (i = 0; i < graph->components->len; i++) { |
| 881 | struct bt_component *other_comp = graph->components->pdata[i]; |
| 882 | |
| 883 | if (strcmp(name, bt_component_get_name(other_comp)) == 0) { |
| 884 | BT_ASSERT_PRE_MSG("Another component with the same name already exists in the graph: " |
| 885 | "%![other-comp-]+c, name=\"%s\"", |
| 886 | other_comp, name); |
| 887 | exists = true; |
| 888 | goto end; |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | end: |
| 893 | return exists; |
| 894 | } |
| 895 | |
| 896 | static |
| 897 | int add_component_with_init_method_data( |
| 898 | struct bt_graph *graph, |
| 899 | struct bt_component_class *comp_cls, |
| 900 | comp_init_method_t init_method, |
| 901 | const char *name, const struct bt_value *params, |
| 902 | void *init_method_data, bt_logging_level log_level, |
| 903 | const struct bt_component **user_component) |
| 904 | { |
| 905 | int status = BT_FUNC_STATUS_OK; |
| 906 | enum bt_component_class_initialize_method_status init_status; |
| 907 | struct bt_component *component = NULL; |
| 908 | int ret; |
| 909 | bool init_can_consume; |
| 910 | struct bt_value *new_params = NULL; |
| 911 | |
| 912 | BT_ASSERT(comp_cls); |
| 913 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 914 | BT_ASSERT_PRE_NON_NULL(name, "Name"); |
| 915 | BT_ASSERT_PRE( |
| 916 | graph->config_state == BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, |
| 917 | "Graph is not in the \"configuring\" state: %!+g", graph); |
| 918 | BT_ASSERT_PRE(!component_name_exists(graph, name), |
| 919 | "Duplicate component name: %!+g, name=\"%s\"", graph, name); |
| 920 | BT_ASSERT_PRE(!params || bt_value_is_map(params), |
| 921 | "Parameter value is not a map value: %!+v", params); |
| 922 | init_can_consume = graph->can_consume; |
| 923 | bt_graph_set_can_consume(graph, false); |
| 924 | BT_LIB_LOGI("Adding component to graph: " |
| 925 | "%![graph-]+g, %![cc-]+C, name=\"%s\", log-level=%s, " |
| 926 | "%![params-]+v, init-method-data-addr=%p", |
| 927 | graph, comp_cls, name, |
| 928 | bt_common_logging_level_string(log_level), params, |
| 929 | init_method_data); |
| 930 | |
| 931 | if (!params) { |
| 932 | new_params = bt_value_map_create(); |
| 933 | if (!new_params) { |
| 934 | BT_LIB_LOGE_APPEND_CAUSE( |
| 935 | "Cannot create empty map value object."); |
| 936 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 937 | goto end; |
| 938 | } |
| 939 | |
| 940 | params = new_params; |
| 941 | } |
| 942 | |
| 943 | ret = bt_component_create(comp_cls, name, log_level, &component); |
| 944 | if (ret) { |
| 945 | BT_LIB_LOGE_APPEND_CAUSE( |
| 946 | "Cannot create empty component object: ret=%d", |
| 947 | ret); |
| 948 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 949 | goto end; |
| 950 | } |
| 951 | |
| 952 | /* |
| 953 | * The user's initialization method needs to see that this |
| 954 | * component is part of the graph. If the user method fails, we |
| 955 | * immediately remove the component from the graph's components. |
| 956 | */ |
| 957 | g_ptr_array_add(graph->components, component); |
| 958 | bt_component_set_graph(component, graph); |
| 959 | bt_value_freeze(params); |
| 960 | |
| 961 | if (init_method) { |
| 962 | /* |
| 963 | * There is no use for config objects right now, so just pass |
| 964 | * NULL. |
| 965 | */ |
| 966 | BT_LOGD_STR("Calling user's initialization method."); |
| 967 | init_status = init_method(component, NULL, params, init_method_data); |
| 968 | BT_LOGD("User method returned: status=%s", |
| 969 | bt_common_func_status_string(init_status)); |
| 970 | BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(init_status); |
| 971 | if (init_status != BT_FUNC_STATUS_OK) { |
| 972 | if (init_status < 0) { |
| 973 | BT_LIB_LOGW_APPEND_CAUSE( |
| 974 | "Component initialization method failed: " |
| 975 | "status=%s, %![comp-]+c", |
| 976 | bt_common_func_status_string(init_status), |
| 977 | component); |
| 978 | } |
| 979 | |
| 980 | status = init_status; |
| 981 | bt_component_set_graph(component, NULL); |
| 982 | g_ptr_array_remove_fast(graph->components, component); |
| 983 | goto end; |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | /* |
| 988 | * Mark the component as initialized so that its finalization |
| 989 | * method is called when it is destroyed. |
| 990 | */ |
| 991 | component->initialized = true; |
| 992 | |
| 993 | /* |
| 994 | * If it's a sink component, it needs to be part of the graph's |
| 995 | * sink queue to be consumed by bt_graph_run() or |
| 996 | * bt_graph_run_once(). |
| 997 | */ |
| 998 | if (bt_component_is_sink(component)) { |
| 999 | graph->has_sink = true; |
| 1000 | g_queue_push_tail(graph->sinks_to_consume, component); |
| 1001 | } |
| 1002 | |
| 1003 | /* |
| 1004 | * Freeze the component class now that it's instantiated at |
| 1005 | * least once. |
| 1006 | */ |
| 1007 | BT_LOGD_STR("Freezing component class."); |
| 1008 | bt_component_class_freeze(comp_cls); |
| 1009 | BT_LIB_LOGI("Added component to graph: " |
| 1010 | "%![graph-]+g, %![cc-]+C, name=\"%s\", log-level=%s, " |
| 1011 | "%![params-]+v, init-method-data-addr=%p, %![comp-]+c", |
| 1012 | graph, comp_cls, name, |
| 1013 | bt_common_logging_level_string(log_level), params, |
| 1014 | init_method_data, component); |
| 1015 | |
| 1016 | if (user_component) { |
| 1017 | /* Move reference to user */ |
| 1018 | *user_component = component; |
| 1019 | } |
| 1020 | |
| 1021 | end: |
| 1022 | if (status != BT_FUNC_STATUS_OK) { |
| 1023 | bt_graph_make_faulty(graph); |
| 1024 | } |
| 1025 | |
| 1026 | bt_object_put_ref(component); |
| 1027 | bt_object_put_ref(new_params); |
| 1028 | (void) init_can_consume; |
| 1029 | bt_graph_set_can_consume(graph, init_can_consume); |
| 1030 | return status; |
| 1031 | } |
| 1032 | |
| 1033 | enum bt_graph_add_component_status |
| 1034 | bt_graph_add_source_component_with_initialize_method_data( |
| 1035 | struct bt_graph *graph, |
| 1036 | const struct bt_component_class_source *comp_cls, |
| 1037 | const char *name, const struct bt_value *params, |
| 1038 | void *init_method_data, bt_logging_level log_level, |
| 1039 | const struct bt_component_source **component) |
| 1040 | { |
| 1041 | BT_ASSERT_PRE_NO_ERROR(); |
| 1042 | BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class"); |
| 1043 | return add_component_with_init_method_data(graph, |
| 1044 | (void *) comp_cls, (comp_init_method_t) comp_cls->methods.init, |
| 1045 | name, params, init_method_data, log_level, (void *) component); |
| 1046 | } |
| 1047 | |
| 1048 | enum bt_graph_add_component_status bt_graph_add_source_component( |
| 1049 | struct bt_graph *graph, |
| 1050 | const struct bt_component_class_source *comp_cls, |
| 1051 | const char *name, const struct bt_value *params, |
| 1052 | enum bt_logging_level log_level, |
| 1053 | const struct bt_component_source **component) |
| 1054 | { |
| 1055 | BT_ASSERT_PRE_NO_ERROR(); |
| 1056 | return bt_graph_add_source_component_with_initialize_method_data( |
| 1057 | graph, comp_cls, name, params, NULL, log_level, component); |
| 1058 | } |
| 1059 | |
| 1060 | enum bt_graph_add_component_status |
| 1061 | bt_graph_add_filter_component_with_initialize_method_data( |
| 1062 | struct bt_graph *graph, |
| 1063 | const struct bt_component_class_filter *comp_cls, |
| 1064 | const char *name, const struct bt_value *params, |
| 1065 | void *init_method_data, enum bt_logging_level log_level, |
| 1066 | const struct bt_component_filter **component) |
| 1067 | { |
| 1068 | BT_ASSERT_PRE_NO_ERROR(); |
| 1069 | BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class"); |
| 1070 | return add_component_with_init_method_data(graph, |
| 1071 | (void *) comp_cls, (comp_init_method_t) comp_cls->methods.init, |
| 1072 | name, params, init_method_data, log_level, (void *) component); |
| 1073 | } |
| 1074 | |
| 1075 | enum bt_graph_add_component_status bt_graph_add_filter_component( |
| 1076 | struct bt_graph *graph, |
| 1077 | const struct bt_component_class_filter *comp_cls, |
| 1078 | const char *name, const struct bt_value *params, |
| 1079 | enum bt_logging_level log_level, |
| 1080 | const struct bt_component_filter **component) |
| 1081 | { |
| 1082 | BT_ASSERT_PRE_NO_ERROR(); |
| 1083 | return bt_graph_add_filter_component_with_initialize_method_data( |
| 1084 | graph, comp_cls, name, params, NULL, log_level, component); |
| 1085 | } |
| 1086 | |
| 1087 | enum bt_graph_add_component_status |
| 1088 | bt_graph_add_sink_component_with_initialize_method_data( |
| 1089 | struct bt_graph *graph, |
| 1090 | const struct bt_component_class_sink *comp_cls, |
| 1091 | const char *name, const struct bt_value *params, |
| 1092 | void *init_method_data, enum bt_logging_level log_level, |
| 1093 | const struct bt_component_sink **component) |
| 1094 | { |
| 1095 | BT_ASSERT_PRE_NO_ERROR(); |
| 1096 | BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class"); |
| 1097 | return add_component_with_init_method_data(graph, |
| 1098 | (void *) comp_cls, (comp_init_method_t) comp_cls->methods.init, |
| 1099 | name, params, init_method_data, log_level, (void *) component); |
| 1100 | } |
| 1101 | |
| 1102 | enum bt_graph_add_component_status bt_graph_add_sink_component( |
| 1103 | struct bt_graph *graph, |
| 1104 | const struct bt_component_class_sink *comp_cls, |
| 1105 | const char *name, const struct bt_value *params, |
| 1106 | enum bt_logging_level log_level, |
| 1107 | const struct bt_component_sink **component) |
| 1108 | { |
| 1109 | BT_ASSERT_PRE_NO_ERROR(); |
| 1110 | return bt_graph_add_sink_component_with_initialize_method_data( |
| 1111 | graph, comp_cls, name, params, NULL, log_level, component); |
| 1112 | } |
| 1113 | |
| 1114 | enum bt_graph_add_component_status |
| 1115 | bt_graph_add_simple_sink_component(struct bt_graph *graph, const char *name, |
| 1116 | bt_graph_simple_sink_component_initialize_func init_func, |
| 1117 | bt_graph_simple_sink_component_consume_func consume_func, |
| 1118 | bt_graph_simple_sink_component_finalize_func finalize_func, |
| 1119 | void *user_data, const bt_component_sink **component) |
| 1120 | { |
| 1121 | enum bt_graph_add_component_status status; |
| 1122 | struct bt_component_class_sink *comp_cls; |
| 1123 | struct simple_sink_init_method_data init_method_data = { |
| 1124 | .init_func = init_func, |
| 1125 | .consume_func = consume_func, |
| 1126 | .finalize_func = finalize_func, |
| 1127 | .user_data = user_data, |
| 1128 | }; |
| 1129 | |
| 1130 | BT_ASSERT_PRE_NO_ERROR(); |
| 1131 | |
| 1132 | /* |
| 1133 | * Other preconditions are checked by |
| 1134 | * bt_graph_add_sink_component_with_init_method_data(). |
| 1135 | */ |
| 1136 | BT_ASSERT_PRE_NON_NULL(consume_func, "Consume function"); |
| 1137 | |
| 1138 | comp_cls = bt_component_class_sink_simple_borrow(); |
| 1139 | if (!comp_cls) { |
| 1140 | BT_LIB_LOGE_APPEND_CAUSE( |
| 1141 | "Cannot borrow simple sink component class."); |
| 1142 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 1143 | goto end; |
| 1144 | } |
| 1145 | |
| 1146 | status = bt_graph_add_sink_component_with_initialize_method_data(graph, |
| 1147 | comp_cls, name, NULL, &init_method_data, |
| 1148 | BT_LOGGING_LEVEL_NONE, component); |
| 1149 | |
| 1150 | end: |
| 1151 | return status; |
| 1152 | } |
| 1153 | |
| 1154 | BT_HIDDEN |
| 1155 | void bt_graph_add_message(struct bt_graph *graph, |
| 1156 | struct bt_message *msg) |
| 1157 | { |
| 1158 | BT_ASSERT(graph); |
| 1159 | BT_ASSERT(msg); |
| 1160 | |
| 1161 | /* |
| 1162 | * It's okay not to take a reference because, when a |
| 1163 | * message's reference count drops to 0, either: |
| 1164 | * |
| 1165 | * * It is recycled back to one of this graph's pool. |
| 1166 | * * It is destroyed because it doesn't have any link to any |
| 1167 | * graph, which means the original graph is already destroyed. |
| 1168 | */ |
| 1169 | g_ptr_array_add(graph->messages, msg); |
| 1170 | } |
| 1171 | |
| 1172 | BT_HIDDEN |
| 1173 | bool bt_graph_is_interrupted(const struct bt_graph *graph) |
| 1174 | { |
| 1175 | BT_ASSERT_DBG(graph); |
| 1176 | return bt_interrupter_array_any_is_set(graph->interrupters); |
| 1177 | } |
| 1178 | |
| 1179 | enum bt_graph_add_interrupter_status bt_graph_add_interrupter( |
| 1180 | struct bt_graph *graph, const struct bt_interrupter *intr) |
| 1181 | { |
| 1182 | BT_ASSERT_PRE_NO_ERROR(); |
| 1183 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 1184 | BT_ASSERT_PRE_NON_NULL(intr, "Interrupter"); |
| 1185 | g_ptr_array_add(graph->interrupters, (void *) intr); |
| 1186 | bt_object_get_ref_no_null_check(intr); |
| 1187 | BT_LIB_LOGD("Added interrupter to graph: %![graph-]+g, %![intr-]+z", |
| 1188 | graph, intr); |
| 1189 | return BT_FUNC_STATUS_OK; |
| 1190 | } |
| 1191 | |
| 1192 | struct bt_interrupter *bt_graph_borrow_default_interrupter(bt_graph *graph) |
| 1193 | { |
| 1194 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 1195 | return graph->default_interrupter; |
| 1196 | } |
| 1197 | |
| 1198 | void bt_graph_get_ref(const struct bt_graph *graph) |
| 1199 | { |
| 1200 | bt_object_get_ref(graph); |
| 1201 | } |
| 1202 | |
| 1203 | void bt_graph_put_ref(const struct bt_graph *graph) |
| 1204 | { |
| 1205 | bt_object_put_ref(graph); |
| 1206 | } |