| 1 | /* |
| 2 | * graph.c |
| 3 | * |
| 4 | * Babeltrace Plugin Component Graph |
| 5 | * |
| 6 | * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 7 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | * of this software and associated documentation files (the "Software"), to deal |
| 11 | * in the Software without restriction, including without limitation the rights |
| 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | * copies of the Software, and to permit persons to whom the Software is |
| 14 | * furnished to do so, subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be included in |
| 17 | * all copies or substantial portions of the Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | * SOFTWARE. |
| 26 | */ |
| 27 | |
| 28 | #define BT_LOG_TAG "GRAPH" |
| 29 | #include <babeltrace/lib-logging-internal.h> |
| 30 | |
| 31 | #include <babeltrace/graph/component-internal.h> |
| 32 | #include <babeltrace/graph/graph-internal.h> |
| 33 | #include <babeltrace/graph/connection-internal.h> |
| 34 | #include <babeltrace/graph/component-sink-internal.h> |
| 35 | #include <babeltrace/graph/component-source.h> |
| 36 | #include <babeltrace/graph/component-filter.h> |
| 37 | #include <babeltrace/graph/port.h> |
| 38 | #include <babeltrace/compiler-internal.h> |
| 39 | #include <babeltrace/types.h> |
| 40 | #include <babeltrace/values.h> |
| 41 | #include <babeltrace/values-internal.h> |
| 42 | #include <unistd.h> |
| 43 | #include <glib.h> |
| 44 | |
| 45 | struct bt_graph_listener { |
| 46 | void *func; |
| 47 | bt_graph_listener_removed removed; |
| 48 | void *data; |
| 49 | }; |
| 50 | |
| 51 | static |
| 52 | int init_listeners_array(GArray **listeners) |
| 53 | { |
| 54 | int ret = 0; |
| 55 | |
| 56 | assert(listeners); |
| 57 | *listeners = g_array_new(FALSE, TRUE, sizeof(struct bt_graph_listener)); |
| 58 | if (!*listeners) { |
| 59 | BT_LOGE_STR("Failed to allocate one GArray."); |
| 60 | ret = -1; |
| 61 | goto end; |
| 62 | } |
| 63 | |
| 64 | end: |
| 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | static |
| 69 | void call_remove_listeners(GArray *listeners) |
| 70 | { |
| 71 | size_t i; |
| 72 | |
| 73 | for (i = 0; i < listeners->len; i++) { |
| 74 | struct bt_graph_listener listener = |
| 75 | g_array_index(listeners, struct bt_graph_listener, i); |
| 76 | |
| 77 | if (listener.removed) { |
| 78 | listener.removed(listener.data); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | static |
| 84 | void bt_graph_destroy(struct bt_object *obj) |
| 85 | { |
| 86 | struct bt_graph *graph = container_of(obj, |
| 87 | struct bt_graph, base); |
| 88 | |
| 89 | /* |
| 90 | * The graph's reference count is 0 if we're here. Increment |
| 91 | * it to avoid a double-destroy (possibly infinitely recursive) |
| 92 | * in this situation: |
| 93 | * |
| 94 | * 1. We put and destroy a connection. |
| 95 | * 2. This connection's destructor finalizes its active |
| 96 | * notification iterators. |
| 97 | * 3. A notification iterator's finalization function gets a |
| 98 | * new reference on its component (reference count goes from |
| 99 | * 0 to 1). |
| 100 | * 4. Since this component's reference count goes to 1, it takes |
| 101 | * a reference on its parent (this graph). This graph's |
| 102 | * reference count goes from 0 to 1. |
| 103 | * 5. The notification iterator's finalization function puts its |
| 104 | * component reference (reference count goes from 1 to 0). |
| 105 | * 6. Since this component's reference count goes from 1 to 0, |
| 106 | * it puts its parent (this graph). This graph's reference |
| 107 | * count goes from 1 to 0. |
| 108 | * 7. Since this graph's reference count goes from 1 to 0, |
| 109 | * its destructor is called (this function). |
| 110 | * |
| 111 | * With the incrementation below, the graph's reference count at |
| 112 | * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This |
| 113 | * ensures that this function is not called two times. |
| 114 | */ |
| 115 | BT_LOGD("Destroying graph: addr=%p", graph); |
| 116 | obj->ref_count.count++; |
| 117 | |
| 118 | /* |
| 119 | * Cancel the graph to disallow some operations, like creating |
| 120 | * notification iterators and adding ports to components. |
| 121 | */ |
| 122 | (void) bt_graph_cancel(graph); |
| 123 | |
| 124 | /* Call all remove listeners */ |
| 125 | call_remove_listeners(graph->listeners.port_added); |
| 126 | call_remove_listeners(graph->listeners.port_removed); |
| 127 | call_remove_listeners(graph->listeners.ports_connected); |
| 128 | call_remove_listeners(graph->listeners.ports_disconnected); |
| 129 | |
| 130 | if (graph->connections) { |
| 131 | BT_LOGD_STR("Destroying connections."); |
| 132 | g_ptr_array_free(graph->connections, TRUE); |
| 133 | } |
| 134 | if (graph->components) { |
| 135 | BT_LOGD_STR("Destroying components."); |
| 136 | g_ptr_array_free(graph->components, TRUE); |
| 137 | } |
| 138 | if (graph->sinks_to_consume) { |
| 139 | g_queue_free(graph->sinks_to_consume); |
| 140 | } |
| 141 | |
| 142 | if (graph->listeners.port_added) { |
| 143 | g_array_free(graph->listeners.port_added, TRUE); |
| 144 | } |
| 145 | |
| 146 | if (graph->listeners.port_removed) { |
| 147 | g_array_free(graph->listeners.port_removed, TRUE); |
| 148 | } |
| 149 | |
| 150 | if (graph->listeners.ports_connected) { |
| 151 | g_array_free(graph->listeners.ports_connected, TRUE); |
| 152 | } |
| 153 | |
| 154 | if (graph->listeners.ports_disconnected) { |
| 155 | g_array_free(graph->listeners.ports_disconnected, TRUE); |
| 156 | } |
| 157 | |
| 158 | g_free(graph); |
| 159 | } |
| 160 | |
| 161 | struct bt_graph *bt_graph_create(void) |
| 162 | { |
| 163 | struct bt_graph *graph; |
| 164 | int ret; |
| 165 | |
| 166 | BT_LOGD_STR("Creating graph object."); |
| 167 | graph = g_new0(struct bt_graph, 1); |
| 168 | if (!graph) { |
| 169 | BT_LOGE_STR("Failed to allocate one graph."); |
| 170 | goto end; |
| 171 | } |
| 172 | |
| 173 | bt_object_init(graph, bt_graph_destroy); |
| 174 | |
| 175 | graph->connections = g_ptr_array_new_with_free_func(bt_object_release); |
| 176 | if (!graph->connections) { |
| 177 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
| 178 | goto error; |
| 179 | } |
| 180 | graph->components = g_ptr_array_new_with_free_func(bt_object_release); |
| 181 | if (!graph->components) { |
| 182 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
| 183 | goto error; |
| 184 | } |
| 185 | graph->sinks_to_consume = g_queue_new(); |
| 186 | if (!graph->sinks_to_consume) { |
| 187 | BT_LOGE_STR("Failed to allocate one GQueue."); |
| 188 | goto error; |
| 189 | } |
| 190 | |
| 191 | graph->can_consume = BT_TRUE; |
| 192 | ret = init_listeners_array(&graph->listeners.port_added); |
| 193 | if (ret) { |
| 194 | BT_LOGE_STR("Cannot create the \"port added\" listener array."); |
| 195 | goto error; |
| 196 | } |
| 197 | |
| 198 | ret = init_listeners_array(&graph->listeners.port_removed); |
| 199 | if (ret) { |
| 200 | BT_LOGE_STR("Cannot create the \"port removed\" listener array."); |
| 201 | goto error; |
| 202 | } |
| 203 | |
| 204 | ret = init_listeners_array(&graph->listeners.ports_connected); |
| 205 | if (ret) { |
| 206 | BT_LOGE_STR("Cannot create the \"port connected\" listener array."); |
| 207 | goto error; |
| 208 | } |
| 209 | |
| 210 | ret = init_listeners_array(&graph->listeners.ports_disconnected); |
| 211 | if (ret) { |
| 212 | BT_LOGE_STR("Cannot create the \"port disconneted\" listener array."); |
| 213 | goto error; |
| 214 | } |
| 215 | |
| 216 | BT_LOGD("Created graph object: addr=%p", graph); |
| 217 | |
| 218 | end: |
| 219 | return graph; |
| 220 | error: |
| 221 | BT_PUT(graph); |
| 222 | goto end; |
| 223 | } |
| 224 | |
| 225 | enum bt_graph_status bt_graph_connect_ports(struct bt_graph *graph, |
| 226 | struct bt_port *upstream_port, struct bt_port *downstream_port, |
| 227 | struct bt_connection **user_connection) |
| 228 | { |
| 229 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
| 230 | struct bt_connection *connection = NULL; |
| 231 | struct bt_graph *upstream_graph = NULL; |
| 232 | struct bt_graph *downstream_graph = NULL; |
| 233 | struct bt_component *upstream_component = NULL; |
| 234 | struct bt_component *downstream_component = NULL; |
| 235 | enum bt_component_status component_status; |
| 236 | const bt_bool init_can_consume = graph->can_consume; |
| 237 | |
| 238 | if (!graph) { |
| 239 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 240 | status = BT_GRAPH_STATUS_INVALID; |
| 241 | goto end; |
| 242 | } |
| 243 | |
| 244 | if (!upstream_port) { |
| 245 | BT_LOGW_STR("Invalid parameter: upstream port is NULL."); |
| 246 | status = BT_GRAPH_STATUS_INVALID; |
| 247 | goto end; |
| 248 | } |
| 249 | |
| 250 | if (!downstream_port) { |
| 251 | BT_LOGW_STR("Invalid parameter: downstream port is NULL."); |
| 252 | status = BT_GRAPH_STATUS_INVALID; |
| 253 | goto end; |
| 254 | } |
| 255 | |
| 256 | BT_LOGD("Connecting component ports within graph: " |
| 257 | "graph-addr=%p, " |
| 258 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " |
| 259 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", |
| 260 | graph, upstream_port, bt_port_get_name(upstream_port), |
| 261 | downstream_port, bt_port_get_name(downstream_port)); |
| 262 | |
| 263 | if (graph->canceled) { |
| 264 | BT_LOGW_STR("Invalid parameter: graph is canceled."); |
| 265 | status = BT_GRAPH_STATUS_CANCELED; |
| 266 | goto end; |
| 267 | } |
| 268 | |
| 269 | graph->can_consume = BT_FALSE; |
| 270 | |
| 271 | /* Ensure appropriate types for upstream and downstream ports. */ |
| 272 | if (bt_port_get_type(upstream_port) != BT_PORT_TYPE_OUTPUT) { |
| 273 | BT_LOGW_STR("Invalid parameter: upstream port is not an output port."); |
| 274 | status = BT_GRAPH_STATUS_INVALID; |
| 275 | goto end; |
| 276 | } |
| 277 | if (bt_port_get_type(downstream_port) != BT_PORT_TYPE_INPUT) { |
| 278 | BT_LOGW_STR("Invalid parameter: downstream port is not an input port."); |
| 279 | status = BT_GRAPH_STATUS_INVALID; |
| 280 | goto end; |
| 281 | } |
| 282 | |
| 283 | /* Ensure that both ports are currently unconnected. */ |
| 284 | if (bt_port_is_connected(upstream_port)) { |
| 285 | BT_LOGW_STR("Invalid parameter: upstream port is already connected."); |
| 286 | status = BT_GRAPH_STATUS_INVALID; |
| 287 | goto end; |
| 288 | } |
| 289 | |
| 290 | if (bt_port_is_connected(downstream_port)) { |
| 291 | BT_LOGW_STR("Invalid parameter: downstream port is already connected."); |
| 292 | status = BT_GRAPH_STATUS_INVALID; |
| 293 | goto end; |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * Ensure that both ports are still attached to their creating |
| 298 | * component. |
| 299 | */ |
| 300 | upstream_component = bt_port_get_component(upstream_port); |
| 301 | if (!upstream_component) { |
| 302 | BT_LOGW_STR("Invalid parameter: upstream port is loose (does not belong to a component)"); |
| 303 | status = BT_GRAPH_STATUS_INVALID; |
| 304 | goto end; |
| 305 | } |
| 306 | |
| 307 | downstream_component = bt_port_get_component(downstream_port); |
| 308 | if (!downstream_component) { |
| 309 | BT_LOGW_STR("Invalid parameter: downstream port is loose (does not belong to a component)"); |
| 310 | status = BT_GRAPH_STATUS_INVALID; |
| 311 | goto end; |
| 312 | } |
| 313 | |
| 314 | BT_LOGD("Connecting component ports: " |
| 315 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " |
| 316 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\"", |
| 317 | upstream_component, bt_component_get_name(upstream_component), |
| 318 | downstream_component, bt_component_get_name(downstream_component)); |
| 319 | |
| 320 | /* |
| 321 | * At this point the ports are not connected yet. Both |
| 322 | * components need to accept an eventual connection to their |
| 323 | * port by the other port before we continue. |
| 324 | */ |
| 325 | BT_LOGD_STR("Asking upstream component to accept the connection."); |
| 326 | component_status = bt_component_accept_port_connection( |
| 327 | upstream_component, upstream_port, downstream_port); |
| 328 | if (component_status != BT_COMPONENT_STATUS_OK) { |
| 329 | if (component_status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) { |
| 330 | BT_LOGD_STR("Upstream component refused the connection."); |
| 331 | } else { |
| 332 | BT_LOGW("Cannot ask upstream component to accept the connection: " |
| 333 | "status=%s", bt_component_status_string(component_status)); |
| 334 | } |
| 335 | |
| 336 | status = bt_graph_status_from_component_status( |
| 337 | component_status); |
| 338 | goto end; |
| 339 | } |
| 340 | |
| 341 | BT_LOGD_STR("Asking downstream component to accept the connection."); |
| 342 | component_status = bt_component_accept_port_connection( |
| 343 | downstream_component, downstream_port, upstream_port); |
| 344 | if (component_status != BT_COMPONENT_STATUS_OK) { |
| 345 | if (component_status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) { |
| 346 | BT_LOGD_STR("Downstream component refused the connection."); |
| 347 | } else { |
| 348 | BT_LOGW("Cannot ask downstream component to accept the connection: " |
| 349 | "status=%s", bt_component_status_string(component_status)); |
| 350 | } |
| 351 | |
| 352 | status = bt_graph_status_from_component_status( |
| 353 | component_status); |
| 354 | goto end; |
| 355 | } |
| 356 | |
| 357 | BT_LOGD_STR("Creating connection."); |
| 358 | connection = bt_connection_create(graph, upstream_port, |
| 359 | downstream_port); |
| 360 | if (!connection) { |
| 361 | BT_LOGW("Cannot create connection object."); |
| 362 | status = BT_GRAPH_STATUS_NOMEM; |
| 363 | goto end; |
| 364 | } |
| 365 | |
| 366 | BT_LOGD("Connection object created: conn-addr=%p", connection); |
| 367 | |
| 368 | /* |
| 369 | * Ownership of upstream_component/downstream_component and of |
| 370 | * the connection object is transferred to the graph. |
| 371 | */ |
| 372 | g_ptr_array_add(graph->connections, connection); |
| 373 | |
| 374 | /* |
| 375 | * Notify both components that their port is connected. |
| 376 | */ |
| 377 | BT_LOGD_STR("Notifying upstream component that its port is connected."); |
| 378 | bt_component_port_connected(upstream_component, upstream_port, |
| 379 | downstream_port); |
| 380 | BT_LOGD_STR("Notifying downstream component that its port is connected."); |
| 381 | bt_component_port_connected(downstream_component, downstream_port, |
| 382 | upstream_port); |
| 383 | |
| 384 | /* |
| 385 | * Notify the graph's creator that both ports are connected. |
| 386 | */ |
| 387 | BT_LOGD_STR("Notifying graph's user that new component ports are connected."); |
| 388 | bt_graph_notify_ports_connected(graph, upstream_port, downstream_port); |
| 389 | BT_LOGD("Connected component ports within graph: " |
| 390 | "graph-addr=%p, " |
| 391 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " |
| 392 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\", " |
| 393 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " |
| 394 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", |
| 395 | graph, |
| 396 | upstream_component, bt_component_get_name(upstream_component), |
| 397 | downstream_component, bt_component_get_name(downstream_component), |
| 398 | upstream_port, bt_port_get_name(upstream_port), |
| 399 | downstream_port, bt_port_get_name(downstream_port)); |
| 400 | |
| 401 | if (user_connection) { |
| 402 | /* Move reference to user */ |
| 403 | *user_connection = connection; |
| 404 | connection = NULL; |
| 405 | } |
| 406 | |
| 407 | end: |
| 408 | bt_put(upstream_graph); |
| 409 | bt_put(downstream_graph); |
| 410 | bt_put(upstream_component); |
| 411 | bt_put(downstream_component); |
| 412 | bt_put(connection); |
| 413 | graph->can_consume = init_can_consume; |
| 414 | return status; |
| 415 | } |
| 416 | |
| 417 | static |
| 418 | enum bt_graph_status bt_graph_consume_no_check(struct bt_graph *graph) |
| 419 | { |
| 420 | struct bt_component *sink; |
| 421 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
| 422 | enum bt_component_status comp_status; |
| 423 | GList *current_node; |
| 424 | |
| 425 | BT_LOGV("Making next sink consume: addr=%p", graph); |
| 426 | |
| 427 | if (!graph->has_sink) { |
| 428 | BT_LOGW_STR("Graph has no sink component."); |
| 429 | status = BT_GRAPH_STATUS_NO_SINK; |
| 430 | goto end; |
| 431 | } |
| 432 | |
| 433 | if (g_queue_is_empty(graph->sinks_to_consume)) { |
| 434 | BT_LOGV_STR("Graph's sink queue is empty: end of graph."); |
| 435 | status = BT_GRAPH_STATUS_END; |
| 436 | goto end; |
| 437 | } |
| 438 | |
| 439 | current_node = g_queue_pop_head_link(graph->sinks_to_consume); |
| 440 | sink = current_node->data; |
| 441 | BT_LOGV("Chose next sink to consume: comp-addr=%p, comp-name=\"%s\"", |
| 442 | sink, bt_component_get_name(sink)); |
| 443 | comp_status = bt_component_sink_consume(sink); |
| 444 | BT_LOGV("Consumed from sink: status=%s", |
| 445 | bt_component_status_string(comp_status)); |
| 446 | switch (comp_status) { |
| 447 | case BT_COMPONENT_STATUS_OK: |
| 448 | break; |
| 449 | case BT_COMPONENT_STATUS_END: |
| 450 | status = BT_GRAPH_STATUS_END; |
| 451 | break; |
| 452 | case BT_COMPONENT_STATUS_AGAIN: |
| 453 | status = BT_GRAPH_STATUS_AGAIN; |
| 454 | break; |
| 455 | case BT_COMPONENT_STATUS_INVALID: |
| 456 | status = BT_GRAPH_STATUS_INVALID; |
| 457 | break; |
| 458 | default: |
| 459 | status = BT_GRAPH_STATUS_ERROR; |
| 460 | break; |
| 461 | } |
| 462 | |
| 463 | if (status != BT_GRAPH_STATUS_END) { |
| 464 | g_queue_push_tail_link(graph->sinks_to_consume, current_node); |
| 465 | goto end; |
| 466 | } |
| 467 | |
| 468 | /* End reached, the node is not added back to the queue and free'd. */ |
| 469 | g_queue_delete_link(graph->sinks_to_consume, current_node); |
| 470 | |
| 471 | /* Don't forward an END status if there are sinks left to consume. */ |
| 472 | if (!g_queue_is_empty(graph->sinks_to_consume)) { |
| 473 | status = BT_GRAPH_STATUS_OK; |
| 474 | goto end; |
| 475 | } |
| 476 | end: |
| 477 | BT_LOGV("Graph consumed: status=%s", bt_graph_status_string(status)); |
| 478 | return status; |
| 479 | } |
| 480 | |
| 481 | enum bt_graph_status bt_graph_consume(struct bt_graph *graph) |
| 482 | { |
| 483 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
| 484 | |
| 485 | if (!graph) { |
| 486 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 487 | status = BT_GRAPH_STATUS_INVALID; |
| 488 | goto end; |
| 489 | } |
| 490 | |
| 491 | if (graph->canceled) { |
| 492 | BT_LOGW("Invalid parameter: graph is canceled: " |
| 493 | "graph-addr=%p", graph); |
| 494 | status = BT_GRAPH_STATUS_CANCELED; |
| 495 | goto end; |
| 496 | } |
| 497 | |
| 498 | if (!graph->can_consume) { |
| 499 | BT_LOGW_STR("Cannot consume graph in its current state."); |
| 500 | status = BT_GRAPH_STATUS_CANNOT_CONSUME; |
| 501 | goto end; |
| 502 | } |
| 503 | |
| 504 | graph->can_consume = BT_FALSE; |
| 505 | status = bt_graph_consume_no_check(graph); |
| 506 | graph->can_consume = BT_TRUE; |
| 507 | |
| 508 | end: |
| 509 | return status; |
| 510 | } |
| 511 | |
| 512 | enum bt_graph_status bt_graph_run(struct bt_graph *graph) |
| 513 | { |
| 514 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
| 515 | |
| 516 | if (!graph) { |
| 517 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 518 | status = BT_GRAPH_STATUS_INVALID; |
| 519 | goto end; |
| 520 | } |
| 521 | |
| 522 | if (graph->canceled) { |
| 523 | BT_LOGW("Invalid parameter: graph is canceled: " |
| 524 | "graph-addr=%p", graph); |
| 525 | status = BT_GRAPH_STATUS_CANCELED; |
| 526 | goto end; |
| 527 | } |
| 528 | |
| 529 | if (!graph->can_consume) { |
| 530 | BT_LOGW_STR("Cannot run graph in its current state."); |
| 531 | status = BT_GRAPH_STATUS_CANNOT_CONSUME; |
| 532 | goto end; |
| 533 | } |
| 534 | |
| 535 | graph->can_consume = BT_FALSE; |
| 536 | BT_LOGV("Running graph: addr=%p", graph); |
| 537 | |
| 538 | do { |
| 539 | /* |
| 540 | * Check if the graph is canceled at each iteration. If |
| 541 | * the graph was canceled by another thread or by a |
| 542 | * signal, this is not a warning nor an error, it was |
| 543 | * intentional: log with a DEBUG level only. |
| 544 | */ |
| 545 | if (graph->canceled) { |
| 546 | BT_LOGD("Stopping the graph: graph is canceled: " |
| 547 | "graph-addr=%p", graph); |
| 548 | status = BT_GRAPH_STATUS_CANCELED; |
| 549 | goto end; |
| 550 | } |
| 551 | |
| 552 | status = bt_graph_consume_no_check(graph); |
| 553 | if (status == BT_GRAPH_STATUS_AGAIN) { |
| 554 | /* |
| 555 | * If AGAIN is received and there are multiple |
| 556 | * sinks, go ahead and consume from the next |
| 557 | * sink. |
| 558 | * |
| 559 | * However, in the case where a single sink is |
| 560 | * left, the caller can decide to busy-wait and |
| 561 | * call bt_graph_run() continuously until the |
| 562 | * source is ready or it can decide to sleep for |
| 563 | * an arbitrary amount of time. |
| 564 | */ |
| 565 | if (graph->sinks_to_consume->length > 1) { |
| 566 | status = BT_GRAPH_STATUS_OK; |
| 567 | } |
| 568 | } else if (status == BT_GRAPH_STATUS_NO_SINK) { |
| 569 | goto end; |
| 570 | } |
| 571 | } while (status == BT_GRAPH_STATUS_OK); |
| 572 | |
| 573 | if (g_queue_is_empty(graph->sinks_to_consume)) { |
| 574 | status = BT_GRAPH_STATUS_END; |
| 575 | } |
| 576 | |
| 577 | end: |
| 578 | BT_LOGV("Graph ran: status=%s", bt_graph_status_string(status)); |
| 579 | graph->can_consume = BT_TRUE; |
| 580 | return status; |
| 581 | } |
| 582 | |
| 583 | static |
| 584 | int add_listener(GArray *listeners, void *func, void *removed, void *data) |
| 585 | { |
| 586 | struct bt_graph_listener listener = { |
| 587 | .func = func, |
| 588 | .removed = removed, |
| 589 | .data = data, |
| 590 | }; |
| 591 | |
| 592 | g_array_append_val(listeners, listener); |
| 593 | return listeners->len - 1; |
| 594 | } |
| 595 | |
| 596 | int bt_graph_add_port_added_listener( |
| 597 | struct bt_graph *graph, |
| 598 | bt_graph_port_added_listener listener, |
| 599 | bt_graph_listener_removed listener_removed, void *data) |
| 600 | { |
| 601 | int ret; |
| 602 | |
| 603 | if (!graph) { |
| 604 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 605 | ret = -1; |
| 606 | goto end; |
| 607 | } |
| 608 | |
| 609 | if (graph->in_remove_listener) { |
| 610 | BT_LOGW("Cannot call this function during the execution of a remove listener: " |
| 611 | "addr=%p", graph); |
| 612 | ret = -1; |
| 613 | goto end; |
| 614 | } |
| 615 | |
| 616 | if (!listener) { |
| 617 | BT_LOGW_STR("Invalid parameter: listener is NULL."); |
| 618 | ret = -1; |
| 619 | goto end; |
| 620 | } |
| 621 | |
| 622 | ret = add_listener(graph->listeners.port_added, listener, |
| 623 | listener_removed, data); |
| 624 | BT_LOGV("Added \"port added\" listener to graph: " |
| 625 | "graph-addr=%p, listener-addr=%p, pos=%d", |
| 626 | graph, listener, ret); |
| 627 | |
| 628 | end: |
| 629 | return ret; |
| 630 | } |
| 631 | |
| 632 | int bt_graph_add_port_removed_listener( |
| 633 | struct bt_graph *graph, |
| 634 | bt_graph_port_removed_listener listener, |
| 635 | bt_graph_listener_removed listener_removed, void *data) |
| 636 | { |
| 637 | int ret; |
| 638 | |
| 639 | if (!graph) { |
| 640 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 641 | ret = -1; |
| 642 | goto end; |
| 643 | } |
| 644 | |
| 645 | if (graph->in_remove_listener) { |
| 646 | BT_LOGW("Cannot call this function during the execution of a remove listener: " |
| 647 | "addr=%p", graph); |
| 648 | ret = -1; |
| 649 | goto end; |
| 650 | } |
| 651 | |
| 652 | if (!listener) { |
| 653 | BT_LOGW_STR("Invalid parameter: listener is NULL."); |
| 654 | ret = -1; |
| 655 | goto end; |
| 656 | } |
| 657 | |
| 658 | ret = add_listener(graph->listeners.port_removed, listener, |
| 659 | listener_removed, data); |
| 660 | BT_LOGV("Added \"port removed\" listener to graph: " |
| 661 | "graph-addr=%p, listener-addr=%p, pos=%d", |
| 662 | graph, listener, ret); |
| 663 | |
| 664 | end: |
| 665 | return ret; |
| 666 | } |
| 667 | |
| 668 | int bt_graph_add_ports_connected_listener( |
| 669 | struct bt_graph *graph, |
| 670 | bt_graph_ports_connected_listener listener, |
| 671 | bt_graph_listener_removed listener_removed, void *data) |
| 672 | { |
| 673 | int ret; |
| 674 | |
| 675 | if (!graph) { |
| 676 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 677 | ret = -1; |
| 678 | goto end; |
| 679 | } |
| 680 | |
| 681 | if (graph->in_remove_listener) { |
| 682 | BT_LOGW("Cannot call this function during the execution of a remove listener: " |
| 683 | "addr=%p", graph); |
| 684 | ret = -1; |
| 685 | goto end; |
| 686 | } |
| 687 | |
| 688 | if (!listener) { |
| 689 | BT_LOGW_STR("Invalid parameter: listener is NULL."); |
| 690 | ret = -1; |
| 691 | goto end; |
| 692 | } |
| 693 | |
| 694 | ret = add_listener(graph->listeners.ports_connected, listener, |
| 695 | listener_removed, data); |
| 696 | BT_LOGV("Added \"port connected\" listener to graph: " |
| 697 | "graph-addr=%p, listener-addr=%p, pos=%d", |
| 698 | graph, listener, ret); |
| 699 | |
| 700 | end: |
| 701 | return ret; |
| 702 | } |
| 703 | |
| 704 | int bt_graph_add_ports_disconnected_listener( |
| 705 | struct bt_graph *graph, |
| 706 | bt_graph_ports_disconnected_listener listener, |
| 707 | bt_graph_listener_removed listener_removed, void *data) |
| 708 | { |
| 709 | int ret; |
| 710 | |
| 711 | if (!graph) { |
| 712 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 713 | ret = -1; |
| 714 | goto end; |
| 715 | } |
| 716 | |
| 717 | if (graph->in_remove_listener) { |
| 718 | BT_LOGW("Cannot call this function during the execution of a remove listener: " |
| 719 | "addr=%p", graph); |
| 720 | ret = -1; |
| 721 | goto end; |
| 722 | } |
| 723 | |
| 724 | if (!listener) { |
| 725 | BT_LOGW_STR("Invalid parameter: listener is NULL."); |
| 726 | ret = -1; |
| 727 | goto end; |
| 728 | } |
| 729 | |
| 730 | ret = add_listener(graph->listeners.ports_disconnected, listener, |
| 731 | listener_removed, data); |
| 732 | BT_LOGV("Added \"port disconnected\" listener to graph: " |
| 733 | "graph-addr=%p, listener-addr=%p, pos=%d", |
| 734 | graph, listener, ret); |
| 735 | |
| 736 | end: |
| 737 | return ret; |
| 738 | } |
| 739 | |
| 740 | BT_HIDDEN |
| 741 | void bt_graph_notify_port_added(struct bt_graph *graph, struct bt_port *port) |
| 742 | { |
| 743 | size_t i; |
| 744 | |
| 745 | BT_LOGV("Notifying graph listeners that a port was added: " |
| 746 | "graph-addr=%p, port-addr=%p, port-name=\"%s\"", |
| 747 | graph, port, bt_port_get_name(port)); |
| 748 | |
| 749 | for (i = 0; i < graph->listeners.port_added->len; i++) { |
| 750 | struct bt_graph_listener listener = |
| 751 | g_array_index(graph->listeners.port_added, |
| 752 | struct bt_graph_listener, i); |
| 753 | bt_graph_port_added_listener func = listener.func; |
| 754 | |
| 755 | assert(func); |
| 756 | func(port, listener.data); |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | BT_HIDDEN |
| 761 | void bt_graph_notify_port_removed(struct bt_graph *graph, |
| 762 | struct bt_component *comp, struct bt_port *port) |
| 763 | { |
| 764 | size_t i; |
| 765 | |
| 766 | BT_LOGV("Notifying graph listeners that a port was removed: " |
| 767 | "graph-addr=%p, port-addr=%p, port-name=\"%s\"", |
| 768 | graph, port, bt_port_get_name(port)); |
| 769 | |
| 770 | for (i = 0; i < graph->listeners.port_removed->len; i++) { |
| 771 | struct bt_graph_listener listener = |
| 772 | g_array_index(graph->listeners.port_removed, |
| 773 | struct bt_graph_listener, i); |
| 774 | bt_graph_port_removed_listener func = listener.func; |
| 775 | |
| 776 | assert(func); |
| 777 | func(comp, port, listener.data); |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | BT_HIDDEN |
| 782 | void bt_graph_notify_ports_connected(struct bt_graph *graph, |
| 783 | struct bt_port *upstream_port, struct bt_port *downstream_port) |
| 784 | { |
| 785 | size_t i; |
| 786 | |
| 787 | BT_LOGV("Notifying graph listeners that two ports were connected: " |
| 788 | "graph-addr=%p, " |
| 789 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " |
| 790 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", |
| 791 | graph, upstream_port, bt_port_get_name(upstream_port), |
| 792 | downstream_port, bt_port_get_name(downstream_port)); |
| 793 | |
| 794 | for (i = 0; i < graph->listeners.ports_connected->len; i++) { |
| 795 | struct bt_graph_listener listener = |
| 796 | g_array_index(graph->listeners.ports_connected, |
| 797 | struct bt_graph_listener, i); |
| 798 | bt_graph_ports_connected_listener func = listener.func; |
| 799 | |
| 800 | assert(func); |
| 801 | func(upstream_port, downstream_port, listener.data); |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | BT_HIDDEN |
| 806 | void bt_graph_notify_ports_disconnected(struct bt_graph *graph, |
| 807 | struct bt_component *upstream_comp, |
| 808 | struct bt_component *downstream_comp, |
| 809 | struct bt_port *upstream_port, struct bt_port *downstream_port) |
| 810 | { |
| 811 | size_t i; |
| 812 | |
| 813 | BT_LOGV("Notifying graph listeners that two ports were disconnected: " |
| 814 | "graph-addr=%p, " |
| 815 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " |
| 816 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", |
| 817 | graph, upstream_port, bt_port_get_name(upstream_port), |
| 818 | downstream_port, bt_port_get_name(downstream_port)); |
| 819 | |
| 820 | for (i = 0; i < graph->listeners.ports_disconnected->len; i++) { |
| 821 | struct bt_graph_listener listener = |
| 822 | g_array_index(graph->listeners.ports_disconnected, |
| 823 | struct bt_graph_listener, i); |
| 824 | bt_graph_ports_disconnected_listener func = listener.func; |
| 825 | |
| 826 | assert(func); |
| 827 | func(upstream_comp, downstream_comp, upstream_port, |
| 828 | downstream_port, listener.data); |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | enum bt_graph_status bt_graph_cancel(struct bt_graph *graph) |
| 833 | { |
| 834 | enum bt_graph_status ret = BT_GRAPH_STATUS_OK; |
| 835 | |
| 836 | if (!graph) { |
| 837 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 838 | ret = BT_GRAPH_STATUS_INVALID; |
| 839 | goto end; |
| 840 | } |
| 841 | |
| 842 | graph->canceled = BT_TRUE; |
| 843 | BT_LOGV("Canceled graph: addr=%p", graph); |
| 844 | |
| 845 | end: |
| 846 | return ret; |
| 847 | } |
| 848 | |
| 849 | bt_bool bt_graph_is_canceled(struct bt_graph *graph) |
| 850 | { |
| 851 | bt_bool canceled = BT_FALSE; |
| 852 | |
| 853 | if (!graph) { |
| 854 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 855 | goto end; |
| 856 | } |
| 857 | |
| 858 | canceled = graph->canceled; |
| 859 | |
| 860 | end: |
| 861 | return canceled; |
| 862 | } |
| 863 | |
| 864 | BT_HIDDEN |
| 865 | void bt_graph_remove_connection(struct bt_graph *graph, |
| 866 | struct bt_connection *connection) |
| 867 | { |
| 868 | assert(graph); |
| 869 | assert(connection); |
| 870 | BT_LOGV("Removing graph's connection: graph-addr=%p, conn-addr=%p", |
| 871 | graph, connection); |
| 872 | g_ptr_array_remove(graph->connections, connection); |
| 873 | } |
| 874 | |
| 875 | enum bt_graph_status bt_graph_add_component_with_init_method_data( |
| 876 | struct bt_graph *graph, |
| 877 | struct bt_component_class *component_class, |
| 878 | const char *name, struct bt_value *params, |
| 879 | void *init_method_data, |
| 880 | struct bt_component **user_component) |
| 881 | { |
| 882 | enum bt_graph_status graph_status = BT_GRAPH_STATUS_OK; |
| 883 | enum bt_component_status comp_status; |
| 884 | struct bt_component *component = NULL; |
| 885 | enum bt_component_class_type type; |
| 886 | size_t i; |
| 887 | const bt_bool init_can_consume = graph->can_consume; |
| 888 | |
| 889 | bt_get(params); |
| 890 | |
| 891 | if (!graph) { |
| 892 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 893 | graph_status = BT_GRAPH_STATUS_INVALID; |
| 894 | goto end; |
| 895 | } |
| 896 | |
| 897 | if (!component_class) { |
| 898 | BT_LOGW_STR("Invalid parameter: component class is NULL."); |
| 899 | graph_status = BT_GRAPH_STATUS_INVALID; |
| 900 | goto end; |
| 901 | } |
| 902 | |
| 903 | graph->can_consume = BT_FALSE; |
| 904 | type = bt_component_class_get_type(component_class); |
| 905 | BT_LOGD("Adding component to graph: " |
| 906 | "graph-addr=%p, comp-cls-addr=%p, " |
| 907 | "comp-cls-type=%s, name=\"%s\", params-addr=%p, " |
| 908 | "init-method-data-addr=%p", |
| 909 | graph, component_class, bt_component_class_type_string(type), |
| 910 | name, params, init_method_data); |
| 911 | |
| 912 | if (!name) { |
| 913 | BT_LOGW_STR("Invalid parameter: name is NULL."); |
| 914 | graph_status = BT_GRAPH_STATUS_INVALID; |
| 915 | goto end; |
| 916 | } |
| 917 | |
| 918 | if (graph->canceled) { |
| 919 | BT_LOGW_STR("Invalid parameter: graph is canceled."); |
| 920 | graph_status = BT_GRAPH_STATUS_CANCELED; |
| 921 | goto end; |
| 922 | } |
| 923 | |
| 924 | if (type != BT_COMPONENT_CLASS_TYPE_SOURCE && |
| 925 | type != BT_COMPONENT_CLASS_TYPE_FILTER && |
| 926 | type != BT_COMPONENT_CLASS_TYPE_SINK) { |
| 927 | BT_LOGW("Invalid parameter: unknown component class type: " |
| 928 | "type=%d", type); |
| 929 | graph_status = BT_GRAPH_STATUS_INVALID; |
| 930 | goto end; |
| 931 | } |
| 932 | |
| 933 | for (i = 0; i < graph->components->len; i++) { |
| 934 | void *other_comp = graph->components->pdata[i]; |
| 935 | |
| 936 | if (strcmp(name, bt_component_get_name(other_comp)) == 0) { |
| 937 | BT_LOGW("Invalid parameter: another component with the same name already exists in the graph: " |
| 938 | "other-comp-addr=%p, name=\"%s\"", |
| 939 | other_comp, name); |
| 940 | graph_status = BT_GRAPH_STATUS_INVALID; |
| 941 | goto end; |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | /* |
| 946 | * Parameters must be a map value, but we create a convenient |
| 947 | * empty one if it's NULL. |
| 948 | */ |
| 949 | if (params) { |
| 950 | if (!bt_value_is_map(params)) { |
| 951 | BT_LOGW("Invalid parameter: initialization parameters must be a map value: " |
| 952 | "type=%s", |
| 953 | bt_value_type_string(bt_value_get_type(params))); |
| 954 | graph_status = BT_GRAPH_STATUS_INVALID; |
| 955 | goto end; |
| 956 | } |
| 957 | } else { |
| 958 | params = bt_value_map_create(); |
| 959 | if (!params) { |
| 960 | BT_LOGE_STR("Cannot create map value object."); |
| 961 | graph_status = BT_GRAPH_STATUS_NOMEM; |
| 962 | goto end; |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | comp_status = bt_component_create(component_class, name, &component); |
| 967 | if (comp_status != BT_COMPONENT_STATUS_OK) { |
| 968 | BT_LOGE("Cannot create empty component object: status=%s", |
| 969 | bt_component_status_string(comp_status)); |
| 970 | graph_status = bt_graph_status_from_component_status( |
| 971 | comp_status); |
| 972 | goto end; |
| 973 | } |
| 974 | |
| 975 | /* |
| 976 | * The user's initialization method needs to see that this |
| 977 | * component is part of the graph. If the user method fails, we |
| 978 | * immediately remove the component from the graph's components. |
| 979 | */ |
| 980 | g_ptr_array_add(graph->components, component); |
| 981 | bt_component_set_graph(component, graph); |
| 982 | |
| 983 | if (component_class->methods.init) { |
| 984 | BT_LOGD_STR("Calling user's initialization method."); |
| 985 | comp_status = component_class->methods.init( |
| 986 | bt_private_component_from_component(component), params, |
| 987 | init_method_data); |
| 988 | BT_LOGD("User method returned: status=%s", |
| 989 | bt_component_status_string(comp_status)); |
| 990 | if (comp_status != BT_COMPONENT_STATUS_OK) { |
| 991 | BT_LOGW_STR("Initialization method failed."); |
| 992 | graph_status = bt_graph_status_from_component_status( |
| 993 | comp_status); |
| 994 | bt_component_set_graph(component, NULL); |
| 995 | g_ptr_array_remove_fast(graph->components, component); |
| 996 | goto end; |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | /* |
| 1001 | * Mark the component as initialized so that its finalization |
| 1002 | * method is called when it is destroyed. |
| 1003 | */ |
| 1004 | component->initialized = true; |
| 1005 | |
| 1006 | /* |
| 1007 | * If it's a sink component, it needs to be part of the graph's |
| 1008 | * sink queue to be consumed by bt_graph_consume(). |
| 1009 | */ |
| 1010 | if (bt_component_is_sink(component)) { |
| 1011 | graph->has_sink = BT_TRUE; |
| 1012 | g_queue_push_tail(graph->sinks_to_consume, component); |
| 1013 | } |
| 1014 | |
| 1015 | /* |
| 1016 | * Freeze the component class now that it's instantiated at |
| 1017 | * least once. |
| 1018 | */ |
| 1019 | BT_LOGD_STR("Freezing component class."); |
| 1020 | bt_component_class_freeze(component->class); |
| 1021 | BT_LOGD("Added component to graph: " |
| 1022 | "graph-addr=%p, comp-cls-addr=%p, " |
| 1023 | "comp-cls-type=%s, name=\"%s\", params-addr=%p, " |
| 1024 | "init-method-data-addr=%p, comp-addr=%p", |
| 1025 | graph, component_class, bt_component_class_type_string(type), |
| 1026 | name, params, init_method_data, component); |
| 1027 | |
| 1028 | if (user_component) { |
| 1029 | /* Move reference to user */ |
| 1030 | *user_component = component; |
| 1031 | component = NULL; |
| 1032 | } |
| 1033 | |
| 1034 | end: |
| 1035 | bt_put(component); |
| 1036 | bt_put(params); |
| 1037 | graph->can_consume = init_can_consume; |
| 1038 | return graph_status; |
| 1039 | } |
| 1040 | |
| 1041 | enum bt_graph_status bt_graph_add_component( |
| 1042 | struct bt_graph *graph, |
| 1043 | struct bt_component_class *component_class, |
| 1044 | const char *name, struct bt_value *params, |
| 1045 | struct bt_component **component) |
| 1046 | { |
| 1047 | return bt_graph_add_component_with_init_method_data(graph, |
| 1048 | component_class, name, params, NULL, component); |
| 1049 | } |