| 1 | /* |
| 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
| 3 | * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | * of this software and associated documentation files (the "Software"), to deal |
| 7 | * in the Software without restriction, including without limitation the rights |
| 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | * copies of the Software, and to permit persons to whom the Software is |
| 10 | * furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be included in |
| 13 | * all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | * SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #define BT_LOG_TAG "LIB/MSG-ITER" |
| 25 | #include "lib/logging.h" |
| 26 | |
| 27 | #include "compat/compiler.h" |
| 28 | #include "compat/glib.h" |
| 29 | #include "lib/trace-ir/clock-class.h" |
| 30 | #include "lib/trace-ir/clock-snapshot.h" |
| 31 | #include <babeltrace2/trace-ir/field.h> |
| 32 | #include <babeltrace2/trace-ir/event-const.h> |
| 33 | #include "lib/trace-ir/event.h" |
| 34 | #include <babeltrace2/trace-ir/packet-const.h> |
| 35 | #include "lib/trace-ir/packet.h" |
| 36 | #include "lib/trace-ir/stream.h" |
| 37 | #include <babeltrace2/trace-ir/clock-class-const.h> |
| 38 | #include <babeltrace2/trace-ir/stream-class-const.h> |
| 39 | #include <babeltrace2/trace-ir/stream-const.h> |
| 40 | #include <babeltrace2/graph/connection-const.h> |
| 41 | #include <babeltrace2/graph/component-const.h> |
| 42 | #include <babeltrace2/graph/component-sink-const.h> |
| 43 | #include <babeltrace2/graph/message-const.h> |
| 44 | #include <babeltrace2/graph/message-iterator.h> |
| 45 | #include <babeltrace2/graph/self-component-port-input-message-iterator.h> |
| 46 | #include <babeltrace2/graph/port-output-message-iterator.h> |
| 47 | #include <babeltrace2/graph/message-event-const.h> |
| 48 | #include <babeltrace2/graph/message-message-iterator-inactivity-const.h> |
| 49 | #include <babeltrace2/graph/message-packet-beginning.h> |
| 50 | #include <babeltrace2/graph/message-packet-beginning-const.h> |
| 51 | #include <babeltrace2/graph/message-packet-end-const.h> |
| 52 | #include <babeltrace2/graph/message-stream-beginning.h> |
| 53 | #include <babeltrace2/graph/message-stream-beginning-const.h> |
| 54 | #include <babeltrace2/graph/message-stream-end-const.h> |
| 55 | #include <babeltrace2/graph/port-const.h> |
| 56 | #include <babeltrace2/graph/graph.h> |
| 57 | #include <babeltrace2/graph/graph-const.h> |
| 58 | #include <babeltrace2/types.h> |
| 59 | #include "common/assert.h" |
| 60 | #include "lib/assert-pre.h" |
| 61 | #include "lib/assert-post.h" |
| 62 | #include <stdint.h> |
| 63 | #include <inttypes.h> |
| 64 | #include <stdlib.h> |
| 65 | |
| 66 | #include "component-class.h" |
| 67 | #include "component-class-sink-colander.h" |
| 68 | #include "component.h" |
| 69 | #include "component-sink.h" |
| 70 | #include "component-source.h" |
| 71 | #include "connection.h" |
| 72 | #include "graph.h" |
| 73 | #include "message/discarded-items.h" |
| 74 | #include "message/event.h" |
| 75 | #include "message/iterator.h" |
| 76 | #include "message/message.h" |
| 77 | #include "message/message-iterator-inactivity.h" |
| 78 | #include "message/stream.h" |
| 79 | #include "message/packet.h" |
| 80 | #include "lib/func-status.h" |
| 81 | |
| 82 | /* |
| 83 | * TODO: Use graph's state (number of active iterators, etc.) and |
| 84 | * possibly system specifications to make a better guess than this. |
| 85 | */ |
| 86 | #define MSG_BATCH_SIZE 15 |
| 87 | |
| 88 | #define BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(_iter) \ |
| 89 | BT_ASSERT_PRE((_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE || \ |
| 90 | (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED || \ |
| 91 | (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN || \ |
| 92 | (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR, \ |
| 93 | "Message iterator is in the wrong state: %!+i", _iter) |
| 94 | |
| 95 | static inline |
| 96 | void set_self_comp_port_input_msg_iterator_state( |
| 97 | struct bt_self_component_port_input_message_iterator *iterator, |
| 98 | enum bt_self_component_port_input_message_iterator_state state) |
| 99 | { |
| 100 | BT_ASSERT(iterator); |
| 101 | BT_LIB_LOGD("Updating message iterator's state: new-state=%s", |
| 102 | bt_self_component_port_input_message_iterator_state_string(state)); |
| 103 | iterator->state = state; |
| 104 | } |
| 105 | |
| 106 | static |
| 107 | void destroy_base_message_iterator(struct bt_object *obj) |
| 108 | { |
| 109 | struct bt_message_iterator *iterator = (void *) obj; |
| 110 | |
| 111 | BT_ASSERT(iterator); |
| 112 | |
| 113 | if (iterator->msgs) { |
| 114 | g_ptr_array_free(iterator->msgs, TRUE); |
| 115 | iterator->msgs = NULL; |
| 116 | } |
| 117 | |
| 118 | g_free(iterator); |
| 119 | } |
| 120 | |
| 121 | static |
| 122 | void bt_self_component_port_input_message_iterator_destroy(struct bt_object *obj) |
| 123 | { |
| 124 | struct bt_self_component_port_input_message_iterator *iterator; |
| 125 | |
| 126 | BT_ASSERT(obj); |
| 127 | |
| 128 | /* |
| 129 | * The message iterator's reference count is 0 if we're |
| 130 | * here. Increment it to avoid a double-destroy (possibly |
| 131 | * infinitely recursive). This could happen for example if the |
| 132 | * message iterator's finalization function does |
| 133 | * bt_object_get_ref() (or anything that causes |
| 134 | * bt_object_get_ref() to be called) on itself (ref. count goes |
| 135 | * from 0 to 1), and then bt_object_put_ref(): the reference |
| 136 | * count would go from 1 to 0 again and this function would be |
| 137 | * called again. |
| 138 | */ |
| 139 | obj->ref_count++; |
| 140 | iterator = (void *) obj; |
| 141 | BT_LIB_LOGI("Destroying self component input port message iterator object: " |
| 142 | "%!+i", iterator); |
| 143 | bt_self_component_port_input_message_iterator_try_finalize(iterator); |
| 144 | |
| 145 | if (iterator->connection) { |
| 146 | /* |
| 147 | * Remove ourself from the originating connection so |
| 148 | * that it does not try to finalize a dangling pointer |
| 149 | * later. |
| 150 | */ |
| 151 | bt_connection_remove_iterator(iterator->connection, iterator); |
| 152 | iterator->connection = NULL; |
| 153 | } |
| 154 | |
| 155 | if (iterator->auto_seek.msgs) { |
| 156 | while (!g_queue_is_empty(iterator->auto_seek.msgs)) { |
| 157 | bt_object_put_no_null_check( |
| 158 | g_queue_pop_tail(iterator->auto_seek.msgs)); |
| 159 | } |
| 160 | |
| 161 | g_queue_free(iterator->auto_seek.msgs); |
| 162 | iterator->auto_seek.msgs = NULL; |
| 163 | } |
| 164 | |
| 165 | if (iterator->upstream_msg_iters) { |
| 166 | /* |
| 167 | * At this point the message iterator is finalized, so |
| 168 | * it's detached from any upstream message iterator. |
| 169 | */ |
| 170 | BT_ASSERT(iterator->upstream_msg_iters->len == 0); |
| 171 | g_ptr_array_free(iterator->upstream_msg_iters, TRUE); |
| 172 | iterator->upstream_msg_iters = NULL; |
| 173 | } |
| 174 | |
| 175 | destroy_base_message_iterator(obj); |
| 176 | } |
| 177 | |
| 178 | BT_HIDDEN |
| 179 | void bt_self_component_port_input_message_iterator_try_finalize( |
| 180 | struct bt_self_component_port_input_message_iterator *iterator) |
| 181 | { |
| 182 | uint64_t i; |
| 183 | typedef void (*method_t)(void *); |
| 184 | |
| 185 | struct bt_component_class *comp_class = NULL; |
| 186 | method_t method = NULL; |
| 187 | |
| 188 | BT_ASSERT(iterator); |
| 189 | |
| 190 | switch (iterator->state) { |
| 191 | case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED: |
| 192 | /* Skip user finalization if user initialization failed */ |
| 193 | BT_LIB_LOGD("Not finalizing non-initialized message iterator: " |
| 194 | "%!+i", iterator); |
| 195 | goto end; |
| 196 | case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED: |
| 197 | /* Already finalized */ |
| 198 | BT_LIB_LOGD("Not finalizing message iterator: already finalized: " |
| 199 | "%!+i", iterator); |
| 200 | goto end; |
| 201 | case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING: |
| 202 | /* Finalizing */ |
| 203 | BT_LIB_LOGF("Message iterator is already being finalized: " |
| 204 | "%!+i", iterator); |
| 205 | abort(); |
| 206 | default: |
| 207 | break; |
| 208 | } |
| 209 | |
| 210 | BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator); |
| 211 | set_self_comp_port_input_msg_iterator_state(iterator, |
| 212 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING); |
| 213 | BT_ASSERT(iterator->upstream_component); |
| 214 | comp_class = iterator->upstream_component->class; |
| 215 | |
| 216 | /* Call user-defined destroy method */ |
| 217 | switch (comp_class->type) { |
| 218 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 219 | { |
| 220 | struct bt_component_class_source *src_comp_cls = |
| 221 | (void *) comp_class; |
| 222 | |
| 223 | method = (method_t) src_comp_cls->methods.msg_iter_finalize; |
| 224 | break; |
| 225 | } |
| 226 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 227 | { |
| 228 | struct bt_component_class_filter *flt_comp_cls = |
| 229 | (void *) comp_class; |
| 230 | |
| 231 | method = (method_t) flt_comp_cls->methods.msg_iter_finalize; |
| 232 | break; |
| 233 | } |
| 234 | default: |
| 235 | /* Unreachable */ |
| 236 | abort(); |
| 237 | } |
| 238 | |
| 239 | if (method) { |
| 240 | BT_LIB_LOGD("Calling user's finalization method: %!+i", |
| 241 | iterator); |
| 242 | method(iterator); |
| 243 | } |
| 244 | |
| 245 | /* Detach upstream message iterators */ |
| 246 | for (i = 0; i < iterator->upstream_msg_iters->len; i++) { |
| 247 | struct bt_self_component_port_input_message_iterator *upstream_msg_iter = |
| 248 | iterator->upstream_msg_iters->pdata[i]; |
| 249 | |
| 250 | upstream_msg_iter->downstream_msg_iter = NULL; |
| 251 | } |
| 252 | |
| 253 | g_ptr_array_set_size(iterator->upstream_msg_iters, 0); |
| 254 | |
| 255 | /* Detach downstream message iterator */ |
| 256 | if (iterator->downstream_msg_iter) { |
| 257 | gboolean existed; |
| 258 | |
| 259 | BT_ASSERT(iterator->downstream_msg_iter->upstream_msg_iters); |
| 260 | existed = g_ptr_array_remove_fast( |
| 261 | iterator->downstream_msg_iter->upstream_msg_iters, |
| 262 | iterator); |
| 263 | BT_ASSERT(existed); |
| 264 | } |
| 265 | |
| 266 | iterator->upstream_component = NULL; |
| 267 | iterator->upstream_port = NULL; |
| 268 | set_self_comp_port_input_msg_iterator_state(iterator, |
| 269 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED); |
| 270 | BT_LIB_LOGD("Finalized message iterator: %!+i", iterator); |
| 271 | |
| 272 | end: |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | BT_HIDDEN |
| 277 | void bt_self_component_port_input_message_iterator_set_connection( |
| 278 | struct bt_self_component_port_input_message_iterator *iterator, |
| 279 | struct bt_connection *connection) |
| 280 | { |
| 281 | BT_ASSERT(iterator); |
| 282 | iterator->connection = connection; |
| 283 | BT_LIB_LOGI("Set message iterator's connection: " |
| 284 | "%![iter-]+i, %![conn-]+x", iterator, connection); |
| 285 | } |
| 286 | |
| 287 | static |
| 288 | int init_message_iterator(struct bt_message_iterator *iterator, |
| 289 | enum bt_message_iterator_type type, |
| 290 | bt_object_release_func destroy) |
| 291 | { |
| 292 | int ret = 0; |
| 293 | |
| 294 | bt_object_init_shared(&iterator->base, destroy); |
| 295 | iterator->type = type; |
| 296 | iterator->msgs = g_ptr_array_new(); |
| 297 | if (!iterator->msgs) { |
| 298 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray."); |
| 299 | ret = -1; |
| 300 | goto end; |
| 301 | } |
| 302 | |
| 303 | g_ptr_array_set_size(iterator->msgs, MSG_BATCH_SIZE); |
| 304 | |
| 305 | end: |
| 306 | return ret; |
| 307 | } |
| 308 | |
| 309 | static |
| 310 | bt_bool can_seek_ns_from_origin_true( |
| 311 | struct bt_self_component_port_input_message_iterator *iterator, |
| 312 | int64_t ns_from_origin) |
| 313 | { |
| 314 | return BT_TRUE; |
| 315 | } |
| 316 | |
| 317 | static |
| 318 | bt_bool can_seek_beginning_true( |
| 319 | struct bt_self_component_port_input_message_iterator *iterator) |
| 320 | { |
| 321 | return BT_TRUE; |
| 322 | } |
| 323 | |
| 324 | static |
| 325 | struct bt_self_component_port_input_message_iterator * |
| 326 | create_self_component_input_port_message_iterator( |
| 327 | struct bt_self_message_iterator *self_downstream_msg_iter, |
| 328 | struct bt_self_component_port_input *self_port) |
| 329 | { |
| 330 | typedef enum bt_component_class_message_iterator_init_method_status (*init_method_t)( |
| 331 | void *, void *, void *); |
| 332 | |
| 333 | int ret; |
| 334 | init_method_t init_method = NULL; |
| 335 | struct bt_self_component_port_input_message_iterator *iterator = |
| 336 | NULL; |
| 337 | struct bt_self_component_port_input_message_iterator *downstream_msg_iter = |
| 338 | (void *) self_downstream_msg_iter; |
| 339 | struct bt_port *port = (void *) self_port; |
| 340 | struct bt_port *upstream_port; |
| 341 | struct bt_component *comp; |
| 342 | struct bt_component *upstream_comp; |
| 343 | struct bt_component_class *upstream_comp_cls; |
| 344 | |
| 345 | BT_ASSERT_PRE_NON_NULL(port, "Input port"); |
| 346 | comp = bt_port_borrow_component_inline(port); |
| 347 | BT_ASSERT_PRE(bt_port_is_connected(port), |
| 348 | "Input port is not connected: %![port-]+p", port); |
| 349 | BT_ASSERT_PRE(comp, "Input port is not part of a component: %![port-]+p", |
| 350 | port); |
| 351 | BT_ASSERT_PRE(!bt_component_graph_is_canceled(comp), |
| 352 | "Input port's component's graph is canceled: " |
| 353 | "%![port-]+p, %![comp-]+c", port, comp); |
| 354 | BT_ASSERT(port->connection); |
| 355 | upstream_port = port->connection->upstream_port; |
| 356 | BT_ASSERT(upstream_port); |
| 357 | upstream_comp = bt_port_borrow_component_inline(upstream_port); |
| 358 | BT_ASSERT(upstream_comp); |
| 359 | BT_ASSERT_PRE( |
| 360 | bt_component_borrow_graph(upstream_comp)->config_state != |
| 361 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, |
| 362 | "Graph is not configured: %!+g", |
| 363 | bt_component_borrow_graph(upstream_comp)); |
| 364 | upstream_comp_cls = upstream_comp->class; |
| 365 | BT_ASSERT(upstream_comp->class->type == |
| 366 | BT_COMPONENT_CLASS_TYPE_SOURCE || |
| 367 | upstream_comp->class->type == |
| 368 | BT_COMPONENT_CLASS_TYPE_FILTER); |
| 369 | BT_LIB_LOGI("Creating message iterator on self component input port: " |
| 370 | "%![up-comp-]+c, %![up-port-]+p", upstream_comp, upstream_port); |
| 371 | iterator = g_new0( |
| 372 | struct bt_self_component_port_input_message_iterator, 1); |
| 373 | if (!iterator) { |
| 374 | BT_LIB_LOGE_APPEND_CAUSE( |
| 375 | "Failed to allocate one self component input port " |
| 376 | "message iterator."); |
| 377 | goto error; |
| 378 | } |
| 379 | |
| 380 | ret = init_message_iterator((void *) iterator, |
| 381 | BT_MESSAGE_ITERATOR_TYPE_SELF_COMPONENT_PORT_INPUT, |
| 382 | bt_self_component_port_input_message_iterator_destroy); |
| 383 | if (ret) { |
| 384 | /* init_message_iterator() logs errors */ |
| 385 | goto error; |
| 386 | } |
| 387 | |
| 388 | iterator->last_ns_from_origin = INT64_MIN; |
| 389 | iterator->auto_seek.msgs = g_queue_new(); |
| 390 | if (!iterator->auto_seek.msgs) { |
| 391 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue."); |
| 392 | goto error; |
| 393 | } |
| 394 | |
| 395 | iterator->upstream_msg_iters = g_ptr_array_new(); |
| 396 | if (!iterator->upstream_msg_iters) { |
| 397 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray."); |
| 398 | goto error; |
| 399 | } |
| 400 | |
| 401 | iterator->upstream_component = upstream_comp; |
| 402 | iterator->upstream_port = upstream_port; |
| 403 | iterator->connection = iterator->upstream_port->connection; |
| 404 | iterator->graph = bt_component_borrow_graph(upstream_comp); |
| 405 | set_self_comp_port_input_msg_iterator_state(iterator, |
| 406 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED); |
| 407 | |
| 408 | switch (iterator->upstream_component->class->type) { |
| 409 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 410 | { |
| 411 | struct bt_component_class_source *src_comp_cls = |
| 412 | (void *) iterator->upstream_component->class; |
| 413 | |
| 414 | iterator->methods.next = |
| 415 | (bt_self_component_port_input_message_iterator_next_method) |
| 416 | src_comp_cls->methods.msg_iter_next; |
| 417 | iterator->methods.seek_ns_from_origin = |
| 418 | (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method) |
| 419 | src_comp_cls->methods.msg_iter_seek_ns_from_origin; |
| 420 | iterator->methods.seek_beginning = |
| 421 | (bt_self_component_port_input_message_iterator_seek_beginning_method) |
| 422 | src_comp_cls->methods.msg_iter_seek_beginning; |
| 423 | iterator->methods.can_seek_ns_from_origin = |
| 424 | (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method) |
| 425 | src_comp_cls->methods.msg_iter_can_seek_ns_from_origin; |
| 426 | iterator->methods.can_seek_beginning = |
| 427 | (bt_self_component_port_input_message_iterator_can_seek_beginning_method) |
| 428 | src_comp_cls->methods.msg_iter_can_seek_beginning; |
| 429 | break; |
| 430 | } |
| 431 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 432 | { |
| 433 | struct bt_component_class_filter *flt_comp_cls = |
| 434 | (void *) iterator->upstream_component->class; |
| 435 | |
| 436 | iterator->methods.next = |
| 437 | (bt_self_component_port_input_message_iterator_next_method) |
| 438 | flt_comp_cls->methods.msg_iter_next; |
| 439 | iterator->methods.seek_ns_from_origin = |
| 440 | (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method) |
| 441 | flt_comp_cls->methods.msg_iter_seek_ns_from_origin; |
| 442 | iterator->methods.seek_beginning = |
| 443 | (bt_self_component_port_input_message_iterator_seek_beginning_method) |
| 444 | flt_comp_cls->methods.msg_iter_seek_beginning; |
| 445 | iterator->methods.can_seek_ns_from_origin = |
| 446 | (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method) |
| 447 | flt_comp_cls->methods.msg_iter_can_seek_ns_from_origin; |
| 448 | iterator->methods.can_seek_beginning = |
| 449 | (bt_self_component_port_input_message_iterator_can_seek_beginning_method) |
| 450 | flt_comp_cls->methods.msg_iter_can_seek_beginning; |
| 451 | break; |
| 452 | } |
| 453 | default: |
| 454 | abort(); |
| 455 | } |
| 456 | |
| 457 | if (iterator->methods.seek_ns_from_origin && |
| 458 | !iterator->methods.can_seek_ns_from_origin) { |
| 459 | iterator->methods.can_seek_ns_from_origin = |
| 460 | (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method) |
| 461 | can_seek_ns_from_origin_true; |
| 462 | } |
| 463 | |
| 464 | if (iterator->methods.seek_beginning && |
| 465 | !iterator->methods.can_seek_beginning) { |
| 466 | iterator->methods.can_seek_beginning = |
| 467 | (bt_self_component_port_input_message_iterator_seek_beginning_method) |
| 468 | can_seek_beginning_true; |
| 469 | } |
| 470 | |
| 471 | switch (upstream_comp_cls->type) { |
| 472 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 473 | { |
| 474 | struct bt_component_class_source *src_comp_cls = |
| 475 | (void *) upstream_comp_cls; |
| 476 | |
| 477 | init_method = |
| 478 | (init_method_t) src_comp_cls->methods.msg_iter_init; |
| 479 | break; |
| 480 | } |
| 481 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 482 | { |
| 483 | struct bt_component_class_filter *flt_comp_cls = |
| 484 | (void *) upstream_comp_cls; |
| 485 | |
| 486 | init_method = |
| 487 | (init_method_t) flt_comp_cls->methods.msg_iter_init; |
| 488 | break; |
| 489 | } |
| 490 | default: |
| 491 | /* Unreachable */ |
| 492 | abort(); |
| 493 | } |
| 494 | |
| 495 | if (init_method) { |
| 496 | enum bt_component_class_message_iterator_init_method_status iter_status; |
| 497 | |
| 498 | BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator); |
| 499 | iter_status = init_method(iterator, upstream_comp, |
| 500 | upstream_port); |
| 501 | BT_LOGD("User method returned: status=%s", |
| 502 | bt_common_func_status_string(iter_status)); |
| 503 | if (iter_status != BT_FUNC_STATUS_OK) { |
| 504 | BT_LIB_LOGW_APPEND_CAUSE( |
| 505 | "Component input port message iterator initialization method failed: " |
| 506 | "%![iter-]+i, status=%s", |
| 507 | iterator, |
| 508 | bt_common_func_status_string(iter_status)); |
| 509 | goto error; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | if (downstream_msg_iter) { |
| 514 | /* Set this message iterator's downstream message iterator */ |
| 515 | iterator->downstream_msg_iter = downstream_msg_iter; |
| 516 | |
| 517 | /* |
| 518 | * Add this message iterator to the downstream message |
| 519 | * iterator's array of upstream message iterators. |
| 520 | */ |
| 521 | g_ptr_array_add(downstream_msg_iter->upstream_msg_iters, |
| 522 | iterator); |
| 523 | } |
| 524 | |
| 525 | set_self_comp_port_input_msg_iterator_state(iterator, |
| 526 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE); |
| 527 | g_ptr_array_add(port->connection->iterators, iterator); |
| 528 | BT_LIB_LOGI("Created message iterator on self component input port: " |
| 529 | "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i", |
| 530 | upstream_port, upstream_comp, iterator); |
| 531 | goto end; |
| 532 | |
| 533 | error: |
| 534 | BT_OBJECT_PUT_REF_AND_RESET(iterator); |
| 535 | |
| 536 | end: |
| 537 | return iterator; |
| 538 | } |
| 539 | |
| 540 | struct bt_self_component_port_input_message_iterator * |
| 541 | bt_self_component_port_input_message_iterator_create_from_message_iterator( |
| 542 | struct bt_self_message_iterator *self_msg_iter, |
| 543 | struct bt_self_component_port_input *input_port) |
| 544 | { |
| 545 | BT_ASSERT_PRE_NON_NULL(self_msg_iter, "Message iterator"); |
| 546 | return create_self_component_input_port_message_iterator(self_msg_iter, |
| 547 | input_port); |
| 548 | } |
| 549 | |
| 550 | struct bt_self_component_port_input_message_iterator * |
| 551 | bt_self_component_port_input_message_iterator_create_from_sink_component( |
| 552 | struct bt_self_component_sink *self_comp, |
| 553 | struct bt_self_component_port_input *input_port) |
| 554 | { |
| 555 | BT_ASSERT_PRE_NON_NULL(self_comp, "Sink component"); |
| 556 | return create_self_component_input_port_message_iterator(NULL, |
| 557 | input_port); |
| 558 | } |
| 559 | |
| 560 | void *bt_self_message_iterator_get_data( |
| 561 | const struct bt_self_message_iterator *self_iterator) |
| 562 | { |
| 563 | struct bt_self_component_port_input_message_iterator *iterator = |
| 564 | (void *) self_iterator; |
| 565 | |
| 566 | BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator"); |
| 567 | return iterator->user_data; |
| 568 | } |
| 569 | |
| 570 | void bt_self_message_iterator_set_data( |
| 571 | struct bt_self_message_iterator *self_iterator, void *data) |
| 572 | { |
| 573 | struct bt_self_component_port_input_message_iterator *iterator = |
| 574 | (void *) self_iterator; |
| 575 | |
| 576 | BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator"); |
| 577 | iterator->user_data = data; |
| 578 | BT_LIB_LOGD("Set message iterator's user data: " |
| 579 | "%!+i, user-data-addr=%p", iterator, data); |
| 580 | } |
| 581 | |
| 582 | /* |
| 583 | * Validate that the default clock snapshot in `msg` doesn't make us go back in |
| 584 | * time. |
| 585 | */ |
| 586 | |
| 587 | BT_ASSERT_POST_DEV_FUNC |
| 588 | static |
| 589 | bool clock_snapshots_are_monotonic_one( |
| 590 | struct bt_self_component_port_input_message_iterator *iterator, |
| 591 | const bt_message *msg) |
| 592 | { |
| 593 | const struct bt_clock_snapshot *clock_snapshot = NULL; |
| 594 | bt_message_type message_type = bt_message_get_type(msg); |
| 595 | int64_t ns_from_origin; |
| 596 | enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status; |
| 597 | |
| 598 | /* |
| 599 | * The default is true: if we can't figure out the clock snapshot |
| 600 | * (or there is none), assume it is fine. |
| 601 | */ |
| 602 | bool result = true; |
| 603 | |
| 604 | switch (message_type) { |
| 605 | case BT_MESSAGE_TYPE_EVENT: |
| 606 | { |
| 607 | struct bt_message_event *event_msg = (struct bt_message_event *) msg; |
| 608 | clock_snapshot = event_msg->default_cs; |
| 609 | break; |
| 610 | } |
| 611 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: |
| 612 | { |
| 613 | struct bt_message_message_iterator_inactivity *inactivity_msg = |
| 614 | (struct bt_message_message_iterator_inactivity *) msg; |
| 615 | clock_snapshot = inactivity_msg->default_cs; |
| 616 | break; |
| 617 | } |
| 618 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
| 619 | case BT_MESSAGE_TYPE_PACKET_END: |
| 620 | { |
| 621 | struct bt_message_packet *packet_msg = (struct bt_message_packet *) msg; |
| 622 | clock_snapshot = packet_msg->default_cs; |
| 623 | break; |
| 624 | } |
| 625 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
| 626 | case BT_MESSAGE_TYPE_STREAM_END: |
| 627 | { |
| 628 | struct bt_message_stream *stream_msg = (struct bt_message_stream *) msg; |
| 629 | if (stream_msg->default_cs_state != BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) { |
| 630 | goto end; |
| 631 | } |
| 632 | |
| 633 | clock_snapshot = stream_msg->default_cs; |
| 634 | break; |
| 635 | } |
| 636 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
| 637 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: |
| 638 | { |
| 639 | struct bt_message_discarded_items *discarded_msg = |
| 640 | (struct bt_message_discarded_items *) msg; |
| 641 | |
| 642 | clock_snapshot = discarded_msg->default_begin_cs; |
| 643 | break; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | if (!clock_snapshot) { |
| 648 | goto end; |
| 649 | } |
| 650 | |
| 651 | clock_snapshot_status = bt_clock_snapshot_get_ns_from_origin(clock_snapshot, &ns_from_origin); |
| 652 | if (clock_snapshot_status != BT_FUNC_STATUS_OK) { |
| 653 | goto end; |
| 654 | } |
| 655 | |
| 656 | result = ns_from_origin >= iterator->last_ns_from_origin; |
| 657 | iterator->last_ns_from_origin = ns_from_origin; |
| 658 | end: |
| 659 | return result; |
| 660 | } |
| 661 | |
| 662 | BT_ASSERT_POST_DEV_FUNC |
| 663 | static |
| 664 | bool clock_snapshots_are_monotonic( |
| 665 | struct bt_self_component_port_input_message_iterator *iterator, |
| 666 | bt_message_array_const msgs, uint64_t msg_count) |
| 667 | { |
| 668 | uint64_t i; |
| 669 | bool result; |
| 670 | |
| 671 | for (i = 0; i < msg_count; i++) { |
| 672 | if (!clock_snapshots_are_monotonic_one(iterator, msgs[i])) { |
| 673 | result = false; |
| 674 | goto end; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | result = true; |
| 679 | |
| 680 | end: |
| 681 | return result; |
| 682 | } |
| 683 | |
| 684 | /* |
| 685 | * When a new stream begins, verify that the clock class tied to this |
| 686 | * stream is compatible with what we've seen before. |
| 687 | */ |
| 688 | |
| 689 | BT_ASSERT_POST_DEV_FUNC |
| 690 | static |
| 691 | bool clock_classes_are_compatible_one(struct bt_self_component_port_input_message_iterator *iterator, |
| 692 | const struct bt_message *msg) |
| 693 | { |
| 694 | enum bt_message_type message_type = bt_message_get_type(msg); |
| 695 | bool result; |
| 696 | |
| 697 | if (message_type == BT_MESSAGE_TYPE_STREAM_BEGINNING) { |
| 698 | const struct bt_message_stream *stream_msg = (struct bt_message_stream *) msg; |
| 699 | const struct bt_clock_class *clock_class = stream_msg->stream->class->default_clock_class; |
| 700 | bt_uuid clock_class_uuid = NULL; |
| 701 | |
| 702 | if (clock_class) { |
| 703 | clock_class_uuid = bt_clock_class_get_uuid(clock_class); |
| 704 | } |
| 705 | |
| 706 | switch (iterator->clock_expectation.type) { |
| 707 | case CLOCK_EXPECTATION_UNSET: |
| 708 | /* |
| 709 | * This is the first time we see a message with a clock |
| 710 | * snapshot: record the properties of that clock, against |
| 711 | * which we'll compare the clock properties of the following |
| 712 | * messages. |
| 713 | */ |
| 714 | |
| 715 | if (!clock_class) { |
| 716 | iterator->clock_expectation.type = CLOCK_EXPECTATION_NONE; |
| 717 | } else if (bt_clock_class_origin_is_unix_epoch(clock_class)) { |
| 718 | iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_UNIX; |
| 719 | } else if (clock_class_uuid) { |
| 720 | iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_OTHER_UUID; |
| 721 | bt_uuid_copy(iterator->clock_expectation.uuid, clock_class_uuid); |
| 722 | } else { |
| 723 | iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID; |
| 724 | } |
| 725 | break; |
| 726 | |
| 727 | case CLOCK_EXPECTATION_NONE: |
| 728 | if (clock_class) { |
| 729 | BT_ASSERT_POST_DEV_MSG( |
| 730 | "Expecting no clock class, got one: %![cc-]+K", |
| 731 | clock_class); |
| 732 | result = false; |
| 733 | goto end; |
| 734 | } |
| 735 | |
| 736 | break; |
| 737 | |
| 738 | case CLOCK_EXPECTATION_ORIGIN_UNIX: |
| 739 | if (!clock_class) { |
| 740 | BT_ASSERT_POST_DEV_MSG( |
| 741 | "Expecting a clock class, got none."); |
| 742 | result = false; |
| 743 | goto end; |
| 744 | } |
| 745 | |
| 746 | if (!bt_clock_class_origin_is_unix_epoch(clock_class)) { |
| 747 | BT_ASSERT_POST_DEV_MSG( |
| 748 | "Expecting a clock class with Unix epoch origin: %![cc-]+K", |
| 749 | clock_class); |
| 750 | result = false; |
| 751 | goto end; |
| 752 | } |
| 753 | break; |
| 754 | |
| 755 | case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID: |
| 756 | if (!clock_class) { |
| 757 | BT_ASSERT_POST_DEV_MSG( |
| 758 | "Expecting a clock class, got none."); |
| 759 | result = false; |
| 760 | goto end; |
| 761 | } |
| 762 | |
| 763 | if (bt_clock_class_origin_is_unix_epoch(clock_class)) { |
| 764 | BT_ASSERT_POST_DEV_MSG( |
| 765 | "Expecting a clock class without Unix epoch origin: %![cc-]+K", |
| 766 | clock_class); |
| 767 | result = false; |
| 768 | goto end; |
| 769 | } |
| 770 | |
| 771 | if (!clock_class_uuid) { |
| 772 | BT_ASSERT_POST_DEV_MSG( |
| 773 | "Expecting a clock class with UUID: %![cc-]+K", |
| 774 | clock_class); |
| 775 | result = false; |
| 776 | goto end; |
| 777 | } |
| 778 | |
| 779 | if (bt_uuid_compare(iterator->clock_expectation.uuid, clock_class_uuid)) { |
| 780 | BT_ASSERT_POST_DEV_MSG( |
| 781 | "Expecting a clock class with UUID, got one " |
| 782 | "with a different UUID: %![cc-]+K, expected-uuid=%!u", |
| 783 | clock_class, iterator->clock_expectation.uuid); |
| 784 | result = false; |
| 785 | goto end; |
| 786 | } |
| 787 | break; |
| 788 | |
| 789 | case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID: |
| 790 | if (!clock_class) { |
| 791 | BT_ASSERT_POST_DEV_MSG( |
| 792 | "Expecting a clock class, got none."); |
| 793 | result = false; |
| 794 | goto end; |
| 795 | } |
| 796 | |
| 797 | if (bt_clock_class_origin_is_unix_epoch(clock_class)) { |
| 798 | BT_ASSERT_POST_DEV_MSG( |
| 799 | "Expecting a clock class without Unix epoch origin: %![cc-]+K", |
| 800 | clock_class); |
| 801 | result = false; |
| 802 | goto end; |
| 803 | } |
| 804 | |
| 805 | if (clock_class_uuid) { |
| 806 | BT_ASSERT_POST_DEV_MSG( |
| 807 | "Expecting a clock class without UUID: %![cc-]+K", |
| 808 | clock_class); |
| 809 | result = false; |
| 810 | goto end; |
| 811 | } |
| 812 | break; |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | result = true; |
| 817 | |
| 818 | end: |
| 819 | return result; |
| 820 | } |
| 821 | |
| 822 | BT_ASSERT_POST_DEV_FUNC |
| 823 | static |
| 824 | bool clock_classes_are_compatible( |
| 825 | struct bt_self_component_port_input_message_iterator *iterator, |
| 826 | bt_message_array_const msgs, uint64_t msg_count) |
| 827 | { |
| 828 | uint64_t i; |
| 829 | bool result; |
| 830 | |
| 831 | for (i = 0; i < msg_count; i++) { |
| 832 | if (!clock_classes_are_compatible_one(iterator, msgs[i])) { |
| 833 | result = false; |
| 834 | goto end; |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | result = true; |
| 839 | |
| 840 | end: |
| 841 | return result; |
| 842 | } |
| 843 | |
| 844 | /* |
| 845 | * Call the `next` method of the iterator. Do some validation on the returned |
| 846 | * messages. |
| 847 | */ |
| 848 | |
| 849 | static |
| 850 | enum bt_component_class_message_iterator_next_method_status |
| 851 | call_iterator_next_method( |
| 852 | struct bt_self_component_port_input_message_iterator *iterator, |
| 853 | bt_message_array_const msgs, uint64_t capacity, uint64_t *user_count) |
| 854 | { |
| 855 | enum bt_component_class_message_iterator_next_method_status status; |
| 856 | |
| 857 | BT_ASSERT(iterator->methods.next); |
| 858 | BT_LOGD_STR("Calling user's \"next\" method."); |
| 859 | status = iterator->methods.next(iterator, msgs, capacity, user_count); |
| 860 | BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64, |
| 861 | bt_common_func_status_string(status), *user_count); |
| 862 | |
| 863 | if (status == BT_FUNC_STATUS_OK) { |
| 864 | BT_ASSERT_POST_DEV(clock_classes_are_compatible(iterator, msgs, *user_count), |
| 865 | "Clocks are not compatible"); |
| 866 | BT_ASSERT_POST_DEV(clock_snapshots_are_monotonic(iterator, msgs, *user_count), |
| 867 | "Clock snapshots are not monotonic"); |
| 868 | } |
| 869 | |
| 870 | return status; |
| 871 | } |
| 872 | |
| 873 | enum bt_message_iterator_next_status |
| 874 | bt_self_component_port_input_message_iterator_next( |
| 875 | struct bt_self_component_port_input_message_iterator *iterator, |
| 876 | bt_message_array_const *msgs, uint64_t *user_count) |
| 877 | { |
| 878 | enum bt_message_iterator_next_status status = BT_FUNC_STATUS_OK; |
| 879 | |
| 880 | BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator"); |
| 881 | BT_ASSERT_PRE_DEV_NON_NULL(msgs, "Message array (output)"); |
| 882 | BT_ASSERT_PRE_DEV_NON_NULL(user_count, "Message count (output)"); |
| 883 | BT_ASSERT_PRE_DEV(iterator->state == |
| 884 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE, |
| 885 | "Message iterator's \"next\" called, but " |
| 886 | "message iterator is in the wrong state: %!+i", iterator); |
| 887 | BT_ASSERT(iterator->upstream_component); |
| 888 | BT_ASSERT(iterator->upstream_component->class); |
| 889 | BT_ASSERT_PRE_DEV( |
| 890 | bt_component_borrow_graph(iterator->upstream_component)->config_state != |
| 891 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, |
| 892 | "Graph is not configured: %!+g", |
| 893 | bt_component_borrow_graph(iterator->upstream_component)); |
| 894 | BT_LIB_LOGD("Getting next self component input port " |
| 895 | "message iterator's messages: %!+i, batch-size=%u", |
| 896 | iterator, MSG_BATCH_SIZE); |
| 897 | |
| 898 | /* |
| 899 | * Call the user's "next" method to get the next messages |
| 900 | * and status. |
| 901 | */ |
| 902 | *user_count = 0; |
| 903 | status = (int) call_iterator_next_method(iterator, |
| 904 | (void *) iterator->base.msgs->pdata, MSG_BATCH_SIZE, |
| 905 | user_count); |
| 906 | BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64, |
| 907 | bt_common_func_status_string(status), *user_count); |
| 908 | if (status < 0) { |
| 909 | BT_LIB_LOGW_APPEND_CAUSE( |
| 910 | "Component input port message iterator's \"next\" method failed: " |
| 911 | "%![iter-]+i, status=%s", |
| 912 | iterator, bt_common_func_status_string(status)); |
| 913 | goto end; |
| 914 | } |
| 915 | |
| 916 | /* |
| 917 | * There is no way that this iterator could have been finalized |
| 918 | * during its "next" method, as the only way to do this is to |
| 919 | * put the last iterator's reference, and this can only be done |
| 920 | * by its downstream owner. |
| 921 | * |
| 922 | * For the same reason, there is no way that this iterator could |
| 923 | * have seeked (cannot seek a self message iterator). |
| 924 | */ |
| 925 | BT_ASSERT(iterator->state == |
| 926 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE); |
| 927 | |
| 928 | switch (status) { |
| 929 | case BT_FUNC_STATUS_OK: |
| 930 | BT_ASSERT_POST_DEV(*user_count <= MSG_BATCH_SIZE, |
| 931 | "Invalid returned message count: greater than " |
| 932 | "batch size: count=%" PRIu64 ", batch-size=%u", |
| 933 | *user_count, MSG_BATCH_SIZE); |
| 934 | *msgs = (void *) iterator->base.msgs->pdata; |
| 935 | break; |
| 936 | case BT_FUNC_STATUS_AGAIN: |
| 937 | goto end; |
| 938 | case BT_FUNC_STATUS_END: |
| 939 | set_self_comp_port_input_msg_iterator_state(iterator, |
| 940 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED); |
| 941 | goto end; |
| 942 | default: |
| 943 | /* Unknown non-error status */ |
| 944 | abort(); |
| 945 | } |
| 946 | |
| 947 | end: |
| 948 | return status; |
| 949 | } |
| 950 | |
| 951 | enum bt_message_iterator_next_status bt_port_output_message_iterator_next( |
| 952 | struct bt_port_output_message_iterator *iterator, |
| 953 | bt_message_array_const *msgs_to_user, |
| 954 | uint64_t *count_to_user) |
| 955 | { |
| 956 | enum bt_message_iterator_next_status status; |
| 957 | int graph_status; |
| 958 | |
| 959 | BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator"); |
| 960 | BT_ASSERT_PRE_DEV_NON_NULL(msgs_to_user, "Message array (output)"); |
| 961 | BT_ASSERT_PRE_DEV_NON_NULL(count_to_user, "Message count (output)"); |
| 962 | BT_LIB_LOGD("Getting next output port message iterator's messages: " |
| 963 | "%!+i", iterator); |
| 964 | graph_status = bt_graph_consume_sink_no_check(iterator->graph, |
| 965 | iterator->colander); |
| 966 | switch (graph_status) { |
| 967 | case BT_FUNC_STATUS_CANCELED: |
| 968 | case BT_FUNC_STATUS_AGAIN: |
| 969 | case BT_FUNC_STATUS_END: |
| 970 | case BT_FUNC_STATUS_MEMORY_ERROR: |
| 971 | status = (int) graph_status; |
| 972 | break; |
| 973 | case BT_FUNC_STATUS_OK: |
| 974 | status = BT_FUNC_STATUS_OK; |
| 975 | |
| 976 | /* |
| 977 | * On success, the colander sink moves the messages |
| 978 | * to this iterator's array and sets this iterator's |
| 979 | * message count: move them to the user. |
| 980 | */ |
| 981 | *msgs_to_user = (void *) iterator->base.msgs->pdata; |
| 982 | *count_to_user = iterator->count; |
| 983 | break; |
| 984 | default: |
| 985 | /* Other errors */ |
| 986 | status = BT_FUNC_STATUS_ERROR; |
| 987 | } |
| 988 | |
| 989 | return status; |
| 990 | } |
| 991 | |
| 992 | struct bt_component * |
| 993 | bt_self_component_port_input_message_iterator_borrow_component( |
| 994 | struct bt_self_component_port_input_message_iterator *iterator) |
| 995 | { |
| 996 | BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator"); |
| 997 | return iterator->upstream_component; |
| 998 | } |
| 999 | |
| 1000 | const struct bt_component * |
| 1001 | bt_self_component_port_input_message_iterator_borrow_component_const( |
| 1002 | const struct bt_self_component_port_input_message_iterator *iterator) |
| 1003 | { |
| 1004 | BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator"); |
| 1005 | return iterator->upstream_component; |
| 1006 | } |
| 1007 | |
| 1008 | struct bt_self_component *bt_self_message_iterator_borrow_component( |
| 1009 | struct bt_self_message_iterator *self_iterator) |
| 1010 | { |
| 1011 | struct bt_self_component_port_input_message_iterator *iterator = |
| 1012 | (void *) self_iterator; |
| 1013 | |
| 1014 | BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator"); |
| 1015 | return (void *) iterator->upstream_component; |
| 1016 | } |
| 1017 | |
| 1018 | struct bt_self_port_output *bt_self_message_iterator_borrow_port( |
| 1019 | struct bt_self_message_iterator *self_iterator) |
| 1020 | { |
| 1021 | struct bt_self_component_port_input_message_iterator *iterator = |
| 1022 | (void *) self_iterator; |
| 1023 | |
| 1024 | BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator"); |
| 1025 | return (void *) iterator->upstream_port; |
| 1026 | } |
| 1027 | |
| 1028 | static |
| 1029 | void bt_port_output_message_iterator_destroy(struct bt_object *obj) |
| 1030 | { |
| 1031 | struct bt_port_output_message_iterator *iterator = (void *) obj; |
| 1032 | |
| 1033 | BT_LIB_LOGI("Destroying output port message iterator object: %!+i", |
| 1034 | iterator); |
| 1035 | BT_LOGD_STR("Putting graph."); |
| 1036 | BT_OBJECT_PUT_REF_AND_RESET(iterator->graph); |
| 1037 | BT_LOGD_STR("Putting colander sink component."); |
| 1038 | BT_OBJECT_PUT_REF_AND_RESET(iterator->colander); |
| 1039 | destroy_base_message_iterator(obj); |
| 1040 | } |
| 1041 | |
| 1042 | struct bt_port_output_message_iterator * |
| 1043 | bt_port_output_message_iterator_create(struct bt_graph *graph, |
| 1044 | const struct bt_port_output *output_port) |
| 1045 | { |
| 1046 | struct bt_port_output_message_iterator *iterator = NULL; |
| 1047 | struct bt_component_class_sink *colander_comp_cls = NULL; |
| 1048 | struct bt_component *output_port_comp = NULL; |
| 1049 | struct bt_component_sink *colander_comp; |
| 1050 | int graph_status; |
| 1051 | struct bt_port_input *colander_in_port = NULL; |
| 1052 | struct bt_component_class_sink_colander_data colander_data; |
| 1053 | int ret; |
| 1054 | |
| 1055 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 1056 | BT_ASSERT_PRE_NON_NULL(output_port, "Output port"); |
| 1057 | output_port_comp = bt_port_borrow_component_inline( |
| 1058 | (const void *) output_port); |
| 1059 | BT_ASSERT_PRE(output_port_comp, |
| 1060 | "Output port has no component: %!+p", output_port); |
| 1061 | BT_ASSERT_PRE(bt_component_borrow_graph(output_port_comp) == |
| 1062 | (void *) graph, |
| 1063 | "Output port is not part of graph: %![graph-]+g, %![port-]+p", |
| 1064 | graph, output_port); |
| 1065 | BT_ASSERT_PRE(!graph->has_sink, |
| 1066 | "Graph already has a sink component: %![graph-]+g"); |
| 1067 | |
| 1068 | /* Create message iterator */ |
| 1069 | BT_LIB_LOGI("Creating message iterator on output port: " |
| 1070 | "%![port-]+p, %![comp-]+c", output_port, output_port_comp); |
| 1071 | iterator = g_new0(struct bt_port_output_message_iterator, 1); |
| 1072 | if (!iterator) { |
| 1073 | BT_LIB_LOGE_APPEND_CAUSE( |
| 1074 | "Failed to allocate one output port message iterator."); |
| 1075 | goto error; |
| 1076 | } |
| 1077 | |
| 1078 | ret = init_message_iterator((void *) iterator, |
| 1079 | BT_MESSAGE_ITERATOR_TYPE_PORT_OUTPUT, |
| 1080 | bt_port_output_message_iterator_destroy); |
| 1081 | if (ret) { |
| 1082 | /* init_message_iterator() logs errors */ |
| 1083 | BT_OBJECT_PUT_REF_AND_RESET(iterator); |
| 1084 | goto end; |
| 1085 | } |
| 1086 | |
| 1087 | /* Create colander component */ |
| 1088 | colander_comp_cls = bt_component_class_sink_colander_get(); |
| 1089 | if (!colander_comp_cls) { |
| 1090 | /* bt_component_class_sink_colander_get() logs errors */ |
| 1091 | BT_LIB_LOGE_APPEND_CAUSE( |
| 1092 | "Cannot get colander sink component class."); |
| 1093 | goto error; |
| 1094 | } |
| 1095 | |
| 1096 | iterator->graph = graph; |
| 1097 | bt_object_get_no_null_check(iterator->graph); |
| 1098 | colander_data.msgs = (void *) iterator->base.msgs->pdata; |
| 1099 | colander_data.count_addr = &iterator->count; |
| 1100 | |
| 1101 | /* |
| 1102 | * Hope that nobody uses this very unique name. |
| 1103 | * |
| 1104 | * We pass `BT_LOGGING_LEVEL_NONE` but the colander component |
| 1105 | * class module does not use this level anyway since it belongs |
| 1106 | * to the library. |
| 1107 | */ |
| 1108 | graph_status = bt_graph_add_sink_component_with_init_method_data( |
| 1109 | (void *) graph, colander_comp_cls, |
| 1110 | "colander-36ac3409-b1a8-4d60-ab1f-4fdf341a8fb1", |
| 1111 | NULL, &colander_data, BT_LOGGING_LEVEL_NONE, |
| 1112 | (void *) &iterator->colander); |
| 1113 | if (graph_status != BT_FUNC_STATUS_OK) { |
| 1114 | BT_LIB_LOGE_APPEND_CAUSE( |
| 1115 | "Cannot add colander sink component to graph: " |
| 1116 | "%![graph-]+g, status=%s", graph, |
| 1117 | bt_common_func_status_string(graph_status)); |
| 1118 | goto error; |
| 1119 | } |
| 1120 | |
| 1121 | /* |
| 1122 | * Connect provided output port to the colander component's |
| 1123 | * input port. |
| 1124 | */ |
| 1125 | colander_in_port = |
| 1126 | (void *) bt_component_sink_borrow_input_port_by_index_const( |
| 1127 | (void *) iterator->colander, 0); |
| 1128 | BT_ASSERT(colander_in_port); |
| 1129 | graph_status = bt_graph_connect_ports(graph, |
| 1130 | output_port, colander_in_port, NULL); |
| 1131 | if (graph_status != BT_FUNC_STATUS_OK) { |
| 1132 | BT_LIB_LOGW_APPEND_CAUSE( |
| 1133 | "Cannot connect colander sink's port: " |
| 1134 | "%![graph-]+g, %![comp-]+c, status=%s", graph, |
| 1135 | iterator->colander, |
| 1136 | bt_common_func_status_string(graph_status)); |
| 1137 | goto error; |
| 1138 | } |
| 1139 | |
| 1140 | /* |
| 1141 | * At this point everything went fine. Make the graph |
| 1142 | * nonconsumable forever so that only this message iterator |
| 1143 | * can consume (thanks to bt_graph_consume_sink_no_check()). |
| 1144 | * This avoids leaking the message created by the colander |
| 1145 | * sink and moved to the message iterator's message |
| 1146 | * member. |
| 1147 | */ |
| 1148 | bt_graph_set_can_consume(iterator->graph, false); |
| 1149 | |
| 1150 | /* Also set the graph as being configured. */ |
| 1151 | graph_status = bt_graph_configure(graph); |
| 1152 | if (graph_status != BT_FUNC_STATUS_OK) { |
| 1153 | BT_LIB_LOGW_APPEND_CAUSE( |
| 1154 | "Cannot configure graph after having " |
| 1155 | "added and connected colander sink: " |
| 1156 | "%![graph-]+g, %![comp-]+c, status=%s", graph, |
| 1157 | iterator->colander, |
| 1158 | bt_common_func_status_string(graph_status)); |
| 1159 | goto error; |
| 1160 | } |
| 1161 | goto end; |
| 1162 | |
| 1163 | error: |
| 1164 | if (iterator && iterator->graph && iterator->colander) { |
| 1165 | int ret; |
| 1166 | |
| 1167 | /* Remove created colander component from graph if any */ |
| 1168 | colander_comp = iterator->colander; |
| 1169 | BT_OBJECT_PUT_REF_AND_RESET(iterator->colander); |
| 1170 | |
| 1171 | /* |
| 1172 | * At this point the colander component's reference |
| 1173 | * count is 0 because iterator->colander was the only |
| 1174 | * owner. We also know that it is not connected because |
| 1175 | * this is the last operation before this function |
| 1176 | * succeeds. |
| 1177 | * |
| 1178 | * Since we honor the preconditions here, |
| 1179 | * bt_graph_remove_unconnected_component() always |
| 1180 | * succeeds. |
| 1181 | */ |
| 1182 | ret = bt_graph_remove_unconnected_component(iterator->graph, |
| 1183 | (void *) colander_comp); |
| 1184 | BT_ASSERT(ret == 0); |
| 1185 | } |
| 1186 | |
| 1187 | BT_OBJECT_PUT_REF_AND_RESET(iterator); |
| 1188 | |
| 1189 | end: |
| 1190 | bt_object_put_ref(colander_comp_cls); |
| 1191 | return (void *) iterator; |
| 1192 | } |
| 1193 | |
| 1194 | bt_bool bt_self_component_port_input_message_iterator_can_seek_ns_from_origin( |
| 1195 | struct bt_self_component_port_input_message_iterator *iterator, |
| 1196 | int64_t ns_from_origin) |
| 1197 | { |
| 1198 | bt_bool can = BT_FALSE; |
| 1199 | |
| 1200 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
| 1201 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); |
| 1202 | BT_ASSERT_PRE( |
| 1203 | bt_component_borrow_graph(iterator->upstream_component)->config_state != |
| 1204 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, |
| 1205 | "Graph is not configured: %!+g", |
| 1206 | bt_component_borrow_graph(iterator->upstream_component)); |
| 1207 | |
| 1208 | if (iterator->methods.can_seek_ns_from_origin) { |
| 1209 | can = iterator->methods.can_seek_ns_from_origin(iterator, |
| 1210 | ns_from_origin); |
| 1211 | goto end; |
| 1212 | } |
| 1213 | |
| 1214 | /* |
| 1215 | * Automatic seeking fall back: if we can seek to the beginning, |
| 1216 | * then we can automatically seek to any message. |
| 1217 | */ |
| 1218 | if (iterator->methods.can_seek_beginning) { |
| 1219 | can = iterator->methods.can_seek_beginning(iterator); |
| 1220 | } |
| 1221 | |
| 1222 | end: |
| 1223 | return can; |
| 1224 | } |
| 1225 | |
| 1226 | bt_bool bt_self_component_port_input_message_iterator_can_seek_beginning( |
| 1227 | struct bt_self_component_port_input_message_iterator *iterator) |
| 1228 | { |
| 1229 | bt_bool can = BT_FALSE; |
| 1230 | |
| 1231 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
| 1232 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); |
| 1233 | BT_ASSERT_PRE( |
| 1234 | bt_component_borrow_graph(iterator->upstream_component)->config_state != |
| 1235 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, |
| 1236 | "Graph is not configured: %!+g", |
| 1237 | bt_component_borrow_graph(iterator->upstream_component)); |
| 1238 | |
| 1239 | if (iterator->methods.can_seek_beginning) { |
| 1240 | can = iterator->methods.can_seek_beginning(iterator); |
| 1241 | } |
| 1242 | |
| 1243 | return can; |
| 1244 | } |
| 1245 | |
| 1246 | static inline |
| 1247 | void set_iterator_state_after_seeking( |
| 1248 | struct bt_self_component_port_input_message_iterator *iterator, |
| 1249 | int status) |
| 1250 | { |
| 1251 | enum bt_self_component_port_input_message_iterator_state new_state = 0; |
| 1252 | |
| 1253 | /* Set iterator's state depending on seeking status */ |
| 1254 | switch (status) { |
| 1255 | case BT_FUNC_STATUS_OK: |
| 1256 | new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE; |
| 1257 | break; |
| 1258 | case BT_FUNC_STATUS_AGAIN: |
| 1259 | new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN; |
| 1260 | break; |
| 1261 | case BT_FUNC_STATUS_ERROR: |
| 1262 | case BT_FUNC_STATUS_MEMORY_ERROR: |
| 1263 | new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR; |
| 1264 | break; |
| 1265 | case BT_FUNC_STATUS_END: |
| 1266 | new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED; |
| 1267 | break; |
| 1268 | default: |
| 1269 | abort(); |
| 1270 | } |
| 1271 | |
| 1272 | set_self_comp_port_input_msg_iterator_state(iterator, new_state); |
| 1273 | } |
| 1274 | |
| 1275 | static |
| 1276 | void reset_iterator_expectations( |
| 1277 | struct bt_self_component_port_input_message_iterator *iterator) |
| 1278 | { |
| 1279 | iterator->last_ns_from_origin = INT64_MIN; |
| 1280 | iterator->clock_expectation.type = CLOCK_EXPECTATION_UNSET; |
| 1281 | } |
| 1282 | |
| 1283 | enum bt_message_iterator_seek_beginning_status |
| 1284 | bt_self_component_port_input_message_iterator_seek_beginning( |
| 1285 | struct bt_self_component_port_input_message_iterator *iterator) |
| 1286 | { |
| 1287 | int status; |
| 1288 | |
| 1289 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
| 1290 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); |
| 1291 | BT_ASSERT_PRE( |
| 1292 | bt_component_borrow_graph(iterator->upstream_component)->config_state != |
| 1293 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, |
| 1294 | "Graph is not configured: %!+g", |
| 1295 | bt_component_borrow_graph(iterator->upstream_component)); |
| 1296 | BT_ASSERT_PRE( |
| 1297 | bt_self_component_port_input_message_iterator_can_seek_beginning( |
| 1298 | iterator), |
| 1299 | "Message iterator cannot seek beginning: %!+i", iterator); |
| 1300 | |
| 1301 | /* |
| 1302 | * We are seeking, reset our expectations about how the following |
| 1303 | * messages should look like. |
| 1304 | */ |
| 1305 | reset_iterator_expectations(iterator); |
| 1306 | |
| 1307 | BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator); |
| 1308 | set_self_comp_port_input_msg_iterator_state(iterator, |
| 1309 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING); |
| 1310 | status = iterator->methods.seek_beginning(iterator); |
| 1311 | BT_LOGD("User method returned: status=%s", |
| 1312 | bt_common_func_status_string(status)); |
| 1313 | BT_ASSERT_POST(status == BT_FUNC_STATUS_OK || |
| 1314 | status == BT_FUNC_STATUS_ERROR || |
| 1315 | status == BT_FUNC_STATUS_MEMORY_ERROR || |
| 1316 | status == BT_FUNC_STATUS_AGAIN, |
| 1317 | "Unexpected status: %![iter-]+i, status=%s", |
| 1318 | iterator, bt_common_func_status_string(status)); |
| 1319 | if (status < 0) { |
| 1320 | BT_LIB_LOGW_APPEND_CAUSE( |
| 1321 | "Component input port message iterator's \"seek beginning\" method failed: " |
| 1322 | "%![iter-]+i, status=%s", |
| 1323 | iterator, bt_common_func_status_string(status)); |
| 1324 | } |
| 1325 | |
| 1326 | set_iterator_state_after_seeking(iterator, status); |
| 1327 | return status; |
| 1328 | } |
| 1329 | |
| 1330 | /* |
| 1331 | * Structure used to record the state of a given stream during the fast-forward |
| 1332 | * phase of an auto-seek. |
| 1333 | */ |
| 1334 | struct auto_seek_stream_state { |
| 1335 | /* |
| 1336 | * Value representing which step of this timeline we are at. |
| 1337 | * |
| 1338 | * time ---> |
| 1339 | * [SB] 1 [PB] 2 [PE] 1 [SE] |
| 1340 | * |
| 1341 | * At each point in the timeline, the messages we need to replicate are: |
| 1342 | * |
| 1343 | * 1: Stream beginning |
| 1344 | * 2: Stream beginning, packet beginning |
| 1345 | * |
| 1346 | * Before "Stream beginning" and after "Stream end", we don't need to |
| 1347 | * replicate anything as the stream doesn't exist. |
| 1348 | */ |
| 1349 | enum { |
| 1350 | AUTO_SEEK_STREAM_STATE_STREAM_BEGAN, |
| 1351 | AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, |
| 1352 | } state; |
| 1353 | |
| 1354 | /* |
| 1355 | * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are |
| 1356 | * in. This is a weak reference, since the packet will always be |
| 1357 | * alive by the time we use it. |
| 1358 | */ |
| 1359 | struct bt_packet *packet; |
| 1360 | |
| 1361 | /* Have we see a message with a clock snapshot yet? */ |
| 1362 | bool seen_clock_snapshot; |
| 1363 | }; |
| 1364 | |
| 1365 | static |
| 1366 | struct auto_seek_stream_state *create_auto_seek_stream_state(void) |
| 1367 | { |
| 1368 | return g_new0(struct auto_seek_stream_state, 1); |
| 1369 | } |
| 1370 | |
| 1371 | static |
| 1372 | void destroy_auto_seek_stream_state(void *ptr) |
| 1373 | { |
| 1374 | g_free(ptr); |
| 1375 | } |
| 1376 | |
| 1377 | static |
| 1378 | GHashTable *create_auto_seek_stream_states(void) |
| 1379 | { |
| 1380 | return g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, |
| 1381 | destroy_auto_seek_stream_state); |
| 1382 | } |
| 1383 | |
| 1384 | static |
| 1385 | void destroy_auto_seek_stream_states(GHashTable *stream_states) |
| 1386 | { |
| 1387 | g_hash_table_destroy(stream_states); |
| 1388 | } |
| 1389 | |
| 1390 | /* |
| 1391 | * Handle one message while we are in the fast-forward phase of an auto-seek. |
| 1392 | * |
| 1393 | * Sets `*got_first` to true if the message's timestamp is greater or equal to |
| 1394 | * `ns_from_origin`. In other words, if this is the first message after our |
| 1395 | * seek point. |
| 1396 | * |
| 1397 | * `stream_states` is an hash table of `bt_stream *` (weak reference) to |
| 1398 | * `struct auto_seek_stream_state` used to keep the state of each stream |
| 1399 | * during the fast-forward. |
| 1400 | */ |
| 1401 | |
| 1402 | static inline |
| 1403 | int auto_seek_handle_message( |
| 1404 | struct bt_self_component_port_input_message_iterator *iterator, |
| 1405 | int64_t ns_from_origin, const struct bt_message *msg, |
| 1406 | bool *got_first, GHashTable *stream_states) |
| 1407 | { |
| 1408 | int status = BT_FUNC_STATUS_OK; |
| 1409 | int64_t msg_ns_from_origin; |
| 1410 | const struct bt_clock_snapshot *clk_snapshot = NULL; |
| 1411 | int ret; |
| 1412 | |
| 1413 | BT_ASSERT(msg); |
| 1414 | BT_ASSERT(got_first); |
| 1415 | |
| 1416 | switch (msg->type) { |
| 1417 | case BT_MESSAGE_TYPE_EVENT: |
| 1418 | { |
| 1419 | const struct bt_message_event *event_msg = |
| 1420 | (const void *) msg; |
| 1421 | |
| 1422 | clk_snapshot = event_msg->default_cs; |
| 1423 | BT_ASSERT_POST_DEV(clk_snapshot, |
| 1424 | "Event message has no default clock snapshot: %!+n", |
| 1425 | event_msg); |
| 1426 | break; |
| 1427 | } |
| 1428 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: |
| 1429 | { |
| 1430 | const struct bt_message_message_iterator_inactivity *inactivity_msg = |
| 1431 | (const void *) msg; |
| 1432 | |
| 1433 | clk_snapshot = inactivity_msg->default_cs; |
| 1434 | BT_ASSERT(clk_snapshot); |
| 1435 | break; |
| 1436 | } |
| 1437 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
| 1438 | case BT_MESSAGE_TYPE_PACKET_END: |
| 1439 | { |
| 1440 | const struct bt_message_packet *packet_msg = |
| 1441 | (const void *) msg; |
| 1442 | |
| 1443 | clk_snapshot = packet_msg->default_cs; |
| 1444 | BT_ASSERT_POST_DEV(clk_snapshot, |
| 1445 | "Packet message has no default clock snapshot: %!+n", |
| 1446 | packet_msg); |
| 1447 | break; |
| 1448 | } |
| 1449 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
| 1450 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: |
| 1451 | { |
| 1452 | struct bt_message_discarded_items *msg_disc_items = |
| 1453 | (void *) msg; |
| 1454 | |
| 1455 | BT_ASSERT_POST_DEV(msg_disc_items->default_begin_cs && |
| 1456 | msg_disc_items->default_end_cs, |
| 1457 | "Discarded events/packets message has no default clock snapshots: %!+n", |
| 1458 | msg_disc_items); |
| 1459 | ret = bt_clock_snapshot_get_ns_from_origin( |
| 1460 | msg_disc_items->default_begin_cs, |
| 1461 | &msg_ns_from_origin); |
| 1462 | if (ret) { |
| 1463 | status = BT_FUNC_STATUS_ERROR; |
| 1464 | goto end; |
| 1465 | } |
| 1466 | |
| 1467 | if (msg_ns_from_origin >= ns_from_origin) { |
| 1468 | *got_first = true; |
| 1469 | goto push_msg; |
| 1470 | } |
| 1471 | |
| 1472 | ret = bt_clock_snapshot_get_ns_from_origin( |
| 1473 | msg_disc_items->default_end_cs, |
| 1474 | &msg_ns_from_origin); |
| 1475 | if (ret) { |
| 1476 | status = BT_FUNC_STATUS_ERROR; |
| 1477 | goto end; |
| 1478 | } |
| 1479 | |
| 1480 | if (msg_ns_from_origin >= ns_from_origin) { |
| 1481 | /* |
| 1482 | * The discarded items message's beginning time |
| 1483 | * is before the requested seeking time, but its |
| 1484 | * end time is after. Modify the message so as |
| 1485 | * to set its beginning time to the requested |
| 1486 | * seeking time, and make its item count unknown |
| 1487 | * as we don't know if items were really |
| 1488 | * discarded within the new time range. |
| 1489 | */ |
| 1490 | uint64_t new_begin_raw_value = 0; |
| 1491 | |
| 1492 | ret = bt_clock_class_clock_value_from_ns_from_origin( |
| 1493 | msg_disc_items->default_end_cs->clock_class, |
| 1494 | ns_from_origin, &new_begin_raw_value); |
| 1495 | if (ret) { |
| 1496 | status = BT_FUNC_STATUS_ERROR; |
| 1497 | goto end; |
| 1498 | } |
| 1499 | |
| 1500 | bt_clock_snapshot_set_raw_value( |
| 1501 | msg_disc_items->default_begin_cs, |
| 1502 | new_begin_raw_value); |
| 1503 | msg_disc_items->count.base.avail = |
| 1504 | BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE; |
| 1505 | |
| 1506 | /* |
| 1507 | * It is safe to push it because its beginning |
| 1508 | * time is exactly the requested seeking time. |
| 1509 | */ |
| 1510 | goto push_msg; |
| 1511 | } else { |
| 1512 | goto skip_msg; |
| 1513 | } |
| 1514 | } |
| 1515 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
| 1516 | case BT_MESSAGE_TYPE_STREAM_END: |
| 1517 | { |
| 1518 | struct bt_message_stream *stream_msg = |
| 1519 | (struct bt_message_stream *) msg; |
| 1520 | |
| 1521 | if (stream_msg->default_cs_state != BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) { |
| 1522 | /* Ignore */ |
| 1523 | goto skip_msg; |
| 1524 | } |
| 1525 | |
| 1526 | clk_snapshot = stream_msg->default_cs; |
| 1527 | break; |
| 1528 | } |
| 1529 | default: |
| 1530 | abort(); |
| 1531 | } |
| 1532 | |
| 1533 | BT_ASSERT(clk_snapshot); |
| 1534 | ret = bt_clock_snapshot_get_ns_from_origin(clk_snapshot, |
| 1535 | &msg_ns_from_origin); |
| 1536 | if (ret) { |
| 1537 | status = BT_FUNC_STATUS_ERROR; |
| 1538 | goto end; |
| 1539 | } |
| 1540 | |
| 1541 | if (msg_ns_from_origin >= ns_from_origin) { |
| 1542 | *got_first = true; |
| 1543 | goto push_msg; |
| 1544 | } |
| 1545 | |
| 1546 | skip_msg: |
| 1547 | /* This message won't be sent downstream. */ |
| 1548 | switch (msg->type) { |
| 1549 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
| 1550 | { |
| 1551 | const struct bt_message_stream *stream_msg = (const void *) msg; |
| 1552 | struct auto_seek_stream_state *stream_state; |
| 1553 | |
| 1554 | /* Update stream's state: stream began. */ |
| 1555 | stream_state = create_auto_seek_stream_state(); |
| 1556 | if (!stream_state) { |
| 1557 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 1558 | goto end; |
| 1559 | } |
| 1560 | |
| 1561 | stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_BEGAN; |
| 1562 | |
| 1563 | if (stream_msg->default_cs_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) { |
| 1564 | stream_state->seen_clock_snapshot = true; |
| 1565 | } |
| 1566 | |
| 1567 | BT_ASSERT(!bt_g_hash_table_contains(stream_states, stream_msg->stream)); |
| 1568 | g_hash_table_insert(stream_states, stream_msg->stream, stream_state); |
| 1569 | break; |
| 1570 | } |
| 1571 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
| 1572 | { |
| 1573 | const struct bt_message_packet *packet_msg = |
| 1574 | (const void *) msg; |
| 1575 | struct auto_seek_stream_state *stream_state; |
| 1576 | |
| 1577 | /* Update stream's state: packet began. */ |
| 1578 | stream_state = g_hash_table_lookup(stream_states, packet_msg->packet->stream); |
| 1579 | BT_ASSERT(stream_state); |
| 1580 | |
| 1581 | BT_ASSERT(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_BEGAN); |
| 1582 | stream_state->state = AUTO_SEEK_STREAM_STATE_PACKET_BEGAN; |
| 1583 | BT_ASSERT(!stream_state->packet); |
| 1584 | stream_state->packet = packet_msg->packet; |
| 1585 | |
| 1586 | if (packet_msg->packet->stream->class->packets_have_beginning_default_clock_snapshot) { |
| 1587 | stream_state->seen_clock_snapshot = true; |
| 1588 | } |
| 1589 | |
| 1590 | break; |
| 1591 | } |
| 1592 | case BT_MESSAGE_TYPE_EVENT: |
| 1593 | { |
| 1594 | const struct bt_message_event *event_msg = (const void *) msg; |
| 1595 | struct auto_seek_stream_state *stream_state; |
| 1596 | |
| 1597 | stream_state = g_hash_table_lookup(stream_states, |
| 1598 | event_msg->event->packet->stream); |
| 1599 | BT_ASSERT(stream_state); |
| 1600 | |
| 1601 | // HELPME: are we sure that event messages have clock snapshots at this point? |
| 1602 | stream_state->seen_clock_snapshot = true; |
| 1603 | |
| 1604 | break; |
| 1605 | } |
| 1606 | case BT_MESSAGE_TYPE_PACKET_END: |
| 1607 | { |
| 1608 | const struct bt_message_packet *packet_msg = |
| 1609 | (const void *) msg; |
| 1610 | struct auto_seek_stream_state *stream_state; |
| 1611 | |
| 1612 | /* Update stream's state: packet ended. */ |
| 1613 | stream_state = g_hash_table_lookup(stream_states, packet_msg->packet->stream); |
| 1614 | BT_ASSERT(stream_state); |
| 1615 | |
| 1616 | BT_ASSERT(stream_state->state == AUTO_SEEK_STREAM_STATE_PACKET_BEGAN); |
| 1617 | stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_BEGAN; |
| 1618 | BT_ASSERT(stream_state->packet); |
| 1619 | stream_state->packet = NULL; |
| 1620 | |
| 1621 | if (packet_msg->packet->stream->class->packets_have_end_default_clock_snapshot) { |
| 1622 | stream_state->seen_clock_snapshot = true; |
| 1623 | } |
| 1624 | |
| 1625 | break; |
| 1626 | } |
| 1627 | case BT_MESSAGE_TYPE_STREAM_END: |
| 1628 | { |
| 1629 | const struct bt_message_stream *stream_msg = (const void *) msg; |
| 1630 | struct auto_seek_stream_state *stream_state; |
| 1631 | |
| 1632 | stream_state = g_hash_table_lookup(stream_states, stream_msg->stream); |
| 1633 | BT_ASSERT(stream_state); |
| 1634 | BT_ASSERT(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_BEGAN); |
| 1635 | BT_ASSERT(!stream_state->packet); |
| 1636 | |
| 1637 | /* Update stream's state: this stream doesn't exist anymore. */ |
| 1638 | g_hash_table_remove(stream_states, stream_msg->stream); |
| 1639 | break; |
| 1640 | } |
| 1641 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
| 1642 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: |
| 1643 | { |
| 1644 | const struct bt_message_discarded_items *discarded_msg = |
| 1645 | (const void *) msg; |
| 1646 | struct auto_seek_stream_state *stream_state; |
| 1647 | |
| 1648 | stream_state = g_hash_table_lookup(stream_states, discarded_msg->stream); |
| 1649 | BT_ASSERT(stream_state); |
| 1650 | |
| 1651 | if ((msg->type == BT_MESSAGE_TYPE_DISCARDED_EVENTS && discarded_msg->stream->class->discarded_events_have_default_clock_snapshots) || |
| 1652 | (msg->type == BT_MESSAGE_TYPE_DISCARDED_PACKETS && discarded_msg->stream->class->discarded_packets_have_default_clock_snapshots)) { |
| 1653 | stream_state->seen_clock_snapshot = true; |
| 1654 | } |
| 1655 | |
| 1656 | break; |
| 1657 | } |
| 1658 | default: |
| 1659 | break; |
| 1660 | } |
| 1661 | |
| 1662 | bt_object_put_no_null_check(msg); |
| 1663 | msg = NULL; |
| 1664 | goto end; |
| 1665 | |
| 1666 | push_msg: |
| 1667 | g_queue_push_tail(iterator->auto_seek.msgs, (void *) msg); |
| 1668 | msg = NULL; |
| 1669 | |
| 1670 | end: |
| 1671 | BT_ASSERT(!msg || status != BT_FUNC_STATUS_OK); |
| 1672 | return status; |
| 1673 | } |
| 1674 | |
| 1675 | static |
| 1676 | int find_message_ge_ns_from_origin( |
| 1677 | struct bt_self_component_port_input_message_iterator *iterator, |
| 1678 | int64_t ns_from_origin, GHashTable *stream_states) |
| 1679 | { |
| 1680 | int status; |
| 1681 | enum bt_self_component_port_input_message_iterator_state init_state = |
| 1682 | iterator->state; |
| 1683 | const struct bt_message *messages[MSG_BATCH_SIZE]; |
| 1684 | uint64_t user_count = 0; |
| 1685 | uint64_t i; |
| 1686 | bool got_first = false; |
| 1687 | |
| 1688 | BT_ASSERT(iterator); |
| 1689 | memset(&messages[0], 0, sizeof(messages[0]) * MSG_BATCH_SIZE); |
| 1690 | |
| 1691 | /* |
| 1692 | * Make this iterator temporarily active (not seeking) to call |
| 1693 | * the "next" method. |
| 1694 | */ |
| 1695 | set_self_comp_port_input_msg_iterator_state(iterator, |
| 1696 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE); |
| 1697 | |
| 1698 | BT_ASSERT(iterator->methods.next); |
| 1699 | |
| 1700 | while (!got_first) { |
| 1701 | /* |
| 1702 | * Call the user's "next" method to get the next |
| 1703 | * messages and status. |
| 1704 | */ |
| 1705 | status = call_iterator_next_method(iterator, |
| 1706 | &messages[0], MSG_BATCH_SIZE, &user_count); |
| 1707 | BT_LOGD("User method returned: status=%s", |
| 1708 | bt_common_func_status_string(status)); |
| 1709 | if (status < 0) { |
| 1710 | BT_LIB_LOGW_APPEND_CAUSE( |
| 1711 | "Component input port message iterator's \"next\" method failed: " |
| 1712 | "%![iter-]+i, status=%s", |
| 1713 | iterator, bt_common_func_status_string(status)); |
| 1714 | } |
| 1715 | |
| 1716 | /* |
| 1717 | * The user's "next" method must not do any action which |
| 1718 | * would change the iterator's state. |
| 1719 | */ |
| 1720 | BT_ASSERT(iterator->state == |
| 1721 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE); |
| 1722 | |
| 1723 | switch (status) { |
| 1724 | case BT_FUNC_STATUS_OK: |
| 1725 | BT_ASSERT_POST_DEV(user_count <= MSG_BATCH_SIZE, |
| 1726 | "Invalid returned message count: greater than " |
| 1727 | "batch size: count=%" PRIu64 ", batch-size=%u", |
| 1728 | user_count, MSG_BATCH_SIZE); |
| 1729 | break; |
| 1730 | case BT_FUNC_STATUS_AGAIN: |
| 1731 | case BT_FUNC_STATUS_ERROR: |
| 1732 | case BT_FUNC_STATUS_MEMORY_ERROR: |
| 1733 | case BT_FUNC_STATUS_END: |
| 1734 | goto end; |
| 1735 | default: |
| 1736 | abort(); |
| 1737 | } |
| 1738 | |
| 1739 | for (i = 0; i < user_count; i++) { |
| 1740 | if (got_first) { |
| 1741 | g_queue_push_tail(iterator->auto_seek.msgs, |
| 1742 | (void *) messages[i]); |
| 1743 | messages[i] = NULL; |
| 1744 | continue; |
| 1745 | } |
| 1746 | |
| 1747 | status = auto_seek_handle_message(iterator, |
| 1748 | ns_from_origin, messages[i], &got_first, |
| 1749 | stream_states); |
| 1750 | if (status == BT_FUNC_STATUS_OK) { |
| 1751 | /* Message was either pushed or moved */ |
| 1752 | messages[i] = NULL; |
| 1753 | } else { |
| 1754 | goto end; |
| 1755 | } |
| 1756 | } |
| 1757 | } |
| 1758 | |
| 1759 | end: |
| 1760 | for (i = 0; i < user_count; i++) { |
| 1761 | if (messages[i]) { |
| 1762 | bt_object_put_no_null_check(messages[i]); |
| 1763 | } |
| 1764 | } |
| 1765 | |
| 1766 | set_self_comp_port_input_msg_iterator_state(iterator, init_state); |
| 1767 | return status; |
| 1768 | } |
| 1769 | |
| 1770 | /* |
| 1771 | * This function is installed as the iterator's next callback after we have |
| 1772 | * auto-seeked (seeked to the beginning and fast-forwarded) to send the |
| 1773 | * messages saved in iterator->auto_seek.msgs. Once this is done, the original |
| 1774 | * next callback is put back. |
| 1775 | */ |
| 1776 | |
| 1777 | static |
| 1778 | enum bt_component_class_message_iterator_next_method_status post_auto_seek_next( |
| 1779 | struct bt_self_component_port_input_message_iterator *iterator, |
| 1780 | bt_message_array_const msgs, uint64_t capacity, |
| 1781 | uint64_t *count) |
| 1782 | { |
| 1783 | BT_ASSERT(!g_queue_is_empty(iterator->auto_seek.msgs)); |
| 1784 | *count = 0; |
| 1785 | |
| 1786 | /* |
| 1787 | * Move auto-seek messages to the output array (which is this |
| 1788 | * iterator's base message array). |
| 1789 | */ |
| 1790 | while (capacity > 0 && !g_queue_is_empty(iterator->auto_seek.msgs)) { |
| 1791 | msgs[*count] = g_queue_pop_head(iterator->auto_seek.msgs); |
| 1792 | capacity--; |
| 1793 | (*count)++; |
| 1794 | } |
| 1795 | |
| 1796 | BT_ASSERT(*count > 0); |
| 1797 | |
| 1798 | if (g_queue_is_empty(iterator->auto_seek.msgs)) { |
| 1799 | /* No more auto-seek messages, restore user's next callback. */ |
| 1800 | BT_ASSERT(iterator->auto_seek.original_next_callback); |
| 1801 | iterator->methods.next = iterator->auto_seek.original_next_callback; |
| 1802 | iterator->auto_seek.original_next_callback = NULL; |
| 1803 | } |
| 1804 | |
| 1805 | return BT_FUNC_STATUS_OK; |
| 1806 | } |
| 1807 | |
| 1808 | static inline |
| 1809 | int clock_raw_value_from_ns_from_origin(const bt_clock_class *clock_class, |
| 1810 | int64_t ns_from_origin, uint64_t *raw_value) |
| 1811 | { |
| 1812 | |
| 1813 | int64_t cc_offset_s = clock_class->offset_seconds; |
| 1814 | uint64_t cc_offset_cycles = clock_class->offset_cycles; |
| 1815 | uint64_t cc_freq = clock_class->frequency; |
| 1816 | |
| 1817 | return bt_common_clock_value_from_ns_from_origin(cc_offset_s, |
| 1818 | cc_offset_cycles, cc_freq, ns_from_origin, raw_value); |
| 1819 | } |
| 1820 | |
| 1821 | |
| 1822 | enum bt_message_iterator_seek_ns_from_origin_status |
| 1823 | bt_self_component_port_input_message_iterator_seek_ns_from_origin( |
| 1824 | struct bt_self_component_port_input_message_iterator *iterator, |
| 1825 | int64_t ns_from_origin) |
| 1826 | { |
| 1827 | int status; |
| 1828 | GHashTable *stream_states = NULL; |
| 1829 | |
| 1830 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
| 1831 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); |
| 1832 | BT_ASSERT_PRE( |
| 1833 | bt_component_borrow_graph(iterator->upstream_component)->config_state != |
| 1834 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, |
| 1835 | "Graph is not configured: %!+g", |
| 1836 | bt_component_borrow_graph(iterator->upstream_component)); |
| 1837 | BT_ASSERT_PRE( |
| 1838 | bt_self_component_port_input_message_iterator_can_seek_ns_from_origin( |
| 1839 | iterator, ns_from_origin), |
| 1840 | "Message iterator cannot seek nanoseconds from origin: %!+i, " |
| 1841 | "ns-from-origin=%" PRId64, iterator, ns_from_origin); |
| 1842 | set_self_comp_port_input_msg_iterator_state(iterator, |
| 1843 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING); |
| 1844 | |
| 1845 | /* |
| 1846 | * We are seeking, reset our expectations about how the following |
| 1847 | * messages should look like. |
| 1848 | */ |
| 1849 | reset_iterator_expectations(iterator); |
| 1850 | |
| 1851 | if (iterator->methods.seek_ns_from_origin) { |
| 1852 | /* The iterator knows how to seek to a particular time, let it handle this. */ |
| 1853 | BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: " |
| 1854 | "%![iter-]+i, ns=%" PRId64, iterator, ns_from_origin); |
| 1855 | status = iterator->methods.seek_ns_from_origin(iterator, |
| 1856 | ns_from_origin); |
| 1857 | BT_LOGD("User method returned: status=%s", |
| 1858 | bt_common_func_status_string(status)); |
| 1859 | BT_ASSERT_POST(status == BT_FUNC_STATUS_OK || |
| 1860 | status == BT_FUNC_STATUS_ERROR || |
| 1861 | status == BT_FUNC_STATUS_MEMORY_ERROR || |
| 1862 | status == BT_FUNC_STATUS_AGAIN, |
| 1863 | "Unexpected status: %![iter-]+i, status=%s", |
| 1864 | iterator, bt_common_func_status_string(status)); |
| 1865 | if (status < 0) { |
| 1866 | BT_LIB_LOGW_APPEND_CAUSE( |
| 1867 | "Component input port message iterator's \"seek nanoseconds from origin\" method failed: " |
| 1868 | "%![iter-]+i, status=%s", |
| 1869 | iterator, bt_common_func_status_string(status)); |
| 1870 | } |
| 1871 | } else { |
| 1872 | /* |
| 1873 | * The iterator doesn't know how to seek to a particular time. We will |
| 1874 | * seek to the beginning and fast forward to the right place. |
| 1875 | */ |
| 1876 | BT_ASSERT(iterator->methods.can_seek_beginning(iterator)); |
| 1877 | BT_ASSERT(iterator->methods.seek_beginning); |
| 1878 | BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", |
| 1879 | iterator); |
| 1880 | status = iterator->methods.seek_beginning(iterator); |
| 1881 | BT_LOGD("User method returned: status=%s", |
| 1882 | bt_common_func_status_string(status)); |
| 1883 | BT_ASSERT_POST(status == BT_FUNC_STATUS_OK || |
| 1884 | status == BT_FUNC_STATUS_ERROR || |
| 1885 | status == BT_FUNC_STATUS_MEMORY_ERROR || |
| 1886 | status == BT_FUNC_STATUS_AGAIN, |
| 1887 | "Unexpected status: %![iter-]+i, status=%s", |
| 1888 | iterator, bt_common_func_status_string(status)); |
| 1889 | if (status < 0) { |
| 1890 | BT_LIB_LOGW_APPEND_CAUSE( |
| 1891 | "Component input port message iterator's \"seek beginning\" method failed: " |
| 1892 | "%![iter-]+i, status=%s", |
| 1893 | iterator, bt_common_func_status_string(status)); |
| 1894 | } |
| 1895 | |
| 1896 | switch (status) { |
| 1897 | case BT_FUNC_STATUS_OK: |
| 1898 | break; |
| 1899 | case BT_FUNC_STATUS_ERROR: |
| 1900 | case BT_FUNC_STATUS_MEMORY_ERROR: |
| 1901 | case BT_FUNC_STATUS_AGAIN: |
| 1902 | goto end; |
| 1903 | default: |
| 1904 | abort(); |
| 1905 | } |
| 1906 | |
| 1907 | /* |
| 1908 | * Find the first message which has a default clock |
| 1909 | * snapshot greater than or equal to the requested |
| 1910 | * seeking time, and move the received messages from |
| 1911 | * this point in the batch to this iterator's auto-seek |
| 1912 | * message queue. |
| 1913 | */ |
| 1914 | while (!g_queue_is_empty(iterator->auto_seek.msgs)) { |
| 1915 | bt_object_put_no_null_check( |
| 1916 | g_queue_pop_tail(iterator->auto_seek.msgs)); |
| 1917 | } |
| 1918 | |
| 1919 | stream_states = create_auto_seek_stream_states(); |
| 1920 | if (!stream_states) { |
| 1921 | BT_LIB_LOGE_APPEND_CAUSE( |
| 1922 | "Failed to allocate one GHashTable."); |
| 1923 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 1924 | goto end; |
| 1925 | } |
| 1926 | |
| 1927 | status = find_message_ge_ns_from_origin(iterator, |
| 1928 | ns_from_origin, stream_states); |
| 1929 | switch (status) { |
| 1930 | case BT_FUNC_STATUS_OK: |
| 1931 | case BT_FUNC_STATUS_END: |
| 1932 | { |
| 1933 | GHashTableIter iter; |
| 1934 | gpointer key, value; |
| 1935 | |
| 1936 | /* |
| 1937 | * If some streams exist at the seek time, prepend the |
| 1938 | * required messages to put those streams in the right |
| 1939 | * state. |
| 1940 | */ |
| 1941 | g_hash_table_iter_init(&iter, stream_states); |
| 1942 | while (g_hash_table_iter_next (&iter, &key, &value)) { |
| 1943 | const bt_stream *stream = key; |
| 1944 | struct auto_seek_stream_state *stream_state = |
| 1945 | (struct auto_seek_stream_state *) value; |
| 1946 | bt_message *msg; |
| 1947 | const bt_clock_class *clock_class = bt_stream_class_borrow_default_clock_class_const( |
| 1948 | bt_stream_borrow_class_const(stream)); |
| 1949 | /* Initialize to silence maybe-uninitialized warning. */ |
| 1950 | uint64_t raw_value = 0; |
| 1951 | |
| 1952 | /* |
| 1953 | * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within |
| 1954 | * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value. |
| 1955 | * |
| 1956 | * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its |
| 1957 | * clock snapshot, because we don't really know if the stream existed at that time. If we have |
| 1958 | * seen a message with a clock snapshot in our seeking, then we are sure that the |
| 1959 | * seek time is not below the clock range, and we know the stream was active at that |
| 1960 | * time (and that we cut it short). |
| 1961 | */ |
| 1962 | if (stream_state->seen_clock_snapshot) { |
| 1963 | if (clock_raw_value_from_ns_from_origin(clock_class, ns_from_origin, &raw_value) != 0) { |
| 1964 | BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64 ", %![cc-]+K", |
| 1965 | ns_from_origin, clock_class); |
| 1966 | status = BT_FUNC_STATUS_ERROR; |
| 1967 | goto end; |
| 1968 | } |
| 1969 | } |
| 1970 | |
| 1971 | switch (stream_state->state) { |
| 1972 | case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN: |
| 1973 | BT_ASSERT(stream_state->packet); |
| 1974 | BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state->packet); |
| 1975 | |
| 1976 | if (stream->class->packets_have_beginning_default_clock_snapshot) { |
| 1977 | /* |
| 1978 | * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning" |
| 1979 | * message. If "packet beginning" packets have clock snapshots, then we must have |
| 1980 | * seen a clock snapshot. |
| 1981 | */ |
| 1982 | BT_ASSERT(stream_state->seen_clock_snapshot); |
| 1983 | |
| 1984 | msg = bt_message_packet_beginning_create_with_default_clock_snapshot( |
| 1985 | (bt_self_message_iterator *) iterator, stream_state->packet, raw_value); |
| 1986 | } else { |
| 1987 | msg = bt_message_packet_beginning_create((bt_self_message_iterator *) iterator, |
| 1988 | stream_state->packet); |
| 1989 | } |
| 1990 | |
| 1991 | if (!msg) { |
| 1992 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 1993 | goto end; |
| 1994 | } |
| 1995 | |
| 1996 | g_queue_push_head(iterator->auto_seek.msgs, msg); |
| 1997 | msg = NULL; |
| 1998 | /* fall-thru */ |
| 1999 | |
| 2000 | case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN: |
| 2001 | msg = bt_message_stream_beginning_create( |
| 2002 | (bt_self_message_iterator *) iterator, stream); |
| 2003 | if (!msg) { |
| 2004 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 2005 | goto end; |
| 2006 | } |
| 2007 | |
| 2008 | if (stream_state->seen_clock_snapshot) { |
| 2009 | bt_message_stream_beginning_set_default_clock_snapshot(msg, raw_value); |
| 2010 | } |
| 2011 | |
| 2012 | g_queue_push_head(iterator->auto_seek.msgs, msg); |
| 2013 | msg = NULL; |
| 2014 | break; |
| 2015 | } |
| 2016 | } |
| 2017 | |
| 2018 | /* |
| 2019 | * If there are messages in the auto-seek |
| 2020 | * message queue, replace the user's "next" |
| 2021 | * method with a custom, temporary "next" method |
| 2022 | * which returns them. |
| 2023 | */ |
| 2024 | if (!g_queue_is_empty(iterator->auto_seek.msgs)) { |
| 2025 | BT_ASSERT(!iterator->auto_seek.original_next_callback); |
| 2026 | iterator->auto_seek.original_next_callback = iterator->methods.next; |
| 2027 | |
| 2028 | iterator->methods.next = |
| 2029 | (bt_self_component_port_input_message_iterator_next_method) |
| 2030 | post_auto_seek_next; |
| 2031 | } |
| 2032 | |
| 2033 | /* |
| 2034 | * `BT_FUNC_STATUS_END` becomes |
| 2035 | * `BT_FUNC_STATUS_OK`: the next |
| 2036 | * time this iterator's "next" method is called, |
| 2037 | * it will return |
| 2038 | * `BT_FUNC_STATUS_END`. |
| 2039 | */ |
| 2040 | status = BT_FUNC_STATUS_OK; |
| 2041 | break; |
| 2042 | } |
| 2043 | case BT_FUNC_STATUS_ERROR: |
| 2044 | case BT_FUNC_STATUS_MEMORY_ERROR: |
| 2045 | case BT_FUNC_STATUS_AGAIN: |
| 2046 | goto end; |
| 2047 | default: |
| 2048 | abort(); |
| 2049 | } |
| 2050 | } |
| 2051 | |
| 2052 | /* |
| 2053 | * The following messages returned by the next method (including |
| 2054 | * post_auto_seek_next) must be after (or at) `ns_from_origin`. |
| 2055 | */ |
| 2056 | iterator->last_ns_from_origin = ns_from_origin; |
| 2057 | |
| 2058 | end: |
| 2059 | if (stream_states) { |
| 2060 | destroy_auto_seek_stream_states(stream_states); |
| 2061 | stream_states = NULL; |
| 2062 | } |
| 2063 | |
| 2064 | set_iterator_state_after_seeking(iterator, status); |
| 2065 | return status; |
| 2066 | } |
| 2067 | |
| 2068 | static inline |
| 2069 | bt_self_component_port_input_message_iterator * |
| 2070 | borrow_output_port_message_iterator_upstream_iterator( |
| 2071 | struct bt_port_output_message_iterator *iterator) |
| 2072 | { |
| 2073 | struct bt_component_class_sink_colander_priv_data *colander_data; |
| 2074 | |
| 2075 | BT_ASSERT(iterator); |
| 2076 | colander_data = (void *) iterator->colander->parent.user_data; |
| 2077 | BT_ASSERT(colander_data); |
| 2078 | BT_ASSERT(colander_data->msg_iter); |
| 2079 | return colander_data->msg_iter; |
| 2080 | } |
| 2081 | |
| 2082 | bt_bool bt_port_output_message_iterator_can_seek_ns_from_origin( |
| 2083 | struct bt_port_output_message_iterator *iterator, |
| 2084 | int64_t ns_from_origin) |
| 2085 | { |
| 2086 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
| 2087 | return bt_self_component_port_input_message_iterator_can_seek_ns_from_origin( |
| 2088 | borrow_output_port_message_iterator_upstream_iterator( |
| 2089 | iterator), ns_from_origin); |
| 2090 | } |
| 2091 | |
| 2092 | bt_bool bt_port_output_message_iterator_can_seek_beginning( |
| 2093 | struct bt_port_output_message_iterator *iterator) |
| 2094 | { |
| 2095 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
| 2096 | return bt_self_component_port_input_message_iterator_can_seek_beginning( |
| 2097 | borrow_output_port_message_iterator_upstream_iterator( |
| 2098 | iterator)); |
| 2099 | } |
| 2100 | |
| 2101 | enum bt_message_iterator_seek_ns_from_origin_status |
| 2102 | bt_port_output_message_iterator_seek_ns_from_origin( |
| 2103 | struct bt_port_output_message_iterator *iterator, |
| 2104 | int64_t ns_from_origin) |
| 2105 | { |
| 2106 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
| 2107 | return bt_self_component_port_input_message_iterator_seek_ns_from_origin( |
| 2108 | borrow_output_port_message_iterator_upstream_iterator(iterator), |
| 2109 | ns_from_origin); |
| 2110 | } |
| 2111 | |
| 2112 | enum bt_message_iterator_seek_beginning_status |
| 2113 | bt_port_output_message_iterator_seek_beginning( |
| 2114 | struct bt_port_output_message_iterator *iterator) |
| 2115 | { |
| 2116 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
| 2117 | return bt_self_component_port_input_message_iterator_seek_beginning( |
| 2118 | borrow_output_port_message_iterator_upstream_iterator( |
| 2119 | iterator)); |
| 2120 | } |
| 2121 | |
| 2122 | void bt_port_output_message_iterator_get_ref( |
| 2123 | const struct bt_port_output_message_iterator *iterator) |
| 2124 | { |
| 2125 | bt_object_get_ref(iterator); |
| 2126 | } |
| 2127 | |
| 2128 | void bt_port_output_message_iterator_put_ref( |
| 2129 | const struct bt_port_output_message_iterator *iterator) |
| 2130 | { |
| 2131 | bt_object_put_ref(iterator); |
| 2132 | } |
| 2133 | |
| 2134 | void bt_self_component_port_input_message_iterator_get_ref( |
| 2135 | const struct bt_self_component_port_input_message_iterator *iterator) |
| 2136 | { |
| 2137 | bt_object_get_ref(iterator); |
| 2138 | } |
| 2139 | |
| 2140 | void bt_self_component_port_input_message_iterator_put_ref( |
| 2141 | const struct bt_self_component_port_input_message_iterator *iterator) |
| 2142 | { |
| 2143 | bt_object_put_ref(iterator); |
| 2144 | } |