| 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/graph.h> |
| 29 | #include <babeltrace/graph/graph-const.h> |
| 30 | #include <babeltrace/graph/graph-internal.h> |
| 31 | #include <babeltrace/graph/connection-internal.h> |
| 32 | #include <babeltrace/graph/component-sink-internal.h> |
| 33 | #include <babeltrace/graph/component-source-const.h> |
| 34 | #include <babeltrace/graph/component-filter-const.h> |
| 35 | #include <babeltrace/graph/port-const.h> |
| 36 | #include <babeltrace/graph/notification-internal.h> |
| 37 | #include <babeltrace/graph/notification-event-internal.h> |
| 38 | #include <babeltrace/graph/notification-packet-internal.h> |
| 39 | #include <babeltrace/compiler-internal.h> |
| 40 | #include <babeltrace/common-internal.h> |
| 41 | #include <babeltrace/types.h> |
| 42 | #include <babeltrace/value.h> |
| 43 | #include <babeltrace/value-const.h> |
| 44 | #include <babeltrace/value-internal.h> |
| 45 | #include <babeltrace/object.h> |
| 46 | #include <babeltrace/assert-internal.h> |
| 47 | #include <babeltrace/assert-pre-internal.h> |
| 48 | #include <unistd.h> |
| 49 | #include <glib.h> |
| 50 | |
| 51 | typedef void (*port_added_func_t)(const void *, const void *, void *); |
| 52 | |
| 53 | typedef void (*port_removed_func_t)(const void *, const void *, void *); |
| 54 | |
| 55 | typedef void (*ports_connected_func_t)(const void *, const void *, const void *, |
| 56 | const void *, void *); |
| 57 | |
| 58 | typedef void (*ports_disconnected_func_t)(const void *, const void *, |
| 59 | const void *, const void *, void *); |
| 60 | |
| 61 | typedef enum bt_self_component_status (*comp_init_method_t)(const void *, |
| 62 | const void *, void *); |
| 63 | |
| 64 | struct bt_graph_listener { |
| 65 | bt_graph_listener_removed_func removed; |
| 66 | void *data; |
| 67 | }; |
| 68 | |
| 69 | struct bt_graph_listener_port_added { |
| 70 | struct bt_graph_listener base; |
| 71 | port_added_func_t func; |
| 72 | }; |
| 73 | |
| 74 | struct bt_graph_listener_port_removed { |
| 75 | struct bt_graph_listener base; |
| 76 | port_removed_func_t func; |
| 77 | }; |
| 78 | |
| 79 | struct bt_graph_listener_ports_connected { |
| 80 | struct bt_graph_listener base; |
| 81 | ports_connected_func_t func; |
| 82 | }; |
| 83 | |
| 84 | struct bt_graph_listener_ports_disconnected { |
| 85 | struct bt_graph_listener base; |
| 86 | ports_disconnected_func_t func; |
| 87 | }; |
| 88 | |
| 89 | #define INIT_LISTENERS_ARRAY(_type, _listeners) \ |
| 90 | do { \ |
| 91 | _listeners = g_array_new(FALSE, TRUE, sizeof(_type)); \ |
| 92 | if (!(_listeners)) { \ |
| 93 | BT_LOGE_STR("Failed to allocate one GArray."); \ |
| 94 | } \ |
| 95 | } while (0) |
| 96 | |
| 97 | #define CALL_REMOVE_LISTENERS(_type, _listeners) \ |
| 98 | do { \ |
| 99 | size_t i; \ |
| 100 | \ |
| 101 | for (i = 0; i < (_listeners)->len; i++) { \ |
| 102 | _type *listener = \ |
| 103 | &g_array_index((_listeners), _type, i); \ |
| 104 | \ |
| 105 | if (listener->base.removed) { \ |
| 106 | listener->base.removed(listener->base.data); \ |
| 107 | } \ |
| 108 | } \ |
| 109 | } while (0) |
| 110 | |
| 111 | static |
| 112 | void destroy_graph(struct bt_object *obj) |
| 113 | { |
| 114 | struct bt_graph *graph = container_of(obj, struct bt_graph, base); |
| 115 | |
| 116 | /* |
| 117 | * The graph's reference count is 0 if we're here. Increment |
| 118 | * it to avoid a double-destroy (possibly infinitely recursive) |
| 119 | * in this situation: |
| 120 | * |
| 121 | * 1. We put and destroy a connection. |
| 122 | * 2. This connection's destructor finalizes its active |
| 123 | * notification iterators. |
| 124 | * 3. A notification iterator's finalization function gets a |
| 125 | * new reference on its component (reference count goes from |
| 126 | * 0 to 1). |
| 127 | * 4. Since this component's reference count goes to 1, it takes |
| 128 | * a reference on its parent (this graph). This graph's |
| 129 | * reference count goes from 0 to 1. |
| 130 | * 5. The notification iterator's finalization function puts its |
| 131 | * component reference (reference count goes from 1 to 0). |
| 132 | * 6. Since this component's reference count goes from 1 to 0, |
| 133 | * it puts its parent (this graph). This graph's reference |
| 134 | * count goes from 1 to 0. |
| 135 | * 7. Since this graph's reference count goes from 1 to 0, |
| 136 | * its destructor is called (this function). |
| 137 | * |
| 138 | * With the incrementation below, the graph's reference count at |
| 139 | * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This |
| 140 | * ensures that this function is not called two times. |
| 141 | */ |
| 142 | BT_LIB_LOGD("Destroying graph: %!+g", graph); |
| 143 | obj->ref_count++; |
| 144 | |
| 145 | /* |
| 146 | * Cancel the graph to disallow some operations, like creating |
| 147 | * notification iterators and adding ports to components. |
| 148 | */ |
| 149 | (void) bt_graph_cancel((void *) graph); |
| 150 | |
| 151 | /* Call all remove listeners */ |
| 152 | CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added, |
| 153 | graph->listeners.source_output_port_added); |
| 154 | CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added, |
| 155 | graph->listeners.filter_output_port_added); |
| 156 | CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added, |
| 157 | graph->listeners.filter_input_port_added); |
| 158 | CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added, |
| 159 | graph->listeners.sink_input_port_added); |
| 160 | CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_removed, |
| 161 | graph->listeners.source_output_port_removed); |
| 162 | CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_removed, |
| 163 | graph->listeners.filter_output_port_removed); |
| 164 | CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_removed, |
| 165 | graph->listeners.filter_input_port_removed); |
| 166 | CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_removed, |
| 167 | graph->listeners.sink_input_port_removed); |
| 168 | CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected, |
| 169 | graph->listeners.source_filter_ports_connected); |
| 170 | CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected, |
| 171 | graph->listeners.source_sink_ports_connected); |
| 172 | CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected, |
| 173 | graph->listeners.filter_sink_ports_connected); |
| 174 | CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_disconnected, |
| 175 | graph->listeners.source_filter_ports_disconnected); |
| 176 | CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_disconnected, |
| 177 | graph->listeners.source_sink_ports_disconnected); |
| 178 | CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_disconnected, |
| 179 | graph->listeners.filter_sink_ports_disconnected); |
| 180 | |
| 181 | if (graph->notifications) { |
| 182 | g_ptr_array_free(graph->notifications, TRUE); |
| 183 | graph->notifications = NULL; |
| 184 | } |
| 185 | |
| 186 | if (graph->connections) { |
| 187 | BT_LOGD_STR("Destroying connections."); |
| 188 | g_ptr_array_free(graph->connections, TRUE); |
| 189 | graph->connections = NULL; |
| 190 | } |
| 191 | |
| 192 | if (graph->components) { |
| 193 | BT_LOGD_STR("Destroying components."); |
| 194 | g_ptr_array_free(graph->components, TRUE); |
| 195 | graph->components = NULL; |
| 196 | } |
| 197 | |
| 198 | if (graph->sinks_to_consume) { |
| 199 | g_queue_free(graph->sinks_to_consume); |
| 200 | graph->sinks_to_consume = NULL; |
| 201 | } |
| 202 | |
| 203 | if (graph->listeners.source_output_port_added) { |
| 204 | g_array_free(graph->listeners.source_output_port_added, TRUE); |
| 205 | graph->listeners.source_output_port_added = NULL; |
| 206 | } |
| 207 | |
| 208 | if (graph->listeners.filter_output_port_added) { |
| 209 | g_array_free(graph->listeners.filter_output_port_added, TRUE); |
| 210 | graph->listeners.filter_output_port_added = NULL; |
| 211 | } |
| 212 | |
| 213 | if (graph->listeners.filter_input_port_added) { |
| 214 | g_array_free(graph->listeners.filter_input_port_added, TRUE); |
| 215 | graph->listeners.filter_input_port_added = NULL; |
| 216 | } |
| 217 | |
| 218 | if (graph->listeners.sink_input_port_added) { |
| 219 | g_array_free(graph->listeners.sink_input_port_added, TRUE); |
| 220 | graph->listeners.sink_input_port_added = NULL; |
| 221 | } |
| 222 | |
| 223 | if (graph->listeners.source_output_port_removed) { |
| 224 | g_array_free(graph->listeners.source_output_port_removed, TRUE); |
| 225 | graph->listeners.source_output_port_removed = NULL; |
| 226 | } |
| 227 | |
| 228 | if (graph->listeners.filter_output_port_removed) { |
| 229 | g_array_free(graph->listeners.filter_output_port_removed, TRUE); |
| 230 | graph->listeners.filter_output_port_removed = NULL; |
| 231 | } |
| 232 | |
| 233 | if (graph->listeners.filter_input_port_removed) { |
| 234 | g_array_free(graph->listeners.filter_input_port_removed, TRUE); |
| 235 | graph->listeners.filter_input_port_removed = NULL; |
| 236 | } |
| 237 | |
| 238 | if (graph->listeners.sink_input_port_removed) { |
| 239 | g_array_free(graph->listeners.sink_input_port_removed, TRUE); |
| 240 | graph->listeners.sink_input_port_removed = NULL; |
| 241 | } |
| 242 | |
| 243 | if (graph->listeners.source_filter_ports_connected) { |
| 244 | g_array_free(graph->listeners.source_filter_ports_connected, |
| 245 | TRUE); |
| 246 | graph->listeners.source_filter_ports_connected = NULL; |
| 247 | } |
| 248 | |
| 249 | if (graph->listeners.source_sink_ports_connected) { |
| 250 | g_array_free(graph->listeners.source_sink_ports_connected, |
| 251 | TRUE); |
| 252 | graph->listeners.source_sink_ports_connected = NULL; |
| 253 | } |
| 254 | |
| 255 | if (graph->listeners.filter_sink_ports_connected) { |
| 256 | g_array_free(graph->listeners.filter_sink_ports_connected, |
| 257 | TRUE); |
| 258 | graph->listeners.filter_sink_ports_connected = NULL; |
| 259 | } |
| 260 | |
| 261 | if (graph->listeners.source_filter_ports_disconnected) { |
| 262 | g_array_free(graph->listeners.source_filter_ports_disconnected, |
| 263 | TRUE); |
| 264 | graph->listeners.source_filter_ports_disconnected = NULL; |
| 265 | } |
| 266 | |
| 267 | if (graph->listeners.source_sink_ports_disconnected) { |
| 268 | g_array_free(graph->listeners.source_sink_ports_disconnected, |
| 269 | TRUE); |
| 270 | graph->listeners.source_sink_ports_disconnected = NULL; |
| 271 | } |
| 272 | |
| 273 | if (graph->listeners.filter_sink_ports_disconnected) { |
| 274 | g_array_free(graph->listeners.filter_sink_ports_disconnected, |
| 275 | TRUE); |
| 276 | graph->listeners.filter_sink_ports_disconnected = NULL; |
| 277 | } |
| 278 | |
| 279 | bt_object_pool_finalize(&graph->event_notif_pool); |
| 280 | bt_object_pool_finalize(&graph->packet_begin_notif_pool); |
| 281 | bt_object_pool_finalize(&graph->packet_end_notif_pool); |
| 282 | g_free(graph); |
| 283 | } |
| 284 | |
| 285 | static |
| 286 | void destroy_notification_event(struct bt_notification *notif, |
| 287 | struct bt_graph *graph) |
| 288 | { |
| 289 | bt_notification_event_destroy(notif); |
| 290 | } |
| 291 | |
| 292 | static |
| 293 | void destroy_notification_packet_begin(struct bt_notification *notif, |
| 294 | struct bt_graph *graph) |
| 295 | { |
| 296 | bt_notification_packet_beginning_destroy(notif); |
| 297 | } |
| 298 | |
| 299 | static |
| 300 | void destroy_notification_packet_end(struct bt_notification *notif, |
| 301 | struct bt_graph *graph) |
| 302 | { |
| 303 | bt_notification_packet_end_destroy(notif); |
| 304 | } |
| 305 | |
| 306 | static |
| 307 | void notify_notification_graph_is_destroyed(struct bt_notification *notif) |
| 308 | { |
| 309 | bt_notification_unlink_graph(notif); |
| 310 | } |
| 311 | |
| 312 | struct bt_graph *bt_graph_create(void) |
| 313 | { |
| 314 | struct bt_graph *graph; |
| 315 | int ret; |
| 316 | |
| 317 | BT_LOGD_STR("Creating graph object."); |
| 318 | graph = g_new0(struct bt_graph, 1); |
| 319 | if (!graph) { |
| 320 | BT_LOGE_STR("Failed to allocate one graph."); |
| 321 | goto end; |
| 322 | } |
| 323 | |
| 324 | bt_object_init_shared(&graph->base, destroy_graph); |
| 325 | graph->connections = g_ptr_array_new_with_free_func( |
| 326 | (GDestroyNotify) bt_object_try_spec_release); |
| 327 | if (!graph->connections) { |
| 328 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
| 329 | goto error; |
| 330 | } |
| 331 | graph->components = g_ptr_array_new_with_free_func( |
| 332 | (GDestroyNotify) bt_object_try_spec_release); |
| 333 | if (!graph->components) { |
| 334 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
| 335 | goto error; |
| 336 | } |
| 337 | graph->sinks_to_consume = g_queue_new(); |
| 338 | if (!graph->sinks_to_consume) { |
| 339 | BT_LOGE_STR("Failed to allocate one GQueue."); |
| 340 | goto error; |
| 341 | } |
| 342 | |
| 343 | bt_graph_set_can_consume(graph, true); |
| 344 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added, |
| 345 | graph->listeners.source_output_port_added); |
| 346 | |
| 347 | if (!graph->listeners.source_output_port_added) { |
| 348 | ret = -1; |
| 349 | goto error; |
| 350 | } |
| 351 | |
| 352 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added, |
| 353 | graph->listeners.filter_output_port_added); |
| 354 | |
| 355 | if (!graph->listeners.filter_output_port_added) { |
| 356 | ret = -1; |
| 357 | goto error; |
| 358 | } |
| 359 | |
| 360 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added, |
| 361 | graph->listeners.filter_input_port_added); |
| 362 | |
| 363 | if (!graph->listeners.filter_input_port_added) { |
| 364 | ret = -1; |
| 365 | goto error; |
| 366 | } |
| 367 | |
| 368 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added, |
| 369 | graph->listeners.sink_input_port_added); |
| 370 | |
| 371 | if (!graph->listeners.sink_input_port_added) { |
| 372 | ret = -1; |
| 373 | goto error; |
| 374 | } |
| 375 | |
| 376 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_removed, |
| 377 | graph->listeners.source_output_port_removed); |
| 378 | |
| 379 | if (!graph->listeners.source_output_port_removed) { |
| 380 | ret = -1; |
| 381 | goto error; |
| 382 | } |
| 383 | |
| 384 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_removed, |
| 385 | graph->listeners.filter_output_port_removed); |
| 386 | |
| 387 | if (!graph->listeners.filter_output_port_removed) { |
| 388 | ret = -1; |
| 389 | goto error; |
| 390 | } |
| 391 | |
| 392 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_removed, |
| 393 | graph->listeners.filter_input_port_removed); |
| 394 | |
| 395 | if (!graph->listeners.filter_input_port_removed) { |
| 396 | ret = -1; |
| 397 | goto error; |
| 398 | } |
| 399 | |
| 400 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_removed, |
| 401 | graph->listeners.sink_input_port_removed); |
| 402 | |
| 403 | if (!graph->listeners.sink_input_port_removed) { |
| 404 | ret = -1; |
| 405 | goto error; |
| 406 | } |
| 407 | |
| 408 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected, |
| 409 | graph->listeners.source_filter_ports_connected); |
| 410 | |
| 411 | if (!graph->listeners.source_filter_ports_connected) { |
| 412 | ret = -1; |
| 413 | goto error; |
| 414 | } |
| 415 | |
| 416 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected, |
| 417 | graph->listeners.source_sink_ports_connected); |
| 418 | |
| 419 | if (!graph->listeners.source_sink_ports_connected) { |
| 420 | ret = -1; |
| 421 | goto error; |
| 422 | } |
| 423 | |
| 424 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected, |
| 425 | graph->listeners.filter_sink_ports_connected); |
| 426 | |
| 427 | if (!graph->listeners.filter_sink_ports_connected) { |
| 428 | ret = -1; |
| 429 | goto error; |
| 430 | } |
| 431 | |
| 432 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_disconnected, |
| 433 | graph->listeners.source_filter_ports_disconnected); |
| 434 | |
| 435 | if (!graph->listeners.source_filter_ports_disconnected) { |
| 436 | ret = -1; |
| 437 | goto error; |
| 438 | } |
| 439 | |
| 440 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_disconnected, |
| 441 | graph->listeners.source_sink_ports_disconnected); |
| 442 | |
| 443 | if (!graph->listeners.source_sink_ports_disconnected) { |
| 444 | ret = -1; |
| 445 | goto error; |
| 446 | } |
| 447 | |
| 448 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_disconnected, |
| 449 | graph->listeners.filter_sink_ports_disconnected); |
| 450 | |
| 451 | if (!graph->listeners.filter_sink_ports_disconnected) { |
| 452 | ret = -1; |
| 453 | goto error; |
| 454 | } |
| 455 | |
| 456 | ret = bt_object_pool_initialize(&graph->event_notif_pool, |
| 457 | (bt_object_pool_new_object_func) bt_notification_event_new, |
| 458 | (bt_object_pool_destroy_object_func) destroy_notification_event, |
| 459 | graph); |
| 460 | if (ret) { |
| 461 | BT_LOGE("Failed to initialize event notification pool: ret=%d", |
| 462 | ret); |
| 463 | goto error; |
| 464 | } |
| 465 | |
| 466 | ret = bt_object_pool_initialize(&graph->packet_begin_notif_pool, |
| 467 | (bt_object_pool_new_object_func) bt_notification_packet_beginning_new, |
| 468 | (bt_object_pool_destroy_object_func) destroy_notification_packet_begin, |
| 469 | graph); |
| 470 | if (ret) { |
| 471 | BT_LOGE("Failed to initialize packet beginning notification pool: ret=%d", |
| 472 | ret); |
| 473 | goto error; |
| 474 | } |
| 475 | |
| 476 | ret = bt_object_pool_initialize(&graph->packet_end_notif_pool, |
| 477 | (bt_object_pool_new_object_func) bt_notification_packet_end_new, |
| 478 | (bt_object_pool_destroy_object_func) destroy_notification_packet_end, |
| 479 | graph); |
| 480 | if (ret) { |
| 481 | BT_LOGE("Failed to initialize packet end notification pool: ret=%d", |
| 482 | ret); |
| 483 | goto error; |
| 484 | } |
| 485 | |
| 486 | graph->notifications = g_ptr_array_new_with_free_func( |
| 487 | (GDestroyNotify) notify_notification_graph_is_destroyed); |
| 488 | BT_LIB_LOGD("Created graph object: %!+g", graph); |
| 489 | |
| 490 | end: |
| 491 | return (void *) graph; |
| 492 | |
| 493 | error: |
| 494 | BT_OBJECT_PUT_REF_AND_RESET(graph); |
| 495 | goto end; |
| 496 | } |
| 497 | |
| 498 | enum bt_graph_status bt_graph_connect_ports( |
| 499 | struct bt_graph *graph, |
| 500 | const struct bt_port_output *upstream_port_out, |
| 501 | const struct bt_port_input *downstream_port_in, |
| 502 | const struct bt_connection **user_connection) |
| 503 | { |
| 504 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
| 505 | struct bt_connection *connection = NULL; |
| 506 | struct bt_port *upstream_port = (void *) upstream_port_out; |
| 507 | struct bt_port *downstream_port = (void *) downstream_port_in; |
| 508 | struct bt_component *upstream_component = NULL; |
| 509 | struct bt_component *downstream_component = NULL; |
| 510 | enum bt_self_component_status component_status; |
| 511 | bool init_can_consume; |
| 512 | |
| 513 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 514 | BT_ASSERT_PRE_NON_NULL(upstream_port, "Upstream port"); |
| 515 | BT_ASSERT_PRE_NON_NULL(downstream_port, "Downstream port port"); |
| 516 | BT_ASSERT_PRE(!graph->canceled, "Graph is canceled: %!+g", graph); |
| 517 | BT_ASSERT_PRE(!bt_port_is_connected(upstream_port), |
| 518 | "Upstream port is already connected: %!+p", upstream_port); |
| 519 | BT_ASSERT_PRE(!bt_port_is_connected(downstream_port), |
| 520 | "Downstream port is already connected: %!+p", downstream_port); |
| 521 | BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) upstream_port), |
| 522 | "Upstream port does not belong to a component: %!+p", |
| 523 | upstream_port); |
| 524 | BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) downstream_port), |
| 525 | "Downstream port does not belong to a component: %!+p", |
| 526 | downstream_port); |
| 527 | init_can_consume = graph->can_consume; |
| 528 | BT_LIB_LOGD("Connecting component ports within graph: " |
| 529 | "%![graph-]+g, %![up-port-]+p, %![down-port-]+p", |
| 530 | graph, upstream_port, downstream_port); |
| 531 | bt_graph_set_can_consume(graph, false); |
| 532 | upstream_component = bt_port_borrow_component_inline( |
| 533 | (void *) upstream_port); |
| 534 | downstream_component = bt_port_borrow_component_inline( |
| 535 | (void *) downstream_port); |
| 536 | |
| 537 | /* |
| 538 | * At this point the ports are not connected yet. Both |
| 539 | * components need to accept an eventual connection to their |
| 540 | * port by the other port before we continue. |
| 541 | */ |
| 542 | BT_LIB_LOGD("Asking upstream component to accept the connection: " |
| 543 | "%![comp-]+c", upstream_component); |
| 544 | component_status = bt_component_accept_port_connection( |
| 545 | upstream_component, (void *) upstream_port, |
| 546 | (void *) downstream_port); |
| 547 | if (component_status != BT_SELF_COMPONENT_STATUS_OK) { |
| 548 | if (component_status == BT_SELF_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) { |
| 549 | BT_LOGD_STR("Upstream component refused the connection."); |
| 550 | } else { |
| 551 | BT_LOGW("Cannot ask upstream component to accept the connection: " |
| 552 | "status=%s", bt_self_component_status_string(component_status)); |
| 553 | } |
| 554 | |
| 555 | status = (int) component_status; |
| 556 | goto end; |
| 557 | } |
| 558 | |
| 559 | BT_LIB_LOGD("Asking downstream component to accept the connection: " |
| 560 | "%![comp-]+c", downstream_component); |
| 561 | component_status = bt_component_accept_port_connection( |
| 562 | downstream_component, (void *) downstream_port, |
| 563 | (void *) upstream_port); |
| 564 | if (component_status != BT_SELF_COMPONENT_STATUS_OK) { |
| 565 | if (component_status == BT_SELF_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) { |
| 566 | BT_LOGD_STR("Downstream component refused the connection."); |
| 567 | } else { |
| 568 | BT_LOGW("Cannot ask downstream component to accept the connection: " |
| 569 | "status=%s", bt_self_component_status_string(component_status)); |
| 570 | } |
| 571 | |
| 572 | status = (int) component_status; |
| 573 | goto end; |
| 574 | } |
| 575 | |
| 576 | BT_LOGD_STR("Creating connection."); |
| 577 | connection = bt_connection_create(graph, (void *) upstream_port, |
| 578 | (void *) downstream_port); |
| 579 | if (!connection) { |
| 580 | BT_LOGW("Cannot create connection object."); |
| 581 | status = BT_GRAPH_STATUS_NOMEM; |
| 582 | goto end; |
| 583 | } |
| 584 | |
| 585 | BT_LIB_LOGD("Connection object created: %!+x", connection); |
| 586 | |
| 587 | /* |
| 588 | * Ownership of upstream_component/downstream_component and of |
| 589 | * the connection object is transferred to the graph. |
| 590 | */ |
| 591 | g_ptr_array_add(graph->connections, connection); |
| 592 | |
| 593 | /* |
| 594 | * Notify both components that their port is connected. |
| 595 | */ |
| 596 | BT_LIB_LOGD("Notifying upstream component that its port is connected: " |
| 597 | "%![comp-]+c, %![port-]+p", upstream_component, upstream_port); |
| 598 | component_status = bt_component_port_connected(upstream_component, |
| 599 | (void *) upstream_port, (void *) downstream_port); |
| 600 | if (component_status != BT_SELF_COMPONENT_STATUS_OK) { |
| 601 | BT_LIB_LOGW("Error while notifying upstream component that its port is connected: " |
| 602 | "status=%s, %![graph-]+g, %![up-comp-]+c, " |
| 603 | "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p", |
| 604 | bt_self_component_status_string(component_status), |
| 605 | graph, upstream_component, downstream_component, |
| 606 | upstream_port, downstream_port); |
| 607 | bt_connection_end(connection, true); |
| 608 | status = (int) component_status; |
| 609 | goto end; |
| 610 | } |
| 611 | |
| 612 | connection->notified_upstream_port_connected = true; |
| 613 | BT_LIB_LOGD("Notifying downstream component that its port is connected: " |
| 614 | "%![comp-]+c, %![port-]+p", downstream_component, |
| 615 | downstream_port); |
| 616 | component_status = bt_component_port_connected(downstream_component, |
| 617 | (void *) downstream_port, (void *) upstream_port); |
| 618 | if (component_status != BT_SELF_COMPONENT_STATUS_OK) { |
| 619 | BT_LIB_LOGW("Error while notifying downstream component that its port is connected: " |
| 620 | "status=%s, %![graph-]+g, %![up-comp-]+c, " |
| 621 | "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p", |
| 622 | bt_self_component_status_string(component_status), |
| 623 | graph, upstream_component, downstream_component, |
| 624 | upstream_port, downstream_port); |
| 625 | bt_connection_end(connection, true); |
| 626 | status = (int) component_status; |
| 627 | goto end; |
| 628 | } |
| 629 | |
| 630 | connection->notified_downstream_port_connected = true; |
| 631 | |
| 632 | /* |
| 633 | * Notify the graph's creator that both ports are connected. |
| 634 | */ |
| 635 | BT_LOGD_STR("Notifying graph's user that new component ports are connected."); |
| 636 | bt_graph_notify_ports_connected(graph, upstream_port, downstream_port); |
| 637 | connection->notified_graph_ports_connected = true; |
| 638 | BT_LIB_LOGD("Connected component ports within graph: " |
| 639 | "%![graph-]+g, %![up-comp-]+c, %![down-comp-]+c, " |
| 640 | "%![up-port-]+p, %![down-port-]+p", |
| 641 | graph, upstream_component, downstream_component, |
| 642 | upstream_port, downstream_port); |
| 643 | |
| 644 | if (user_connection) { |
| 645 | /* Move reference to user */ |
| 646 | *user_connection = connection; |
| 647 | connection = NULL; |
| 648 | } |
| 649 | |
| 650 | end: |
| 651 | bt_object_put_ref(connection); |
| 652 | (void) init_can_consume; |
| 653 | bt_graph_set_can_consume(graph, init_can_consume); |
| 654 | return status; |
| 655 | } |
| 656 | |
| 657 | static inline |
| 658 | enum bt_graph_status consume_graph_sink(struct bt_component_sink *comp) |
| 659 | { |
| 660 | enum bt_self_component_status comp_status; |
| 661 | struct bt_component_class_sink *sink_class = NULL; |
| 662 | |
| 663 | BT_ASSERT(comp); |
| 664 | sink_class = (void *) comp->parent.class; |
| 665 | BT_ASSERT(sink_class->methods.consume); |
| 666 | BT_LIB_LOGD("Calling user's consume method: %!+c", comp); |
| 667 | comp_status = sink_class->methods.consume((void *) comp); |
| 668 | BT_LOGD("User method returned: status=%s", |
| 669 | bt_self_component_status_string(comp_status)); |
| 670 | BT_ASSERT_PRE(comp_status == BT_SELF_COMPONENT_STATUS_OK || |
| 671 | comp_status == BT_SELF_COMPONENT_STATUS_END || |
| 672 | comp_status == BT_SELF_COMPONENT_STATUS_AGAIN || |
| 673 | comp_status == BT_SELF_COMPONENT_STATUS_ERROR || |
| 674 | comp_status == BT_SELF_COMPONENT_STATUS_NOMEM, |
| 675 | "Invalid component status returned by consuming method: " |
| 676 | "status=%s", bt_self_component_status_string(comp_status)); |
| 677 | if (comp_status < 0) { |
| 678 | BT_LOGW_STR("Consume method failed."); |
| 679 | goto end; |
| 680 | } |
| 681 | |
| 682 | BT_LIB_LOGV("Consumed from sink: %![comp-]+c, status=%s", |
| 683 | comp, bt_self_component_status_string(comp_status)); |
| 684 | |
| 685 | end: |
| 686 | return (int) comp_status; |
| 687 | } |
| 688 | |
| 689 | /* |
| 690 | * `node` is removed from the queue of sinks to consume when passed to |
| 691 | * this function. This function adds it back to the queue if there's |
| 692 | * still something to consume afterwards. |
| 693 | */ |
| 694 | static inline |
| 695 | enum bt_graph_status consume_sink_node(struct bt_graph *graph, GList *node) |
| 696 | { |
| 697 | enum bt_graph_status status; |
| 698 | struct bt_component_sink *sink; |
| 699 | |
| 700 | sink = node->data; |
| 701 | status = consume_graph_sink(sink); |
| 702 | if (unlikely(status != BT_GRAPH_STATUS_END)) { |
| 703 | g_queue_push_tail_link(graph->sinks_to_consume, node); |
| 704 | goto end; |
| 705 | } |
| 706 | |
| 707 | /* End reached, the node is not added back to the queue and free'd. */ |
| 708 | g_queue_delete_link(graph->sinks_to_consume, node); |
| 709 | |
| 710 | /* Don't forward an END status if there are sinks left to consume. */ |
| 711 | if (!g_queue_is_empty(graph->sinks_to_consume)) { |
| 712 | status = BT_GRAPH_STATUS_OK; |
| 713 | goto end; |
| 714 | } |
| 715 | |
| 716 | end: |
| 717 | BT_LIB_LOGV("Consumed sink node: %![comp-]+c, status=%s", |
| 718 | sink, bt_graph_status_string(status)); |
| 719 | return status; |
| 720 | } |
| 721 | |
| 722 | BT_HIDDEN |
| 723 | enum bt_graph_status bt_graph_consume_sink_no_check(struct bt_graph *graph, |
| 724 | struct bt_component_sink *sink) |
| 725 | { |
| 726 | enum bt_graph_status status; |
| 727 | GList *sink_node; |
| 728 | int index; |
| 729 | |
| 730 | BT_LIB_LOGV("Making specific sink consume: %![comp-]+c", sink); |
| 731 | BT_ASSERT(bt_component_borrow_graph((void *) sink) == graph); |
| 732 | |
| 733 | if (g_queue_is_empty(graph->sinks_to_consume)) { |
| 734 | BT_LOGV_STR("Graph's sink queue is empty: end of graph."); |
| 735 | status = BT_GRAPH_STATUS_END; |
| 736 | goto end; |
| 737 | } |
| 738 | |
| 739 | index = g_queue_index(graph->sinks_to_consume, sink); |
| 740 | if (index < 0) { |
| 741 | BT_LOGV_STR("Sink is not marked as consumable: sink is ended."); |
| 742 | status = BT_GRAPH_STATUS_END; |
| 743 | goto end; |
| 744 | } |
| 745 | |
| 746 | sink_node = g_queue_pop_nth_link(graph->sinks_to_consume, index); |
| 747 | BT_ASSERT(sink_node); |
| 748 | status = consume_sink_node(graph, sink_node); |
| 749 | |
| 750 | end: |
| 751 | return status; |
| 752 | } |
| 753 | |
| 754 | static inline |
| 755 | enum bt_graph_status consume_no_check(struct bt_graph *graph) |
| 756 | { |
| 757 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
| 758 | struct bt_component *sink; |
| 759 | GList *current_node; |
| 760 | |
| 761 | BT_ASSERT_PRE(graph->has_sink, |
| 762 | "Graph has no sink component: %!+g", graph); |
| 763 | BT_LIB_LOGV("Making next sink consume: %![graph-]+g", graph); |
| 764 | |
| 765 | if (unlikely(g_queue_is_empty(graph->sinks_to_consume))) { |
| 766 | BT_LOGV_STR("Graph's sink queue is empty: end of graph."); |
| 767 | status = BT_GRAPH_STATUS_END; |
| 768 | goto end; |
| 769 | } |
| 770 | |
| 771 | current_node = g_queue_pop_head_link(graph->sinks_to_consume); |
| 772 | sink = current_node->data; |
| 773 | BT_LIB_LOGV("Chose next sink to consume: %!+c", sink); |
| 774 | status = consume_sink_node(graph, current_node); |
| 775 | |
| 776 | end: |
| 777 | return status; |
| 778 | } |
| 779 | |
| 780 | enum bt_graph_status bt_graph_consume( |
| 781 | struct bt_graph *graph) |
| 782 | { |
| 783 | enum bt_graph_status status; |
| 784 | |
| 785 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 786 | BT_ASSERT_PRE(!graph->canceled, "Graph is canceled: %!+g", graph); |
| 787 | BT_ASSERT_PRE(graph->can_consume, |
| 788 | "Cannot consume graph in its current state: %!+g", graph); |
| 789 | bt_graph_set_can_consume(graph, BT_FALSE); |
| 790 | status = consume_no_check(graph); |
| 791 | bt_graph_set_can_consume(graph, BT_TRUE); |
| 792 | return status; |
| 793 | } |
| 794 | |
| 795 | enum bt_graph_status bt_graph_run(struct bt_graph *graph) |
| 796 | { |
| 797 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
| 798 | |
| 799 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 800 | BT_ASSERT_PRE(!graph->canceled, "Graph is canceled: %!+g", graph); |
| 801 | BT_ASSERT_PRE(graph->can_consume, |
| 802 | "Cannot consume graph in its current state: %!+g", graph); |
| 803 | bt_graph_set_can_consume(graph, BT_FALSE); |
| 804 | BT_LIB_LOGV("Running graph: %!+g", graph); |
| 805 | |
| 806 | do { |
| 807 | /* |
| 808 | * Check if the graph is canceled at each iteration. If |
| 809 | * the graph was canceled by another thread or by a |
| 810 | * signal handler, this is not a warning nor an error, |
| 811 | * it was intentional: log with a DEBUG level only. |
| 812 | */ |
| 813 | if (unlikely(graph->canceled)) { |
| 814 | BT_LIB_LOGD("Stopping the graph: graph is canceled: " |
| 815 | "%!+g", graph); |
| 816 | status = BT_GRAPH_STATUS_CANCELED; |
| 817 | goto end; |
| 818 | } |
| 819 | |
| 820 | status = consume_no_check(graph); |
| 821 | if (unlikely(status == BT_GRAPH_STATUS_AGAIN)) { |
| 822 | /* |
| 823 | * If AGAIN is received and there are multiple |
| 824 | * sinks, go ahead and consume from the next |
| 825 | * sink. |
| 826 | * |
| 827 | * However, in the case where a single sink is |
| 828 | * left, the caller can decide to busy-wait and |
| 829 | * call bt_graph_run() continuously |
| 830 | * until the source is ready or it can decide to |
| 831 | * sleep for an arbitrary amount of time. |
| 832 | */ |
| 833 | if (graph->sinks_to_consume->length > 1) { |
| 834 | status = BT_GRAPH_STATUS_OK; |
| 835 | } |
| 836 | } else if (status == BT_GRAPH_STATUS_NO_SINK) { |
| 837 | goto end; |
| 838 | } |
| 839 | } while (status == BT_GRAPH_STATUS_OK); |
| 840 | |
| 841 | if (g_queue_is_empty(graph->sinks_to_consume)) { |
| 842 | status = BT_GRAPH_STATUS_END; |
| 843 | } |
| 844 | |
| 845 | end: |
| 846 | BT_LIB_LOGV("Graph ran: %![graph-]+g, status=%s", graph, |
| 847 | bt_graph_status_string(status)); |
| 848 | bt_graph_set_can_consume(graph, BT_TRUE); |
| 849 | return status; |
| 850 | } |
| 851 | |
| 852 | enum bt_graph_status |
| 853 | bt_graph_add_source_component_output_port_added_listener( |
| 854 | struct bt_graph *graph, |
| 855 | bt_graph_source_component_output_port_added_listener_func func, |
| 856 | bt_graph_listener_removed_func listener_removed, void *data, |
| 857 | int *out_listener_id) |
| 858 | { |
| 859 | struct bt_graph_listener_port_added listener = { |
| 860 | .base = { |
| 861 | .removed = listener_removed, |
| 862 | .data = data, |
| 863 | }, |
| 864 | .func = (port_added_func_t) func, |
| 865 | }; |
| 866 | int listener_id; |
| 867 | |
| 868 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 869 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); |
| 870 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); |
| 871 | BT_ASSERT_PRE(!graph->in_remove_listener, |
| 872 | "Graph currently executing a \"listener removed\" listener: " |
| 873 | "%!+g", graph); |
| 874 | g_array_append_val(graph->listeners.source_output_port_added, listener); |
| 875 | listener_id = graph->listeners.source_output_port_added->len - 1; |
| 876 | BT_LIB_LOGV("Added \"source component output port added\" listener to graph: " |
| 877 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
| 878 | listener_id); |
| 879 | |
| 880 | if (listener_id) { |
| 881 | *out_listener_id = listener_id; |
| 882 | } |
| 883 | |
| 884 | return BT_GRAPH_STATUS_OK; |
| 885 | } |
| 886 | |
| 887 | enum bt_graph_status |
| 888 | bt_graph_add_filter_component_output_port_added_listener( |
| 889 | struct bt_graph *graph, |
| 890 | bt_graph_filter_component_output_port_added_listener_func func, |
| 891 | bt_graph_listener_removed_func listener_removed, void *data, |
| 892 | int *out_listener_id) |
| 893 | { |
| 894 | struct bt_graph_listener_port_added listener = { |
| 895 | .base = { |
| 896 | .removed = listener_removed, |
| 897 | .data = data, |
| 898 | }, |
| 899 | .func = (port_added_func_t) func, |
| 900 | }; |
| 901 | int listener_id; |
| 902 | |
| 903 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 904 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); |
| 905 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); |
| 906 | BT_ASSERT_PRE(!graph->in_remove_listener, |
| 907 | "Graph currently executing a \"listener removed\" listener: " |
| 908 | "%!+g", graph); |
| 909 | g_array_append_val(graph->listeners.filter_output_port_added, listener); |
| 910 | listener_id = graph->listeners.filter_output_port_added->len - 1; |
| 911 | BT_LIB_LOGV("Added \"filter component output port added\" listener to graph: " |
| 912 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
| 913 | listener_id); |
| 914 | |
| 915 | if (listener_id) { |
| 916 | *out_listener_id = listener_id; |
| 917 | } |
| 918 | |
| 919 | return BT_GRAPH_STATUS_OK; |
| 920 | } |
| 921 | |
| 922 | enum bt_graph_status |
| 923 | bt_graph_add_filter_component_input_port_added_listener( |
| 924 | struct bt_graph *graph, |
| 925 | bt_graph_filter_component_input_port_added_listener_func func, |
| 926 | bt_graph_listener_removed_func listener_removed, void *data, |
| 927 | int *out_listener_id) |
| 928 | { |
| 929 | struct bt_graph_listener_port_added listener = { |
| 930 | .base = { |
| 931 | .removed = listener_removed, |
| 932 | .data = data, |
| 933 | }, |
| 934 | .func = (port_added_func_t) func, |
| 935 | }; |
| 936 | int listener_id; |
| 937 | |
| 938 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 939 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); |
| 940 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); |
| 941 | BT_ASSERT_PRE(!graph->in_remove_listener, |
| 942 | "Graph currently executing a \"listener removed\" listener: " |
| 943 | "%!+g", graph); |
| 944 | g_array_append_val(graph->listeners.filter_input_port_added, listener); |
| 945 | listener_id = graph->listeners.filter_input_port_added->len - 1; |
| 946 | BT_LIB_LOGV("Added \"filter component input port added\" listener to graph: " |
| 947 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
| 948 | listener_id); |
| 949 | |
| 950 | if (listener_id) { |
| 951 | *out_listener_id = listener_id; |
| 952 | } |
| 953 | |
| 954 | return BT_GRAPH_STATUS_OK; |
| 955 | } |
| 956 | |
| 957 | enum bt_graph_status |
| 958 | bt_graph_add_sink_component_input_port_added_listener( |
| 959 | struct bt_graph *graph, |
| 960 | bt_graph_sink_component_input_port_added_listener_func func, |
| 961 | bt_graph_listener_removed_func listener_removed, void *data, |
| 962 | int *out_listener_id) |
| 963 | { |
| 964 | struct bt_graph_listener_port_added listener = { |
| 965 | .base = { |
| 966 | .removed = listener_removed, |
| 967 | .data = data, |
| 968 | }, |
| 969 | .func = (port_added_func_t) func, |
| 970 | }; |
| 971 | int listener_id; |
| 972 | |
| 973 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 974 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); |
| 975 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); |
| 976 | BT_ASSERT_PRE(!graph->in_remove_listener, |
| 977 | "Graph currently executing a \"listener removed\" listener: " |
| 978 | "%!+g", graph); |
| 979 | g_array_append_val(graph->listeners.sink_input_port_added, listener); |
| 980 | listener_id = graph->listeners.sink_input_port_added->len - 1; |
| 981 | BT_LIB_LOGV("Added \"sink component input port added\" listener to graph: " |
| 982 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
| 983 | listener_id); |
| 984 | |
| 985 | if (listener_id) { |
| 986 | *out_listener_id = listener_id; |
| 987 | } |
| 988 | |
| 989 | return BT_GRAPH_STATUS_OK; |
| 990 | } |
| 991 | |
| 992 | enum bt_graph_status |
| 993 | bt_graph_add_source_component_output_port_removed_listener( |
| 994 | struct bt_graph *graph, |
| 995 | bt_graph_source_component_output_port_removed_listener_func func, |
| 996 | bt_graph_listener_removed_func listener_removed, void *data, |
| 997 | int *out_listener_id) |
| 998 | { |
| 999 | struct bt_graph_listener_port_removed listener = { |
| 1000 | .base = { |
| 1001 | .removed = listener_removed, |
| 1002 | .data = data, |
| 1003 | }, |
| 1004 | .func = (port_removed_func_t) func, |
| 1005 | }; |
| 1006 | int listener_id; |
| 1007 | |
| 1008 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 1009 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); |
| 1010 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); |
| 1011 | BT_ASSERT_PRE(!graph->in_remove_listener, |
| 1012 | "Graph currently executing a \"listener removed\" listener: " |
| 1013 | "%!+g", graph); |
| 1014 | g_array_append_val(graph->listeners.source_output_port_removed, listener); |
| 1015 | listener_id = graph->listeners.source_output_port_removed->len - 1; |
| 1016 | BT_LIB_LOGV("Added \"source component output port removed\" listener to graph: " |
| 1017 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
| 1018 | listener_id); |
| 1019 | |
| 1020 | if (listener_id) { |
| 1021 | *out_listener_id = listener_id; |
| 1022 | } |
| 1023 | |
| 1024 | return BT_GRAPH_STATUS_OK; |
| 1025 | } |
| 1026 | |
| 1027 | enum bt_graph_status |
| 1028 | bt_graph_add_filter_component_output_port_removed_listener( |
| 1029 | struct bt_graph *graph, |
| 1030 | bt_graph_filter_component_output_port_removed_listener_func func, |
| 1031 | bt_graph_listener_removed_func listener_removed, void *data, |
| 1032 | int *out_listener_id) |
| 1033 | { |
| 1034 | struct bt_graph_listener_port_removed listener = { |
| 1035 | .base = { |
| 1036 | .removed = listener_removed, |
| 1037 | .data = data, |
| 1038 | }, |
| 1039 | .func = (port_removed_func_t) func, |
| 1040 | }; |
| 1041 | int listener_id; |
| 1042 | |
| 1043 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 1044 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); |
| 1045 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); |
| 1046 | BT_ASSERT_PRE(!graph->in_remove_listener, |
| 1047 | "Graph currently executing a \"listener removed\" listener: " |
| 1048 | "%!+g", graph); |
| 1049 | g_array_append_val(graph->listeners.filter_output_port_removed, listener); |
| 1050 | listener_id = graph->listeners.filter_output_port_removed->len - 1; |
| 1051 | BT_LIB_LOGV("Added \"filter component output port removed\" listener to graph: " |
| 1052 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
| 1053 | listener_id); |
| 1054 | |
| 1055 | if (listener_id) { |
| 1056 | *out_listener_id = listener_id; |
| 1057 | } |
| 1058 | |
| 1059 | return BT_GRAPH_STATUS_OK; |
| 1060 | } |
| 1061 | |
| 1062 | enum bt_graph_status |
| 1063 | bt_graph_add_filter_component_input_port_removed_listener( |
| 1064 | struct bt_graph *graph, |
| 1065 | bt_graph_filter_component_input_port_removed_listener_func func, |
| 1066 | bt_graph_listener_removed_func listener_removed, void *data, |
| 1067 | int *out_listener_id) |
| 1068 | { |
| 1069 | struct bt_graph_listener_port_removed listener = { |
| 1070 | .base = { |
| 1071 | .removed = listener_removed, |
| 1072 | .data = data, |
| 1073 | }, |
| 1074 | .func = (port_removed_func_t) func, |
| 1075 | }; |
| 1076 | int listener_id; |
| 1077 | |
| 1078 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 1079 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); |
| 1080 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); |
| 1081 | BT_ASSERT_PRE(!graph->in_remove_listener, |
| 1082 | "Graph currently executing a \"listener removed\" listener: " |
| 1083 | "%!+g", graph); |
| 1084 | g_array_append_val(graph->listeners.filter_input_port_removed, listener); |
| 1085 | listener_id = graph->listeners.filter_input_port_removed->len - 1; |
| 1086 | BT_LIB_LOGV("Added \"filter component input port removed\" listener to graph: " |
| 1087 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
| 1088 | listener_id); |
| 1089 | |
| 1090 | if (listener_id) { |
| 1091 | *out_listener_id = listener_id; |
| 1092 | } |
| 1093 | |
| 1094 | return BT_GRAPH_STATUS_OK; |
| 1095 | } |
| 1096 | |
| 1097 | enum bt_graph_status |
| 1098 | bt_graph_add_sink_component_input_port_removed_listener( |
| 1099 | struct bt_graph *graph, |
| 1100 | bt_graph_sink_component_input_port_removed_listener_func func, |
| 1101 | bt_graph_listener_removed_func listener_removed, void *data, |
| 1102 | int *out_listener_id) |
| 1103 | { |
| 1104 | struct bt_graph_listener_port_removed listener = { |
| 1105 | .base = { |
| 1106 | .removed = listener_removed, |
| 1107 | .data = data, |
| 1108 | }, |
| 1109 | .func = (port_removed_func_t) func, |
| 1110 | }; |
| 1111 | int listener_id; |
| 1112 | |
| 1113 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 1114 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); |
| 1115 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); |
| 1116 | BT_ASSERT_PRE(!graph->in_remove_listener, |
| 1117 | "Graph currently executing a \"listener removed\" listener: " |
| 1118 | "%!+g", graph); |
| 1119 | g_array_append_val(graph->listeners.sink_input_port_removed, listener); |
| 1120 | listener_id = graph->listeners.sink_input_port_removed->len - 1; |
| 1121 | BT_LIB_LOGV("Added \"sink component input port removed\" listener to graph: " |
| 1122 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
| 1123 | listener_id); |
| 1124 | |
| 1125 | if (listener_id) { |
| 1126 | *out_listener_id = listener_id; |
| 1127 | } |
| 1128 | |
| 1129 | return BT_GRAPH_STATUS_OK; |
| 1130 | } |
| 1131 | |
| 1132 | enum bt_graph_status |
| 1133 | bt_graph_add_source_filter_component_ports_connected_listener( |
| 1134 | struct bt_graph *graph, |
| 1135 | bt_graph_source_filter_component_ports_connected_listener_func func, |
| 1136 | bt_graph_listener_removed_func listener_removed, void *data, |
| 1137 | int *out_listener_id) |
| 1138 | { |
| 1139 | struct bt_graph_listener_ports_connected listener = { |
| 1140 | .base = { |
| 1141 | .removed = listener_removed, |
| 1142 | .data = data, |
| 1143 | }, |
| 1144 | .func = (ports_connected_func_t) func, |
| 1145 | }; |
| 1146 | int listener_id; |
| 1147 | |
| 1148 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 1149 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); |
| 1150 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); |
| 1151 | BT_ASSERT_PRE(!graph->in_remove_listener, |
| 1152 | "Graph currently executing a \"listener removed\" listener: " |
| 1153 | "%!+g", graph); |
| 1154 | g_array_append_val(graph->listeners.source_filter_ports_connected, |
| 1155 | listener); |
| 1156 | listener_id = graph->listeners.source_filter_ports_connected->len - 1; |
| 1157 | BT_LIB_LOGV("Added \"source to filter component ports connected\" listener to graph: " |
| 1158 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
| 1159 | listener_id); |
| 1160 | |
| 1161 | if (listener_id) { |
| 1162 | *out_listener_id = listener_id; |
| 1163 | } |
| 1164 | |
| 1165 | return BT_GRAPH_STATUS_OK; |
| 1166 | } |
| 1167 | |
| 1168 | enum bt_graph_status |
| 1169 | bt_graph_add_source_sink_component_ports_connected_listener( |
| 1170 | struct bt_graph *graph, |
| 1171 | bt_graph_source_sink_component_ports_connected_listener_func func, |
| 1172 | bt_graph_listener_removed_func listener_removed, void *data, |
| 1173 | int *out_listener_id) |
| 1174 | { |
| 1175 | struct bt_graph_listener_ports_connected listener = { |
| 1176 | .base = { |
| 1177 | .removed = listener_removed, |
| 1178 | .data = data, |
| 1179 | }, |
| 1180 | .func = (ports_connected_func_t) func, |
| 1181 | }; |
| 1182 | int listener_id; |
| 1183 | |
| 1184 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 1185 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); |
| 1186 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); |
| 1187 | BT_ASSERT_PRE(!graph->in_remove_listener, |
| 1188 | "Graph currently executing a \"listener removed\" listener: " |
| 1189 | "%!+g", graph); |
| 1190 | g_array_append_val(graph->listeners.source_sink_ports_connected, |
| 1191 | listener); |
| 1192 | listener_id = graph->listeners.source_sink_ports_connected->len - 1; |
| 1193 | BT_LIB_LOGV("Added \"source to sink component ports connected\" listener to graph: " |
| 1194 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
| 1195 | listener_id); |
| 1196 | |
| 1197 | if (listener_id) { |
| 1198 | *out_listener_id = listener_id; |
| 1199 | } |
| 1200 | |
| 1201 | return BT_GRAPH_STATUS_OK; |
| 1202 | } |
| 1203 | |
| 1204 | enum bt_graph_status |
| 1205 | bt_graph_add_filter_sink_component_ports_connected_listener( |
| 1206 | struct bt_graph *graph, |
| 1207 | bt_graph_filter_sink_component_ports_connected_listener_func func, |
| 1208 | bt_graph_listener_removed_func listener_removed, void *data, |
| 1209 | int *out_listener_id) |
| 1210 | { |
| 1211 | struct bt_graph_listener_ports_connected listener = { |
| 1212 | .base = { |
| 1213 | .removed = listener_removed, |
| 1214 | .data = data, |
| 1215 | }, |
| 1216 | .func = (ports_connected_func_t) func, |
| 1217 | }; |
| 1218 | int listener_id; |
| 1219 | |
| 1220 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 1221 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); |
| 1222 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); |
| 1223 | BT_ASSERT_PRE(!graph->in_remove_listener, |
| 1224 | "Graph currently executing a \"listener removed\" listener: " |
| 1225 | "%!+g", graph); |
| 1226 | g_array_append_val(graph->listeners.filter_sink_ports_connected, |
| 1227 | listener); |
| 1228 | listener_id = graph->listeners.filter_sink_ports_connected->len - 1; |
| 1229 | BT_LIB_LOGV("Added \"filter to sink component ports connected\" listener to graph: " |
| 1230 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
| 1231 | listener_id); |
| 1232 | |
| 1233 | if (listener_id) { |
| 1234 | *out_listener_id = listener_id; |
| 1235 | } |
| 1236 | |
| 1237 | return BT_GRAPH_STATUS_OK; |
| 1238 | } |
| 1239 | |
| 1240 | enum bt_graph_status |
| 1241 | bt_graph_add_source_filter_component_ports_disconnected_listener( |
| 1242 | struct bt_graph *graph, |
| 1243 | bt_graph_source_filter_component_ports_disconnected_listener_func func, |
| 1244 | bt_graph_listener_removed_func listener_removed, void *data, |
| 1245 | int *out_listener_id) |
| 1246 | { |
| 1247 | struct bt_graph_listener_ports_disconnected listener = { |
| 1248 | .base = { |
| 1249 | .removed = listener_removed, |
| 1250 | .data = data, |
| 1251 | }, |
| 1252 | .func = (ports_disconnected_func_t) func, |
| 1253 | }; |
| 1254 | int listener_id; |
| 1255 | |
| 1256 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 1257 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); |
| 1258 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); |
| 1259 | BT_ASSERT_PRE(!graph->in_remove_listener, |
| 1260 | "Graph currently executing a \"listener removed\" listener: " |
| 1261 | "%!+g", graph); |
| 1262 | g_array_append_val(graph->listeners.source_filter_ports_disconnected, |
| 1263 | listener); |
| 1264 | listener_id = graph->listeners.source_filter_ports_disconnected->len - 1; |
| 1265 | BT_LIB_LOGV("Added \"source to filter component ports disconnected\" listener to graph: " |
| 1266 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
| 1267 | listener_id); |
| 1268 | |
| 1269 | if (listener_id) { |
| 1270 | *out_listener_id = listener_id; |
| 1271 | } |
| 1272 | |
| 1273 | return BT_GRAPH_STATUS_OK; |
| 1274 | } |
| 1275 | |
| 1276 | enum bt_graph_status |
| 1277 | bt_graph_add_source_sink_component_ports_disconnected_listener( |
| 1278 | struct bt_graph *graph, |
| 1279 | bt_graph_source_sink_component_ports_disconnected_listener_func func, |
| 1280 | bt_graph_listener_removed_func listener_removed, void *data, |
| 1281 | int *out_listener_id) |
| 1282 | { |
| 1283 | struct bt_graph_listener_ports_disconnected listener = { |
| 1284 | .base = { |
| 1285 | .removed = listener_removed, |
| 1286 | .data = data, |
| 1287 | }, |
| 1288 | .func = (ports_disconnected_func_t) func, |
| 1289 | }; |
| 1290 | int listener_id; |
| 1291 | |
| 1292 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 1293 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); |
| 1294 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); |
| 1295 | BT_ASSERT_PRE(!graph->in_remove_listener, |
| 1296 | "Graph currently executing a \"listener removed\" listener: " |
| 1297 | "%!+g", graph); |
| 1298 | g_array_append_val(graph->listeners.source_sink_ports_disconnected, |
| 1299 | listener); |
| 1300 | listener_id = graph->listeners.source_sink_ports_disconnected->len - 1; |
| 1301 | BT_LIB_LOGV("Added \"source to sink component ports disconnected\" listener to graph: " |
| 1302 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
| 1303 | listener_id); |
| 1304 | |
| 1305 | if (listener_id) { |
| 1306 | *out_listener_id = listener_id; |
| 1307 | } |
| 1308 | |
| 1309 | return BT_GRAPH_STATUS_OK; |
| 1310 | } |
| 1311 | |
| 1312 | enum bt_graph_status |
| 1313 | bt_graph_add_filter_sink_component_ports_disconnected_listener( |
| 1314 | struct bt_graph *graph, |
| 1315 | bt_graph_filter_sink_component_ports_disconnected_listener_func func, |
| 1316 | bt_graph_listener_removed_func listener_removed, void *data, |
| 1317 | int *out_listener_id) |
| 1318 | { |
| 1319 | struct bt_graph_listener_ports_disconnected listener = { |
| 1320 | .base = { |
| 1321 | .removed = listener_removed, |
| 1322 | .data = data, |
| 1323 | }, |
| 1324 | .func = (ports_disconnected_func_t) func, |
| 1325 | }; |
| 1326 | int listener_id; |
| 1327 | |
| 1328 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 1329 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); |
| 1330 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); |
| 1331 | BT_ASSERT_PRE(!graph->in_remove_listener, |
| 1332 | "Graph currently executing a \"listener removed\" listener: " |
| 1333 | "%!+g", graph); |
| 1334 | g_array_append_val(graph->listeners.filter_sink_ports_disconnected, |
| 1335 | listener); |
| 1336 | listener_id = graph->listeners.filter_sink_ports_disconnected->len - 1; |
| 1337 | BT_LIB_LOGV("Added \"filter to sink component ports disconnected\" listener to graph: " |
| 1338 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
| 1339 | listener_id); |
| 1340 | |
| 1341 | if (listener_id) { |
| 1342 | *out_listener_id = listener_id; |
| 1343 | } |
| 1344 | |
| 1345 | return BT_GRAPH_STATUS_OK; |
| 1346 | } |
| 1347 | |
| 1348 | BT_HIDDEN |
| 1349 | void bt_graph_notify_port_added(struct bt_graph *graph, struct bt_port *port) |
| 1350 | { |
| 1351 | uint64_t i; |
| 1352 | GArray *listeners; |
| 1353 | struct bt_component *comp; |
| 1354 | |
| 1355 | BT_ASSERT(graph); |
| 1356 | BT_ASSERT(port); |
| 1357 | BT_LIB_LOGV("Notifying graph listeners that a port was added: " |
| 1358 | "%![graph-]+g, %![port-]+p", graph, port); |
| 1359 | comp = bt_port_borrow_component_inline(port); |
| 1360 | BT_ASSERT(comp); |
| 1361 | |
| 1362 | switch (comp->class->type) { |
| 1363 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 1364 | { |
| 1365 | switch (port->type) { |
| 1366 | case BT_PORT_TYPE_OUTPUT: |
| 1367 | listeners = graph->listeners.source_output_port_added; |
| 1368 | break; |
| 1369 | default: |
| 1370 | abort(); |
| 1371 | } |
| 1372 | |
| 1373 | break; |
| 1374 | } |
| 1375 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 1376 | { |
| 1377 | switch (port->type) { |
| 1378 | case BT_PORT_TYPE_INPUT: |
| 1379 | listeners = graph->listeners.filter_input_port_added; |
| 1380 | break; |
| 1381 | case BT_PORT_TYPE_OUTPUT: |
| 1382 | listeners = graph->listeners.filter_output_port_added; |
| 1383 | break; |
| 1384 | default: |
| 1385 | abort(); |
| 1386 | } |
| 1387 | |
| 1388 | break; |
| 1389 | } |
| 1390 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 1391 | { |
| 1392 | switch (port->type) { |
| 1393 | case BT_PORT_TYPE_INPUT: |
| 1394 | listeners = graph->listeners.sink_input_port_added; |
| 1395 | break; |
| 1396 | default: |
| 1397 | abort(); |
| 1398 | } |
| 1399 | |
| 1400 | break; |
| 1401 | } |
| 1402 | default: |
| 1403 | abort(); |
| 1404 | } |
| 1405 | |
| 1406 | for (i = 0; i < listeners->len; i++) { |
| 1407 | struct bt_graph_listener_port_added *listener = |
| 1408 | &g_array_index(listeners, |
| 1409 | struct bt_graph_listener_port_added, i); |
| 1410 | |
| 1411 | BT_ASSERT(listener->func); |
| 1412 | listener->func(comp, port, listener->base.data); |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | BT_HIDDEN |
| 1417 | void bt_graph_notify_port_removed(struct bt_graph *graph, |
| 1418 | struct bt_component *comp, struct bt_port *port) |
| 1419 | { |
| 1420 | uint64_t i; |
| 1421 | GArray *listeners; |
| 1422 | |
| 1423 | BT_ASSERT(graph); |
| 1424 | BT_ASSERT(port); |
| 1425 | BT_LIB_LOGV("Notifying graph listeners that a port was removed: " |
| 1426 | "%![graph-]+g, %![comp-]+c, %![port-]+p", graph, comp, port); |
| 1427 | |
| 1428 | switch (comp->class->type) { |
| 1429 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 1430 | { |
| 1431 | switch (port->type) { |
| 1432 | case BT_PORT_TYPE_OUTPUT: |
| 1433 | listeners = graph->listeners.source_output_port_removed; |
| 1434 | break; |
| 1435 | default: |
| 1436 | abort(); |
| 1437 | } |
| 1438 | |
| 1439 | break; |
| 1440 | } |
| 1441 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 1442 | { |
| 1443 | switch (port->type) { |
| 1444 | case BT_PORT_TYPE_INPUT: |
| 1445 | listeners = graph->listeners.filter_input_port_removed; |
| 1446 | break; |
| 1447 | case BT_PORT_TYPE_OUTPUT: |
| 1448 | listeners = graph->listeners.filter_output_port_removed; |
| 1449 | break; |
| 1450 | default: |
| 1451 | abort(); |
| 1452 | } |
| 1453 | |
| 1454 | break; |
| 1455 | } |
| 1456 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 1457 | { |
| 1458 | switch (port->type) { |
| 1459 | case BT_PORT_TYPE_INPUT: |
| 1460 | listeners = graph->listeners.sink_input_port_removed; |
| 1461 | break; |
| 1462 | default: |
| 1463 | abort(); |
| 1464 | } |
| 1465 | |
| 1466 | break; |
| 1467 | } |
| 1468 | default: |
| 1469 | abort(); |
| 1470 | } |
| 1471 | |
| 1472 | for (i = 0; i < listeners->len; i++) { |
| 1473 | struct bt_graph_listener_port_removed *listener = |
| 1474 | &g_array_index(listeners, |
| 1475 | struct bt_graph_listener_port_removed, i); |
| 1476 | |
| 1477 | BT_ASSERT(listener->func); |
| 1478 | listener->func(comp, port, listener->base.data); |
| 1479 | } |
| 1480 | } |
| 1481 | |
| 1482 | BT_HIDDEN |
| 1483 | void bt_graph_notify_ports_connected(struct bt_graph *graph, |
| 1484 | struct bt_port *upstream_port, struct bt_port *downstream_port) |
| 1485 | { |
| 1486 | uint64_t i; |
| 1487 | GArray *listeners; |
| 1488 | struct bt_component *upstream_comp; |
| 1489 | struct bt_component *downstream_comp; |
| 1490 | |
| 1491 | BT_ASSERT(graph); |
| 1492 | BT_ASSERT(upstream_port); |
| 1493 | BT_ASSERT(downstream_port); |
| 1494 | BT_LIB_LOGV("Notifying graph listeners that ports were connected: " |
| 1495 | "%![graph-]+g, %![up-port-]+p, %![down-port-]+p", |
| 1496 | graph, upstream_port, downstream_port); |
| 1497 | upstream_comp = bt_port_borrow_component_inline(upstream_port); |
| 1498 | BT_ASSERT(upstream_comp); |
| 1499 | downstream_comp = bt_port_borrow_component_inline(downstream_port); |
| 1500 | BT_ASSERT(downstream_comp); |
| 1501 | |
| 1502 | switch (upstream_comp->class->type) { |
| 1503 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 1504 | { |
| 1505 | switch (downstream_comp->class->type) { |
| 1506 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 1507 | listeners = |
| 1508 | graph->listeners.source_filter_ports_connected; |
| 1509 | break; |
| 1510 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 1511 | listeners = |
| 1512 | graph->listeners.source_sink_ports_connected; |
| 1513 | break; |
| 1514 | default: |
| 1515 | abort(); |
| 1516 | } |
| 1517 | |
| 1518 | break; |
| 1519 | } |
| 1520 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 1521 | { |
| 1522 | switch (downstream_comp->class->type) { |
| 1523 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 1524 | listeners = |
| 1525 | graph->listeners.filter_sink_ports_connected; |
| 1526 | break; |
| 1527 | default: |
| 1528 | abort(); |
| 1529 | } |
| 1530 | |
| 1531 | break; |
| 1532 | } |
| 1533 | default: |
| 1534 | abort(); |
| 1535 | } |
| 1536 | |
| 1537 | for (i = 0; i < listeners->len; i++) { |
| 1538 | struct bt_graph_listener_ports_connected *listener = |
| 1539 | &g_array_index(listeners, |
| 1540 | struct bt_graph_listener_ports_connected, i); |
| 1541 | |
| 1542 | BT_ASSERT(listener->func); |
| 1543 | listener->func(upstream_comp, downstream_comp, |
| 1544 | upstream_port, downstream_port, listener->base.data); |
| 1545 | } |
| 1546 | } |
| 1547 | |
| 1548 | BT_HIDDEN |
| 1549 | void bt_graph_notify_ports_disconnected(struct bt_graph *graph, |
| 1550 | struct bt_component *upstream_comp, |
| 1551 | struct bt_component *downstream_comp, |
| 1552 | struct bt_port *upstream_port, |
| 1553 | struct bt_port *downstream_port) |
| 1554 | { |
| 1555 | uint64_t i; |
| 1556 | GArray *listeners; |
| 1557 | |
| 1558 | BT_ASSERT(graph); |
| 1559 | BT_ASSERT(upstream_comp); |
| 1560 | BT_ASSERT(downstream_comp); |
| 1561 | BT_ASSERT(upstream_port); |
| 1562 | BT_ASSERT(downstream_port); |
| 1563 | BT_LIB_LOGV("Notifying graph listeners that ports were disconnected: " |
| 1564 | "%![graph-]+g, %![up-port-]+p, %![down-port-]+p, " |
| 1565 | "%![up-comp-]+c, %![down-comp-]+c", |
| 1566 | graph, upstream_port, downstream_port, upstream_comp, |
| 1567 | downstream_comp); |
| 1568 | |
| 1569 | switch (upstream_comp->class->type) { |
| 1570 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 1571 | { |
| 1572 | switch (downstream_comp->class->type) { |
| 1573 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 1574 | listeners = |
| 1575 | graph->listeners.source_filter_ports_disconnected; |
| 1576 | break; |
| 1577 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 1578 | listeners = |
| 1579 | graph->listeners.source_sink_ports_disconnected; |
| 1580 | break; |
| 1581 | default: |
| 1582 | abort(); |
| 1583 | } |
| 1584 | |
| 1585 | break; |
| 1586 | } |
| 1587 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 1588 | { |
| 1589 | switch (downstream_comp->class->type) { |
| 1590 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 1591 | listeners = |
| 1592 | graph->listeners.filter_sink_ports_disconnected; |
| 1593 | break; |
| 1594 | default: |
| 1595 | abort(); |
| 1596 | } |
| 1597 | |
| 1598 | break; |
| 1599 | } |
| 1600 | default: |
| 1601 | abort(); |
| 1602 | } |
| 1603 | |
| 1604 | for (i = 0; i < listeners->len; i++) { |
| 1605 | struct bt_graph_listener_ports_disconnected *listener = |
| 1606 | &g_array_index(listeners, |
| 1607 | struct bt_graph_listener_ports_disconnected, i); |
| 1608 | |
| 1609 | BT_ASSERT(listener->func); |
| 1610 | listener->func(upstream_comp, downstream_comp, |
| 1611 | upstream_port, downstream_port, listener->base.data); |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | enum bt_graph_status bt_graph_cancel( |
| 1616 | struct bt_graph *graph) |
| 1617 | { |
| 1618 | |
| 1619 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 1620 | graph->canceled = true; |
| 1621 | BT_LIB_LOGV("Canceled graph: %!+i", graph); |
| 1622 | return BT_GRAPH_STATUS_OK; |
| 1623 | } |
| 1624 | |
| 1625 | bt_bool bt_graph_is_canceled(const struct bt_graph *graph) |
| 1626 | { |
| 1627 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 1628 | return graph->canceled ? BT_TRUE : BT_FALSE; |
| 1629 | } |
| 1630 | |
| 1631 | BT_HIDDEN |
| 1632 | void bt_graph_remove_connection(struct bt_graph *graph, |
| 1633 | struct bt_connection *connection) |
| 1634 | { |
| 1635 | BT_ASSERT(graph); |
| 1636 | BT_ASSERT(connection); |
| 1637 | BT_LIB_LOGV("Removing graph's connection: %![graph-]+g, %![conn-]+x", |
| 1638 | graph, connection); |
| 1639 | g_ptr_array_remove(graph->connections, connection); |
| 1640 | } |
| 1641 | |
| 1642 | BT_ASSERT_PRE_FUNC |
| 1643 | static inline |
| 1644 | bool component_name_exists(struct bt_graph *graph, const char *name) |
| 1645 | { |
| 1646 | bool exists = false; |
| 1647 | uint64_t i; |
| 1648 | |
| 1649 | for (i = 0; i < graph->components->len; i++) { |
| 1650 | struct bt_component *other_comp = graph->components->pdata[i]; |
| 1651 | |
| 1652 | if (strcmp(name, bt_component_get_name(other_comp)) == 0) { |
| 1653 | BT_ASSERT_PRE_MSG("Another component with the same name already exists in the graph: " |
| 1654 | "%![other-comp-]+c, name=\"%s\"", |
| 1655 | other_comp, name); |
| 1656 | exists = true; |
| 1657 | goto end; |
| 1658 | } |
| 1659 | } |
| 1660 | |
| 1661 | end: |
| 1662 | return exists; |
| 1663 | } |
| 1664 | |
| 1665 | static |
| 1666 | enum bt_graph_status add_component_with_init_method_data( |
| 1667 | struct bt_graph *graph, |
| 1668 | struct bt_component_class *comp_cls, |
| 1669 | comp_init_method_t init_method, |
| 1670 | const char *name, const struct bt_value *params, |
| 1671 | void *init_method_data, struct bt_component **user_component) |
| 1672 | { |
| 1673 | enum bt_graph_status graph_status = BT_GRAPH_STATUS_OK; |
| 1674 | enum bt_self_component_status comp_status; |
| 1675 | struct bt_component *component = NULL; |
| 1676 | int ret; |
| 1677 | bool init_can_consume; |
| 1678 | struct bt_value *new_params = NULL; |
| 1679 | |
| 1680 | BT_ASSERT(comp_cls); |
| 1681 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 1682 | BT_ASSERT_PRE_NON_NULL(name, "Name"); |
| 1683 | BT_ASSERT_PRE(!graph->canceled, "Graph is canceled: %!+g", graph); |
| 1684 | BT_ASSERT_PRE(!component_name_exists(graph, name), |
| 1685 | "Duplicate component name: %!+g, name=\"%s\"", graph, name); |
| 1686 | BT_ASSERT_PRE(!params || bt_value_is_map(params), |
| 1687 | "Parameter value is not a map value: %!+v", params); |
| 1688 | init_can_consume = graph->can_consume; |
| 1689 | bt_graph_set_can_consume(graph, false); |
| 1690 | BT_LIB_LOGD("Adding component to graph: " |
| 1691 | "%![graph-]+g, %![cc-]+C, name=\"%s\", %![params-]+v, " |
| 1692 | "init-method-data-addr=%p", |
| 1693 | graph, comp_cls, name, params, init_method_data); |
| 1694 | |
| 1695 | if (!params) { |
| 1696 | new_params = bt_value_map_create(); |
| 1697 | if (!new_params) { |
| 1698 | BT_LOGE_STR("Cannot create map value object."); |
| 1699 | graph_status = BT_GRAPH_STATUS_NOMEM; |
| 1700 | goto end; |
| 1701 | } |
| 1702 | |
| 1703 | params = new_params; |
| 1704 | } |
| 1705 | |
| 1706 | ret = bt_component_create(comp_cls, name, &component); |
| 1707 | if (ret) { |
| 1708 | BT_LOGE("Cannot create empty component object: ret=%d", |
| 1709 | ret); |
| 1710 | graph_status = BT_GRAPH_STATUS_NOMEM; |
| 1711 | goto end; |
| 1712 | } |
| 1713 | |
| 1714 | /* |
| 1715 | * The user's initialization method needs to see that this |
| 1716 | * component is part of the graph. If the user method fails, we |
| 1717 | * immediately remove the component from the graph's components. |
| 1718 | */ |
| 1719 | g_ptr_array_add(graph->components, component); |
| 1720 | bt_component_set_graph(component, graph); |
| 1721 | |
| 1722 | if (init_method) { |
| 1723 | BT_LOGD_STR("Calling user's initialization method."); |
| 1724 | comp_status = init_method(component, params, init_method_data); |
| 1725 | BT_LOGD("User method returned: status=%s", |
| 1726 | bt_self_component_status_string(comp_status)); |
| 1727 | if (comp_status != BT_SELF_COMPONENT_STATUS_OK) { |
| 1728 | BT_LOGW_STR("Initialization method failed."); |
| 1729 | graph_status = (int) comp_status; |
| 1730 | bt_component_set_graph(component, NULL); |
| 1731 | g_ptr_array_remove_fast(graph->components, component); |
| 1732 | goto end; |
| 1733 | } |
| 1734 | } |
| 1735 | |
| 1736 | /* |
| 1737 | * Mark the component as initialized so that its finalization |
| 1738 | * method is called when it is destroyed. |
| 1739 | */ |
| 1740 | component->initialized = true; |
| 1741 | |
| 1742 | /* |
| 1743 | * If it's a sink component, it needs to be part of the graph's |
| 1744 | * sink queue to be consumed by bt_graph_consume(). |
| 1745 | */ |
| 1746 | if (bt_component_is_sink(component)) { |
| 1747 | graph->has_sink = true; |
| 1748 | g_queue_push_tail(graph->sinks_to_consume, component); |
| 1749 | } |
| 1750 | |
| 1751 | /* |
| 1752 | * Freeze the component class now that it's instantiated at |
| 1753 | * least once. |
| 1754 | */ |
| 1755 | BT_LOGD_STR("Freezing component class."); |
| 1756 | bt_component_class_freeze(comp_cls); |
| 1757 | BT_LIB_LOGD("Added component to graph: " |
| 1758 | "%![graph-]+g, %![cc-]+C, name=\"%s\", %![params-]+v, " |
| 1759 | "init-method-data-addr=%p, %![comp-]+c", |
| 1760 | graph, comp_cls, name, params, init_method_data, component); |
| 1761 | |
| 1762 | if (user_component) { |
| 1763 | /* Move reference to user */ |
| 1764 | *user_component = component; |
| 1765 | component = NULL; |
| 1766 | } |
| 1767 | |
| 1768 | end: |
| 1769 | bt_object_put_ref(component); |
| 1770 | bt_object_put_ref(new_params); |
| 1771 | (void) init_can_consume; |
| 1772 | bt_graph_set_can_consume(graph, init_can_consume); |
| 1773 | return graph_status; |
| 1774 | } |
| 1775 | |
| 1776 | enum bt_graph_status |
| 1777 | bt_graph_add_source_component_with_init_method_data( |
| 1778 | struct bt_graph *graph, |
| 1779 | const struct bt_component_class_source *comp_cls, |
| 1780 | const char *name, const struct bt_value *params, |
| 1781 | void *init_method_data, |
| 1782 | const struct bt_component_source **component) |
| 1783 | { |
| 1784 | BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class"); |
| 1785 | return add_component_with_init_method_data(graph, |
| 1786 | (void *) comp_cls, (comp_init_method_t) comp_cls->methods.init, |
| 1787 | name, params, init_method_data, (void *) component); |
| 1788 | } |
| 1789 | |
| 1790 | enum bt_graph_status bt_graph_add_source_component( |
| 1791 | struct bt_graph *graph, |
| 1792 | const struct bt_component_class_source *comp_cls, |
| 1793 | const char *name, const struct bt_value *params, |
| 1794 | const struct bt_component_source **component) |
| 1795 | { |
| 1796 | return bt_graph_add_source_component_with_init_method_data( |
| 1797 | graph, comp_cls, name, params, NULL, component); |
| 1798 | } |
| 1799 | |
| 1800 | enum bt_graph_status |
| 1801 | bt_graph_add_filter_component_with_init_method_data( |
| 1802 | struct bt_graph *graph, |
| 1803 | const struct bt_component_class_filter *comp_cls, |
| 1804 | const char *name, const struct bt_value *params, |
| 1805 | void *init_method_data, |
| 1806 | const struct bt_component_filter **component) |
| 1807 | { |
| 1808 | BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class"); |
| 1809 | return add_component_with_init_method_data(graph, |
| 1810 | (void *) comp_cls, (comp_init_method_t) comp_cls->methods.init, |
| 1811 | name, params, init_method_data, (void *) component); |
| 1812 | } |
| 1813 | |
| 1814 | enum bt_graph_status bt_graph_add_filter_component( |
| 1815 | struct bt_graph *graph, |
| 1816 | const struct bt_component_class_filter *comp_cls, |
| 1817 | const char *name, const struct bt_value *params, |
| 1818 | const struct bt_component_filter **component) |
| 1819 | { |
| 1820 | return bt_graph_add_filter_component_with_init_method_data( |
| 1821 | graph, comp_cls, name, params, NULL, component); |
| 1822 | } |
| 1823 | |
| 1824 | enum bt_graph_status |
| 1825 | bt_graph_add_sink_component_with_init_method_data( |
| 1826 | struct bt_graph *graph, |
| 1827 | const struct bt_component_class_sink *comp_cls, |
| 1828 | const char *name, const struct bt_value *params, |
| 1829 | void *init_method_data, |
| 1830 | const struct bt_component_sink **component) |
| 1831 | { |
| 1832 | BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class"); |
| 1833 | return add_component_with_init_method_data(graph, |
| 1834 | (void *) comp_cls, (comp_init_method_t) comp_cls->methods.init, |
| 1835 | name, params, init_method_data, (void *) component); |
| 1836 | } |
| 1837 | |
| 1838 | enum bt_graph_status bt_graph_add_sink_component( |
| 1839 | struct bt_graph *graph, |
| 1840 | const struct bt_component_class_sink *comp_cls, |
| 1841 | const char *name, const struct bt_value *params, |
| 1842 | const struct bt_component_sink **component) |
| 1843 | { |
| 1844 | return bt_graph_add_sink_component_with_init_method_data( |
| 1845 | graph, comp_cls, name, params, NULL, component); |
| 1846 | } |
| 1847 | |
| 1848 | BT_HIDDEN |
| 1849 | int bt_graph_remove_unconnected_component(struct bt_graph *graph, |
| 1850 | struct bt_component *component) |
| 1851 | { |
| 1852 | bool init_can_consume; |
| 1853 | uint64_t count; |
| 1854 | uint64_t i; |
| 1855 | int ret = 0; |
| 1856 | |
| 1857 | BT_ASSERT(graph); |
| 1858 | BT_ASSERT(component); |
| 1859 | BT_ASSERT(component->base.ref_count == 0); |
| 1860 | BT_ASSERT(bt_component_borrow_graph(component) == graph); |
| 1861 | |
| 1862 | init_can_consume = graph->can_consume; |
| 1863 | count = bt_component_get_input_port_count(component); |
| 1864 | |
| 1865 | for (i = 0; i < count; i++) { |
| 1866 | struct bt_port *port = (void *) |
| 1867 | bt_component_borrow_input_port_by_index(component, i); |
| 1868 | |
| 1869 | BT_ASSERT(port); |
| 1870 | |
| 1871 | if (bt_port_is_connected(port)) { |
| 1872 | BT_LIB_LOGW("Cannot remove component from graph: " |
| 1873 | "an input port is connected: " |
| 1874 | "%![graph-]+g, %![comp-]+c, %![port-]+p", |
| 1875 | graph, component, port); |
| 1876 | goto error; |
| 1877 | } |
| 1878 | } |
| 1879 | |
| 1880 | count = bt_component_get_output_port_count(component); |
| 1881 | |
| 1882 | for (i = 0; i < count; i++) { |
| 1883 | struct bt_port *port = (void *) |
| 1884 | bt_component_borrow_output_port_by_index(component, i); |
| 1885 | |
| 1886 | BT_ASSERT(port); |
| 1887 | |
| 1888 | if (bt_port_is_connected(port)) { |
| 1889 | BT_LIB_LOGW("Cannot remove component from graph: " |
| 1890 | "an output port is connected: " |
| 1891 | "%![graph-]+g, %![comp-]+c, %![port-]+p", |
| 1892 | graph, component, port); |
| 1893 | goto error; |
| 1894 | } |
| 1895 | } |
| 1896 | |
| 1897 | bt_graph_set_can_consume(graph, false); |
| 1898 | |
| 1899 | /* Possibly remove from sinks to consume */ |
| 1900 | (void) g_queue_remove(graph->sinks_to_consume, component); |
| 1901 | |
| 1902 | if (graph->sinks_to_consume->length == 0) { |
| 1903 | graph->has_sink = false; |
| 1904 | } |
| 1905 | |
| 1906 | /* |
| 1907 | * This calls bt_object_try_spec_release() on the component, and |
| 1908 | * since its reference count is 0, its destructor is called. Its |
| 1909 | * destructor calls the user's finalization method (if set). |
| 1910 | */ |
| 1911 | g_ptr_array_remove(graph->components, component); |
| 1912 | goto end; |
| 1913 | |
| 1914 | error: |
| 1915 | ret = -1; |
| 1916 | |
| 1917 | end: |
| 1918 | (void) init_can_consume; |
| 1919 | bt_graph_set_can_consume(graph, init_can_consume); |
| 1920 | return ret; |
| 1921 | } |
| 1922 | |
| 1923 | BT_HIDDEN |
| 1924 | void bt_graph_add_notification(struct bt_graph *graph, |
| 1925 | struct bt_notification *notif) |
| 1926 | { |
| 1927 | BT_ASSERT(graph); |
| 1928 | BT_ASSERT(notif); |
| 1929 | |
| 1930 | /* |
| 1931 | * It's okay not to take a reference because, when a |
| 1932 | * notification's reference count drops to 0, either: |
| 1933 | * |
| 1934 | * * It is recycled back to one of this graph's pool. |
| 1935 | * * It is destroyed because it doesn't have any link to any |
| 1936 | * graph, which means the original graph is already destroyed. |
| 1937 | */ |
| 1938 | g_ptr_array_add(graph->notifications, notif); |
| 1939 | } |