| 1 | /* |
| 2 | * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * Copyright 2017 Philippe Proulx <pproulx@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 "GRAPH" |
| 25 | #include <babeltrace/lib-logging-internal.h> |
| 26 | |
| 27 | #include <babeltrace/graph/component-internal.h> |
| 28 | #include <babeltrace/graph/private-graph.h> |
| 29 | #include <babeltrace/graph/graph-internal.h> |
| 30 | #include <babeltrace/graph/connection-internal.h> |
| 31 | #include <babeltrace/graph/component-sink-internal.h> |
| 32 | #include <babeltrace/graph/component-source.h> |
| 33 | #include <babeltrace/graph/component-filter.h> |
| 34 | #include <babeltrace/graph/port.h> |
| 35 | #include <babeltrace/graph/notification-internal.h> |
| 36 | #include <babeltrace/graph/notification-event-internal.h> |
| 37 | #include <babeltrace/graph/notification-packet-internal.h> |
| 38 | #include <babeltrace/compiler-internal.h> |
| 39 | #include <babeltrace/common-internal.h> |
| 40 | #include <babeltrace/types.h> |
| 41 | #include <babeltrace/values.h> |
| 42 | #include <babeltrace/private-values.h> |
| 43 | #include <babeltrace/values-internal.h> |
| 44 | #include <babeltrace/assert-internal.h> |
| 45 | #include <babeltrace/assert-pre-internal.h> |
| 46 | #include <unistd.h> |
| 47 | #include <glib.h> |
| 48 | |
| 49 | struct bt_graph_listener { |
| 50 | void *func; |
| 51 | bt_private_graph_listener_removed removed; |
| 52 | void *data; |
| 53 | }; |
| 54 | |
| 55 | static |
| 56 | int init_listeners_array(GArray **listeners) |
| 57 | { |
| 58 | int ret = 0; |
| 59 | |
| 60 | BT_ASSERT(listeners); |
| 61 | *listeners = g_array_new(FALSE, TRUE, sizeof(struct bt_graph_listener)); |
| 62 | if (!*listeners) { |
| 63 | BT_LOGE_STR("Failed to allocate one GArray."); |
| 64 | ret = -1; |
| 65 | goto end; |
| 66 | } |
| 67 | |
| 68 | end: |
| 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | static |
| 73 | void call_remove_listeners(GArray *listeners) |
| 74 | { |
| 75 | size_t i; |
| 76 | |
| 77 | for (i = 0; i < listeners->len; i++) { |
| 78 | struct bt_graph_listener listener = |
| 79 | g_array_index(listeners, struct bt_graph_listener, i); |
| 80 | |
| 81 | if (listener.removed) { |
| 82 | listener.removed(listener.data); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | static |
| 88 | void bt_graph_destroy(struct bt_object *obj) |
| 89 | { |
| 90 | struct bt_graph *graph = container_of(obj, |
| 91 | struct bt_graph, base); |
| 92 | |
| 93 | /* |
| 94 | * The graph's reference count is 0 if we're here. Increment |
| 95 | * it to avoid a double-destroy (possibly infinitely recursive) |
| 96 | * in this situation: |
| 97 | * |
| 98 | * 1. We put and destroy a connection. |
| 99 | * 2. This connection's destructor finalizes its active |
| 100 | * notification iterators. |
| 101 | * 3. A notification iterator's finalization function gets a |
| 102 | * new reference on its component (reference count goes from |
| 103 | * 0 to 1). |
| 104 | * 4. Since this component's reference count goes to 1, it takes |
| 105 | * a reference on its parent (this graph). This graph's |
| 106 | * reference count goes from 0 to 1. |
| 107 | * 5. The notification iterator's finalization function puts its |
| 108 | * component reference (reference count goes from 1 to 0). |
| 109 | * 6. Since this component's reference count goes from 1 to 0, |
| 110 | * it puts its parent (this graph). This graph's reference |
| 111 | * count goes from 1 to 0. |
| 112 | * 7. Since this graph's reference count goes from 1 to 0, |
| 113 | * its destructor is called (this function). |
| 114 | * |
| 115 | * With the incrementation below, the graph's reference count at |
| 116 | * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This |
| 117 | * ensures that this function is not called two times. |
| 118 | */ |
| 119 | BT_LOGD("Destroying graph: addr=%p", graph); |
| 120 | obj->ref_count++; |
| 121 | |
| 122 | /* |
| 123 | * Cancel the graph to disallow some operations, like creating |
| 124 | * notification iterators and adding ports to components. |
| 125 | */ |
| 126 | (void) bt_private_graph_cancel((void *) graph); |
| 127 | |
| 128 | /* Call all remove listeners */ |
| 129 | call_remove_listeners(graph->listeners.port_added); |
| 130 | call_remove_listeners(graph->listeners.port_removed); |
| 131 | call_remove_listeners(graph->listeners.ports_connected); |
| 132 | call_remove_listeners(graph->listeners.ports_disconnected); |
| 133 | |
| 134 | if (graph->notifications) { |
| 135 | g_ptr_array_free(graph->notifications, TRUE); |
| 136 | } |
| 137 | |
| 138 | if (graph->connections) { |
| 139 | BT_LOGD_STR("Destroying connections."); |
| 140 | g_ptr_array_free(graph->connections, TRUE); |
| 141 | } |
| 142 | |
| 143 | if (graph->components) { |
| 144 | BT_LOGD_STR("Destroying components."); |
| 145 | g_ptr_array_free(graph->components, TRUE); |
| 146 | } |
| 147 | |
| 148 | if (graph->sinks_to_consume) { |
| 149 | g_queue_free(graph->sinks_to_consume); |
| 150 | } |
| 151 | |
| 152 | if (graph->listeners.port_added) { |
| 153 | g_array_free(graph->listeners.port_added, TRUE); |
| 154 | } |
| 155 | |
| 156 | if (graph->listeners.port_removed) { |
| 157 | g_array_free(graph->listeners.port_removed, TRUE); |
| 158 | } |
| 159 | |
| 160 | if (graph->listeners.ports_connected) { |
| 161 | g_array_free(graph->listeners.ports_connected, TRUE); |
| 162 | } |
| 163 | |
| 164 | if (graph->listeners.ports_disconnected) { |
| 165 | g_array_free(graph->listeners.ports_disconnected, TRUE); |
| 166 | } |
| 167 | |
| 168 | bt_object_pool_finalize(&graph->event_notif_pool); |
| 169 | bt_object_pool_finalize(&graph->packet_begin_notif_pool); |
| 170 | bt_object_pool_finalize(&graph->packet_end_notif_pool); |
| 171 | g_free(graph); |
| 172 | } |
| 173 | |
| 174 | static |
| 175 | void destroy_notification_event(struct bt_notification *notif, |
| 176 | struct bt_graph *graph) |
| 177 | { |
| 178 | bt_notification_event_destroy(notif); |
| 179 | } |
| 180 | |
| 181 | static |
| 182 | void destroy_notification_packet_begin(struct bt_notification *notif, |
| 183 | struct bt_graph *graph) |
| 184 | { |
| 185 | bt_notification_packet_begin_destroy(notif); |
| 186 | } |
| 187 | |
| 188 | static |
| 189 | void destroy_notification_packet_end(struct bt_notification *notif, |
| 190 | struct bt_graph *graph) |
| 191 | { |
| 192 | bt_notification_packet_end_destroy(notif); |
| 193 | } |
| 194 | |
| 195 | static |
| 196 | void notify_notification_graph_is_destroyed(struct bt_notification *notif) |
| 197 | { |
| 198 | bt_notification_unlink_graph(notif); |
| 199 | } |
| 200 | |
| 201 | struct bt_private_graph *bt_private_graph_create(void) |
| 202 | { |
| 203 | struct bt_graph *graph; |
| 204 | int ret; |
| 205 | |
| 206 | BT_LOGD_STR("Creating graph object."); |
| 207 | graph = g_new0(struct bt_graph, 1); |
| 208 | if (!graph) { |
| 209 | BT_LOGE_STR("Failed to allocate one graph."); |
| 210 | goto end; |
| 211 | } |
| 212 | |
| 213 | bt_object_init_shared(&graph->base, bt_graph_destroy); |
| 214 | graph->connections = g_ptr_array_new_with_free_func( |
| 215 | (GDestroyNotify) bt_object_try_spec_release); |
| 216 | if (!graph->connections) { |
| 217 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
| 218 | goto error; |
| 219 | } |
| 220 | graph->components = g_ptr_array_new_with_free_func( |
| 221 | (GDestroyNotify) bt_object_try_spec_release); |
| 222 | if (!graph->components) { |
| 223 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
| 224 | goto error; |
| 225 | } |
| 226 | graph->sinks_to_consume = g_queue_new(); |
| 227 | if (!graph->sinks_to_consume) { |
| 228 | BT_LOGE_STR("Failed to allocate one GQueue."); |
| 229 | goto error; |
| 230 | } |
| 231 | |
| 232 | bt_graph_set_can_consume(graph, BT_TRUE); |
| 233 | ret = init_listeners_array(&graph->listeners.port_added); |
| 234 | if (ret) { |
| 235 | BT_LOGE_STR("Cannot create the \"port added\" listener array."); |
| 236 | goto error; |
| 237 | } |
| 238 | |
| 239 | ret = init_listeners_array(&graph->listeners.port_removed); |
| 240 | if (ret) { |
| 241 | BT_LOGE_STR("Cannot create the \"port removed\" listener array."); |
| 242 | goto error; |
| 243 | } |
| 244 | |
| 245 | ret = init_listeners_array(&graph->listeners.ports_connected); |
| 246 | if (ret) { |
| 247 | BT_LOGE_STR("Cannot create the \"port connected\" listener array."); |
| 248 | goto error; |
| 249 | } |
| 250 | |
| 251 | ret = init_listeners_array(&graph->listeners.ports_disconnected); |
| 252 | if (ret) { |
| 253 | BT_LOGE_STR("Cannot create the \"port disconneted\" listener array."); |
| 254 | goto error; |
| 255 | } |
| 256 | |
| 257 | ret = bt_object_pool_initialize(&graph->event_notif_pool, |
| 258 | (bt_object_pool_new_object_func) bt_notification_event_new, |
| 259 | (bt_object_pool_destroy_object_func) destroy_notification_event, |
| 260 | graph); |
| 261 | if (ret) { |
| 262 | BT_LOGE("Failed to initialize event notification pool: ret=%d", |
| 263 | ret); |
| 264 | goto error; |
| 265 | } |
| 266 | |
| 267 | ret = bt_object_pool_initialize(&graph->packet_begin_notif_pool, |
| 268 | (bt_object_pool_new_object_func) bt_notification_packet_begin_new, |
| 269 | (bt_object_pool_destroy_object_func) destroy_notification_packet_begin, |
| 270 | graph); |
| 271 | if (ret) { |
| 272 | BT_LOGE("Failed to initialize packet beginning notification pool: ret=%d", |
| 273 | ret); |
| 274 | goto error; |
| 275 | } |
| 276 | |
| 277 | ret = bt_object_pool_initialize(&graph->packet_end_notif_pool, |
| 278 | (bt_object_pool_new_object_func) bt_notification_packet_end_new, |
| 279 | (bt_object_pool_destroy_object_func) destroy_notification_packet_end, |
| 280 | graph); |
| 281 | if (ret) { |
| 282 | BT_LOGE("Failed to initialize packet end notification pool: ret=%d", |
| 283 | ret); |
| 284 | goto error; |
| 285 | } |
| 286 | |
| 287 | graph->notifications = g_ptr_array_new_with_free_func( |
| 288 | (GDestroyNotify) notify_notification_graph_is_destroyed); |
| 289 | BT_LOGD("Created graph object: addr=%p", graph); |
| 290 | |
| 291 | end: |
| 292 | return (void *) graph; |
| 293 | error: |
| 294 | BT_OBJECT_PUT_REF_AND_RESET(graph); |
| 295 | goto end; |
| 296 | } |
| 297 | |
| 298 | enum bt_graph_status bt_private_graph_connect_ports( |
| 299 | struct bt_private_graph *priv_graph, |
| 300 | struct bt_port *upstream_port, struct bt_port *downstream_port, |
| 301 | struct bt_connection **user_connection) |
| 302 | { |
| 303 | struct bt_graph *graph = (void *) priv_graph; |
| 304 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
| 305 | struct bt_connection *connection = NULL; |
| 306 | struct bt_graph *upstream_graph = NULL; |
| 307 | struct bt_graph *downstream_graph = NULL; |
| 308 | struct bt_component *upstream_component = NULL; |
| 309 | struct bt_component *downstream_component = NULL; |
| 310 | enum bt_component_status component_status; |
| 311 | bt_bool init_can_consume; |
| 312 | |
| 313 | if (!graph) { |
| 314 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 315 | status = BT_GRAPH_STATUS_INVALID; |
| 316 | goto end; |
| 317 | } |
| 318 | init_can_consume = graph->can_consume; |
| 319 | |
| 320 | if (!upstream_port) { |
| 321 | BT_LOGW_STR("Invalid parameter: upstream port is NULL."); |
| 322 | status = BT_GRAPH_STATUS_INVALID; |
| 323 | goto end; |
| 324 | } |
| 325 | |
| 326 | if (!downstream_port) { |
| 327 | BT_LOGW_STR("Invalid parameter: downstream port is NULL."); |
| 328 | status = BT_GRAPH_STATUS_INVALID; |
| 329 | goto end; |
| 330 | } |
| 331 | |
| 332 | BT_LOGD("Connecting component ports within graph: " |
| 333 | "graph-addr=%p, " |
| 334 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " |
| 335 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", |
| 336 | graph, upstream_port, bt_port_get_name(upstream_port), |
| 337 | downstream_port, bt_port_get_name(downstream_port)); |
| 338 | |
| 339 | if (graph->canceled) { |
| 340 | BT_LOGW_STR("Invalid parameter: graph is canceled."); |
| 341 | status = BT_GRAPH_STATUS_CANCELED; |
| 342 | goto end; |
| 343 | } |
| 344 | |
| 345 | bt_graph_set_can_consume(graph, BT_FALSE); |
| 346 | |
| 347 | /* Ensure appropriate types for upstream and downstream ports. */ |
| 348 | if (bt_port_get_type(upstream_port) != BT_PORT_TYPE_OUTPUT) { |
| 349 | BT_LOGW_STR("Invalid parameter: upstream port is not an output port."); |
| 350 | status = BT_GRAPH_STATUS_INVALID; |
| 351 | goto end; |
| 352 | } |
| 353 | if (bt_port_get_type(downstream_port) != BT_PORT_TYPE_INPUT) { |
| 354 | BT_LOGW_STR("Invalid parameter: downstream port is not an input port."); |
| 355 | status = BT_GRAPH_STATUS_INVALID; |
| 356 | goto end; |
| 357 | } |
| 358 | |
| 359 | /* Ensure that both ports are currently unconnected. */ |
| 360 | if (bt_port_is_connected(upstream_port)) { |
| 361 | BT_LOGW_STR("Invalid parameter: upstream port is already connected."); |
| 362 | status = BT_GRAPH_STATUS_INVALID; |
| 363 | goto end; |
| 364 | } |
| 365 | |
| 366 | if (bt_port_is_connected(downstream_port)) { |
| 367 | BT_LOGW_STR("Invalid parameter: downstream port is already connected."); |
| 368 | status = BT_GRAPH_STATUS_INVALID; |
| 369 | goto end; |
| 370 | } |
| 371 | |
| 372 | /* |
| 373 | * Ensure that both ports are still attached to their creating |
| 374 | * component. |
| 375 | */ |
| 376 | upstream_component = bt_port_get_component(upstream_port); |
| 377 | if (!upstream_component) { |
| 378 | BT_LOGW_STR("Invalid parameter: upstream port is loose (does not belong to a component)"); |
| 379 | status = BT_GRAPH_STATUS_INVALID; |
| 380 | goto end; |
| 381 | } |
| 382 | |
| 383 | downstream_component = bt_port_get_component(downstream_port); |
| 384 | if (!downstream_component) { |
| 385 | BT_LOGW_STR("Invalid parameter: downstream port is loose (does not belong to a component)"); |
| 386 | status = BT_GRAPH_STATUS_INVALID; |
| 387 | goto end; |
| 388 | } |
| 389 | |
| 390 | BT_LOGD("Connecting component ports: " |
| 391 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " |
| 392 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\"", |
| 393 | upstream_component, bt_component_get_name(upstream_component), |
| 394 | downstream_component, bt_component_get_name(downstream_component)); |
| 395 | |
| 396 | /* |
| 397 | * At this point the ports are not connected yet. Both |
| 398 | * components need to accept an eventual connection to their |
| 399 | * port by the other port before we continue. |
| 400 | */ |
| 401 | BT_LOGD_STR("Asking upstream component to accept the connection."); |
| 402 | component_status = bt_component_accept_port_connection( |
| 403 | upstream_component, upstream_port, downstream_port); |
| 404 | if (component_status != BT_COMPONENT_STATUS_OK) { |
| 405 | if (component_status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) { |
| 406 | BT_LOGD_STR("Upstream component refused the connection."); |
| 407 | } else { |
| 408 | BT_LOGW("Cannot ask upstream component to accept the connection: " |
| 409 | "status=%s", bt_component_status_string(component_status)); |
| 410 | } |
| 411 | |
| 412 | status = bt_graph_status_from_component_status( |
| 413 | component_status); |
| 414 | goto end; |
| 415 | } |
| 416 | |
| 417 | BT_LOGD_STR("Asking downstream component to accept the connection."); |
| 418 | component_status = bt_component_accept_port_connection( |
| 419 | downstream_component, downstream_port, upstream_port); |
| 420 | if (component_status != BT_COMPONENT_STATUS_OK) { |
| 421 | if (component_status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) { |
| 422 | BT_LOGD_STR("Downstream component refused the connection."); |
| 423 | } else { |
| 424 | BT_LOGW("Cannot ask downstream component to accept the connection: " |
| 425 | "status=%s", bt_component_status_string(component_status)); |
| 426 | } |
| 427 | |
| 428 | status = bt_graph_status_from_component_status( |
| 429 | component_status); |
| 430 | goto end; |
| 431 | } |
| 432 | |
| 433 | BT_LOGD_STR("Creating connection."); |
| 434 | connection = bt_connection_create(graph, upstream_port, |
| 435 | downstream_port); |
| 436 | if (!connection) { |
| 437 | BT_LOGW("Cannot create connection object."); |
| 438 | status = BT_GRAPH_STATUS_NOMEM; |
| 439 | goto end; |
| 440 | } |
| 441 | |
| 442 | BT_LOGD("Connection object created: conn-addr=%p", connection); |
| 443 | |
| 444 | /* |
| 445 | * Ownership of upstream_component/downstream_component and of |
| 446 | * the connection object is transferred to the graph. |
| 447 | */ |
| 448 | g_ptr_array_add(graph->connections, connection); |
| 449 | |
| 450 | /* |
| 451 | * Notify both components that their port is connected. |
| 452 | */ |
| 453 | BT_LOGD_STR("Notifying upstream component that its port is connected."); |
| 454 | component_status = bt_component_port_connected(upstream_component, |
| 455 | upstream_port, downstream_port); |
| 456 | if (component_status != BT_COMPONENT_STATUS_OK) { |
| 457 | BT_LOGW("Error while notifying upstream component that its port is connected: " |
| 458 | "status=%s, graph-addr=%p, " |
| 459 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " |
| 460 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\", " |
| 461 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " |
| 462 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", |
| 463 | bt_component_status_string(component_status), graph, |
| 464 | upstream_component, bt_component_get_name(upstream_component), |
| 465 | downstream_component, bt_component_get_name(downstream_component), |
| 466 | upstream_port, bt_port_get_name(upstream_port), |
| 467 | downstream_port, bt_port_get_name(downstream_port)); |
| 468 | bt_connection_end(connection, true); |
| 469 | status = bt_graph_status_from_component_status( |
| 470 | component_status); |
| 471 | goto end; |
| 472 | } |
| 473 | |
| 474 | connection->notified_upstream_port_connected = true; |
| 475 | BT_LOGD_STR("Notifying downstream component that its port is connected."); |
| 476 | component_status = bt_component_port_connected(downstream_component, |
| 477 | downstream_port, upstream_port); |
| 478 | if (component_status != BT_COMPONENT_STATUS_OK) { |
| 479 | BT_LOGW("Error while notifying downstream component that its port is connected: " |
| 480 | "status=%s, graph-addr=%p, " |
| 481 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " |
| 482 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\", " |
| 483 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " |
| 484 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", |
| 485 | bt_component_status_string(component_status), graph, |
| 486 | upstream_component, bt_component_get_name(upstream_component), |
| 487 | downstream_component, bt_component_get_name(downstream_component), |
| 488 | upstream_port, bt_port_get_name(upstream_port), |
| 489 | downstream_port, bt_port_get_name(downstream_port)); |
| 490 | bt_connection_end(connection, true); |
| 491 | status = bt_graph_status_from_component_status( |
| 492 | component_status); |
| 493 | goto end; |
| 494 | } |
| 495 | |
| 496 | connection->notified_downstream_port_connected = true; |
| 497 | |
| 498 | /* |
| 499 | * Notify the graph's creator that both ports are connected. |
| 500 | */ |
| 501 | BT_LOGD_STR("Notifying graph's user that new component ports are connected."); |
| 502 | bt_graph_notify_ports_connected(graph, upstream_port, downstream_port); |
| 503 | connection->notified_graph_ports_connected = true; |
| 504 | BT_LOGD("Connected component ports within graph: " |
| 505 | "graph-addr=%p, " |
| 506 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " |
| 507 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\", " |
| 508 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " |
| 509 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", |
| 510 | graph, |
| 511 | upstream_component, bt_component_get_name(upstream_component), |
| 512 | downstream_component, bt_component_get_name(downstream_component), |
| 513 | upstream_port, bt_port_get_name(upstream_port), |
| 514 | downstream_port, bt_port_get_name(downstream_port)); |
| 515 | |
| 516 | if (user_connection) { |
| 517 | /* Move reference to user */ |
| 518 | *user_connection = connection; |
| 519 | connection = NULL; |
| 520 | } |
| 521 | |
| 522 | end: |
| 523 | bt_object_put_ref(upstream_graph); |
| 524 | bt_object_put_ref(downstream_graph); |
| 525 | bt_object_put_ref(upstream_component); |
| 526 | bt_object_put_ref(downstream_component); |
| 527 | bt_object_put_ref(connection); |
| 528 | if (graph) { |
| 529 | (void) init_can_consume; |
| 530 | bt_graph_set_can_consume(graph, init_can_consume); |
| 531 | } |
| 532 | return status; |
| 533 | } |
| 534 | |
| 535 | static inline |
| 536 | enum bt_graph_status consume_graph_sink(struct bt_component *sink) |
| 537 | { |
| 538 | enum bt_component_status comp_status; |
| 539 | |
| 540 | BT_ASSERT(sink); |
| 541 | comp_status = bt_component_sink_consume(sink); |
| 542 | BT_LOGV("Consumed from sink: addr=%p, name=\"%s\", status=%s", |
| 543 | sink, bt_component_get_name(sink), |
| 544 | bt_component_status_string(comp_status)); |
| 545 | BT_ASSERT_PRE(comp_status == BT_COMPONENT_STATUS_OK || |
| 546 | comp_status == BT_COMPONENT_STATUS_END || |
| 547 | comp_status == BT_COMPONENT_STATUS_AGAIN || |
| 548 | comp_status == BT_COMPONENT_STATUS_ERROR || |
| 549 | comp_status == BT_COMPONENT_STATUS_NOMEM, |
| 550 | "Invalid component status returned by consuming function: " |
| 551 | "status=%s", bt_component_status_string(comp_status)); |
| 552 | return (enum bt_graph_status) comp_status; |
| 553 | } |
| 554 | |
| 555 | /* |
| 556 | * `node` is removed from the queue of sinks to consume when passed to |
| 557 | * this function. This function adds it back to the queue if there's |
| 558 | * still something to consume afterwards. |
| 559 | */ |
| 560 | static inline |
| 561 | enum bt_graph_status consume_sink_node(struct bt_graph *graph, |
| 562 | GList *node) |
| 563 | { |
| 564 | enum bt_graph_status status; |
| 565 | struct bt_component *sink; |
| 566 | |
| 567 | sink = node->data; |
| 568 | status = consume_graph_sink(sink); |
| 569 | if (unlikely(status != BT_GRAPH_STATUS_END)) { |
| 570 | g_queue_push_tail_link(graph->sinks_to_consume, node); |
| 571 | goto end; |
| 572 | } |
| 573 | |
| 574 | /* End reached, the node is not added back to the queue and free'd. */ |
| 575 | g_queue_delete_link(graph->sinks_to_consume, node); |
| 576 | |
| 577 | /* Don't forward an END status if there are sinks left to consume. */ |
| 578 | if (!g_queue_is_empty(graph->sinks_to_consume)) { |
| 579 | status = BT_GRAPH_STATUS_OK; |
| 580 | goto end; |
| 581 | } |
| 582 | |
| 583 | end: |
| 584 | BT_LOGV("Consumed sink node: status=%s", bt_graph_status_string(status)); |
| 585 | return status; |
| 586 | } |
| 587 | |
| 588 | BT_HIDDEN |
| 589 | enum bt_graph_status bt_graph_consume_sink_no_check(struct bt_graph *graph, |
| 590 | struct bt_component *sink) |
| 591 | { |
| 592 | enum bt_graph_status status; |
| 593 | GList *sink_node; |
| 594 | int index; |
| 595 | |
| 596 | BT_LOGV("Making specific sink consume: addr=%p, " |
| 597 | "comp-addr=%p, comp-name=\"%s\"", |
| 598 | graph, sink, bt_component_get_name(sink)); |
| 599 | |
| 600 | BT_ASSERT(bt_component_borrow_graph(sink) == graph); |
| 601 | |
| 602 | if (g_queue_is_empty(graph->sinks_to_consume)) { |
| 603 | BT_LOGV_STR("Graph's sink queue is empty: end of graph."); |
| 604 | status = BT_GRAPH_STATUS_END; |
| 605 | goto end; |
| 606 | } |
| 607 | |
| 608 | index = g_queue_index(graph->sinks_to_consume, sink); |
| 609 | if (index < 0) { |
| 610 | BT_LOGV_STR("Sink is not marked as consumable: sink is ended."); |
| 611 | status = BT_GRAPH_STATUS_END; |
| 612 | goto end; |
| 613 | } |
| 614 | |
| 615 | sink_node = g_queue_pop_nth_link(graph->sinks_to_consume, index); |
| 616 | BT_ASSERT(sink_node); |
| 617 | status = consume_sink_node(graph, sink_node); |
| 618 | |
| 619 | end: |
| 620 | return status; |
| 621 | } |
| 622 | |
| 623 | static inline |
| 624 | enum bt_graph_status bt_graph_consume_no_check(struct bt_graph *graph) |
| 625 | { |
| 626 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
| 627 | struct bt_component *sink; |
| 628 | GList *current_node; |
| 629 | |
| 630 | BT_LOGV("Making next sink consume: addr=%p", graph); |
| 631 | BT_ASSERT_PRE(graph->has_sink, |
| 632 | "Graph has no sink component: %!+g", graph); |
| 633 | |
| 634 | if (unlikely(g_queue_is_empty(graph->sinks_to_consume))) { |
| 635 | BT_LOGV_STR("Graph's sink queue is empty: end of graph."); |
| 636 | status = BT_GRAPH_STATUS_END; |
| 637 | goto end; |
| 638 | } |
| 639 | |
| 640 | current_node = g_queue_pop_head_link(graph->sinks_to_consume); |
| 641 | sink = current_node->data; |
| 642 | BT_LOGV("Chose next sink to consume: comp-addr=%p, comp-name=\"%s\"", |
| 643 | sink, bt_component_get_name(sink)); |
| 644 | status = consume_sink_node(graph, current_node); |
| 645 | |
| 646 | end: |
| 647 | return status; |
| 648 | } |
| 649 | |
| 650 | enum bt_graph_status bt_private_graph_consume( |
| 651 | struct bt_private_graph *priv_graph) |
| 652 | { |
| 653 | struct bt_graph *graph = (void *) priv_graph; |
| 654 | enum bt_graph_status status; |
| 655 | |
| 656 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 657 | BT_ASSERT_PRE(!graph->canceled, "Graph is canceled: %!+g", graph); |
| 658 | BT_ASSERT_PRE(graph->can_consume, |
| 659 | "Cannot consume graph in its current state: %!+g", graph); |
| 660 | bt_graph_set_can_consume(graph, BT_FALSE); |
| 661 | status = bt_graph_consume_no_check(graph); |
| 662 | bt_graph_set_can_consume(graph, BT_TRUE); |
| 663 | return status; |
| 664 | } |
| 665 | |
| 666 | enum bt_graph_status bt_private_graph_run( |
| 667 | struct bt_private_graph *priv_graph) |
| 668 | { |
| 669 | struct bt_graph *graph = (void *) priv_graph; |
| 670 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
| 671 | |
| 672 | if (!graph) { |
| 673 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 674 | status = BT_GRAPH_STATUS_INVALID; |
| 675 | goto end; |
| 676 | } |
| 677 | |
| 678 | if (graph->canceled) { |
| 679 | BT_LOGW("Invalid parameter: graph is canceled: " |
| 680 | "graph-addr=%p", graph); |
| 681 | status = BT_GRAPH_STATUS_CANCELED; |
| 682 | goto end; |
| 683 | } |
| 684 | |
| 685 | BT_ASSERT_PRE(graph->can_consume, |
| 686 | "Cannot consume graph in its current state: %!+g", graph); |
| 687 | bt_graph_set_can_consume(graph, BT_FALSE); |
| 688 | BT_LOGV("Running graph: addr=%p", graph); |
| 689 | |
| 690 | do { |
| 691 | /* |
| 692 | * Check if the graph is canceled at each iteration. If |
| 693 | * the graph was canceled by another thread or by a |
| 694 | * signal, this is not a warning nor an error, it was |
| 695 | * intentional: log with a DEBUG level only. |
| 696 | */ |
| 697 | if (unlikely(graph->canceled)) { |
| 698 | BT_LOGD("Stopping the graph: graph is canceled: " |
| 699 | "graph-addr=%p", graph); |
| 700 | status = BT_GRAPH_STATUS_CANCELED; |
| 701 | goto end; |
| 702 | } |
| 703 | |
| 704 | status = bt_graph_consume_no_check(graph); |
| 705 | if (unlikely(status == BT_GRAPH_STATUS_AGAIN)) { |
| 706 | /* |
| 707 | * If AGAIN is received and there are multiple |
| 708 | * sinks, go ahead and consume from the next |
| 709 | * sink. |
| 710 | * |
| 711 | * However, in the case where a single sink is |
| 712 | * left, the caller can decide to busy-wait and |
| 713 | * call bt_graph_run() continuously until the |
| 714 | * source is ready or it can decide to sleep for |
| 715 | * an arbitrary amount of time. |
| 716 | */ |
| 717 | if (graph->sinks_to_consume->length > 1) { |
| 718 | status = BT_GRAPH_STATUS_OK; |
| 719 | } |
| 720 | } else if (status == BT_GRAPH_STATUS_NO_SINK) { |
| 721 | goto end; |
| 722 | } |
| 723 | } while (status == BT_GRAPH_STATUS_OK); |
| 724 | |
| 725 | if (g_queue_is_empty(graph->sinks_to_consume)) { |
| 726 | status = BT_GRAPH_STATUS_END; |
| 727 | } |
| 728 | |
| 729 | end: |
| 730 | BT_LOGV("Graph ran: status=%s", bt_graph_status_string(status)); |
| 731 | if (graph) { |
| 732 | bt_graph_set_can_consume(graph, BT_TRUE); |
| 733 | } |
| 734 | return status; |
| 735 | } |
| 736 | |
| 737 | static |
| 738 | int add_listener(GArray *listeners, void *func, void *removed, void *data) |
| 739 | { |
| 740 | struct bt_graph_listener listener = { |
| 741 | .func = func, |
| 742 | .removed = removed, |
| 743 | .data = data, |
| 744 | }; |
| 745 | |
| 746 | g_array_append_val(listeners, listener); |
| 747 | return listeners->len - 1; |
| 748 | } |
| 749 | |
| 750 | int bt_private_graph_add_port_added_listener( |
| 751 | struct bt_private_graph *priv_graph, |
| 752 | bt_private_graph_port_added_listener listener, |
| 753 | bt_private_graph_listener_removed listener_removed, void *data) |
| 754 | { |
| 755 | struct bt_graph *graph = (void *) priv_graph; |
| 756 | int ret; |
| 757 | |
| 758 | if (!graph) { |
| 759 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 760 | ret = -1; |
| 761 | goto end; |
| 762 | } |
| 763 | |
| 764 | if (graph->in_remove_listener) { |
| 765 | BT_LOGW("Cannot call this function during the execution of a remove listener: " |
| 766 | "addr=%p", graph); |
| 767 | ret = -1; |
| 768 | goto end; |
| 769 | } |
| 770 | |
| 771 | if (!listener) { |
| 772 | BT_LOGW_STR("Invalid parameter: listener is NULL."); |
| 773 | ret = -1; |
| 774 | goto end; |
| 775 | } |
| 776 | |
| 777 | ret = add_listener(graph->listeners.port_added, listener, |
| 778 | listener_removed, data); |
| 779 | BT_LOGV("Added \"port added\" listener to graph: " |
| 780 | "graph-addr=%p, listener-addr=%p, pos=%d", |
| 781 | graph, listener, ret); |
| 782 | |
| 783 | end: |
| 784 | return ret; |
| 785 | } |
| 786 | |
| 787 | int bt_private_graph_add_port_removed_listener( |
| 788 | struct bt_private_graph *priv_graph, |
| 789 | bt_private_graph_port_removed_listener listener, |
| 790 | bt_private_graph_listener_removed listener_removed, void *data) |
| 791 | { |
| 792 | struct bt_graph *graph = (void *) priv_graph; |
| 793 | int ret; |
| 794 | |
| 795 | if (!graph) { |
| 796 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 797 | ret = -1; |
| 798 | goto end; |
| 799 | } |
| 800 | |
| 801 | if (graph->in_remove_listener) { |
| 802 | BT_LOGW("Cannot call this function during the execution of a remove listener: " |
| 803 | "addr=%p", graph); |
| 804 | ret = -1; |
| 805 | goto end; |
| 806 | } |
| 807 | |
| 808 | if (!listener) { |
| 809 | BT_LOGW_STR("Invalid parameter: listener is NULL."); |
| 810 | ret = -1; |
| 811 | goto end; |
| 812 | } |
| 813 | |
| 814 | ret = add_listener(graph->listeners.port_removed, listener, |
| 815 | listener_removed, data); |
| 816 | BT_LOGV("Added \"port removed\" listener to graph: " |
| 817 | "graph-addr=%p, listener-addr=%p, pos=%d", |
| 818 | graph, listener, ret); |
| 819 | |
| 820 | end: |
| 821 | return ret; |
| 822 | } |
| 823 | |
| 824 | int bt_private_graph_add_ports_connected_listener( |
| 825 | struct bt_private_graph *priv_graph, |
| 826 | bt_private_graph_ports_connected_listener listener, |
| 827 | bt_private_graph_listener_removed listener_removed, void *data) |
| 828 | { |
| 829 | struct bt_graph *graph = (void *) priv_graph; |
| 830 | int ret; |
| 831 | |
| 832 | if (!graph) { |
| 833 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 834 | ret = -1; |
| 835 | goto end; |
| 836 | } |
| 837 | |
| 838 | if (graph->in_remove_listener) { |
| 839 | BT_LOGW("Cannot call this function during the execution of a remove listener: " |
| 840 | "addr=%p", graph); |
| 841 | ret = -1; |
| 842 | goto end; |
| 843 | } |
| 844 | |
| 845 | if (!listener) { |
| 846 | BT_LOGW_STR("Invalid parameter: listener is NULL."); |
| 847 | ret = -1; |
| 848 | goto end; |
| 849 | } |
| 850 | |
| 851 | ret = add_listener(graph->listeners.ports_connected, listener, |
| 852 | listener_removed, data); |
| 853 | BT_LOGV("Added \"port connected\" listener to graph: " |
| 854 | "graph-addr=%p, listener-addr=%p, pos=%d", |
| 855 | graph, listener, ret); |
| 856 | |
| 857 | end: |
| 858 | return ret; |
| 859 | } |
| 860 | |
| 861 | int bt_private_graph_add_ports_disconnected_listener( |
| 862 | struct bt_private_graph *priv_graph, |
| 863 | bt_private_graph_ports_disconnected_listener listener, |
| 864 | bt_private_graph_listener_removed listener_removed, void *data) |
| 865 | { |
| 866 | struct bt_graph *graph = (void *) priv_graph; |
| 867 | int ret; |
| 868 | |
| 869 | if (!graph) { |
| 870 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 871 | ret = -1; |
| 872 | goto end; |
| 873 | } |
| 874 | |
| 875 | if (graph->in_remove_listener) { |
| 876 | BT_LOGW("Cannot call this function during the execution of a remove listener: " |
| 877 | "addr=%p", graph); |
| 878 | ret = -1; |
| 879 | goto end; |
| 880 | } |
| 881 | |
| 882 | if (!listener) { |
| 883 | BT_LOGW_STR("Invalid parameter: listener is NULL."); |
| 884 | ret = -1; |
| 885 | goto end; |
| 886 | } |
| 887 | |
| 888 | ret = add_listener(graph->listeners.ports_disconnected, listener, |
| 889 | listener_removed, data); |
| 890 | BT_LOGV("Added \"port disconnected\" listener to graph: " |
| 891 | "graph-addr=%p, listener-addr=%p, pos=%d", |
| 892 | graph, listener, ret); |
| 893 | |
| 894 | end: |
| 895 | return ret; |
| 896 | } |
| 897 | |
| 898 | BT_HIDDEN |
| 899 | void bt_graph_notify_port_added(struct bt_graph *graph, struct bt_port *port) |
| 900 | { |
| 901 | size_t i; |
| 902 | |
| 903 | BT_LOGV("Notifying graph listeners that a port was added: " |
| 904 | "graph-addr=%p, port-addr=%p, port-name=\"%s\"", |
| 905 | graph, port, bt_port_get_name(port)); |
| 906 | |
| 907 | for (i = 0; i < graph->listeners.port_added->len; i++) { |
| 908 | struct bt_graph_listener listener = |
| 909 | g_array_index(graph->listeners.port_added, |
| 910 | struct bt_graph_listener, i); |
| 911 | bt_private_graph_port_added_listener func = listener.func; |
| 912 | |
| 913 | BT_ASSERT(func); |
| 914 | func(port, listener.data); |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | BT_HIDDEN |
| 919 | void bt_graph_notify_port_removed(struct bt_graph *graph, |
| 920 | struct bt_component *comp, struct bt_port *port) |
| 921 | { |
| 922 | size_t i; |
| 923 | |
| 924 | BT_LOGV("Notifying graph listeners that a port was removed: " |
| 925 | "graph-addr=%p, port-addr=%p, port-name=\"%s\"", |
| 926 | graph, port, bt_port_get_name(port)); |
| 927 | |
| 928 | for (i = 0; i < graph->listeners.port_removed->len; i++) { |
| 929 | struct bt_graph_listener listener = |
| 930 | g_array_index(graph->listeners.port_removed, |
| 931 | struct bt_graph_listener, i); |
| 932 | bt_private_graph_port_removed_listener func = listener.func; |
| 933 | |
| 934 | BT_ASSERT(func); |
| 935 | func(comp, port, listener.data); |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | BT_HIDDEN |
| 940 | void bt_graph_notify_ports_connected(struct bt_graph *graph, |
| 941 | struct bt_port *upstream_port, struct bt_port *downstream_port) |
| 942 | { |
| 943 | size_t i; |
| 944 | |
| 945 | BT_LOGV("Notifying graph listeners that two ports were connected: " |
| 946 | "graph-addr=%p, " |
| 947 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " |
| 948 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", |
| 949 | graph, upstream_port, bt_port_get_name(upstream_port), |
| 950 | downstream_port, bt_port_get_name(downstream_port)); |
| 951 | |
| 952 | for (i = 0; i < graph->listeners.ports_connected->len; i++) { |
| 953 | struct bt_graph_listener listener = |
| 954 | g_array_index(graph->listeners.ports_connected, |
| 955 | struct bt_graph_listener, i); |
| 956 | bt_private_graph_ports_connected_listener func = listener.func; |
| 957 | |
| 958 | BT_ASSERT(func); |
| 959 | func(upstream_port, downstream_port, listener.data); |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | BT_HIDDEN |
| 964 | void bt_graph_notify_ports_disconnected(struct bt_graph *graph, |
| 965 | struct bt_component *upstream_comp, |
| 966 | struct bt_component *downstream_comp, |
| 967 | struct bt_port *upstream_port, struct bt_port *downstream_port) |
| 968 | { |
| 969 | size_t i; |
| 970 | |
| 971 | BT_LOGV("Notifying graph listeners that two ports were disconnected: " |
| 972 | "graph-addr=%p, " |
| 973 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " |
| 974 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", |
| 975 | graph, upstream_port, bt_port_get_name(upstream_port), |
| 976 | downstream_port, bt_port_get_name(downstream_port)); |
| 977 | |
| 978 | for (i = 0; i < graph->listeners.ports_disconnected->len; i++) { |
| 979 | struct bt_graph_listener listener = |
| 980 | g_array_index(graph->listeners.ports_disconnected, |
| 981 | struct bt_graph_listener, i); |
| 982 | bt_private_graph_ports_disconnected_listener func = listener.func; |
| 983 | |
| 984 | BT_ASSERT(func); |
| 985 | func(upstream_comp, downstream_comp, upstream_port, |
| 986 | downstream_port, listener.data); |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | enum bt_graph_status bt_private_graph_cancel( |
| 991 | struct bt_private_graph *priv_graph) |
| 992 | { |
| 993 | struct bt_graph *graph = (void *) priv_graph; |
| 994 | enum bt_graph_status ret = BT_GRAPH_STATUS_OK; |
| 995 | |
| 996 | if (!graph) { |
| 997 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 998 | ret = BT_GRAPH_STATUS_INVALID; |
| 999 | goto end; |
| 1000 | } |
| 1001 | |
| 1002 | graph->canceled = BT_TRUE; |
| 1003 | BT_LOGV("Canceled graph: addr=%p", graph); |
| 1004 | |
| 1005 | end: |
| 1006 | return ret; |
| 1007 | } |
| 1008 | |
| 1009 | bt_bool bt_graph_is_canceled(struct bt_graph *graph) |
| 1010 | { |
| 1011 | bt_bool canceled = BT_FALSE; |
| 1012 | |
| 1013 | if (!graph) { |
| 1014 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 1015 | goto end; |
| 1016 | } |
| 1017 | |
| 1018 | canceled = graph->canceled; |
| 1019 | |
| 1020 | end: |
| 1021 | return canceled; |
| 1022 | } |
| 1023 | |
| 1024 | BT_HIDDEN |
| 1025 | void bt_graph_remove_connection(struct bt_graph *graph, |
| 1026 | struct bt_connection *connection) |
| 1027 | { |
| 1028 | BT_ASSERT(graph); |
| 1029 | BT_ASSERT(connection); |
| 1030 | BT_LOGV("Removing graph's connection: graph-addr=%p, conn-addr=%p", |
| 1031 | graph, connection); |
| 1032 | g_ptr_array_remove(graph->connections, connection); |
| 1033 | } |
| 1034 | |
| 1035 | enum bt_graph_status bt_private_graph_add_component_with_init_method_data( |
| 1036 | struct bt_private_graph *priv_graph, |
| 1037 | struct bt_component_class *component_class, |
| 1038 | const char *name, struct bt_value *params, |
| 1039 | void *init_method_data, |
| 1040 | struct bt_component **user_component) |
| 1041 | { |
| 1042 | struct bt_graph *graph = (void *) priv_graph; |
| 1043 | enum bt_graph_status graph_status = BT_GRAPH_STATUS_OK; |
| 1044 | enum bt_component_status comp_status; |
| 1045 | struct bt_component *component = NULL; |
| 1046 | enum bt_component_class_type type; |
| 1047 | size_t i; |
| 1048 | bt_bool init_can_consume; |
| 1049 | |
| 1050 | bt_object_get_ref(params); |
| 1051 | |
| 1052 | if (!graph) { |
| 1053 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 1054 | graph_status = BT_GRAPH_STATUS_INVALID; |
| 1055 | goto end; |
| 1056 | } |
| 1057 | init_can_consume = graph->can_consume; |
| 1058 | |
| 1059 | if (!component_class) { |
| 1060 | BT_LOGW_STR("Invalid parameter: component class is NULL."); |
| 1061 | graph_status = BT_GRAPH_STATUS_INVALID; |
| 1062 | goto end; |
| 1063 | } |
| 1064 | |
| 1065 | graph->can_consume = BT_FALSE; |
| 1066 | type = bt_component_class_get_type(component_class); |
| 1067 | BT_LOGD("Adding component to graph: " |
| 1068 | "graph-addr=%p, comp-cls-addr=%p, " |
| 1069 | "comp-cls-type=%s, name=\"%s\", params-addr=%p, " |
| 1070 | "init-method-data-addr=%p", |
| 1071 | graph, component_class, bt_component_class_type_string(type), |
| 1072 | name, params, init_method_data); |
| 1073 | |
| 1074 | if (!name) { |
| 1075 | BT_LOGW_STR("Invalid parameter: name is NULL."); |
| 1076 | graph_status = BT_GRAPH_STATUS_INVALID; |
| 1077 | goto end; |
| 1078 | } |
| 1079 | |
| 1080 | if (graph->canceled) { |
| 1081 | BT_LOGW_STR("Invalid parameter: graph is canceled."); |
| 1082 | graph_status = BT_GRAPH_STATUS_CANCELED; |
| 1083 | goto end; |
| 1084 | } |
| 1085 | |
| 1086 | if (type != BT_COMPONENT_CLASS_TYPE_SOURCE && |
| 1087 | type != BT_COMPONENT_CLASS_TYPE_FILTER && |
| 1088 | type != BT_COMPONENT_CLASS_TYPE_SINK) { |
| 1089 | BT_LOGW("Invalid parameter: unknown component class type: " |
| 1090 | "type=%d", type); |
| 1091 | graph_status = BT_GRAPH_STATUS_INVALID; |
| 1092 | goto end; |
| 1093 | } |
| 1094 | |
| 1095 | for (i = 0; i < graph->components->len; i++) { |
| 1096 | void *other_comp = graph->components->pdata[i]; |
| 1097 | |
| 1098 | if (strcmp(name, bt_component_get_name(other_comp)) == 0) { |
| 1099 | BT_LOGW("Invalid parameter: another component with the same name already exists in the graph: " |
| 1100 | "other-comp-addr=%p, name=\"%s\"", |
| 1101 | other_comp, name); |
| 1102 | graph_status = BT_GRAPH_STATUS_INVALID; |
| 1103 | goto end; |
| 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | /* |
| 1108 | * Parameters must be a map value, but we create a convenient |
| 1109 | * empty one if it's NULL. |
| 1110 | */ |
| 1111 | if (params) { |
| 1112 | if (!bt_value_is_map(params)) { |
| 1113 | BT_LOGW("Invalid parameter: initialization parameters must be a map value: " |
| 1114 | "type=%s", |
| 1115 | bt_common_value_type_string( |
| 1116 | bt_value_get_type(params))); |
| 1117 | graph_status = BT_GRAPH_STATUS_INVALID; |
| 1118 | goto end; |
| 1119 | } |
| 1120 | } else { |
| 1121 | params = bt_value_borrow_from_private( |
| 1122 | bt_private_value_map_create()); |
| 1123 | if (!params) { |
| 1124 | BT_LOGE_STR("Cannot create map value object."); |
| 1125 | graph_status = BT_GRAPH_STATUS_NOMEM; |
| 1126 | goto end; |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | comp_status = bt_component_create(component_class, name, &component); |
| 1131 | if (comp_status != BT_COMPONENT_STATUS_OK) { |
| 1132 | BT_LOGE("Cannot create empty component object: status=%s", |
| 1133 | bt_component_status_string(comp_status)); |
| 1134 | graph_status = bt_graph_status_from_component_status( |
| 1135 | comp_status); |
| 1136 | goto end; |
| 1137 | } |
| 1138 | |
| 1139 | /* |
| 1140 | * The user's initialization method needs to see that this |
| 1141 | * component is part of the graph. If the user method fails, we |
| 1142 | * immediately remove the component from the graph's components. |
| 1143 | */ |
| 1144 | g_ptr_array_add(graph->components, component); |
| 1145 | bt_component_set_graph(component, graph); |
| 1146 | |
| 1147 | if (component_class->methods.init) { |
| 1148 | BT_LOGD_STR("Calling user's initialization method."); |
| 1149 | comp_status = component_class->methods.init( |
| 1150 | bt_private_component_from_component(component), params, |
| 1151 | init_method_data); |
| 1152 | BT_LOGD("User method returned: status=%s", |
| 1153 | bt_component_status_string(comp_status)); |
| 1154 | if (comp_status != BT_COMPONENT_STATUS_OK) { |
| 1155 | BT_LOGW_STR("Initialization method failed."); |
| 1156 | graph_status = bt_graph_status_from_component_status( |
| 1157 | comp_status); |
| 1158 | bt_component_set_graph(component, NULL); |
| 1159 | g_ptr_array_remove_fast(graph->components, component); |
| 1160 | goto end; |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | /* |
| 1165 | * Mark the component as initialized so that its finalization |
| 1166 | * method is called when it is destroyed. |
| 1167 | */ |
| 1168 | component->initialized = true; |
| 1169 | |
| 1170 | /* |
| 1171 | * If it's a sink component, it needs to be part of the graph's |
| 1172 | * sink queue to be consumed by bt_graph_consume(). |
| 1173 | */ |
| 1174 | if (bt_component_is_sink(component)) { |
| 1175 | graph->has_sink = BT_TRUE; |
| 1176 | g_queue_push_tail(graph->sinks_to_consume, component); |
| 1177 | } |
| 1178 | |
| 1179 | /* |
| 1180 | * Freeze the component class now that it's instantiated at |
| 1181 | * least once. |
| 1182 | */ |
| 1183 | BT_LOGD_STR("Freezing component class."); |
| 1184 | bt_component_class_freeze(component->class); |
| 1185 | BT_LOGD("Added component to graph: " |
| 1186 | "graph-addr=%p, comp-cls-addr=%p, " |
| 1187 | "comp-cls-type=%s, name=\"%s\", params-addr=%p, " |
| 1188 | "init-method-data-addr=%p, comp-addr=%p", |
| 1189 | graph, component_class, bt_component_class_type_string(type), |
| 1190 | name, params, init_method_data, component); |
| 1191 | |
| 1192 | if (user_component) { |
| 1193 | /* Move reference to user */ |
| 1194 | *user_component = component; |
| 1195 | component = NULL; |
| 1196 | } |
| 1197 | |
| 1198 | end: |
| 1199 | bt_object_put_ref(component); |
| 1200 | bt_object_put_ref(params); |
| 1201 | if (graph) { |
| 1202 | graph->can_consume = init_can_consume; |
| 1203 | } |
| 1204 | return graph_status; |
| 1205 | } |
| 1206 | |
| 1207 | enum bt_graph_status bt_private_graph_add_component( |
| 1208 | struct bt_private_graph *graph, |
| 1209 | struct bt_component_class *component_class, |
| 1210 | const char *name, struct bt_value *params, |
| 1211 | struct bt_component **component) |
| 1212 | { |
| 1213 | return bt_private_graph_add_component_with_init_method_data(graph, |
| 1214 | component_class, name, params, NULL, component); |
| 1215 | } |
| 1216 | |
| 1217 | BT_HIDDEN |
| 1218 | int bt_graph_remove_unconnected_component(struct bt_graph *graph, |
| 1219 | struct bt_component *component) |
| 1220 | { |
| 1221 | bt_bool init_can_consume; |
| 1222 | int64_t count; |
| 1223 | uint64_t i; |
| 1224 | int ret = 0; |
| 1225 | |
| 1226 | BT_ASSERT(graph); |
| 1227 | BT_ASSERT(component); |
| 1228 | BT_ASSERT(component->base.ref_count == 0); |
| 1229 | BT_ASSERT(bt_component_borrow_graph(component) == graph); |
| 1230 | |
| 1231 | init_can_consume = graph->can_consume; |
| 1232 | count = bt_component_get_input_port_count(component); |
| 1233 | |
| 1234 | for (i = 0; i < count; i++) { |
| 1235 | struct bt_port *port = |
| 1236 | bt_component_get_input_port_by_index(component, i); |
| 1237 | |
| 1238 | BT_ASSERT(port); |
| 1239 | bt_object_put_ref(port); |
| 1240 | |
| 1241 | if (bt_port_is_connected(port)) { |
| 1242 | BT_LOGW("Cannot remove component from graph: " |
| 1243 | "an input port is connected: " |
| 1244 | "graph-addr=%p, comp-addr=%p, " |
| 1245 | "comp-name=\"%s\", connected-port-addr=%p, " |
| 1246 | "connected-port-name=\"%s\"", |
| 1247 | graph, component, |
| 1248 | bt_component_get_name(component), |
| 1249 | port, bt_port_get_name(port)); |
| 1250 | goto error; |
| 1251 | } |
| 1252 | } |
| 1253 | |
| 1254 | count = bt_component_get_output_port_count(component); |
| 1255 | |
| 1256 | for (i = 0; i < count; i++) { |
| 1257 | struct bt_port *port = |
| 1258 | bt_component_get_output_port_by_index(component, i); |
| 1259 | |
| 1260 | BT_ASSERT(port); |
| 1261 | bt_object_put_ref(port); |
| 1262 | |
| 1263 | if (bt_port_is_connected(port)) { |
| 1264 | BT_LOGW("Cannot remove component from graph: " |
| 1265 | "an output port is connected: " |
| 1266 | "graph-addr=%p, comp-addr=%p, " |
| 1267 | "comp-name=\"%s\", connected-port-addr=%p, " |
| 1268 | "connected-port-name=\"%s\"", |
| 1269 | graph, component, |
| 1270 | bt_component_get_name(component), |
| 1271 | port, bt_port_get_name(port)); |
| 1272 | goto error; |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | graph->can_consume = BT_FALSE; |
| 1277 | |
| 1278 | /* Possibly remove from sinks to consume */ |
| 1279 | (void) g_queue_remove(graph->sinks_to_consume, component); |
| 1280 | |
| 1281 | if (graph->sinks_to_consume->length == 0) { |
| 1282 | graph->has_sink = BT_FALSE; |
| 1283 | } |
| 1284 | |
| 1285 | /* |
| 1286 | * This calls bt_object_try_spec_release() on the component, and |
| 1287 | * since its reference count is 0, its destructor is called. Its |
| 1288 | * destructor calls the user's finalization method (if set). |
| 1289 | */ |
| 1290 | g_ptr_array_remove(graph->components, component); |
| 1291 | goto end; |
| 1292 | |
| 1293 | error: |
| 1294 | ret = -1; |
| 1295 | |
| 1296 | end: |
| 1297 | graph->can_consume = init_can_consume; |
| 1298 | return ret; |
| 1299 | } |
| 1300 | |
| 1301 | BT_HIDDEN |
| 1302 | void bt_graph_add_notification(struct bt_graph *graph, |
| 1303 | struct bt_notification *notif) |
| 1304 | { |
| 1305 | BT_ASSERT(graph); |
| 1306 | BT_ASSERT(notif); |
| 1307 | |
| 1308 | /* |
| 1309 | * It's okay not to take a reference because, when a |
| 1310 | * notification's reference count drops to 0, either: |
| 1311 | * |
| 1312 | * * It is recycled back to one of this graph's pool. |
| 1313 | * * It is destroyed because it doesn't have any link to any |
| 1314 | * graph, which means the original graph is already destroyed. |
| 1315 | */ |
| 1316 | g_ptr_array_add(graph->notifications, notif); |
| 1317 | } |
| 1318 | |
| 1319 | struct bt_graph *bt_graph_borrow_from_private( |
| 1320 | struct bt_private_graph *priv_graph) |
| 1321 | { |
| 1322 | return (void *) priv_graph; |
| 1323 | } |